1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ASTCommon.h"
14#include "ASTReaderInternals.h"
15#include "TemplateArgumentHasher.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTMutationListener.h"
19#include "clang/AST/ASTStructuralEquivalence.h"
20#include "clang/AST/ASTUnresolvedSet.h"
21#include "clang/AST/AbstractTypeReader.h"
22#include "clang/AST/Attr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/DeclBase.h"
25#include "clang/AST/DeclCXX.h"
26#include "clang/AST/DeclFriend.h"
27#include "clang/AST/DeclGroup.h"
28#include "clang/AST/DeclObjC.h"
29#include "clang/AST/DeclOpenMP.h"
30#include "clang/AST/DeclTemplate.h"
31#include "clang/AST/DeclarationName.h"
32#include "clang/AST/Expr.h"
33#include "clang/AST/ExprCXX.h"
34#include "clang/AST/ExternalASTSource.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/ODRDiagsEmitter.h"
37#include "clang/AST/OpenACCClause.h"
38#include "clang/AST/OpenMPClause.h"
39#include "clang/AST/RawCommentList.h"
40#include "clang/AST/TemplateBase.h"
41#include "clang/AST/TemplateName.h"
42#include "clang/AST/Type.h"
43#include "clang/AST/TypeLoc.h"
44#include "clang/AST/TypeLocVisitor.h"
45#include "clang/AST/UnresolvedSet.h"
46#include "clang/Basic/ASTSourceDescriptor.h"
47#include "clang/Basic/CommentOptions.h"
48#include "clang/Basic/Diagnostic.h"
49#include "clang/Basic/DiagnosticIDs.h"
50#include "clang/Basic/DiagnosticOptions.h"
51#include "clang/Basic/DiagnosticSema.h"
52#include "clang/Basic/FileManager.h"
53#include "clang/Basic/FileSystemOptions.h"
54#include "clang/Basic/IdentifierTable.h"
55#include "clang/Basic/LLVM.h"
56#include "clang/Basic/LangOptions.h"
57#include "clang/Basic/Module.h"
58#include "clang/Basic/ObjCRuntime.h"
59#include "clang/Basic/OpenACCKinds.h"
60#include "clang/Basic/OpenMPKinds.h"
61#include "clang/Basic/OperatorKinds.h"
62#include "clang/Basic/PragmaKinds.h"
63#include "clang/Basic/Sanitizers.h"
64#include "clang/Basic/SourceLocation.h"
65#include "clang/Basic/SourceManager.h"
66#include "clang/Basic/SourceManagerInternals.h"
67#include "clang/Basic/Specifiers.h"
68#include "clang/Basic/TargetInfo.h"
69#include "clang/Basic/TargetOptions.h"
70#include "clang/Basic/TokenKinds.h"
71#include "clang/Basic/Version.h"
72#include "clang/Lex/HeaderSearch.h"
73#include "clang/Lex/HeaderSearchOptions.h"
74#include "clang/Lex/MacroInfo.h"
75#include "clang/Lex/ModuleMap.h"
76#include "clang/Lex/PreprocessingRecord.h"
77#include "clang/Lex/Preprocessor.h"
78#include "clang/Lex/PreprocessorOptions.h"
79#include "clang/Lex/Token.h"
80#include "clang/Sema/ObjCMethodList.h"
81#include "clang/Sema/Scope.h"
82#include "clang/Sema/Sema.h"
83#include "clang/Sema/SemaCUDA.h"
84#include "clang/Sema/SemaObjC.h"
85#include "clang/Sema/SemaOpenMP.h"
86#include "clang/Sema/SemaRISCV.h"
87#include "clang/Sema/Weak.h"
88#include "clang/Serialization/ASTBitCodes.h"
89#include "clang/Serialization/ASTDeserializationListener.h"
90#include "clang/Serialization/ASTRecordReader.h"
91#include "clang/Serialization/ContinuousRangeMap.h"
92#include "clang/Serialization/GlobalModuleIndex.h"
93#include "clang/Serialization/InMemoryModuleCache.h"
94#include "clang/Serialization/ModuleCache.h"
95#include "clang/Serialization/ModuleFile.h"
96#include "clang/Serialization/ModuleFileExtension.h"
97#include "clang/Serialization/ModuleManager.h"
98#include "clang/Serialization/PCHContainerOperations.h"
99#include "clang/Serialization/SerializationDiagnostic.h"
100#include "llvm/ADT/APFloat.h"
101#include "llvm/ADT/APInt.h"
102#include "llvm/ADT/ArrayRef.h"
103#include "llvm/ADT/DenseMap.h"
104#include "llvm/ADT/FoldingSet.h"
105#include "llvm/ADT/IntrusiveRefCntPtr.h"
106#include "llvm/ADT/STLExtras.h"
107#include "llvm/ADT/ScopeExit.h"
108#include "llvm/ADT/Sequence.h"
109#include "llvm/ADT/SmallPtrSet.h"
110#include "llvm/ADT/SmallVector.h"
111#include "llvm/ADT/StringExtras.h"
112#include "llvm/ADT/StringMap.h"
113#include "llvm/ADT/StringRef.h"
114#include "llvm/ADT/iterator_range.h"
115#include "llvm/Bitstream/BitstreamReader.h"
116#include "llvm/Support/Chrono.h"
117#include "llvm/Support/Compiler.h"
118#include "llvm/Support/Compression.h"
119#include "llvm/Support/DJB.h"
120#include "llvm/Support/Endian.h"
121#include "llvm/Support/Error.h"
122#include "llvm/Support/ErrorHandling.h"
123#include "llvm/Support/LEB128.h"
124#include "llvm/Support/MemoryBuffer.h"
125#include "llvm/Support/Path.h"
126#include "llvm/Support/SaveAndRestore.h"
127#include "llvm/Support/TimeProfiler.h"
128#include "llvm/Support/Timer.h"
129#include "llvm/Support/VersionTuple.h"
130#include "llvm/Support/VirtualFileSystem.h"
131#include "llvm/Support/raw_ostream.h"
132#include "llvm/TargetParser/Triple.h"
133#include <algorithm>
134#include <cassert>
135#include <cstddef>
136#include <cstdint>
137#include <cstdio>
138#include <ctime>
139#include <iterator>
140#include <limits>
141#include <map>
142#include <memory>
143#include <optional>
144#include <string>
145#include <system_error>
146#include <tuple>
147#include <utility>
148#include <vector>
149
150using namespace clang;
151using namespace clang::serialization;
152using namespace clang::serialization::reader;
153using llvm::BitstreamCursor;
154
155//===----------------------------------------------------------------------===//
156// ChainedASTReaderListener implementation
157//===----------------------------------------------------------------------===//
158
159bool
160ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
161 return First->ReadFullVersionInformation(FullVersion) ||
162 Second->ReadFullVersionInformation(FullVersion);
163}
164
165void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
166 First->ReadModuleName(ModuleName);
167 Second->ReadModuleName(ModuleName);
168}
169
170void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
171 First->ReadModuleMapFile(ModuleMapPath);
172 Second->ReadModuleMapFile(ModuleMapPath);
173}
174
175bool ChainedASTReaderListener::ReadLanguageOptions(
176 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
181 AllowCompatibleDifferences);
182}
183
184bool ChainedASTReaderListener::ReadCodeGenOptions(
185 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
186 bool AllowCompatibleDifferences) {
187 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
188 AllowCompatibleDifferences) ||
189 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
190 AllowCompatibleDifferences);
191}
192
193bool ChainedASTReaderListener::ReadTargetOptions(
194 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
195 bool AllowCompatibleDifferences) {
196 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
197 AllowCompatibleDifferences) ||
198 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
199 AllowCompatibleDifferences);
200}
201
202bool ChainedASTReaderListener::ReadDiagnosticOptions(
203 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
204 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
205 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
206}
207
208bool
209ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
210 bool Complain) {
211 return First->ReadFileSystemOptions(FSOpts, Complain) ||
212 Second->ReadFileSystemOptions(FSOpts, Complain);
213}
214
215bool ChainedASTReaderListener::ReadHeaderSearchOptions(
216 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
217 StringRef ContextHash, bool Complain) {
218 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
219 Complain) ||
220 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
221 Complain);
222}
223
224bool ChainedASTReaderListener::ReadPreprocessorOptions(
225 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
226 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
227 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
228 Complain, SuggestedPredefines) ||
229 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
230 Complain, SuggestedPredefines);
231}
232
233void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
234 uint32_t Value) {
235 First->ReadCounter(M, Value);
236 Second->ReadCounter(M, Value);
237}
238
239bool ChainedASTReaderListener::needsInputFileVisitation() {
240 return First->needsInputFileVisitation() ||
241 Second->needsInputFileVisitation();
242}
243
244bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
245 return First->needsSystemInputFileVisitation() ||
246 Second->needsSystemInputFileVisitation();
247}
248
249void ChainedASTReaderListener::visitModuleFile(ModuleFileName Filename,
250 ModuleKind Kind,
251 bool DirectlyImported) {
252 First->visitModuleFile(Filename, Kind, DirectlyImported);
253 Second->visitModuleFile(Filename, Kind, DirectlyImported);
254}
255
256bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
257 bool isSystem,
258 bool isOverridden,
259 bool isExplicitModule) {
260 bool Continue = false;
261 if (First->needsInputFileVisitation() &&
262 (!isSystem || First->needsSystemInputFileVisitation()))
263 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
264 isExplicitModule);
265 if (Second->needsInputFileVisitation() &&
266 (!isSystem || Second->needsSystemInputFileVisitation()))
267 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
268 isExplicitModule);
269 return Continue;
270}
271
272void ChainedASTReaderListener::readModuleFileExtension(
273 const ModuleFileExtensionMetadata &Metadata) {
274 First->readModuleFileExtension(Metadata);
275 Second->readModuleFileExtension(Metadata);
276}
277
278//===----------------------------------------------------------------------===//
279// PCH validator implementation
280//===----------------------------------------------------------------------===//
281
282ASTReaderListener::~ASTReaderListener() = default;
283
284static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionFlagMismatch(
285 DiagnosticsEngine *Diags, StringRef Description, bool SerializedValue,
286 bool CurrentValue, StringRef ModuleFilename) {
287 if (!Diags)
288 return true;
289 return Diags->Report(DiagID: diag::err_ast_file_langopt_mismatch)
290 << Description << SerializedValue << CurrentValue << ModuleFilename;
291}
292
293static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionValueMismatch(
294 DiagnosticsEngine *Diags, StringRef Description, StringRef ModuleFilename) {
295 if (!Diags)
296 return true;
297 return Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
298 << Description << ModuleFilename;
299}
300
301/// Compare the given set of language options against an existing set of
302/// language options.
303///
304/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
305/// \param AllowCompatibleDifferences If true, differences between compatible
306/// language options will be permitted.
307///
308/// \returns true if the languagae options mis-match, false otherwise.
309static bool checkLanguageOptions(const LangOptions &LangOpts,
310 const LangOptions &ExistingLangOpts,
311 StringRef ModuleFilename,
312 DiagnosticsEngine *Diags,
313 bool AllowCompatibleDifferences = true) {
314 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
315 using CK = LangOptions::CompatibilityKind;
316
317#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
318 if constexpr (CK::Compatibility != CK::Benign) { \
319 if ((CK::Compatibility == CK::NotCompatible) || \
320 (CK::Compatibility == CK::Compatible && \
321 !AllowCompatibleDifferences)) { \
322 if (ExistingLangOpts.Name != LangOpts.Name) { \
323 if (Bits == 1) \
324 return diagnoseLanguageOptionFlagMismatch( \
325 Diags, Description, LangOpts.Name, ExistingLangOpts.Name, \
326 ModuleFilename); \
327 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
328 ModuleFilename); \
329 } \
330 } \
331 }
332
333#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
334 if constexpr (CK::Compatibility != CK::Benign) { \
335 if ((CK::Compatibility == CK::NotCompatible) || \
336 (CK::Compatibility == CK::Compatible && \
337 !AllowCompatibleDifferences)) { \
338 if (ExistingLangOpts.Name != LangOpts.Name) { \
339 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
340 ModuleFilename); \
341 } \
342 } \
343 }
344
345#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
346 if constexpr (CK::Compatibility != CK::Benign) { \
347 if ((CK::Compatibility == CK::NotCompatible) || \
348 (CK::Compatibility == CK::Compatible && \
349 !AllowCompatibleDifferences)) { \
350 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
351 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
352 ModuleFilename); \
353 } \
354 } \
355 }
356
357#include "clang/Basic/LangOptions.def"
358
359 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
360 return diagnoseLanguageOptionValueMismatch(Diags, Description: "module features",
361 ModuleFilename);
362 }
363
364 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
365 return diagnoseLanguageOptionValueMismatch(
366 Diags, Description: "target Objective-C runtime", ModuleFilename);
367 }
368
369 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
370 LangOpts.CommentOpts.BlockCommandNames) {
371 return diagnoseLanguageOptionValueMismatch(Diags, Description: "block command names",
372 ModuleFilename);
373 }
374
375 // Sanitizer feature mismatches are treated as compatible differences. If
376 // compatible differences aren't allowed, we still only want to check for
377 // mismatches of non-modular sanitizers (the only ones which can affect AST
378 // generation).
379 if (!AllowCompatibleDifferences) {
380 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
381 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
382 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
383 ExistingSanitizers.clear(K: ModularSanitizers);
384 ImportedSanitizers.clear(K: ModularSanitizers);
385 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
386 const std::string Flag = "-fsanitize=";
387 if (Diags) {
388#define SANITIZER(NAME, ID) \
389 { \
390 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
391 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
392 if (InExistingModule != InImportedModule) \
393 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
394 << InExistingModule << ModuleFilename << (Flag + NAME); \
395 }
396#include "clang/Basic/Sanitizers.def"
397 }
398 return true;
399 }
400 }
401
402 return false;
403}
404
405static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
406 const CodeGenOptions &ExistingCGOpts,
407 StringRef ModuleFilename,
408 DiagnosticsEngine *Diags,
409 bool AllowCompatibleDifferences = true) {
410 // FIXME: Specify and print a description for each option instead of the name.
411 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
412 using CK = CodeGenOptions::CompatibilityKind;
413#define CODEGENOPT(Name, Bits, Default, Compatibility) \
414 if constexpr (CK::Compatibility != CK::Benign) { \
415 if ((CK::Compatibility == CK::NotCompatible) || \
416 (CK::Compatibility == CK::Compatible && \
417 !AllowCompatibleDifferences)) { \
418 if (ExistingCGOpts.Name != CGOpts.Name) { \
419 if (Diags) { \
420 if (Bits == 1) \
421 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
422 << #Name << CGOpts.Name << ExistingCGOpts.Name \
423 << ModuleFilename; \
424 else \
425 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
426 << #Name << ModuleFilename; \
427 } \
428 return true; \
429 } \
430 } \
431 }
432
433#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
434 if constexpr (CK::Compatibility != CK::Benign) { \
435 if ((CK::Compatibility == CK::NotCompatible) || \
436 (CK::Compatibility == CK::Compatible && \
437 !AllowCompatibleDifferences)) { \
438 if (ExistingCGOpts.Name != CGOpts.Name) { \
439 if (Diags) \
440 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
441 << #Name << ModuleFilename; \
442 return true; \
443 } \
444 } \
445 }
446#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
447 if constexpr (CK::Compatibility != CK::Benign) { \
448 if ((CK::Compatibility == CK::NotCompatible) || \
449 (CK::Compatibility == CK::Compatible && \
450 !AllowCompatibleDifferences)) { \
451 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
452 if (Diags) \
453 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
454 << #Name << ModuleFilename; \
455 return true; \
456 } \
457 } \
458 }
459#define DEBUGOPT(Name, Bits, Default, Compatibility)
460#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
461#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
462#include "clang/Basic/CodeGenOptions.def"
463
464 return false;
465}
466
467static std::vector<std::string>
468accumulateFeaturesAsWritten(std::vector<std::string> FeaturesAsWritten) {
469 llvm::erase_if(C&: FeaturesAsWritten, P: [](const std::string &S) {
470 return S.empty() || (S[0] != '+' && S[0] != '-');
471 });
472 llvm::stable_sort(Range&: FeaturesAsWritten,
473 C: [](const std::string &A, const std::string &B) {
474 return A.substr(pos: 1) < B.substr(pos: 1);
475 });
476 auto NewRend =
477 std::unique(first: FeaturesAsWritten.rbegin(), last: FeaturesAsWritten.rend(),
478 binary_pred: [](const std::string &A, const std::string &B) {
479 return A.substr(pos: 1) == B.substr(pos: 1);
480 });
481 // Because we are operating on reverse iterators, the duplicate elements
482 // are actually at the beginning.
483 FeaturesAsWritten.erase(first: FeaturesAsWritten.begin(), last: NewRend.base());
484 return FeaturesAsWritten;
485}
486
487/// Compare the given set of target options against an existing set of
488/// target options.
489///
490/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
491///
492/// \returns true if the target options mis-match, false otherwise.
493static bool checkTargetOptions(const TargetOptions &TargetOpts,
494 const TargetOptions &ExistingTargetOpts,
495 StringRef ModuleFilename,
496 DiagnosticsEngine *Diags,
497 bool AllowCompatibleDifferences = true) {
498#define CHECK_TARGET_OPT(Field, Name) \
499 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
500 if (Diags) \
501 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
502 << ModuleFilename << Name << TargetOpts.Field \
503 << ExistingTargetOpts.Field; \
504 return true; \
505 }
506
507 // The triple and ABI must match exactly.
508 CHECK_TARGET_OPT(Triple, "target");
509 CHECK_TARGET_OPT(ABI, "target ABI");
510
511 // We can tolerate different CPUs in many cases, notably when one CPU
512 // supports a strict superset of another. When allowing compatible
513 // differences skip this check.
514 if (!AllowCompatibleDifferences) {
515 CHECK_TARGET_OPT(CPU, "target CPU");
516 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
517 }
518
519#undef CHECK_TARGET_OPT
520
521 // Compare feature sets.
522 // Alternatively, we could be diffing TargetOpts.Features, but that would
523 // clutter the output with implied features.
524 std::vector<std::string> ExistingFeatures =
525 accumulateFeaturesAsWritten(FeaturesAsWritten: ExistingTargetOpts.FeaturesAsWritten);
526 std::vector<std::string> ReadFeatures =
527 accumulateFeaturesAsWritten(FeaturesAsWritten: TargetOpts.FeaturesAsWritten);
528
529 // We compute the set difference in both directions explicitly so that we can
530 // diagnose the differences differently.
531 auto FeatureLess = [](StringRef A, StringRef B) {
532 return A.substr(Start: 1) < B.substr(Start: 1);
533 };
534
535 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
536 std::set_difference(first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(),
537 first2: ReadFeatures.begin(), last2: ReadFeatures.end(),
538 result: std::back_inserter(x&: UnmatchedExistingFeatures),
539 comp: FeatureLess);
540 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
541 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
542 result: std::back_inserter(x&: UnmatchedReadFeatures), comp: FeatureLess);
543
544 // If we are allowing compatible differences and the read feature set is
545 // a strict subset of the existing feature set, there is nothing to diagnose.
546 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
547 return false;
548
549 if (Diags) {
550 for (StringRef Feature : UnmatchedReadFeatures)
551 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
552 << /* is-existing-feature */ false << ModuleFilename << Feature;
553 for (StringRef Feature : UnmatchedExistingFeatures)
554 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
555 << /* is-existing-feature */ true << ModuleFilename << Feature;
556 }
557
558 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
559}
560
561bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
562 StringRef ModuleFilename, bool Complain,
563 bool AllowCompatibleDifferences) {
564 const LangOptions &ExistingLangOpts = PP.getLangOpts();
565 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
566 Diags: Complain ? &Reader.Diags : nullptr,
567 AllowCompatibleDifferences);
568}
569
570bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
571 StringRef ModuleFilename, bool Complain,
572 bool AllowCompatibleDifferences) {
573 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
574 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
575 Diags: Complain ? &Reader.Diags : nullptr,
576 AllowCompatibleDifferences);
577}
578
579bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
580 StringRef ModuleFilename, bool Complain,
581 bool AllowCompatibleDifferences) {
582 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
583 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
584 Diags: Complain ? &Reader.Diags : nullptr,
585 AllowCompatibleDifferences);
586}
587
588namespace {
589
590using MacroDefinitionsMap =
591 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
592
593class DeclsSet {
594 SmallVector<NamedDecl *, 64> Decls;
595 llvm::SmallPtrSet<NamedDecl *, 8> Found;
596
597public:
598 operator ArrayRef<NamedDecl *>() const { return Decls; }
599
600 bool empty() const { return Decls.empty(); }
601
602 bool insert(NamedDecl *ND) {
603 auto [_, Inserted] = Found.insert(Ptr: ND);
604 if (Inserted)
605 Decls.push_back(Elt: ND);
606 return Inserted;
607 }
608};
609
610using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
611
612} // namespace
613
614static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
615 DiagnosticsEngine &Diags,
616 StringRef ModuleFilename,
617 bool Complain) {
618 using Level = DiagnosticsEngine::Level;
619
620 // Check current mappings for new -Werror mappings, and the stored mappings
621 // for cases that were explicitly mapped to *not* be errors that are now
622 // errors because of options like -Werror.
623 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
624
625 for (DiagnosticsEngine *MappingSource : MappingSources) {
626 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
627 diag::kind DiagID = DiagIDMappingPair.first;
628 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
629 if (CurLevel < DiagnosticsEngine::Error)
630 continue; // not significant
631 Level StoredLevel =
632 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
633 if (StoredLevel < DiagnosticsEngine::Error) {
634 if (Complain)
635 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
636 << "-Werror=" + Diags.getDiagnosticIDs()
637 ->getWarningOptionForDiag(DiagID)
638 .str()
639 << ModuleFilename;
640 return true;
641 }
642 }
643 }
644
645 return false;
646}
647
648static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
649 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
650 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
651 return true;
652 return Ext >= diag::Severity::Error;
653}
654
655static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
656 DiagnosticsEngine &Diags,
657 StringRef ModuleFilename, bool IsSystem,
658 bool SystemHeaderWarningsInModule,
659 bool Complain) {
660 // Top-level options
661 if (IsSystem) {
662 if (Diags.getSuppressSystemWarnings())
663 return false;
664 // If -Wsystem-headers was not enabled before, and it was not explicit,
665 // be conservative
666 if (StoredDiags.getSuppressSystemWarnings() &&
667 !SystemHeaderWarningsInModule) {
668 if (Complain)
669 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
670 << "-Wsystem-headers" << ModuleFilename;
671 return true;
672 }
673 }
674
675 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
676 if (Complain)
677 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
678 << "-Werror" << ModuleFilename;
679 return true;
680 }
681
682 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
683 !StoredDiags.getEnableAllWarnings()) {
684 if (Complain)
685 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
686 << "-Weverything -Werror" << ModuleFilename;
687 return true;
688 }
689
690 if (isExtHandlingFromDiagsError(Diags) &&
691 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
692 if (Complain)
693 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
694 << "-pedantic-errors" << ModuleFilename;
695 return true;
696 }
697
698 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
699 Complain);
700}
701
702/// Return the top import module if it is implicit, nullptr otherwise.
703static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
704 Preprocessor &PP) {
705 // If the original import came from a file explicitly generated by the user,
706 // don't check the diagnostic mappings.
707 // FIXME: currently this is approximated by checking whether this is not a
708 // module import of an implicitly-loaded module file.
709 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
710 // the transitive closure of its imports, since unrelated modules cannot be
711 // imported until after this module finishes validation.
712 ModuleFile *TopImport = &*ModuleMgr.rbegin();
713 while (!TopImport->ImportedBy.empty())
714 TopImport = TopImport->ImportedBy[0];
715 if (TopImport->Kind != MK_ImplicitModule)
716 return nullptr;
717
718 StringRef ModuleName = TopImport->ModuleName;
719 assert(!ModuleName.empty() && "diagnostic options read before module name");
720
721 Module *M =
722 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
723 assert(M && "missing module");
724 return M;
725}
726
727bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
728 StringRef ModuleFilename,
729 bool Complain) {
730 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
731 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
732 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
733 // This should never fail, because we would have processed these options
734 // before writing them to an ASTFile.
735 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
736 VFS&: PP.getFileManager().getVirtualFileSystem(),
737 /*Report*/ ReportDiags: false);
738
739 ModuleManager &ModuleMgr = Reader.getModuleManager();
740 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
741
742 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
743 if (!TopM)
744 return false;
745
746 Module *Importer = PP.getCurrentModule();
747
748 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
749 bool SystemHeaderWarningsInModule =
750 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
751 Element: Importer->Name);
752
753 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
754 // contains the union of their flags.
755 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
756 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
757 Complain);
758}
759
760/// Collect the macro definitions provided by the given preprocessor
761/// options.
762static void
763collectMacroDefinitions(const PreprocessorOptions &PPOpts,
764 MacroDefinitionsMap &Macros,
765 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
766 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
767 StringRef Macro = PPOpts.Macros[I].first;
768 bool IsUndef = PPOpts.Macros[I].second;
769
770 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
771 StringRef MacroName = MacroPair.first;
772 StringRef MacroBody = MacroPair.second;
773
774 // For an #undef'd macro, we only care about the name.
775 if (IsUndef) {
776 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
777 if (MacroNames && Inserted)
778 MacroNames->push_back(Elt: MacroName);
779
780 It->second = std::make_pair(x: "", y: true);
781 continue;
782 }
783
784 // For a #define'd macro, figure out the actual definition.
785 if (MacroName.size() == Macro.size())
786 MacroBody = "1";
787 else {
788 // Note: GCC drops anything following an end-of-line character.
789 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
790 MacroBody = MacroBody.substr(Start: 0, N: End);
791 }
792
793 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
794 if (MacroNames && Inserted)
795 MacroNames->push_back(Elt: MacroName);
796 It->second = std::make_pair(x&: MacroBody, y: false);
797 }
798}
799
800enum OptionValidation {
801 OptionValidateNone,
802 OptionValidateContradictions,
803 OptionValidateStrictMatches,
804};
805
806/// Check the preprocessor options deserialized from the control block
807/// against the preprocessor options in an existing preprocessor.
808///
809/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
810/// \param Validation If set to OptionValidateNone, ignore differences in
811/// preprocessor options. If set to OptionValidateContradictions,
812/// require that options passed both in the AST file and on the command
813/// line (-D or -U) match, but tolerate options missing in one or the
814/// other. If set to OptionValidateContradictions, require that there
815/// are no differences in the options between the two.
816static bool checkPreprocessorOptions(
817 const PreprocessorOptions &PPOpts,
818 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
819 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
820 std::string &SuggestedPredefines, const LangOptions &LangOpts,
821 OptionValidation Validation = OptionValidateContradictions) {
822 if (ReadMacros) {
823 // Check macro definitions.
824 MacroDefinitionsMap ASTFileMacros;
825 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
826 MacroDefinitionsMap ExistingMacros;
827 SmallVector<StringRef, 4> ExistingMacroNames;
828 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
829 MacroNames: &ExistingMacroNames);
830
831 // Use a line marker to enter the <command line> file, as the defines and
832 // undefines here will have come from the command line.
833 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
834
835 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
836 // Dig out the macro definition in the existing preprocessor options.
837 StringRef MacroName = ExistingMacroNames[I];
838 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
839
840 // Check whether we know anything about this macro name or not.
841 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
842 ASTFileMacros.find(Key: MacroName);
843 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
844 if (Validation == OptionValidateStrictMatches) {
845 // If strict matches are requested, don't tolerate any extra defines
846 // on the command line that are missing in the AST file.
847 if (Diags) {
848 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
849 << MacroName << true << ModuleFilename;
850 }
851 return true;
852 }
853 // FIXME: Check whether this identifier was referenced anywhere in the
854 // AST file. If so, we should reject the AST file. Unfortunately, this
855 // information isn't in the control block. What shall we do about it?
856
857 if (Existing.second) {
858 SuggestedPredefines += "#undef ";
859 SuggestedPredefines += MacroName.str();
860 SuggestedPredefines += '\n';
861 } else {
862 SuggestedPredefines += "#define ";
863 SuggestedPredefines += MacroName.str();
864 SuggestedPredefines += ' ';
865 SuggestedPredefines += Existing.first.str();
866 SuggestedPredefines += '\n';
867 }
868 continue;
869 }
870
871 // If the macro was defined in one but undef'd in the other, we have a
872 // conflict.
873 if (Existing.second != Known->second.second) {
874 if (Diags) {
875 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
876 << MacroName << Known->second.second << ModuleFilename;
877 }
878 return true;
879 }
880
881 // If the macro was #undef'd in both, or if the macro bodies are
882 // identical, it's fine.
883 if (Existing.second || Existing.first == Known->second.first) {
884 ASTFileMacros.erase(I: Known);
885 continue;
886 }
887
888 // The macro bodies differ; complain.
889 if (Diags) {
890 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
891 << MacroName << Known->second.first << Existing.first
892 << ModuleFilename;
893 }
894 return true;
895 }
896
897 // Leave the <command line> file and return to <built-in>.
898 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
899
900 if (Validation == OptionValidateStrictMatches) {
901 // If strict matches are requested, don't tolerate any extra defines in
902 // the AST file that are missing on the command line.
903 for (const auto &MacroName : ASTFileMacros.keys()) {
904 if (Diags) {
905 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
906 << MacroName << false << ModuleFilename;
907 }
908 return true;
909 }
910 }
911 }
912
913 // Check whether we're using predefines.
914 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
915 Validation != OptionValidateNone) {
916 if (Diags) {
917 Diags->Report(DiagID: diag::err_ast_file_undef)
918 << ExistingPPOpts.UsePredefines << ModuleFilename;
919 }
920 return true;
921 }
922
923 // Detailed record is important since it is used for the module cache hash.
924 if (LangOpts.Modules &&
925 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
926 Validation != OptionValidateNone) {
927 if (Diags) {
928 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
929 << PPOpts.DetailedRecord << ModuleFilename;
930 }
931 return true;
932 }
933
934 // Compute the #include and #include_macros lines we need.
935 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
936 StringRef File = ExistingPPOpts.MacroIncludes[I];
937 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
938 continue;
939
940 SuggestedPredefines += "#__include_macros \"";
941 SuggestedPredefines += File;
942 SuggestedPredefines += "\"\n##\n";
943 }
944
945 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
946 StringRef File = ExistingPPOpts.Includes[I];
947
948 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
949 !ExistingPPOpts.PCHThroughHeader.empty()) {
950 // In case the through header is an include, we must add all the includes
951 // to the predefines so the start point can be determined.
952 SuggestedPredefines += "#include \"";
953 SuggestedPredefines += File;
954 SuggestedPredefines += "\"\n";
955 continue;
956 }
957
958 if (File == ExistingPPOpts.ImplicitPCHInclude)
959 continue;
960
961 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
962 continue;
963
964 SuggestedPredefines += "#include \"";
965 SuggestedPredefines += File;
966 SuggestedPredefines += "\"\n";
967 }
968
969 return false;
970}
971
972bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
973 StringRef ModuleFilename,
974 bool ReadMacros, bool Complain,
975 std::string &SuggestedPredefines) {
976 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
977
978 return checkPreprocessorOptions(
979 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
980 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
981 SuggestedPredefines, LangOpts: PP.getLangOpts());
982}
983
984bool SimpleASTReaderListener::ReadPreprocessorOptions(
985 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
986 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
987 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
988 ModuleFilename, ReadMacros, Diags: nullptr,
989 FileMgr&: PP.getFileManager(), SuggestedPredefines,
990 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
991}
992
993/// Check that the specified and the existing module cache paths are equivalent.
994///
995/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
996/// \returns true when the module cache paths differ.
997static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
998 StringRef ExistingSpecificModuleCachePath,
999 StringRef ASTFilename,
1000 DiagnosticsEngine *Diags,
1001 const LangOptions &LangOpts,
1002 const PreprocessorOptions &PPOpts,
1003 const HeaderSearchOptions &HSOpts,
1004 const HeaderSearchOptions &ASTFileHSOpts) {
1005 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
1006 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
1007 ContextHash: std::string(ContextHash));
1008
1009 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
1010 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
1011 return false;
1012 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1013 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
1014 if (EqualOrErr && *EqualOrErr)
1015 return false;
1016 if (Diags) {
1017 // If the module cache arguments provided from the command line are the
1018 // same, the mismatch must come from other arguments of the configuration
1019 // and not directly the cache path.
1020 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1021 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
1022 if (EqualOrErr && *EqualOrErr)
1023 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
1024 else
1025 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
1026 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
1027 << ASTFilename;
1028 }
1029 return true;
1030}
1031
1032bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1033 StringRef ASTFilename,
1034 StringRef ContextHash,
1035 bool Complain) {
1036 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1037 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1038 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1039 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1040 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1041 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1042}
1043
1044void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1045 PP.setCounterValue(Value);
1046}
1047
1048//===----------------------------------------------------------------------===//
1049// AST reader implementation
1050//===----------------------------------------------------------------------===//
1051
1052static uint64_t readULEB(const unsigned char *&P) {
1053 unsigned Length = 0;
1054 const char *Error = nullptr;
1055
1056 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1057 if (Error)
1058 llvm::report_fatal_error(reason: Error);
1059 P += Length;
1060 return Val;
1061}
1062
1063/// Read ULEB-encoded key length and data length.
1064static std::pair<unsigned, unsigned>
1065readULEBKeyDataLength(const unsigned char *&P) {
1066 unsigned KeyLen = readULEB(P);
1067 if ((unsigned)KeyLen != KeyLen)
1068 llvm::report_fatal_error(reason: "key too large");
1069
1070 unsigned DataLen = readULEB(P);
1071 if ((unsigned)DataLen != DataLen)
1072 llvm::report_fatal_error(reason: "data too large");
1073
1074 return std::make_pair(x&: KeyLen, y&: DataLen);
1075}
1076
1077void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1078 bool TakeOwnership) {
1079 DeserializationListener = Listener;
1080 OwnsDeserializationListener = TakeOwnership;
1081}
1082
1083unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1084 return serialization::ComputeHash(Sel);
1085}
1086
1087LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1088 LocalDeclID ID(Value);
1089#ifndef NDEBUG
1090 if (!MF.ModuleOffsetMap.empty())
1091 Reader.ReadModuleOffsetMap(MF);
1092
1093 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1094 unsigned LocalDeclID = ID.getLocalDeclIndex();
1095
1096 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1097
1098 ModuleFile *OwningModuleFile =
1099 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1100 assert(OwningModuleFile);
1101
1102 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1103
1104 if (!ModuleFileIndex)
1105 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1106
1107 assert(LocalDeclID < LocalNumDecls);
1108#endif
1109 (void)Reader;
1110 (void)MF;
1111 return ID;
1112}
1113
1114LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1115 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1116 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1117 return LocalDeclID::get(Reader, MF, Value);
1118}
1119
1120std::pair<unsigned, unsigned>
1121ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1122 return readULEBKeyDataLength(P&: d);
1123}
1124
1125ASTSelectorLookupTrait::internal_key_type
1126ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1127 using namespace llvm::support;
1128
1129 SelectorTable &SelTable = Reader.getContext().Selectors;
1130 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1131 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1132 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1133 if (N == 0)
1134 return SelTable.getNullarySelector(ID: FirstII);
1135 else if (N == 1)
1136 return SelTable.getUnarySelector(ID: FirstII);
1137
1138 SmallVector<const IdentifierInfo *, 16> Args;
1139 Args.push_back(Elt: FirstII);
1140 for (unsigned I = 1; I != N; ++I)
1141 Args.push_back(Elt: Reader.getLocalIdentifier(
1142 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1143
1144 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1145}
1146
1147ASTSelectorLookupTrait::data_type
1148ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1149 unsigned DataLen) {
1150 using namespace llvm::support;
1151
1152 data_type Result;
1153
1154 Result.ID = Reader.getGlobalSelectorID(
1155 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1156 unsigned FullInstanceBits =
1157 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1158 unsigned FullFactoryBits =
1159 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1160 Result.InstanceBits = FullInstanceBits & 0x3;
1161 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1162 Result.FactoryBits = FullFactoryBits & 0x3;
1163 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1164 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1165 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1166
1167 // Load instance methods
1168 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1169 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1170 F, LocalID: LocalDeclID::get(
1171 Reader, MF&: F,
1172 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1173 Result.Instance.push_back(Elt: Method);
1174 }
1175
1176 // Load factory methods
1177 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1178 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1179 F, LocalID: LocalDeclID::get(
1180 Reader, MF&: F,
1181 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1182 Result.Factory.push_back(Elt: Method);
1183 }
1184
1185 return Result;
1186}
1187
1188unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1189 return llvm::djbHash(Buffer: a);
1190}
1191
1192std::pair<unsigned, unsigned>
1193ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1194 return readULEBKeyDataLength(P&: d);
1195}
1196
1197ASTIdentifierLookupTraitBase::internal_key_type
1198ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1199 assert(n >= 2 && d[n-1] == '\0');
1200 return StringRef((const char*) d, n-1);
1201}
1202
1203/// Whether the given identifier is "interesting".
1204static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1205 bool IsModule) {
1206 bool IsInteresting =
1207 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1208 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1209 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1210 return II.hadMacroDefinition() || II.isPoisoned() ||
1211 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1212 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1213 II.getFETokenInfo());
1214}
1215
1216static bool readBit(unsigned &Bits) {
1217 bool Value = Bits & 0x1;
1218 Bits >>= 1;
1219 return Value;
1220}
1221
1222IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1223 using namespace llvm::support;
1224
1225 IdentifierID RawID =
1226 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1227 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1228}
1229
1230static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1231 bool IsModule) {
1232 if (!II.isFromAST()) {
1233 II.setIsFromAST();
1234 if (isInterestingIdentifier(Reader, II, IsModule))
1235 II.setChangedSinceDeserialization();
1236 }
1237}
1238
1239IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1240 const unsigned char* d,
1241 unsigned DataLen) {
1242 using namespace llvm::support;
1243
1244 IdentifierID RawID =
1245 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1246 bool IsInteresting = RawID & 0x01;
1247
1248 DataLen -= sizeof(IdentifierID);
1249
1250 // Wipe out the "is interesting" bit.
1251 RawID = RawID >> 1;
1252
1253 // Build the IdentifierInfo and link the identifier ID with it.
1254 IdentifierInfo *II = KnownII;
1255 if (!II) {
1256 II = &Reader.getIdentifierTable().getOwn(Name: k);
1257 KnownII = II;
1258 }
1259 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1260 markIdentifierFromAST(Reader, II&: *II, IsModule);
1261 Reader.markIdentifierUpToDate(II);
1262
1263 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1264 if (!IsInteresting) {
1265 // For uninteresting identifiers, there's nothing else to do. Just notify
1266 // the reader that we've finished loading this identifier.
1267 Reader.SetIdentifierInfo(ID, II);
1268 return II;
1269 }
1270
1271 unsigned ObjCOrBuiltinID =
1272 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1273 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1274 bool CPlusPlusOperatorKeyword = readBit(Bits);
1275 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1276 bool Poisoned = readBit(Bits);
1277 bool ExtensionToken = readBit(Bits);
1278 bool HasMacroDefinition = readBit(Bits);
1279
1280 assert(Bits == 0 && "Extra bits in the identifier?");
1281 DataLen -= sizeof(uint16_t) * 2;
1282
1283 // Set or check the various bits in the IdentifierInfo structure.
1284 // Token IDs are read-only.
1285 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1286 II->revertTokenIDToIdentifier();
1287 if (!F.isModule())
1288 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1289 assert(II->isExtensionToken() == ExtensionToken &&
1290 "Incorrect extension token flag");
1291 (void)ExtensionToken;
1292 if (Poisoned)
1293 II->setIsPoisoned(true);
1294 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1295 "Incorrect C++ operator keyword flag");
1296 (void)CPlusPlusOperatorKeyword;
1297
1298 // If this identifier has a macro definition, deserialize it or notify the
1299 // visitor the actual definition is in a different module.
1300 if (HasMacroDefinition) {
1301 uint32_t MacroDirectivesOffset =
1302 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1303 DataLen -= 4;
1304
1305 if (MacroDirectivesOffset)
1306 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1307 else
1308 hasMacroDefinitionInDependencies = true;
1309 }
1310
1311 Reader.SetIdentifierInfo(ID, II);
1312
1313 // Read all of the declarations visible at global scope with this
1314 // name.
1315 if (DataLen > 0) {
1316 SmallVector<GlobalDeclID, 4> DeclIDs;
1317 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1318 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1319 F, LocalID: LocalDeclID::get(
1320 Reader, MF&: F,
1321 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1322 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1323 }
1324
1325 return II;
1326}
1327
1328DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1329 : Kind(Name.getNameKind()) {
1330 switch (Kind) {
1331 case DeclarationName::Identifier:
1332 Data = (uint64_t)Name.getAsIdentifierInfo();
1333 break;
1334 case DeclarationName::ObjCZeroArgSelector:
1335 case DeclarationName::ObjCOneArgSelector:
1336 case DeclarationName::ObjCMultiArgSelector:
1337 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1338 break;
1339 case DeclarationName::CXXOperatorName:
1340 Data = Name.getCXXOverloadedOperator();
1341 break;
1342 case DeclarationName::CXXLiteralOperatorName:
1343 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1344 break;
1345 case DeclarationName::CXXDeductionGuideName:
1346 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1347 ->getDeclName().getAsIdentifierInfo();
1348 break;
1349 case DeclarationName::CXXConstructorName:
1350 case DeclarationName::CXXDestructorName:
1351 case DeclarationName::CXXConversionFunctionName:
1352 case DeclarationName::CXXUsingDirective:
1353 Data = 0;
1354 break;
1355 }
1356}
1357
1358unsigned DeclarationNameKey::getHash() const {
1359 llvm::FoldingSetNodeID ID;
1360 ID.AddInteger(I: Kind);
1361
1362 switch (Kind) {
1363 case DeclarationName::Identifier:
1364 case DeclarationName::CXXLiteralOperatorName:
1365 case DeclarationName::CXXDeductionGuideName:
1366 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1367 break;
1368 case DeclarationName::ObjCZeroArgSelector:
1369 case DeclarationName::ObjCOneArgSelector:
1370 case DeclarationName::ObjCMultiArgSelector:
1371 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1372 break;
1373 case DeclarationName::CXXOperatorName:
1374 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1375 break;
1376 case DeclarationName::CXXConstructorName:
1377 case DeclarationName::CXXDestructorName:
1378 case DeclarationName::CXXConversionFunctionName:
1379 case DeclarationName::CXXUsingDirective:
1380 break;
1381 }
1382
1383 return ID.computeStableHash();
1384}
1385
1386ModuleFile *
1387ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1388 using namespace llvm::support;
1389
1390 uint32_t ModuleFileID =
1391 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1392 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1393}
1394
1395std::pair<unsigned, unsigned>
1396ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1397 return readULEBKeyDataLength(P&: d);
1398}
1399
1400DeclarationNameKey
1401ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1402 using namespace llvm::support;
1403
1404 auto Kind = (DeclarationName::NameKind)*d++;
1405 uint64_t Data;
1406 switch (Kind) {
1407 case DeclarationName::Identifier:
1408 case DeclarationName::CXXLiteralOperatorName:
1409 case DeclarationName::CXXDeductionGuideName:
1410 Data = (uint64_t)Reader.getLocalIdentifier(
1411 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1412 break;
1413 case DeclarationName::ObjCZeroArgSelector:
1414 case DeclarationName::ObjCOneArgSelector:
1415 case DeclarationName::ObjCMultiArgSelector:
1416 Data = (uint64_t)Reader
1417 .getLocalSelector(
1418 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1419 .getAsOpaquePtr();
1420 break;
1421 case DeclarationName::CXXOperatorName:
1422 Data = *d++; // OverloadedOperatorKind
1423 break;
1424 case DeclarationName::CXXConstructorName:
1425 case DeclarationName::CXXDestructorName:
1426 case DeclarationName::CXXConversionFunctionName:
1427 case DeclarationName::CXXUsingDirective:
1428 Data = 0;
1429 break;
1430 }
1431
1432 return DeclarationNameKey(Kind, Data);
1433}
1434
1435ASTDeclContextNameLookupTrait::internal_key_type
1436ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1437 return ReadKeyBase(d);
1438}
1439
1440void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1441 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1442 using namespace llvm::support;
1443
1444 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1445 LocalDeclID ID = LocalDeclID::get(
1446 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1447 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1448 }
1449}
1450
1451void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1452 const unsigned char *d,
1453 unsigned DataLen,
1454 data_type_builder &Val) {
1455 ReadDataIntoImpl(d, DataLen, Val);
1456}
1457
1458ModuleLocalNameLookupTrait::hash_value_type
1459ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1460 llvm::FoldingSetNodeID ID;
1461 ID.AddInteger(I: Key.first.getHash());
1462 ID.AddInteger(I: Key.second);
1463 return ID.computeStableHash();
1464}
1465
1466ModuleLocalNameLookupTrait::internal_key_type
1467ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1468 DeclarationNameKey Name(Key.first);
1469
1470 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1471 if (!ModuleHash)
1472 return {Name, 0};
1473
1474 return {Name, *ModuleHash};
1475}
1476
1477ModuleLocalNameLookupTrait::internal_key_type
1478ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1479 DeclarationNameKey Name = ReadKeyBase(d);
1480 unsigned PrimaryModuleHash =
1481 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1482 return {Name, PrimaryModuleHash};
1483}
1484
1485void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1486 const unsigned char *d,
1487 unsigned DataLen,
1488 data_type_builder &Val) {
1489 ReadDataIntoImpl(d, DataLen, Val);
1490}
1491
1492ModuleFile *
1493LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1494 using namespace llvm::support;
1495
1496 uint32_t ModuleFileID =
1497 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1498 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1499}
1500
1501LazySpecializationInfoLookupTrait::internal_key_type
1502LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1503 using namespace llvm::support;
1504 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1505}
1506
1507std::pair<unsigned, unsigned>
1508LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1509 return readULEBKeyDataLength(P&: d);
1510}
1511
1512void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1513 const unsigned char *d,
1514 unsigned DataLen,
1515 data_type_builder &Val) {
1516 using namespace llvm::support;
1517
1518 for (unsigned NumDecls =
1519 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1520 NumDecls; --NumDecls) {
1521 LocalDeclID LocalID = LocalDeclID::get(
1522 Reader, MF&: F,
1523 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1524 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1525 }
1526}
1527
1528bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1529 BitstreamCursor &Cursor,
1530 uint64_t Offset,
1531 DeclContext *DC) {
1532 assert(Offset != 0);
1533
1534 SavedStreamPosition SavedPosition(Cursor);
1535 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1536 Error(Err: std::move(Err));
1537 return true;
1538 }
1539
1540 RecordData Record;
1541 StringRef Blob;
1542 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1543 if (!MaybeCode) {
1544 Error(Err: MaybeCode.takeError());
1545 return true;
1546 }
1547 unsigned Code = MaybeCode.get();
1548
1549 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1550 if (!MaybeRecCode) {
1551 Error(Err: MaybeRecCode.takeError());
1552 return true;
1553 }
1554 unsigned RecCode = MaybeRecCode.get();
1555 if (RecCode != DECL_CONTEXT_LEXICAL) {
1556 Error(Msg: "Expected lexical block");
1557 return true;
1558 }
1559
1560 assert(!isa<TranslationUnitDecl>(DC) &&
1561 "expected a TU_UPDATE_LEXICAL record for TU");
1562 // If we are handling a C++ class template instantiation, we can see multiple
1563 // lexical updates for the same record. It's important that we select only one
1564 // of them, so that field numbering works properly. Just pick the first one we
1565 // see.
1566 auto &Lex = LexicalDecls[DC];
1567 if (!Lex.first) {
1568 Lex = std::make_pair(
1569 x: &M, y: llvm::ArrayRef(
1570 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1571 Blob.size() / sizeof(DeclID)));
1572 }
1573 DC->setHasExternalLexicalStorage(true);
1574 return false;
1575}
1576
1577bool ASTReader::ReadVisibleDeclContextStorage(
1578 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1579 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1580 assert(Offset != 0);
1581
1582 SavedStreamPosition SavedPosition(Cursor);
1583 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1584 Error(Err: std::move(Err));
1585 return true;
1586 }
1587
1588 RecordData Record;
1589 StringRef Blob;
1590 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1591 if (!MaybeCode) {
1592 Error(Err: MaybeCode.takeError());
1593 return true;
1594 }
1595 unsigned Code = MaybeCode.get();
1596
1597 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1598 if (!MaybeRecCode) {
1599 Error(Err: MaybeRecCode.takeError());
1600 return true;
1601 }
1602 unsigned RecCode = MaybeRecCode.get();
1603 switch (VisibleKind) {
1604 case VisibleDeclContextStorageKind::GenerallyVisible:
1605 if (RecCode != DECL_CONTEXT_VISIBLE) {
1606 Error(Msg: "Expected visible lookup table block");
1607 return true;
1608 }
1609 break;
1610 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1611 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1612 Error(Msg: "Expected module local visible lookup table block");
1613 return true;
1614 }
1615 break;
1616 case VisibleDeclContextStorageKind::TULocalVisible:
1617 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1618 Error(Msg: "Expected TU local lookup table block");
1619 return true;
1620 }
1621 break;
1622 }
1623
1624 // We can't safely determine the primary context yet, so delay attaching the
1625 // lookup table until we're done with recursive deserialization.
1626 auto *Data = (const unsigned char*)Blob.data();
1627 switch (VisibleKind) {
1628 case VisibleDeclContextStorageKind::GenerallyVisible:
1629 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1630 break;
1631 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1632 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1633 break;
1634 case VisibleDeclContextStorageKind::TULocalVisible:
1635 if (M.Kind == MK_MainFile)
1636 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1637 break;
1638 }
1639 return false;
1640}
1641
1642void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1643 ModuleFile &M, bool IsPartial) {
1644 D = D->getCanonicalDecl();
1645 auto &SpecLookups =
1646 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1647 SpecLookups[D].Table.add(File: &M, Data,
1648 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1649}
1650
1651bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1652 uint64_t Offset, Decl *D, bool IsPartial) {
1653 assert(Offset != 0);
1654
1655 SavedStreamPosition SavedPosition(Cursor);
1656 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1657 Error(Err: std::move(Err));
1658 return true;
1659 }
1660
1661 RecordData Record;
1662 StringRef Blob;
1663 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1664 if (!MaybeCode) {
1665 Error(Err: MaybeCode.takeError());
1666 return true;
1667 }
1668 unsigned Code = MaybeCode.get();
1669
1670 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1671 if (!MaybeRecCode) {
1672 Error(Err: MaybeRecCode.takeError());
1673 return true;
1674 }
1675 unsigned RecCode = MaybeRecCode.get();
1676 if (RecCode != DECL_SPECIALIZATIONS &&
1677 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1678 Error(Msg: "Expected decl specs block");
1679 return true;
1680 }
1681
1682 auto *Data = (const unsigned char *)Blob.data();
1683 AddSpecializations(D, Data, M, IsPartial);
1684 return false;
1685}
1686
1687void ASTReader::Error(StringRef Msg) const {
1688 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1689 if (PP.getLangOpts().Modules &&
1690 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1691 Diag(DiagID: diag::note_module_cache_path)
1692 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1693 }
1694}
1695
1696void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1697 StringRef Arg3) const {
1698 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1699}
1700
1701namespace {
1702struct AlreadyReportedDiagnosticError
1703 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1704 static char ID;
1705
1706 void log(raw_ostream &OS) const override {
1707 llvm_unreachable("reporting an already-reported diagnostic error");
1708 }
1709
1710 std::error_code convertToErrorCode() const override {
1711 return llvm::inconvertibleErrorCode();
1712 }
1713};
1714
1715char AlreadyReportedDiagnosticError::ID = 0;
1716} // namespace
1717
1718void ASTReader::Error(llvm::Error &&Err) const {
1719 handleAllErrors(
1720 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1721 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1722}
1723
1724//===----------------------------------------------------------------------===//
1725// Source Manager Deserialization
1726//===----------------------------------------------------------------------===//
1727
1728/// Read the line table in the source manager block.
1729void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1730 unsigned Idx = 0;
1731 LineTableInfo &LineTable = SourceMgr.getLineTable();
1732
1733 // Parse the file names
1734 std::map<int, int> FileIDs;
1735 FileIDs[-1] = -1; // For unspecified filenames.
1736 for (unsigned I = 0; Record[Idx]; ++I) {
1737 // Extract the file name
1738 auto Filename = ReadPath(F, Record, Idx);
1739 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1740 }
1741 ++Idx;
1742
1743 // Parse the line entries
1744 std::vector<LineEntry> Entries;
1745 while (Idx < Record.size()) {
1746 FileID FID = ReadFileID(F, Record, Idx);
1747
1748 // Extract the line entries
1749 unsigned NumEntries = Record[Idx++];
1750 assert(NumEntries && "no line entries for file ID");
1751 Entries.clear();
1752 Entries.reserve(n: NumEntries);
1753 for (unsigned I = 0; I != NumEntries; ++I) {
1754 unsigned FileOffset = Record[Idx++];
1755 unsigned LineNo = Record[Idx++];
1756 int FilenameID = FileIDs[Record[Idx++]];
1757 SrcMgr::CharacteristicKind FileKind
1758 = (SrcMgr::CharacteristicKind)Record[Idx++];
1759 unsigned IncludeOffset = Record[Idx++];
1760 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1761 FileKind, IncludeOffset));
1762 }
1763 LineTable.AddEntry(FID, Entries);
1764 }
1765}
1766
1767/// Read a source manager block
1768llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1769 using namespace SrcMgr;
1770
1771 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1772
1773 // Set the source-location entry cursor to the current position in
1774 // the stream. This cursor will be used to read the contents of the
1775 // source manager block initially, and then lazily read
1776 // source-location entries as needed.
1777 SLocEntryCursor = F.Stream;
1778
1779 // The stream itself is going to skip over the source manager block.
1780 if (llvm::Error Err = F.Stream.SkipBlock())
1781 return Err;
1782
1783 // Enter the source manager block.
1784 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1785 return Err;
1786 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1787
1788 RecordData Record;
1789 while (true) {
1790 Expected<llvm::BitstreamEntry> MaybeE =
1791 SLocEntryCursor.advanceSkippingSubblocks();
1792 if (!MaybeE)
1793 return MaybeE.takeError();
1794 llvm::BitstreamEntry E = MaybeE.get();
1795
1796 switch (E.Kind) {
1797 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1798 case llvm::BitstreamEntry::Error:
1799 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1800 Fmt: "malformed block record in AST file");
1801 case llvm::BitstreamEntry::EndBlock:
1802 return llvm::Error::success();
1803 case llvm::BitstreamEntry::Record:
1804 // The interesting case.
1805 break;
1806 }
1807
1808 // Read a record.
1809 Record.clear();
1810 StringRef Blob;
1811 Expected<unsigned> MaybeRecord =
1812 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1813 if (!MaybeRecord)
1814 return MaybeRecord.takeError();
1815 switch (MaybeRecord.get()) {
1816 default: // Default behavior: ignore.
1817 break;
1818
1819 case SM_SLOC_FILE_ENTRY:
1820 case SM_SLOC_BUFFER_ENTRY:
1821 case SM_SLOC_EXPANSION_ENTRY:
1822 // Once we hit one of the source location entries, we're done.
1823 return llvm::Error::success();
1824 }
1825 }
1826}
1827
1828llvm::Expected<SourceLocation::UIntTy>
1829ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1830 BitstreamCursor &Cursor = F->SLocEntryCursor;
1831 SavedStreamPosition SavedPosition(Cursor);
1832 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1833 F->SLocEntryOffsets[Index]))
1834 return std::move(Err);
1835
1836 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1837 if (!MaybeEntry)
1838 return MaybeEntry.takeError();
1839
1840 llvm::BitstreamEntry Entry = MaybeEntry.get();
1841 if (Entry.Kind != llvm::BitstreamEntry::Record)
1842 return llvm::createStringError(
1843 EC: std::errc::illegal_byte_sequence,
1844 Fmt: "incorrectly-formatted source location entry in AST file");
1845
1846 RecordData Record;
1847 StringRef Blob;
1848 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1849 if (!MaybeSLOC)
1850 return MaybeSLOC.takeError();
1851
1852 switch (MaybeSLOC.get()) {
1853 default:
1854 return llvm::createStringError(
1855 EC: std::errc::illegal_byte_sequence,
1856 Fmt: "incorrectly-formatted source location entry in AST file");
1857 case SM_SLOC_FILE_ENTRY:
1858 case SM_SLOC_BUFFER_ENTRY:
1859 case SM_SLOC_EXPANSION_ENTRY:
1860 return F->SLocEntryBaseOffset + Record[0];
1861 }
1862}
1863
1864int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1865 auto SLocMapI =
1866 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1867 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1868 "Corrupted global sloc offset map");
1869 ModuleFile *F = SLocMapI->second;
1870
1871 bool Invalid = false;
1872
1873 auto It = llvm::upper_bound(
1874 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1875 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1876 int ID = F->SLocEntryBaseID + LocalIndex;
1877 std::size_t Index = -ID - 2;
1878 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1879 assert(!SourceMgr.SLocEntryLoaded[Index]);
1880 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1881 if (!MaybeEntryOffset) {
1882 Error(Err: MaybeEntryOffset.takeError());
1883 Invalid = true;
1884 return true;
1885 }
1886 SourceMgr.LoadedSLocEntryTable[Index] =
1887 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1888 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1889 }
1890 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1891 });
1892
1893 if (Invalid)
1894 return 0;
1895
1896 // The iterator points to the first entry with start offset greater than the
1897 // offset of interest. The previous entry must contain the offset of interest.
1898 return F->SLocEntryBaseID + *std::prev(x: It);
1899}
1900
1901std::pair<SourceLocation::UIntTy, SourceLocationEncoding::Chain>
1902ASTReader::ReadSourceLocationOffset(const RecordDataImpl &Record, unsigned Idx,
1903 SourceLocation::UIntTy InitialDelta) {
1904 SourceLocation::UIntTy Offset = Record[Idx];
1905 return {Offset, SourceLocationEncoding::Chain(Offset + InitialDelta)};
1906}
1907
1908std::pair<SourceLocation::UIntTy, SourceLocationEncoding::Chain>
1909ASTReader::ReadEntryOffset(const RecordDataImpl &Record) {
1910 // Anchor the chain at the entry's own local offset, deriving it exactly as
1911 // the writer does -- see ASTWriter::EmitEntryOffset. The field has the dummy
1912 // entry subtracted out, so add it back. The anchor stays in the writing
1913 // module's local space, which is the space the deltas are differences in;
1914 // deltaDecode therefore runs before the locations are translated into ours.
1915 return ReadSourceLocationOffset(Record, Idx: 0, InitialDelta: 2);
1916}
1917
1918bool ASTReader::ReadSLocEntry(int ID) {
1919 if (ID == 0)
1920 return false;
1921
1922 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1923 Error(Msg: "source location entry ID out-of-range for AST file");
1924 return true;
1925 }
1926
1927 // Local helper to read the (possibly-compressed) buffer data following the
1928 // entry record.
1929 auto ReadBuffer = [this](
1930 BitstreamCursor &SLocEntryCursor,
1931 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1932 RecordData Record;
1933 StringRef Blob;
1934 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1935 if (!MaybeCode) {
1936 Error(Err: MaybeCode.takeError());
1937 return nullptr;
1938 }
1939 unsigned Code = MaybeCode.get();
1940
1941 Expected<unsigned> MaybeRecCode =
1942 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1943 if (!MaybeRecCode) {
1944 Error(Err: MaybeRecCode.takeError());
1945 return nullptr;
1946 }
1947 unsigned RecCode = MaybeRecCode.get();
1948
1949 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1950 SmallVector<uint8_t, 0> Decompressed;
1951 if (llvm::Error E = llvm::compression::decompress(
1952 Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1953 Error(Msg: "could not decompress embedded file contents: " +
1954 llvm::toString(E: std::move(E)));
1955 return nullptr;
1956 }
1957 return llvm::MemoryBuffer::getMemBufferCopy(
1958 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1959 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1960 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1961 } else {
1962 Error(Msg: "AST record has invalid code");
1963 return nullptr;
1964 }
1965 };
1966
1967 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1968 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1969 BitNo: F->SLocEntryOffsetsBase +
1970 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1971 Error(Err: std::move(Err));
1972 return true;
1973 }
1974
1975 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1976 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1977
1978 ++NumSLocEntriesRead;
1979 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1980 if (!MaybeEntry) {
1981 Error(Err: MaybeEntry.takeError());
1982 return true;
1983 }
1984 llvm::BitstreamEntry Entry = MaybeEntry.get();
1985
1986 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1987 Error(Msg: "incorrectly-formatted source location entry in AST file");
1988 return true;
1989 }
1990
1991 RecordData Record;
1992 StringRef Blob;
1993 Expected<unsigned> MaybeSLOC =
1994 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1995 if (!MaybeSLOC) {
1996 Error(Err: MaybeSLOC.takeError());
1997 return true;
1998 }
1999 switch (MaybeSLOC.get()) {
2000 default:
2001 Error(Msg: "incorrectly-formatted source location entry in AST file");
2002 return true;
2003
2004 case SM_SLOC_FILE_ENTRY: {
2005 // We will detect whether a file changed and return 'Failure' for it, but
2006 // we will also try to fail gracefully by setting up the SLocEntry.
2007 unsigned InputID = Record[4];
2008 InputFile IF = getInputFile(F&: *F, ID: InputID);
2009 OptionalFileEntryRef File = IF.getFile();
2010 bool OverriddenBuffer = IF.isOverridden();
2011
2012 // Note that we only check if a File was returned. If it was out-of-date
2013 // we have complained but we will continue creating a FileID to recover
2014 // gracefully.
2015 if (!File)
2016 return true;
2017
2018 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2019 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
2020 // This is the module's main file.
2021 IncludeLoc = getImportLocation(F);
2022 }
2023 SrcMgr::CharacteristicKind
2024 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2025 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
2026 LoadedOffset: BaseOffset + Record[0]);
2027 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2028 FileInfo.NumCreatedFIDs = Record[5];
2029 if (Record[3])
2030 FileInfo.setHasLineDirectives();
2031
2032 unsigned NumFileDecls = Record[7];
2033 if (NumFileDecls && ContextObj) {
2034 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
2035 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
2036 FileDeclIDs[FID] =
2037 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
2038 }
2039
2040 const SrcMgr::ContentCache &ContentCache =
2041 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2042 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2043 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2044 !ContentCache.getBufferIfLoaded()) {
2045 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2046 if (!Buffer)
2047 return true;
2048 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2049 }
2050
2051 break;
2052 }
2053
2054 case SM_SLOC_BUFFER_ENTRY: {
2055 const char *Name = Blob.data();
2056 unsigned Offset = Record[0];
2057 SrcMgr::CharacteristicKind
2058 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2059 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2060 if (IncludeLoc.isInvalid() && F->isModule()) {
2061 IncludeLoc = getImportLocation(F);
2062 }
2063
2064 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2065 if (!Buffer)
2066 return true;
2067 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2068 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2069 if (Record[3]) {
2070 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2071 FileInfo.setHasLineDirectives();
2072 }
2073 break;
2074 }
2075
2076 case SM_SLOC_EXPANSION_ENTRY: {
2077 auto [EntryOffset, Chain] = ReadEntryOffset(Record);
2078 // The chain is stateful: decode in the same order the writer emitted, each
2079 // in its own statement. See CreateSLocExpansionAbbrev for the field order.
2080 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[1], Chain);
2081 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2], Chain);
2082 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[3], Chain);
2083 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2084 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2085 LoadedOffset: BaseOffset + EntryOffset);
2086 break;
2087 }
2088 }
2089
2090 return false;
2091}
2092
2093std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2094 if (ID == 0)
2095 return std::make_pair(x: SourceLocation(), y: "");
2096
2097 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2098 Error(Msg: "source location entry ID out-of-range for AST file");
2099 return std::make_pair(x: SourceLocation(), y: "");
2100 }
2101
2102 // Find which module file this entry lands in.
2103 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2104 if (!M->isModule())
2105 return std::make_pair(x: SourceLocation(), y: "");
2106
2107 // FIXME: Can we map this down to a particular submodule? That would be
2108 // ideal.
2109 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2110}
2111
2112/// Find the location where the module F is imported.
2113SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2114 if (F->ImportLoc.isValid())
2115 return F->ImportLoc;
2116
2117 // Otherwise we have a PCH. It's considered to be "imported" at the first
2118 // location of its includer.
2119 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2120 // Main file is the importer.
2121 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2122 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2123 }
2124 return F->ImportedBy[0]->FirstLoc;
2125}
2126
2127/// Enter a subblock of the specified BlockID with the specified cursor. Read
2128/// the abbreviations that are at the top of the block and then leave the cursor
2129/// pointing into the block.
2130llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2131 unsigned BlockID,
2132 uint64_t *StartOfBlockOffset) {
2133 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2134 return Err;
2135
2136 if (StartOfBlockOffset)
2137 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2138
2139 while (true) {
2140 uint64_t Offset = Cursor.GetCurrentBitNo();
2141 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2142 if (!MaybeCode)
2143 return MaybeCode.takeError();
2144 unsigned Code = MaybeCode.get();
2145
2146 // We expect all abbrevs to be at the start of the block.
2147 if (Code != llvm::bitc::DEFINE_ABBREV) {
2148 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2149 return Err;
2150 return llvm::Error::success();
2151 }
2152 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2153 return Err;
2154 }
2155}
2156
2157Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2158 unsigned &Idx) {
2159 Token Tok;
2160 Tok.startToken();
2161 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2162 Tok.setKind((tok::TokenKind)Record[Idx++]);
2163 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2164
2165 if (Tok.isAnnotation()) {
2166 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2167 switch (Tok.getKind()) {
2168 case tok::annot_pragma_loop_hint: {
2169 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2170 Info->PragmaName = ReadToken(M, Record, Idx);
2171 Info->Option = ReadToken(M, Record, Idx);
2172 unsigned NumTokens = Record[Idx++];
2173 SmallVector<Token, 4> Toks;
2174 Toks.reserve(N: NumTokens);
2175 for (unsigned I = 0; I < NumTokens; ++I)
2176 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2177 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2178 Tok.setAnnotationValue(static_cast<void *>(Info));
2179 break;
2180 }
2181 case tok::annot_pragma_pack: {
2182 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2183 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2184 auto SlotLabel = ReadString(Record, Idx);
2185 Info->SlotLabel =
2186 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2187 Info->Alignment = ReadToken(M, Record, Idx);
2188 Tok.setAnnotationValue(static_cast<void *>(Info));
2189 break;
2190 }
2191 // Some annotation tokens do not use the PtrData field.
2192 case tok::annot_pragma_openmp:
2193 case tok::annot_pragma_openmp_end:
2194 case tok::annot_pragma_unused:
2195 case tok::annot_pragma_openacc:
2196 case tok::annot_pragma_openacc_end:
2197 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(BitNo: 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(BitNo: 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(BitNo: 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(BitNo: 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(BitNo: 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
3542 off_t StoredSize = 0;
3543 time_t StoredModTime = 0;
3544 unsigned FileNameKind = 0;
3545 ASTFileSignature StoredSignature;
3546 ModuleFileName ImportedFile;
3547 std::string StoredFile;
3548 bool IgnoreImportedByNote = false;
3549
3550 // For prebuilt and explicit modules first consult the file map for
3551 // an override. Note that here we don't search prebuilt module
3552 // directories if we're not importing standard c++ module, only the
3553 // explicit name to file mappings. Also, we will still verify the
3554 // size/signature making sure it is essentially the same file but
3555 // perhaps in a different location.
3556 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3557 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3558 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3559
3560 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3561 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3562 return Missing;
3563 }
3564
3565 if (!IsImportingStdCXXModule) {
3566 StoredSize = (off_t)Record[Idx++];
3567 StoredModTime = (time_t)Record[Idx++];
3568 FileNameKind = (unsigned)Record[Idx++];
3569
3570 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3571 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3572 Last: SignatureBytes.end());
3573 Blob = Blob.substr(Start: ASTFileSignature::size);
3574
3575 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3576 if (ImportedFile.empty()) {
3577 ImportedFile = ModuleFileName::makeFromRaw(Name: StoredFile, RawKind: FileNameKind);
3578 } else if (!getDiags().isIgnored(
3579 DiagID: diag::warn_module_file_mapping_mismatch,
3580 Loc: CurrentImportLoc)) {
3581 auto ImportedFileRef =
3582 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3583 auto StoredFileRef =
3584 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3585 if ((ImportedFileRef && StoredFileRef) &&
3586 (*ImportedFileRef != *StoredFileRef)) {
3587 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3588 << ImportedFile << StoredFile;
3589 Diag(DiagID: diag::note_module_file_imported_by)
3590 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3591 IgnoreImportedByNote = true;
3592 }
3593 }
3594 }
3595
3596 // If our client can't cope with us being out of date, we can't cope with
3597 // our dependency being missing.
3598 unsigned Capabilities = ClientLoadCapabilities;
3599 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3600 Capabilities &= ~ARR_Missing;
3601
3602 // Load the AST file.
3603 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3604 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3605 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3606
3607 // Check the AST we just read from ImportedFile contains a different
3608 // module than we expected (ImportedName). This can occur for C++20
3609 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3610 if (IsImportingStdCXXModule) {
3611 if (const auto *Imported =
3612 getModuleManager().lookupByFileName(FileName: ImportedFile);
3613 Imported != nullptr && Imported->ModuleName != ImportedName) {
3614 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3615 Result = Missing;
3616 }
3617 }
3618
3619 // If we diagnosed a problem, produce a backtrace.
3620 bool recompilingFinalized = Result == OutOfDate &&
3621 (Capabilities & ARR_OutOfDate) &&
3622 getModuleManager()
3623 .getModuleCache()
3624 .getInMemoryModuleCache()
3625 .isPCMFinal(Filename: F.FileName);
3626 if (!IgnoreImportedByNote &&
3627 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3628 Diag(DiagID: diag::note_module_file_imported_by)
3629 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3630
3631 switch (Result) {
3632 case Failure: return Failure;
3633 // If we have to ignore the dependency, we'll have to ignore this too.
3634 case Missing:
3635 case OutOfDate: return OutOfDate;
3636 case VersionMismatch: return VersionMismatch;
3637 case ConfigurationMismatch: return ConfigurationMismatch;
3638 case HadErrors: return HadErrors;
3639 case Success: break;
3640 }
3641 break;
3642 }
3643
3644 case ORIGINAL_FILE:
3645 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3646 F.ActualOriginalSourceFileName = std::string(Blob);
3647 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3648 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3649 break;
3650
3651 case ORIGINAL_FILE_ID:
3652 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3653 break;
3654
3655 case MODULE_NAME:
3656 F.ModuleName = std::string(Blob);
3657 Diag(DiagID: diag::remark_module_import)
3658 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3659 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3660 if (Listener)
3661 Listener->ReadModuleName(ModuleName: F.ModuleName);
3662
3663 // Validate the AST as soon as we have a name so we can exit early on
3664 // failure.
3665 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3666 return Result;
3667
3668 break;
3669
3670 case MODULE_DIRECTORY: {
3671 // Save the BaseDirectory as written in the PCM for computing the module
3672 // filename for the ModuleCache.
3673 BaseDirectoryAsWritten = Blob;
3674 assert(!F.ModuleName.empty() &&
3675 "MODULE_DIRECTORY found before MODULE_NAME");
3676 F.BaseDirectory = std::string(Blob);
3677
3678 auto [MaybeM, IgnoreError] =
3679 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3680 if (!MaybeM.has_value())
3681 break;
3682
3683 Module *M = MaybeM.value();
3684 if (!M || !M->Directory)
3685 break;
3686 if (IgnoreError) {
3687 F.BaseDirectory = std::string(M->Directory->getName());
3688 break;
3689 }
3690 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3691 break;
3692
3693 // If we're implicitly loading a module, the base directory can't
3694 // change between the build and use.
3695 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3696 if (BuildDir && (*BuildDir == M->Directory)) {
3697 F.BaseDirectory = std::string(M->Directory->getName());
3698 break;
3699 }
3700 Diag(DiagID: diag::remark_module_relocated)
3701 << F.ModuleName << Blob << M->Directory->getName();
3702
3703 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3704 Diag(DiagID: diag::err_imported_module_relocated)
3705 << F.ModuleName << Blob << M->Directory->getName();
3706 return OutOfDate;
3707 }
3708
3709 case MODULE_MAP_FILE:
3710 if (ASTReadResult Result =
3711 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3712 return Result;
3713 break;
3714
3715 case MODULE_DIRECTORY_DEPENDENCIES:
3716 ReadDirectoryDependencies(Record, F);
3717 break;
3718
3719 case INPUT_FILE_OFFSETS:
3720 NumInputs = Record[0];
3721 NumUserInputs = Record[1];
3722 F.InputFileOffsets =
3723 (const llvm::support::unaligned_uint64_t *)Blob.data();
3724 F.InputFilesLoaded.resize(new_size: NumInputs);
3725 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3726 F.NumUserInputFiles = NumUserInputs;
3727 break;
3728 }
3729 }
3730}
3731
3732llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3733 unsigned ClientLoadCapabilities) {
3734 BitstreamCursor &Stream = F.Stream;
3735
3736 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3737 return Err;
3738 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3739
3740 // Read all of the records and blocks for the AST file.
3741 RecordData Record;
3742 while (true) {
3743 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3744 if (!MaybeEntry)
3745 return MaybeEntry.takeError();
3746 llvm::BitstreamEntry Entry = MaybeEntry.get();
3747
3748 switch (Entry.Kind) {
3749 case llvm::BitstreamEntry::Error:
3750 return llvm::createStringError(
3751 EC: std::errc::illegal_byte_sequence,
3752 Fmt: "error at end of module block in AST file");
3753 case llvm::BitstreamEntry::EndBlock:
3754 // Outside of C++, we do not store a lookup map for the translation unit.
3755 // Instead, mark it as needing a lookup map to be built if this module
3756 // contains any declarations lexically within it (which it always does!).
3757 // This usually has no cost, since we very rarely need the lookup map for
3758 // the translation unit outside C++.
3759 if (ASTContext *Ctx = ContextObj) {
3760 DeclContext *DC = Ctx->getTranslationUnitDecl();
3761 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3762 DC->setMustBuildLookupTable();
3763 }
3764
3765 return llvm::Error::success();
3766 case llvm::BitstreamEntry::SubBlock:
3767 switch (Entry.ID) {
3768 case DECLTYPES_BLOCK_ID:
3769 // We lazily load the decls block, but we want to set up the
3770 // DeclsCursor cursor to point into it. Clone our current bitcode
3771 // cursor to it, enter the block and read the abbrevs in that block.
3772 // With the main cursor, we just skip over it.
3773 F.DeclsCursor = Stream;
3774 if (llvm::Error Err = Stream.SkipBlock())
3775 return Err;
3776 if (llvm::Error Err = ReadBlockAbbrevs(
3777 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3778 return Err;
3779 break;
3780
3781 case PREPROCESSOR_BLOCK_ID:
3782 F.MacroCursor = Stream;
3783 if (!PP.getExternalSource())
3784 PP.setExternalSource(this);
3785
3786 if (llvm::Error Err = Stream.SkipBlock())
3787 return Err;
3788 if (llvm::Error Err =
3789 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3790 return Err;
3791 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3792 break;
3793
3794 case PREPROCESSOR_DETAIL_BLOCK_ID:
3795 F.PreprocessorDetailCursor = Stream;
3796
3797 if (llvm::Error Err = Stream.SkipBlock()) {
3798 return Err;
3799 }
3800 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3801 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3802 return Err;
3803 F.PreprocessorDetailStartOffset
3804 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3805
3806 if (!PP.getPreprocessingRecord())
3807 PP.createPreprocessingRecord();
3808 if (!PP.getPreprocessingRecord()->getExternalSource())
3809 PP.getPreprocessingRecord()->SetExternalSource(*this);
3810 break;
3811
3812 case SOURCE_MANAGER_BLOCK_ID:
3813 if (llvm::Error Err = ReadSourceManagerBlock(F))
3814 return Err;
3815 break;
3816
3817 case SUBMODULE_BLOCK_ID:
3818 F.SubmodulesCursor = Stream;
3819 if (llvm::Error Err = Stream.SkipBlock())
3820 return Err;
3821 if (llvm::Error Err =
3822 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3823 return Err;
3824 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3825 break;
3826
3827 case COMMENTS_BLOCK_ID: {
3828 BitstreamCursor C = Stream;
3829
3830 if (llvm::Error Err = Stream.SkipBlock())
3831 return Err;
3832 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3833 return Err;
3834 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3835 break;
3836 }
3837
3838 default:
3839 if (llvm::Error Err = Stream.SkipBlock())
3840 return Err;
3841 break;
3842 }
3843 continue;
3844
3845 case llvm::BitstreamEntry::Record:
3846 // The interesting case.
3847 break;
3848 }
3849
3850 // Read and process a record.
3851 Record.clear();
3852 StringRef Blob;
3853 Expected<unsigned> MaybeRecordType =
3854 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3855 if (!MaybeRecordType)
3856 return MaybeRecordType.takeError();
3857 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3858
3859 // If we're not loading an AST context, we don't care about most records.
3860 if (!ContextObj) {
3861 switch (RecordType) {
3862 case IDENTIFIER_TABLE:
3863 case IDENTIFIER_OFFSET:
3864 case INTERESTING_IDENTIFIERS:
3865 case STATISTICS:
3866 case PP_ASSUME_NONNULL_LOC:
3867 case PP_CONDITIONAL_STACK:
3868 case PP_COUNTER_VALUE:
3869 case SOURCE_LOCATION_OFFSETS:
3870 case MODULE_OFFSET_MAP:
3871 case SOURCE_MANAGER_LINE_TABLE:
3872 case PPD_ENTITIES_OFFSETS:
3873 case HEADER_SEARCH_TABLE:
3874 case IMPORTED_MODULES:
3875 case MACRO_OFFSET:
3876 case SUBMODULE_METADATA:
3877 break;
3878 default:
3879 continue;
3880 }
3881 }
3882
3883 switch (RecordType) {
3884 default: // Default behavior: ignore.
3885 break;
3886
3887 case SUBMODULE_METADATA: {
3888 F.BaseSubmoduleID = getTotalNumSubmodules();
3889 F.LocalNumSubmodules = Record[0];
3890 F.LocalBaseSubmoduleID = Record[1];
3891 F.LocalTopLevelSubmoduleID = Record[2];
3892 F.SubmoduleOffsets =
3893 (const llvm::support::unaligned_uint64_t *)Blob.data();
3894 if (F.LocalNumSubmodules > 0) {
3895 // Introduce the global -> local mapping for submodules within this
3896 // module.
3897 GlobalSubmoduleMap.insert(
3898 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3899
3900 // Introduce the local -> global mapping for submodules within this
3901 // module.
3902 F.SubmoduleRemap.insertOrReplace(
3903 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3904 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3905
3906 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3907 }
3908
3909 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3910 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3911 };
3912
3913 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3914 // If we already knew about this module, make sure to bring all
3915 // submodules up to date.
3916 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3917 unsigned LocalID =
3918 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3919 ReadSubmodule(LocalID);
3920 }
3921 } else {
3922 // If we didn't know this module, we loaded it transitively. Deserialize
3923 // just the top-level module to register it with ModuleMap, but load the
3924 // rest lazily.
3925 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3926 }
3927
3928 break;
3929 }
3930
3931 case TYPE_OFFSET: {
3932 if (F.LocalNumTypes != 0)
3933 return llvm::createStringError(
3934 EC: std::errc::illegal_byte_sequence,
3935 Fmt: "duplicate TYPE_OFFSET record in AST file");
3936 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3937 F.LocalNumTypes = Record[0];
3938 F.BaseTypeIndex = getTotalNumTypes();
3939
3940 if (F.LocalNumTypes > 0)
3941 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3942
3943 break;
3944 }
3945
3946 case DECL_OFFSET: {
3947 if (F.LocalNumDecls != 0)
3948 return llvm::createStringError(
3949 EC: std::errc::illegal_byte_sequence,
3950 Fmt: "duplicate DECL_OFFSET record in AST file");
3951 F.DeclOffsets = (const DeclOffset *)Blob.data();
3952 F.LocalNumDecls = Record[0];
3953 F.BaseDeclIndex = getTotalNumDecls();
3954
3955 if (F.LocalNumDecls > 0)
3956 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3957
3958 break;
3959 }
3960
3961 case TU_UPDATE_LEXICAL: {
3962 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3963 LexicalContents Contents(
3964 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3965 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3966 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3967 TU->setHasExternalLexicalStorage(true);
3968 break;
3969 }
3970
3971 case UPDATE_VISIBLE: {
3972 unsigned Idx = 0;
3973 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3974 auto *Data = (const unsigned char*)Blob.data();
3975 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3976 // If we've already loaded the decl, perform the updates when we finish
3977 // loading this block.
3978 if (Decl *D = GetExistingDecl(ID))
3979 PendingUpdateRecords.push_back(
3980 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3981 break;
3982 }
3983
3984 case UPDATE_MODULE_LOCAL_VISIBLE: {
3985 unsigned Idx = 0;
3986 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3987 auto *Data = (const unsigned char *)Blob.data();
3988 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3989 // If we've already loaded the decl, perform the updates when we finish
3990 // loading this block.
3991 if (Decl *D = GetExistingDecl(ID))
3992 PendingUpdateRecords.push_back(
3993 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3994 break;
3995 }
3996
3997 case UPDATE_TU_LOCAL_VISIBLE: {
3998 if (F.Kind != MK_MainFile)
3999 break;
4000 unsigned Idx = 0;
4001 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4002 auto *Data = (const unsigned char *)Blob.data();
4003 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4004 // If we've already loaded the decl, perform the updates when we finish
4005 // loading this block.
4006 if (Decl *D = GetExistingDecl(ID))
4007 PendingUpdateRecords.push_back(
4008 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4009 break;
4010 }
4011
4012 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
4013 unsigned Idx = 0;
4014 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4015 auto *Data = (const unsigned char *)Blob.data();
4016 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4017 // If we've already loaded the decl, perform the updates when we finish
4018 // loading this block.
4019 if (Decl *D = GetExistingDecl(ID))
4020 PendingUpdateRecords.push_back(
4021 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4022 break;
4023 }
4024
4025 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
4026 unsigned Idx = 0;
4027 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4028 auto *Data = (const unsigned char *)Blob.data();
4029 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4030 // If we've already loaded the decl, perform the updates when we finish
4031 // loading this block.
4032 if (Decl *D = GetExistingDecl(ID))
4033 PendingUpdateRecords.push_back(
4034 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4035 break;
4036 }
4037
4038 case IDENTIFIER_TABLE:
4039 F.IdentifierTableData =
4040 reinterpret_cast<const unsigned char *>(Blob.data());
4041 if (Record[0]) {
4042 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
4043 Buckets: F.IdentifierTableData + Record[0],
4044 Payload: F.IdentifierTableData + sizeof(uint32_t),
4045 Base: F.IdentifierTableData,
4046 InfoObj: ASTIdentifierLookupTrait(*this, F));
4047
4048 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4049 }
4050 break;
4051
4052 case IDENTIFIER_OFFSET: {
4053 if (F.LocalNumIdentifiers != 0)
4054 return llvm::createStringError(
4055 EC: std::errc::illegal_byte_sequence,
4056 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4057 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4058 F.LocalNumIdentifiers = Record[0];
4059 F.BaseIdentifierID = getTotalNumIdentifiers();
4060
4061 if (F.LocalNumIdentifiers > 0)
4062 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4063 + F.LocalNumIdentifiers);
4064 break;
4065 }
4066
4067 case INTERESTING_IDENTIFIERS:
4068 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4069 break;
4070
4071 case EAGERLY_DESERIALIZED_DECLS:
4072 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4073 // about "interesting" decls (for instance, if we're building a module).
4074 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4075 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4076 break;
4077
4078 case MODULAR_CODEGEN_DECLS:
4079 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4080 // them (ie: if we're not codegenerating this module).
4081 if (F.Kind == MK_MainFile ||
4082 getContext().getLangOpts().BuildingPCHWithObjectFile)
4083 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4084 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4085 break;
4086
4087 case SPECIAL_TYPES:
4088 if (SpecialTypes.empty()) {
4089 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4090 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4091 break;
4092 }
4093
4094 if (Record.empty())
4095 break;
4096
4097 if (SpecialTypes.size() != Record.size())
4098 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4099 Fmt: "invalid special-types record");
4100
4101 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4102 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4103 if (!SpecialTypes[I])
4104 SpecialTypes[I] = ID;
4105 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4106 // merge step?
4107 }
4108 break;
4109
4110 case STATISTICS:
4111 TotalNumStatements += Record[0];
4112 TotalNumMacros += Record[1];
4113 TotalLexicalDeclContexts += Record[2];
4114 TotalVisibleDeclContexts += Record[3];
4115 TotalModuleLocalVisibleDeclContexts += Record[4];
4116 TotalTULocalVisibleDeclContexts += Record[5];
4117 break;
4118
4119 case UNUSED_FILESCOPED_DECLS:
4120 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4121 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4122 break;
4123
4124 case DELEGATING_CTORS:
4125 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4126 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4127 break;
4128
4129 case WEAK_UNDECLARED_IDENTIFIERS:
4130 if (Record.size() % 3 != 0)
4131 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4132 Fmt: "invalid weak identifiers record");
4133
4134 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4135 // files. This isn't the way to do it :)
4136 WeakUndeclaredIdentifiers.clear();
4137
4138 // Translate the weak, undeclared identifiers into global IDs.
4139 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4140 WeakUndeclaredIdentifiers.push_back(
4141 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4142 WeakUndeclaredIdentifiers.push_back(
4143 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4144 WeakUndeclaredIdentifiers.push_back(
4145 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4146 }
4147 break;
4148
4149 case EXTNAME_UNDECLARED_IDENTIFIERS:
4150 if (Record.size() % 3 != 0)
4151 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4152 Fmt: "invalid extname identifiers record");
4153
4154 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4155 // non-original PCH files. This isn't the way to do it :)
4156 ExtnameUndeclaredIdentifiers.clear();
4157
4158 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4159 // global IDs.
4160 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4161 ExtnameUndeclaredIdentifiers.push_back(
4162 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4163 ExtnameUndeclaredIdentifiers.push_back(
4164 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4165 ExtnameUndeclaredIdentifiers.push_back(
4166 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4167 }
4168 break;
4169
4170 case SELECTOR_OFFSETS: {
4171 F.SelectorOffsets = (const uint32_t *)Blob.data();
4172 F.LocalNumSelectors = Record[0];
4173 unsigned LocalBaseSelectorID = Record[1];
4174 F.BaseSelectorID = getTotalNumSelectors();
4175
4176 if (F.LocalNumSelectors > 0) {
4177 // Introduce the global -> local mapping for selectors within this
4178 // module.
4179 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4180
4181 // Introduce the local -> global mapping for selectors within this
4182 // module.
4183 F.SelectorRemap.insertOrReplace(
4184 Val: std::make_pair(x&: LocalBaseSelectorID,
4185 y: F.BaseSelectorID - LocalBaseSelectorID));
4186
4187 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4188 }
4189 break;
4190 }
4191
4192 case METHOD_POOL:
4193 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4194 if (Record[0])
4195 F.SelectorLookupTable
4196 = ASTSelectorLookupTable::Create(
4197 Buckets: F.SelectorLookupTableData + Record[0],
4198 Base: F.SelectorLookupTableData,
4199 InfoObj: ASTSelectorLookupTrait(*this, F));
4200 TotalNumMethodPoolEntries += Record[1];
4201 break;
4202
4203 case REFERENCED_SELECTOR_POOL:
4204 if (!Record.empty()) {
4205 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4206 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4207 LocalID: Record[Idx++]));
4208 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4209 getRawEncoding());
4210 }
4211 }
4212 break;
4213
4214 case PP_ASSUME_NONNULL_LOC: {
4215 unsigned Idx = 0;
4216 if (!Record.empty())
4217 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4218 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4219 break;
4220 }
4221
4222 case PP_UNSAFE_BUFFER_USAGE: {
4223 if (!Record.empty()) {
4224 SmallVector<SourceLocation, 64> SrcLocs;
4225 unsigned Idx = 0;
4226 while (Idx < Record.size())
4227 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4228 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4229 }
4230 break;
4231 }
4232
4233 case PP_CONDITIONAL_STACK:
4234 if (!Record.empty()) {
4235 unsigned Idx = 0, End = Record.size() - 1;
4236 bool ReachedEOFWhileSkipping = Record[Idx++];
4237 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4238 if (ReachedEOFWhileSkipping) {
4239 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4240 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4241 bool FoundNonSkipPortion = Record[Idx++];
4242 bool FoundElse = Record[Idx++];
4243 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4244 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4245 args&: FoundElse, args&: ElseLoc);
4246 }
4247 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4248 while (Idx < End) {
4249 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4250 bool WasSkipping = Record[Idx++];
4251 bool FoundNonSkip = Record[Idx++];
4252 bool FoundElse = Record[Idx++];
4253 ConditionalStack.push_back(
4254 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4255 }
4256 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4257 }
4258 break;
4259
4260 case PP_COUNTER_VALUE:
4261 if (!Record.empty() && Listener)
4262 Listener->ReadCounter(M: F, Value: Record[0]);
4263 break;
4264
4265 case FILE_SORTED_DECLS:
4266 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4267 F.NumFileSortedDecls = Record[0];
4268 break;
4269
4270 case SOURCE_LOCATION_OFFSETS: {
4271 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4272 F.LocalNumSLocEntries = Record[0];
4273 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4274 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4275 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4276 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4277 TotalSize: SLocSpaceSize);
4278 if (!F.SLocEntryBaseID) {
4279 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4280 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4281 return llvm::createStringError(EC: std::errc::invalid_argument,
4282 Fmt: "ran out of source locations");
4283 }
4284 // Make our entry in the range map. BaseID is negative and growing, so
4285 // we invert it. Because we invert it, though, we need the other end of
4286 // the range.
4287 unsigned RangeStart =
4288 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4289 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4290 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4291
4292 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4293 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4294 GlobalSLocOffsetMap.insert(
4295 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4296 - SLocSpaceSize,y: &F));
4297
4298 TotalNumSLocEntries += F.LocalNumSLocEntries;
4299 break;
4300 }
4301
4302 case MODULE_OFFSET_MAP:
4303 F.ModuleOffsetMap = Blob;
4304 break;
4305
4306 case SOURCE_MANAGER_LINE_TABLE:
4307 ParseLineTable(F, Record);
4308 break;
4309
4310 case EXT_VECTOR_DECLS:
4311 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4312 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4313 break;
4314
4315 case VTABLE_USES:
4316 if (Record.size() % 3 != 0)
4317 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4318 Fmt: "Invalid VTABLE_USES record");
4319
4320 // Later tables overwrite earlier ones.
4321 // FIXME: Modules will have some trouble with this. This is clearly not
4322 // the right way to do this.
4323 VTableUses.clear();
4324
4325 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4326 VTableUses.push_back(
4327 Elt: {.ID: ReadDeclID(F, Record, Idx),
4328 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4329 .Used: (bool)Record[Idx++]});
4330 }
4331 break;
4332
4333 case PENDING_IMPLICIT_INSTANTIATIONS:
4334
4335 if (Record.size() % 2 != 0)
4336 return llvm::createStringError(
4337 EC: std::errc::illegal_byte_sequence,
4338 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4339
4340 // For standard C++20 module, we will only reads the instantiations
4341 // if it is the main file.
4342 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4343 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4344 PendingInstantiations.push_back(
4345 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4346 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4347 }
4348 }
4349 break;
4350
4351 case SEMA_DECL_REFS:
4352 if (Record.size() != 3)
4353 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4354 Fmt: "Invalid SEMA_DECL_REFS block");
4355 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4356 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4357 break;
4358
4359 case PPD_ENTITIES_OFFSETS: {
4360 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4361 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4362 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4363
4364 unsigned StartingID;
4365 if (!PP.getPreprocessingRecord())
4366 PP.createPreprocessingRecord();
4367 if (!PP.getPreprocessingRecord()->getExternalSource())
4368 PP.getPreprocessingRecord()->SetExternalSource(*this);
4369 StartingID
4370 = PP.getPreprocessingRecord()
4371 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4372 F.BasePreprocessedEntityID = StartingID;
4373
4374 if (F.NumPreprocessedEntities > 0) {
4375 // Introduce the global -> local mapping for preprocessed entities in
4376 // this module.
4377 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4378 }
4379
4380 break;
4381 }
4382
4383 case PPD_SKIPPED_RANGES: {
4384 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4385 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4386 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4387
4388 if (!PP.getPreprocessingRecord())
4389 PP.createPreprocessingRecord();
4390 if (!PP.getPreprocessingRecord()->getExternalSource())
4391 PP.getPreprocessingRecord()->SetExternalSource(*this);
4392 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4393 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4394
4395 if (F.NumPreprocessedSkippedRanges > 0)
4396 GlobalSkippedRangeMap.insert(
4397 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4398 break;
4399 }
4400
4401 case DECL_UPDATE_OFFSETS:
4402 if (Record.size() % 2 != 0)
4403 return llvm::createStringError(
4404 EC: std::errc::illegal_byte_sequence,
4405 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4406 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4407 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4408 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4409
4410 // If we've already loaded the decl, perform the updates when we finish
4411 // loading this block.
4412 if (Decl *D = GetExistingDecl(ID))
4413 PendingUpdateRecords.push_back(
4414 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4415 }
4416 break;
4417
4418 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4419 if (Record.size() % 5 != 0)
4420 return llvm::createStringError(
4421 EC: std::errc::illegal_byte_sequence,
4422 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4423 "file");
4424 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4425 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4426
4427 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4428 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4429 uint64_t LocalLexicalOffset = Record[I++];
4430 uint64_t LexicalOffset =
4431 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4432 uint64_t LocalVisibleOffset = Record[I++];
4433 uint64_t VisibleOffset =
4434 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4435 uint64_t LocalModuleLocalOffset = Record[I++];
4436 uint64_t ModuleLocalOffset =
4437 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4438 uint64_t TULocalLocalOffset = Record[I++];
4439 uint64_t TULocalOffset =
4440 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4441
4442 DelayedNamespaceOffsetMap[ID] = {
4443 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4444
4445 assert(!GetExistingDecl(ID) &&
4446 "We shouldn't load the namespace in the front of delayed "
4447 "namespace lexical and visible block");
4448 }
4449 break;
4450 }
4451
4452 case RELATED_DECLS_MAP:
4453 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4454 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4455 auto &RelatedDecls = RelatedDeclsMap[ID];
4456 unsigned NN = Record[I++];
4457 RelatedDecls.reserve(N: NN);
4458 for (unsigned II = 0; II < NN; II++)
4459 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4460 }
4461 break;
4462
4463 case OBJC_CATEGORIES_MAP:
4464 if (F.LocalNumObjCCategoriesInMap != 0)
4465 return llvm::createStringError(
4466 EC: std::errc::illegal_byte_sequence,
4467 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4468
4469 F.LocalNumObjCCategoriesInMap = Record[0];
4470 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4471 break;
4472
4473 case OBJC_CATEGORIES:
4474 F.ObjCCategories.swap(RHS&: Record);
4475 break;
4476
4477 case CUDA_SPECIAL_DECL_REFS:
4478 // Later tables overwrite earlier ones.
4479 // FIXME: Modules will have trouble with this.
4480 CUDASpecialDeclRefs.clear();
4481 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4482 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4483 break;
4484
4485 case HEADER_SEARCH_TABLE:
4486 F.HeaderFileInfoTableData = Blob.data();
4487 F.LocalNumHeaderFileInfos = Record[1];
4488 if (Record[0]) {
4489 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4490 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4491 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4492 InfoObj: HeaderFileInfoTrait(*this, F));
4493
4494 PP.getHeaderSearchInfo().SetExternalSource(this);
4495 if (!PP.getHeaderSearchInfo().getExternalLookup())
4496 PP.getHeaderSearchInfo().SetExternalLookup(this);
4497 }
4498 break;
4499
4500 case FP_PRAGMA_OPTIONS:
4501 // Later tables overwrite earlier ones.
4502 FPPragmaOptions.swap(RHS&: Record);
4503 break;
4504
4505 case DECLS_WITH_EFFECTS_TO_VERIFY:
4506 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4507 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4508 break;
4509
4510 case OMP_REQUIRES_DECLS:
4511 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4512 OpenMPRequiresDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4513 break;
4514
4515 case OPENCL_EXTENSIONS:
4516 for (unsigned I = 0, E = Record.size(); I != E; ) {
4517 auto Name = ReadString(Record, Idx&: I);
4518 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4519 OptInfo.Supported = Record[I++] != 0;
4520 OptInfo.Enabled = Record[I++] != 0;
4521 OptInfo.WithPragma = Record[I++] != 0;
4522 OptInfo.Avail = Record[I++];
4523 OptInfo.Core = Record[I++];
4524 OptInfo.Opt = Record[I++];
4525 }
4526 break;
4527
4528 case TENTATIVE_DEFINITIONS:
4529 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4530 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4531 break;
4532
4533 case KNOWN_NAMESPACES:
4534 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4535 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4536 break;
4537
4538 case UNDEFINED_BUT_USED:
4539 if (Record.size() % 2 != 0)
4540 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4541 Fmt: "invalid undefined-but-used record");
4542 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4543 UndefinedButUsed.push_back(
4544 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4545 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4546 }
4547 break;
4548
4549 case DELETE_EXPRS_TO_ANALYZE:
4550 for (unsigned I = 0, N = Record.size(); I != N;) {
4551 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4552 const uint64_t Count = Record[I++];
4553 DelayedDeleteExprs.push_back(Elt: Count);
4554 for (uint64_t C = 0; C < Count; ++C) {
4555 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4556 bool IsArrayForm = Record[I++] == 1;
4557 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4558 }
4559 }
4560 break;
4561
4562 case VTABLES_TO_EMIT:
4563 if (F.Kind == MK_MainFile ||
4564 getContext().getLangOpts().BuildingPCHWithObjectFile)
4565 for (unsigned I = 0, N = Record.size(); I != N;)
4566 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4567 break;
4568
4569 case IMPORTED_MODULES:
4570 if (!F.isModule()) {
4571 // If we aren't loading a module (which has its own exports), make
4572 // all of the imported modules visible.
4573 // FIXME: Deal with macros-only imports.
4574 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4575 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4576 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4577 if (GlobalID) {
4578 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4579 if (DeserializationListener)
4580 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4581 }
4582 }
4583 }
4584 break;
4585
4586 case MACRO_OFFSET: {
4587 if (F.LocalNumMacros != 0)
4588 return llvm::createStringError(
4589 EC: std::errc::illegal_byte_sequence,
4590 Fmt: "duplicate MACRO_OFFSET record in AST file");
4591 F.MacroOffsets = (const uint32_t *)Blob.data();
4592 F.LocalNumMacros = Record[0];
4593 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4594 F.BaseMacroID = getTotalNumMacros();
4595
4596 if (F.LocalNumMacros > 0)
4597 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4598 break;
4599 }
4600
4601 case LATE_PARSED_TEMPLATE:
4602 LateParsedTemplates.emplace_back(
4603 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4604 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4605 break;
4606
4607 case OPTIMIZE_PRAGMA_OPTIONS:
4608 if (Record.size() != 1)
4609 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4610 Fmt: "invalid pragma optimize record");
4611 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4612 break;
4613
4614 case MSSTRUCT_PRAGMA_OPTIONS:
4615 if (Record.size() != 1)
4616 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4617 Fmt: "invalid pragma ms_struct record");
4618 PragmaMSStructState = Record[0];
4619 break;
4620
4621 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4622 if (Record.size() != 2)
4623 return llvm::createStringError(
4624 EC: std::errc::illegal_byte_sequence,
4625 Fmt: "invalid pragma pointers to members record");
4626 PragmaMSPointersToMembersState = Record[0];
4627 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4628 break;
4629
4630 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4631 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4632 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4633 break;
4634
4635 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4636 if (Record.size() != 1)
4637 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4638 Fmt: "invalid cuda pragma options record");
4639 ForceHostDeviceDepth = Record[0];
4640 break;
4641
4642 case ALIGN_PACK_PRAGMA_OPTIONS: {
4643 if (Record.size() < 3)
4644 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4645 Fmt: "invalid pragma pack record");
4646 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4647 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4648 unsigned NumStackEntries = Record[2];
4649 unsigned Idx = 3;
4650 // Reset the stack when importing a new module.
4651 PragmaAlignPackStack.clear();
4652 for (unsigned I = 0; I < NumStackEntries; ++I) {
4653 PragmaAlignPackStackEntry Entry;
4654 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4655 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4656 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4657 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4658 Entry.SlotLabel = PragmaAlignPackStrings.back();
4659 PragmaAlignPackStack.push_back(Elt: Entry);
4660 }
4661 break;
4662 }
4663
4664 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4665 if (Record.size() < 3)
4666 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4667 Fmt: "invalid pragma float control record");
4668 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4669 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4670 unsigned NumStackEntries = Record[2];
4671 unsigned Idx = 3;
4672 // Reset the stack when importing a new module.
4673 FpPragmaStack.clear();
4674 for (unsigned I = 0; I < NumStackEntries; ++I) {
4675 FpPragmaStackEntry Entry;
4676 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4677 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4678 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4679 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4680 Entry.SlotLabel = FpPragmaStrings.back();
4681 FpPragmaStack.push_back(Elt: Entry);
4682 }
4683 break;
4684 }
4685
4686 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4687 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4688 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4689 break;
4690
4691 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4692 unsigned NumRecords = Record.front();
4693 // Last record which is used to keep number of valid records.
4694 if (Record.size() - 1 != NumRecords)
4695 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4696 Fmt: "invalid rvv intrinsic pragma record");
4697
4698 if (RISCVVecIntrinsicPragma.empty())
4699 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4700 // There might be multiple precompiled modules imported, we need to union
4701 // them all.
4702 for (unsigned i = 0; i < NumRecords; ++i)
4703 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4704 break;
4705 }
4706 }
4707 }
4708}
4709
4710void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4711 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4712
4713 // Additional remapping information.
4714 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4715 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4716 F.ModuleOffsetMap = StringRef();
4717
4718 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4719 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4720 RemapBuilder SelectorRemap(F.SelectorRemap);
4721
4722 auto &ImportedModuleVector = F.TransitiveImports;
4723 assert(ImportedModuleVector.empty());
4724
4725 while (Data < DataEnd) {
4726 // FIXME: Looking up dependency modules by filename is horrible. Let's
4727 // start fixing this with prebuilt, explicit and implicit modules and see
4728 // how it goes...
4729 using namespace llvm::support;
4730 ModuleKind Kind = static_cast<ModuleKind>(
4731 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4732 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4733 StringRef Name = StringRef((const char*)Data, Len);
4734 Data += Len;
4735 ModuleFile *OM =
4736 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4737 Kind == MK_ImplicitModule
4738 ? ModuleMgr.lookupByModuleName(ModName: Name)
4739 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4740 if (!OM)
4741 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4742 if (!OM) {
4743 std::string Msg = "refers to unknown module, cannot find ";
4744 Msg.append(str: std::string(Name));
4745 Error(Msg);
4746 return;
4747 }
4748
4749 ImportedModuleVector.push_back(Elt: OM);
4750
4751 uint32_t SubmoduleIDOffset =
4752 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4753 uint32_t SelectorIDOffset =
4754 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4755
4756 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4757 RemapBuilder &Remap) {
4758 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4759 if (Offset != None)
4760 Remap.insert(Val: std::make_pair(x&: Offset,
4761 y: static_cast<int>(BaseOffset - Offset)));
4762 };
4763
4764 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4765 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4766 }
4767}
4768
4769ASTReader::ASTReadResult
4770ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4771 const ModuleFile *ImportedBy,
4772 unsigned ClientLoadCapabilities) {
4773 unsigned Idx = 0;
4774 F.ModuleMapPath = ReadPath(F, Record, Idx);
4775
4776 // Try to resolve ModuleName in the current header search context and
4777 // verify that it is found in the same module map file as we saved. If the
4778 // top-level AST file is a main file, skip this check because there is no
4779 // usable header search context.
4780 assert(!F.ModuleName.empty() &&
4781 "MODULE_NAME should come before MODULE_MAP_FILE");
4782 auto [MaybeM, IgnoreError] =
4783 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4784 if (MaybeM.has_value()) {
4785 // An implicitly-loaded module file should have its module listed in some
4786 // module map file that we've already loaded.
4787 Module *M = MaybeM.value();
4788 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4789 OptionalFileEntryRef ModMap =
4790 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4791 if (!IgnoreError && !ModMap) {
4792 if (M && M->Directory)
4793 Diag(DiagID: diag::remark_module_relocated)
4794 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4795
4796 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4797 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4798 // This module was defined by an imported (explicit) module.
4799 Diag(DiagID: diag::err_module_file_conflict)
4800 << F.ModuleName << F.FileName << *ASTFileName;
4801 // TODO: Add a note with the module map paths if they differ.
4802 } else {
4803 // This module was built with a different module map.
4804 Diag(DiagID: diag::err_imported_module_not_found)
4805 << F.ModuleName << F.FileName
4806 << (ImportedBy ? ImportedBy->FileName.str() : "")
4807 << F.ModuleMapPath << !ImportedBy;
4808 // In case it was imported by a PCH, there's a chance the user is
4809 // just missing to include the search path to the directory containing
4810 // the modulemap.
4811 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4812 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4813 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4814 }
4815 }
4816 return OutOfDate;
4817 }
4818
4819 assert(M && M->Name == F.ModuleName && "found module with different name");
4820
4821 // Check the primary module map file.
4822 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4823 if (!StoredModMap || *StoredModMap != ModMap) {
4824 assert(ModMap && "found module is missing module map file");
4825 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4826 "top-level import should be verified");
4827 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4828 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4829 Diag(DiagID: diag::err_imported_module_modmap_changed)
4830 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4831 << ModMap->getName() << F.ModuleMapPath << NotImported;
4832 return OutOfDate;
4833 }
4834
4835 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4836 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4837 // FIXME: we should use input files rather than storing names.
4838 std::string Filename = ReadPath(F, Record, Idx);
4839 auto SF = FileMgr.getOptionalFileRef(Filename);
4840 if (!SF) {
4841 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4842 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4843 return OutOfDate;
4844 }
4845 AdditionalStoredMaps.insert(V: *SF);
4846 }
4847
4848 // Check any additional module map files (e.g. module.private.modulemap)
4849 // that are not in the pcm.
4850 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4851 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4852 // Remove files that match
4853 // Note: SmallPtrSet::erase is really remove
4854 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4855 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4856 Diag(DiagID: diag::err_module_different_modmap)
4857 << F.ModuleName << /*new*/0 << ModMap.getName();
4858 return OutOfDate;
4859 }
4860 }
4861 }
4862
4863 // Check any additional module map files that are in the pcm, but not
4864 // found in header search. Cases that match are already removed.
4865 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4866 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4867 Diag(DiagID: diag::err_module_different_modmap)
4868 << F.ModuleName << /*not new*/1 << ModMap.getName();
4869 return OutOfDate;
4870 }
4871 }
4872
4873 if (Listener)
4874 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4875 return Success;
4876}
4877
4878void ASTReader::ReadDirectoryDependencies(const RecordData &Record,
4879 ModuleFile &F) {
4880 unsigned Idx = 0;
4881 unsigned N = Record[Idx++];
4882 F.DirectoryDependencies.reserve(n: N);
4883 for (unsigned I = 0; I != N; ++I) {
4884 // The base directory is relative if the module was found through a
4885 // relative search path, and empty with -fmodule-file-home-is-cwd.
4886 SmallString<256> Dir(ReadPath(F, Record, Idx));
4887 ModuleMap::canonicalizeDirectoryDependency(FileMgr, Path&: Dir);
4888 F.DirectoryDependencies.push_back(x: std::string(Dir));
4889 }
4890}
4891
4892bool ASTReader::isDirectoryDependencyOutOfDate(ModuleFile &F, bool Complain) {
4893 llvm::vfs::FileSystem &FS = PP.getFileManager().getVirtualFileSystem();
4894 // Adding or removing an entry updates the modification time of the directory
4895 // holding it, so comparing every directory in the tree against the module
4896 // file catches a header that was added, even one whose own modification time
4897 // is older. A directory that doesn't exist has no listing to depend on.
4898 auto IsNewer = [&](StringRef Dir) {
4899 llvm::ErrorOr<llvm::vfs::Status> Status = FS.status(Path: Dir);
4900 return Status && Status->isDirectory() &&
4901 llvm::sys::toTimeT(TP: Status->getLastModificationTime()) > F.ModTime;
4902 };
4903
4904 ModuleCache &ModCache = getModuleManager().getModuleCache();
4905 const HeaderSearchOptions &HSOpts =
4906 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4907 for (StringRef Dir : F.DirectoryDependencies) {
4908 if (std::optional<bool> Invalidated =
4909 ModCache.isDirectoryInvalidated(Directory: Dir)) {
4910 // The build system reports paths as changed before the build session
4911 // started, so a module built during it is newer than them.
4912 if (!*Invalidated ||
4913 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
4914 continue;
4915 Diag(DiagID: diag::remark_module_path_invalidated) << F.ModuleName << Dir;
4916 if (Complain)
4917 Diag(DiagID: diag::err_module_path_invalidated) << F.ModuleName << Dir;
4918 return true;
4919 }
4920
4921 std::optional<std::string> Changed;
4922 if (IsNewer(Dir)) {
4923 Changed = Dir.str();
4924 } else {
4925 std::error_code EC;
4926 for (llvm::vfs::recursive_directory_iterator I(FS, Dir, EC), E;
4927 I != E && !EC; I.increment(EC)) {
4928 if (I->type() == llvm::sys::fs::file_type::directory_file &&
4929 IsNewer(I->path())) {
4930 Changed = I->path().str();
4931 break;
4932 }
4933 }
4934 }
4935 if (!Changed)
4936 continue;
4937 Diag(DiagID: diag::remark_module_directory_dep_changed) << F.ModuleName << *Changed;
4938 if (Complain)
4939 Diag(DiagID: diag::err_module_directory_dep_changed) << F.ModuleName << *Changed;
4940 return true;
4941 }
4942 return false;
4943}
4944
4945/// Move the given method to the back of the global list of methods.
4946static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4947 // Find the entry for this selector in the method pool.
4948 SemaObjC::GlobalMethodPool::iterator Known =
4949 S.ObjC().MethodPool.find(Val: Method->getSelector());
4950 if (Known == S.ObjC().MethodPool.end())
4951 return;
4952
4953 // Retrieve the appropriate method list.
4954 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4955 : Known->second.second;
4956 bool Found = false;
4957 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4958 if (!Found) {
4959 if (List->getMethod() == Method) {
4960 Found = true;
4961 } else {
4962 // Keep searching.
4963 continue;
4964 }
4965 }
4966
4967 if (List->getNext())
4968 List->setMethod(List->getNext()->getMethod());
4969 else
4970 List->setMethod(Method);
4971 }
4972}
4973
4974void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4975 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4976 for (Decl *D : Names) {
4977 bool wasHidden = !D->isUnconditionallyVisible();
4978 D->setVisibleDespiteOwningModule();
4979
4980 if (wasHidden && SemaObj) {
4981 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4982 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4983 }
4984 }
4985 }
4986}
4987
4988void ASTReader::makeModuleVisible(Module *Mod,
4989 Module::NameVisibilityKind NameVisibility,
4990 SourceLocation ImportLoc) {
4991 llvm::SmallPtrSet<Module *, 4> Visited;
4992 SmallVector<Module *, 4> Stack;
4993 Stack.push_back(Elt: Mod);
4994 while (!Stack.empty()) {
4995 Mod = Stack.pop_back_val();
4996
4997 if (NameVisibility <= Mod->NameVisibility) {
4998 // This module already has this level of visibility (or greater), so
4999 // there is nothing more to do.
5000 continue;
5001 }
5002
5003 if (Mod->isUnimportable()) {
5004 // Modules that aren't importable cannot be made visible.
5005 continue;
5006 }
5007
5008 // Update the module's name visibility.
5009 Mod->NameVisibility = NameVisibility;
5010
5011 // If we've already deserialized any names from this module,
5012 // mark them as visible.
5013 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
5014 if (Hidden != HiddenNamesMap.end()) {
5015 auto HiddenNames = std::move(*Hidden);
5016 HiddenNamesMap.erase(I: Hidden);
5017 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
5018 assert(!HiddenNamesMap.contains(Mod) &&
5019 "making names visible added hidden names");
5020 }
5021
5022 // Push any exported modules onto the stack to be marked as visible.
5023 SmallVector<Module *, 16> Exports;
5024 Mod->getExportedModules(Exported&: Exports);
5025 for (SmallVectorImpl<Module *>::iterator
5026 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
5027 Module *Exported = *I;
5028 if (Visited.insert(Ptr: Exported).second)
5029 Stack.push_back(Elt: Exported);
5030 }
5031 }
5032}
5033
5034/// We've merged the definition \p MergedDef into the existing definition
5035/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
5036/// visible.
5037void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
5038 NamedDecl *MergedDef) {
5039 if (!Def->isUnconditionallyVisible()) {
5040 // If MergedDef is visible or becomes visible, make the definition visible.
5041 if (MergedDef->isUnconditionallyVisible())
5042 Def->setVisibleDespiteOwningModule();
5043 else {
5044 getContext().mergeDefinitionIntoModule(
5045 ND: Def, M: MergedDef->getImportedOwningModule(),
5046 /*NotifyListeners*/ false);
5047 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
5048 }
5049 }
5050}
5051
5052bool ASTReader::loadGlobalIndex() {
5053 if (GlobalIndex)
5054 return false;
5055
5056 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
5057 !PP.getLangOpts().Modules)
5058 return true;
5059
5060 // Try to load the global index.
5061 TriedLoadingGlobalIndex = true;
5062 StringRef SpecificModuleCachePath =
5063 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
5064 std::pair<GlobalModuleIndex *, llvm::Error> Result =
5065 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
5066 if (llvm::Error Err = std::move(Result.second)) {
5067 assert(!Result.first);
5068 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5069 return true;
5070 }
5071
5072 GlobalIndex.reset(p: Result.first);
5073 ModuleMgr.setGlobalIndex(GlobalIndex.get());
5074 return false;
5075}
5076
5077bool ASTReader::isGlobalIndexUnavailable() const {
5078 return PP.getLangOpts().Modules && UseGlobalIndex &&
5079 !hasGlobalIndex() && TriedLoadingGlobalIndex;
5080}
5081
5082/// Given a cursor at the start of an AST file, scan ahead and drop the
5083/// cursor into the start of the given block ID, returning false on success and
5084/// true on failure.
5085static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
5086 while (true) {
5087 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
5088 if (!MaybeEntry) {
5089 // FIXME this drops errors on the floor.
5090 consumeError(Err: MaybeEntry.takeError());
5091 return true;
5092 }
5093 llvm::BitstreamEntry Entry = MaybeEntry.get();
5094
5095 switch (Entry.Kind) {
5096 case llvm::BitstreamEntry::Error:
5097 case llvm::BitstreamEntry::EndBlock:
5098 return true;
5099
5100 case llvm::BitstreamEntry::Record:
5101 // Ignore top-level records.
5102 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
5103 break;
5104 else {
5105 // FIXME this drops errors on the floor.
5106 consumeError(Err: Skipped.takeError());
5107 return true;
5108 }
5109
5110 case llvm::BitstreamEntry::SubBlock:
5111 if (Entry.ID == BlockID) {
5112 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
5113 // FIXME this drops the error on the floor.
5114 consumeError(Err: std::move(Err));
5115 return true;
5116 }
5117 // Found it!
5118 return false;
5119 }
5120
5121 if (llvm::Error Err = Cursor.SkipBlock()) {
5122 // FIXME this drops the error on the floor.
5123 consumeError(Err: std::move(Err));
5124 return true;
5125 }
5126 }
5127 }
5128}
5129
5130ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5131 ModuleKind Type,
5132 SourceLocation ImportLoc,
5133 unsigned ClientLoadCapabilities,
5134 ModuleFile **NewLoadedModuleFile) {
5135 llvm::TimeTraceScope scope("ReadAST", FileName);
5136
5137 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5138 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5139 CurrentDeserializingModuleKind, Type);
5140
5141 // Defer any pending actions until we get to the end of reading the AST file.
5142 Deserializing AnASTFile(this);
5143
5144 // Bump the generation number.
5145 unsigned PreviousGeneration = 0;
5146 if (ContextObj)
5147 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5148
5149 unsigned NumModules = ModuleMgr.size();
5150 SmallVector<ImportedModule, 4> Loaded;
5151 if (ASTReadResult ReadResult =
5152 ReadASTCore(FileName, Type, ImportLoc,
5153 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5154 ClientLoadCapabilities)) {
5155 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5156
5157 // If we find that any modules are unusable, the global index is going
5158 // to be out-of-date. Just remove it.
5159 GlobalIndex.reset();
5160 ModuleMgr.setGlobalIndex(nullptr);
5161 return ReadResult;
5162 }
5163
5164 if (NewLoadedModuleFile && !Loaded.empty())
5165 *NewLoadedModuleFile = Loaded.back().Mod;
5166
5167 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5168 // remove modules from this point. Various fields are updated during reading
5169 // the AST block and removing the modules would result in dangling pointers.
5170 // They are generally only incidentally dereferenced, ie. a binary search
5171 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5172 // be dereferenced but it wouldn't actually be used.
5173
5174 // Load the AST blocks of all of the modules that we loaded. We can still
5175 // hit errors parsing the ASTs at this point.
5176 for (ImportedModule &M : Loaded) {
5177 ModuleFile &F = *M.Mod;
5178 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5179
5180 // Read the AST block.
5181 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5182 Error(Err: std::move(Err));
5183 return Failure;
5184 }
5185
5186 // The AST block should always have a definition for the main module.
5187 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5188 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5189 return Failure;
5190 }
5191
5192 // Read the extension blocks.
5193 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5194 if (llvm::Error Err = ReadExtensionBlock(F)) {
5195 Error(Err: std::move(Err));
5196 return Failure;
5197 }
5198 }
5199
5200 // Once read, set the ModuleFile bit base offset and update the size in
5201 // bits of all files we've seen.
5202 F.GlobalBitOffset = TotalModulesSizeInBits;
5203 TotalModulesSizeInBits += F.SizeInBits;
5204 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5205 }
5206
5207 // Preload source locations and interesting indentifiers.
5208 for (ImportedModule &M : Loaded) {
5209 ModuleFile &F = *M.Mod;
5210
5211 // Map the original source file ID into the ID space of the current
5212 // compilation.
5213 if (F.OriginalSourceFileID.isValid())
5214 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5215
5216 for (auto Offset : F.PreloadIdentifierOffsets) {
5217 const unsigned char *Data = F.IdentifierTableData + Offset;
5218
5219 ASTIdentifierLookupTrait Trait(*this, F);
5220 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5221 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5222
5223 IdentifierInfo *II;
5224 if (!PP.getLangOpts().CPlusPlus) {
5225 // Identifiers present in both the module file and the importing
5226 // instance are marked out-of-date so that they can be deserialized
5227 // on next use via ASTReader::updateOutOfDateIdentifier().
5228 // Identifiers present in the module file but not in the importing
5229 // instance are ignored for now, preventing growth of the identifier
5230 // table. They will be deserialized on first use via ASTReader::get().
5231 auto It = PP.getIdentifierTable().find(Name: Key);
5232 if (It == PP.getIdentifierTable().end())
5233 continue;
5234 II = It->second;
5235 } else {
5236 // With C++ modules, not many identifiers are considered interesting.
5237 // All identifiers in the module file can be placed into the identifier
5238 // table of the importing instance and marked as out-of-date. This makes
5239 // ASTReader::get() a no-op, and deserialization will take place on
5240 // first/next use via ASTReader::updateOutOfDateIdentifier().
5241 II = &PP.getIdentifierTable().getOwn(Name: Key);
5242 }
5243
5244 II->setOutOfDate(true);
5245
5246 // Mark this identifier as being from an AST file so that we can track
5247 // whether we need to serialize it.
5248 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5249
5250 // Associate the ID with the identifier so that the writer can reuse it.
5251 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5252 SetIdentifierInfo(ID, II);
5253 }
5254 }
5255
5256 // Builtins and library builtins have already been initialized. Mark all
5257 // identifiers as out-of-date, so that they are deserialized on first use.
5258 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5259 for (auto &Id : PP.getIdentifierTable())
5260 Id.second->setOutOfDate(true);
5261
5262 // Mark selectors as out of date.
5263 for (const auto &Sel : SelectorGeneration)
5264 SelectorOutOfDate[Sel.first] = true;
5265
5266 // Setup the import locations and notify the module manager that we've
5267 // committed to these module files.
5268 for (ImportedModule &M : Loaded) {
5269 ModuleFile &F = *M.Mod;
5270
5271 ModuleMgr.moduleFileAccepted(MF: &F);
5272
5273 // Set the import location.
5274 F.DirectImportLoc = ImportLoc;
5275 // FIXME: We assume that locations from PCH / preamble do not need
5276 // any translation.
5277 if (!M.ImportedBy)
5278 F.ImportLoc = M.ImportLoc;
5279 else
5280 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5281 }
5282
5283 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5284 // Might be unnecessary as use declarations are only used to build the
5285 // module itself.
5286
5287 if (ContextObj)
5288 InitializeContext();
5289
5290 if (SemaObj)
5291 UpdateSema();
5292
5293 if (DeserializationListener)
5294 DeserializationListener->ReaderInitialized(Reader: this);
5295
5296 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5297 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5298 // If this AST file is a precompiled preamble, then set the
5299 // preamble file ID of the source manager to the file source file
5300 // from which the preamble was built.
5301 if (Type == MK_Preamble) {
5302 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5303 } else if (Type == MK_MainFile) {
5304 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5305 }
5306 }
5307
5308 // For any Objective-C class definitions we have already loaded, make sure
5309 // that we load any additional categories.
5310 if (ContextObj) {
5311 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5312 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5313 D: ObjCClassesLoaded[I], PreviousGeneration);
5314 }
5315 }
5316
5317 const HeaderSearchOptions &HSOpts =
5318 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5319 if (HSOpts.ModulesValidateOncePerBuildSession) {
5320 // Now we are certain that the module and all modules it depends on are
5321 // up-to-date. For implicitly-built module files, ensure the corresponding
5322 // timestamp files are up-to-date in this build session.
5323 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5324 ImportedModule &M = Loaded[I];
5325 if (M.Mod->Kind == MK_ImplicitModule &&
5326 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5327 getModuleManager().getModuleCache().updateModuleTimestamp(
5328 ModuleFilename: M.Mod->FileName);
5329 }
5330 }
5331
5332 return Success;
5333}
5334
5335static ASTFileSignature readASTFileSignature(StringRef PCH);
5336
5337/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5338static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5339 // FIXME checking magic headers is done in other places such as
5340 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5341 // always done the same. Unify it all with a helper.
5342 if (!Stream.canSkipToPos(pos: 4))
5343 return llvm::createStringError(
5344 EC: std::errc::illegal_byte_sequence,
5345 Fmt: "file too small to contain precompiled file magic");
5346 for (unsigned C : {'C', 'P', 'C', 'H'})
5347 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5348 if (Res.get() != C)
5349 return llvm::createStringError(
5350 EC: std::errc::illegal_byte_sequence,
5351 Fmt: "file doesn't start with precompiled file magic");
5352 } else
5353 return Res.takeError();
5354 return llvm::Error::success();
5355}
5356
5357static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5358 switch (Kind) {
5359 case MK_PCH:
5360 return 0; // PCH
5361 case MK_ImplicitModule:
5362 case MK_ExplicitModule:
5363 case MK_PrebuiltModule:
5364 return 1; // module
5365 case MK_MainFile:
5366 case MK_Preamble:
5367 return 2; // main source file
5368 }
5369 llvm_unreachable("unknown module kind");
5370}
5371
5372ASTReader::ASTReadResult ASTReader::ReadASTCore(
5373 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5374 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5375 off_t ExpectedSize, time_t ExpectedModTime,
5376 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5377 auto Result = ModuleMgr.addModule(
5378 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5379 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5380 ModuleFile *M = Result.getModule();
5381
5382 switch (Result.getKind()) {
5383 case AddModuleResult::AlreadyLoaded: {
5384 Diag(DiagID: diag::remark_module_import)
5385 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5386 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5387 return Success;
5388 }
5389
5390 case AddModuleResult::NewlyLoaded:
5391 // Load module file below.
5392 break;
5393
5394 case AddModuleResult::Missing:
5395 // The module file was missing; if the client can handle that, return
5396 // it.
5397 if (ClientLoadCapabilities & ARR_Missing)
5398 return Missing;
5399
5400 // Otherwise, return an error.
5401 Diag(DiagID: diag::err_ast_file_not_found)
5402 << moduleKindForDiagnostic(Kind: Type) << FileName;
5403 if (!Result.getBufferError().empty())
5404 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5405 return Failure;
5406
5407 case AddModuleResult::OutOfDate:
5408 // We couldn't load the module file because it is out-of-date. If the
5409 // client can handle out-of-date, return it.
5410 if (ClientLoadCapabilities & ARR_OutOfDate)
5411 return OutOfDate;
5412
5413 // Otherwise, return an error.
5414 Diag(DiagID: diag::err_ast_file_out_of_date)
5415 << moduleKindForDiagnostic(Kind: Type) << FileName;
5416 for (const auto &C : Result.getChanges()) {
5417 Diag(DiagID: diag::note_fe_ast_file_modified)
5418 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5419 << llvm::itostr(X: C.New.value_or(u: 0));
5420 }
5421 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5422 << Result.getValidationStatus();
5423 if (!Result.getSignatureError().empty())
5424 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5425 return Failure;
5426
5427 case AddModuleResult::None:
5428 llvm_unreachable("Unexpected value from adding module.");
5429 }
5430
5431 assert(M && "Missing module file");
5432
5433 bool ShouldFinalizePCM = false;
5434 llvm::scope_exit FinalizeOrDropPCM([&]() {
5435 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5436 if (ShouldFinalizePCM)
5437 MC.finalizePCM(Filename: FileName);
5438 else
5439 MC.tryToDropPCM(Filename: FileName);
5440 });
5441 ModuleFile &F = *M;
5442 BitstreamCursor &Stream = F.Stream;
5443 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5444 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5445
5446 // Sniff for the signature.
5447 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5448 Diag(DiagID: diag::err_ast_file_invalid)
5449 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5450 return Failure;
5451 }
5452
5453 // This is used for compatibility with older PCH formats.
5454 bool HaveReadControlBlock = false;
5455 while (true) {
5456 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5457 if (!MaybeEntry) {
5458 Error(Err: MaybeEntry.takeError());
5459 return Failure;
5460 }
5461 llvm::BitstreamEntry Entry = MaybeEntry.get();
5462
5463 switch (Entry.Kind) {
5464 case llvm::BitstreamEntry::Error:
5465 case llvm::BitstreamEntry::Record:
5466 case llvm::BitstreamEntry::EndBlock:
5467 Error(Msg: "invalid record at top-level of AST file");
5468 return Failure;
5469
5470 case llvm::BitstreamEntry::SubBlock:
5471 break;
5472 }
5473
5474 switch (Entry.ID) {
5475 case CONTROL_BLOCK_ID:
5476 HaveReadControlBlock = true;
5477 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5478 case Success:
5479 // Check that we didn't try to load a non-module AST file as a module.
5480 //
5481 // FIXME: Should we also perform the converse check? Loading a module as
5482 // a PCH file sort of works, but it's a bit wonky.
5483 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5484 Type == MK_PrebuiltModule) &&
5485 F.ModuleName.empty()) {
5486 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5487 if (Result != OutOfDate ||
5488 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5489 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5490 return Result;
5491 }
5492 break;
5493
5494 case Failure: return Failure;
5495 case Missing: return Missing;
5496 case OutOfDate: return OutOfDate;
5497 case VersionMismatch: return VersionMismatch;
5498 case ConfigurationMismatch: return ConfigurationMismatch;
5499 case HadErrors: return HadErrors;
5500 }
5501 break;
5502
5503 case AST_BLOCK_ID:
5504 if (!HaveReadControlBlock) {
5505 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5506 Diag(DiagID: diag::err_ast_file_version_too_old)
5507 << moduleKindForDiagnostic(Kind: Type) << FileName;
5508 return VersionMismatch;
5509 }
5510
5511 // Record that we've loaded this module.
5512 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5513 ShouldFinalizePCM = true;
5514 return Success;
5515
5516 default:
5517 if (llvm::Error Err = Stream.SkipBlock()) {
5518 Error(Err: std::move(Err));
5519 return Failure;
5520 }
5521 break;
5522 }
5523 }
5524
5525 llvm_unreachable("unexpected break; expected return");
5526}
5527
5528ASTReader::ASTReadResult
5529ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5530 unsigned ClientLoadCapabilities) {
5531 const HeaderSearchOptions &HSOpts =
5532 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5533 bool AllowCompatibleConfigurationMismatch =
5534 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5535 bool DisableValidation = shouldDisableValidationForFile(M: F);
5536
5537 ASTReadResult Result = readUnhashedControlBlockImpl(
5538 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5539 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5540 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5541
5542 // If F was directly imported by another module, it's implicitly validated by
5543 // the importing module.
5544 if (DisableValidation || WasImportedBy ||
5545 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5546 return Success;
5547
5548 if (Result == Failure) {
5549 Error(Msg: "malformed block record in AST file");
5550 return Failure;
5551 }
5552
5553 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5554 // If this module has already been finalized in the ModuleCache, we're stuck
5555 // with it; we can only load a single version of each module.
5556 //
5557 // This can happen when a module is imported in two contexts: in one, as a
5558 // user module; in another, as a system module (due to an import from
5559 // another module marked with the [system] flag). It usually indicates a
5560 // bug in the module map: this module should also be marked with [system].
5561 //
5562 // If -Wno-system-headers (the default), and the first import is as a
5563 // system module, then validation will fail during the as-user import,
5564 // since -Werror flags won't have been validated. However, it's reasonable
5565 // to treat this consistently as a system module.
5566 //
5567 // If -Wsystem-headers, the PCM on disk was built with
5568 // -Wno-system-headers, and the first import is as a user module, then
5569 // validation will fail during the as-system import since the PCM on disk
5570 // doesn't guarantee that -Werror was respected. However, the -Werror
5571 // flags were checked during the initial as-user import.
5572 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5573 Filename: F.FileName)) {
5574 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5575 return Success;
5576 }
5577 }
5578
5579 return Result;
5580}
5581
5582ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5583 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5584 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5585 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5586 // Initialize a stream.
5587 BitstreamCursor Stream(StreamData);
5588
5589 // Sniff for the signature.
5590 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5591 // FIXME this drops the error on the floor.
5592 consumeError(Err: std::move(Err));
5593 return Failure;
5594 }
5595
5596 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5597 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5598 return Failure;
5599
5600 // Read all of the records in the options block.
5601 RecordData Record;
5602 ASTReadResult Result = Success;
5603 while (true) {
5604 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5605 if (!MaybeEntry) {
5606 // FIXME this drops the error on the floor.
5607 consumeError(Err: MaybeEntry.takeError());
5608 return Failure;
5609 }
5610 llvm::BitstreamEntry Entry = MaybeEntry.get();
5611
5612 switch (Entry.Kind) {
5613 case llvm::BitstreamEntry::Error:
5614 case llvm::BitstreamEntry::SubBlock:
5615 return Failure;
5616
5617 case llvm::BitstreamEntry::EndBlock:
5618 return Result;
5619
5620 case llvm::BitstreamEntry::Record:
5621 // The interesting case.
5622 break;
5623 }
5624
5625 // Read and process a record.
5626 Record.clear();
5627 StringRef Blob;
5628 Expected<unsigned> MaybeRecordType =
5629 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5630 if (!MaybeRecordType) {
5631 // FIXME this drops the error.
5632 return Failure;
5633 }
5634 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5635 case SIGNATURE:
5636 if (F) {
5637 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5638 assert(F->Signature != ASTFileSignature::createDummy() &&
5639 "Dummy AST file signature not backpatched in ASTWriter.");
5640 }
5641 break;
5642 case AST_BLOCK_HASH:
5643 if (F) {
5644 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5645 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5646 "Dummy AST block hash not backpatched in ASTWriter.");
5647 }
5648 break;
5649 case DIAGNOSTIC_OPTIONS: {
5650 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5651 if (Listener && ValidateDiagnosticOptions &&
5652 !AllowCompatibleConfigurationMismatch &&
5653 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5654 Result = OutOfDate; // Don't return early. Read the signature.
5655 break;
5656 }
5657 case HEADER_SEARCH_PATHS: {
5658 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5659 if (Listener && !AllowCompatibleConfigurationMismatch &&
5660 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5661 Result = ConfigurationMismatch;
5662 break;
5663 }
5664 case DIAG_PRAGMA_MAPPINGS:
5665 if (!F)
5666 break;
5667 if (F->PragmaDiagMappings.empty())
5668 F->PragmaDiagMappings.swap(RHS&: Record);
5669 else
5670 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5671 From: Record.begin(), To: Record.end());
5672 break;
5673 case HEADER_SEARCH_ENTRY_USAGE:
5674 if (F)
5675 F->SearchPathUsage = ReadBitVector(Record, Blob);
5676 break;
5677 case VFS_USAGE:
5678 if (F)
5679 F->VFSUsage = ReadBitVector(Record, Blob);
5680 break;
5681 }
5682 }
5683}
5684
5685/// Parse a record and blob containing module file extension metadata.
5686static bool parseModuleFileExtensionMetadata(
5687 const SmallVectorImpl<uint64_t> &Record,
5688 StringRef Blob,
5689 ModuleFileExtensionMetadata &Metadata) {
5690 if (Record.size() < 4) return true;
5691
5692 Metadata.MajorVersion = Record[0];
5693 Metadata.MinorVersion = Record[1];
5694
5695 unsigned BlockNameLen = Record[2];
5696 unsigned UserInfoLen = Record[3];
5697
5698 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5699
5700 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5701 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5702 Blob.data() + BlockNameLen + UserInfoLen);
5703 return false;
5704}
5705
5706llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5707 BitstreamCursor &Stream = F.Stream;
5708
5709 RecordData Record;
5710 while (true) {
5711 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5712 if (!MaybeEntry)
5713 return MaybeEntry.takeError();
5714 llvm::BitstreamEntry Entry = MaybeEntry.get();
5715
5716 switch (Entry.Kind) {
5717 case llvm::BitstreamEntry::SubBlock:
5718 if (llvm::Error Err = Stream.SkipBlock())
5719 return Err;
5720 continue;
5721 case llvm::BitstreamEntry::EndBlock:
5722 return llvm::Error::success();
5723 case llvm::BitstreamEntry::Error:
5724 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5725 Fmt: "malformed block record in AST file");
5726 case llvm::BitstreamEntry::Record:
5727 break;
5728 }
5729
5730 Record.clear();
5731 StringRef Blob;
5732 Expected<unsigned> MaybeRecCode =
5733 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5734 if (!MaybeRecCode)
5735 return MaybeRecCode.takeError();
5736 switch (MaybeRecCode.get()) {
5737 case EXTENSION_METADATA: {
5738 ModuleFileExtensionMetadata Metadata;
5739 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5740 return llvm::createStringError(
5741 EC: std::errc::illegal_byte_sequence,
5742 Fmt: "malformed EXTENSION_METADATA in AST file");
5743
5744 // Find a module file extension with this block name.
5745 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5746 if (Known == ModuleFileExtensions.end()) break;
5747
5748 // Form a reader.
5749 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5750 Mod&: F, Stream)) {
5751 F.ExtensionReaders.push_back(x: std::move(Reader));
5752 }
5753
5754 break;
5755 }
5756 }
5757 }
5758
5759 llvm_unreachable("ReadExtensionBlock should return from while loop");
5760}
5761
5762void ASTReader::InitializeContext() {
5763 assert(ContextObj && "no context to initialize");
5764 ASTContext &Context = *ContextObj;
5765
5766 // If there's a listener, notify them that we "read" the translation unit.
5767 if (DeserializationListener)
5768 DeserializationListener->DeclRead(
5769 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5770 D: Context.getTranslationUnitDecl());
5771
5772 // FIXME: Find a better way to deal with collisions between these
5773 // built-in types. Right now, we just ignore the problem.
5774
5775 // Load the special types.
5776 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5777 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5778 if (!Context.CFConstantStringTypeDecl)
5779 Context.setCFConstantStringType(GetType(ID: String));
5780 }
5781
5782 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5783 QualType FileType = GetType(ID: File);
5784 if (FileType.isNull()) {
5785 Error(Msg: "FILE type is NULL");
5786 return;
5787 }
5788
5789 if (!Context.FILEDecl) {
5790 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5791 Context.setFILEDecl(Typedef->getDecl());
5792 else {
5793 const TagType *Tag = FileType->getAs<TagType>();
5794 if (!Tag) {
5795 Error(Msg: "Invalid FILE type in AST file");
5796 return;
5797 }
5798 Context.setFILEDecl(Tag->getDecl());
5799 }
5800 }
5801 }
5802
5803 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5804 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5805 if (Jmp_bufType.isNull()) {
5806 Error(Msg: "jmp_buf type is NULL");
5807 return;
5808 }
5809
5810 if (!Context.jmp_bufDecl) {
5811 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5812 Context.setjmp_bufDecl(Typedef->getDecl());
5813 else {
5814 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5815 if (!Tag) {
5816 Error(Msg: "Invalid jmp_buf type in AST file");
5817 return;
5818 }
5819 Context.setjmp_bufDecl(Tag->getDecl());
5820 }
5821 }
5822 }
5823
5824 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5825 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5826 if (Sigjmp_bufType.isNull()) {
5827 Error(Msg: "sigjmp_buf type is NULL");
5828 return;
5829 }
5830
5831 if (!Context.sigjmp_bufDecl) {
5832 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5833 Context.setsigjmp_bufDecl(Typedef->getDecl());
5834 else {
5835 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5836 assert(Tag && "Invalid sigjmp_buf type in AST file");
5837 Context.setsigjmp_bufDecl(Tag->getDecl());
5838 }
5839 }
5840 }
5841
5842 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5843 if (Context.ObjCIdRedefinitionType.isNull())
5844 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5845 }
5846
5847 if (TypeID ObjCClassRedef =
5848 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5849 if (Context.ObjCClassRedefinitionType.isNull())
5850 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5851 }
5852
5853 if (TypeID ObjCSelRedef =
5854 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5855 if (Context.ObjCSelRedefinitionType.isNull())
5856 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5857 }
5858
5859 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5860 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5861 if (Ucontext_tType.isNull()) {
5862 Error(Msg: "ucontext_t type is NULL");
5863 return;
5864 }
5865
5866 if (!Context.ucontext_tDecl) {
5867 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5868 Context.setucontext_tDecl(Typedef->getDecl());
5869 else {
5870 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5871 assert(Tag && "Invalid ucontext_t type in AST file");
5872 Context.setucontext_tDecl(Tag->getDecl());
5873 }
5874 }
5875 }
5876 }
5877
5878 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5879
5880 // If there were any CUDA special declarations, deserialize them.
5881 if (!CUDASpecialDeclRefs.empty()) {
5882 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5883 Context.setcudaConfigureCallDecl(
5884 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5885 Context.setcudaGetParameterBufferDecl(
5886 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5887 Context.setcudaLaunchDeviceDecl(
5888 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5889 }
5890
5891 // Re-export any modules that were imported by a non-module AST file.
5892 // FIXME: This does not make macro-only imports visible again.
5893 for (auto &Import : PendingImportedModules) {
5894 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5895 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5896 /*ImportLoc=*/Import.ImportLoc);
5897 if (Import.ImportLoc.isValid())
5898 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5899 // This updates visibility for Preprocessor only. For Sema, which can be
5900 // nullptr here, we do the same later, in UpdateSema().
5901 }
5902 }
5903
5904 // Hand off these modules to Sema.
5905 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5906 PendingImportedModules.clear();
5907}
5908
5909void ASTReader::finalizeForWriting() {
5910 // Nothing to do for now.
5911}
5912
5913/// Reads and return the signature record from \p PCH's control block, or
5914/// else returns 0.
5915static ASTFileSignature readASTFileSignature(StringRef PCH) {
5916 BitstreamCursor Stream(PCH);
5917 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5918 // FIXME this drops the error on the floor.
5919 consumeError(Err: std::move(Err));
5920 return ASTFileSignature();
5921 }
5922
5923 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5924 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5925 return ASTFileSignature();
5926
5927 // Scan for SIGNATURE inside the diagnostic options block.
5928 ASTReader::RecordData Record;
5929 while (true) {
5930 Expected<llvm::BitstreamEntry> MaybeEntry =
5931 Stream.advanceSkippingSubblocks();
5932 if (!MaybeEntry) {
5933 // FIXME this drops the error on the floor.
5934 consumeError(Err: MaybeEntry.takeError());
5935 return ASTFileSignature();
5936 }
5937 llvm::BitstreamEntry Entry = MaybeEntry.get();
5938
5939 if (Entry.Kind != llvm::BitstreamEntry::Record)
5940 return ASTFileSignature();
5941
5942 Record.clear();
5943 StringRef Blob;
5944 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5945 if (!MaybeRecord) {
5946 // FIXME this drops the error on the floor.
5947 consumeError(Err: MaybeRecord.takeError());
5948 return ASTFileSignature();
5949 }
5950 if (SIGNATURE == MaybeRecord.get()) {
5951 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5952 assert(Signature != ASTFileSignature::createDummy() &&
5953 "Dummy AST file signature not backpatched in ASTWriter.");
5954 return Signature;
5955 }
5956 }
5957}
5958
5959/// Retrieve the name of the original source file name
5960/// directly from the AST file, without actually loading the AST
5961/// file.
5962std::string ASTReader::getOriginalSourceFile(
5963 const std::string &ASTFileName, FileManager &FileMgr,
5964 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5965 // Open the AST file.
5966 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5967 /*RequiresNullTerminator=*/false,
5968 /*MaybeLimit=*/std::nullopt,
5969 /*IsText=*/false);
5970 if (!Buffer) {
5971 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5972 << ASTFileName << Buffer.getError().message();
5973 return std::string();
5974 }
5975
5976 // Initialize the stream
5977 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5978
5979 // Sniff for the signature.
5980 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5981 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5982 return std::string();
5983 }
5984
5985 // Scan for the CONTROL_BLOCK_ID block.
5986 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5987 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5988 return std::string();
5989 }
5990
5991 // Scan for ORIGINAL_FILE inside the control block.
5992 RecordData Record;
5993 while (true) {
5994 Expected<llvm::BitstreamEntry> MaybeEntry =
5995 Stream.advanceSkippingSubblocks();
5996 if (!MaybeEntry) {
5997 // FIXME this drops errors on the floor.
5998 consumeError(Err: MaybeEntry.takeError());
5999 return std::string();
6000 }
6001 llvm::BitstreamEntry Entry = MaybeEntry.get();
6002
6003 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
6004 return std::string();
6005
6006 if (Entry.Kind != llvm::BitstreamEntry::Record) {
6007 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
6008 return std::string();
6009 }
6010
6011 Record.clear();
6012 StringRef Blob;
6013 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6014 if (!MaybeRecord) {
6015 // FIXME this drops the errors on the floor.
6016 consumeError(Err: MaybeRecord.takeError());
6017 return std::string();
6018 }
6019 if (ORIGINAL_FILE == MaybeRecord.get())
6020 return Blob.str();
6021 }
6022}
6023
6024namespace {
6025
6026 class SimplePCHValidator : public ASTReaderListener {
6027 const LangOptions &ExistingLangOpts;
6028 const CodeGenOptions &ExistingCGOpts;
6029 const TargetOptions &ExistingTargetOpts;
6030 const PreprocessorOptions &ExistingPPOpts;
6031 const HeaderSearchOptions &ExistingHSOpts;
6032 std::string ExistingSpecificModuleCachePath;
6033 FileManager &FileMgr;
6034 bool StrictOptionMatches;
6035
6036 public:
6037 SimplePCHValidator(const LangOptions &ExistingLangOpts,
6038 const CodeGenOptions &ExistingCGOpts,
6039 const TargetOptions &ExistingTargetOpts,
6040 const PreprocessorOptions &ExistingPPOpts,
6041 const HeaderSearchOptions &ExistingHSOpts,
6042 StringRef ExistingSpecificModuleCachePath,
6043 FileManager &FileMgr, bool StrictOptionMatches)
6044 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
6045 ExistingTargetOpts(ExistingTargetOpts),
6046 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
6047 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
6048 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
6049
6050 bool ReadLanguageOptions(const LangOptions &LangOpts,
6051 StringRef ModuleFilename, bool Complain,
6052 bool AllowCompatibleDifferences) override {
6053 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
6054 Diags: nullptr, AllowCompatibleDifferences);
6055 }
6056
6057 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
6058 StringRef ModuleFilename, bool Complain,
6059 bool AllowCompatibleDifferences) override {
6060 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
6061 Diags: nullptr, AllowCompatibleDifferences);
6062 }
6063
6064 bool ReadTargetOptions(const TargetOptions &TargetOpts,
6065 StringRef ModuleFilename, bool Complain,
6066 bool AllowCompatibleDifferences) override {
6067 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
6068 Diags: nullptr, AllowCompatibleDifferences);
6069 }
6070
6071 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
6072 StringRef ASTFilename, StringRef ContextHash,
6073 bool Complain) override {
6074 return checkModuleCachePath(
6075 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
6076 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
6077 }
6078
6079 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
6080 StringRef ModuleFilename, bool ReadMacros,
6081 bool Complain,
6082 std::string &SuggestedPredefines) override {
6083 return checkPreprocessorOptions(
6084 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
6085 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
6086 Validation: StrictOptionMatches ? OptionValidateStrictMatches
6087 : OptionValidateContradictions);
6088 }
6089 };
6090
6091} // namespace
6092
6093bool ASTReader::readASTFileControlBlock(
6094 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6095 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
6096 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
6097 unsigned ClientLoadCapabilities) {
6098 // Open the AST file.
6099 off_t Size;
6100 time_t ModTime;
6101 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
6102 llvm::MemoryBuffer *Buffer =
6103 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
6104 if (!Buffer) {
6105 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
6106 // read again later, but we do not have the context here to determine if it
6107 // is safe to change the result of InMemoryModuleCache::getPCMState().
6108
6109 // FIXME: This allows use of the VFS; we do not allow use of the
6110 // VFS when actually loading a module.
6111 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
6112 : FileMgr.getFileRef(Filename,
6113 /*OpenFile=*/false,
6114 /*CacheFailure=*/true,
6115 /*IsText=*/false);
6116 if (!Entry) {
6117 llvm::consumeError(Err: Entry.takeError());
6118 return true;
6119 }
6120 auto BufferOrErr =
6121 FileMgr.getBufferForFile(Entry: *Entry,
6122 /*IsVolatile=*/isVolatile: false,
6123 /*RequiresNullTerminator=*/false,
6124 /*MaybeLimit=*/std::nullopt,
6125 /*IsText=*/false);
6126 if (!BufferOrErr)
6127 return true;
6128 OwnedBuffer = std::move(*BufferOrErr);
6129 Buffer = OwnedBuffer.get();
6130 }
6131
6132 // Initialize the stream
6133 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6134 BitstreamCursor Stream(Bytes);
6135
6136 // Sniff for the signature.
6137 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6138 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6139 return true;
6140 }
6141
6142 // Scan for the CONTROL_BLOCK_ID block.
6143 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6144 return true;
6145
6146 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6147 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6148 bool NeedsImports = Listener.needsImportVisitation();
6149 BitstreamCursor InputFilesCursor;
6150 uint64_t InputFilesOffsetBase = 0;
6151
6152 RecordData Record;
6153 std::string ModuleDir;
6154 bool DoneWithControlBlock = false;
6155 SmallString<0> PathBuf;
6156 PathBuf.reserve(N: 256);
6157 // Additional path buffer to use when multiple paths need to be resolved.
6158 // For example, when deserializing input files that contains a path that was
6159 // resolved from a vfs overlay and an external location.
6160 SmallString<0> AdditionalPathBuf;
6161 AdditionalPathBuf.reserve(N: 256);
6162 while (!DoneWithControlBlock) {
6163 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6164 if (!MaybeEntry) {
6165 // FIXME this drops the error on the floor.
6166 consumeError(Err: MaybeEntry.takeError());
6167 return true;
6168 }
6169 llvm::BitstreamEntry Entry = MaybeEntry.get();
6170
6171 switch (Entry.Kind) {
6172 case llvm::BitstreamEntry::SubBlock: {
6173 switch (Entry.ID) {
6174 case OPTIONS_BLOCK_ID: {
6175 std::string IgnoredSuggestedPredefines;
6176 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6177 /*AllowCompatibleConfigurationMismatch*/ false,
6178 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6179 return true;
6180 break;
6181 }
6182
6183 case INPUT_FILES_BLOCK_ID:
6184 InputFilesCursor = Stream;
6185 if (llvm::Error Err = Stream.SkipBlock()) {
6186 // FIXME this drops the error on the floor.
6187 consumeError(Err: std::move(Err));
6188 return true;
6189 }
6190 if (NeedsInputFiles &&
6191 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6192 return true;
6193 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6194 break;
6195
6196 default:
6197 if (llvm::Error Err = Stream.SkipBlock()) {
6198 // FIXME this drops the error on the floor.
6199 consumeError(Err: std::move(Err));
6200 return true;
6201 }
6202 break;
6203 }
6204
6205 continue;
6206 }
6207
6208 case llvm::BitstreamEntry::EndBlock:
6209 DoneWithControlBlock = true;
6210 break;
6211
6212 case llvm::BitstreamEntry::Error:
6213 return true;
6214
6215 case llvm::BitstreamEntry::Record:
6216 break;
6217 }
6218
6219 if (DoneWithControlBlock) break;
6220
6221 Record.clear();
6222 StringRef Blob;
6223 Expected<unsigned> MaybeRecCode =
6224 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6225 if (!MaybeRecCode) {
6226 // FIXME this drops the error.
6227 return Failure;
6228 }
6229 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6230 case METADATA:
6231 if (Record[0] != VERSION_MAJOR)
6232 return true;
6233 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6234 return true;
6235 break;
6236 case MODULE_NAME:
6237 Listener.ReadModuleName(ModuleName: Blob);
6238 break;
6239 case MODULE_DIRECTORY:
6240 ModuleDir = std::string(Blob);
6241 break;
6242 case MODULE_MAP_FILE: {
6243 unsigned Idx = 0;
6244 std::string PathStr = ReadString(Record, Idx);
6245 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6246 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6247 break;
6248 }
6249 case INPUT_FILE_OFFSETS: {
6250 if (!NeedsInputFiles)
6251 break;
6252
6253 unsigned NumInputFiles = Record[0];
6254 unsigned NumUserFiles = Record[1];
6255 const llvm::support::unaligned_uint64_t *InputFileOffs =
6256 (const llvm::support::unaligned_uint64_t *)Blob.data();
6257 for (unsigned I = 0; I != NumInputFiles; ++I) {
6258 // Go find this input file.
6259 bool isSystemFile = I >= NumUserFiles;
6260
6261 if (isSystemFile && !NeedsSystemInputFiles)
6262 break; // the rest are system input files
6263
6264 BitstreamCursor &Cursor = InputFilesCursor;
6265 SavedStreamPosition SavedPosition(Cursor);
6266 if (llvm::Error Err =
6267 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6268 // FIXME this drops errors on the floor.
6269 consumeError(Err: std::move(Err));
6270 }
6271
6272 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6273 if (!MaybeCode) {
6274 // FIXME this drops errors on the floor.
6275 consumeError(Err: MaybeCode.takeError());
6276 }
6277 unsigned Code = MaybeCode.get();
6278
6279 RecordData Record;
6280 StringRef Blob;
6281 bool shouldContinue = false;
6282 Expected<unsigned> MaybeRecordType =
6283 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6284 if (!MaybeRecordType) {
6285 // FIXME this drops errors on the floor.
6286 consumeError(Err: MaybeRecordType.takeError());
6287 }
6288 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6289 case INPUT_FILE_HASH:
6290 break;
6291 case INPUT_FILE:
6292 time_t StoredTime = static_cast<time_t>(Record[2]);
6293 bool Overridden = static_cast<bool>(Record[3]);
6294 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6295 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6296 auto FilenameAsRequestedBuf = ResolveImportedPath(
6297 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6298 StringRef Filename;
6299 if (UnresolvedFilename.empty())
6300 Filename = *FilenameAsRequestedBuf;
6301 else {
6302 auto FilenameBuf = ResolveImportedPath(
6303 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6304 Filename = *FilenameBuf;
6305 }
6306 shouldContinue = Listener.visitInputFileAsRequested(
6307 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6308 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6309 break;
6310 }
6311 if (!shouldContinue)
6312 break;
6313 }
6314 break;
6315 }
6316
6317 case IMPORT: {
6318 if (!NeedsImports)
6319 break;
6320
6321 unsigned Idx = 0;
6322 // Read information about the AST file.
6323
6324 // Skip Kind
6325 Idx++;
6326
6327 // Skip ImportLoc
6328 Idx++;
6329
6330 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6331
6332 bool IsStandardCXXModule = Record[Idx++];
6333
6334 // In C++20 Modules, we don't record the path to imported
6335 // modules in the BMI files.
6336 if (IsStandardCXXModule) {
6337 Listener.visitImport(ModuleName, /*Filename=*/"");
6338 continue;
6339 }
6340
6341 // Skip Size, ModTime and ImplicitModuleSuffix.
6342 Idx += 1 + 1 + 1;
6343 // Skip signature.
6344 Blob = Blob.substr(Start: ASTFileSignature::size);
6345
6346 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6347 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6348 Listener.visitImport(ModuleName, Filename: *Filename);
6349 break;
6350 }
6351
6352 default:
6353 // No other validation to perform.
6354 break;
6355 }
6356 }
6357
6358 // Look for module file extension blocks, if requested.
6359 if (FindModuleFileExtensions) {
6360 BitstreamCursor SavedStream = Stream;
6361 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6362 bool DoneWithExtensionBlock = false;
6363 while (!DoneWithExtensionBlock) {
6364 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6365 if (!MaybeEntry) {
6366 // FIXME this drops the error.
6367 return true;
6368 }
6369 llvm::BitstreamEntry Entry = MaybeEntry.get();
6370
6371 switch (Entry.Kind) {
6372 case llvm::BitstreamEntry::SubBlock:
6373 if (llvm::Error Err = Stream.SkipBlock()) {
6374 // FIXME this drops the error on the floor.
6375 consumeError(Err: std::move(Err));
6376 return true;
6377 }
6378 continue;
6379
6380 case llvm::BitstreamEntry::EndBlock:
6381 DoneWithExtensionBlock = true;
6382 continue;
6383
6384 case llvm::BitstreamEntry::Error:
6385 return true;
6386
6387 case llvm::BitstreamEntry::Record:
6388 break;
6389 }
6390
6391 Record.clear();
6392 StringRef Blob;
6393 Expected<unsigned> MaybeRecCode =
6394 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6395 if (!MaybeRecCode) {
6396 // FIXME this drops the error.
6397 return true;
6398 }
6399 switch (MaybeRecCode.get()) {
6400 case EXTENSION_METADATA: {
6401 ModuleFileExtensionMetadata Metadata;
6402 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6403 return true;
6404
6405 Listener.readModuleFileExtension(Metadata);
6406 break;
6407 }
6408 }
6409 }
6410 }
6411 Stream = std::move(SavedStream);
6412 }
6413
6414 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6415 if (readUnhashedControlBlockImpl(
6416 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6417 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6418 ValidateDiagnosticOptions) != Success)
6419 return true;
6420
6421 return false;
6422}
6423
6424bool ASTReader::isAcceptableASTFile(
6425 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6426 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6427 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6428 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6429 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6430 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6431 SpecificModuleCachePath, FileMgr,
6432 RequireStrictOptionMatches);
6433 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6434 /*FindModuleFileExtensions=*/false, Listener&: validator,
6435 /*ValidateDiagnosticOptions=*/true);
6436}
6437
6438Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6439 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6440 assert(GlobalID == 0 && "Unhandled global submodule ID");
6441 return nullptr;
6442 }
6443
6444 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6445 if (GlobalIndex >= SubmodulesLoaded.size()) {
6446 Error(Msg: "submodule ID out of range in AST file");
6447 return nullptr;
6448 }
6449
6450 if (SubmodulesLoaded[GlobalIndex])
6451 return SubmodulesLoaded[GlobalIndex];
6452
6453 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6454 assert(It != GlobalSubmoduleMap.end());
6455 ModuleFile &F = *It->second;
6456 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6457 [[maybe_unused]] unsigned LocalID =
6458 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6459
6460 BitstreamCursor &Cursor = F.SubmodulesCursor;
6461 SavedStreamPosition SavedPosition(Cursor);
6462 unsigned Offset = F.SubmoduleOffsets[Index];
6463 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.SubmodulesOffsetBase + Offset)) {
6464 Error(Err: std::move(Err));
6465 return nullptr;
6466 }
6467
6468 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6469 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6470 // If we don't know the top-level module, there's no point in doing qualified
6471 // lookup of its submodules; it won't find anything anywhere within this tree.
6472 // Let's skip that and avoid some string lookups.
6473 auto CreateModule = !KnowsTopLevelModule
6474 ? &ModuleMap::createModule
6475 : &ModuleMap::findOrCreateModuleFirst;
6476
6477 Module *CurrentModule = nullptr;
6478 RecordData Record;
6479 while (true) {
6480 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6481 if (!MaybeEntry) {
6482 Error(Err: MaybeEntry.takeError());
6483 return nullptr;
6484 }
6485 llvm::BitstreamEntry Entry = MaybeEntry.get();
6486
6487 switch (Entry.Kind) {
6488 case llvm::BitstreamEntry::SubBlock:
6489 case llvm::BitstreamEntry::Error:
6490 case llvm::BitstreamEntry::EndBlock: {
6491 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6492 Fmt: "malformed block record in AST file"));
6493 return nullptr;
6494 }
6495 case llvm::BitstreamEntry::Record:
6496 // The interesting case.
6497 break;
6498 }
6499
6500 // Read a record.
6501 StringRef Blob;
6502 Record.clear();
6503 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6504 if (!MaybeKind) {
6505 Error(Err: MaybeKind.takeError());
6506 return nullptr;
6507 }
6508 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6509
6510 switch (Kind) {
6511 case SUBMODULE_END:
6512 if (!CurrentModule) {
6513 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6514 Fmt: "malformed module definition"));
6515 return nullptr;
6516 }
6517 return CurrentModule;
6518
6519 case SUBMODULE_DEFINITION: {
6520 if (Record.size() < 13) {
6521 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6522 Fmt: "malformed module definition"));
6523 return nullptr;
6524 }
6525
6526 StringRef Name = Blob;
6527 unsigned Idx = 0;
6528 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6529 assert(LocalID == ReadLocalID);
6530 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6531 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6532 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6533 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6534 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6535 bool IsFramework = Record[Idx++];
6536 bool IsExplicit = Record[Idx++];
6537 bool IsSystem = Record[Idx++];
6538 bool IsExternC = Record[Idx++];
6539 bool InferSubmodules = Record[Idx++];
6540 bool InferExplicitSubmodules = Record[Idx++];
6541 bool InferExportWildcard = Record[Idx++];
6542 bool ConfigMacrosExhaustive = Record[Idx++];
6543 bool ModuleMapIsPrivate = Record[Idx++];
6544 bool NamedModuleHasInit = Record[Idx++];
6545
6546 Module *ParentModule = nullptr;
6547 if (Parent) {
6548 ParentModule = getSubmodule(GlobalID: Parent);
6549 if (!ParentModule)
6550 return nullptr;
6551 }
6552
6553 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6554 args&: IsFramework, args&: IsExplicit);
6555
6556 if (!ParentModule) {
6557 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6558 CurrentModule->getASTFileKey()) {
6559 // Don't emit module relocation error if we have -fno-validate-pch
6560 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6561 DisableValidationForModuleKind::Module)) {
6562 assert(*CurFileKey != F.FileKey &&
6563 "ModuleManager did not de-duplicate");
6564
6565 Diag(DiagID: diag::err_module_file_conflict)
6566 << CurrentModule->getTopLevelModuleName()
6567 << *CurrentModule->getASTFileName() << F.FileName;
6568
6569 auto CurModMapFile =
6570 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6571 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6572 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6573 Diag(DiagID: diag::note_module_file_conflict)
6574 << CurModMapFile->getName() << ModMapFile->getName();
6575
6576 return nullptr;
6577 }
6578 }
6579
6580 F.DidReadTopLevelSubmodule = true;
6581 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6582 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6583 }
6584
6585 CurrentModule->Kind = Kind;
6586 // Note that we may be rewriting an existing location and it is important
6587 // to keep doing that. In particular, we would like to prefer a
6588 // `DefinitionLoc` loaded from the module file instead of the location
6589 // created in the current source manager, because it allows the new
6590 // location to be marked as "unaffecting" when writing and avoid creating
6591 // duplicate locations for the same module map file.
6592 CurrentModule->DefinitionLoc = DefinitionLoc;
6593 CurrentModule->Signature = F.Signature;
6594 CurrentModule->IsFromModuleFile = true;
6595 if (InferredAllowedBy.isValid())
6596 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6597 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6598 CurrentModule->IsExternC = IsExternC;
6599 CurrentModule->InferSubmodules = InferSubmodules;
6600 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6601 CurrentModule->InferExportWildcard = InferExportWildcard;
6602 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6603 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6604 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6605
6606 if (!ParentModule && !F.BaseDirectory.empty()) {
6607 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6608 CurrentModule->Directory = *Dir;
6609 } else if (ParentModule && ParentModule->Directory) {
6610 // Submodules inherit the directory from their parent.
6611 CurrentModule->Directory = ParentModule->Directory;
6612 }
6613
6614 if (DeserializationListener)
6615 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6616
6617 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6618
6619 // Clear out data that will be replaced by what is in the module file.
6620 CurrentModule->LinkLibraries.clear();
6621 CurrentModule->ConfigMacros.clear();
6622 CurrentModule->UnresolvedConflicts.clear();
6623 CurrentModule->Conflicts.clear();
6624
6625 // The module is available unless it's missing a requirement; relevant
6626 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6627 // Missing headers that were present when the module was built do not
6628 // make it unavailable -- if we got this far, this must be an explicitly
6629 // imported module file.
6630 CurrentModule->Requirements.clear();
6631 CurrentModule->MissingHeaders.clear();
6632 CurrentModule->IsUnimportable =
6633 ParentModule && ParentModule->IsUnimportable;
6634 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6635 break;
6636 }
6637
6638 case SUBMODULE_UMBRELLA_HEADER: {
6639 SmallString<128> RelativePathName;
6640 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6641 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6642 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6643 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6644 PathRelativeToRootModuleDirectory: RelativePathName);
6645 }
6646 // Note that it's too late at this point to return out of date if the
6647 // name from the PCM doesn't match up with the one in the module map,
6648 // but also quite unlikely since we will have already checked the
6649 // modification time and size of the module map file itself.
6650 }
6651 break;
6652 }
6653
6654 case SUBMODULE_HEADER:
6655 case SUBMODULE_EXCLUDED_HEADER:
6656 case SUBMODULE_PRIVATE_HEADER:
6657 // We lazily associate headers with their modules via the HeaderInfo table.
6658 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6659 // of complete filenames or remove it entirely.
6660 break;
6661
6662 case SUBMODULE_TEXTUAL_HEADER:
6663 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6664 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6665 // them here.
6666 break;
6667
6668 case SUBMODULE_TOPHEADER: {
6669 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6670 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6671 break;
6672 }
6673
6674 case SUBMODULE_UMBRELLA_DIR: {
6675 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6676 if (auto Umbrella =
6677 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6678 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6679 // FIXME: NameAsWritten
6680 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6681 }
6682 }
6683 break;
6684 }
6685
6686 case SUBMODULE_IMPORTS:
6687 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6688 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6689 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6690 }
6691 break;
6692
6693 case SUBMODULE_AFFECTING_MODULES:
6694 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6695 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6696 CurrentModule->AffectingClangModules.push_back(
6697 Elt: ModuleRef(this, GlobalID));
6698 }
6699 break;
6700
6701 case SUBMODULE_EXPORTS:
6702 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6703 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6704 bool IsWildcard = Record[Idx + 1];
6705 ModuleRef ExportedMod =
6706 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6707 if (ExportedMod || IsWildcard)
6708 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6709 }
6710
6711 // Once we've loaded the set of exports, there's no reason to keep
6712 // the parsed, unresolved exports around.
6713 CurrentModule->UnresolvedExports.clear();
6714 break;
6715
6716 case SUBMODULE_REQUIRES:
6717 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6718 Target: PP.getTargetInfo());
6719 break;
6720
6721 case SUBMODULE_LINK_LIBRARY:
6722 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6723 CurrentModule->LinkLibraries.push_back(
6724 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6725 break;
6726
6727 case SUBMODULE_CONFIG_MACRO:
6728 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6729 break;
6730
6731 case SUBMODULE_CONFLICT: {
6732 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6733 Module::Conflict Conflict;
6734 Conflict.Other = ModuleRef(this, GlobalID);
6735 Conflict.Message = Blob.str();
6736 CurrentModule->Conflicts.push_back(x: Conflict);
6737 break;
6738 }
6739
6740 case SUBMODULE_INITIALIZERS: {
6741 if (!ContextObj)
6742 break;
6743 // Standard C++ module has its own way to initialize variables.
6744 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6745 SmallVector<GlobalDeclID, 16> Inits;
6746 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6747 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6748 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6749 }
6750 break;
6751 }
6752
6753 case SUBMODULE_EXPORT_AS:
6754 CurrentModule->ExportAsModule = Blob.str();
6755 ModMap.addLinkAsDependency(Mod: CurrentModule);
6756 break;
6757
6758 case SUBMODULE_CHILD: {
6759 // Record a not-yet-loaded direct child for on-demand deserialization.
6760 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6761 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6762 break;
6763 }
6764 }
6765 }
6766}
6767
6768/// Parse the record that corresponds to a LangOptions data
6769/// structure.
6770///
6771/// This routine parses the language options from the AST file and then gives
6772/// them to the AST listener if one is set.
6773///
6774/// \returns true if the listener deems the file unacceptable, false otherwise.
6775bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6776 StringRef ModuleFilename, bool Complain,
6777 ASTReaderListener &Listener,
6778 bool AllowCompatibleDifferences) {
6779 LangOptions LangOpts;
6780 unsigned Idx = 0;
6781 LangOpts.LangStd = static_cast<LangStandard::Kind>(Record[Idx++]);
6782#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6783 LangOpts.Name = Record[Idx++];
6784#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6785 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6786#include "clang/Basic/LangOptions.def"
6787#define SANITIZER(NAME, ID) \
6788 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6789#include "clang/Basic/Sanitizers.def"
6790
6791 for (unsigned N = Record[Idx++]; N; --N)
6792 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6793
6794 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6795 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6796 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6797
6798 LangOpts.CurrentModule = ReadString(Record, Idx);
6799
6800 // Comment options.
6801 for (unsigned N = Record[Idx++]; N; --N) {
6802 LangOpts.CommentOpts.BlockCommandNames.push_back(
6803 x: ReadString(Record, Idx));
6804 }
6805 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6806
6807 // OpenMP offloading options.
6808 for (unsigned N = Record[Idx++]; N; --N) {
6809 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6810 }
6811
6812 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6813
6814 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6815 AllowCompatibleDifferences);
6816}
6817
6818bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6819 StringRef ModuleFilename, bool Complain,
6820 ASTReaderListener &Listener,
6821 bool AllowCompatibleDifferences) {
6822 unsigned Idx = 0;
6823 CodeGenOptions CGOpts;
6824 using CK = CodeGenOptions::CompatibilityKind;
6825#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6826 if constexpr (CK::Compatibility != CK::Benign) \
6827 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6828#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6829 if constexpr (CK::Compatibility != CK::Benign) \
6830 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6831#define DEBUGOPT(Name, Bits, Default, Compatibility)
6832#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6833#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6834#include "clang/Basic/CodeGenOptions.def"
6835
6836 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6837 AllowCompatibleDifferences);
6838}
6839
6840bool ASTReader::ParseTargetOptions(const RecordData &Record,
6841 StringRef ModuleFilename, bool Complain,
6842 ASTReaderListener &Listener,
6843 bool AllowCompatibleDifferences) {
6844 unsigned Idx = 0;
6845 TargetOptions TargetOpts;
6846 TargetOpts.Triple = ReadString(Record, Idx);
6847 TargetOpts.CPU = ReadString(Record, Idx);
6848 TargetOpts.TuneCPU = ReadString(Record, Idx);
6849 TargetOpts.ABI = ReadString(Record, Idx);
6850 for (unsigned N = Record[Idx++]; N; --N) {
6851 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6852 }
6853 for (unsigned N = Record[Idx++]; N; --N) {
6854 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6855 }
6856
6857 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6858 AllowCompatibleDifferences);
6859}
6860
6861bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6862 StringRef ModuleFilename, bool Complain,
6863 ASTReaderListener &Listener) {
6864 DiagnosticOptions DiagOpts;
6865 unsigned Idx = 0;
6866#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6867#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6868 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6869#include "clang/Basic/DiagnosticOptions.def"
6870
6871 for (unsigned N = Record[Idx++]; N; --N)
6872 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6873 for (unsigned N = Record[Idx++]; N; --N)
6874 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6875
6876 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6877}
6878
6879bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6880 ASTReaderListener &Listener) {
6881 FileSystemOptions FSOpts;
6882 unsigned Idx = 0;
6883 FSOpts.WorkingDir = ReadString(Record, Idx);
6884 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6885}
6886
6887bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6888 StringRef ModuleFilename,
6889 bool Complain,
6890 ASTReaderListener &Listener) {
6891 HeaderSearchOptions HSOpts;
6892 unsigned Idx = 0;
6893 HSOpts.Sysroot = ReadString(Record, Idx);
6894
6895 HSOpts.ResourceDir = ReadString(Record, Idx);
6896 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6897 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6898 HSOpts.DisableModuleHash = Record[Idx++];
6899 HSOpts.ImplicitModuleMaps = Record[Idx++];
6900 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6901 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6902 HSOpts.UseBuiltinIncludes = Record[Idx++];
6903 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6904 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6905 HSOpts.UseLibcxx = Record[Idx++];
6906 std::string ContextHash = ReadString(Record, Idx);
6907
6908 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6909 Complain);
6910}
6911
6912bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6913 ASTReaderListener &Listener) {
6914 HeaderSearchOptions HSOpts;
6915 unsigned Idx = 0;
6916
6917 // Include entries.
6918 for (unsigned N = Record[Idx++]; N; --N) {
6919 std::string Path = ReadString(Record, Idx);
6920 frontend::IncludeDirGroup Group
6921 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6922 bool IsFramework = Record[Idx++];
6923 bool IgnoreSysRoot = Record[Idx++];
6924 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6925 args&: IgnoreSysRoot);
6926 }
6927
6928 // System header prefixes.
6929 for (unsigned N = Record[Idx++]; N; --N) {
6930 std::string Prefix = ReadString(Record, Idx);
6931 bool IsSystemHeader = Record[Idx++];
6932 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6933 }
6934
6935 // VFS overlay files.
6936 for (unsigned N = Record[Idx++]; N; --N) {
6937 std::string VFSOverlayFile = ReadString(Record, Idx);
6938 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6939 }
6940
6941 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6942}
6943
6944bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6945 StringRef ModuleFilename,
6946 bool Complain,
6947 ASTReaderListener &Listener,
6948 std::string &SuggestedPredefines) {
6949 PreprocessorOptions PPOpts;
6950 unsigned Idx = 0;
6951
6952 // Macro definitions/undefs
6953 bool ReadMacros = Record[Idx++];
6954 if (ReadMacros) {
6955 for (unsigned N = Record[Idx++]; N; --N) {
6956 std::string Macro = ReadString(Record, Idx);
6957 bool IsUndef = Record[Idx++];
6958 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6959 }
6960 }
6961
6962 // Includes
6963 for (unsigned N = Record[Idx++]; N; --N) {
6964 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6965 }
6966
6967 // Macro Includes
6968 for (unsigned N = Record[Idx++]; N; --N) {
6969 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6970 }
6971
6972 PPOpts.UsePredefines = Record[Idx++];
6973 PPOpts.DetailedRecord = Record[Idx++];
6974 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6975 PPOpts.ObjCXXARCStandardLibrary =
6976 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6977 SuggestedPredefines.clear();
6978 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6979 Complain, SuggestedPredefines);
6980}
6981
6982std::pair<ModuleFile *, unsigned>
6983ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6984 GlobalPreprocessedEntityMapType::iterator
6985 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6986 assert(I != GlobalPreprocessedEntityMap.end() &&
6987 "Corrupted global preprocessed entity map");
6988 ModuleFile *M = I->second;
6989 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6990 return std::make_pair(x&: M, y&: LocalIndex);
6991}
6992
6993llvm::iterator_range<PreprocessingRecord::iterator>
6994ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6995 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6996 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6997 count: Mod.NumPreprocessedEntities);
6998
6999 return llvm::make_range(x: PreprocessingRecord::iterator(),
7000 y: PreprocessingRecord::iterator());
7001}
7002
7003bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
7004 unsigned int ClientLoadCapabilities) {
7005 return ClientLoadCapabilities & ARR_OutOfDate &&
7006 !getModuleManager()
7007 .getModuleCache()
7008 .getInMemoryModuleCache()
7009 .isPCMFinal(Filename: ModuleFileName);
7010}
7011
7012llvm::iterator_range<ASTReader::ModuleDeclIterator>
7013ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
7014 return llvm::make_range(
7015 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
7016 y: ModuleDeclIterator(this, &Mod,
7017 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
7018}
7019
7020SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
7021 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
7022 assert(I != GlobalSkippedRangeMap.end() &&
7023 "Corrupted global skipped range map");
7024 ModuleFile *M = I->second;
7025 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
7026 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
7027 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
7028 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
7029 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
7030 assert(Range.isValid());
7031 return Range;
7032}
7033
7034unsigned
7035ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
7036 unsigned ModuleFileIndex = ID >> 32;
7037 assert(ModuleFileIndex && "not translating loaded MacroID?");
7038 assert(getModuleManager().size() > ModuleFileIndex - 1);
7039 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
7040
7041 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
7042 return MF.BasePreprocessedEntityID + ID;
7043}
7044
7045PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
7046 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7047 ModuleFile &M = *PPInfo.first;
7048 unsigned LocalIndex = PPInfo.second;
7049 PreprocessedEntityID PPID =
7050 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
7051 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7052
7053 if (!PP.getPreprocessingRecord()) {
7054 Error(Msg: "no preprocessing record");
7055 return nullptr;
7056 }
7057
7058 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
7059 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
7060 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
7061 Error(Err: std::move(Err));
7062 return nullptr;
7063 }
7064
7065 Expected<llvm::BitstreamEntry> MaybeEntry =
7066 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
7067 if (!MaybeEntry) {
7068 Error(Err: MaybeEntry.takeError());
7069 return nullptr;
7070 }
7071 llvm::BitstreamEntry Entry = MaybeEntry.get();
7072
7073 if (Entry.Kind != llvm::BitstreamEntry::Record)
7074 return nullptr;
7075
7076 // Read the record.
7077 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
7078 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
7079 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
7080 StringRef Blob;
7081 RecordData Record;
7082 Expected<unsigned> MaybeRecType =
7083 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
7084 if (!MaybeRecType) {
7085 Error(Err: MaybeRecType.takeError());
7086 return nullptr;
7087 }
7088 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
7089 case PPD_MACRO_EXPANSION: {
7090 bool isBuiltin = Record[0];
7091 IdentifierInfo *Name = nullptr;
7092 MacroDefinitionRecord *Def = nullptr;
7093 if (isBuiltin)
7094 Name = getLocalIdentifier(M, LocalID: Record[1]);
7095 else {
7096 PreprocessedEntityID GlobalID =
7097 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
7098 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
7099 Def =
7100 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
7101 }
7102
7103 MacroExpansion *ME;
7104 if (isBuiltin)
7105 ME = new (PPRec) MacroExpansion(Name, Range);
7106 else
7107 ME = new (PPRec) MacroExpansion(Def, Range);
7108
7109 return ME;
7110 }
7111
7112 case PPD_MACRO_DEFINITION: {
7113 // Decode the identifier info and then check again; if the macro is
7114 // still defined and associated with the identifier,
7115 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
7116 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
7117
7118 if (DeserializationListener)
7119 DeserializationListener->MacroDefinitionRead(PPID, MD);
7120
7121 return MD;
7122 }
7123
7124 case PPD_INCLUSION_DIRECTIVE: {
7125 const char *FullFileNameStart = Blob.data() + Record[0];
7126 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7127 OptionalFileEntryRef File;
7128 if (!FullFileName.empty())
7129 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7130
7131 // FIXME: Stable encoding
7132 InclusionDirective::InclusionKind Kind
7133 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7134 InclusionDirective *ID
7135 = new (PPRec) InclusionDirective(PPRec, Kind,
7136 StringRef(Blob.data(), Record[0]),
7137 Record[1], Record[3],
7138 File,
7139 Range);
7140 return ID;
7141 }
7142 }
7143
7144 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7145}
7146
7147/// Find the next module that contains entities and return the ID
7148/// of the first entry.
7149///
7150/// \param SLocMapI points at a chunk of a module that contains no
7151/// preprocessed entities or the entities it contains are not the ones we are
7152/// looking for.
7153unsigned ASTReader::findNextPreprocessedEntity(
7154 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7155 ++SLocMapI;
7156 for (GlobalSLocOffsetMapType::const_iterator
7157 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7158 ModuleFile &M = *SLocMapI->second;
7159 if (M.NumPreprocessedEntities)
7160 return M.BasePreprocessedEntityID;
7161 }
7162
7163 return getTotalNumPreprocessedEntities();
7164}
7165
7166namespace {
7167
7168struct PPEntityComp {
7169 const ASTReader &Reader;
7170 ModuleFile &M;
7171
7172 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7173
7174 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7175 SourceLocation LHS = getLoc(PPE: L);
7176 SourceLocation RHS = getLoc(PPE: R);
7177 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7178 }
7179
7180 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7181 SourceLocation LHS = getLoc(PPE: L);
7182 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7183 }
7184
7185 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7186 SourceLocation RHS = getLoc(PPE: R);
7187 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7188 }
7189
7190 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7191 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7192 }
7193};
7194
7195} // namespace
7196
7197unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7198 bool EndsAfter) const {
7199 if (SourceMgr.isLocalSourceLocation(Loc))
7200 return getTotalNumPreprocessedEntities();
7201
7202 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7203 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7204 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7205 "Corrupted global sloc offset map");
7206
7207 if (SLocMapI->second->NumPreprocessedEntities == 0)
7208 return findNextPreprocessedEntity(SLocMapI);
7209
7210 ModuleFile &M = *SLocMapI->second;
7211
7212 using pp_iterator = const PPEntityOffset *;
7213
7214 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7215 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7216
7217 size_t Count = M.NumPreprocessedEntities;
7218 size_t Half;
7219 pp_iterator First = pp_begin;
7220 pp_iterator PPI;
7221
7222 if (EndsAfter) {
7223 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7224 comp: PPEntityComp(*this, M));
7225 } else {
7226 // Do a binary search manually instead of using std::lower_bound because
7227 // The end locations of entities may be unordered (when a macro expansion
7228 // is inside another macro argument), but for this case it is not important
7229 // whether we get the first macro expansion or its containing macro.
7230 while (Count > 0) {
7231 Half = Count / 2;
7232 PPI = First;
7233 std::advance(i&: PPI, n: Half);
7234 if (SourceMgr.isBeforeInTranslationUnit(
7235 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7236 First = PPI;
7237 ++First;
7238 Count = Count - Half - 1;
7239 } else
7240 Count = Half;
7241 }
7242 }
7243
7244 if (PPI == pp_end)
7245 return findNextPreprocessedEntity(SLocMapI);
7246
7247 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7248}
7249
7250/// Returns a pair of [Begin, End) indices of preallocated
7251/// preprocessed entities that \arg Range encompasses.
7252std::pair<unsigned, unsigned>
7253 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7254 if (Range.isInvalid())
7255 return std::make_pair(x: 0,y: 0);
7256 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7257
7258 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7259 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7260 return std::make_pair(x&: BeginID, y&: EndID);
7261}
7262
7263/// Optionally returns true or false if the preallocated preprocessed
7264/// entity with index \arg Index came from file \arg FID.
7265std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7266 FileID FID) {
7267 if (FID.isInvalid())
7268 return false;
7269
7270 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7271 ModuleFile &M = *PPInfo.first;
7272 unsigned LocalIndex = PPInfo.second;
7273 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7274
7275 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7276 if (Loc.isInvalid())
7277 return false;
7278
7279 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7280 return true;
7281 else
7282 return false;
7283}
7284
7285namespace {
7286
7287 /// Visitor used to search for information about a header file.
7288 class HeaderFileInfoVisitor {
7289 FileEntryRef FE;
7290 std::optional<HeaderFileInfo> HFI;
7291
7292 public:
7293 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7294
7295 bool operator()(ModuleFile &M) {
7296 HeaderFileInfoLookupTable *Table
7297 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7298 if (!Table)
7299 return false;
7300
7301 // Look in the on-disk hash table for an entry for this file name.
7302 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7303 if (Pos == Table->end())
7304 return false;
7305
7306 HFI = *Pos;
7307 return true;
7308 }
7309
7310 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7311 };
7312
7313} // namespace
7314
7315HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7316 HeaderFileInfoVisitor Visitor(FE);
7317 ModuleMgr.visit(Visitor);
7318 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7319 return *HFI;
7320
7321 return HeaderFileInfo();
7322}
7323
7324void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7325 using DiagState = DiagnosticsEngine::DiagState;
7326 SmallVector<DiagState *, 32> DiagStates;
7327
7328 for (ModuleFile &F : ModuleMgr) {
7329 unsigned Idx = 0;
7330 auto &Record = F.PragmaDiagMappings;
7331 if (Record.empty())
7332 continue;
7333
7334 DiagStates.clear();
7335
7336 auto ReadDiagState = [&](const DiagState &BasedOn,
7337 bool IncludeNonPragmaStates) {
7338 unsigned BackrefID = Record[Idx++];
7339 if (BackrefID != 0)
7340 return DiagStates[BackrefID - 1];
7341
7342 // A new DiagState was created here.
7343 Diag.DiagStates.push_back(x: BasedOn);
7344 DiagState *NewState = &Diag.DiagStates.back();
7345 DiagStates.push_back(Elt: NewState);
7346 unsigned Size = Record[Idx++];
7347 assert(Idx + Size * 2 <= Record.size() &&
7348 "Invalid data, not enough diag/map pairs");
7349 while (Size--) {
7350 unsigned DiagID = Record[Idx++];
7351 DiagnosticMapping NewMapping =
7352 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7353 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7354 continue;
7355
7356 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7357
7358 // If this mapping was specified as a warning but the severity was
7359 // upgraded due to diagnostic settings, simulate the current diagnostic
7360 // settings (and use a warning).
7361 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7362 NewMapping.setSeverity(diag::Severity::Warning);
7363 NewMapping.setUpgradedFromWarning(false);
7364 }
7365
7366 Mapping = NewMapping;
7367 }
7368 return NewState;
7369 };
7370
7371 // Read the first state.
7372 DiagState *FirstState;
7373 if (F.Kind == MK_ImplicitModule) {
7374 // Implicitly-built modules are reused with different diagnostic
7375 // settings. Use the initial diagnostic state from Diag to simulate this
7376 // compilation's diagnostic settings.
7377 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7378 DiagStates.push_back(Elt: FirstState);
7379
7380 // Skip the initial diagnostic state from the serialized module.
7381 assert(Record[1] == 0 &&
7382 "Invalid data, unexpected backref in initial state");
7383 Idx = 3 + Record[2] * 2;
7384 assert(Idx < Record.size() &&
7385 "Invalid data, not enough state change pairs in initial state");
7386 } else if (F.isModule()) {
7387 // For an explicit module, preserve the flags from the module build
7388 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7389 // -Wblah flags.
7390 unsigned Flags = Record[Idx++];
7391 DiagState Initial(*Diag.getDiagnosticIDs());
7392 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7393 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7394 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7395 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7396 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7397 Initial.ExtBehavior = (diag::Severity)Flags;
7398 FirstState = ReadDiagState(Initial, true);
7399
7400 assert(F.OriginalSourceFileID.isValid());
7401
7402 // Set up the root buffer of the module to start with the initial
7403 // diagnostic state of the module itself, to cover files that contain no
7404 // explicit transitions (for which we did not serialize anything).
7405 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7406 .StateTransitions.push_back(Elt: {FirstState, 0});
7407 } else {
7408 // For prefix ASTs, start with whatever the user configured on the
7409 // command line.
7410 Idx++; // Skip flags.
7411 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7412 }
7413
7414 // Read the state transitions.
7415 unsigned NumLocations = Record[Idx++];
7416 while (NumLocations--) {
7417 assert(Idx < Record.size() &&
7418 "Invalid data, missing pragma diagnostic states");
7419 FileID FID = ReadFileID(F, Record, Idx);
7420 assert(FID.isValid() && "invalid FileID for transition");
7421 unsigned Transitions = Record[Idx++];
7422
7423 // Note that we don't need to set up Parent/ParentOffset here, because
7424 // we won't be changing the diagnostic state within imported FileIDs
7425 // (other than perhaps appending to the main source file, which has no
7426 // parent).
7427 auto &F = Diag.DiagStatesByLoc.Files[FID];
7428 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7429 for (unsigned I = 0; I != Transitions; ++I) {
7430 unsigned Offset = Record[Idx++];
7431 auto *State = ReadDiagState(*FirstState, false);
7432 F.StateTransitions.push_back(Elt: {State, Offset});
7433 }
7434 }
7435
7436 // Read the final state.
7437 assert(Idx < Record.size() &&
7438 "Invalid data, missing final pragma diagnostic state");
7439 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7440 auto *CurState = ReadDiagState(*FirstState, false);
7441
7442 if (!F.isModule()) {
7443 Diag.DiagStatesByLoc.CurDiagState = CurState;
7444 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7445
7446 // Preserve the property that the imaginary root file describes the
7447 // current state.
7448 FileID NullFile;
7449 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7450 if (T.empty())
7451 T.push_back(Elt: {CurState, 0});
7452 else
7453 T[0].State = CurState;
7454 }
7455
7456 // Restore the push stack so that unmatched pushes from a preamble are
7457 // visible when the main file is parsed, allowing the corresponding
7458 // `#pragma diagnostic pop` to succeed.
7459 assert(Idx < Record.size() &&
7460 "Invalid data, missing diagnostic push stack");
7461 unsigned NumPushes = Record[Idx++];
7462 for (unsigned I = 0; I != NumPushes; ++I) {
7463 auto *State = ReadDiagState(*FirstState, false);
7464 if (!F.isModule())
7465 Diag.DiagStateOnPushStack.push_back(x: State);
7466 }
7467
7468 // Don't try to read these mappings again.
7469 Record.clear();
7470 }
7471}
7472
7473/// Get the correct cursor and offset for loading a type.
7474ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7475 auto [M, Index] = translateTypeIDToIndex(ID);
7476 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7477 M->DeclsBlockStartOffset);
7478}
7479
7480static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7481 switch (code) {
7482#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7483 case TYPE_##CODE_ID: return Type::CLASS_ID;
7484#include "clang/Serialization/TypeBitCodes.def"
7485 default:
7486 return std::nullopt;
7487 }
7488}
7489
7490/// Read and return the type with the given index..
7491///
7492/// The index is the type ID, shifted and minus the number of predefs. This
7493/// routine actually reads the record corresponding to the type at the given
7494/// location. It is a helper routine for GetType, which deals with reading type
7495/// IDs.
7496QualType ASTReader::readTypeRecord(TypeID ID) {
7497 assert(ContextObj && "reading type with no AST context");
7498 ASTContext &Context = *ContextObj;
7499 RecordLocation Loc = TypeCursorForIndex(ID);
7500 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7501
7502 // Keep track of where we are in the stream, then jump back there
7503 // after reading this type.
7504 SavedStreamPosition SavedPosition(DeclsCursor);
7505
7506 ReadingKindTracker ReadingKind(Read_Type, *this);
7507
7508 // Note that we are loading a type record.
7509 Deserializing AType(this);
7510
7511 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7512 Error(Err: std::move(Err));
7513 return QualType();
7514 }
7515 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7516 if (!RawCode) {
7517 Error(Err: RawCode.takeError());
7518 return QualType();
7519 }
7520
7521 ASTRecordReader Record(*this, *Loc.F);
7522 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7523 if (!Code) {
7524 Error(Err: Code.takeError());
7525 return QualType();
7526 }
7527 if (Code.get() == TYPE_EXT_QUAL) {
7528 QualType baseType = Record.readQualType();
7529 Qualifiers quals = Record.readQualifiers();
7530 return Context.getQualifiedType(T: baseType, Qs: quals);
7531 }
7532
7533 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7534 if (!maybeClass) {
7535 Error(Msg: "Unexpected code for type");
7536 return QualType();
7537 }
7538
7539 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7540 return TypeReader.read(kind: *maybeClass);
7541}
7542
7543namespace clang {
7544
7545class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7546 ASTRecordReader &Reader;
7547
7548 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7549 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7550
7551 TypeSourceInfo *GetTypeSourceInfo() {
7552 return Reader.readTypeSourceInfo();
7553 }
7554
7555 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7556 return Reader.readNestedNameSpecifierLoc();
7557 }
7558
7559 Attr *ReadAttr() {
7560 return Reader.readAttr();
7561 }
7562
7563public:
7564 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7565
7566 // We want compile-time assurance that we've enumerated all of
7567 // these, so unfortunately we have to declare them first, then
7568 // define them out-of-line.
7569#define ABSTRACT_TYPELOC(CLASS, PARENT)
7570#define TYPELOC(CLASS, PARENT) \
7571 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7572#include "clang/AST/TypeLocNodes.def"
7573
7574 void VisitFunctionTypeLoc(FunctionTypeLoc);
7575 void VisitArrayTypeLoc(ArrayTypeLoc);
7576 void VisitTagTypeLoc(TagTypeLoc TL);
7577};
7578
7579} // namespace clang
7580
7581void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7582 // nothing to do
7583}
7584
7585void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7586 TL.setBuiltinLoc(readSourceLocation());
7587 if (TL.needsExtraLocalData()) {
7588 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7589 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7590 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7591 TL.setModeAttr(Reader.readInt());
7592 }
7593}
7594
7595void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7596 TL.setNameLoc(readSourceLocation());
7597}
7598
7599void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7600 TL.setStarLoc(readSourceLocation());
7601}
7602
7603void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7604 // nothing to do
7605}
7606
7607void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7608 // nothing to do
7609}
7610
7611void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7612 // nothing to do
7613}
7614
7615void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7616 TL.setExpansionLoc(readSourceLocation());
7617}
7618
7619void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7620 TL.setCaretLoc(readSourceLocation());
7621}
7622
7623void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7624 TL.setAmpLoc(readSourceLocation());
7625}
7626
7627void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7628 TL.setAmpAmpLoc(readSourceLocation());
7629}
7630
7631void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7632 TL.setStarLoc(readSourceLocation());
7633 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7634}
7635
7636void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7637 TL.setLBracketLoc(readSourceLocation());
7638 TL.setRBracketLoc(readSourceLocation());
7639 if (Reader.readBool())
7640 TL.setSizeExpr(Reader.readExpr());
7641 else
7642 TL.setSizeExpr(nullptr);
7643}
7644
7645void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7646 VisitArrayTypeLoc(TL);
7647}
7648
7649void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7650 VisitArrayTypeLoc(TL);
7651}
7652
7653void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7654 VisitArrayTypeLoc(TL);
7655}
7656
7657void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7658 DependentSizedArrayTypeLoc TL) {
7659 VisitArrayTypeLoc(TL);
7660}
7661
7662void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7663 DependentAddressSpaceTypeLoc TL) {
7664
7665 TL.setAttrNameLoc(readSourceLocation());
7666 TL.setAttrOperandParensRange(readSourceRange());
7667 TL.setAttrExprOperand(Reader.readExpr());
7668}
7669
7670void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7671 DependentSizedExtVectorTypeLoc TL) {
7672 TL.setNameLoc(readSourceLocation());
7673}
7674
7675void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7676 TL.setNameLoc(readSourceLocation());
7677}
7678
7679void TypeLocReader::VisitDependentVectorTypeLoc(
7680 DependentVectorTypeLoc TL) {
7681 TL.setNameLoc(readSourceLocation());
7682}
7683
7684void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7685 TL.setNameLoc(readSourceLocation());
7686}
7687
7688void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7689 TL.setAttrNameLoc(readSourceLocation());
7690 TL.setAttrOperandParensRange(readSourceRange());
7691 TL.setAttrRowOperand(Reader.readExpr());
7692 TL.setAttrColumnOperand(Reader.readExpr());
7693}
7694
7695void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7696 DependentSizedMatrixTypeLoc TL) {
7697 TL.setAttrNameLoc(readSourceLocation());
7698 TL.setAttrOperandParensRange(readSourceRange());
7699 TL.setAttrRowOperand(Reader.readExpr());
7700 TL.setAttrColumnOperand(Reader.readExpr());
7701}
7702
7703void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7704 TL.setLocalRangeBegin(readSourceLocation());
7705 TL.setLParenLoc(readSourceLocation());
7706 TL.setRParenLoc(readSourceLocation());
7707 TL.setExceptionSpecRange(readSourceRange());
7708 TL.setLocalRangeEnd(readSourceLocation());
7709 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7710 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7711 }
7712}
7713
7714void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7715 VisitFunctionTypeLoc(TL);
7716}
7717
7718void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7719 VisitFunctionTypeLoc(TL);
7720}
7721
7722void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7723 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7724 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7725 SourceLocation NameLoc = readSourceLocation();
7726 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7727}
7728
7729void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7730 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7731 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7732 SourceLocation NameLoc = readSourceLocation();
7733 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7734}
7735
7736void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7737 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7738 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7739 SourceLocation NameLoc = readSourceLocation();
7740 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7741}
7742
7743void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7744 TL.setTypeofLoc(readSourceLocation());
7745 TL.setLParenLoc(readSourceLocation());
7746 TL.setRParenLoc(readSourceLocation());
7747}
7748
7749void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7750 TL.setTypeofLoc(readSourceLocation());
7751 TL.setLParenLoc(readSourceLocation());
7752 TL.setRParenLoc(readSourceLocation());
7753 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7754}
7755
7756void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7757 TL.setDecltypeLoc(readSourceLocation());
7758 TL.setRParenLoc(readSourceLocation());
7759}
7760
7761void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7762 TL.setEllipsisLoc(readSourceLocation());
7763}
7764
7765void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7766 TL.setKWLoc(readSourceLocation());
7767 TL.setLParenLoc(readSourceLocation());
7768 TL.setRParenLoc(readSourceLocation());
7769 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7770}
7771
7772ConceptReference *ASTRecordReader::readConceptReference() {
7773 auto NNS = readNestedNameSpecifierLoc();
7774 auto TemplateKWLoc = readSourceLocation();
7775 auto ConceptNameLoc = readDeclarationNameInfo();
7776 auto FoundDecl = readDeclAs<NamedDecl>();
7777 auto NamedConcept = readTemplateName();
7778 auto *CR = ConceptReference::Create(
7779 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7780 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7781 return CR;
7782}
7783
7784void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7785 TL.setNameLoc(readSourceLocation());
7786 if (Reader.readBool())
7787 TL.setConceptReference(Reader.readConceptReference());
7788 if (Reader.readBool())
7789 TL.setRParenLoc(readSourceLocation());
7790}
7791
7792void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7793 DeducedTemplateSpecializationTypeLoc TL) {
7794 TL.setElaboratedKeywordLoc(readSourceLocation());
7795 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7796 TL.setTemplateNameLoc(readSourceLocation());
7797}
7798
7799void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7800 TL.setElaboratedKeywordLoc(readSourceLocation());
7801 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7802 TL.setNameLoc(readSourceLocation());
7803}
7804
7805void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7806 VisitTagTypeLoc(TL);
7807}
7808
7809void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7810 VisitTagTypeLoc(TL);
7811}
7812
7813void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7814
7815void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7816 TL.setAttr(ReadAttr());
7817}
7818
7819void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7820 // Nothing to do
7821}
7822
7823void TypeLocReader::VisitLateParsedAttrTypeLoc(LateParsedAttrTypeLoc TL) {
7824 llvm_unreachable(
7825 "should be replaced with a concrete type before serialization");
7826}
7827
7828void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7829 // Nothing to do.
7830}
7831
7832void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7833 TL.setAttrLoc(readSourceLocation());
7834}
7835
7836void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7837 HLSLAttributedResourceTypeLoc TL) {
7838 // Nothing to do.
7839}
7840
7841void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7842 // Nothing to do.
7843}
7844
7845void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7846 TL.setNameLoc(readSourceLocation());
7847}
7848
7849void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7850 SubstTemplateTypeParmTypeLoc TL) {
7851 TL.setNameLoc(readSourceLocation());
7852}
7853
7854void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7855 SubstTemplateTypeParmPackTypeLoc TL) {
7856 TL.setNameLoc(readSourceLocation());
7857}
7858
7859void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7860 SubstBuiltinTemplatePackTypeLoc TL) {
7861 TL.setNameLoc(readSourceLocation());
7862}
7863
7864void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7865 TemplateSpecializationTypeLoc TL) {
7866 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7867 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7868 SourceLocation TemplateKeywordLoc = readSourceLocation();
7869 SourceLocation NameLoc = readSourceLocation();
7870 SourceLocation LAngleLoc = readSourceLocation();
7871 SourceLocation RAngleLoc = readSourceLocation();
7872 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7873 LAngleLoc, RAngleLoc);
7874 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7875 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7876 Args[I] = Reader.readTemplateArgumentLocInfo(
7877 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7878}
7879
7880void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7881 TL.setLParenLoc(readSourceLocation());
7882 TL.setRParenLoc(readSourceLocation());
7883}
7884
7885void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7886 TL.setElaboratedKeywordLoc(readSourceLocation());
7887 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7888 TL.setNameLoc(readSourceLocation());
7889}
7890
7891void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7892 TL.setEllipsisLoc(readSourceLocation());
7893}
7894
7895void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7896 TL.setNameLoc(readSourceLocation());
7897 TL.setNameEndLoc(readSourceLocation());
7898}
7899
7900void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7901 if (TL.getNumProtocols()) {
7902 TL.setProtocolLAngleLoc(readSourceLocation());
7903 TL.setProtocolRAngleLoc(readSourceLocation());
7904 }
7905 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7906 TL.setProtocolLoc(i, Loc: readSourceLocation());
7907}
7908
7909void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7910 TL.setHasBaseTypeAsWritten(Reader.readBool());
7911 TL.setTypeArgsLAngleLoc(readSourceLocation());
7912 TL.setTypeArgsRAngleLoc(readSourceLocation());
7913 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7914 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7915 TL.setProtocolLAngleLoc(readSourceLocation());
7916 TL.setProtocolRAngleLoc(readSourceLocation());
7917 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7918 TL.setProtocolLoc(i, Loc: readSourceLocation());
7919}
7920
7921void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7922 TL.setStarLoc(readSourceLocation());
7923}
7924
7925void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7926 TL.setKWLoc(readSourceLocation());
7927 TL.setLParenLoc(readSourceLocation());
7928 TL.setRParenLoc(readSourceLocation());
7929}
7930
7931void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7932 TL.setKWLoc(readSourceLocation());
7933}
7934
7935void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7936 TL.setNameLoc(readSourceLocation());
7937}
7938
7939void TypeLocReader::VisitDependentBitIntTypeLoc(
7940 clang::DependentBitIntTypeLoc TL) {
7941 TL.setNameLoc(readSourceLocation());
7942}
7943
7944void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7945 // Nothing to do.
7946}
7947
7948void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7949 TypeLocReader TLR(*this);
7950 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7951 TLR.Visit(TyLoc: TL);
7952}
7953
7954TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7955 QualType InfoTy = readType();
7956 if (InfoTy.isNull())
7957 return nullptr;
7958
7959 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7960 readTypeLoc(TL: TInfo->getTypeLoc());
7961 return TInfo;
7962}
7963
7964static unsigned getIndexForTypeID(serialization::TypeID ID) {
7965 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7966}
7967
7968static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7969 return ID >> 32;
7970}
7971
7972static bool isPredefinedType(serialization::TypeID ID) {
7973 // We don't need to erase the higher bits since if these bits are not 0,
7974 // it must be larger than NUM_PREDEF_TYPE_IDS.
7975 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7976}
7977
7978std::pair<ModuleFile *, unsigned>
7979ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7980 assert(!isPredefinedType(ID) &&
7981 "Predefined type shouldn't be in TypesLoaded");
7982 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7983 assert(ModuleFileIndex && "Untranslated Local Decl?");
7984
7985 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7986 assert(OwningModuleFile &&
7987 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7988
7989 return {OwningModuleFile,
7990 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7991}
7992
7993QualType ASTReader::GetType(TypeID ID) {
7994 assert(ContextObj && "reading type with no AST context");
7995 ASTContext &Context = *ContextObj;
7996
7997 unsigned FastQuals = ID & Qualifiers::FastMask;
7998
7999 if (isPredefinedType(ID)) {
8000 QualType T;
8001 unsigned Index = getIndexForTypeID(ID);
8002 switch ((PredefinedTypeIDs)Index) {
8003 case PREDEF_TYPE_LAST_ID:
8004 // We should never use this one.
8005 llvm_unreachable("Invalid predefined type");
8006 break;
8007 case PREDEF_TYPE_NULL_ID:
8008 return QualType();
8009 case PREDEF_TYPE_VOID_ID:
8010 T = Context.VoidTy;
8011 break;
8012 case PREDEF_TYPE_BOOL_ID:
8013 T = Context.BoolTy;
8014 break;
8015 case PREDEF_TYPE_CHAR_U_ID:
8016 case PREDEF_TYPE_CHAR_S_ID:
8017 // FIXME: Check that the signedness of CharTy is correct!
8018 T = Context.CharTy;
8019 break;
8020 case PREDEF_TYPE_UCHAR_ID:
8021 T = Context.UnsignedCharTy;
8022 break;
8023 case PREDEF_TYPE_USHORT_ID:
8024 T = Context.UnsignedShortTy;
8025 break;
8026 case PREDEF_TYPE_UINT_ID:
8027 T = Context.UnsignedIntTy;
8028 break;
8029 case PREDEF_TYPE_ULONG_ID:
8030 T = Context.UnsignedLongTy;
8031 break;
8032 case PREDEF_TYPE_ULONGLONG_ID:
8033 T = Context.UnsignedLongLongTy;
8034 break;
8035 case PREDEF_TYPE_UINT128_ID:
8036 T = Context.UnsignedInt128Ty;
8037 break;
8038 case PREDEF_TYPE_SCHAR_ID:
8039 T = Context.SignedCharTy;
8040 break;
8041 case PREDEF_TYPE_WCHAR_ID:
8042 T = Context.WCharTy;
8043 break;
8044 case PREDEF_TYPE_SHORT_ID:
8045 T = Context.ShortTy;
8046 break;
8047 case PREDEF_TYPE_INT_ID:
8048 T = Context.IntTy;
8049 break;
8050 case PREDEF_TYPE_LONG_ID:
8051 T = Context.LongTy;
8052 break;
8053 case PREDEF_TYPE_LONGLONG_ID:
8054 T = Context.LongLongTy;
8055 break;
8056 case PREDEF_TYPE_INT128_ID:
8057 T = Context.Int128Ty;
8058 break;
8059 case PREDEF_TYPE_BFLOAT16_ID:
8060 T = Context.BFloat16Ty;
8061 break;
8062 case PREDEF_TYPE_HALF_ID:
8063 T = Context.HalfTy;
8064 break;
8065 case PREDEF_TYPE_FLOAT_ID:
8066 T = Context.FloatTy;
8067 break;
8068 case PREDEF_TYPE_DOUBLE_ID:
8069 T = Context.DoubleTy;
8070 break;
8071 case PREDEF_TYPE_LONGDOUBLE_ID:
8072 T = Context.LongDoubleTy;
8073 break;
8074 case PREDEF_TYPE_SHORT_ACCUM_ID:
8075 T = Context.ShortAccumTy;
8076 break;
8077 case PREDEF_TYPE_ACCUM_ID:
8078 T = Context.AccumTy;
8079 break;
8080 case PREDEF_TYPE_LONG_ACCUM_ID:
8081 T = Context.LongAccumTy;
8082 break;
8083 case PREDEF_TYPE_USHORT_ACCUM_ID:
8084 T = Context.UnsignedShortAccumTy;
8085 break;
8086 case PREDEF_TYPE_UACCUM_ID:
8087 T = Context.UnsignedAccumTy;
8088 break;
8089 case PREDEF_TYPE_ULONG_ACCUM_ID:
8090 T = Context.UnsignedLongAccumTy;
8091 break;
8092 case PREDEF_TYPE_SHORT_FRACT_ID:
8093 T = Context.ShortFractTy;
8094 break;
8095 case PREDEF_TYPE_FRACT_ID:
8096 T = Context.FractTy;
8097 break;
8098 case PREDEF_TYPE_LONG_FRACT_ID:
8099 T = Context.LongFractTy;
8100 break;
8101 case PREDEF_TYPE_USHORT_FRACT_ID:
8102 T = Context.UnsignedShortFractTy;
8103 break;
8104 case PREDEF_TYPE_UFRACT_ID:
8105 T = Context.UnsignedFractTy;
8106 break;
8107 case PREDEF_TYPE_ULONG_FRACT_ID:
8108 T = Context.UnsignedLongFractTy;
8109 break;
8110 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
8111 T = Context.SatShortAccumTy;
8112 break;
8113 case PREDEF_TYPE_SAT_ACCUM_ID:
8114 T = Context.SatAccumTy;
8115 break;
8116 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
8117 T = Context.SatLongAccumTy;
8118 break;
8119 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
8120 T = Context.SatUnsignedShortAccumTy;
8121 break;
8122 case PREDEF_TYPE_SAT_UACCUM_ID:
8123 T = Context.SatUnsignedAccumTy;
8124 break;
8125 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8126 T = Context.SatUnsignedLongAccumTy;
8127 break;
8128 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8129 T = Context.SatShortFractTy;
8130 break;
8131 case PREDEF_TYPE_SAT_FRACT_ID:
8132 T = Context.SatFractTy;
8133 break;
8134 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8135 T = Context.SatLongFractTy;
8136 break;
8137 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8138 T = Context.SatUnsignedShortFractTy;
8139 break;
8140 case PREDEF_TYPE_SAT_UFRACT_ID:
8141 T = Context.SatUnsignedFractTy;
8142 break;
8143 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8144 T = Context.SatUnsignedLongFractTy;
8145 break;
8146 case PREDEF_TYPE_FLOAT16_ID:
8147 T = Context.Float16Ty;
8148 break;
8149 case PREDEF_TYPE_FLOAT128_ID:
8150 T = Context.Float128Ty;
8151 break;
8152 case PREDEF_TYPE_IBM128_ID:
8153 T = Context.Ibm128Ty;
8154 break;
8155 case PREDEF_TYPE_OVERLOAD_ID:
8156 T = Context.OverloadTy;
8157 break;
8158 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8159 T = Context.UnresolvedTemplateTy;
8160 break;
8161 case PREDEF_TYPE_BOUND_MEMBER:
8162 T = Context.BoundMemberTy;
8163 break;
8164 case PREDEF_TYPE_PSEUDO_OBJECT:
8165 T = Context.PseudoObjectTy;
8166 break;
8167 case PREDEF_TYPE_DEPENDENT_ID:
8168 T = Context.DependentTy;
8169 break;
8170 case PREDEF_TYPE_UNKNOWN_ANY:
8171 T = Context.UnknownAnyTy;
8172 break;
8173 case PREDEF_TYPE_NULLPTR_ID:
8174 T = Context.NullPtrTy;
8175 break;
8176 case PREDEF_TYPE_META_INFO_ID:
8177 T = Context.MetaInfoTy;
8178 break;
8179 case PREDEF_TYPE_CHAR8_ID:
8180 T = Context.Char8Ty;
8181 break;
8182 case PREDEF_TYPE_CHAR16_ID:
8183 T = Context.Char16Ty;
8184 break;
8185 case PREDEF_TYPE_CHAR32_ID:
8186 T = Context.Char32Ty;
8187 break;
8188 case PREDEF_TYPE_OBJC_ID:
8189 T = Context.ObjCBuiltinIdTy;
8190 break;
8191 case PREDEF_TYPE_OBJC_CLASS:
8192 T = Context.ObjCBuiltinClassTy;
8193 break;
8194 case PREDEF_TYPE_OBJC_SEL:
8195 T = Context.ObjCBuiltinSelTy;
8196 break;
8197#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8198 case PREDEF_TYPE_##Id##_ID: \
8199 T = Context.SingletonId; \
8200 break;
8201#include "clang/Basic/OpenCLImageTypes.def"
8202#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8203 case PREDEF_TYPE_##Id##_ID: \
8204 T = Context.Id##Ty; \
8205 break;
8206#include "clang/Basic/OpenCLExtensionTypes.def"
8207 case PREDEF_TYPE_SAMPLER_ID:
8208 T = Context.OCLSamplerTy;
8209 break;
8210 case PREDEF_TYPE_EVENT_ID:
8211 T = Context.OCLEventTy;
8212 break;
8213 case PREDEF_TYPE_CLK_EVENT_ID:
8214 T = Context.OCLClkEventTy;
8215 break;
8216 case PREDEF_TYPE_QUEUE_ID:
8217 T = Context.OCLQueueTy;
8218 break;
8219 case PREDEF_TYPE_RESERVE_ID_ID:
8220 T = Context.OCLReserveIDTy;
8221 break;
8222 case PREDEF_TYPE_AUTO_DEDUCT:
8223 T = Context.getAutoDeductType();
8224 break;
8225 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8226 T = Context.getAutoRRefDeductType();
8227 break;
8228 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8229 T = Context.ARCUnbridgedCastTy;
8230 break;
8231 case PREDEF_TYPE_BUILTIN_FN:
8232 T = Context.BuiltinFnTy;
8233 break;
8234 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8235 T = Context.IncompleteMatrixIdxTy;
8236 break;
8237 case PREDEF_TYPE_ARRAY_SECTION:
8238 T = Context.ArraySectionTy;
8239 break;
8240 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8241 T = Context.OMPArrayShapingTy;
8242 break;
8243 case PREDEF_TYPE_OMP_ITERATOR:
8244 T = Context.OMPIteratorTy;
8245 break;
8246#define SVE_TYPE(Name, Id, SingletonId) \
8247 case PREDEF_TYPE_##Id##_ID: \
8248 T = Context.SingletonId; \
8249 break;
8250#include "clang/Basic/AArch64ACLETypes.def"
8251#define PPC_VECTOR_TYPE(Name, Id, Size) \
8252 case PREDEF_TYPE_##Id##_ID: \
8253 T = Context.Id##Ty; \
8254 break;
8255#include "clang/Basic/PPCTypes.def"
8256#define RVV_TYPE(Name, Id, SingletonId) \
8257 case PREDEF_TYPE_##Id##_ID: \
8258 T = Context.SingletonId; \
8259 break;
8260#include "clang/Basic/RISCVVTypes.def"
8261#define WASM_TYPE(Name, Id, SingletonId) \
8262 case PREDEF_TYPE_##Id##_ID: \
8263 T = Context.SingletonId; \
8264 break;
8265#include "clang/Basic/WebAssemblyReferenceTypes.def"
8266#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8267 case PREDEF_TYPE_##Id##_ID: \
8268 T = Context.SingletonId; \
8269 break;
8270#include "clang/Basic/AMDGPUTypes.def"
8271#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8272 case PREDEF_TYPE_##Id##_ID: \
8273 T = Context.SingletonId; \
8274 break;
8275#include "clang/Basic/HLSLIntangibleTypes.def"
8276#define HLSL_PACKED_TYPE(Name, Id, SingletonId) \
8277 case PREDEF_TYPE_##Id##_ID: \
8278 T = Context.SingletonId; \
8279 break;
8280#include "clang/Basic/HLSLPackedTypes.def"
8281#define SPIRV_TYPE(Name, Id, SingletonId) \
8282 case PREDEF_TYPE_##Id##_ID: \
8283 T = Context.SingletonId; \
8284 break;
8285#include "clang/Basic/SPIRVTypes.def"
8286 }
8287
8288 assert(!T.isNull() && "Unknown predefined type");
8289 return T.withFastQualifiers(TQs: FastQuals);
8290 }
8291
8292 unsigned Index = translateTypeIDToIndex(ID).second;
8293
8294 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8295 if (TypesLoaded[Index].isNull()) {
8296 TypesLoaded[Index] = readTypeRecord(ID);
8297 if (TypesLoaded[Index].isNull())
8298 return QualType();
8299
8300 TypesLoaded[Index]->setFromAST();
8301 if (DeserializationListener)
8302 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8303 T: TypesLoaded[Index]);
8304 }
8305
8306 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8307}
8308
8309QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8310 return GetType(ID: getGlobalTypeID(F, LocalID));
8311}
8312
8313serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8314 LocalTypeID LocalID) const {
8315 if (isPredefinedType(ID: LocalID))
8316 return LocalID;
8317
8318 if (!F.ModuleOffsetMap.empty())
8319 ReadModuleOffsetMap(F);
8320
8321 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8322 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8323
8324 if (ModuleFileIndex == 0)
8325 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8326
8327 ModuleFile &MF =
8328 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8329 ModuleFileIndex = MF.Index + 1;
8330 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8331}
8332
8333TemplateArgumentLocInfo
8334ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8335 switch (Kind) {
8336 case TemplateArgument::Expression:
8337 return readExpr();
8338 case TemplateArgument::Type:
8339 return readTypeSourceInfo();
8340 case TemplateArgument::Template:
8341 case TemplateArgument::TemplateExpansion: {
8342 SourceLocation TemplateKWLoc = readSourceLocation();
8343 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8344 SourceLocation TemplateNameLoc = readSourceLocation();
8345 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8346 ? readSourceLocation()
8347 : SourceLocation();
8348 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8349 TemplateNameLoc, EllipsisLoc);
8350 }
8351 case TemplateArgument::Null:
8352 case TemplateArgument::Integral:
8353 case TemplateArgument::Declaration:
8354 case TemplateArgument::NullPtr:
8355 case TemplateArgument::StructuralValue:
8356 case TemplateArgument::Pack:
8357 // FIXME: Is this right?
8358 return TemplateArgumentLocInfo();
8359 }
8360 llvm_unreachable("unexpected template argument loc");
8361}
8362
8363TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8364 TemplateArgument Arg = readTemplateArgument();
8365
8366 if (Arg.getKind() == TemplateArgument::Expression) {
8367 if (readBool()) // bool InfoHasSameExpr.
8368 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8369 }
8370 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8371}
8372
8373void ASTRecordReader::readTemplateArgumentListInfo(
8374 TemplateArgumentListInfo &Result) {
8375 Result.setLAngleLoc(readSourceLocation());
8376 Result.setRAngleLoc(readSourceLocation());
8377 unsigned NumArgsAsWritten = readInt();
8378 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8379 Result.addArgument(Loc: readTemplateArgumentLoc());
8380}
8381
8382const ASTTemplateArgumentListInfo *
8383ASTRecordReader::readASTTemplateArgumentListInfo() {
8384 TemplateArgumentListInfo Result;
8385 readTemplateArgumentListInfo(Result);
8386 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8387}
8388
8389Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8390
8391void ASTReader::CompleteRedeclChain(const Decl *D) {
8392 if (NumCurrentElementsDeserializing) {
8393 // We arrange to not care about the complete redeclaration chain while we're
8394 // deserializing. Just remember that the AST has marked this one as complete
8395 // but that it's not actually complete yet, so we know we still need to
8396 // complete it later.
8397 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8398 return;
8399 }
8400
8401 if (!D->getDeclContext()) {
8402 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8403 return;
8404 }
8405
8406 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8407
8408 // If this is a named declaration, complete it by looking it up
8409 // within its context.
8410 //
8411 // FIXME: Merging a function definition should merge
8412 // all mergeable entities within it.
8413 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8414 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8415 if (!getContext().getLangOpts().CPlusPlus &&
8416 isa<TranslationUnitDecl>(Val: DC)) {
8417 // Outside of C++, we don't have a lookup table for the TU, so update
8418 // the identifier instead. (For C++ modules, we don't store decls
8419 // in the serialized identifier table, so we do the lookup in the TU.)
8420 auto *II = Name.getAsIdentifierInfo();
8421 assert(II && "non-identifier name in C?");
8422 if (II->isOutOfDate())
8423 updateOutOfDateIdentifier(II: *II);
8424 } else
8425 DC->lookup(Name);
8426 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8427 // Find all declarations of this kind from the relevant context.
8428 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8429 auto *DC = cast<DeclContext>(Val: DCDecl);
8430 SmallVector<Decl*, 8> Decls;
8431 FindExternalLexicalDecls(
8432 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8433 }
8434 }
8435 }
8436
8437 RedeclarableTemplateDecl *Template = nullptr;
8438 ArrayRef<TemplateArgument> Args;
8439 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8440 Template = CTSD->getSpecializedTemplate();
8441 Args = CTSD->getTemplateArgs().asArray();
8442 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8443 Template = VTSD->getSpecializedTemplate();
8444 Args = VTSD->getTemplateArgs().asArray();
8445 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8446 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8447 Template = Tmplt;
8448 Args = FD->getTemplateSpecializationArgs()->asArray();
8449 }
8450 }
8451
8452 if (Template)
8453 Template->loadLazySpecializationsImpl(Args);
8454}
8455
8456CXXCtorInitializer **
8457ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8458 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8459 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8460 SavedStreamPosition SavedPosition(Cursor);
8461 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8462 Error(Err: std::move(Err));
8463 return nullptr;
8464 }
8465 ReadingKindTracker ReadingKind(Read_Decl, *this);
8466 Deserializing D(this);
8467
8468 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8469 if (!MaybeCode) {
8470 Error(Err: MaybeCode.takeError());
8471 return nullptr;
8472 }
8473 unsigned Code = MaybeCode.get();
8474
8475 ASTRecordReader Record(*this, *Loc.F);
8476 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8477 if (!MaybeRecCode) {
8478 Error(Err: MaybeRecCode.takeError());
8479 return nullptr;
8480 }
8481 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8482 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8483 return nullptr;
8484 }
8485
8486 return Record.readCXXCtorInitializers();
8487}
8488
8489CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8490 assert(ContextObj && "reading base specifiers with no AST context");
8491 ASTContext &Context = *ContextObj;
8492
8493 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8494 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8495 SavedStreamPosition SavedPosition(Cursor);
8496 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8497 Error(Err: std::move(Err));
8498 return nullptr;
8499 }
8500 ReadingKindTracker ReadingKind(Read_Decl, *this);
8501 Deserializing D(this);
8502
8503 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8504 if (!MaybeCode) {
8505 Error(Err: MaybeCode.takeError());
8506 return nullptr;
8507 }
8508 unsigned Code = MaybeCode.get();
8509
8510 ASTRecordReader Record(*this, *Loc.F);
8511 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8512 if (!MaybeRecCode) {
8513 Error(Err: MaybeCode.takeError());
8514 return nullptr;
8515 }
8516 unsigned RecCode = MaybeRecCode.get();
8517
8518 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8519 Error(Msg: "malformed AST file: missing C++ base specifiers");
8520 return nullptr;
8521 }
8522
8523 unsigned NumBases = Record.readInt();
8524 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8525 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8526 for (unsigned I = 0; I != NumBases; ++I)
8527 Bases[I] = Record.readCXXBaseSpecifier();
8528 return Bases;
8529}
8530
8531GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8532 LocalDeclID LocalID) const {
8533 if (LocalID < NUM_PREDEF_DECL_IDS)
8534 return GlobalDeclID(LocalID.getRawValue());
8535
8536 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8537 DeclID ID = LocalID.getLocalDeclIndex();
8538
8539 if (!F.ModuleOffsetMap.empty())
8540 ReadModuleOffsetMap(F);
8541
8542 ModuleFile *OwningModuleFile =
8543 OwningModuleFileIndex == 0
8544 ? &F
8545 : F.TransitiveImports[OwningModuleFileIndex - 1];
8546
8547 if (OwningModuleFileIndex == 0)
8548 ID -= NUM_PREDEF_DECL_IDS;
8549
8550 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8551 return GlobalDeclID(NewModuleFileIndex, ID);
8552}
8553
8554bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8555 // Predefined decls aren't from any module.
8556 if (ID < NUM_PREDEF_DECL_IDS)
8557 return false;
8558
8559 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8560 return M.Index == ModuleFileIndex - 1;
8561}
8562
8563ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8564 // Predefined decls aren't from any module.
8565 if (ID < NUM_PREDEF_DECL_IDS)
8566 return nullptr;
8567
8568 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8569 assert(ModuleFileIndex && "Untranslated Local Decl?");
8570
8571 return &getModuleManager()[ModuleFileIndex - 1];
8572}
8573
8574ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8575 if (!D->isFromASTFile())
8576 return nullptr;
8577
8578 return getOwningModuleFile(ID: D->getGlobalID());
8579}
8580
8581SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8582 if (ID < NUM_PREDEF_DECL_IDS)
8583 return SourceLocation();
8584
8585 if (Decl *D = GetExistingDecl(ID))
8586 return D->getLocation();
8587
8588 SourceLocation Loc;
8589 DeclCursorForID(ID, Location&: Loc);
8590 return Loc;
8591}
8592
8593Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8594 assert(ContextObj && "reading predefined decl without AST context");
8595 ASTContext &Context = *ContextObj;
8596 Decl *NewLoaded = nullptr;
8597 switch (ID) {
8598 case PREDEF_DECL_NULL_ID:
8599 return nullptr;
8600
8601 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8602 return Context.getTranslationUnitDecl();
8603
8604 case PREDEF_DECL_OBJC_ID_ID:
8605 if (Context.ObjCIdDecl)
8606 return Context.ObjCIdDecl;
8607 NewLoaded = Context.getObjCIdDecl();
8608 break;
8609
8610 case PREDEF_DECL_OBJC_SEL_ID:
8611 if (Context.ObjCSelDecl)
8612 return Context.ObjCSelDecl;
8613 NewLoaded = Context.getObjCSelDecl();
8614 break;
8615
8616 case PREDEF_DECL_OBJC_CLASS_ID:
8617 if (Context.ObjCClassDecl)
8618 return Context.ObjCClassDecl;
8619 NewLoaded = Context.getObjCClassDecl();
8620 break;
8621
8622 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8623 if (Context.ObjCProtocolClassDecl)
8624 return Context.ObjCProtocolClassDecl;
8625 NewLoaded = Context.getObjCProtocolDecl();
8626 break;
8627
8628 case PREDEF_DECL_INT_128_ID:
8629 if (Context.Int128Decl)
8630 return Context.Int128Decl;
8631 NewLoaded = Context.getInt128Decl();
8632 break;
8633
8634 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8635 if (Context.UInt128Decl)
8636 return Context.UInt128Decl;
8637 NewLoaded = Context.getUInt128Decl();
8638 break;
8639
8640 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8641 if (Context.ObjCInstanceTypeDecl)
8642 return Context.ObjCInstanceTypeDecl;
8643 NewLoaded = Context.getObjCInstanceTypeDecl();
8644 break;
8645
8646 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8647 if (Context.BuiltinVaListDecl)
8648 return Context.BuiltinVaListDecl;
8649 NewLoaded = Context.getBuiltinVaListDecl();
8650 break;
8651
8652 case PREDEF_DECL_VA_LIST_TAG:
8653 if (Context.VaListTagDecl)
8654 return Context.VaListTagDecl;
8655 NewLoaded = Context.getVaListTagDecl();
8656 break;
8657
8658 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8659 if (Context.BuiltinMSVaListDecl)
8660 return Context.BuiltinMSVaListDecl;
8661 NewLoaded = Context.getBuiltinMSVaListDecl();
8662 break;
8663
8664 case PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID:
8665 if (Context.BuiltinZOSVaListDecl)
8666 return Context.BuiltinZOSVaListDecl;
8667 NewLoaded = Context.getBuiltinZOSVaListDecl();
8668 break;
8669
8670 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8671 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8672 return Context.getMSGuidTagDecl();
8673
8674 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8675 if (Context.ExternCContext)
8676 return Context.ExternCContext;
8677 NewLoaded = Context.getExternCContextDecl();
8678 break;
8679
8680 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8681 if (Context.CFConstantStringTypeDecl)
8682 return Context.CFConstantStringTypeDecl;
8683 NewLoaded = Context.getCFConstantStringDecl();
8684 break;
8685
8686 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8687 if (Context.CFConstantStringTagDecl)
8688 return Context.CFConstantStringTagDecl;
8689 NewLoaded = Context.getCFConstantStringTagDecl();
8690 break;
8691
8692 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8693 return Context.getMSTypeInfoTagDecl();
8694
8695#define BuiltinTemplate(BTName) \
8696 case PREDEF_DECL##BTName##_ID: \
8697 if (Context.Decl##BTName) \
8698 return Context.Decl##BTName; \
8699 NewLoaded = Context.get##BTName##Decl(); \
8700 break;
8701#include "clang/Basic/BuiltinTemplates.inc"
8702
8703 case NUM_PREDEF_DECL_IDS:
8704 llvm_unreachable("Invalid decl ID");
8705 break;
8706 }
8707
8708 assert(NewLoaded && "Failed to load predefined decl?");
8709
8710 if (DeserializationListener)
8711 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8712
8713 return NewLoaded;
8714}
8715
8716unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8717 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8718 if (!OwningModuleFile) {
8719 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8720 return GlobalID.getRawValue();
8721 }
8722
8723 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8724}
8725
8726Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8727 assert(ContextObj && "reading decl with no AST context");
8728
8729 if (ID < NUM_PREDEF_DECL_IDS) {
8730 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8731 if (D) {
8732 // Track that we have merged the declaration with ID \p ID into the
8733 // pre-existing predefined declaration \p D.
8734 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8735 if (Merged.empty())
8736 Merged.push_back(Elt: ID);
8737 }
8738 return D;
8739 }
8740
8741 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8742
8743 if (Index >= DeclsLoaded.size()) {
8744 assert(0 && "declaration ID out-of-range for AST file");
8745 Error(Msg: "declaration ID out-of-range for AST file");
8746 return nullptr;
8747 }
8748
8749 return DeclsLoaded[Index];
8750}
8751
8752Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8753 if (ID < NUM_PREDEF_DECL_IDS)
8754 return GetExistingDecl(ID);
8755
8756 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8757
8758 if (Index >= DeclsLoaded.size()) {
8759 assert(0 && "declaration ID out-of-range for AST file");
8760 Error(Msg: "declaration ID out-of-range for AST file");
8761 return nullptr;
8762 }
8763
8764 if (!DeclsLoaded[Index]) {
8765 ReadDeclRecord(ID);
8766 if (DeserializationListener)
8767 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8768 }
8769
8770 return DeclsLoaded[Index];
8771}
8772
8773LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8774 GlobalDeclID GlobalID) {
8775 if (GlobalID < NUM_PREDEF_DECL_IDS)
8776 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8777
8778 if (!M.ModuleOffsetMap.empty())
8779 ReadModuleOffsetMap(F&: M);
8780
8781 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8782 DeclID ID = GlobalID.getLocalDeclIndex();
8783
8784 if (Owner == &M) {
8785 ID += NUM_PREDEF_DECL_IDS;
8786 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8787 }
8788
8789 uint64_t OrignalModuleFileIndex = 0;
8790 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8791 if (M.TransitiveImports[I] == Owner) {
8792 OrignalModuleFileIndex = I + 1;
8793 break;
8794 }
8795
8796 if (!OrignalModuleFileIndex)
8797 return LocalDeclID();
8798
8799 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8800}
8801
8802GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8803 unsigned &Idx) {
8804 if (Idx >= Record.size()) {
8805 Error(Msg: "Corrupted AST file");
8806 return GlobalDeclID(0);
8807 }
8808
8809 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8810}
8811
8812/// Resolve the offset of a statement into a statement.
8813///
8814/// This operation will read a new statement from the external
8815/// source each time it is called, and is meant to be used via a
8816/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8817Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8818 // Switch case IDs are per Decl.
8819 ClearSwitchCaseIDs();
8820
8821 // Offset here is a global offset across the entire chain.
8822 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8823 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8824 Error(Err: std::move(Err));
8825 return nullptr;
8826 }
8827 assert(NumCurrentElementsDeserializing == 0 &&
8828 "should not be called while already deserializing");
8829 Deserializing D(this);
8830 return ReadStmtFromStream(F&: *Loc.F);
8831}
8832
8833bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8834 const Decl *D) {
8835 assert(D);
8836
8837 auto It = SpecLookups.find(Val: D);
8838 if (It == SpecLookups.end())
8839 return false;
8840
8841 // Get Decl may violate the iterator from SpecializationsLookups so we store
8842 // the DeclIDs in ahead.
8843 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8844 It->second.Table.findAll();
8845
8846 // Since we've loaded all the specializations, we can erase it from
8847 // the lookup table.
8848 SpecLookups.erase(I: It);
8849
8850 bool NewSpecsFound = false;
8851 Deserializing LookupResults(this);
8852 for (auto &Info : Infos) {
8853 if (GetExistingDecl(ID: Info))
8854 continue;
8855 NewSpecsFound = true;
8856 GetDecl(ID: Info);
8857 }
8858
8859 return NewSpecsFound;
8860}
8861
8862bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8863 assert(D);
8864
8865 CompleteRedeclChain(D);
8866 bool NewSpecsFound =
8867 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8868 if (OnlyPartial)
8869 return NewSpecsFound;
8870
8871 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8872 return NewSpecsFound;
8873}
8874
8875bool ASTReader::LoadExternalSpecializationsImpl(
8876 SpecLookupTableTy &SpecLookups, const Decl *D,
8877 ArrayRef<TemplateArgument> TemplateArgs) {
8878 assert(D);
8879
8880 auto It = SpecLookups.find(Val: D);
8881 if (It == SpecLookups.end())
8882 return false;
8883
8884 Deserializing LookupResults(this);
8885 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8886
8887 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8888 It->second.Table.find(EKey: HashValue);
8889
8890 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8891 std::string Name;
8892 llvm::raw_string_ostream OS(Name);
8893 auto *ND = cast<NamedDecl>(Val: D);
8894 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8895 /*Qualified=*/true);
8896 return Name;
8897 });
8898
8899 bool NewSpecsFound = false;
8900 for (auto &Info : Infos) {
8901 if (GetExistingDecl(ID: Info))
8902 continue;
8903 NewSpecsFound = true;
8904 GetDecl(ID: Info);
8905 }
8906
8907 return NewSpecsFound;
8908}
8909
8910bool ASTReader::LoadExternalSpecializations(
8911 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8912 assert(D);
8913
8914 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8915 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8916 NewDeclsFound |=
8917 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8918
8919 return NewDeclsFound;
8920}
8921
8922void ASTReader::FindExternalLexicalDecls(
8923 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8924 SmallVectorImpl<Decl *> &Decls) {
8925 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8926
8927 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8928 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8929 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8930 auto K = (Decl::Kind)+LexicalDecls[I];
8931 if (!IsKindWeWant(K))
8932 continue;
8933
8934 auto ID = (DeclID) + LexicalDecls[I + 1];
8935
8936 // Don't add predefined declarations to the lexical context more
8937 // than once.
8938 if (ID < NUM_PREDEF_DECL_IDS) {
8939 if (PredefsVisited[ID])
8940 continue;
8941
8942 PredefsVisited[ID] = true;
8943 }
8944
8945 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8946 assert(D->getKind() == K && "wrong kind for lexical decl");
8947 if (!DC->isDeclInLexicalTraversal(D))
8948 Decls.push_back(Elt: D);
8949 }
8950 }
8951 };
8952
8953 if (isa<TranslationUnitDecl>(Val: DC)) {
8954 for (const auto &Lexical : TULexicalDecls)
8955 Visit(Lexical.first, Lexical.second);
8956 } else {
8957 auto I = LexicalDecls.find(Val: DC);
8958 if (I != LexicalDecls.end())
8959 Visit(I->second.first, I->second.second);
8960 }
8961
8962 ++NumLexicalDeclContextsRead;
8963}
8964
8965namespace {
8966
8967class UnalignedDeclIDComp {
8968 ASTReader &Reader;
8969 ModuleFile &Mod;
8970
8971public:
8972 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8973 : Reader(Reader), Mod(M) {}
8974
8975 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8976 SourceLocation LHS = getLocation(ID: L);
8977 SourceLocation RHS = getLocation(ID: R);
8978 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8979 }
8980
8981 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8982 SourceLocation RHS = getLocation(ID: R);
8983 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8984 }
8985
8986 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8987 SourceLocation LHS = getLocation(ID: L);
8988 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8989 }
8990
8991 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8992 return Reader.getSourceManager().getFileLoc(
8993 Loc: Reader.getSourceLocationForDeclID(
8994 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8995 }
8996};
8997
8998} // namespace
8999
9000void ASTReader::FindFileRegionDecls(FileID File,
9001 unsigned Offset, unsigned Length,
9002 SmallVectorImpl<Decl *> &Decls) {
9003 SourceManager &SM = getSourceManager();
9004
9005 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
9006 if (I == FileDeclIDs.end())
9007 return;
9008
9009 FileDeclsInfo &DInfo = I->second;
9010 if (DInfo.Decls.empty())
9011 return;
9012
9013 SourceLocation
9014 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
9015 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
9016
9017 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
9018 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
9019 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
9020 if (BeginIt != DInfo.Decls.begin())
9021 --BeginIt;
9022
9023 // If we are pointing at a top-level decl inside an objc container, we need
9024 // to backtrack until we find it otherwise we will fail to report that the
9025 // region overlaps with an objc container.
9026 while (BeginIt != DInfo.Decls.begin() &&
9027 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
9028 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
9029 ->isTopLevelDeclInObjCContainer())
9030 --BeginIt;
9031
9032 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
9033 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
9034 if (EndIt != DInfo.Decls.end())
9035 ++EndIt;
9036
9037 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
9038 ++DIt)
9039 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
9040 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
9041}
9042
9043bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
9044 DeclarationName Name,
9045 const DeclContext *OriginalDC) {
9046 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
9047 "DeclContext has no visible decls in storage");
9048 if (!Name)
9049 return false;
9050
9051 // Load the list of declarations.
9052 DeclsSet DS;
9053
9054 auto Find = [&, this](auto &&Table, auto &&Key) {
9055 for (GlobalDeclID ID : Table.find(Key)) {
9056 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9057 if (ND->getDeclName() != Name)
9058 continue;
9059 // Special case for namespaces: There can be a lot of redeclarations of
9060 // some namespaces, and we import a "key declaration" per imported module.
9061 // Since all declarations of a namespace are essentially interchangeable,
9062 // we can optimize namespace look-up by only storing the key declaration
9063 // of the current TU, rather than storing N key declarations where N is
9064 // the # of imported modules that declare that namespace.
9065 // TODO: Try to generalize this optimization to other redeclarable decls.
9066 if (isa<NamespaceDecl>(Val: ND))
9067 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9068 DS.insert(ND);
9069 }
9070 };
9071
9072 Deserializing LookupResults(this);
9073
9074 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
9075 // available.
9076 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
9077 ++NumVisibleDeclContextsRead;
9078 Find(It->second.Table, Name);
9079 }
9080
9081 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
9082 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
9083 ++NumModuleLocalVisibleDeclContexts;
9084 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
9085 }
9086 };
9087 if (auto *NamedModule =
9088 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
9089 : nullptr)
9090 FindModuleLocalLookup(NamedModule);
9091 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
9092 // We're going to find a decl but the decl context of the lookup is
9093 // unspecified. In this case, the OriginalDC may be the decl context in other
9094 // module.
9095 if (ContextObj && ContextObj->getCurrentNamedModule())
9096 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
9097
9098 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
9099 ++NumTULocalVisibleDeclContexts;
9100 Find(It->second.Table, Name);
9101 }
9102
9103 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9104 return !DS.empty();
9105}
9106
9107void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
9108 if (!DC->hasExternalVisibleStorage())
9109 return;
9110
9111 DeclsMap Decls;
9112
9113 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
9114 auto It = LookupTables.find(DC);
9115 if (It == LookupTables.end())
9116 return;
9117
9118 NumRead++;
9119
9120 for (GlobalDeclID ID : It->second.Table.findAll()) {
9121 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9122 // Special case for namespaces: There can be a lot of redeclarations of
9123 // some namespaces, and we import a "key declaration" per imported module.
9124 // Since all declarations of a namespace are essentially interchangeable,
9125 // we can optimize namespace look-up by only storing the key declaration
9126 // of the current TU, rather than storing N key declarations where N is
9127 // the # of imported modules that declare that namespace.
9128 // TODO: Try to generalize this optimization to other redeclarable decls.
9129 if (isa<NamespaceDecl>(Val: ND))
9130 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9131 Decls[ND->getDeclName()].insert(ND);
9132 }
9133
9134 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
9135 };
9136
9137 findAll(Lookups, NumVisibleDeclContextsRead);
9138 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
9139 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
9140
9141 for (auto &[Name, DS] : Decls)
9142 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9143
9144 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9145}
9146
9147const serialization::reader::DeclContextLookupTable *
9148ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9149 auto I = Lookups.find(Val: Primary);
9150 return I == Lookups.end() ? nullptr : &I->second;
9151}
9152
9153const serialization::reader::ModuleLocalLookupTable *
9154ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9155 auto I = ModuleLocalLookups.find(Val: Primary);
9156 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9157}
9158
9159const serialization::reader::DeclContextLookupTable *
9160ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9161 auto I = TULocalLookups.find(Val: Primary);
9162 return I == TULocalLookups.end() ? nullptr : &I->second;
9163}
9164
9165serialization::reader::LazySpecializationInfoLookupTable *
9166ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9167 assert(D->isCanonicalDecl());
9168 auto &LookupTable =
9169 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9170 auto I = LookupTable.find(Val: D);
9171 return I == LookupTable.end() ? nullptr : &I->second;
9172}
9173
9174bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9175 assert(D->isCanonicalDecl());
9176 return PartialSpecializationsLookups.contains(Val: D) ||
9177 SpecializationsLookups.contains(Val: D);
9178}
9179
9180/// Under non-PCH compilation the consumer receives the objc methods
9181/// before receiving the implementation, and codegen depends on this.
9182/// We simulate this by deserializing and passing to consumer the methods of the
9183/// implementation before passing the deserialized implementation decl.
9184static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9185 ASTConsumer *Consumer) {
9186 assert(ImplD && Consumer);
9187
9188 for (auto *I : ImplD->methods())
9189 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9190
9191 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9192}
9193
9194void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9195 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9196 PassObjCImplDeclToConsumer(ImplD, Consumer);
9197 else
9198 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9199}
9200
9201void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9202 Consumer->HandleVTable(RD);
9203}
9204
9205void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9206 this->Consumer = Consumer;
9207
9208 if (Consumer)
9209 PassInterestingDeclsToConsumer();
9210
9211 if (DeserializationListener)
9212 DeserializationListener->ReaderInitialized(Reader: this);
9213}
9214
9215void ASTReader::PrintStats() {
9216 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9217
9218 unsigned NumTypesLoaded =
9219 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9220 unsigned NumDeclsLoaded =
9221 DeclsLoaded.size() -
9222 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9223 unsigned NumIdentifiersLoaded =
9224 IdentifiersLoaded.size() -
9225 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9226 unsigned NumMacrosLoaded =
9227 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9228 unsigned NumSelectorsLoaded =
9229 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9230
9231 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9232 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9233 NumSLocEntriesRead, TotalNumSLocEntries,
9234 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9235 if (!TypesLoaded.empty())
9236 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9237 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9238 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9239 if (!DeclsLoaded.empty())
9240 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9241 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9242 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9243 if (!IdentifiersLoaded.empty())
9244 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9245 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9246 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9247 if (!MacrosLoaded.empty())
9248 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9249 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9250 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9251 if (!SelectorsLoaded.empty())
9252 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9253 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9254 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9255 if (TotalNumStatements)
9256 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9257 NumStatementsRead, TotalNumStatements,
9258 ((float)NumStatementsRead/TotalNumStatements * 100));
9259 if (TotalNumMacros)
9260 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9261 NumMacrosRead, TotalNumMacros,
9262 ((float)NumMacrosRead/TotalNumMacros * 100));
9263 if (TotalLexicalDeclContexts)
9264 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9265 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9266 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9267 * 100));
9268 if (TotalVisibleDeclContexts)
9269 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9270 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9271 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9272 * 100));
9273 if (TotalModuleLocalVisibleDeclContexts)
9274 std::fprintf(
9275 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9276 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9277 ((float)NumModuleLocalVisibleDeclContexts /
9278 TotalModuleLocalVisibleDeclContexts * 100));
9279 if (TotalTULocalVisibleDeclContexts)
9280 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9281 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9282 ((float)NumTULocalVisibleDeclContexts /
9283 TotalTULocalVisibleDeclContexts * 100));
9284 if (TotalNumMethodPoolEntries)
9285 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9286 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9287 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9288 * 100));
9289 if (NumMethodPoolLookups)
9290 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9291 NumMethodPoolHits, NumMethodPoolLookups,
9292 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9293 if (NumMethodPoolTableLookups)
9294 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9295 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9296 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9297 * 100.0));
9298 if (NumIdentifierLookupHits)
9299 std::fprintf(stderr,
9300 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9301 NumIdentifierLookupHits, NumIdentifierLookups,
9302 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9303
9304 if (GlobalIndex) {
9305 std::fprintf(stderr, format: "\n");
9306 GlobalIndex->printStats();
9307 }
9308
9309 std::fprintf(stderr, format: "\n");
9310 dump();
9311 std::fprintf(stderr, format: "\n");
9312}
9313
9314template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9315LLVM_DUMP_METHOD static void
9316dumpModuleIDMap(StringRef Name,
9317 const ContinuousRangeMap<Key, ModuleFile *,
9318 InitialCapacity> &Map) {
9319 if (Map.begin() == Map.end())
9320 return;
9321
9322 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9323
9324 llvm::errs() << Name << ":\n";
9325 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9326 I != IEnd; ++I)
9327 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9328 << "\n";
9329}
9330
9331LLVM_DUMP_METHOD void ASTReader::dump() {
9332 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9333 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9334 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9335 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9336 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9337 dumpModuleIDMap(Name: "Global preprocessed entity map",
9338 Map: GlobalPreprocessedEntityMap);
9339
9340 llvm::errs() << "\n*** PCH/Modules Loaded:";
9341 for (ModuleFile &M : ModuleMgr)
9342 M.dump();
9343}
9344
9345/// Return the amount of memory used by memory buffers, breaking down
9346/// by heap-backed versus mmap'ed memory.
9347void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9348 for (ModuleFile &I : ModuleMgr) {
9349 if (llvm::MemoryBuffer *buf = I.Buffer) {
9350 size_t bytes = buf->getBufferSize();
9351 switch (buf->getBufferKind()) {
9352 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9353 sizes.malloc_bytes += bytes;
9354 break;
9355 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9356 sizes.mmap_bytes += bytes;
9357 break;
9358 }
9359 }
9360 }
9361}
9362
9363void ASTReader::InitializeSema(Sema &S) {
9364 SemaObj = &S;
9365 S.addExternalSource(E: this);
9366
9367 // Makes sure any declarations that were deserialized "too early"
9368 // still get added to the identifier's declaration chains.
9369 for (GlobalDeclID ID : PreloadedDeclIDs) {
9370 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9371 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9372 }
9373 PreloadedDeclIDs.clear();
9374
9375 // FIXME: What happens if these are changed by a module import?
9376 if (!FPPragmaOptions.empty()) {
9377 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9378 FPOptionsOverride NewOverrides =
9379 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9380 SemaObj->CurFPFeatures =
9381 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9382 }
9383
9384 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9385 Decl *D = GetDecl(ID);
9386 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9387 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9388 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9389 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9390 else
9391 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9392 }
9393 DeclsWithEffectsToVerify.clear();
9394
9395 SemaObj->OpenCLFeatures = OpenCLExtensions;
9396
9397 UpdateSema();
9398}
9399
9400void ASTReader::UpdateSema() {
9401 assert(SemaObj && "no Sema to update");
9402
9403 // UpdateSema() runs after each AST file is loaded, not only the first, so a
9404 // 'requires' directive from a module is registered too.
9405 for (GlobalDeclID ID : OpenMPRequiresDecls)
9406 SemaObj->OpenMP().addRequiresDecl(D: cast<OMPRequiresDecl>(Val: GetDecl(ID)));
9407 OpenMPRequiresDecls.clear();
9408
9409 // Load the offsets of the declarations that Sema references.
9410 // They will be lazily deserialized when needed.
9411 if (!SemaDeclRefs.empty()) {
9412 assert(SemaDeclRefs.size() % 3 == 0);
9413 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9414 if (!SemaObj->StdNamespace)
9415 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9416 if (!SemaObj->StdBadAlloc)
9417 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9418 if (!SemaObj->StdAlignValT)
9419 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9420 }
9421 SemaDeclRefs.clear();
9422 }
9423
9424 // Update the state of pragmas. Use the same API as if we had encountered the
9425 // pragma in the source.
9426 if(OptimizeOffPragmaLocation.isValid())
9427 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9428 if (PragmaMSStructState != -1)
9429 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9430 if (PointersToMembersPragmaLocation.isValid()) {
9431 SemaObj->ActOnPragmaMSPointersToMembers(
9432 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9433 PragmaMSPointersToMembersState,
9434 PragmaLoc: PointersToMembersPragmaLocation);
9435 }
9436 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9437 if (!RISCVVecIntrinsicPragma.empty()) {
9438 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9439 "Wrong number of RISCVVecIntrinsicPragma");
9440 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9441 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9442 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9443 }
9444
9445 if (PragmaAlignPackCurrentValue) {
9446 // The bottom of the stack might have a default value. It must be adjusted
9447 // to the current value to ensure that the packing state is preserved after
9448 // popping entries that were included/imported from a PCH/module.
9449 bool DropFirst = false;
9450 if (!PragmaAlignPackStack.empty() &&
9451 PragmaAlignPackStack.front().Location.isInvalid()) {
9452 assert(PragmaAlignPackStack.front().Value ==
9453 SemaObj->AlignPackStack.DefaultValue &&
9454 "Expected a default alignment value");
9455 SemaObj->AlignPackStack.Stack.emplace_back(
9456 Args&: PragmaAlignPackStack.front().SlotLabel,
9457 Args&: SemaObj->AlignPackStack.CurrentValue,
9458 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9459 Args&: PragmaAlignPackStack.front().PushLocation);
9460 DropFirst = true;
9461 }
9462 for (const auto &Entry :
9463 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9464 SemaObj->AlignPackStack.Stack.emplace_back(
9465 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9466 }
9467 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9468 assert(*PragmaAlignPackCurrentValue ==
9469 SemaObj->AlignPackStack.DefaultValue &&
9470 "Expected a default align and pack value");
9471 // Keep the current values.
9472 } else {
9473 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9474 SemaObj->AlignPackStack.CurrentPragmaLocation =
9475 PragmaAlignPackCurrentLocation;
9476 }
9477 }
9478 if (FpPragmaCurrentValue) {
9479 // The bottom of the stack might have a default value. It must be adjusted
9480 // to the current value to ensure that fp-pragma state is preserved after
9481 // popping entries that were included/imported from a PCH/module.
9482 bool DropFirst = false;
9483 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9484 assert(FpPragmaStack.front().Value ==
9485 SemaObj->FpPragmaStack.DefaultValue &&
9486 "Expected a default pragma float_control value");
9487 SemaObj->FpPragmaStack.Stack.emplace_back(
9488 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9489 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9490 Args&: FpPragmaStack.front().PushLocation);
9491 DropFirst = true;
9492 }
9493 for (const auto &Entry :
9494 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9495 SemaObj->FpPragmaStack.Stack.emplace_back(
9496 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9497 if (FpPragmaCurrentLocation.isInvalid()) {
9498 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9499 "Expected a default pragma float_control value");
9500 // Keep the current values.
9501 } else {
9502 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9503 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9504 }
9505 }
9506
9507 // For non-modular AST files, restore visiblity of modules.
9508 for (auto &Import : PendingImportedModulesSema) {
9509 if (Import.ImportLoc.isInvalid())
9510 continue;
9511 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9512 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9513 }
9514 }
9515 PendingImportedModulesSema.clear();
9516}
9517
9518IdentifierInfo *ASTReader::get(StringRef Name) {
9519 // Note that we are loading an identifier.
9520 Deserializing AnIdentifier(this);
9521
9522 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9523 NumIdentifierLookups,
9524 NumIdentifierLookupHits);
9525
9526 // We don't need to do identifier table lookups in C++ modules (we preload
9527 // all interesting declarations, and don't need to use the scope for name
9528 // lookups). Perform the lookup in PCH files, though, since we don't build
9529 // a complete initial identifier table if we're carrying on from a PCH.
9530 if (PP.getLangOpts().CPlusPlus) {
9531 for (auto *F : ModuleMgr.pch_modules())
9532 if (Visitor(*F))
9533 break;
9534 } else {
9535 // If there is a global index, look there first to determine which modules
9536 // provably do not have any results for this identifier.
9537 GlobalModuleIndex::HitSet Hits;
9538 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9539 if (!loadGlobalIndex()) {
9540 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9541 HitsPtr = &Hits;
9542 }
9543 }
9544
9545 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9546 }
9547
9548 IdentifierInfo *II = Visitor.getIdentifierInfo();
9549 markIdentifierUpToDate(II);
9550 return II;
9551}
9552
9553namespace clang {
9554
9555 /// An identifier-lookup iterator that enumerates all of the
9556 /// identifiers stored within a set of AST files.
9557 class ASTIdentifierIterator : public IdentifierIterator {
9558 /// The AST reader whose identifiers are being enumerated.
9559 const ASTReader &Reader;
9560
9561 /// The current index into the chain of AST files stored in
9562 /// the AST reader.
9563 unsigned Index;
9564
9565 /// The current position within the identifier lookup table
9566 /// of the current AST file.
9567 ASTIdentifierLookupTable::key_iterator Current;
9568
9569 /// The end position within the identifier lookup table of
9570 /// the current AST file.
9571 ASTIdentifierLookupTable::key_iterator End;
9572
9573 /// Whether to skip any modules in the ASTReader.
9574 bool SkipModules;
9575
9576 public:
9577 explicit ASTIdentifierIterator(const ASTReader &Reader,
9578 bool SkipModules = false);
9579
9580 StringRef Next() override;
9581 };
9582
9583} // namespace clang
9584
9585ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9586 bool SkipModules)
9587 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9588}
9589
9590StringRef ASTIdentifierIterator::Next() {
9591 while (Current == End) {
9592 // If we have exhausted all of our AST files, we're done.
9593 if (Index == 0)
9594 return StringRef();
9595
9596 --Index;
9597 ModuleFile &F = Reader.ModuleMgr[Index];
9598 if (SkipModules && F.isModule())
9599 continue;
9600
9601 ASTIdentifierLookupTable *IdTable =
9602 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9603 Current = IdTable->key_begin();
9604 End = IdTable->key_end();
9605 }
9606
9607 // We have any identifiers remaining in the current AST file; return
9608 // the next one.
9609 StringRef Result = *Current;
9610 ++Current;
9611 return Result;
9612}
9613
9614namespace {
9615
9616/// A utility for appending two IdentifierIterators.
9617class ChainedIdentifierIterator : public IdentifierIterator {
9618 std::unique_ptr<IdentifierIterator> Current;
9619 std::unique_ptr<IdentifierIterator> Queued;
9620
9621public:
9622 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9623 std::unique_ptr<IdentifierIterator> Second)
9624 : Current(std::move(First)), Queued(std::move(Second)) {}
9625
9626 StringRef Next() override {
9627 if (!Current)
9628 return StringRef();
9629
9630 StringRef result = Current->Next();
9631 if (!result.empty())
9632 return result;
9633
9634 // Try the queued iterator, which may itself be empty.
9635 Current.reset();
9636 std::swap(x&: Current, y&: Queued);
9637 return Next();
9638 }
9639};
9640
9641} // namespace
9642
9643IdentifierIterator *ASTReader::getIdentifiers() {
9644 if (!loadGlobalIndex()) {
9645 std::unique_ptr<IdentifierIterator> ReaderIter(
9646 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9647 std::unique_ptr<IdentifierIterator> ModulesIter(
9648 GlobalIndex->createIdentifierIterator());
9649 return new ChainedIdentifierIterator(std::move(ReaderIter),
9650 std::move(ModulesIter));
9651 }
9652
9653 return new ASTIdentifierIterator(*this);
9654}
9655
9656namespace clang {
9657namespace serialization {
9658
9659 class ReadMethodPoolVisitor {
9660 ASTReader &Reader;
9661 Selector Sel;
9662 unsigned PriorGeneration;
9663 unsigned InstanceBits = 0;
9664 unsigned FactoryBits = 0;
9665 bool InstanceHasMoreThanOneDecl = false;
9666 bool FactoryHasMoreThanOneDecl = false;
9667 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9668 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9669
9670 public:
9671 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9672 unsigned PriorGeneration)
9673 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9674
9675 bool operator()(ModuleFile &M) {
9676 if (!M.SelectorLookupTable)
9677 return false;
9678
9679 // If we've already searched this module file, skip it now.
9680 if (M.Generation <= PriorGeneration)
9681 return true;
9682
9683 ++Reader.NumMethodPoolTableLookups;
9684 ASTSelectorLookupTable *PoolTable
9685 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9686 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9687 if (Pos == PoolTable->end())
9688 return false;
9689
9690 ++Reader.NumMethodPoolTableHits;
9691 ++Reader.NumSelectorsRead;
9692 // FIXME: Not quite happy with the statistics here. We probably should
9693 // disable this tracking when called via LoadSelector.
9694 // Also, should entries without methods count as misses?
9695 ++Reader.NumMethodPoolEntriesRead;
9696 ASTSelectorLookupTrait::data_type Data = *Pos;
9697 if (Reader.DeserializationListener)
9698 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9699
9700 // Append methods in the reverse order, so that later we can process them
9701 // in the order they appear in the source code by iterating through
9702 // the vector in the reverse order.
9703 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9704 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9705 InstanceBits = Data.InstanceBits;
9706 FactoryBits = Data.FactoryBits;
9707 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9708 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9709 return false;
9710 }
9711
9712 /// Retrieve the instance methods found by this visitor.
9713 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9714 return InstanceMethods;
9715 }
9716
9717 /// Retrieve the instance methods found by this visitor.
9718 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9719 return FactoryMethods;
9720 }
9721
9722 unsigned getInstanceBits() const { return InstanceBits; }
9723 unsigned getFactoryBits() const { return FactoryBits; }
9724
9725 bool instanceHasMoreThanOneDecl() const {
9726 return InstanceHasMoreThanOneDecl;
9727 }
9728
9729 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9730 };
9731
9732} // namespace serialization
9733} // namespace clang
9734
9735/// Add the given set of methods to the method list.
9736static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9737 ObjCMethodList &List) {
9738 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9739 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9740}
9741
9742void ASTReader::ReadMethodPool(Selector Sel) {
9743 // Get the selector generation and update it to the current generation.
9744 unsigned &Generation = SelectorGeneration[Sel];
9745 unsigned PriorGeneration = Generation;
9746 Generation = getGeneration();
9747 SelectorOutOfDate[Sel] = false;
9748
9749 // Search for methods defined with this selector.
9750 ++NumMethodPoolLookups;
9751 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9752 ModuleMgr.visit(Visitor);
9753
9754 if (Visitor.getInstanceMethods().empty() &&
9755 Visitor.getFactoryMethods().empty())
9756 return;
9757
9758 ++NumMethodPoolHits;
9759
9760 if (!getSema())
9761 return;
9762
9763 Sema &S = *getSema();
9764 auto &Methods = S.ObjC().MethodPool[Sel];
9765
9766 Methods.first.setBits(Visitor.getInstanceBits());
9767 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9768 Methods.second.setBits(Visitor.getFactoryBits());
9769 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9770
9771 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9772 // when building a module we keep every method individually and may need to
9773 // update hasMoreThanOneDecl as we add the methods.
9774 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9775 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9776}
9777
9778void ASTReader::updateOutOfDateSelector(Selector Sel) {
9779 if (SelectorOutOfDate[Sel])
9780 ReadMethodPool(Sel);
9781}
9782
9783void ASTReader::ReadKnownNamespaces(
9784 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9785 Namespaces.clear();
9786
9787 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9788 if (NamespaceDecl *Namespace
9789 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9790 Namespaces.push_back(Elt: Namespace);
9791 }
9792}
9793
9794void ASTReader::ReadUndefinedButUsed(
9795 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9796 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9797 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9798 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9799 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9800 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9801 }
9802 UndefinedButUsed.clear();
9803}
9804
9805void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9806 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9807 Exprs) {
9808 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9809 FieldDecl *FD =
9810 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9811 uint64_t Count = DelayedDeleteExprs[Idx++];
9812 for (uint64_t C = 0; C < Count; ++C) {
9813 SourceLocation DeleteLoc =
9814 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9815 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9816 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9817 }
9818 }
9819}
9820
9821void ASTReader::ReadTentativeDefinitions(
9822 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9823 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9824 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9825 if (Var)
9826 TentativeDefs.push_back(Elt: Var);
9827 }
9828 TentativeDefinitions.clear();
9829}
9830
9831void ASTReader::ReadUnusedFileScopedDecls(
9832 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9833 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9834 DeclaratorDecl *D
9835 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9836 if (D)
9837 Decls.push_back(Elt: D);
9838 }
9839 UnusedFileScopedDecls.clear();
9840}
9841
9842void ASTReader::ReadDelegatingConstructors(
9843 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9844 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9845 CXXConstructorDecl *D
9846 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9847 if (D)
9848 Decls.push_back(Elt: D);
9849 }
9850 DelegatingCtorDecls.clear();
9851}
9852
9853void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9854 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9855 TypedefNameDecl *D
9856 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9857 if (D)
9858 Decls.push_back(Elt: D);
9859 }
9860 ExtVectorDecls.clear();
9861}
9862
9863void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9864 llvm::SmallPtrSetImpl<const TypedefNameDecl *> &Decls) {
9865 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9866 ++I) {
9867 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9868 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9869 if (D)
9870 Decls.insert(Ptr: D);
9871 }
9872 UnusedLocalTypedefNameCandidates.clear();
9873}
9874
9875void ASTReader::ReadDeclsToCheckForDeferredDiags(
9876 llvm::SmallSetVector<Decl *, 4> &Decls) {
9877 for (auto I : DeclsToCheckForDeferredDiags) {
9878 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9879 if (D)
9880 Decls.insert(X: D);
9881 }
9882 DeclsToCheckForDeferredDiags.clear();
9883}
9884
9885void ASTReader::ReadReferencedSelectors(
9886 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9887 if (ReferencedSelectorsData.empty())
9888 return;
9889
9890 // If there are @selector references added them to its pool. This is for
9891 // implementation of -Wselector.
9892 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9893 unsigned I = 0;
9894 while (I < DataSize) {
9895 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9896 SourceLocation SelLoc
9897 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9898 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9899 }
9900 ReferencedSelectorsData.clear();
9901}
9902
9903void ASTReader::ReadWeakUndeclaredIdentifiers(
9904 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9905 if (WeakUndeclaredIdentifiers.empty())
9906 return;
9907
9908 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9909 IdentifierInfo *WeakId
9910 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9911 IdentifierInfo *AliasId
9912 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9913 SourceLocation Loc =
9914 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9915 WeakInfo WI(AliasId, Loc);
9916 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9917 }
9918 WeakUndeclaredIdentifiers.clear();
9919}
9920
9921void ASTReader::ReadExtnameUndeclaredIdentifiers(
9922 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9923 if (ExtnameUndeclaredIdentifiers.empty())
9924 return;
9925
9926 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9927 IdentifierInfo *NameId =
9928 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9929 IdentifierInfo *ExtnameId =
9930 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9931 SourceLocation Loc =
9932 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9933 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9934 Ctx&: getContext(), Label: ExtnameId->getName(),
9935 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9936 AttributeCommonInfo::Form::Pragma()));
9937 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9938 }
9939 ExtnameUndeclaredIdentifiers.clear();
9940}
9941
9942void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9943 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9944 ExternalVTableUse VT;
9945 VTableUse &TableInfo = VTableUses[Idx++];
9946 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9947 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9948 VT.DefinitionRequired = TableInfo.Used;
9949 VTables.push_back(Elt: VT);
9950 }
9951
9952 VTableUses.clear();
9953}
9954
9955void ASTReader::ReadPendingInstantiations(
9956 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9957 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9958 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9959 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9960 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9961
9962 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9963 }
9964 PendingInstantiations.clear();
9965}
9966
9967void ASTReader::ReadLateParsedTemplates(
9968 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9969 &LPTMap) {
9970 for (auto &LPT : LateParsedTemplates) {
9971 ModuleFile *FMod = LPT.first;
9972 RecordDataImpl &LateParsed = LPT.second;
9973 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9974 /* In loop */) {
9975 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9976
9977 auto LT = std::make_unique<LateParsedTemplate>();
9978 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9979 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9980
9981 ModuleFile *F = getOwningModuleFile(D: LT->D);
9982 assert(F && "No module");
9983
9984 unsigned TokN = LateParsed[Idx++];
9985 LT->Toks.reserve(N: TokN);
9986 for (unsigned T = 0; T < TokN; ++T)
9987 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9988
9989 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9990 }
9991 }
9992
9993 LateParsedTemplates.clear();
9994}
9995
9996void ASTReader::LoadSelector(Selector Sel) {
9997 // It would be complicated to avoid reading the methods anyway. So don't.
9998 ReadMethodPool(Sel);
9999}
10000
10001void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
10002 assert(ID && "Non-zero identifier ID required");
10003 unsigned Index = translateIdentifierIDToIndex(ID).second;
10004 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
10005 IdentifiersLoaded[Index] = II;
10006 if (DeserializationListener)
10007 DeserializationListener->IdentifierRead(ID, II);
10008}
10009
10010/// Set the globally-visible declarations associated with the given
10011/// identifier.
10012///
10013/// If the AST reader is currently in a state where the given declaration IDs
10014/// cannot safely be resolved, they are queued until it is safe to resolve
10015/// them.
10016///
10017/// \param II an IdentifierInfo that refers to one or more globally-visible
10018/// declarations.
10019///
10020/// \param DeclIDs the set of declaration IDs with the name @p II that are
10021/// visible at global scope.
10022///
10023/// \param Decls if non-null, this vector will be populated with the set of
10024/// deserialized declarations. These declarations will not be pushed into
10025/// scope.
10026void ASTReader::SetGloballyVisibleDecls(
10027 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
10028 SmallVectorImpl<Decl *> *Decls) {
10029 if (NumCurrentElementsDeserializing && !Decls) {
10030 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
10031 return;
10032 }
10033
10034 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
10035 if (!SemaObj) {
10036 // Queue this declaration so that it will be added to the
10037 // translation unit scope and identifier's declaration chain
10038 // once a Sema object is known.
10039 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
10040 continue;
10041 }
10042
10043 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
10044
10045 // If we're simply supposed to record the declarations, do so now.
10046 if (Decls) {
10047 Decls->push_back(Elt: D);
10048 continue;
10049 }
10050
10051 // Introduce this declaration into the translation-unit scope
10052 // and add it to the declaration chain for this identifier, so
10053 // that (unqualified) name lookup will find it.
10054 pushExternalDeclIntoScope(D, Name: II);
10055 }
10056}
10057
10058std::pair<ModuleFile *, unsigned>
10059ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
10060 if (ID == 0)
10061 return {nullptr, 0};
10062
10063 unsigned ModuleFileIndex = ID >> 32;
10064 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
10065
10066 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
10067 assert(getModuleManager().size() > ModuleFileIndex - 1);
10068
10069 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10070 assert(LocalID < MF.LocalNumIdentifiers);
10071 return {&MF, MF.BaseIdentifierID + LocalID};
10072}
10073
10074IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
10075 if (ID == 0)
10076 return nullptr;
10077
10078 if (IdentifiersLoaded.empty()) {
10079 Error(Msg: "no identifier table in AST file");
10080 return nullptr;
10081 }
10082
10083 auto [M, Index] = translateIdentifierIDToIndex(ID);
10084 if (!IdentifiersLoaded[Index]) {
10085 assert(M != nullptr && "Untranslated Identifier ID?");
10086 assert(Index >= M->BaseIdentifierID);
10087 unsigned LocalIndex = Index - M->BaseIdentifierID;
10088 const unsigned char *Data =
10089 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
10090
10091 ASTIdentifierLookupTrait Trait(*this, *M);
10092 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
10093 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
10094 auto &II = PP.getIdentifierTable().get(Name: Key);
10095 IdentifiersLoaded[Index] = &II;
10096 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
10097 markIdentifierFromAST(Reader&: *this, II, IsModule);
10098 if (DeserializationListener)
10099 DeserializationListener->IdentifierRead(ID, II: &II);
10100 }
10101
10102 return IdentifiersLoaded[Index];
10103}
10104
10105IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
10106 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
10107}
10108
10109IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
10110 if (LocalID < NUM_PREDEF_IDENT_IDS)
10111 return LocalID;
10112
10113 if (!M.ModuleOffsetMap.empty())
10114 ReadModuleOffsetMap(F&: M);
10115
10116 unsigned ModuleFileIndex = LocalID >> 32;
10117 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
10118 ModuleFile *MF =
10119 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10120 assert(MF && "malformed identifier ID encoding?");
10121
10122 if (!ModuleFileIndex)
10123 LocalID -= NUM_PREDEF_IDENT_IDS;
10124
10125 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10126}
10127
10128std::pair<ModuleFile *, unsigned>
10129ASTReader::translateMacroIDToIndex(MacroID ID) const {
10130 if (ID == 0)
10131 return {nullptr, 0};
10132
10133 unsigned ModuleFileIndex = ID >> 32;
10134 assert(ModuleFileIndex && "not translating loaded MacroID?");
10135 assert(getModuleManager().size() > ModuleFileIndex - 1);
10136 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10137
10138 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10139 assert(LocalID < MF.LocalNumMacros);
10140 return {&MF, MF.BaseMacroID + LocalID};
10141}
10142
10143MacroInfo *ASTReader::getMacro(MacroID ID) {
10144 if (ID == 0)
10145 return nullptr;
10146
10147 if (MacrosLoaded.empty()) {
10148 Error(Msg: "no macro table in AST file");
10149 return nullptr;
10150 }
10151
10152 auto [M, Index] = translateMacroIDToIndex(ID);
10153 if (!MacrosLoaded[Index]) {
10154 assert(M != nullptr && "Untranslated Macro ID?");
10155 assert(Index >= M->BaseMacroID);
10156 unsigned LocalIndex = Index - M->BaseMacroID;
10157 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10158 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10159
10160 if (DeserializationListener)
10161 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10162 }
10163
10164 return MacrosLoaded[Index];
10165}
10166
10167MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10168 if (LocalID < NUM_PREDEF_MACRO_IDS)
10169 return LocalID;
10170
10171 if (!M.ModuleOffsetMap.empty())
10172 ReadModuleOffsetMap(F&: M);
10173
10174 unsigned ModuleFileIndex = LocalID >> 32;
10175 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10176 ModuleFile *MF =
10177 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10178 assert(MF && "malformed identifier ID encoding?");
10179
10180 if (!ModuleFileIndex) {
10181 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10182 LocalID -= NUM_PREDEF_MACRO_IDS;
10183 }
10184
10185 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10186}
10187
10188serialization::SubmoduleID
10189ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10190 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10191 return LocalID;
10192
10193 if (!M.ModuleOffsetMap.empty())
10194 ReadModuleOffsetMap(F&: M);
10195
10196 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10197 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10198 assert(I != M.SubmoduleRemap.end()
10199 && "Invalid index into submodule index remap");
10200
10201 return LocalID + I->second;
10202}
10203
10204Module *ASTReader::getModule(unsigned ID) {
10205 return getSubmodule(GlobalID: ID);
10206}
10207
10208ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10209 if (ID & 1) {
10210 // It's a module, look it up by submodule ID.
10211 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10212 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10213 } else {
10214 // It's a prefix (preamble, PCH, ...). Look it up by index.
10215 int IndexFromEnd = static_cast<int>(ID >> 1);
10216 assert(IndexFromEnd && "got reference to unknown module file");
10217 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10218 }
10219}
10220
10221unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10222 if (!M)
10223 return 1;
10224
10225 // For a file representing a module, use the submodule ID of the top-level
10226 // module as the file ID. For any other kind of file, the number of such
10227 // files loaded beforehand will be the same on reload.
10228 // FIXME: Is this true even if we have an explicit module file and a PCH?
10229 if (M->isModule())
10230 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10231
10232 auto PCHModules = getModuleManager().pch_modules();
10233 auto I = llvm::find(Range&: PCHModules, Val: M);
10234 assert(I != PCHModules.end() && "emitting reference to unknown file");
10235 return std::distance(first: I, last: PCHModules.end()) << 1;
10236}
10237
10238std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10239 if (Module *M = getSubmodule(GlobalID: ID))
10240 return ASTSourceDescriptor(*M);
10241
10242 // If there is only a single PCH, return it instead.
10243 // Chained PCH are not supported.
10244 const auto &PCHChain = ModuleMgr.pch_modules();
10245 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10246 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10247 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10248 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10249 return ASTSourceDescriptor(ModuleName,
10250 llvm::sys::path::parent_path(path: MF.FileName),
10251 FileName, MF.Signature);
10252 }
10253 return std::nullopt;
10254}
10255
10256ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10257 auto I = DefinitionSource.find(Val: FD);
10258 if (I == DefinitionSource.end())
10259 return EK_ReplyHazy;
10260 return I->second ? EK_Never : EK_Always;
10261}
10262
10263bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10264 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10265}
10266
10267Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10268 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10269}
10270
10271Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10272 if (ID == 0)
10273 return Selector();
10274
10275 if (ID > SelectorsLoaded.size()) {
10276 Error(Msg: "selector ID out of range in AST file");
10277 return Selector();
10278 }
10279
10280 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10281 // Load this selector from the selector table.
10282 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10283 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10284 ModuleFile &M = *I->second;
10285 ASTSelectorLookupTrait Trait(*this, M);
10286 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10287 SelectorsLoaded[ID - 1] =
10288 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10289 if (DeserializationListener)
10290 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10291 }
10292
10293 return SelectorsLoaded[ID - 1];
10294}
10295
10296Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10297 return DecodeSelector(ID);
10298}
10299
10300uint32_t ASTReader::GetNumExternalSelectors() {
10301 // ID 0 (the null selector) is considered an external selector.
10302 return getTotalNumSelectors() + 1;
10303}
10304
10305serialization::SelectorID
10306ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10307 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10308 return LocalID;
10309
10310 if (!M.ModuleOffsetMap.empty())
10311 ReadModuleOffsetMap(F&: M);
10312
10313 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10314 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10315 assert(I != M.SelectorRemap.end()
10316 && "Invalid index into selector index remap");
10317
10318 return LocalID + I->second;
10319}
10320
10321DeclarationNameLoc
10322ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10323 switch (Name.getNameKind()) {
10324 case DeclarationName::CXXConstructorName:
10325 case DeclarationName::CXXDestructorName:
10326 case DeclarationName::CXXConversionFunctionName:
10327 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10328
10329 case DeclarationName::CXXOperatorName:
10330 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10331
10332 case DeclarationName::CXXLiteralOperatorName:
10333 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10334 Loc: readSourceLocation());
10335
10336 case DeclarationName::Identifier:
10337 case DeclarationName::ObjCZeroArgSelector:
10338 case DeclarationName::ObjCOneArgSelector:
10339 case DeclarationName::ObjCMultiArgSelector:
10340 case DeclarationName::CXXUsingDirective:
10341 case DeclarationName::CXXDeductionGuideName:
10342 break;
10343 }
10344 return DeclarationNameLoc();
10345}
10346
10347DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10348 DeclarationNameInfo NameInfo;
10349 NameInfo.setName(readDeclarationName());
10350 NameInfo.setLoc(readSourceLocation());
10351 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10352 return NameInfo;
10353}
10354
10355TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10356 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10357}
10358
10359SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10360 auto Kind = readInt();
10361 auto ResultType = readQualType();
10362 auto Value = readAPInt();
10363 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10364 assert(Op.isValid());
10365 return Op;
10366}
10367
10368void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10369 Info.QualifierLoc = readNestedNameSpecifierLoc();
10370 unsigned NumTPLists = readInt();
10371 Info.NumTemplParamLists = NumTPLists;
10372 if (NumTPLists) {
10373 Info.TemplParamLists =
10374 new (getContext()) TemplateParameterList *[NumTPLists];
10375 for (unsigned i = 0; i != NumTPLists; ++i)
10376 Info.TemplParamLists[i] = readTemplateParameterList();
10377 }
10378}
10379
10380TemplateParameterList *
10381ASTRecordReader::readTemplateParameterList() {
10382 SourceLocation TemplateLoc = readSourceLocation();
10383 SourceLocation LAngleLoc = readSourceLocation();
10384 SourceLocation RAngleLoc = readSourceLocation();
10385
10386 unsigned NumParams = readInt();
10387 SmallVector<NamedDecl *, 16> Params;
10388 Params.reserve(N: NumParams);
10389 while (NumParams--)
10390 Params.push_back(Elt: readDeclAs<NamedDecl>());
10391
10392 bool HasRequiresClause = readBool();
10393 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10394
10395 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10396 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10397 return TemplateParams;
10398}
10399
10400void ASTRecordReader::readTemplateArgumentList(
10401 SmallVectorImpl<TemplateArgument> &TemplArgs,
10402 bool Canonicalize) {
10403 unsigned NumTemplateArgs = readInt();
10404 TemplArgs.reserve(N: NumTemplateArgs);
10405 while (NumTemplateArgs--)
10406 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10407}
10408
10409/// Read a UnresolvedSet structure.
10410void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10411 unsigned NumDecls = readInt();
10412 Set.reserve(C&: getContext(), N: NumDecls);
10413 while (NumDecls--) {
10414 GlobalDeclID ID = readDeclID();
10415 AccessSpecifier AS = (AccessSpecifier) readInt();
10416 Set.addLazyDecl(C&: getContext(), ID, AS);
10417 }
10418}
10419
10420CXXBaseSpecifier
10421ASTRecordReader::readCXXBaseSpecifier() {
10422 bool isVirtual = readBool();
10423 bool isBaseOfClass = readBool();
10424 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10425 bool inheritConstructors = readBool();
10426 TypeSourceInfo *TInfo = readTypeSourceInfo();
10427 SourceRange Range = readSourceRange();
10428 SourceLocation EllipsisLoc = readSourceLocation();
10429 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10430 EllipsisLoc);
10431 Result.setInheritConstructors(inheritConstructors);
10432 return Result;
10433}
10434
10435CXXCtorInitializer **
10436ASTRecordReader::readCXXCtorInitializers() {
10437 ASTContext &Context = getContext();
10438 unsigned NumInitializers = readInt();
10439 assert(NumInitializers && "wrote ctor initializers but have no inits");
10440 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10441 for (unsigned i = 0; i != NumInitializers; ++i) {
10442 TypeSourceInfo *TInfo = nullptr;
10443 bool IsBaseVirtual = false;
10444 FieldDecl *Member = nullptr;
10445 IndirectFieldDecl *IndirectMember = nullptr;
10446
10447 CtorInitializerType Type = (CtorInitializerType) readInt();
10448 switch (Type) {
10449 case CTOR_INITIALIZER_BASE:
10450 TInfo = readTypeSourceInfo();
10451 IsBaseVirtual = readBool();
10452 break;
10453
10454 case CTOR_INITIALIZER_DELEGATING:
10455 TInfo = readTypeSourceInfo();
10456 break;
10457
10458 case CTOR_INITIALIZER_MEMBER:
10459 Member = readDeclAs<FieldDecl>();
10460 break;
10461
10462 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10463 IndirectMember = readDeclAs<IndirectFieldDecl>();
10464 break;
10465 }
10466
10467 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10468 Expr *Init = readExpr();
10469 SourceLocation LParenLoc = readSourceLocation();
10470 SourceLocation RParenLoc = readSourceLocation();
10471
10472 CXXCtorInitializer *BOMInit;
10473 if (Type == CTOR_INITIALIZER_BASE)
10474 BOMInit = new (Context)
10475 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10476 RParenLoc, MemberOrEllipsisLoc);
10477 else if (Type == CTOR_INITIALIZER_DELEGATING)
10478 BOMInit = new (Context)
10479 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10480 else if (Member)
10481 BOMInit = new (Context)
10482 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10483 Init, RParenLoc);
10484 else
10485 BOMInit = new (Context)
10486 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10487 LParenLoc, Init, RParenLoc);
10488
10489 if (/*IsWritten*/readBool()) {
10490 unsigned SourceOrder = readInt();
10491 BOMInit->setSourceOrder(SourceOrder);
10492 }
10493
10494 CtorInitializers[i] = BOMInit;
10495 }
10496
10497 return CtorInitializers;
10498}
10499
10500NestedNameSpecifierLoc
10501ASTRecordReader::readNestedNameSpecifierLoc() {
10502 ASTContext &Context = getContext();
10503 unsigned N = readInt();
10504 NestedNameSpecifierLocBuilder Builder;
10505 for (unsigned I = 0; I != N; ++I) {
10506 auto Kind = readNestedNameSpecifierKind();
10507 switch (Kind) {
10508 case NestedNameSpecifier::Kind::Namespace: {
10509 auto *NS = readDeclAs<NamespaceBaseDecl>();
10510 SourceRange Range = readSourceRange();
10511 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10512 break;
10513 }
10514
10515 case NestedNameSpecifier::Kind::Type: {
10516 TypeSourceInfo *T = readTypeSourceInfo();
10517 if (!T)
10518 return NestedNameSpecifierLoc();
10519 SourceLocation ColonColonLoc = readSourceLocation();
10520 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10521 break;
10522 }
10523
10524 case NestedNameSpecifier::Kind::Global: {
10525 SourceLocation ColonColonLoc = readSourceLocation();
10526 Builder.MakeGlobal(Context, ColonColonLoc);
10527 break;
10528 }
10529
10530 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10531 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10532 SourceRange Range = readSourceRange();
10533 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10534 break;
10535 }
10536
10537 case NestedNameSpecifier::Kind::Null:
10538 llvm_unreachable("unexpected null nested name specifier");
10539 }
10540 }
10541
10542 return Builder.getWithLocInContext(Context);
10543}
10544
10545SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10546 unsigned &Idx) {
10547 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10548 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10549 return SourceRange(beg, end);
10550}
10551
10552llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10553 const StringRef Blob) {
10554 unsigned Count = Record[0];
10555 const char *Byte = Blob.data();
10556 llvm::BitVector Ret = llvm::BitVector(Count, false);
10557 for (unsigned I = 0; I < Count; ++Byte)
10558 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10559 if (*Byte & (1 << Bit))
10560 Ret[I] = true;
10561 return Ret;
10562}
10563
10564/// Read a floating-point value
10565llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10566 return llvm::APFloat(Sem, readAPInt());
10567}
10568
10569// Read a string
10570std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10571 unsigned Len = Record[Idx++];
10572 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10573 Idx += Len;
10574 return Result;
10575}
10576
10577StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10578 StringRef &Blob) {
10579 unsigned Len = Record[Idx++];
10580 StringRef Result = Blob.substr(Start: 0, N: Len);
10581 Blob = Blob.substr(Start: Len);
10582 return Result;
10583}
10584
10585std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10586 unsigned &Idx) {
10587 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10588}
10589
10590std::string ASTReader::ReadPath(StringRef BaseDirectory,
10591 const RecordData &Record, unsigned &Idx) {
10592 std::string Filename = ReadString(Record, Idx);
10593 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10594}
10595
10596std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10597 const RecordData &Record, unsigned &Idx,
10598 StringRef &Blob) {
10599 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10600 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10601}
10602
10603VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10604 unsigned &Idx) {
10605 unsigned Major = Record[Idx++];
10606 unsigned Minor = Record[Idx++];
10607 unsigned Subminor = Record[Idx++];
10608 if (Minor == 0)
10609 return VersionTuple(Major);
10610 if (Subminor == 0)
10611 return VersionTuple(Major, Minor - 1);
10612 return VersionTuple(Major, Minor - 1, Subminor - 1);
10613}
10614
10615CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10616 const RecordData &Record,
10617 unsigned &Idx) {
10618 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10619 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10620}
10621
10622DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10623 return Diag(Loc: CurrentImportLoc, DiagID);
10624}
10625
10626DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10627 return Diags.Report(Loc, DiagID);
10628}
10629
10630void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10631 llvm::function_ref<void()> Fn) {
10632 // When Sema is available, avoid duplicate errors.
10633 if (SemaObj) {
10634 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10635 return;
10636 }
10637
10638 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10639}
10640
10641/// Retrieve the identifier table associated with the
10642/// preprocessor.
10643IdentifierTable &ASTReader::getIdentifierTable() {
10644 return PP.getIdentifierTable();
10645}
10646
10647/// Record that the given ID maps to the given switch-case
10648/// statement.
10649void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10650 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10651 "Already have a SwitchCase with this ID");
10652 (*CurrSwitchCaseStmts)[ID] = SC;
10653}
10654
10655/// Retrieve the switch-case statement with the given ID.
10656SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10657 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10658 return (*CurrSwitchCaseStmts)[ID];
10659}
10660
10661void ASTReader::ClearSwitchCaseIDs() {
10662 CurrSwitchCaseStmts->clear();
10663}
10664
10665void ASTReader::ReadComments() {
10666 ASTContext &Context = getContext();
10667 std::vector<RawComment *> Comments;
10668 for (SmallVectorImpl<std::pair<BitstreamCursor,
10669 serialization::ModuleFile *>>::iterator
10670 I = CommentsCursors.begin(),
10671 E = CommentsCursors.end();
10672 I != E; ++I) {
10673 Comments.clear();
10674 BitstreamCursor &Cursor = I->first;
10675 serialization::ModuleFile &F = *I->second;
10676 SavedStreamPosition SavedPosition(Cursor);
10677
10678 RecordData Record;
10679 while (true) {
10680 Expected<llvm::BitstreamEntry> MaybeEntry =
10681 Cursor.advanceSkippingSubblocks(
10682 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10683 if (!MaybeEntry) {
10684 Error(Err: MaybeEntry.takeError());
10685 return;
10686 }
10687 llvm::BitstreamEntry Entry = MaybeEntry.get();
10688
10689 switch (Entry.Kind) {
10690 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10691 case llvm::BitstreamEntry::Error:
10692 Error(Msg: "malformed block record in AST file");
10693 return;
10694 case llvm::BitstreamEntry::EndBlock:
10695 goto NextCursor;
10696 case llvm::BitstreamEntry::Record:
10697 // The interesting case.
10698 break;
10699 }
10700
10701 // Read a record.
10702 Record.clear();
10703 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10704 if (!MaybeComment) {
10705 Error(Err: MaybeComment.takeError());
10706 return;
10707 }
10708 switch ((CommentRecordTypes)MaybeComment.get()) {
10709 case COMMENTS_RAW_COMMENT: {
10710 unsigned Idx = 0;
10711 SourceRange SR = ReadSourceRange(F, Record, Idx);
10712 RawComment::CommentKind Kind =
10713 (RawComment::CommentKind) Record[Idx++];
10714 bool IsTrailingComment = Record[Idx++];
10715 bool IsAlmostTrailingComment = Record[Idx++];
10716 Comments.push_back(x: new (Context) RawComment(
10717 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10718 break;
10719 }
10720 }
10721 }
10722 NextCursor:
10723 for (RawComment *C : Comments) {
10724 SourceLocation CommentLoc = C->getBeginLoc();
10725 if (CommentLoc.isValid()) {
10726 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10727 if (Loc.first.isValid())
10728 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10729 }
10730 }
10731 }
10732}
10733
10734void ASTReader::visitInputFileInfos(
10735 serialization::ModuleFile &MF, bool IncludeSystem,
10736 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10737 bool IsSystem)>
10738 Visitor) {
10739 unsigned NumUserInputs = MF.NumUserInputFiles;
10740 unsigned NumInputs = MF.InputFilesLoaded.size();
10741 assert(NumUserInputs <= NumInputs);
10742 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10743 for (unsigned I = 0; I < N; ++I) {
10744 bool IsSystem = I >= NumUserInputs;
10745 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10746 Visitor(IFI, IsSystem);
10747 }
10748}
10749
10750void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10751 bool IncludeSystem, bool Complain,
10752 llvm::function_ref<void(const serialization::InputFile &IF,
10753 bool isSystem)> Visitor) {
10754 unsigned NumUserInputs = MF.NumUserInputFiles;
10755 unsigned NumInputs = MF.InputFilesLoaded.size();
10756 assert(NumUserInputs <= NumInputs);
10757 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10758 for (unsigned I = 0; I < N; ++I) {
10759 bool IsSystem = I >= NumUserInputs;
10760 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10761 Visitor(IF, IsSystem);
10762 }
10763}
10764
10765void ASTReader::visitTopLevelModuleMaps(
10766 serialization::ModuleFile &MF,
10767 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10768 unsigned NumInputs = MF.InputFilesLoaded.size();
10769 for (unsigned I = 0; I < NumInputs; ++I) {
10770 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10771 if (IFI.TopLevel && IFI.ModuleMap)
10772 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10773 Visitor(*FE);
10774 }
10775}
10776
10777void ASTReader::finishPendingActions() {
10778 while (!PendingIdentifierInfos.empty() ||
10779 !PendingDeducedFunctionTypes.empty() ||
10780 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10781 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10782 !PendingUpdateRecords.empty() ||
10783 !PendingObjCExtensionIvarRedeclarations.empty()) {
10784 // If any identifiers with corresponding top-level declarations have
10785 // been loaded, load those declarations now.
10786 using TopLevelDeclsMap =
10787 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10788 TopLevelDeclsMap TopLevelDecls;
10789
10790 while (!PendingIdentifierInfos.empty()) {
10791 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10792 SmallVector<GlobalDeclID, 4> DeclIDs =
10793 std::move(PendingIdentifierInfos.back().second);
10794 PendingIdentifierInfos.pop_back();
10795
10796 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10797 }
10798
10799 // Load each function type that we deferred loading because it was a
10800 // deduced type that might refer to a local type declared within itself.
10801 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10802 auto *FD = PendingDeducedFunctionTypes[I].first;
10803 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10804
10805 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10806 // If we gave a function a deduced return type, remember that we need to
10807 // propagate that along the redeclaration chain.
10808 if (DT->isDeduced()) {
10809 PendingDeducedTypeUpdates.insert(
10810 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10811 continue;
10812 }
10813
10814 // The function has undeduced DeduceType return type. We hope we can
10815 // find the deduced type by iterating the redecls in other modules
10816 // later.
10817 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10818 continue;
10819 }
10820 }
10821 PendingDeducedFunctionTypes.clear();
10822
10823 // Load each variable type that we deferred loading because it was a
10824 // deduced type that might refer to a local type declared within itself.
10825 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10826 auto *VD = PendingDeducedVarTypes[I].first;
10827 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10828 }
10829 PendingDeducedVarTypes.clear();
10830
10831 // Load pending declaration chains.
10832 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10833 loadPendingDeclChain(D: PendingDeclChains[I].first,
10834 LocalOffset: PendingDeclChains[I].second);
10835 PendingDeclChains.clear();
10836
10837 // Make the most recent of the top-level declarations visible.
10838 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10839 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10840 IdentifierInfo *II = TLD->first;
10841 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10842 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10843 }
10844 }
10845
10846 // Load any pending macro definitions.
10847 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10848 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10849 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10850 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10851 // Initialize the macro history from chained-PCHs ahead of module imports.
10852 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10853 ++IDIdx) {
10854 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10855 if (!Info.M->isModule())
10856 resolvePendingMacro(II, PMInfo: Info);
10857 }
10858 // Handle module imports.
10859 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10860 ++IDIdx) {
10861 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10862 if (Info.M->isModule())
10863 resolvePendingMacro(II, PMInfo: Info);
10864 }
10865 }
10866 PendingMacroIDs.clear();
10867
10868 // Wire up the DeclContexts for Decls that we delayed setting until
10869 // recursive loading is completed.
10870 while (!PendingDeclContextInfos.empty()) {
10871 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10872 PendingDeclContextInfos.pop_front();
10873 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10874 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10875 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10876 }
10877
10878 // Perform any pending declaration updates.
10879 while (!PendingUpdateRecords.empty()) {
10880 auto Update = PendingUpdateRecords.pop_back_val();
10881 ReadingKindTracker ReadingKind(Read_Decl, *this);
10882 loadDeclUpdateRecords(Record&: Update);
10883 }
10884
10885 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10886 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10887 auto DuplicateIvars =
10888 PendingObjCExtensionIvarRedeclarations.back().second;
10889 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10890 StructuralEquivalenceContext Ctx(
10891 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10892 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10893 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10894 /*Complain =*/false,
10895 /*ErrorOnTagTypeMismatch =*/true);
10896 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10897 // Merge redeclared ivars with their predecessors.
10898 for (auto IvarPair : DuplicateIvars) {
10899 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10900 // Change semantic DeclContext but keep the lexical one.
10901 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10902 LexicalDC: Ivar->getLexicalDeclContext(),
10903 Ctx&: getContext());
10904 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10905 }
10906 // Invalidate duplicate extension and the cached ivar list.
10907 ExtensionsPair.first->setInvalidDecl();
10908 ExtensionsPair.second->getClassInterface()
10909 ->getDefinition()
10910 ->setIvarList(nullptr);
10911 } else {
10912 for (auto IvarPair : DuplicateIvars) {
10913 Diag(Loc: IvarPair.first->getLocation(),
10914 DiagID: diag::err_duplicate_ivar_declaration)
10915 << IvarPair.first->getIdentifier();
10916 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10917 }
10918 }
10919 PendingObjCExtensionIvarRedeclarations.pop_back();
10920 }
10921 }
10922
10923 // At this point, all update records for loaded decls are in place, so any
10924 // fake class definitions should have become real.
10925 assert(PendingFakeDefinitionData.empty() &&
10926 "faked up a class definition but never saw the real one");
10927
10928 // If we deserialized any C++ or Objective-C class definitions, any
10929 // Objective-C protocol definitions, or any redeclarable templates, make sure
10930 // that all redeclarations point to the definitions. Note that this can only
10931 // happen now, after the redeclaration chains have been fully wired.
10932 for (Decl *D : PendingDefinitions) {
10933 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10934 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10935 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10936 R = R->getPreviousDecl()) {
10937 assert((R == D) ==
10938 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10939 "declaration thinks it's the definition but it isn't");
10940 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10941 }
10942 }
10943
10944 continue;
10945 }
10946
10947 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10948 // Make sure that the ObjCInterfaceType points at the definition.
10949 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10950 ->Decl = ID;
10951
10952 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10953 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10954
10955 continue;
10956 }
10957
10958 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10959 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10960 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10961
10962 continue;
10963 }
10964
10965 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10966 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10967 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10968 }
10969 PendingDefinitions.clear();
10970
10971 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10972 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10973 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10974 return VD->isThisDeclarationADefinition() ||
10975 VD->isThisDeclarationADemotedDefinition();
10976
10977 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10978 return TD->isThisDeclarationADefinition() ||
10979 TD->isThisDeclarationADemotedDefinition();
10980
10981 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10982 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10983
10984 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10985 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10986
10987 // Conservatively return false here.
10988 return false;
10989 };
10990
10991 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10992 return hasDefinitionImpl(D, hasDefinitionImpl);
10993 };
10994
10995 // It is not good to prevent multiple declarations since the forward
10996 // declaration is common. Let's try to avoid duplicated definitions
10997 // only.
10998 if (!hasDefinition(D) || !hasDefinition(Previous))
10999 continue;
11000
11001 Module *PM = Previous->getOwningModule();
11002 Module *DM = D->getOwningModule();
11003 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
11004 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
11005 << (DM ? DM->getTopLevelModuleName() : "global module");
11006 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
11007 }
11008 PendingWarningForDuplicatedDefsInModuleUnits.clear();
11009
11010 // Load the bodies of any functions or methods we've encountered. We do
11011 // this now (delayed) so that we can be sure that the declaration chains
11012 // have been fully wired up (hasBody relies on this).
11013 // FIXME: We shouldn't require complete redeclaration chains here.
11014 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
11015 PBEnd = PendingBodies.end();
11016 PB != PBEnd; ++PB) {
11017 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
11018 // FIXME: Check for =delete/=default?
11019 const FunctionDecl *Defn = nullptr;
11020 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
11021 FD->setLazyBody(PB->second);
11022 } else {
11023 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
11024 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
11025
11026 if (!FD->isLateTemplateParsed() &&
11027 !NonConstDefn->isLateTemplateParsed() &&
11028 // We only perform ODR checks for decls not in the explicit
11029 // global module fragment.
11030 !shouldSkipCheckingODR(D: FD) &&
11031 !shouldSkipCheckingODR(D: NonConstDefn) &&
11032 FD->getODRHash() != NonConstDefn->getODRHash()) {
11033 if (!isa<CXXMethodDecl>(Val: FD)) {
11034 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11035 } else if (FD->getLexicalParent()->isFileContext() &&
11036 NonConstDefn->getLexicalParent()->isFileContext()) {
11037 // Only diagnose out-of-line method definitions. If they are
11038 // in class definitions, then an error will be generated when
11039 // processing the class bodies.
11040 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
11041 }
11042 }
11043 }
11044 continue;
11045 }
11046
11047 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
11048 if (!getContext().getLangOpts().Modules || !MD->hasBody())
11049 MD->setLazyBody(PB->second);
11050 }
11051 PendingBodies.clear();
11052
11053 // Inform any classes that had members added that they now have more members.
11054 for (auto [RD, MD] : PendingAddedClassMembers) {
11055 // The module we just imported added MD to RD so we should do the same, but
11056 // only RD doesn't already contain the same member. This can happen when two
11057 // modules independent add and serialize the same implicit member.
11058
11059 // Fast path: we can check for the presence of implicit special members
11060 // (default/copy/move constructors + copy/move assignments + destructors)
11061 // with RD's needsImplicit*() methods (which are constant time).
11062 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Val: MD)) {
11063 if (!Ctor->isInheritingConstructor()) {
11064 if (Ctor->isDefaultConstructor()) {
11065 if (RD->needsImplicitDefaultConstructor())
11066 RD->addHiddenDecl(D: MD);
11067 continue;
11068 }
11069 if (Ctor->isCopyConstructor()) {
11070 if (RD->needsImplicitCopyConstructor())
11071 RD->addHiddenDecl(D: MD);
11072 continue;
11073 }
11074 if (Ctor->isMoveConstructor()) {
11075 if (RD->needsImplicitMoveConstructor())
11076 RD->addHiddenDecl(D: MD);
11077 continue;
11078 }
11079 }
11080 } else if (const auto *Mthd = dyn_cast<CXXMethodDecl>(Val: MD)) {
11081 if (isa<CXXDestructorDecl>(Val: Mthd)) {
11082 if (RD->needsImplicitDestructor())
11083 RD->addHiddenDecl(D: MD);
11084 continue;
11085 }
11086 if (Mthd->isCopyAssignmentOperator()) {
11087 if (RD->needsImplicitCopyAssignment())
11088 RD->addHiddenDecl(D: MD);
11089 continue;
11090 }
11091 if (Mthd->isMoveAssignmentOperator()) {
11092 if (RD->needsImplicitMoveAssignment())
11093 RD->addHiddenDecl(D: MD);
11094 continue;
11095 }
11096 }
11097
11098 if (auto *FD = dyn_cast<FunctionDecl>(Val: MD)) {
11099 // Slow path: this is some other implicit added member (e.g., inherited
11100 // constructor). We need iterate through all of RD's members and compare
11101 // their ODR hash against MD's.
11102 auto needToAddMD = true;
11103 for (auto *RDD : RD->noload_decls()) {
11104 if (auto *RDFD = dyn_cast<FunctionDecl>(Val: RDD)) {
11105 if (FD->getODRHash() == RDFD->getODRHash()) {
11106 needToAddMD = false;
11107 break;
11108 }
11109 }
11110 }
11111 if (needToAddMD)
11112 RD->addHiddenDecl(D: MD);
11113 continue;
11114 }
11115
11116 // This branch is only reachable if MD is not a function decl (which means
11117 // it doesn't have an ODR hash to compare). Call addedMember() to preserve
11118 // historical behavior.
11119 RD->addedMember(D: MD);
11120 }
11121 PendingAddedClassMembers.clear();
11122
11123 // Do some cleanup.
11124 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
11125 getContext().deduplicateMergedDefinitionsFor(ND);
11126 PendingMergedDefinitionsToDeduplicate.clear();
11127
11128 // For each decl chain that we wanted to complete while deserializing, mark
11129 // it as "still needs to be completed".
11130 for (Decl *D : PendingIncompleteDeclChains)
11131 markIncompleteDeclChain(D);
11132 PendingIncompleteDeclChains.clear();
11133
11134 assert(PendingIdentifierInfos.empty() &&
11135 "Should be empty at the end of finishPendingActions");
11136 assert(PendingDeducedFunctionTypes.empty() &&
11137 "Should be empty at the end of finishPendingActions");
11138 assert(PendingDeducedVarTypes.empty() &&
11139 "Should be empty at the end of finishPendingActions");
11140 assert(PendingDeclChains.empty() &&
11141 "Should be empty at the end of finishPendingActions");
11142 assert(PendingMacroIDs.empty() &&
11143 "Should be empty at the end of finishPendingActions");
11144 assert(PendingDeclContextInfos.empty() &&
11145 "Should be empty at the end of finishPendingActions");
11146 assert(PendingUpdateRecords.empty() &&
11147 "Should be empty at the end of finishPendingActions");
11148 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
11149 "Should be empty at the end of finishPendingActions");
11150 assert(PendingFakeDefinitionData.empty() &&
11151 "Should be empty at the end of finishPendingActions");
11152 assert(PendingDefinitions.empty() &&
11153 "Should be empty at the end of finishPendingActions");
11154 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
11155 "Should be empty at the end of finishPendingActions");
11156 assert(PendingBodies.empty() &&
11157 "Should be empty at the end of finishPendingActions");
11158 assert(PendingAddedClassMembers.empty() &&
11159 "Should be empty at the end of finishPendingActions");
11160 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
11161 "Should be empty at the end of finishPendingActions");
11162 assert(PendingIncompleteDeclChains.empty() &&
11163 "Should be empty at the end of finishPendingActions");
11164}
11165
11166void ASTReader::diagnoseOdrViolations() {
11167 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
11168 PendingRecordOdrMergeFailures.empty() &&
11169 PendingFunctionOdrMergeFailures.empty() &&
11170 PendingEnumOdrMergeFailures.empty() &&
11171 PendingObjCInterfaceOdrMergeFailures.empty() &&
11172 PendingObjCProtocolOdrMergeFailures.empty())
11173 return;
11174
11175 // Trigger the import of the full definition of each class that had any
11176 // odr-merging problems, so we can produce better diagnostics for them.
11177 // These updates may in turn find and diagnose some ODR failures, so take
11178 // ownership of the set first.
11179 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
11180 PendingOdrMergeFailures.clear();
11181 for (auto &Merge : OdrMergeFailures) {
11182 Merge.first->buildLookup();
11183 Merge.first->decls_begin();
11184 Merge.first->bases_begin();
11185 Merge.first->vbases_begin();
11186 for (auto &RecordPair : Merge.second) {
11187 auto *RD = RecordPair.first;
11188 RD->decls_begin();
11189 RD->bases_begin();
11190 RD->vbases_begin();
11191 }
11192 }
11193
11194 // Trigger the import of the full definition of each record in C/ObjC.
11195 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11196 PendingRecordOdrMergeFailures.clear();
11197 for (auto &Merge : RecordOdrMergeFailures) {
11198 Merge.first->decls_begin();
11199 for (auto &D : Merge.second)
11200 D->decls_begin();
11201 }
11202
11203 // Trigger the import of the full interface definition.
11204 auto ObjCInterfaceOdrMergeFailures =
11205 std::move(PendingObjCInterfaceOdrMergeFailures);
11206 PendingObjCInterfaceOdrMergeFailures.clear();
11207 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11208 Merge.first->decls_begin();
11209 for (auto &InterfacePair : Merge.second)
11210 InterfacePair.first->decls_begin();
11211 }
11212
11213 // Trigger the import of functions.
11214 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11215 PendingFunctionOdrMergeFailures.clear();
11216 for (auto &Merge : FunctionOdrMergeFailures) {
11217 Merge.first->buildLookup();
11218 Merge.first->decls_begin();
11219 Merge.first->getBody();
11220 for (auto &FD : Merge.second) {
11221 FD->buildLookup();
11222 FD->decls_begin();
11223 FD->getBody();
11224 }
11225 }
11226
11227 // Trigger the import of enums.
11228 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11229 PendingEnumOdrMergeFailures.clear();
11230 for (auto &Merge : EnumOdrMergeFailures) {
11231 Merge.first->decls_begin();
11232 for (auto &Enum : Merge.second) {
11233 Enum->decls_begin();
11234 }
11235 }
11236
11237 // Trigger the import of the full protocol definition.
11238 auto ObjCProtocolOdrMergeFailures =
11239 std::move(PendingObjCProtocolOdrMergeFailures);
11240 PendingObjCProtocolOdrMergeFailures.clear();
11241 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11242 Merge.first->decls_begin();
11243 for (auto &ProtocolPair : Merge.second)
11244 ProtocolPair.first->decls_begin();
11245 }
11246
11247 // For each declaration from a merged context, check that the canonical
11248 // definition of that context also contains a declaration of the same
11249 // entity.
11250 //
11251 // Caution: this loop does things that might invalidate iterators into
11252 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11253 while (!PendingOdrMergeChecks.empty()) {
11254 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11255
11256 // FIXME: Skip over implicit declarations for now. This matters for things
11257 // like implicitly-declared special member functions. This isn't entirely
11258 // correct; we can end up with multiple unmerged declarations of the same
11259 // implicit entity.
11260 if (D->isImplicit())
11261 continue;
11262
11263 DeclContext *CanonDef = D->getDeclContext();
11264
11265 bool Found = false;
11266 const Decl *DCanon = D->getCanonicalDecl();
11267
11268 for (auto *RI : D->redecls()) {
11269 if (RI->getLexicalDeclContext() == CanonDef) {
11270 Found = true;
11271 break;
11272 }
11273 }
11274 if (Found)
11275 continue;
11276
11277 // Quick check failed, time to do the slow thing. Note, we can't just
11278 // look up the name of D in CanonDef here, because the member that is
11279 // in CanonDef might not be found by name lookup (it might have been
11280 // replaced by a more recent declaration in the lookup table), and we
11281 // can't necessarily find it in the redeclaration chain because it might
11282 // be merely mergeable, not redeclarable.
11283 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11284 for (auto *CanonMember : CanonDef->decls()) {
11285 if (CanonMember->getCanonicalDecl() == DCanon) {
11286 // This can happen if the declaration is merely mergeable and not
11287 // actually redeclarable (we looked for redeclarations earlier).
11288 //
11289 // FIXME: We should be able to detect this more efficiently, without
11290 // pulling in all of the members of CanonDef.
11291 Found = true;
11292 break;
11293 }
11294 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11295 if (ND->getDeclName() == D->getDeclName())
11296 Candidates.push_back(Elt: ND);
11297 }
11298
11299 if (!Found) {
11300 // The AST doesn't like TagDecls becoming invalid after they've been
11301 // completed. We only really need to mark FieldDecls as invalid here.
11302 if (!isa<TagDecl>(Val: D))
11303 D->setInvalidDecl();
11304
11305 // Ensure we don't accidentally recursively enter deserialization while
11306 // we're producing our diagnostic.
11307 Deserializing RecursionGuard(this);
11308
11309 std::string CanonDefModule =
11310 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11311 D: cast<Decl>(Val: CanonDef));
11312 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11313 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11314 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11315
11316 if (Candidates.empty())
11317 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11318 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11319 else {
11320 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11321 Diag(Loc: Candidates[I]->getLocation(),
11322 DiagID: diag::note_module_odr_violation_possible_decl)
11323 << Candidates[I];
11324 }
11325
11326 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11327 }
11328 }
11329
11330 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11331 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11332 ObjCInterfaceOdrMergeFailures.empty() &&
11333 ObjCProtocolOdrMergeFailures.empty())
11334 return;
11335
11336 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11337 getPreprocessor().getLangOpts());
11338
11339 // Issue any pending ODR-failure diagnostics.
11340 for (auto &Merge : OdrMergeFailures) {
11341 // If we've already pointed out a specific problem with this class, don't
11342 // bother issuing a general "something's different" diagnostic.
11343 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11344 continue;
11345
11346 bool Diagnosed = false;
11347 CXXRecordDecl *FirstRecord = Merge.first;
11348 for (auto &RecordPair : Merge.second) {
11349 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11350 SecondDD: RecordPair.second)) {
11351 Diagnosed = true;
11352 break;
11353 }
11354 }
11355
11356 if (!Diagnosed) {
11357 // All definitions are updates to the same declaration. This happens if a
11358 // module instantiates the declaration of a class template specialization
11359 // and two or more other modules instantiate its definition.
11360 //
11361 // FIXME: Indicate which modules had instantiations of this definition.
11362 // FIXME: How can this even happen?
11363 Diag(Loc: Merge.first->getLocation(),
11364 DiagID: diag::err_module_odr_violation_different_instantiations)
11365 << Merge.first;
11366 }
11367 }
11368
11369 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11370 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11371 for (auto &Merge : RecordOdrMergeFailures) {
11372 // If we've already pointed out a specific problem with this class, don't
11373 // bother issuing a general "something's different" diagnostic.
11374 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11375 continue;
11376
11377 RecordDecl *FirstRecord = Merge.first;
11378 bool Diagnosed = false;
11379 for (auto *SecondRecord : Merge.second) {
11380 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11381 Diagnosed = true;
11382 break;
11383 }
11384 }
11385 (void)Diagnosed;
11386 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11387 }
11388
11389 // Issue ODR failures diagnostics for functions.
11390 for (auto &Merge : FunctionOdrMergeFailures) {
11391 FunctionDecl *FirstFunction = Merge.first;
11392 bool Diagnosed = false;
11393 for (auto &SecondFunction : Merge.second) {
11394 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11395 Diagnosed = true;
11396 break;
11397 }
11398 }
11399 (void)Diagnosed;
11400 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11401 }
11402
11403 // Issue ODR failures diagnostics for enums.
11404 for (auto &Merge : EnumOdrMergeFailures) {
11405 // If we've already pointed out a specific problem with this enum, don't
11406 // bother issuing a general "something's different" diagnostic.
11407 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11408 continue;
11409
11410 EnumDecl *FirstEnum = Merge.first;
11411 bool Diagnosed = false;
11412 for (auto &SecondEnum : Merge.second) {
11413 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11414 Diagnosed = true;
11415 break;
11416 }
11417 }
11418 (void)Diagnosed;
11419 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11420 }
11421
11422 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11423 // If we've already pointed out a specific problem with this interface,
11424 // don't bother issuing a general "something's different" diagnostic.
11425 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11426 continue;
11427
11428 bool Diagnosed = false;
11429 ObjCInterfaceDecl *FirstID = Merge.first;
11430 for (auto &InterfacePair : Merge.second) {
11431 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11432 SecondDD: InterfacePair.second)) {
11433 Diagnosed = true;
11434 break;
11435 }
11436 }
11437 (void)Diagnosed;
11438 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11439 }
11440
11441 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11442 // If we've already pointed out a specific problem with this protocol,
11443 // don't bother issuing a general "something's different" diagnostic.
11444 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11445 continue;
11446
11447 ObjCProtocolDecl *FirstProtocol = Merge.first;
11448 bool Diagnosed = false;
11449 for (auto &ProtocolPair : Merge.second) {
11450 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11451 SecondDD: ProtocolPair.second)) {
11452 Diagnosed = true;
11453 break;
11454 }
11455 }
11456 (void)Diagnosed;
11457 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11458 }
11459}
11460
11461void ASTReader::StartedDeserializing() {
11462 if (llvm::Timer *T = ReadTimer.get();
11463 ++NumCurrentElementsDeserializing == 1 && T)
11464 ReadTimeRegion.emplace(args&: T);
11465}
11466
11467void ASTReader::FinishedDeserializing() {
11468 assert(NumCurrentElementsDeserializing &&
11469 "FinishedDeserializing not paired with StartedDeserializing");
11470 if (NumCurrentElementsDeserializing == 1) {
11471 // We decrease NumCurrentElementsDeserializing only after pending actions
11472 // are finished, to avoid recursively re-calling finishPendingActions().
11473 finishPendingActions();
11474 }
11475 --NumCurrentElementsDeserializing;
11476
11477 if (NumCurrentElementsDeserializing == 0) {
11478 {
11479 // Guard variable to avoid recursively entering the process of passing
11480 // decls to consumer.
11481 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11482 /*NewValue=*/false);
11483
11484 // Propagate exception specification and deduced type updates along
11485 // redeclaration chains.
11486 //
11487 // We do this now rather than in finishPendingActions because we want to
11488 // be able to walk the complete redeclaration chains of the updated decls.
11489 while (!PendingExceptionSpecUpdates.empty() ||
11490 !PendingDeducedTypeUpdates.empty() ||
11491 !PendingUndeducedFunctionDecls.empty()) {
11492 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11493 PendingExceptionSpecUpdates.clear();
11494 for (auto Update : ESUpdates) {
11495 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11496 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11497 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11498 if (auto *Listener = getContext().getASTMutationListener())
11499 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11500 for (auto *Redecl : Update.second->redecls())
11501 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11502 }
11503
11504 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11505 PendingDeducedTypeUpdates.clear();
11506 for (auto Update : DTUpdates) {
11507 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11508 // FIXME: If the return type is already deduced, check that it
11509 // matches.
11510 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11511 ResultType: Update.second);
11512 }
11513
11514 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11515 PendingUndeducedFunctionDecls.clear();
11516 // We hope we can find the deduced type for the functions by iterating
11517 // redeclarations in other modules.
11518 for (FunctionDecl *UndeducedFD : UDTUpdates)
11519 (void)UndeducedFD->getMostRecentDecl();
11520 }
11521
11522 ReadTimeRegion.reset();
11523
11524 diagnoseOdrViolations();
11525 }
11526
11527 // We are not in recursive loading, so it's safe to pass the "interesting"
11528 // decls to the consumer.
11529 if (Consumer)
11530 PassInterestingDeclsToConsumer();
11531 }
11532}
11533
11534void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11535 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11536 // Remove any fake results before adding any real ones.
11537 auto It = PendingFakeLookupResults.find(Key: II);
11538 if (It != PendingFakeLookupResults.end()) {
11539 for (auto *ND : It->second)
11540 SemaObj->IdResolver.RemoveDecl(D: ND);
11541 // FIXME: this works around module+PCH performance issue.
11542 // Rather than erase the result from the map, which is O(n), just clear
11543 // the vector of NamedDecls.
11544 It->second.clear();
11545 }
11546 }
11547
11548 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11549 SemaObj->TUScope->AddDecl(D);
11550 } else if (SemaObj->TUScope) {
11551 // Adding the decl to IdResolver may have failed because it was already in
11552 // (even though it was not added in scope). If it is already in, make sure
11553 // it gets in the scope as well.
11554 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11555 SemaObj->TUScope->AddDecl(D);
11556 }
11557}
11558
11559ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11560 ASTContext *Context,
11561 const PCHContainerReader &PCHContainerRdr,
11562 const CodeGenOptions &CodeGenOpts,
11563 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11564 StringRef isysroot,
11565 DisableValidationForModuleKind DisableValidationKind,
11566 bool AllowASTWithCompilerErrors,
11567 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11568 bool ForceValidateUserInputs,
11569 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11570 std::unique_ptr<llvm::Timer> ReadTimer)
11571 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11572 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11573 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11574 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11575 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11576 StackHandler(Diags), PP(PP), ContextObj(Context),
11577 CodeGenOpts(CodeGenOpts),
11578 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11579 PP.getHeaderSearchInfo()),
11580 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11581 DisableValidationKind(DisableValidationKind),
11582 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11583 AllowConfigurationMismatch(AllowConfigurationMismatch),
11584 ValidateSystemInputs(ValidateSystemInputs),
11585 ForceValidateUserInputs(ForceValidateUserInputs),
11586 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11587 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11588 SourceMgr.setExternalSLocEntrySource(this);
11589
11590 PathBuf.reserve(N: 256);
11591
11592 for (const auto &Ext : Extensions) {
11593 auto BlockName = Ext->getExtensionMetadata().BlockName;
11594 auto Known = ModuleFileExtensions.find(Key: BlockName);
11595 if (Known != ModuleFileExtensions.end()) {
11596 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11597 << BlockName;
11598 continue;
11599 }
11600
11601 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11602 }
11603}
11604
11605ASTReader::~ASTReader() {
11606 if (OwnsDeserializationListener)
11607 delete DeserializationListener;
11608}
11609
11610IdentifierResolver &ASTReader::getIdResolver() {
11611 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11612}
11613
11614Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11615 unsigned AbbrevID) {
11616 Idx = 0;
11617 Record.clear();
11618 return Cursor.readRecord(AbbrevID, Vals&: Record);
11619}
11620//===----------------------------------------------------------------------===//
11621//// OMPClauseReader implementation
11622////===----------------------------------------------------------------------===//
11623
11624// This has to be in namespace clang because it's friended by all
11625// of the OMP clauses.
11626namespace clang {
11627
11628class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11629 ASTRecordReader &Record;
11630 ASTContext &Context;
11631
11632public:
11633 OMPClauseReader(ASTRecordReader &Record)
11634 : Record(Record), Context(Record.getContext()) {}
11635#define GEN_CLANG_CLAUSE_CLASS
11636#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11637#include "llvm/Frontend/OpenMP/OMP.inc"
11638 OMPClause *readClause();
11639 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11640 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11641};
11642
11643} // end namespace clang
11644
11645OMPClause *ASTRecordReader::readOMPClause() {
11646 return OMPClauseReader(*this).readClause();
11647}
11648
11649OMPClause *OMPClauseReader::readClause() {
11650 OMPClause *C = nullptr;
11651 switch (llvm::omp::Clause(Record.readInt())) {
11652 case llvm::omp::OMPC_if:
11653 C = new (Context) OMPIfClause();
11654 break;
11655 case llvm::omp::OMPC_final:
11656 C = new (Context) OMPFinalClause();
11657 break;
11658 case llvm::omp::OMPC_num_threads:
11659 C = OMPNumThreadsClause::CreateEmpty(C: Context, N: Record.readInt());
11660 break;
11661 case llvm::omp::OMPC_safelen:
11662 C = new (Context) OMPSafelenClause();
11663 break;
11664 case llvm::omp::OMPC_simdlen:
11665 C = new (Context) OMPSimdlenClause();
11666 break;
11667 case llvm::omp::OMPC_sizes: {
11668 unsigned NumSizes = Record.readInt();
11669 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11670 break;
11671 }
11672 case llvm::omp::OMPC_counts: {
11673 unsigned NumCounts = Record.readInt();
11674 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11675 break;
11676 }
11677 case llvm::omp::OMPC_permutation: {
11678 unsigned NumLoops = Record.readInt();
11679 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11680 break;
11681 }
11682 case llvm::omp::OMPC_full:
11683 C = new (Context) OMPFullClause();
11684 break;
11685 case llvm::omp::OMPC_partial:
11686 C = new (Context) OMPPartialClause();
11687 break;
11688 case llvm::omp::OMPC_depth:
11689 C = new (Context) OMPDepthClause();
11690 break;
11691 case llvm::omp::OMPC_looprange:
11692 C = new (Context) OMPLoopRangeClause();
11693 break;
11694 case llvm::omp::OMPC_allocator:
11695 C = new (Context) OMPAllocatorClause();
11696 break;
11697 case llvm::omp::OMPC_collapse:
11698 C = new (Context) OMPCollapseClause();
11699 break;
11700 case llvm::omp::OMPC_default:
11701 C = new (Context) OMPDefaultClause();
11702 break;
11703 case llvm::omp::OMPC_proc_bind:
11704 C = new (Context) OMPProcBindClause();
11705 break;
11706 case llvm::omp::OMPC_schedule:
11707 C = new (Context) OMPScheduleClause();
11708 break;
11709 case llvm::omp::OMPC_ordered:
11710 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11711 break;
11712 case llvm::omp::OMPC_nowait:
11713 C = new (Context) OMPNowaitClause();
11714 break;
11715 case llvm::omp::OMPC_untied:
11716 C = new (Context) OMPUntiedClause();
11717 break;
11718 case llvm::omp::OMPC_mergeable:
11719 C = new (Context) OMPMergeableClause();
11720 break;
11721 case llvm::omp::OMPC_threadset:
11722 C = new (Context) OMPThreadsetClause();
11723 break;
11724 case llvm::omp::OMPC_transparent:
11725 C = new (Context) OMPTransparentClause();
11726 break;
11727 case llvm::omp::OMPC_read:
11728 C = new (Context) OMPReadClause();
11729 break;
11730 case llvm::omp::OMPC_write:
11731 C = new (Context) OMPWriteClause();
11732 break;
11733 case llvm::omp::OMPC_update:
11734 C = new (Context) OMPUpdateClause();
11735 break;
11736 case llvm::omp::OMPC_update_depend_objects:
11737 C = OMPUpdateDependObjectsClause::CreateEmpty(C: Context);
11738 break;
11739 case llvm::omp::OMPC_capture:
11740 C = new (Context) OMPCaptureClause();
11741 break;
11742 case llvm::omp::OMPC_compare:
11743 C = new (Context) OMPCompareClause();
11744 break;
11745 case llvm::omp::OMPC_fail:
11746 C = new (Context) OMPFailClause();
11747 break;
11748 case llvm::omp::OMPC_seq_cst:
11749 C = new (Context) OMPSeqCstClause();
11750 break;
11751 case llvm::omp::OMPC_acq_rel:
11752 C = new (Context) OMPAcqRelClause();
11753 break;
11754 case llvm::omp::OMPC_absent: {
11755 unsigned NumKinds = Record.readInt();
11756 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11757 break;
11758 }
11759 case llvm::omp::OMPC_holds:
11760 C = new (Context) OMPHoldsClause();
11761 break;
11762 case llvm::omp::OMPC_contains: {
11763 unsigned NumKinds = Record.readInt();
11764 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11765 break;
11766 }
11767 case llvm::omp::OMPC_no_openmp:
11768 C = new (Context) OMPNoOpenMPClause();
11769 break;
11770 case llvm::omp::OMPC_no_openmp_routines:
11771 C = new (Context) OMPNoOpenMPRoutinesClause();
11772 break;
11773 case llvm::omp::OMPC_no_openmp_constructs:
11774 C = new (Context) OMPNoOpenMPConstructsClause();
11775 break;
11776 case llvm::omp::OMPC_no_parallelism:
11777 C = new (Context) OMPNoParallelismClause();
11778 break;
11779 case llvm::omp::OMPC_acquire:
11780 C = new (Context) OMPAcquireClause();
11781 break;
11782 case llvm::omp::OMPC_release:
11783 C = new (Context) OMPReleaseClause();
11784 break;
11785 case llvm::omp::OMPC_relaxed:
11786 C = new (Context) OMPRelaxedClause();
11787 break;
11788 case llvm::omp::OMPC_weak:
11789 C = new (Context) OMPWeakClause();
11790 break;
11791 case llvm::omp::OMPC_threads:
11792 C = new (Context) OMPThreadsClause();
11793 break;
11794 case llvm::omp::OMPC_simd:
11795 C = new (Context) OMPSIMDClause();
11796 break;
11797 case llvm::omp::OMPC_nogroup:
11798 C = new (Context) OMPNogroupClause();
11799 break;
11800 case llvm::omp::OMPC_unified_address:
11801 C = new (Context) OMPUnifiedAddressClause();
11802 break;
11803 case llvm::omp::OMPC_unified_shared_memory:
11804 C = new (Context) OMPUnifiedSharedMemoryClause();
11805 break;
11806 case llvm::omp::OMPC_reverse_offload:
11807 C = new (Context) OMPReverseOffloadClause();
11808 break;
11809 case llvm::omp::OMPC_dynamic_allocators:
11810 C = new (Context) OMPDynamicAllocatorsClause();
11811 break;
11812 case llvm::omp::OMPC_atomic_default_mem_order:
11813 C = new (Context) OMPAtomicDefaultMemOrderClause();
11814 break;
11815 case llvm::omp::OMPC_self_maps:
11816 C = new (Context) OMPSelfMapsClause();
11817 break;
11818 case llvm::omp::OMPC_at:
11819 C = new (Context) OMPAtClause();
11820 break;
11821 case llvm::omp::OMPC_severity:
11822 C = new (Context) OMPSeverityClause();
11823 break;
11824 case llvm::omp::OMPC_message:
11825 C = new (Context) OMPMessageClause();
11826 break;
11827 case llvm::omp::OMPC_private:
11828 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11829 break;
11830 case llvm::omp::OMPC_firstprivate:
11831 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11832 break;
11833 case llvm::omp::OMPC_lastprivate:
11834 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11835 break;
11836 case llvm::omp::OMPC_shared:
11837 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11838 break;
11839 case llvm::omp::OMPC_reduction: {
11840 unsigned N = Record.readInt();
11841 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11842 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11843 break;
11844 }
11845 case llvm::omp::OMPC_task_reduction:
11846 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11847 break;
11848 case llvm::omp::OMPC_in_reduction:
11849 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11850 break;
11851 case llvm::omp::OMPC_linear:
11852 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11853 break;
11854 case llvm::omp::OMPC_aligned:
11855 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11856 break;
11857 case llvm::omp::OMPC_copyin:
11858 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11859 break;
11860 case llvm::omp::OMPC_copyprivate:
11861 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11862 break;
11863 case llvm::omp::OMPC_flush:
11864 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11865 break;
11866 case llvm::omp::OMPC_depobj:
11867 C = new (Context) OMPDepobjClause();
11868 break;
11869 case llvm::omp::OMPC_depend: {
11870 unsigned NumVars = Record.readInt();
11871 unsigned NumLoops = Record.readInt();
11872 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11873 break;
11874 }
11875 case llvm::omp::OMPC_device:
11876 C = new (Context) OMPDeviceClause();
11877 break;
11878 case llvm::omp::OMPC_map: {
11879 OMPMappableExprListSizeTy Sizes;
11880 Sizes.NumVars = Record.readInt();
11881 Sizes.NumUniqueDeclarations = Record.readInt();
11882 Sizes.NumComponentLists = Record.readInt();
11883 Sizes.NumComponents = Record.readInt();
11884 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11885 break;
11886 }
11887 case llvm::omp::OMPC_num_teams:
11888 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11889 break;
11890 case llvm::omp::OMPC_thread_limit:
11891 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11892 break;
11893 case llvm::omp::OMPC_priority:
11894 C = new (Context) OMPPriorityClause();
11895 break;
11896 case llvm::omp::OMPC_grainsize:
11897 C = new (Context) OMPGrainsizeClause();
11898 break;
11899 case llvm::omp::OMPC_num_tasks:
11900 C = new (Context) OMPNumTasksClause();
11901 break;
11902 case llvm::omp::OMPC_hint:
11903 C = new (Context) OMPHintClause();
11904 break;
11905 case llvm::omp::OMPC_dist_schedule:
11906 C = new (Context) OMPDistScheduleClause();
11907 break;
11908 case llvm::omp::OMPC_defaultmap:
11909 C = new (Context) OMPDefaultmapClause();
11910 break;
11911 case llvm::omp::OMPC_to: {
11912 OMPMappableExprListSizeTy Sizes;
11913 Sizes.NumVars = Record.readInt();
11914 Sizes.NumUniqueDeclarations = Record.readInt();
11915 Sizes.NumComponentLists = Record.readInt();
11916 Sizes.NumComponents = Record.readInt();
11917 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11918 break;
11919 }
11920 case llvm::omp::OMPC_from: {
11921 OMPMappableExprListSizeTy Sizes;
11922 Sizes.NumVars = Record.readInt();
11923 Sizes.NumUniqueDeclarations = Record.readInt();
11924 Sizes.NumComponentLists = Record.readInt();
11925 Sizes.NumComponents = Record.readInt();
11926 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11927 break;
11928 }
11929 case llvm::omp::OMPC_use_device_ptr: {
11930 OMPMappableExprListSizeTy Sizes;
11931 Sizes.NumVars = Record.readInt();
11932 Sizes.NumUniqueDeclarations = Record.readInt();
11933 Sizes.NumComponentLists = Record.readInt();
11934 Sizes.NumComponents = Record.readInt();
11935 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11936 break;
11937 }
11938 case llvm::omp::OMPC_use_device_addr: {
11939 OMPMappableExprListSizeTy Sizes;
11940 Sizes.NumVars = Record.readInt();
11941 Sizes.NumUniqueDeclarations = Record.readInt();
11942 Sizes.NumComponentLists = Record.readInt();
11943 Sizes.NumComponents = Record.readInt();
11944 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11945 break;
11946 }
11947 case llvm::omp::OMPC_is_device_ptr: {
11948 OMPMappableExprListSizeTy Sizes;
11949 Sizes.NumVars = Record.readInt();
11950 Sizes.NumUniqueDeclarations = Record.readInt();
11951 Sizes.NumComponentLists = Record.readInt();
11952 Sizes.NumComponents = Record.readInt();
11953 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11954 break;
11955 }
11956 case llvm::omp::OMPC_has_device_addr: {
11957 OMPMappableExprListSizeTy Sizes;
11958 Sizes.NumVars = Record.readInt();
11959 Sizes.NumUniqueDeclarations = Record.readInt();
11960 Sizes.NumComponentLists = Record.readInt();
11961 Sizes.NumComponents = Record.readInt();
11962 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11963 break;
11964 }
11965 case llvm::omp::OMPC_allocate:
11966 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11967 break;
11968 case llvm::omp::OMPC_nontemporal:
11969 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11970 break;
11971 case llvm::omp::OMPC_inclusive:
11972 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11973 break;
11974 case llvm::omp::OMPC_exclusive:
11975 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11976 break;
11977 case llvm::omp::OMPC_order:
11978 C = new (Context) OMPOrderClause();
11979 break;
11980 case llvm::omp::OMPC_init: {
11981 unsigned VarListSize = Record.readInt();
11982 unsigned NumAttrs = Record.readInt();
11983 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11984 NumAttrs);
11985 break;
11986 }
11987 case llvm::omp::OMPC_use:
11988 C = new (Context) OMPUseClause();
11989 break;
11990 case llvm::omp::OMPC_destroy:
11991 C = new (Context) OMPDestroyClause();
11992 break;
11993 case llvm::omp::OMPC_novariants:
11994 C = new (Context) OMPNovariantsClause();
11995 break;
11996 case llvm::omp::OMPC_nocontext:
11997 C = new (Context) OMPNocontextClause();
11998 break;
11999 case llvm::omp::OMPC_detach:
12000 C = new (Context) OMPDetachClause();
12001 break;
12002 case llvm::omp::OMPC_uses_allocators:
12003 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
12004 break;
12005 case llvm::omp::OMPC_affinity:
12006 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
12007 break;
12008 case llvm::omp::OMPC_filter:
12009 C = new (Context) OMPFilterClause();
12010 break;
12011 case llvm::omp::OMPC_bind:
12012 C = new (Context) OMPBindClause();
12013 break;
12014 case llvm::omp::OMPC_align:
12015 C = new (Context) OMPAlignClause();
12016 break;
12017 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
12018 C = new (Context) OMPXDynCGroupMemClause();
12019 break;
12020 case llvm::omp::OMPC_dyn_groupprivate:
12021 C = new (Context) OMPDynGroupprivateClause();
12022 break;
12023 case llvm::omp::OMPC_doacross: {
12024 unsigned NumVars = Record.readInt();
12025 unsigned NumLoops = Record.readInt();
12026 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
12027 break;
12028 }
12029 case llvm::omp::OMPC_ompx_attribute:
12030 C = new (Context) OMPXAttributeClause();
12031 break;
12032 case llvm::omp::OMPC_ompx_bare:
12033 C = new (Context) OMPXBareClause();
12034 break;
12035#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
12036 case llvm::omp::Enum: \
12037 break;
12038#include "llvm/Frontend/OpenMP/OMPKinds.def"
12039 default:
12040 break;
12041 }
12042 assert(C && "Unknown OMPClause type");
12043
12044 Visit(S: C);
12045 C->setLocStart(Record.readSourceLocation());
12046 C->setLocEnd(Record.readSourceLocation());
12047
12048 return C;
12049}
12050
12051void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12052 C->setPreInitStmt(S: Record.readSubStmt(),
12053 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
12054}
12055
12056void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12057 VisitOMPClauseWithPreInit(C);
12058 C->setPostUpdateExpr(Record.readSubExpr());
12059}
12060
12061void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12062 VisitOMPClauseWithPreInit(C);
12063 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12064 C->setNameModifierLoc(Record.readSourceLocation());
12065 C->setColonLoc(Record.readSourceLocation());
12066 C->setCondition(Record.readSubExpr());
12067 C->setLParenLoc(Record.readSourceLocation());
12068}
12069
12070void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12071 VisitOMPClauseWithPreInit(C);
12072 C->setCondition(Record.readSubExpr());
12073 C->setLParenLoc(Record.readSourceLocation());
12074}
12075
12076void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12077 C->setPrescriptivenessModifier(
12078 Record.readEnum<OpenMPNumThreadsClauseModifier>());
12079 C->setPrescriptivenessModifierLoc(Record.readSourceLocation());
12080 C->setDimsModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
12081 C->setDimsModifierLoc(Record.readSourceLocation());
12082 C->setDimsModifierExpr(Record.readSubExpr());
12083 VisitOMPClauseWithPreInit(C);
12084 C->setLParenLoc(Record.readSourceLocation());
12085 unsigned NumVars = C->varlist_size();
12086 SmallVector<Expr *, 16> Vars;
12087 Vars.reserve(N: NumVars);
12088 for (unsigned I = 0; I != NumVars; ++I)
12089 Vars.push_back(Elt: Record.readSubExpr());
12090 C->setVarRefs(Vars);
12091}
12092
12093void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12094 C->setSafelen(Record.readSubExpr());
12095 C->setLParenLoc(Record.readSourceLocation());
12096}
12097
12098void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12099 C->setSimdlen(Record.readSubExpr());
12100 C->setLParenLoc(Record.readSourceLocation());
12101}
12102
12103void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12104 for (Expr *&E : C->getSizesRefs())
12105 E = Record.readSubExpr();
12106 C->setLParenLoc(Record.readSourceLocation());
12107}
12108
12109void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
12110 bool HasFill = Record.readBool();
12111 if (HasFill)
12112 C->setOmpFillIndex(Record.readInt());
12113 C->setOmpFillLoc(Record.readSourceLocation());
12114 for (Expr *&E : C->getCountsRefs())
12115 E = Record.readSubExpr();
12116 C->setLParenLoc(Record.readSourceLocation());
12117}
12118
12119void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
12120 for (Expr *&E : C->getArgsRefs())
12121 E = Record.readSubExpr();
12122 C->setLParenLoc(Record.readSourceLocation());
12123}
12124
12125void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12126
12127void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12128 C->setFactor(Record.readSubExpr());
12129 C->setLParenLoc(Record.readSourceLocation());
12130}
12131
12132void OMPClauseReader::VisitOMPDepthClause(OMPDepthClause *C) {
12133 C->setDepth(Record.readSubExpr());
12134 C->setLParenLoc(Record.readSourceLocation());
12135}
12136
12137void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
12138 C->setFirst(Record.readSubExpr());
12139 C->setCount(Record.readSubExpr());
12140 C->setLParenLoc(Record.readSourceLocation());
12141 C->setFirstLoc(Record.readSourceLocation());
12142 C->setCountLoc(Record.readSourceLocation());
12143}
12144
12145void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12146 C->setAllocator(Record.readExpr());
12147 C->setLParenLoc(Record.readSourceLocation());
12148}
12149
12150void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12151 C->setNumForLoops(Record.readSubExpr());
12152 C->setLParenLoc(Record.readSourceLocation());
12153}
12154
12155void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12156 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12157 C->setLParenLoc(Record.readSourceLocation());
12158 C->setDefaultKindKwLoc(Record.readSourceLocation());
12159 C->setDefaultVariableCategory(
12160 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
12161 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
12162}
12163
12164// Read the parameter of threadset clause. This will have been saved when
12165// OMPClauseWriter is called.
12166void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
12167 C->setLParenLoc(Record.readSourceLocation());
12168 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
12169 C->setThreadsetKindLoc(ThreadsetKindLoc);
12170 OpenMPThreadsetKind TKind =
12171 static_cast<OpenMPThreadsetKind>(Record.readInt());
12172 C->setThreadsetKind(TKind);
12173}
12174
12175void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
12176 C->setLParenLoc(Record.readSourceLocation());
12177 C->setImpexTypeKind(Record.readSubExpr());
12178}
12179
12180void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12181 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12182 C->setLParenLoc(Record.readSourceLocation());
12183 C->setProcBindKindKwLoc(Record.readSourceLocation());
12184}
12185
12186void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12187 VisitOMPClauseWithPreInit(C);
12188 C->setScheduleKind(
12189 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12190 C->setFirstScheduleModifier(
12191 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12192 C->setSecondScheduleModifier(
12193 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12194 C->setChunkSize(Record.readSubExpr());
12195 C->setLParenLoc(Record.readSourceLocation());
12196 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12197 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12198 C->setScheduleKindLoc(Record.readSourceLocation());
12199 C->setCommaLoc(Record.readSourceLocation());
12200}
12201
12202void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12203 C->setNumForLoops(Record.readSubExpr());
12204 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12205 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
12206 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12207 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
12208 C->setLParenLoc(Record.readSourceLocation());
12209}
12210
12211void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12212 C->setEventHandler(Record.readSubExpr());
12213 C->setLParenLoc(Record.readSourceLocation());
12214}
12215
12216void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12217 C->setCondition(Record.readSubExpr());
12218 C->setLParenLoc(Record.readSourceLocation());
12219}
12220
12221void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12222
12223void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12224
12225void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12226
12227void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12228
12229void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12230
12231void OMPClauseReader::VisitOMPUpdateDependObjectsClause(
12232 OMPUpdateDependObjectsClause *C) {
12233 C->setLParenLoc(Record.readSourceLocation());
12234 C->setArgumentLoc(Record.readSourceLocation());
12235 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12236}
12237
12238void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12239
12240void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12241
12242// Read the parameter of fail clause. This will have been saved when
12243// OMPClauseWriter is called.
12244void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12245 C->setLParenLoc(Record.readSourceLocation());
12246 SourceLocation FailParameterLoc = Record.readSourceLocation();
12247 C->setFailParameterLoc(FailParameterLoc);
12248 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12249 C->setFailParameter(CKind);
12250}
12251
12252void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12253 unsigned Count = C->getDirectiveKinds().size();
12254 C->setLParenLoc(Record.readSourceLocation());
12255 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12256 DKVec.reserve(N: Count);
12257 for (unsigned I = 0; I < Count; I++) {
12258 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12259 }
12260 C->setDirectiveKinds(DKVec);
12261}
12262
12263void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12264 C->setExpr(Record.readExpr());
12265 C->setLParenLoc(Record.readSourceLocation());
12266}
12267
12268void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12269 unsigned Count = C->getDirectiveKinds().size();
12270 C->setLParenLoc(Record.readSourceLocation());
12271 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12272 DKVec.reserve(N: Count);
12273 for (unsigned I = 0; I < Count; I++) {
12274 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12275 }
12276 C->setDirectiveKinds(DKVec);
12277}
12278
12279void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12280
12281void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12282 OMPNoOpenMPRoutinesClause *) {}
12283
12284void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12285 OMPNoOpenMPConstructsClause *) {}
12286
12287void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12288
12289void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12290
12291void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12292
12293void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12294
12295void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12296
12297void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12298
12299void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12300
12301void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12302
12303void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12304
12305void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12306
12307void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12308 unsigned NumVars = C->varlist_size();
12309 SmallVector<Expr *, 16> Vars;
12310 Vars.reserve(N: NumVars);
12311 for (unsigned I = 0; I != NumVars; ++I)
12312 Vars.push_back(Elt: Record.readSubExpr());
12313 C->setVarRefs(Vars);
12314 C->setIsTarget(Record.readBool());
12315 C->setIsTargetSync(Record.readBool());
12316 C->setHasPreferAttrs(Record.readBool());
12317
12318 unsigned NumPrefs = C->varlist_size() - 1;
12319 SmallVector<unsigned, 4> Counts;
12320 SmallVector<Expr *, 8> Attrs;
12321 Counts.reserve(N: NumPrefs);
12322 for (unsigned I = 0; I < NumPrefs; ++I) {
12323 unsigned NA = Record.readInt();
12324 Counts.push_back(Elt: NA);
12325 for (unsigned J = 0; J < NA; ++J)
12326 Attrs.push_back(Elt: Record.readSubExpr());
12327 }
12328 C->setAttrs(Counts, Attrs);
12329
12330 C->setLParenLoc(Record.readSourceLocation());
12331 C->setVarLoc(Record.readSourceLocation());
12332}
12333
12334void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12335 C->setInteropVar(Record.readSubExpr());
12336 C->setLParenLoc(Record.readSourceLocation());
12337 C->setVarLoc(Record.readSourceLocation());
12338}
12339
12340void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12341 C->setInteropVar(Record.readSubExpr());
12342 C->setLParenLoc(Record.readSourceLocation());
12343 C->setVarLoc(Record.readSourceLocation());
12344}
12345
12346void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12347 VisitOMPClauseWithPreInit(C);
12348 C->setCondition(Record.readSubExpr());
12349 C->setLParenLoc(Record.readSourceLocation());
12350}
12351
12352void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12353 VisitOMPClauseWithPreInit(C);
12354 C->setCondition(Record.readSubExpr());
12355 C->setLParenLoc(Record.readSourceLocation());
12356}
12357
12358void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12359
12360void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12361 OMPUnifiedSharedMemoryClause *) {}
12362
12363void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12364
12365void
12366OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12367}
12368
12369void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12370 OMPAtomicDefaultMemOrderClause *C) {
12371 C->setAtomicDefaultMemOrderKind(
12372 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12373 C->setLParenLoc(Record.readSourceLocation());
12374 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12375}
12376
12377void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12378
12379void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12380 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12381 C->setLParenLoc(Record.readSourceLocation());
12382 C->setAtKindKwLoc(Record.readSourceLocation());
12383}
12384
12385void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12386 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12387 C->setLParenLoc(Record.readSourceLocation());
12388 C->setSeverityKindKwLoc(Record.readSourceLocation());
12389}
12390
12391void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12392 VisitOMPClauseWithPreInit(C);
12393 C->setMessageString(Record.readSubExpr());
12394 C->setLParenLoc(Record.readSourceLocation());
12395}
12396
12397void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12398 C->setLParenLoc(Record.readSourceLocation());
12399 unsigned NumVars = C->varlist_size();
12400 SmallVector<Expr *, 16> Vars;
12401 Vars.reserve(N: NumVars);
12402 for (unsigned i = 0; i != NumVars; ++i)
12403 Vars.push_back(Elt: Record.readSubExpr());
12404 C->setVarRefs(Vars);
12405 Vars.clear();
12406 for (unsigned i = 0; i != NumVars; ++i)
12407 Vars.push_back(Elt: Record.readSubExpr());
12408 C->setPrivateCopies(Vars);
12409}
12410
12411void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12412 VisitOMPClauseWithPreInit(C);
12413 C->setLParenLoc(Record.readSourceLocation());
12414 unsigned NumVars = C->varlist_size();
12415 SmallVector<Expr *, 16> Vars;
12416 Vars.reserve(N: NumVars);
12417 for (unsigned i = 0; i != NumVars; ++i)
12418 Vars.push_back(Elt: Record.readSubExpr());
12419 C->setVarRefs(Vars);
12420 Vars.clear();
12421 for (unsigned i = 0; i != NumVars; ++i)
12422 Vars.push_back(Elt: Record.readSubExpr());
12423 C->setPrivateCopies(Vars);
12424 Vars.clear();
12425 for (unsigned i = 0; i != NumVars; ++i)
12426 Vars.push_back(Elt: Record.readSubExpr());
12427 C->setInits(Vars);
12428}
12429
12430void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12431 VisitOMPClauseWithPostUpdate(C);
12432 C->setLParenLoc(Record.readSourceLocation());
12433 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12434 C->setKindLoc(Record.readSourceLocation());
12435 C->setColonLoc(Record.readSourceLocation());
12436 unsigned NumVars = C->varlist_size();
12437 SmallVector<Expr *, 16> Vars;
12438 Vars.reserve(N: NumVars);
12439 for (unsigned i = 0; i != NumVars; ++i)
12440 Vars.push_back(Elt: Record.readSubExpr());
12441 C->setVarRefs(Vars);
12442 Vars.clear();
12443 for (unsigned i = 0; i != NumVars; ++i)
12444 Vars.push_back(Elt: Record.readSubExpr());
12445 C->setPrivateCopies(Vars);
12446 Vars.clear();
12447 for (unsigned i = 0; i != NumVars; ++i)
12448 Vars.push_back(Elt: Record.readSubExpr());
12449 C->setSourceExprs(Vars);
12450 Vars.clear();
12451 for (unsigned i = 0; i != NumVars; ++i)
12452 Vars.push_back(Elt: Record.readSubExpr());
12453 C->setDestinationExprs(Vars);
12454 Vars.clear();
12455 for (unsigned i = 0; i != NumVars; ++i)
12456 Vars.push_back(Elt: Record.readSubExpr());
12457 C->setAssignmentOps(Vars);
12458}
12459
12460void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12461 C->setLParenLoc(Record.readSourceLocation());
12462 unsigned NumVars = C->varlist_size();
12463 SmallVector<Expr *, 16> Vars;
12464 Vars.reserve(N: NumVars);
12465 for (unsigned i = 0; i != NumVars; ++i)
12466 Vars.push_back(Elt: Record.readSubExpr());
12467 C->setVarRefs(Vars);
12468}
12469
12470void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12471 VisitOMPClauseWithPostUpdate(C);
12472 C->setLParenLoc(Record.readSourceLocation());
12473 C->setModifierLoc(Record.readSourceLocation());
12474 C->setColonLoc(Record.readSourceLocation());
12475 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12476 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12477 C->setQualifierLoc(NNSL);
12478 C->setNameInfo(DNI);
12479
12480 unsigned NumVars = C->varlist_size();
12481 SmallVector<Expr *, 16> Vars;
12482 Vars.reserve(N: NumVars);
12483 for (unsigned i = 0; i != NumVars; ++i)
12484 Vars.push_back(Elt: Record.readSubExpr());
12485 C->setVarRefs(Vars);
12486 Vars.clear();
12487 for (unsigned i = 0; i != NumVars; ++i)
12488 Vars.push_back(Elt: Record.readSubExpr());
12489 C->setPrivates(Vars);
12490 Vars.clear();
12491 for (unsigned i = 0; i != NumVars; ++i)
12492 Vars.push_back(Elt: Record.readSubExpr());
12493 C->setLHSExprs(Vars);
12494 Vars.clear();
12495 for (unsigned i = 0; i != NumVars; ++i)
12496 Vars.push_back(Elt: Record.readSubExpr());
12497 C->setRHSExprs(Vars);
12498 Vars.clear();
12499 for (unsigned i = 0; i != NumVars; ++i)
12500 Vars.push_back(Elt: Record.readSubExpr());
12501 C->setReductionOps(Vars);
12502 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12503 Vars.clear();
12504 for (unsigned i = 0; i != NumVars; ++i)
12505 Vars.push_back(Elt: Record.readSubExpr());
12506 C->setInscanCopyOps(Vars);
12507 Vars.clear();
12508 for (unsigned i = 0; i != NumVars; ++i)
12509 Vars.push_back(Elt: Record.readSubExpr());
12510 C->setInscanCopyArrayTemps(Vars);
12511 Vars.clear();
12512 for (unsigned i = 0; i != NumVars; ++i)
12513 Vars.push_back(Elt: Record.readSubExpr());
12514 C->setInscanCopyArrayElems(Vars);
12515 }
12516 unsigned NumFlags = Record.readInt();
12517 SmallVector<bool, 16> Flags;
12518 Flags.reserve(N: NumFlags);
12519 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12520 Flags.push_back(Elt: Record.readInt());
12521 C->setPrivateVariableReductionFlags(Flags);
12522}
12523
12524void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12525 VisitOMPClauseWithPostUpdate(C);
12526 C->setLParenLoc(Record.readSourceLocation());
12527 C->setColonLoc(Record.readSourceLocation());
12528 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12529 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12530 C->setQualifierLoc(NNSL);
12531 C->setNameInfo(DNI);
12532
12533 unsigned NumVars = C->varlist_size();
12534 SmallVector<Expr *, 16> Vars;
12535 Vars.reserve(N: NumVars);
12536 for (unsigned I = 0; I != NumVars; ++I)
12537 Vars.push_back(Elt: Record.readSubExpr());
12538 C->setVarRefs(Vars);
12539 Vars.clear();
12540 for (unsigned I = 0; I != NumVars; ++I)
12541 Vars.push_back(Elt: Record.readSubExpr());
12542 C->setPrivates(Vars);
12543 Vars.clear();
12544 for (unsigned I = 0; I != NumVars; ++I)
12545 Vars.push_back(Elt: Record.readSubExpr());
12546 C->setLHSExprs(Vars);
12547 Vars.clear();
12548 for (unsigned I = 0; I != NumVars; ++I)
12549 Vars.push_back(Elt: Record.readSubExpr());
12550 C->setRHSExprs(Vars);
12551 Vars.clear();
12552 for (unsigned I = 0; I != NumVars; ++I)
12553 Vars.push_back(Elt: Record.readSubExpr());
12554 C->setReductionOps(Vars);
12555}
12556
12557void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12558 VisitOMPClauseWithPostUpdate(C);
12559 C->setLParenLoc(Record.readSourceLocation());
12560 C->setColonLoc(Record.readSourceLocation());
12561 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12562 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12563 C->setQualifierLoc(NNSL);
12564 C->setNameInfo(DNI);
12565
12566 unsigned NumVars = C->varlist_size();
12567 SmallVector<Expr *, 16> Vars;
12568 Vars.reserve(N: NumVars);
12569 for (unsigned I = 0; I != NumVars; ++I)
12570 Vars.push_back(Elt: Record.readSubExpr());
12571 C->setVarRefs(Vars);
12572 Vars.clear();
12573 for (unsigned I = 0; I != NumVars; ++I)
12574 Vars.push_back(Elt: Record.readSubExpr());
12575 C->setPrivates(Vars);
12576 Vars.clear();
12577 for (unsigned I = 0; I != NumVars; ++I)
12578 Vars.push_back(Elt: Record.readSubExpr());
12579 C->setLHSExprs(Vars);
12580 Vars.clear();
12581 for (unsigned I = 0; I != NumVars; ++I)
12582 Vars.push_back(Elt: Record.readSubExpr());
12583 C->setRHSExprs(Vars);
12584 Vars.clear();
12585 for (unsigned I = 0; I != NumVars; ++I)
12586 Vars.push_back(Elt: Record.readSubExpr());
12587 C->setReductionOps(Vars);
12588 Vars.clear();
12589 for (unsigned I = 0; I != NumVars; ++I)
12590 Vars.push_back(Elt: Record.readSubExpr());
12591 C->setTaskgroupDescriptors(Vars);
12592}
12593
12594void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12595 VisitOMPClauseWithPostUpdate(C);
12596 C->setLParenLoc(Record.readSourceLocation());
12597 C->setColonLoc(Record.readSourceLocation());
12598 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12599 C->setModifierLoc(Record.readSourceLocation());
12600 unsigned NumVars = C->varlist_size();
12601 SmallVector<Expr *, 16> Vars;
12602 Vars.reserve(N: NumVars);
12603 for (unsigned i = 0; i != NumVars; ++i)
12604 Vars.push_back(Elt: Record.readSubExpr());
12605 C->setVarRefs(Vars);
12606 Vars.clear();
12607 for (unsigned i = 0; i != NumVars; ++i)
12608 Vars.push_back(Elt: Record.readSubExpr());
12609 C->setPrivates(Vars);
12610 Vars.clear();
12611 for (unsigned i = 0; i != NumVars; ++i)
12612 Vars.push_back(Elt: Record.readSubExpr());
12613 C->setInits(Vars);
12614 Vars.clear();
12615 for (unsigned i = 0; i != NumVars; ++i)
12616 Vars.push_back(Elt: Record.readSubExpr());
12617 C->setUpdates(Vars);
12618 Vars.clear();
12619 for (unsigned i = 0; i != NumVars; ++i)
12620 Vars.push_back(Elt: Record.readSubExpr());
12621 C->setFinals(Vars);
12622 C->setStep(Record.readSubExpr());
12623 C->setCalcStep(Record.readSubExpr());
12624 Vars.clear();
12625 for (unsigned I = 0; I != NumVars + 1; ++I)
12626 Vars.push_back(Elt: Record.readSubExpr());
12627 C->setUsedExprs(Vars);
12628}
12629
12630void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12631 C->setLParenLoc(Record.readSourceLocation());
12632 C->setColonLoc(Record.readSourceLocation());
12633 unsigned NumVars = C->varlist_size();
12634 SmallVector<Expr *, 16> Vars;
12635 Vars.reserve(N: NumVars);
12636 for (unsigned i = 0; i != NumVars; ++i)
12637 Vars.push_back(Elt: Record.readSubExpr());
12638 C->setVarRefs(Vars);
12639 C->setAlignment(Record.readSubExpr());
12640}
12641
12642void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12643 C->setLParenLoc(Record.readSourceLocation());
12644 unsigned NumVars = C->varlist_size();
12645 SmallVector<Expr *, 16> Exprs;
12646 Exprs.reserve(N: NumVars);
12647 for (unsigned i = 0; i != NumVars; ++i)
12648 Exprs.push_back(Elt: Record.readSubExpr());
12649 C->setVarRefs(Exprs);
12650 Exprs.clear();
12651 for (unsigned i = 0; i != NumVars; ++i)
12652 Exprs.push_back(Elt: Record.readSubExpr());
12653 C->setSourceExprs(Exprs);
12654 Exprs.clear();
12655 for (unsigned i = 0; i != NumVars; ++i)
12656 Exprs.push_back(Elt: Record.readSubExpr());
12657 C->setDestinationExprs(Exprs);
12658 Exprs.clear();
12659 for (unsigned i = 0; i != NumVars; ++i)
12660 Exprs.push_back(Elt: Record.readSubExpr());
12661 C->setAssignmentOps(Exprs);
12662}
12663
12664void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12665 C->setLParenLoc(Record.readSourceLocation());
12666 unsigned NumVars = C->varlist_size();
12667 SmallVector<Expr *, 16> Exprs;
12668 Exprs.reserve(N: NumVars);
12669 for (unsigned i = 0; i != NumVars; ++i)
12670 Exprs.push_back(Elt: Record.readSubExpr());
12671 C->setVarRefs(Exprs);
12672 Exprs.clear();
12673 for (unsigned i = 0; i != NumVars; ++i)
12674 Exprs.push_back(Elt: Record.readSubExpr());
12675 C->setSourceExprs(Exprs);
12676 Exprs.clear();
12677 for (unsigned i = 0; i != NumVars; ++i)
12678 Exprs.push_back(Elt: Record.readSubExpr());
12679 C->setDestinationExprs(Exprs);
12680 Exprs.clear();
12681 for (unsigned i = 0; i != NumVars; ++i)
12682 Exprs.push_back(Elt: Record.readSubExpr());
12683 C->setAssignmentOps(Exprs);
12684}
12685
12686void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12687 C->setLParenLoc(Record.readSourceLocation());
12688 unsigned NumVars = C->varlist_size();
12689 SmallVector<Expr *, 16> Vars;
12690 Vars.reserve(N: NumVars);
12691 for (unsigned i = 0; i != NumVars; ++i)
12692 Vars.push_back(Elt: Record.readSubExpr());
12693 C->setVarRefs(Vars);
12694}
12695
12696void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12697 C->setDepobj(Record.readSubExpr());
12698 C->setLParenLoc(Record.readSourceLocation());
12699}
12700
12701void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12702 C->setLParenLoc(Record.readSourceLocation());
12703 C->setModifier(Record.readSubExpr());
12704 C->setDependencyKind(
12705 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12706 C->setDependencyLoc(Record.readSourceLocation());
12707 C->setColonLoc(Record.readSourceLocation());
12708 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12709 unsigned NumVars = C->varlist_size();
12710 SmallVector<Expr *, 16> Vars;
12711 Vars.reserve(N: NumVars);
12712 for (unsigned I = 0; I != NumVars; ++I)
12713 Vars.push_back(Elt: Record.readSubExpr());
12714 C->setVarRefs(Vars);
12715 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12716 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12717}
12718
12719void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12720 VisitOMPClauseWithPreInit(C);
12721 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12722 C->setDevice(Record.readSubExpr());
12723 C->setModifierLoc(Record.readSourceLocation());
12724 C->setLParenLoc(Record.readSourceLocation());
12725}
12726
12727void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12728 C->setLParenLoc(Record.readSourceLocation());
12729 bool HasIteratorModifier = false;
12730 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12731 C->setMapTypeModifier(
12732 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12733 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12734 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12735 HasIteratorModifier = true;
12736 }
12737 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12738 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12739 C->setMapType(
12740 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12741 C->setMapLoc(Record.readSourceLocation());
12742 C->setColonLoc(Record.readSourceLocation());
12743 auto NumVars = C->varlist_size();
12744 auto UniqueDecls = C->getUniqueDeclarationsNum();
12745 auto TotalLists = C->getTotalComponentListNum();
12746 auto TotalComponents = C->getTotalComponentsNum();
12747
12748 SmallVector<Expr *, 16> Vars;
12749 Vars.reserve(N: NumVars);
12750 for (unsigned i = 0; i != NumVars; ++i)
12751 Vars.push_back(Elt: Record.readExpr());
12752 C->setVarRefs(Vars);
12753
12754 SmallVector<Expr *, 16> UDMappers;
12755 UDMappers.reserve(N: NumVars);
12756 for (unsigned I = 0; I < NumVars; ++I)
12757 UDMappers.push_back(Elt: Record.readExpr());
12758 C->setUDMapperRefs(UDMappers);
12759
12760 if (HasIteratorModifier)
12761 C->setIteratorModifier(Record.readExpr());
12762
12763 SmallVector<ValueDecl *, 16> Decls;
12764 Decls.reserve(N: UniqueDecls);
12765 for (unsigned i = 0; i < UniqueDecls; ++i)
12766 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12767 C->setUniqueDecls(Decls);
12768
12769 SmallVector<unsigned, 16> ListsPerDecl;
12770 ListsPerDecl.reserve(N: UniqueDecls);
12771 for (unsigned i = 0; i < UniqueDecls; ++i)
12772 ListsPerDecl.push_back(Elt: Record.readInt());
12773 C->setDeclNumLists(ListsPerDecl);
12774
12775 SmallVector<unsigned, 32> ListSizes;
12776 ListSizes.reserve(N: TotalLists);
12777 for (unsigned i = 0; i < TotalLists; ++i)
12778 ListSizes.push_back(Elt: Record.readInt());
12779 C->setComponentListSizes(ListSizes);
12780
12781 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12782 Components.reserve(N: TotalComponents);
12783 for (unsigned i = 0; i < TotalComponents; ++i) {
12784 Expr *AssociatedExprPr = Record.readExpr();
12785 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12786 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12787 /*IsNonContiguous=*/Args: false);
12788 }
12789 C->setComponents(Components, CLSs: ListSizes);
12790}
12791
12792void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12793 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12794 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12795 C->setLParenLoc(Record.readSourceLocation());
12796 C->setColonLoc(Record.readSourceLocation());
12797 C->setAllocator(Record.readSubExpr());
12798 C->setAlignment(Record.readSubExpr());
12799 unsigned NumVars = C->varlist_size();
12800 SmallVector<Expr *, 16> Vars;
12801 Vars.reserve(N: NumVars);
12802 for (unsigned i = 0; i != NumVars; ++i)
12803 Vars.push_back(Elt: Record.readSubExpr());
12804 C->setVarRefs(Vars);
12805}
12806
12807void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12808 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12809 C->setModifierLoc(Record.readSourceLocation());
12810 C->setModifierExpr(Record.readSubExpr());
12811 VisitOMPClauseWithPreInit(C);
12812 C->setLParenLoc(Record.readSourceLocation());
12813 unsigned NumVars = C->varlist_size();
12814 SmallVector<Expr *, 16> Vars;
12815 Vars.reserve(N: NumVars);
12816 for (unsigned I = 0; I != NumVars; ++I)
12817 Vars.push_back(Elt: Record.readSubExpr());
12818 C->setVarRefs(Vars);
12819}
12820
12821void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12822 C->setModifier(Record.readEnum<OpenMPThreadLimitClauseModifier>());
12823 C->setModifierLoc(Record.readSourceLocation());
12824 C->setModifierExpr(Record.readSubExpr());
12825 VisitOMPClauseWithPreInit(C);
12826 C->setLParenLoc(Record.readSourceLocation());
12827 unsigned NumVars = C->varlist_size();
12828 SmallVector<Expr *, 16> Vars;
12829 Vars.reserve(N: NumVars);
12830 for (unsigned I = 0; I != NumVars; ++I)
12831 Vars.push_back(Elt: Record.readSubExpr());
12832 C->setVarRefs(Vars);
12833}
12834
12835void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12836 VisitOMPClauseWithPreInit(C);
12837 C->setPriority(Record.readSubExpr());
12838 C->setLParenLoc(Record.readSourceLocation());
12839}
12840
12841void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12842 VisitOMPClauseWithPreInit(C);
12843 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12844 C->setGrainsize(Record.readSubExpr());
12845 C->setModifierLoc(Record.readSourceLocation());
12846 C->setLParenLoc(Record.readSourceLocation());
12847}
12848
12849void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12850 VisitOMPClauseWithPreInit(C);
12851 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12852 C->setNumTasks(Record.readSubExpr());
12853 C->setModifierLoc(Record.readSourceLocation());
12854 C->setLParenLoc(Record.readSourceLocation());
12855}
12856
12857void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12858 C->setHint(Record.readSubExpr());
12859 C->setLParenLoc(Record.readSourceLocation());
12860}
12861
12862void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12863 VisitOMPClauseWithPreInit(C);
12864 C->setDistScheduleKind(
12865 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12866 C->setChunkSize(Record.readSubExpr());
12867 C->setLParenLoc(Record.readSourceLocation());
12868 C->setDistScheduleKindLoc(Record.readSourceLocation());
12869 C->setCommaLoc(Record.readSourceLocation());
12870}
12871
12872void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12873 C->setDefaultmapKind(
12874 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12875 C->setDefaultmapModifier(
12876 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12877 C->setLParenLoc(Record.readSourceLocation());
12878 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12879 C->setDefaultmapKindLoc(Record.readSourceLocation());
12880}
12881
12882void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12883 C->setLParenLoc(Record.readSourceLocation());
12884 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12885 C->setMotionModifier(
12886 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12887 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12888 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12889 C->setIteratorModifier(Record.readExpr());
12890 }
12891 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12892 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12893 C->setColonLoc(Record.readSourceLocation());
12894 auto NumVars = C->varlist_size();
12895 auto UniqueDecls = C->getUniqueDeclarationsNum();
12896 auto TotalLists = C->getTotalComponentListNum();
12897 auto TotalComponents = C->getTotalComponentsNum();
12898
12899 SmallVector<Expr *, 16> Vars;
12900 Vars.reserve(N: NumVars);
12901 for (unsigned i = 0; i != NumVars; ++i)
12902 Vars.push_back(Elt: Record.readSubExpr());
12903 C->setVarRefs(Vars);
12904
12905 SmallVector<Expr *, 16> UDMappers;
12906 UDMappers.reserve(N: NumVars);
12907 for (unsigned I = 0; I < NumVars; ++I)
12908 UDMappers.push_back(Elt: Record.readSubExpr());
12909 C->setUDMapperRefs(UDMappers);
12910
12911 SmallVector<ValueDecl *, 16> Decls;
12912 Decls.reserve(N: UniqueDecls);
12913 for (unsigned i = 0; i < UniqueDecls; ++i)
12914 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12915 C->setUniqueDecls(Decls);
12916
12917 SmallVector<unsigned, 16> ListsPerDecl;
12918 ListsPerDecl.reserve(N: UniqueDecls);
12919 for (unsigned i = 0; i < UniqueDecls; ++i)
12920 ListsPerDecl.push_back(Elt: Record.readInt());
12921 C->setDeclNumLists(ListsPerDecl);
12922
12923 SmallVector<unsigned, 32> ListSizes;
12924 ListSizes.reserve(N: TotalLists);
12925 for (unsigned i = 0; i < TotalLists; ++i)
12926 ListSizes.push_back(Elt: Record.readInt());
12927 C->setComponentListSizes(ListSizes);
12928
12929 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12930 Components.reserve(N: TotalComponents);
12931 for (unsigned i = 0; i < TotalComponents; ++i) {
12932 Expr *AssociatedExprPr = Record.readSubExpr();
12933 bool IsNonContiguous = Record.readBool();
12934 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12935 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12936 }
12937 C->setComponents(Components, CLSs: ListSizes);
12938}
12939
12940void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12941 C->setLParenLoc(Record.readSourceLocation());
12942 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12943 C->setMotionModifier(
12944 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12945 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12946 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12947 C->setIteratorModifier(Record.readExpr());
12948 }
12949 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12950 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12951 C->setColonLoc(Record.readSourceLocation());
12952 auto NumVars = C->varlist_size();
12953 auto UniqueDecls = C->getUniqueDeclarationsNum();
12954 auto TotalLists = C->getTotalComponentListNum();
12955 auto TotalComponents = C->getTotalComponentsNum();
12956
12957 SmallVector<Expr *, 16> Vars;
12958 Vars.reserve(N: NumVars);
12959 for (unsigned i = 0; i != NumVars; ++i)
12960 Vars.push_back(Elt: Record.readSubExpr());
12961 C->setVarRefs(Vars);
12962
12963 SmallVector<Expr *, 16> UDMappers;
12964 UDMappers.reserve(N: NumVars);
12965 for (unsigned I = 0; I < NumVars; ++I)
12966 UDMappers.push_back(Elt: Record.readSubExpr());
12967 C->setUDMapperRefs(UDMappers);
12968
12969 SmallVector<ValueDecl *, 16> Decls;
12970 Decls.reserve(N: UniqueDecls);
12971 for (unsigned i = 0; i < UniqueDecls; ++i)
12972 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12973 C->setUniqueDecls(Decls);
12974
12975 SmallVector<unsigned, 16> ListsPerDecl;
12976 ListsPerDecl.reserve(N: UniqueDecls);
12977 for (unsigned i = 0; i < UniqueDecls; ++i)
12978 ListsPerDecl.push_back(Elt: Record.readInt());
12979 C->setDeclNumLists(ListsPerDecl);
12980
12981 SmallVector<unsigned, 32> ListSizes;
12982 ListSizes.reserve(N: TotalLists);
12983 for (unsigned i = 0; i < TotalLists; ++i)
12984 ListSizes.push_back(Elt: Record.readInt());
12985 C->setComponentListSizes(ListSizes);
12986
12987 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12988 Components.reserve(N: TotalComponents);
12989 for (unsigned i = 0; i < TotalComponents; ++i) {
12990 Expr *AssociatedExprPr = Record.readSubExpr();
12991 bool IsNonContiguous = Record.readBool();
12992 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12993 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12994 }
12995 C->setComponents(Components, CLSs: ListSizes);
12996}
12997
12998void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12999 C->setLParenLoc(Record.readSourceLocation());
13000 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
13001 C->setFallbackModifierLoc(Record.readSourceLocation());
13002 auto NumVars = C->varlist_size();
13003 auto UniqueDecls = C->getUniqueDeclarationsNum();
13004 auto TotalLists = C->getTotalComponentListNum();
13005 auto TotalComponents = C->getTotalComponentsNum();
13006
13007 SmallVector<Expr *, 16> Vars;
13008 Vars.reserve(N: NumVars);
13009 for (unsigned i = 0; i != NumVars; ++i)
13010 Vars.push_back(Elt: Record.readSubExpr());
13011 C->setVarRefs(Vars);
13012 Vars.clear();
13013 for (unsigned i = 0; i != NumVars; ++i)
13014 Vars.push_back(Elt: Record.readSubExpr());
13015 C->setPrivateCopies(Vars);
13016 Vars.clear();
13017 for (unsigned i = 0; i != NumVars; ++i)
13018 Vars.push_back(Elt: Record.readSubExpr());
13019 C->setInits(Vars);
13020
13021 SmallVector<ValueDecl *, 16> Decls;
13022 Decls.reserve(N: UniqueDecls);
13023 for (unsigned i = 0; i < UniqueDecls; ++i)
13024 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13025 C->setUniqueDecls(Decls);
13026
13027 SmallVector<unsigned, 16> ListsPerDecl;
13028 ListsPerDecl.reserve(N: UniqueDecls);
13029 for (unsigned i = 0; i < UniqueDecls; ++i)
13030 ListsPerDecl.push_back(Elt: Record.readInt());
13031 C->setDeclNumLists(ListsPerDecl);
13032
13033 SmallVector<unsigned, 32> ListSizes;
13034 ListSizes.reserve(N: TotalLists);
13035 for (unsigned i = 0; i < TotalLists; ++i)
13036 ListSizes.push_back(Elt: Record.readInt());
13037 C->setComponentListSizes(ListSizes);
13038
13039 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13040 Components.reserve(N: TotalComponents);
13041 for (unsigned i = 0; i < TotalComponents; ++i) {
13042 auto *AssociatedExprPr = Record.readSubExpr();
13043 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13044 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
13045 /*IsNonContiguous=*/Args: false);
13046 }
13047 C->setComponents(Components, CLSs: ListSizes);
13048}
13049
13050void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
13051 C->setLParenLoc(Record.readSourceLocation());
13052 auto NumVars = C->varlist_size();
13053 auto UniqueDecls = C->getUniqueDeclarationsNum();
13054 auto TotalLists = C->getTotalComponentListNum();
13055 auto TotalComponents = C->getTotalComponentsNum();
13056
13057 SmallVector<Expr *, 16> Vars;
13058 Vars.reserve(N: NumVars);
13059 for (unsigned i = 0; i != NumVars; ++i)
13060 Vars.push_back(Elt: Record.readSubExpr());
13061 C->setVarRefs(Vars);
13062
13063 SmallVector<ValueDecl *, 16> Decls;
13064 Decls.reserve(N: UniqueDecls);
13065 for (unsigned i = 0; i < UniqueDecls; ++i)
13066 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13067 C->setUniqueDecls(Decls);
13068
13069 SmallVector<unsigned, 16> ListsPerDecl;
13070 ListsPerDecl.reserve(N: UniqueDecls);
13071 for (unsigned i = 0; i < UniqueDecls; ++i)
13072 ListsPerDecl.push_back(Elt: Record.readInt());
13073 C->setDeclNumLists(ListsPerDecl);
13074
13075 SmallVector<unsigned, 32> ListSizes;
13076 ListSizes.reserve(N: TotalLists);
13077 for (unsigned i = 0; i < TotalLists; ++i)
13078 ListSizes.push_back(Elt: Record.readInt());
13079 C->setComponentListSizes(ListSizes);
13080
13081 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13082 Components.reserve(N: TotalComponents);
13083 for (unsigned i = 0; i < TotalComponents; ++i) {
13084 Expr *AssociatedExpr = Record.readSubExpr();
13085 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13086 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13087 /*IsNonContiguous*/ Args: false);
13088 }
13089 C->setComponents(Components, CLSs: ListSizes);
13090}
13091
13092void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
13093 C->setLParenLoc(Record.readSourceLocation());
13094 auto NumVars = C->varlist_size();
13095 auto UniqueDecls = C->getUniqueDeclarationsNum();
13096 auto TotalLists = C->getTotalComponentListNum();
13097 auto TotalComponents = C->getTotalComponentsNum();
13098
13099 SmallVector<Expr *, 16> Vars;
13100 Vars.reserve(N: NumVars);
13101 for (unsigned i = 0; i != NumVars; ++i)
13102 Vars.push_back(Elt: Record.readSubExpr());
13103 C->setVarRefs(Vars);
13104 Vars.clear();
13105
13106 SmallVector<ValueDecl *, 16> Decls;
13107 Decls.reserve(N: UniqueDecls);
13108 for (unsigned i = 0; i < UniqueDecls; ++i)
13109 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13110 C->setUniqueDecls(Decls);
13111
13112 SmallVector<unsigned, 16> ListsPerDecl;
13113 ListsPerDecl.reserve(N: UniqueDecls);
13114 for (unsigned i = 0; i < UniqueDecls; ++i)
13115 ListsPerDecl.push_back(Elt: Record.readInt());
13116 C->setDeclNumLists(ListsPerDecl);
13117
13118 SmallVector<unsigned, 32> ListSizes;
13119 ListSizes.reserve(N: TotalLists);
13120 for (unsigned i = 0; i < TotalLists; ++i)
13121 ListSizes.push_back(Elt: Record.readInt());
13122 C->setComponentListSizes(ListSizes);
13123
13124 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13125 Components.reserve(N: TotalComponents);
13126 for (unsigned i = 0; i < TotalComponents; ++i) {
13127 Expr *AssociatedExpr = Record.readSubExpr();
13128 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13129 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13130 /*IsNonContiguous=*/Args: false);
13131 }
13132 C->setComponents(Components, CLSs: ListSizes);
13133}
13134
13135void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
13136 C->setLParenLoc(Record.readSourceLocation());
13137 auto NumVars = C->varlist_size();
13138 auto UniqueDecls = C->getUniqueDeclarationsNum();
13139 auto TotalLists = C->getTotalComponentListNum();
13140 auto TotalComponents = C->getTotalComponentsNum();
13141
13142 SmallVector<Expr *, 16> Vars;
13143 Vars.reserve(N: NumVars);
13144 for (unsigned I = 0; I != NumVars; ++I)
13145 Vars.push_back(Elt: Record.readSubExpr());
13146 C->setVarRefs(Vars);
13147 Vars.clear();
13148
13149 SmallVector<ValueDecl *, 16> Decls;
13150 Decls.reserve(N: UniqueDecls);
13151 for (unsigned I = 0; I < UniqueDecls; ++I)
13152 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
13153 C->setUniqueDecls(Decls);
13154
13155 SmallVector<unsigned, 16> ListsPerDecl;
13156 ListsPerDecl.reserve(N: UniqueDecls);
13157 for (unsigned I = 0; I < UniqueDecls; ++I)
13158 ListsPerDecl.push_back(Elt: Record.readInt());
13159 C->setDeclNumLists(ListsPerDecl);
13160
13161 SmallVector<unsigned, 32> ListSizes;
13162 ListSizes.reserve(N: TotalLists);
13163 for (unsigned i = 0; i < TotalLists; ++i)
13164 ListSizes.push_back(Elt: Record.readInt());
13165 C->setComponentListSizes(ListSizes);
13166
13167 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
13168 Components.reserve(N: TotalComponents);
13169 for (unsigned I = 0; I < TotalComponents; ++I) {
13170 Expr *AssociatedExpr = Record.readSubExpr();
13171 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
13172 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
13173 /*IsNonContiguous=*/Args: false);
13174 }
13175 C->setComponents(Components, CLSs: ListSizes);
13176}
13177
13178void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
13179 C->setLParenLoc(Record.readSourceLocation());
13180 unsigned NumVars = C->varlist_size();
13181 SmallVector<Expr *, 16> Vars;
13182 Vars.reserve(N: NumVars);
13183 for (unsigned i = 0; i != NumVars; ++i)
13184 Vars.push_back(Elt: Record.readSubExpr());
13185 C->setVarRefs(Vars);
13186 Vars.clear();
13187 Vars.reserve(N: NumVars);
13188 for (unsigned i = 0; i != NumVars; ++i)
13189 Vars.push_back(Elt: Record.readSubExpr());
13190 C->setPrivateRefs(Vars);
13191}
13192
13193void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
13194 C->setLParenLoc(Record.readSourceLocation());
13195 unsigned NumVars = C->varlist_size();
13196 SmallVector<Expr *, 16> Vars;
13197 Vars.reserve(N: NumVars);
13198 for (unsigned i = 0; i != NumVars; ++i)
13199 Vars.push_back(Elt: Record.readSubExpr());
13200 C->setVarRefs(Vars);
13201}
13202
13203void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *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::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13214 C->setLParenLoc(Record.readSourceLocation());
13215 unsigned NumOfAllocators = C->getNumberOfAllocators();
13216 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13217 Data.reserve(N: NumOfAllocators);
13218 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13219 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13220 D.Allocator = Record.readSubExpr();
13221 D.AllocatorTraits = Record.readSubExpr();
13222 D.LParenLoc = Record.readSourceLocation();
13223 D.RParenLoc = Record.readSourceLocation();
13224 }
13225 C->setAllocatorsData(Data);
13226}
13227
13228void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13229 C->setLParenLoc(Record.readSourceLocation());
13230 C->setModifier(Record.readSubExpr());
13231 C->setColonLoc(Record.readSourceLocation());
13232 unsigned NumOfLocators = C->varlist_size();
13233 SmallVector<Expr *, 4> Locators;
13234 Locators.reserve(N: NumOfLocators);
13235 for (unsigned I = 0; I != NumOfLocators; ++I)
13236 Locators.push_back(Elt: Record.readSubExpr());
13237 C->setVarRefs(Locators);
13238}
13239
13240void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13241 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13242 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13243 C->setLParenLoc(Record.readSourceLocation());
13244 C->setKindKwLoc(Record.readSourceLocation());
13245 C->setModifierKwLoc(Record.readSourceLocation());
13246}
13247
13248void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13249 VisitOMPClauseWithPreInit(C);
13250 C->setThreadID(Record.readSubExpr());
13251 C->setLParenLoc(Record.readSourceLocation());
13252}
13253
13254void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13255 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13256 C->setLParenLoc(Record.readSourceLocation());
13257 C->setBindKindLoc(Record.readSourceLocation());
13258}
13259
13260void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13261 C->setAlignment(Record.readExpr());
13262 C->setLParenLoc(Record.readSourceLocation());
13263}
13264
13265void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13266 VisitOMPClauseWithPreInit(C);
13267 C->setSize(Record.readSubExpr());
13268 C->setLParenLoc(Record.readSourceLocation());
13269}
13270
13271void OMPClauseReader::VisitOMPDynGroupprivateClause(
13272 OMPDynGroupprivateClause *C) {
13273 VisitOMPClauseWithPreInit(C);
13274 C->setDynGroupprivateModifier(
13275 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13276 C->setDynGroupprivateFallbackModifier(
13277 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13278 C->setSize(Record.readSubExpr());
13279 C->setLParenLoc(Record.readSourceLocation());
13280 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13281 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13282}
13283
13284void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13285 C->setLParenLoc(Record.readSourceLocation());
13286 C->setDependenceType(
13287 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13288 C->setDependenceLoc(Record.readSourceLocation());
13289 C->setColonLoc(Record.readSourceLocation());
13290 unsigned NumVars = C->varlist_size();
13291 SmallVector<Expr *, 16> Vars;
13292 Vars.reserve(N: NumVars);
13293 for (unsigned I = 0; I != NumVars; ++I)
13294 Vars.push_back(Elt: Record.readSubExpr());
13295 C->setVarRefs(Vars);
13296 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13297 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13298}
13299
13300void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13301 AttrVec Attrs;
13302 Record.readAttributes(Attrs);
13303 C->setAttrs(Attrs);
13304 C->setLocStart(Record.readSourceLocation());
13305 C->setLParenLoc(Record.readSourceLocation());
13306 C->setLocEnd(Record.readSourceLocation());
13307}
13308
13309void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13310
13311OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13312 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13313 TI.Sets.resize(N: readUInt32());
13314 for (auto &Set : TI.Sets) {
13315 Set.Kind = readEnum<llvm::omp::TraitSet>();
13316 Set.Selectors.resize(N: readUInt32());
13317 for (auto &Selector : Set.Selectors) {
13318 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13319 Selector.ScoreOrCondition = nullptr;
13320 if (readBool())
13321 Selector.ScoreOrCondition = readExprRef();
13322 Selector.Properties.resize(N: readUInt32());
13323 for (auto &Property : Selector.Properties)
13324 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13325 }
13326 }
13327 return &TI;
13328}
13329
13330void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13331 if (!Data)
13332 return;
13333 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13334 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13335 skipInts(N: 3);
13336 }
13337 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13338 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13339 Clauses[I] = readOMPClause();
13340 Data->setClauses(Clauses);
13341 if (Data->hasAssociatedStmt())
13342 Data->setAssociatedStmt(readStmt());
13343 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13344 Data->getChildren()[I] = readStmt();
13345}
13346
13347SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13348 unsigned NumVars = readInt();
13349 llvm::SmallVector<Expr *> VarList;
13350 for (unsigned I = 0; I < NumVars; ++I)
13351 VarList.push_back(Elt: readExpr());
13352 return VarList;
13353}
13354
13355SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13356 unsigned NumExprs = readInt();
13357 llvm::SmallVector<Expr *> ExprList;
13358 for (unsigned I = 0; I < NumExprs; ++I)
13359 ExprList.push_back(Elt: readSubExpr());
13360 return ExprList;
13361}
13362
13363OpenACCClause *ASTRecordReader::readOpenACCClause() {
13364 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13365 SourceLocation BeginLoc = readSourceLocation();
13366 SourceLocation EndLoc = readSourceLocation();
13367
13368 switch (ClauseKind) {
13369 case OpenACCClauseKind::Default: {
13370 SourceLocation LParenLoc = readSourceLocation();
13371 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13372 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13373 EndLoc);
13374 }
13375 case OpenACCClauseKind::If: {
13376 SourceLocation LParenLoc = readSourceLocation();
13377 Expr *CondExpr = readSubExpr();
13378 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13379 EndLoc);
13380 }
13381 case OpenACCClauseKind::Self: {
13382 SourceLocation LParenLoc = readSourceLocation();
13383 bool isConditionExprClause = readBool();
13384 if (isConditionExprClause) {
13385 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13386 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13387 ConditionExpr: CondExpr, EndLoc);
13388 }
13389 unsigned NumVars = readInt();
13390 llvm::SmallVector<Expr *> VarList;
13391 for (unsigned I = 0; I < NumVars; ++I)
13392 VarList.push_back(Elt: readSubExpr());
13393 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13394 EndLoc);
13395 }
13396 case OpenACCClauseKind::NumGangs: {
13397 SourceLocation LParenLoc = readSourceLocation();
13398 unsigned NumClauses = readInt();
13399 llvm::SmallVector<Expr *> IntExprs;
13400 for (unsigned I = 0; I < NumClauses; ++I)
13401 IntExprs.push_back(Elt: readSubExpr());
13402 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13403 IntExprs, EndLoc);
13404 }
13405 case OpenACCClauseKind::NumWorkers: {
13406 SourceLocation LParenLoc = readSourceLocation();
13407 Expr *IntExpr = readSubExpr();
13408 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13409 IntExpr, EndLoc);
13410 }
13411 case OpenACCClauseKind::DeviceNum: {
13412 SourceLocation LParenLoc = readSourceLocation();
13413 Expr *IntExpr = readSubExpr();
13414 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13415 IntExpr, EndLoc);
13416 }
13417 case OpenACCClauseKind::DefaultAsync: {
13418 SourceLocation LParenLoc = readSourceLocation();
13419 Expr *IntExpr = readSubExpr();
13420 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13421 IntExpr, EndLoc);
13422 }
13423 case OpenACCClauseKind::VectorLength: {
13424 SourceLocation LParenLoc = readSourceLocation();
13425 Expr *IntExpr = readSubExpr();
13426 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13427 IntExpr, EndLoc);
13428 }
13429 case OpenACCClauseKind::Private: {
13430 SourceLocation LParenLoc = readSourceLocation();
13431 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13432
13433 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13434 for (unsigned I = 0; I < VarList.size(); ++I) {
13435 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13436 VarDecl *Alloca = readDeclAs<VarDecl>();
13437 RecipeList.push_back(Elt: {Alloca});
13438 }
13439
13440 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13441 VarList, InitRecipes: RecipeList, EndLoc);
13442 }
13443 case OpenACCClauseKind::Host: {
13444 SourceLocation LParenLoc = readSourceLocation();
13445 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13446 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13447 EndLoc);
13448 }
13449 case OpenACCClauseKind::Device: {
13450 SourceLocation LParenLoc = readSourceLocation();
13451 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13452 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13453 VarList, EndLoc);
13454 }
13455 case OpenACCClauseKind::FirstPrivate: {
13456 SourceLocation LParenLoc = readSourceLocation();
13457 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13458 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13459 for (unsigned I = 0; I < VarList.size(); ++I) {
13460 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13461 VarDecl *Recipe = readDeclAs<VarDecl>();
13462 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13463 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13464 }
13465
13466 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13467 VarList, InitRecipes: RecipeList, EndLoc);
13468 }
13469 case OpenACCClauseKind::Attach: {
13470 SourceLocation LParenLoc = readSourceLocation();
13471 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13472 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13473 VarList, EndLoc);
13474 }
13475 case OpenACCClauseKind::Detach: {
13476 SourceLocation LParenLoc = readSourceLocation();
13477 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13478 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13479 VarList, EndLoc);
13480 }
13481 case OpenACCClauseKind::Delete: {
13482 SourceLocation LParenLoc = readSourceLocation();
13483 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13484 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13485 VarList, EndLoc);
13486 }
13487 case OpenACCClauseKind::UseDevice: {
13488 SourceLocation LParenLoc = readSourceLocation();
13489 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13490 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13491 VarList, EndLoc);
13492 }
13493 case OpenACCClauseKind::DevicePtr: {
13494 SourceLocation LParenLoc = readSourceLocation();
13495 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13496 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13497 VarList, EndLoc);
13498 }
13499 case OpenACCClauseKind::NoCreate: {
13500 SourceLocation LParenLoc = readSourceLocation();
13501 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13502 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13503 VarList, EndLoc);
13504 }
13505 case OpenACCClauseKind::Present: {
13506 SourceLocation LParenLoc = readSourceLocation();
13507 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13508 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13509 VarList, EndLoc);
13510 }
13511 case OpenACCClauseKind::PCopy:
13512 case OpenACCClauseKind::PresentOrCopy:
13513 case OpenACCClauseKind::Copy: {
13514 SourceLocation LParenLoc = readSourceLocation();
13515 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13516 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13517 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13518 LParenLoc, Mods: ModList, VarList, EndLoc);
13519 }
13520 case OpenACCClauseKind::CopyIn:
13521 case OpenACCClauseKind::PCopyIn:
13522 case OpenACCClauseKind::PresentOrCopyIn: {
13523 SourceLocation LParenLoc = readSourceLocation();
13524 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13525 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13526 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13527 LParenLoc, Mods: ModList, VarList, EndLoc);
13528 }
13529 case OpenACCClauseKind::CopyOut:
13530 case OpenACCClauseKind::PCopyOut:
13531 case OpenACCClauseKind::PresentOrCopyOut: {
13532 SourceLocation LParenLoc = readSourceLocation();
13533 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13534 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13535 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13536 LParenLoc, Mods: ModList, VarList, EndLoc);
13537 }
13538 case OpenACCClauseKind::Create:
13539 case OpenACCClauseKind::PCreate:
13540 case OpenACCClauseKind::PresentOrCreate: {
13541 SourceLocation LParenLoc = readSourceLocation();
13542 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13543 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13544 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13545 LParenLoc, Mods: ModList, VarList, EndLoc);
13546 }
13547 case OpenACCClauseKind::Async: {
13548 SourceLocation LParenLoc = readSourceLocation();
13549 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13550 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13551 IntExpr: AsyncExpr, EndLoc);
13552 }
13553 case OpenACCClauseKind::Wait: {
13554 SourceLocation LParenLoc = readSourceLocation();
13555 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13556 SourceLocation QueuesLoc = readSourceLocation();
13557 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13558 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13559 DevNumExpr, QueuesLoc, QueueIdExprs,
13560 EndLoc);
13561 }
13562 case OpenACCClauseKind::DeviceType:
13563 case OpenACCClauseKind::DType: {
13564 SourceLocation LParenLoc = readSourceLocation();
13565 llvm::SmallVector<DeviceTypeArgument> Archs;
13566 unsigned NumArchs = readInt();
13567
13568 for (unsigned I = 0; I < NumArchs; ++I) {
13569 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13570 SourceLocation Loc = readSourceLocation();
13571 Archs.emplace_back(Args&: Loc, Args&: Ident);
13572 }
13573
13574 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13575 LParenLoc, Archs, EndLoc);
13576 }
13577 case OpenACCClauseKind::Reduction: {
13578 SourceLocation LParenLoc = readSourceLocation();
13579 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13580 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13581 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13582
13583 for (unsigned I = 0; I < VarList.size(); ++I) {
13584 VarDecl *Recipe = readDeclAs<VarDecl>();
13585
13586 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13587 3 * sizeof(int *));
13588
13589 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13590 unsigned NumCombiners = readInt();
13591 for (unsigned I = 0; I < NumCombiners; ++I) {
13592 VarDecl *LHS = readDeclAs<VarDecl>();
13593 VarDecl *RHS = readDeclAs<VarDecl>();
13594 Expr *Op = readExpr();
13595
13596 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13597 }
13598
13599 RecipeList.push_back(Elt: {Recipe, Combiners});
13600 }
13601
13602 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13603 VarList, Recipes: RecipeList, EndLoc);
13604 }
13605 case OpenACCClauseKind::Seq:
13606 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13607 case OpenACCClauseKind::NoHost:
13608 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13609 case OpenACCClauseKind::Finalize:
13610 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13611 case OpenACCClauseKind::IfPresent:
13612 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13613 case OpenACCClauseKind::Independent:
13614 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13615 case OpenACCClauseKind::Auto:
13616 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13617 case OpenACCClauseKind::Collapse: {
13618 SourceLocation LParenLoc = readSourceLocation();
13619 bool HasForce = readBool();
13620 Expr *LoopCount = readSubExpr();
13621 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13622 HasForce, LoopCount, EndLoc);
13623 }
13624 case OpenACCClauseKind::Tile: {
13625 SourceLocation LParenLoc = readSourceLocation();
13626 unsigned NumClauses = readInt();
13627 llvm::SmallVector<Expr *> SizeExprs;
13628 for (unsigned I = 0; I < NumClauses; ++I)
13629 SizeExprs.push_back(Elt: readSubExpr());
13630 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13631 SizeExprs, EndLoc);
13632 }
13633 case OpenACCClauseKind::Gang: {
13634 SourceLocation LParenLoc = readSourceLocation();
13635 unsigned NumExprs = readInt();
13636 llvm::SmallVector<OpenACCGangKind> GangKinds;
13637 llvm::SmallVector<Expr *> Exprs;
13638 for (unsigned I = 0; I < NumExprs; ++I) {
13639 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13640 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13641 Exprs.push_back(Elt: readExpr());
13642 }
13643 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13644 GangKinds, IntExprs: Exprs, EndLoc);
13645 }
13646 case OpenACCClauseKind::Worker: {
13647 SourceLocation LParenLoc = readSourceLocation();
13648 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13649 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13650 IntExpr: WorkerExpr, EndLoc);
13651 }
13652 case OpenACCClauseKind::Vector: {
13653 SourceLocation LParenLoc = readSourceLocation();
13654 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13655 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13656 IntExpr: VectorExpr, EndLoc);
13657 }
13658 case OpenACCClauseKind::Link: {
13659 SourceLocation LParenLoc = readSourceLocation();
13660 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13661 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13662 EndLoc);
13663 }
13664 case OpenACCClauseKind::DeviceResident: {
13665 SourceLocation LParenLoc = readSourceLocation();
13666 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13667 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13668 LParenLoc, VarList, EndLoc);
13669 }
13670
13671 case OpenACCClauseKind::Bind: {
13672 SourceLocation LParenLoc = readSourceLocation();
13673 bool IsString = readBool();
13674 if (IsString)
13675 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13676 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13677 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13678 ID: readIdentifier(), EndLoc);
13679 }
13680 case OpenACCClauseKind::Shortloop:
13681 case OpenACCClauseKind::Invalid:
13682 llvm_unreachable("Clause serialization not yet implemented");
13683 }
13684 llvm_unreachable("Invalid Clause Kind");
13685}
13686
13687void ASTRecordReader::readOpenACCClauseList(
13688 MutableArrayRef<const OpenACCClause *> Clauses) {
13689 for (unsigned I = 0; I < Clauses.size(); ++I)
13690 Clauses[I] = readOpenACCClause();
13691}
13692
13693void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13694 unsigned NumVars = readInt();
13695 A->Clauses.resize(N: NumVars);
13696 readOpenACCClauseList(Clauses: A->Clauses);
13697}
13698
13699static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13700 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13701 // module name?
13702 llvm::FoldingSetNodeID ID;
13703 ID.AddString(String: PrimaryModuleName);
13704 return ID.computeStableHash();
13705}
13706
13707UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13708 if (!M)
13709 return std::nullopt;
13710
13711 if (M->isHeaderLikeModule())
13712 return std::nullopt;
13713
13714 if (M->isGlobalModule())
13715 return std::nullopt;
13716
13717 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13718 return getStableHashForModuleName(PrimaryModuleName);
13719}
13720