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/DeclTemplate.h"
30#include "clang/AST/DeclarationName.h"
31#include "clang/AST/Expr.h"
32#include "clang/AST/ExprCXX.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/NestedNameSpecifier.h"
35#include "clang/AST/ODRDiagsEmitter.h"
36#include "clang/AST/OpenACCClause.h"
37#include "clang/AST/OpenMPClause.h"
38#include "clang/AST/RawCommentList.h"
39#include "clang/AST/TemplateBase.h"
40#include "clang/AST/TemplateName.h"
41#include "clang/AST/Type.h"
42#include "clang/AST/TypeLoc.h"
43#include "clang/AST/TypeLocVisitor.h"
44#include "clang/AST/UnresolvedSet.h"
45#include "clang/Basic/ASTSourceDescriptor.h"
46#include "clang/Basic/CommentOptions.h"
47#include "clang/Basic/Diagnostic.h"
48#include "clang/Basic/DiagnosticIDs.h"
49#include "clang/Basic/DiagnosticOptions.h"
50#include "clang/Basic/DiagnosticSema.h"
51#include "clang/Basic/FileManager.h"
52#include "clang/Basic/FileSystemOptions.h"
53#include "clang/Basic/IdentifierTable.h"
54#include "clang/Basic/LLVM.h"
55#include "clang/Basic/LangOptions.h"
56#include "clang/Basic/Module.h"
57#include "clang/Basic/ObjCRuntime.h"
58#include "clang/Basic/OpenACCKinds.h"
59#include "clang/Basic/OpenMPKinds.h"
60#include "clang/Basic/OperatorKinds.h"
61#include "clang/Basic/PragmaKinds.h"
62#include "clang/Basic/Sanitizers.h"
63#include "clang/Basic/SourceLocation.h"
64#include "clang/Basic/SourceManager.h"
65#include "clang/Basic/SourceManagerInternals.h"
66#include "clang/Basic/Specifiers.h"
67#include "clang/Basic/TargetInfo.h"
68#include "clang/Basic/TargetOptions.h"
69#include "clang/Basic/TokenKinds.h"
70#include "clang/Basic/Version.h"
71#include "clang/Lex/HeaderSearch.h"
72#include "clang/Lex/HeaderSearchOptions.h"
73#include "clang/Lex/MacroInfo.h"
74#include "clang/Lex/ModuleMap.h"
75#include "clang/Lex/PreprocessingRecord.h"
76#include "clang/Lex/Preprocessor.h"
77#include "clang/Lex/PreprocessorOptions.h"
78#include "clang/Lex/Token.h"
79#include "clang/Sema/ObjCMethodList.h"
80#include "clang/Sema/Scope.h"
81#include "clang/Sema/Sema.h"
82#include "clang/Sema/SemaCUDA.h"
83#include "clang/Sema/SemaObjC.h"
84#include "clang/Sema/Weak.h"
85#include "clang/Serialization/ASTBitCodes.h"
86#include "clang/Serialization/ASTDeserializationListener.h"
87#include "clang/Serialization/ASTRecordReader.h"
88#include "clang/Serialization/ContinuousRangeMap.h"
89#include "clang/Serialization/GlobalModuleIndex.h"
90#include "clang/Serialization/InMemoryModuleCache.h"
91#include "clang/Serialization/ModuleCache.h"
92#include "clang/Serialization/ModuleFile.h"
93#include "clang/Serialization/ModuleFileExtension.h"
94#include "clang/Serialization/ModuleManager.h"
95#include "clang/Serialization/PCHContainerOperations.h"
96#include "clang/Serialization/SerializationDiagnostic.h"
97#include "llvm/ADT/APFloat.h"
98#include "llvm/ADT/APInt.h"
99#include "llvm/ADT/ArrayRef.h"
100#include "llvm/ADT/DenseMap.h"
101#include "llvm/ADT/FoldingSet.h"
102#include "llvm/ADT/IntrusiveRefCntPtr.h"
103#include "llvm/ADT/STLExtras.h"
104#include "llvm/ADT/ScopeExit.h"
105#include "llvm/ADT/Sequence.h"
106#include "llvm/ADT/SmallPtrSet.h"
107#include "llvm/ADT/SmallVector.h"
108#include "llvm/ADT/StringExtras.h"
109#include "llvm/ADT/StringMap.h"
110#include "llvm/ADT/StringRef.h"
111#include "llvm/ADT/iterator_range.h"
112#include "llvm/Bitstream/BitstreamReader.h"
113#include "llvm/Support/Compiler.h"
114#include "llvm/Support/Compression.h"
115#include "llvm/Support/DJB.h"
116#include "llvm/Support/Endian.h"
117#include "llvm/Support/Error.h"
118#include "llvm/Support/ErrorHandling.h"
119#include "llvm/Support/LEB128.h"
120#include "llvm/Support/MemoryBuffer.h"
121#include "llvm/Support/Path.h"
122#include "llvm/Support/SaveAndRestore.h"
123#include "llvm/Support/TimeProfiler.h"
124#include "llvm/Support/Timer.h"
125#include "llvm/Support/VersionTuple.h"
126#include "llvm/Support/raw_ostream.h"
127#include "llvm/TargetParser/Triple.h"
128#include <algorithm>
129#include <cassert>
130#include <cstddef>
131#include <cstdint>
132#include <cstdio>
133#include <ctime>
134#include <iterator>
135#include <limits>
136#include <map>
137#include <memory>
138#include <optional>
139#include <string>
140#include <system_error>
141#include <tuple>
142#include <utility>
143#include <vector>
144
145using namespace clang;
146using namespace clang::serialization;
147using namespace clang::serialization::reader;
148using llvm::BitstreamCursor;
149
150//===----------------------------------------------------------------------===//
151// ChainedASTReaderListener implementation
152//===----------------------------------------------------------------------===//
153
154bool
155ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
156 return First->ReadFullVersionInformation(FullVersion) ||
157 Second->ReadFullVersionInformation(FullVersion);
158}
159
160void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
161 First->ReadModuleName(ModuleName);
162 Second->ReadModuleName(ModuleName);
163}
164
165void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
166 First->ReadModuleMapFile(ModuleMapPath);
167 Second->ReadModuleMapFile(ModuleMapPath);
168}
169
170bool ChainedASTReaderListener::ReadLanguageOptions(
171 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
176 AllowCompatibleDifferences);
177}
178
179bool ChainedASTReaderListener::ReadCodeGenOptions(
180 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
181 bool AllowCompatibleDifferences) {
182 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
183 AllowCompatibleDifferences) ||
184 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
185 AllowCompatibleDifferences);
186}
187
188bool ChainedASTReaderListener::ReadTargetOptions(
189 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
190 bool AllowCompatibleDifferences) {
191 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
192 AllowCompatibleDifferences) ||
193 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
194 AllowCompatibleDifferences);
195}
196
197bool ChainedASTReaderListener::ReadDiagnosticOptions(
198 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
199 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
200 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
201}
202
203bool
204ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
205 bool Complain) {
206 return First->ReadFileSystemOptions(FSOpts, Complain) ||
207 Second->ReadFileSystemOptions(FSOpts, Complain);
208}
209
210bool ChainedASTReaderListener::ReadHeaderSearchOptions(
211 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
212 StringRef ContextHash, bool Complain) {
213 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
214 Complain) ||
215 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
216 Complain);
217}
218
219bool ChainedASTReaderListener::ReadPreprocessorOptions(
220 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
221 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
222 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
223 Complain, SuggestedPredefines) ||
224 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
225 Complain, SuggestedPredefines);
226}
227
228void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
229 uint32_t Value) {
230 First->ReadCounter(M, Value);
231 Second->ReadCounter(M, Value);
232}
233
234bool ChainedASTReaderListener::needsInputFileVisitation() {
235 return First->needsInputFileVisitation() ||
236 Second->needsInputFileVisitation();
237}
238
239bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
240 return First->needsSystemInputFileVisitation() ||
241 Second->needsSystemInputFileVisitation();
242}
243
244void ChainedASTReaderListener::visitModuleFile(ModuleFileName Filename,
245 ModuleKind Kind,
246 bool DirectlyImported) {
247 First->visitModuleFile(Filename, Kind, DirectlyImported);
248 Second->visitModuleFile(Filename, Kind, DirectlyImported);
249}
250
251bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
252 bool isSystem,
253 bool isOverridden,
254 bool isExplicitModule) {
255 bool Continue = false;
256 if (First->needsInputFileVisitation() &&
257 (!isSystem || First->needsSystemInputFileVisitation()))
258 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
259 isExplicitModule);
260 if (Second->needsInputFileVisitation() &&
261 (!isSystem || Second->needsSystemInputFileVisitation()))
262 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
263 isExplicitModule);
264 return Continue;
265}
266
267void ChainedASTReaderListener::readModuleFileExtension(
268 const ModuleFileExtensionMetadata &Metadata) {
269 First->readModuleFileExtension(Metadata);
270 Second->readModuleFileExtension(Metadata);
271}
272
273//===----------------------------------------------------------------------===//
274// PCH validator implementation
275//===----------------------------------------------------------------------===//
276
277ASTReaderListener::~ASTReaderListener() = default;
278
279static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionFlagMismatch(
280 DiagnosticsEngine *Diags, StringRef Description, bool SerializedValue,
281 bool CurrentValue, StringRef ModuleFilename) {
282 if (!Diags)
283 return true;
284 return Diags->Report(DiagID: diag::err_ast_file_langopt_mismatch)
285 << Description << SerializedValue << CurrentValue << ModuleFilename;
286}
287
288static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionValueMismatch(
289 DiagnosticsEngine *Diags, StringRef Description, StringRef ModuleFilename) {
290 if (!Diags)
291 return true;
292 return Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
293 << Description << ModuleFilename;
294}
295
296/// Compare the given set of language options against an existing set of
297/// language options.
298///
299/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
300/// \param AllowCompatibleDifferences If true, differences between compatible
301/// language options will be permitted.
302///
303/// \returns true if the languagae options mis-match, false otherwise.
304static bool checkLanguageOptions(const LangOptions &LangOpts,
305 const LangOptions &ExistingLangOpts,
306 StringRef ModuleFilename,
307 DiagnosticsEngine *Diags,
308 bool AllowCompatibleDifferences = true) {
309 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
310 using CK = LangOptions::CompatibilityKind;
311
312#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
313 if constexpr (CK::Compatibility != CK::Benign) { \
314 if ((CK::Compatibility == CK::NotCompatible) || \
315 (CK::Compatibility == CK::Compatible && \
316 !AllowCompatibleDifferences)) { \
317 if (ExistingLangOpts.Name != LangOpts.Name) { \
318 if (Bits == 1) \
319 return diagnoseLanguageOptionFlagMismatch( \
320 Diags, Description, LangOpts.Name, ExistingLangOpts.Name, \
321 ModuleFilename); \
322 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
323 ModuleFilename); \
324 } \
325 } \
326 }
327
328#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
329 if constexpr (CK::Compatibility != CK::Benign) { \
330 if ((CK::Compatibility == CK::NotCompatible) || \
331 (CK::Compatibility == CK::Compatible && \
332 !AllowCompatibleDifferences)) { \
333 if (ExistingLangOpts.Name != LangOpts.Name) { \
334 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
335 ModuleFilename); \
336 } \
337 } \
338 }
339
340#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
341 if constexpr (CK::Compatibility != CK::Benign) { \
342 if ((CK::Compatibility == CK::NotCompatible) || \
343 (CK::Compatibility == CK::Compatible && \
344 !AllowCompatibleDifferences)) { \
345 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
346 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
347 ModuleFilename); \
348 } \
349 } \
350 }
351
352#include "clang/Basic/LangOptions.def"
353
354 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
355 return diagnoseLanguageOptionValueMismatch(Diags, Description: "module features",
356 ModuleFilename);
357 }
358
359 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
360 return diagnoseLanguageOptionValueMismatch(
361 Diags, Description: "target Objective-C runtime", ModuleFilename);
362 }
363
364 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
365 LangOpts.CommentOpts.BlockCommandNames) {
366 return diagnoseLanguageOptionValueMismatch(Diags, Description: "block command names",
367 ModuleFilename);
368 }
369
370 // Sanitizer feature mismatches are treated as compatible differences. If
371 // compatible differences aren't allowed, we still only want to check for
372 // mismatches of non-modular sanitizers (the only ones which can affect AST
373 // generation).
374 if (!AllowCompatibleDifferences) {
375 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
376 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
377 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
378 ExistingSanitizers.clear(K: ModularSanitizers);
379 ImportedSanitizers.clear(K: ModularSanitizers);
380 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
381 const std::string Flag = "-fsanitize=";
382 if (Diags) {
383#define SANITIZER(NAME, ID) \
384 { \
385 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
386 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
387 if (InExistingModule != InImportedModule) \
388 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
389 << InExistingModule << ModuleFilename << (Flag + NAME); \
390 }
391#include "clang/Basic/Sanitizers.def"
392 }
393 return true;
394 }
395 }
396
397 return false;
398}
399
400static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
401 const CodeGenOptions &ExistingCGOpts,
402 StringRef ModuleFilename,
403 DiagnosticsEngine *Diags,
404 bool AllowCompatibleDifferences = true) {
405 // FIXME: Specify and print a description for each option instead of the name.
406 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
407 using CK = CodeGenOptions::CompatibilityKind;
408#define CODEGENOPT(Name, Bits, Default, Compatibility) \
409 if constexpr (CK::Compatibility != CK::Benign) { \
410 if ((CK::Compatibility == CK::NotCompatible) || \
411 (CK::Compatibility == CK::Compatible && \
412 !AllowCompatibleDifferences)) { \
413 if (ExistingCGOpts.Name != CGOpts.Name) { \
414 if (Diags) { \
415 if (Bits == 1) \
416 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
417 << #Name << CGOpts.Name << ExistingCGOpts.Name \
418 << ModuleFilename; \
419 else \
420 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
421 << #Name << ModuleFilename; \
422 } \
423 return true; \
424 } \
425 } \
426 }
427
428#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
429 if constexpr (CK::Compatibility != CK::Benign) { \
430 if ((CK::Compatibility == CK::NotCompatible) || \
431 (CK::Compatibility == CK::Compatible && \
432 !AllowCompatibleDifferences)) { \
433 if (ExistingCGOpts.Name != CGOpts.Name) { \
434 if (Diags) \
435 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
436 << #Name << ModuleFilename; \
437 return true; \
438 } \
439 } \
440 }
441#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
442 if constexpr (CK::Compatibility != CK::Benign) { \
443 if ((CK::Compatibility == CK::NotCompatible) || \
444 (CK::Compatibility == CK::Compatible && \
445 !AllowCompatibleDifferences)) { \
446 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
447 if (Diags) \
448 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
449 << #Name << ModuleFilename; \
450 return true; \
451 } \
452 } \
453 }
454#define DEBUGOPT(Name, Bits, Default, Compatibility)
455#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
456#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
457#include "clang/Basic/CodeGenOptions.def"
458
459 return false;
460}
461
462static std::vector<std::string>
463accumulateFeaturesAsWritten(std::vector<std::string> FeaturesAsWritten) {
464 llvm::erase_if(C&: FeaturesAsWritten, P: [](const std::string &S) {
465 return S.empty() || (S[0] != '+' && S[0] != '-');
466 });
467 llvm::stable_sort(Range&: FeaturesAsWritten,
468 C: [](const std::string &A, const std::string &B) {
469 return A.substr(pos: 1) < B.substr(pos: 1);
470 });
471 auto NewRend =
472 std::unique(first: FeaturesAsWritten.rbegin(), last: FeaturesAsWritten.rend(),
473 binary_pred: [](const std::string &A, const std::string &B) {
474 return A.substr(pos: 1) == B.substr(pos: 1);
475 });
476 // Because we are operating on reverse iterators, the duplicate elements
477 // are actually at the beginning.
478 FeaturesAsWritten.erase(first: FeaturesAsWritten.begin(), last: NewRend.base());
479 return FeaturesAsWritten;
480}
481
482/// Compare the given set of target options against an existing set of
483/// target options.
484///
485/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
486///
487/// \returns true if the target options mis-match, false otherwise.
488static bool checkTargetOptions(const TargetOptions &TargetOpts,
489 const TargetOptions &ExistingTargetOpts,
490 StringRef ModuleFilename,
491 DiagnosticsEngine *Diags,
492 bool AllowCompatibleDifferences = true) {
493#define CHECK_TARGET_OPT(Field, Name) \
494 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
495 if (Diags) \
496 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
497 << ModuleFilename << Name << TargetOpts.Field \
498 << ExistingTargetOpts.Field; \
499 return true; \
500 }
501
502 // The triple and ABI must match exactly.
503 CHECK_TARGET_OPT(Triple, "target");
504 CHECK_TARGET_OPT(ABI, "target ABI");
505
506 // We can tolerate different CPUs in many cases, notably when one CPU
507 // supports a strict superset of another. When allowing compatible
508 // differences skip this check.
509 if (!AllowCompatibleDifferences) {
510 CHECK_TARGET_OPT(CPU, "target CPU");
511 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
512 }
513
514#undef CHECK_TARGET_OPT
515
516 // Compare feature sets.
517 // Alternatively, we could be diffing TargetOpts.Features, but that would
518 // clutter the output with implied features.
519 std::vector<std::string> ExistingFeatures =
520 accumulateFeaturesAsWritten(FeaturesAsWritten: ExistingTargetOpts.FeaturesAsWritten);
521 std::vector<std::string> ReadFeatures =
522 accumulateFeaturesAsWritten(FeaturesAsWritten: TargetOpts.FeaturesAsWritten);
523
524 // We compute the set difference in both directions explicitly so that we can
525 // diagnose the differences differently.
526 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
527 std::set_difference(
528 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
529 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
530 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
531 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
532 result: std::back_inserter(x&: UnmatchedReadFeatures));
533
534 // If we are allowing compatible differences and the read feature set is
535 // a strict subset of the existing feature set, there is nothing to diagnose.
536 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
537 return false;
538
539 if (Diags) {
540 for (StringRef Feature : UnmatchedReadFeatures)
541 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
542 << /* is-existing-feature */ false << ModuleFilename << Feature;
543 for (StringRef Feature : UnmatchedExistingFeatures)
544 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
545 << /* is-existing-feature */ true << ModuleFilename << Feature;
546 }
547
548 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
549}
550
551bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
552 StringRef ModuleFilename, bool Complain,
553 bool AllowCompatibleDifferences) {
554 const LangOptions &ExistingLangOpts = PP.getLangOpts();
555 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
556 Diags: Complain ? &Reader.Diags : nullptr,
557 AllowCompatibleDifferences);
558}
559
560bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
561 StringRef ModuleFilename, bool Complain,
562 bool AllowCompatibleDifferences) {
563 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
564 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
565 Diags: Complain ? &Reader.Diags : nullptr,
566 AllowCompatibleDifferences);
567}
568
569bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
570 StringRef ModuleFilename, bool Complain,
571 bool AllowCompatibleDifferences) {
572 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
573 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
574 Diags: Complain ? &Reader.Diags : nullptr,
575 AllowCompatibleDifferences);
576}
577
578namespace {
579
580using MacroDefinitionsMap =
581 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
582
583class DeclsSet {
584 SmallVector<NamedDecl *, 64> Decls;
585 llvm::SmallPtrSet<NamedDecl *, 8> Found;
586
587public:
588 operator ArrayRef<NamedDecl *>() const { return Decls; }
589
590 bool empty() const { return Decls.empty(); }
591
592 bool insert(NamedDecl *ND) {
593 auto [_, Inserted] = Found.insert(Ptr: ND);
594 if (Inserted)
595 Decls.push_back(Elt: ND);
596 return Inserted;
597 }
598};
599
600using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
601
602} // namespace
603
604static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
605 DiagnosticsEngine &Diags,
606 StringRef ModuleFilename,
607 bool Complain) {
608 using Level = DiagnosticsEngine::Level;
609
610 // Check current mappings for new -Werror mappings, and the stored mappings
611 // for cases that were explicitly mapped to *not* be errors that are now
612 // errors because of options like -Werror.
613 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
614
615 for (DiagnosticsEngine *MappingSource : MappingSources) {
616 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
617 diag::kind DiagID = DiagIDMappingPair.first;
618 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
619 if (CurLevel < DiagnosticsEngine::Error)
620 continue; // not significant
621 Level StoredLevel =
622 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
623 if (StoredLevel < DiagnosticsEngine::Error) {
624 if (Complain)
625 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
626 << "-Werror=" + Diags.getDiagnosticIDs()
627 ->getWarningOptionForDiag(DiagID)
628 .str()
629 << ModuleFilename;
630 return true;
631 }
632 }
633 }
634
635 return false;
636}
637
638static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
639 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
640 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
641 return true;
642 return Ext >= diag::Severity::Error;
643}
644
645static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
646 DiagnosticsEngine &Diags,
647 StringRef ModuleFilename, bool IsSystem,
648 bool SystemHeaderWarningsInModule,
649 bool Complain) {
650 // Top-level options
651 if (IsSystem) {
652 if (Diags.getSuppressSystemWarnings())
653 return false;
654 // If -Wsystem-headers was not enabled before, and it was not explicit,
655 // be conservative
656 if (StoredDiags.getSuppressSystemWarnings() &&
657 !SystemHeaderWarningsInModule) {
658 if (Complain)
659 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
660 << "-Wsystem-headers" << ModuleFilename;
661 return true;
662 }
663 }
664
665 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
666 if (Complain)
667 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
668 << "-Werror" << ModuleFilename;
669 return true;
670 }
671
672 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
673 !StoredDiags.getEnableAllWarnings()) {
674 if (Complain)
675 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
676 << "-Weverything -Werror" << ModuleFilename;
677 return true;
678 }
679
680 if (isExtHandlingFromDiagsError(Diags) &&
681 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
682 if (Complain)
683 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
684 << "-pedantic-errors" << ModuleFilename;
685 return true;
686 }
687
688 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
689 Complain);
690}
691
692/// Return the top import module if it is implicit, nullptr otherwise.
693static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
694 Preprocessor &PP) {
695 // If the original import came from a file explicitly generated by the user,
696 // don't check the diagnostic mappings.
697 // FIXME: currently this is approximated by checking whether this is not a
698 // module import of an implicitly-loaded module file.
699 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
700 // the transitive closure of its imports, since unrelated modules cannot be
701 // imported until after this module finishes validation.
702 ModuleFile *TopImport = &*ModuleMgr.rbegin();
703 while (!TopImport->ImportedBy.empty())
704 TopImport = TopImport->ImportedBy[0];
705 if (TopImport->Kind != MK_ImplicitModule)
706 return nullptr;
707
708 StringRef ModuleName = TopImport->ModuleName;
709 assert(!ModuleName.empty() && "diagnostic options read before module name");
710
711 Module *M =
712 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
713 assert(M && "missing module");
714 return M;
715}
716
717bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
718 StringRef ModuleFilename,
719 bool Complain) {
720 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
721 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
722 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
723 // This should never fail, because we would have processed these options
724 // before writing them to an ASTFile.
725 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
726 VFS&: PP.getFileManager().getVirtualFileSystem(),
727 /*Report*/ ReportDiags: false);
728
729 ModuleManager &ModuleMgr = Reader.getModuleManager();
730 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
731
732 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
733 if (!TopM)
734 return false;
735
736 Module *Importer = PP.getCurrentModule();
737
738 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
739 bool SystemHeaderWarningsInModule =
740 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
741 Element: Importer->Name);
742
743 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
744 // contains the union of their flags.
745 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
746 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
747 Complain);
748}
749
750/// Collect the macro definitions provided by the given preprocessor
751/// options.
752static void
753collectMacroDefinitions(const PreprocessorOptions &PPOpts,
754 MacroDefinitionsMap &Macros,
755 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
756 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
757 StringRef Macro = PPOpts.Macros[I].first;
758 bool IsUndef = PPOpts.Macros[I].second;
759
760 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
761 StringRef MacroName = MacroPair.first;
762 StringRef MacroBody = MacroPair.second;
763
764 // For an #undef'd macro, we only care about the name.
765 if (IsUndef) {
766 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
767 if (MacroNames && Inserted)
768 MacroNames->push_back(Elt: MacroName);
769
770 It->second = std::make_pair(x: "", y: true);
771 continue;
772 }
773
774 // For a #define'd macro, figure out the actual definition.
775 if (MacroName.size() == Macro.size())
776 MacroBody = "1";
777 else {
778 // Note: GCC drops anything following an end-of-line character.
779 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
780 MacroBody = MacroBody.substr(Start: 0, N: End);
781 }
782
783 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
784 if (MacroNames && Inserted)
785 MacroNames->push_back(Elt: MacroName);
786 It->second = std::make_pair(x&: MacroBody, y: false);
787 }
788}
789
790enum OptionValidation {
791 OptionValidateNone,
792 OptionValidateContradictions,
793 OptionValidateStrictMatches,
794};
795
796/// Check the preprocessor options deserialized from the control block
797/// against the preprocessor options in an existing preprocessor.
798///
799/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
800/// \param Validation If set to OptionValidateNone, ignore differences in
801/// preprocessor options. If set to OptionValidateContradictions,
802/// require that options passed both in the AST file and on the command
803/// line (-D or -U) match, but tolerate options missing in one or the
804/// other. If set to OptionValidateContradictions, require that there
805/// are no differences in the options between the two.
806static bool checkPreprocessorOptions(
807 const PreprocessorOptions &PPOpts,
808 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
809 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
810 std::string &SuggestedPredefines, const LangOptions &LangOpts,
811 OptionValidation Validation = OptionValidateContradictions) {
812 if (ReadMacros) {
813 // Check macro definitions.
814 MacroDefinitionsMap ASTFileMacros;
815 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
816 MacroDefinitionsMap ExistingMacros;
817 SmallVector<StringRef, 4> ExistingMacroNames;
818 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
819 MacroNames: &ExistingMacroNames);
820
821 // Use a line marker to enter the <command line> file, as the defines and
822 // undefines here will have come from the command line.
823 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
824
825 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
826 // Dig out the macro definition in the existing preprocessor options.
827 StringRef MacroName = ExistingMacroNames[I];
828 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
829
830 // Check whether we know anything about this macro name or not.
831 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
832 ASTFileMacros.find(Key: MacroName);
833 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
834 if (Validation == OptionValidateStrictMatches) {
835 // If strict matches are requested, don't tolerate any extra defines
836 // on the command line that are missing in the AST file.
837 if (Diags) {
838 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
839 << MacroName << true << ModuleFilename;
840 }
841 return true;
842 }
843 // FIXME: Check whether this identifier was referenced anywhere in the
844 // AST file. If so, we should reject the AST file. Unfortunately, this
845 // information isn't in the control block. What shall we do about it?
846
847 if (Existing.second) {
848 SuggestedPredefines += "#undef ";
849 SuggestedPredefines += MacroName.str();
850 SuggestedPredefines += '\n';
851 } else {
852 SuggestedPredefines += "#define ";
853 SuggestedPredefines += MacroName.str();
854 SuggestedPredefines += ' ';
855 SuggestedPredefines += Existing.first.str();
856 SuggestedPredefines += '\n';
857 }
858 continue;
859 }
860
861 // If the macro was defined in one but undef'd in the other, we have a
862 // conflict.
863 if (Existing.second != Known->second.second) {
864 if (Diags) {
865 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
866 << MacroName << Known->second.second << ModuleFilename;
867 }
868 return true;
869 }
870
871 // If the macro was #undef'd in both, or if the macro bodies are
872 // identical, it's fine.
873 if (Existing.second || Existing.first == Known->second.first) {
874 ASTFileMacros.erase(I: Known);
875 continue;
876 }
877
878 // The macro bodies differ; complain.
879 if (Diags) {
880 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
881 << MacroName << Known->second.first << Existing.first
882 << ModuleFilename;
883 }
884 return true;
885 }
886
887 // Leave the <command line> file and return to <built-in>.
888 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
889
890 if (Validation == OptionValidateStrictMatches) {
891 // If strict matches are requested, don't tolerate any extra defines in
892 // the AST file that are missing on the command line.
893 for (const auto &MacroName : ASTFileMacros.keys()) {
894 if (Diags) {
895 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
896 << MacroName << false << ModuleFilename;
897 }
898 return true;
899 }
900 }
901 }
902
903 // Check whether we're using predefines.
904 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
905 Validation != OptionValidateNone) {
906 if (Diags) {
907 Diags->Report(DiagID: diag::err_ast_file_undef)
908 << ExistingPPOpts.UsePredefines << ModuleFilename;
909 }
910 return true;
911 }
912
913 // Detailed record is important since it is used for the module cache hash.
914 if (LangOpts.Modules &&
915 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
916 Validation != OptionValidateNone) {
917 if (Diags) {
918 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
919 << PPOpts.DetailedRecord << ModuleFilename;
920 }
921 return true;
922 }
923
924 // Compute the #include and #include_macros lines we need.
925 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
926 StringRef File = ExistingPPOpts.Includes[I];
927
928 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
929 !ExistingPPOpts.PCHThroughHeader.empty()) {
930 // In case the through header is an include, we must add all the includes
931 // to the predefines so the start point can be determined.
932 SuggestedPredefines += "#include \"";
933 SuggestedPredefines += File;
934 SuggestedPredefines += "\"\n";
935 continue;
936 }
937
938 if (File == ExistingPPOpts.ImplicitPCHInclude)
939 continue;
940
941 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
942 continue;
943
944 SuggestedPredefines += "#include \"";
945 SuggestedPredefines += File;
946 SuggestedPredefines += "\"\n";
947 }
948
949 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
950 StringRef File = ExistingPPOpts.MacroIncludes[I];
951 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
952 continue;
953
954 SuggestedPredefines += "#__include_macros \"";
955 SuggestedPredefines += File;
956 SuggestedPredefines += "\"\n##\n";
957 }
958
959 return false;
960}
961
962bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
963 StringRef ModuleFilename,
964 bool ReadMacros, bool Complain,
965 std::string &SuggestedPredefines) {
966 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
967
968 return checkPreprocessorOptions(
969 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
970 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
971 SuggestedPredefines, LangOpts: PP.getLangOpts());
972}
973
974bool SimpleASTReaderListener::ReadPreprocessorOptions(
975 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
976 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
977 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
978 ModuleFilename, ReadMacros, Diags: nullptr,
979 FileMgr&: PP.getFileManager(), SuggestedPredefines,
980 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
981}
982
983/// Check that the specified and the existing module cache paths are equivalent.
984///
985/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
986/// \returns true when the module cache paths differ.
987static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
988 StringRef ExistingSpecificModuleCachePath,
989 StringRef ASTFilename,
990 DiagnosticsEngine *Diags,
991 const LangOptions &LangOpts,
992 const PreprocessorOptions &PPOpts,
993 const HeaderSearchOptions &HSOpts,
994 const HeaderSearchOptions &ASTFileHSOpts) {
995 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
996 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
997 ContextHash: std::string(ContextHash));
998
999 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
1000 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
1001 return false;
1002 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1003 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
1004 if (EqualOrErr && *EqualOrErr)
1005 return false;
1006 if (Diags) {
1007 // If the module cache arguments provided from the command line are the
1008 // same, the mismatch must come from other arguments of the configuration
1009 // and not directly the cache path.
1010 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1011 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
1012 if (EqualOrErr && *EqualOrErr)
1013 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
1014 else
1015 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
1016 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
1017 << ASTFilename;
1018 }
1019 return true;
1020}
1021
1022bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1023 StringRef ASTFilename,
1024 StringRef ContextHash,
1025 bool Complain) {
1026 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1027 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1028 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1029 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1030 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1031 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1032}
1033
1034void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1035 PP.setCounterValue(Value);
1036}
1037
1038//===----------------------------------------------------------------------===//
1039// AST reader implementation
1040//===----------------------------------------------------------------------===//
1041
1042static uint64_t readULEB(const unsigned char *&P) {
1043 unsigned Length = 0;
1044 const char *Error = nullptr;
1045
1046 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1047 if (Error)
1048 llvm::report_fatal_error(reason: Error);
1049 P += Length;
1050 return Val;
1051}
1052
1053/// Read ULEB-encoded key length and data length.
1054static std::pair<unsigned, unsigned>
1055readULEBKeyDataLength(const unsigned char *&P) {
1056 unsigned KeyLen = readULEB(P);
1057 if ((unsigned)KeyLen != KeyLen)
1058 llvm::report_fatal_error(reason: "key too large");
1059
1060 unsigned DataLen = readULEB(P);
1061 if ((unsigned)DataLen != DataLen)
1062 llvm::report_fatal_error(reason: "data too large");
1063
1064 return std::make_pair(x&: KeyLen, y&: DataLen);
1065}
1066
1067void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1068 bool TakeOwnership) {
1069 DeserializationListener = Listener;
1070 OwnsDeserializationListener = TakeOwnership;
1071}
1072
1073unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1074 return serialization::ComputeHash(Sel);
1075}
1076
1077LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1078 LocalDeclID ID(Value);
1079#ifndef NDEBUG
1080 if (!MF.ModuleOffsetMap.empty())
1081 Reader.ReadModuleOffsetMap(MF);
1082
1083 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1084 unsigned LocalDeclID = ID.getLocalDeclIndex();
1085
1086 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1087
1088 ModuleFile *OwningModuleFile =
1089 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1090 assert(OwningModuleFile);
1091
1092 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1093
1094 if (!ModuleFileIndex)
1095 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1096
1097 assert(LocalDeclID < LocalNumDecls);
1098#endif
1099 (void)Reader;
1100 (void)MF;
1101 return ID;
1102}
1103
1104LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1105 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1106 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1107 return LocalDeclID::get(Reader, MF, Value);
1108}
1109
1110std::pair<unsigned, unsigned>
1111ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1112 return readULEBKeyDataLength(P&: d);
1113}
1114
1115ASTSelectorLookupTrait::internal_key_type
1116ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1117 using namespace llvm::support;
1118
1119 SelectorTable &SelTable = Reader.getContext().Selectors;
1120 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1121 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1122 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1123 if (N == 0)
1124 return SelTable.getNullarySelector(ID: FirstII);
1125 else if (N == 1)
1126 return SelTable.getUnarySelector(ID: FirstII);
1127
1128 SmallVector<const IdentifierInfo *, 16> Args;
1129 Args.push_back(Elt: FirstII);
1130 for (unsigned I = 1; I != N; ++I)
1131 Args.push_back(Elt: Reader.getLocalIdentifier(
1132 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1133
1134 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1135}
1136
1137ASTSelectorLookupTrait::data_type
1138ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1139 unsigned DataLen) {
1140 using namespace llvm::support;
1141
1142 data_type Result;
1143
1144 Result.ID = Reader.getGlobalSelectorID(
1145 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1146 unsigned FullInstanceBits =
1147 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1148 unsigned FullFactoryBits =
1149 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1150 Result.InstanceBits = FullInstanceBits & 0x3;
1151 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1152 Result.FactoryBits = FullFactoryBits & 0x3;
1153 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1154 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1155 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1156
1157 // Load instance methods
1158 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1159 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1160 F, LocalID: LocalDeclID::get(
1161 Reader, MF&: F,
1162 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1163 Result.Instance.push_back(Elt: Method);
1164 }
1165
1166 // Load factory methods
1167 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1168 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1169 F, LocalID: LocalDeclID::get(
1170 Reader, MF&: F,
1171 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1172 Result.Factory.push_back(Elt: Method);
1173 }
1174
1175 return Result;
1176}
1177
1178unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1179 return llvm::djbHash(Buffer: a);
1180}
1181
1182std::pair<unsigned, unsigned>
1183ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1184 return readULEBKeyDataLength(P&: d);
1185}
1186
1187ASTIdentifierLookupTraitBase::internal_key_type
1188ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1189 assert(n >= 2 && d[n-1] == '\0');
1190 return StringRef((const char*) d, n-1);
1191}
1192
1193/// Whether the given identifier is "interesting".
1194static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1195 bool IsModule) {
1196 bool IsInteresting =
1197 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1198 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1199 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1200 return II.hadMacroDefinition() || II.isPoisoned() ||
1201 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1202 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1203 II.getFETokenInfo());
1204}
1205
1206static bool readBit(unsigned &Bits) {
1207 bool Value = Bits & 0x1;
1208 Bits >>= 1;
1209 return Value;
1210}
1211
1212IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1213 using namespace llvm::support;
1214
1215 IdentifierID RawID =
1216 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1217 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1218}
1219
1220static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1221 bool IsModule) {
1222 if (!II.isFromAST()) {
1223 II.setIsFromAST();
1224 if (isInterestingIdentifier(Reader, II, IsModule))
1225 II.setChangedSinceDeserialization();
1226 }
1227}
1228
1229IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1230 const unsigned char* d,
1231 unsigned DataLen) {
1232 using namespace llvm::support;
1233
1234 IdentifierID RawID =
1235 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1236 bool IsInteresting = RawID & 0x01;
1237
1238 DataLen -= sizeof(IdentifierID);
1239
1240 // Wipe out the "is interesting" bit.
1241 RawID = RawID >> 1;
1242
1243 // Build the IdentifierInfo and link the identifier ID with it.
1244 IdentifierInfo *II = KnownII;
1245 if (!II) {
1246 II = &Reader.getIdentifierTable().getOwn(Name: k);
1247 KnownII = II;
1248 }
1249 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1250 markIdentifierFromAST(Reader, II&: *II, IsModule);
1251 Reader.markIdentifierUpToDate(II);
1252
1253 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1254 if (!IsInteresting) {
1255 // For uninteresting identifiers, there's nothing else to do. Just notify
1256 // the reader that we've finished loading this identifier.
1257 Reader.SetIdentifierInfo(ID, II);
1258 return II;
1259 }
1260
1261 unsigned ObjCOrBuiltinID =
1262 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1263 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1264 bool CPlusPlusOperatorKeyword = readBit(Bits);
1265 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1266 bool Poisoned = readBit(Bits);
1267 bool ExtensionToken = readBit(Bits);
1268 bool HasMacroDefinition = readBit(Bits);
1269
1270 assert(Bits == 0 && "Extra bits in the identifier?");
1271 DataLen -= sizeof(uint16_t) * 2;
1272
1273 // Set or check the various bits in the IdentifierInfo structure.
1274 // Token IDs are read-only.
1275 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1276 II->revertTokenIDToIdentifier();
1277 if (!F.isModule())
1278 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1279 assert(II->isExtensionToken() == ExtensionToken &&
1280 "Incorrect extension token flag");
1281 (void)ExtensionToken;
1282 if (Poisoned)
1283 II->setIsPoisoned(true);
1284 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1285 "Incorrect C++ operator keyword flag");
1286 (void)CPlusPlusOperatorKeyword;
1287
1288 // If this identifier has a macro definition, deserialize it or notify the
1289 // visitor the actual definition is in a different module.
1290 if (HasMacroDefinition) {
1291 uint32_t MacroDirectivesOffset =
1292 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1293 DataLen -= 4;
1294
1295 if (MacroDirectivesOffset)
1296 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1297 else
1298 hasMacroDefinitionInDependencies = true;
1299 }
1300
1301 Reader.SetIdentifierInfo(ID, II);
1302
1303 // Read all of the declarations visible at global scope with this
1304 // name.
1305 if (DataLen > 0) {
1306 SmallVector<GlobalDeclID, 4> DeclIDs;
1307 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1308 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1309 F, LocalID: LocalDeclID::get(
1310 Reader, MF&: F,
1311 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1312 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1313 }
1314
1315 return II;
1316}
1317
1318DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1319 : Kind(Name.getNameKind()) {
1320 switch (Kind) {
1321 case DeclarationName::Identifier:
1322 Data = (uint64_t)Name.getAsIdentifierInfo();
1323 break;
1324 case DeclarationName::ObjCZeroArgSelector:
1325 case DeclarationName::ObjCOneArgSelector:
1326 case DeclarationName::ObjCMultiArgSelector:
1327 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1328 break;
1329 case DeclarationName::CXXOperatorName:
1330 Data = Name.getCXXOverloadedOperator();
1331 break;
1332 case DeclarationName::CXXLiteralOperatorName:
1333 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1334 break;
1335 case DeclarationName::CXXDeductionGuideName:
1336 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1337 ->getDeclName().getAsIdentifierInfo();
1338 break;
1339 case DeclarationName::CXXConstructorName:
1340 case DeclarationName::CXXDestructorName:
1341 case DeclarationName::CXXConversionFunctionName:
1342 case DeclarationName::CXXUsingDirective:
1343 Data = 0;
1344 break;
1345 }
1346}
1347
1348unsigned DeclarationNameKey::getHash() const {
1349 llvm::FoldingSetNodeID ID;
1350 ID.AddInteger(I: Kind);
1351
1352 switch (Kind) {
1353 case DeclarationName::Identifier:
1354 case DeclarationName::CXXLiteralOperatorName:
1355 case DeclarationName::CXXDeductionGuideName:
1356 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1357 break;
1358 case DeclarationName::ObjCZeroArgSelector:
1359 case DeclarationName::ObjCOneArgSelector:
1360 case DeclarationName::ObjCMultiArgSelector:
1361 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1362 break;
1363 case DeclarationName::CXXOperatorName:
1364 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1365 break;
1366 case DeclarationName::CXXConstructorName:
1367 case DeclarationName::CXXDestructorName:
1368 case DeclarationName::CXXConversionFunctionName:
1369 case DeclarationName::CXXUsingDirective:
1370 break;
1371 }
1372
1373 return ID.computeStableHash();
1374}
1375
1376ModuleFile *
1377ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1378 using namespace llvm::support;
1379
1380 uint32_t ModuleFileID =
1381 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1382 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1383}
1384
1385std::pair<unsigned, unsigned>
1386ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1387 return readULEBKeyDataLength(P&: d);
1388}
1389
1390DeclarationNameKey
1391ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1392 using namespace llvm::support;
1393
1394 auto Kind = (DeclarationName::NameKind)*d++;
1395 uint64_t Data;
1396 switch (Kind) {
1397 case DeclarationName::Identifier:
1398 case DeclarationName::CXXLiteralOperatorName:
1399 case DeclarationName::CXXDeductionGuideName:
1400 Data = (uint64_t)Reader.getLocalIdentifier(
1401 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1402 break;
1403 case DeclarationName::ObjCZeroArgSelector:
1404 case DeclarationName::ObjCOneArgSelector:
1405 case DeclarationName::ObjCMultiArgSelector:
1406 Data = (uint64_t)Reader
1407 .getLocalSelector(
1408 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1409 .getAsOpaquePtr();
1410 break;
1411 case DeclarationName::CXXOperatorName:
1412 Data = *d++; // OverloadedOperatorKind
1413 break;
1414 case DeclarationName::CXXConstructorName:
1415 case DeclarationName::CXXDestructorName:
1416 case DeclarationName::CXXConversionFunctionName:
1417 case DeclarationName::CXXUsingDirective:
1418 Data = 0;
1419 break;
1420 }
1421
1422 return DeclarationNameKey(Kind, Data);
1423}
1424
1425ASTDeclContextNameLookupTrait::internal_key_type
1426ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1427 return ReadKeyBase(d);
1428}
1429
1430void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1431 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1432 using namespace llvm::support;
1433
1434 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1435 LocalDeclID ID = LocalDeclID::get(
1436 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1437 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1438 }
1439}
1440
1441void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1442 const unsigned char *d,
1443 unsigned DataLen,
1444 data_type_builder &Val) {
1445 ReadDataIntoImpl(d, DataLen, Val);
1446}
1447
1448ModuleLocalNameLookupTrait::hash_value_type
1449ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1450 llvm::FoldingSetNodeID ID;
1451 ID.AddInteger(I: Key.first.getHash());
1452 ID.AddInteger(I: Key.second);
1453 return ID.computeStableHash();
1454}
1455
1456ModuleLocalNameLookupTrait::internal_key_type
1457ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1458 DeclarationNameKey Name(Key.first);
1459
1460 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1461 if (!ModuleHash)
1462 return {Name, 0};
1463
1464 return {Name, *ModuleHash};
1465}
1466
1467ModuleLocalNameLookupTrait::internal_key_type
1468ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1469 DeclarationNameKey Name = ReadKeyBase(d);
1470 unsigned PrimaryModuleHash =
1471 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1472 return {Name, PrimaryModuleHash};
1473}
1474
1475void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1476 const unsigned char *d,
1477 unsigned DataLen,
1478 data_type_builder &Val) {
1479 ReadDataIntoImpl(d, DataLen, Val);
1480}
1481
1482ModuleFile *
1483LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1484 using namespace llvm::support;
1485
1486 uint32_t ModuleFileID =
1487 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1488 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1489}
1490
1491LazySpecializationInfoLookupTrait::internal_key_type
1492LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1493 using namespace llvm::support;
1494 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1495}
1496
1497std::pair<unsigned, unsigned>
1498LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1499 return readULEBKeyDataLength(P&: d);
1500}
1501
1502void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1503 const unsigned char *d,
1504 unsigned DataLen,
1505 data_type_builder &Val) {
1506 using namespace llvm::support;
1507
1508 for (unsigned NumDecls =
1509 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1510 NumDecls; --NumDecls) {
1511 LocalDeclID LocalID = LocalDeclID::get(
1512 Reader, MF&: F,
1513 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1514 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1515 }
1516}
1517
1518bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1519 BitstreamCursor &Cursor,
1520 uint64_t Offset,
1521 DeclContext *DC) {
1522 assert(Offset != 0);
1523
1524 SavedStreamPosition SavedPosition(Cursor);
1525 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1526 Error(Err: std::move(Err));
1527 return true;
1528 }
1529
1530 RecordData Record;
1531 StringRef Blob;
1532 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1533 if (!MaybeCode) {
1534 Error(Err: MaybeCode.takeError());
1535 return true;
1536 }
1537 unsigned Code = MaybeCode.get();
1538
1539 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1540 if (!MaybeRecCode) {
1541 Error(Err: MaybeRecCode.takeError());
1542 return true;
1543 }
1544 unsigned RecCode = MaybeRecCode.get();
1545 if (RecCode != DECL_CONTEXT_LEXICAL) {
1546 Error(Msg: "Expected lexical block");
1547 return true;
1548 }
1549
1550 assert(!isa<TranslationUnitDecl>(DC) &&
1551 "expected a TU_UPDATE_LEXICAL record for TU");
1552 // If we are handling a C++ class template instantiation, we can see multiple
1553 // lexical updates for the same record. It's important that we select only one
1554 // of them, so that field numbering works properly. Just pick the first one we
1555 // see.
1556 auto &Lex = LexicalDecls[DC];
1557 if (!Lex.first) {
1558 Lex = std::make_pair(
1559 x: &M, y: llvm::ArrayRef(
1560 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1561 Blob.size() / sizeof(DeclID)));
1562 }
1563 DC->setHasExternalLexicalStorage(true);
1564 return false;
1565}
1566
1567bool ASTReader::ReadVisibleDeclContextStorage(
1568 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1569 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1570 assert(Offset != 0);
1571
1572 SavedStreamPosition SavedPosition(Cursor);
1573 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1574 Error(Err: std::move(Err));
1575 return true;
1576 }
1577
1578 RecordData Record;
1579 StringRef Blob;
1580 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1581 if (!MaybeCode) {
1582 Error(Err: MaybeCode.takeError());
1583 return true;
1584 }
1585 unsigned Code = MaybeCode.get();
1586
1587 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1588 if (!MaybeRecCode) {
1589 Error(Err: MaybeRecCode.takeError());
1590 return true;
1591 }
1592 unsigned RecCode = MaybeRecCode.get();
1593 switch (VisibleKind) {
1594 case VisibleDeclContextStorageKind::GenerallyVisible:
1595 if (RecCode != DECL_CONTEXT_VISIBLE) {
1596 Error(Msg: "Expected visible lookup table block");
1597 return true;
1598 }
1599 break;
1600 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1601 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1602 Error(Msg: "Expected module local visible lookup table block");
1603 return true;
1604 }
1605 break;
1606 case VisibleDeclContextStorageKind::TULocalVisible:
1607 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1608 Error(Msg: "Expected TU local lookup table block");
1609 return true;
1610 }
1611 break;
1612 }
1613
1614 // We can't safely determine the primary context yet, so delay attaching the
1615 // lookup table until we're done with recursive deserialization.
1616 auto *Data = (const unsigned char*)Blob.data();
1617 switch (VisibleKind) {
1618 case VisibleDeclContextStorageKind::GenerallyVisible:
1619 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1620 break;
1621 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1622 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1623 break;
1624 case VisibleDeclContextStorageKind::TULocalVisible:
1625 if (M.Kind == MK_MainFile)
1626 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1627 break;
1628 }
1629 return false;
1630}
1631
1632void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1633 ModuleFile &M, bool IsPartial) {
1634 D = D->getCanonicalDecl();
1635 auto &SpecLookups =
1636 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1637 SpecLookups[D].Table.add(File: &M, Data,
1638 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1639}
1640
1641bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1642 uint64_t Offset, Decl *D, bool IsPartial) {
1643 assert(Offset != 0);
1644
1645 SavedStreamPosition SavedPosition(Cursor);
1646 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1647 Error(Err: std::move(Err));
1648 return true;
1649 }
1650
1651 RecordData Record;
1652 StringRef Blob;
1653 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1654 if (!MaybeCode) {
1655 Error(Err: MaybeCode.takeError());
1656 return true;
1657 }
1658 unsigned Code = MaybeCode.get();
1659
1660 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1661 if (!MaybeRecCode) {
1662 Error(Err: MaybeRecCode.takeError());
1663 return true;
1664 }
1665 unsigned RecCode = MaybeRecCode.get();
1666 if (RecCode != DECL_SPECIALIZATIONS &&
1667 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1668 Error(Msg: "Expected decl specs block");
1669 return true;
1670 }
1671
1672 auto *Data = (const unsigned char *)Blob.data();
1673 AddSpecializations(D, Data, M, IsPartial);
1674 return false;
1675}
1676
1677void ASTReader::Error(StringRef Msg) const {
1678 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1679 if (PP.getLangOpts().Modules &&
1680 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1681 Diag(DiagID: diag::note_module_cache_path)
1682 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1683 }
1684}
1685
1686void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1687 StringRef Arg3) const {
1688 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1689}
1690
1691namespace {
1692struct AlreadyReportedDiagnosticError
1693 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1694 static char ID;
1695
1696 void log(raw_ostream &OS) const override {
1697 llvm_unreachable("reporting an already-reported diagnostic error");
1698 }
1699
1700 std::error_code convertToErrorCode() const override {
1701 return llvm::inconvertibleErrorCode();
1702 }
1703};
1704
1705char AlreadyReportedDiagnosticError::ID = 0;
1706} // namespace
1707
1708void ASTReader::Error(llvm::Error &&Err) const {
1709 handleAllErrors(
1710 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1711 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1712}
1713
1714//===----------------------------------------------------------------------===//
1715// Source Manager Deserialization
1716//===----------------------------------------------------------------------===//
1717
1718/// Read the line table in the source manager block.
1719void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1720 unsigned Idx = 0;
1721 LineTableInfo &LineTable = SourceMgr.getLineTable();
1722
1723 // Parse the file names
1724 std::map<int, int> FileIDs;
1725 FileIDs[-1] = -1; // For unspecified filenames.
1726 for (unsigned I = 0; Record[Idx]; ++I) {
1727 // Extract the file name
1728 auto Filename = ReadPath(F, Record, Idx);
1729 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1730 }
1731 ++Idx;
1732
1733 // Parse the line entries
1734 std::vector<LineEntry> Entries;
1735 while (Idx < Record.size()) {
1736 FileID FID = ReadFileID(F, Record, Idx);
1737
1738 // Extract the line entries
1739 unsigned NumEntries = Record[Idx++];
1740 assert(NumEntries && "no line entries for file ID");
1741 Entries.clear();
1742 Entries.reserve(n: NumEntries);
1743 for (unsigned I = 0; I != NumEntries; ++I) {
1744 unsigned FileOffset = Record[Idx++];
1745 unsigned LineNo = Record[Idx++];
1746 int FilenameID = FileIDs[Record[Idx++]];
1747 SrcMgr::CharacteristicKind FileKind
1748 = (SrcMgr::CharacteristicKind)Record[Idx++];
1749 unsigned IncludeOffset = Record[Idx++];
1750 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1751 FileKind, IncludeOffset));
1752 }
1753 LineTable.AddEntry(FID, Entries);
1754 }
1755}
1756
1757/// Read a source manager block
1758llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1759 using namespace SrcMgr;
1760
1761 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1762
1763 // Set the source-location entry cursor to the current position in
1764 // the stream. This cursor will be used to read the contents of the
1765 // source manager block initially, and then lazily read
1766 // source-location entries as needed.
1767 SLocEntryCursor = F.Stream;
1768
1769 // The stream itself is going to skip over the source manager block.
1770 if (llvm::Error Err = F.Stream.SkipBlock())
1771 return Err;
1772
1773 // Enter the source manager block.
1774 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1775 return Err;
1776 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1777
1778 RecordData Record;
1779 while (true) {
1780 Expected<llvm::BitstreamEntry> MaybeE =
1781 SLocEntryCursor.advanceSkippingSubblocks();
1782 if (!MaybeE)
1783 return MaybeE.takeError();
1784 llvm::BitstreamEntry E = MaybeE.get();
1785
1786 switch (E.Kind) {
1787 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1788 case llvm::BitstreamEntry::Error:
1789 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1790 Fmt: "malformed block record in AST file");
1791 case llvm::BitstreamEntry::EndBlock:
1792 return llvm::Error::success();
1793 case llvm::BitstreamEntry::Record:
1794 // The interesting case.
1795 break;
1796 }
1797
1798 // Read a record.
1799 Record.clear();
1800 StringRef Blob;
1801 Expected<unsigned> MaybeRecord =
1802 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1803 if (!MaybeRecord)
1804 return MaybeRecord.takeError();
1805 switch (MaybeRecord.get()) {
1806 default: // Default behavior: ignore.
1807 break;
1808
1809 case SM_SLOC_FILE_ENTRY:
1810 case SM_SLOC_BUFFER_ENTRY:
1811 case SM_SLOC_EXPANSION_ENTRY:
1812 // Once we hit one of the source location entries, we're done.
1813 return llvm::Error::success();
1814 }
1815 }
1816}
1817
1818llvm::Expected<SourceLocation::UIntTy>
1819ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1820 BitstreamCursor &Cursor = F->SLocEntryCursor;
1821 SavedStreamPosition SavedPosition(Cursor);
1822 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1823 F->SLocEntryOffsets[Index]))
1824 return std::move(Err);
1825
1826 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1827 if (!MaybeEntry)
1828 return MaybeEntry.takeError();
1829
1830 llvm::BitstreamEntry Entry = MaybeEntry.get();
1831 if (Entry.Kind != llvm::BitstreamEntry::Record)
1832 return llvm::createStringError(
1833 EC: std::errc::illegal_byte_sequence,
1834 Fmt: "incorrectly-formatted source location entry in AST file");
1835
1836 RecordData Record;
1837 StringRef Blob;
1838 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1839 if (!MaybeSLOC)
1840 return MaybeSLOC.takeError();
1841
1842 switch (MaybeSLOC.get()) {
1843 default:
1844 return llvm::createStringError(
1845 EC: std::errc::illegal_byte_sequence,
1846 Fmt: "incorrectly-formatted source location entry in AST file");
1847 case SM_SLOC_FILE_ENTRY:
1848 case SM_SLOC_BUFFER_ENTRY:
1849 case SM_SLOC_EXPANSION_ENTRY:
1850 return F->SLocEntryBaseOffset + Record[0];
1851 }
1852}
1853
1854int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1855 auto SLocMapI =
1856 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1857 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1858 "Corrupted global sloc offset map");
1859 ModuleFile *F = SLocMapI->second;
1860
1861 bool Invalid = false;
1862
1863 auto It = llvm::upper_bound(
1864 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1865 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1866 int ID = F->SLocEntryBaseID + LocalIndex;
1867 std::size_t Index = -ID - 2;
1868 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1869 assert(!SourceMgr.SLocEntryLoaded[Index]);
1870 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1871 if (!MaybeEntryOffset) {
1872 Error(Err: MaybeEntryOffset.takeError());
1873 Invalid = true;
1874 return true;
1875 }
1876 SourceMgr.LoadedSLocEntryTable[Index] =
1877 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1878 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1879 }
1880 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1881 });
1882
1883 if (Invalid)
1884 return 0;
1885
1886 // The iterator points to the first entry with start offset greater than the
1887 // offset of interest. The previous entry must contain the offset of interest.
1888 return F->SLocEntryBaseID + *std::prev(x: It);
1889}
1890
1891bool ASTReader::ReadSLocEntry(int ID) {
1892 if (ID == 0)
1893 return false;
1894
1895 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1896 Error(Msg: "source location entry ID out-of-range for AST file");
1897 return true;
1898 }
1899
1900 // Local helper to read the (possibly-compressed) buffer data following the
1901 // entry record.
1902 auto ReadBuffer = [this](
1903 BitstreamCursor &SLocEntryCursor,
1904 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1905 RecordData Record;
1906 StringRef Blob;
1907 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1908 if (!MaybeCode) {
1909 Error(Err: MaybeCode.takeError());
1910 return nullptr;
1911 }
1912 unsigned Code = MaybeCode.get();
1913
1914 Expected<unsigned> MaybeRecCode =
1915 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1916 if (!MaybeRecCode) {
1917 Error(Err: MaybeRecCode.takeError());
1918 return nullptr;
1919 }
1920 unsigned RecCode = MaybeRecCode.get();
1921
1922 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1923 // Inspect the first byte to differentiate zlib (\x78) and zstd
1924 // (little-endian 0xFD2FB528).
1925 const llvm::compression::Format F =
1926 Blob.size() > 0 && Blob.data()[0] == 0x78
1927 ? llvm::compression::Format::Zlib
1928 : llvm::compression::Format::Zstd;
1929 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1930 Error(Msg: Reason);
1931 return nullptr;
1932 }
1933 SmallVector<uint8_t, 0> Decompressed;
1934 if (llvm::Error E = llvm::compression::decompress(
1935 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1936 Error(Msg: "could not decompress embedded file contents: " +
1937 llvm::toString(E: std::move(E)));
1938 return nullptr;
1939 }
1940 return llvm::MemoryBuffer::getMemBufferCopy(
1941 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1942 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1943 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1944 } else {
1945 Error(Msg: "AST record has invalid code");
1946 return nullptr;
1947 }
1948 };
1949
1950 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1951 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1952 BitNo: F->SLocEntryOffsetsBase +
1953 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1954 Error(Err: std::move(Err));
1955 return true;
1956 }
1957
1958 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1959 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1960
1961 ++NumSLocEntriesRead;
1962 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1963 if (!MaybeEntry) {
1964 Error(Err: MaybeEntry.takeError());
1965 return true;
1966 }
1967 llvm::BitstreamEntry Entry = MaybeEntry.get();
1968
1969 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1970 Error(Msg: "incorrectly-formatted source location entry in AST file");
1971 return true;
1972 }
1973
1974 RecordData Record;
1975 StringRef Blob;
1976 Expected<unsigned> MaybeSLOC =
1977 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1978 if (!MaybeSLOC) {
1979 Error(Err: MaybeSLOC.takeError());
1980 return true;
1981 }
1982 switch (MaybeSLOC.get()) {
1983 default:
1984 Error(Msg: "incorrectly-formatted source location entry in AST file");
1985 return true;
1986
1987 case SM_SLOC_FILE_ENTRY: {
1988 // We will detect whether a file changed and return 'Failure' for it, but
1989 // we will also try to fail gracefully by setting up the SLocEntry.
1990 unsigned InputID = Record[4];
1991 InputFile IF = getInputFile(F&: *F, ID: InputID);
1992 OptionalFileEntryRef File = IF.getFile();
1993 bool OverriddenBuffer = IF.isOverridden();
1994
1995 // Note that we only check if a File was returned. If it was out-of-date
1996 // we have complained but we will continue creating a FileID to recover
1997 // gracefully.
1998 if (!File)
1999 return true;
2000
2001 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2002 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
2003 // This is the module's main file.
2004 IncludeLoc = getImportLocation(F);
2005 }
2006 SrcMgr::CharacteristicKind
2007 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2008 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
2009 LoadedOffset: BaseOffset + Record[0]);
2010 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2011 FileInfo.NumCreatedFIDs = Record[5];
2012 if (Record[3])
2013 FileInfo.setHasLineDirectives();
2014
2015 unsigned NumFileDecls = Record[7];
2016 if (NumFileDecls && ContextObj) {
2017 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
2018 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
2019 FileDeclIDs[FID] =
2020 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
2021 }
2022
2023 const SrcMgr::ContentCache &ContentCache =
2024 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2025 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2026 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2027 !ContentCache.getBufferIfLoaded()) {
2028 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2029 if (!Buffer)
2030 return true;
2031 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2032 }
2033
2034 break;
2035 }
2036
2037 case SM_SLOC_BUFFER_ENTRY: {
2038 const char *Name = Blob.data();
2039 unsigned Offset = Record[0];
2040 SrcMgr::CharacteristicKind
2041 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2042 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2043 if (IncludeLoc.isInvalid() && F->isModule()) {
2044 IncludeLoc = getImportLocation(F);
2045 }
2046
2047 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2048 if (!Buffer)
2049 return true;
2050 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2051 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2052 if (Record[3]) {
2053 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2054 FileInfo.setHasLineDirectives();
2055 }
2056 break;
2057 }
2058
2059 case SM_SLOC_EXPANSION_ENTRY: {
2060 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2061 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2062 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2063 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2064 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2065 LoadedOffset: BaseOffset + Record[0]);
2066 break;
2067 }
2068 }
2069
2070 return false;
2071}
2072
2073std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2074 if (ID == 0)
2075 return std::make_pair(x: SourceLocation(), y: "");
2076
2077 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2078 Error(Msg: "source location entry ID out-of-range for AST file");
2079 return std::make_pair(x: SourceLocation(), y: "");
2080 }
2081
2082 // Find which module file this entry lands in.
2083 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2084 if (!M->isModule())
2085 return std::make_pair(x: SourceLocation(), y: "");
2086
2087 // FIXME: Can we map this down to a particular submodule? That would be
2088 // ideal.
2089 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2090}
2091
2092/// Find the location where the module F is imported.
2093SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2094 if (F->ImportLoc.isValid())
2095 return F->ImportLoc;
2096
2097 // Otherwise we have a PCH. It's considered to be "imported" at the first
2098 // location of its includer.
2099 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2100 // Main file is the importer.
2101 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2102 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2103 }
2104 return F->ImportedBy[0]->FirstLoc;
2105}
2106
2107/// Enter a subblock of the specified BlockID with the specified cursor. Read
2108/// the abbreviations that are at the top of the block and then leave the cursor
2109/// pointing into the block.
2110llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2111 unsigned BlockID,
2112 uint64_t *StartOfBlockOffset) {
2113 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2114 return Err;
2115
2116 if (StartOfBlockOffset)
2117 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2118
2119 while (true) {
2120 uint64_t Offset = Cursor.GetCurrentBitNo();
2121 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2122 if (!MaybeCode)
2123 return MaybeCode.takeError();
2124 unsigned Code = MaybeCode.get();
2125
2126 // We expect all abbrevs to be at the start of the block.
2127 if (Code != llvm::bitc::DEFINE_ABBREV) {
2128 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2129 return Err;
2130 return llvm::Error::success();
2131 }
2132 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2133 return Err;
2134 }
2135}
2136
2137Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2138 unsigned &Idx) {
2139 Token Tok;
2140 Tok.startToken();
2141 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2142 Tok.setKind((tok::TokenKind)Record[Idx++]);
2143 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2144
2145 if (Tok.isAnnotation()) {
2146 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2147 switch (Tok.getKind()) {
2148 case tok::annot_pragma_loop_hint: {
2149 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2150 Info->PragmaName = ReadToken(M, Record, Idx);
2151 Info->Option = ReadToken(M, Record, Idx);
2152 unsigned NumTokens = Record[Idx++];
2153 SmallVector<Token, 4> Toks;
2154 Toks.reserve(N: NumTokens);
2155 for (unsigned I = 0; I < NumTokens; ++I)
2156 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2157 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2158 Tok.setAnnotationValue(static_cast<void *>(Info));
2159 break;
2160 }
2161 case tok::annot_pragma_pack: {
2162 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2163 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2164 auto SlotLabel = ReadString(Record, Idx);
2165 Info->SlotLabel =
2166 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2167 Info->Alignment = ReadToken(M, Record, Idx);
2168 Tok.setAnnotationValue(static_cast<void *>(Info));
2169 break;
2170 }
2171 // Some annotation tokens do not use the PtrData field.
2172 case tok::annot_pragma_openmp:
2173 case tok::annot_pragma_openmp_end:
2174 case tok::annot_pragma_unused:
2175 case tok::annot_pragma_openacc:
2176 case tok::annot_pragma_openacc_end:
2177 case tok::annot_repl_input_end:
2178 break;
2179 default:
2180 llvm_unreachable("missing deserialization code for annotation token");
2181 }
2182 } else {
2183 Tok.setLength(Record[Idx++]);
2184 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2185 Tok.setIdentifierInfo(II);
2186 }
2187 return Tok;
2188}
2189
2190MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2191 BitstreamCursor &Stream = F.MacroCursor;
2192
2193 // Keep track of where we are in the stream, then jump back there
2194 // after reading this macro.
2195 SavedStreamPosition SavedPosition(Stream);
2196
2197 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2198 // FIXME this drops errors on the floor.
2199 consumeError(Err: std::move(Err));
2200 return nullptr;
2201 }
2202 RecordData Record;
2203 SmallVector<IdentifierInfo*, 16> MacroParams;
2204 MacroInfo *Macro = nullptr;
2205 llvm::MutableArrayRef<Token> MacroTokens;
2206
2207 while (true) {
2208 // Advance to the next record, but if we get to the end of the block, don't
2209 // pop it (removing all the abbreviations from the cursor) since we want to
2210 // be able to reseek within the block and read entries.
2211 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2212 Expected<llvm::BitstreamEntry> MaybeEntry =
2213 Stream.advanceSkippingSubblocks(Flags);
2214 if (!MaybeEntry) {
2215 Error(Err: MaybeEntry.takeError());
2216 return Macro;
2217 }
2218 llvm::BitstreamEntry Entry = MaybeEntry.get();
2219
2220 switch (Entry.Kind) {
2221 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2222 case llvm::BitstreamEntry::Error:
2223 Error(Msg: "malformed block record in AST file");
2224 return Macro;
2225 case llvm::BitstreamEntry::EndBlock:
2226 return Macro;
2227 case llvm::BitstreamEntry::Record:
2228 // The interesting case.
2229 break;
2230 }
2231
2232 // Read a record.
2233 Record.clear();
2234 PreprocessorRecordTypes RecType;
2235 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2236 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2237 else {
2238 Error(Err: MaybeRecType.takeError());
2239 return Macro;
2240 }
2241 switch (RecType) {
2242 case PP_MODULE_MACRO:
2243 case PP_MACRO_DIRECTIVE_HISTORY:
2244 return Macro;
2245
2246 case PP_MACRO_OBJECT_LIKE:
2247 case PP_MACRO_FUNCTION_LIKE: {
2248 // If we already have a macro, that means that we've hit the end
2249 // of the definition of the macro we were looking for. We're
2250 // done.
2251 if (Macro)
2252 return Macro;
2253
2254 unsigned NextIndex = 1; // Skip identifier ID.
2255 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2256 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2257 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2258 MI->setIsUsed(Record[NextIndex++]);
2259 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2260 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2261 PPAllocator&: PP.getPreprocessorAllocator());
2262 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2263 // Decode function-like macro info.
2264 bool isC99VarArgs = Record[NextIndex++];
2265 bool isGNUVarArgs = Record[NextIndex++];
2266 bool hasCommaPasting = Record[NextIndex++];
2267 MacroParams.clear();
2268 unsigned NumArgs = Record[NextIndex++];
2269 for (unsigned i = 0; i != NumArgs; ++i)
2270 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2271
2272 // Install function-like macro info.
2273 MI->setIsFunctionLike();
2274 if (isC99VarArgs) MI->setIsC99Varargs();
2275 if (isGNUVarArgs) MI->setIsGNUVarargs();
2276 if (hasCommaPasting) MI->setHasCommaPasting();
2277 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2278 }
2279
2280 // Remember that we saw this macro last so that we add the tokens that
2281 // form its body to it.
2282 Macro = MI;
2283
2284 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2285 Record[NextIndex]) {
2286 // We have a macro definition. Register the association
2287 PreprocessedEntityID
2288 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2289 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2290 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2291 PreprocessingRecord::PPEntityID PPID =
2292 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2293 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2294 Val: PPRec.getPreprocessedEntity(PPID));
2295 if (PPDef)
2296 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2297 }
2298
2299 ++NumMacrosRead;
2300 break;
2301 }
2302
2303 case PP_TOKEN: {
2304 // If we see a TOKEN before a PP_MACRO_*, then the file is
2305 // erroneous, just pretend we didn't see this.
2306 if (!Macro) break;
2307 if (MacroTokens.empty()) {
2308 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2309 return Macro;
2310 }
2311
2312 unsigned Idx = 0;
2313 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2314 MacroTokens = MacroTokens.drop_front();
2315 break;
2316 }
2317 }
2318 }
2319}
2320
2321PreprocessedEntityID
2322ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2323 PreprocessedEntityID LocalID) const {
2324 if (!M.ModuleOffsetMap.empty())
2325 ReadModuleOffsetMap(F&: M);
2326
2327 unsigned ModuleFileIndex = LocalID >> 32;
2328 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2329 ModuleFile *MF =
2330 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2331 assert(MF && "malformed identifier ID encoding?");
2332
2333 if (!ModuleFileIndex) {
2334 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2335 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2336 }
2337
2338 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2339}
2340
2341OptionalFileEntryRef
2342HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2343 FileManager &FileMgr = Reader.getFileManager();
2344 if (!Key.Imported)
2345 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2346
2347 auto Resolved =
2348 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2349 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2350}
2351
2352unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2353 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2354 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2355 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2356 return llvm::xxh3_64bits(data: buf);
2357}
2358
2359HeaderFileInfoTrait::internal_key_type
2360HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2361 internal_key_type ikey = {.Size: ekey.getSize(),
2362 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2363 .Filename: ekey.getName(), /*Imported*/ false};
2364 return ikey;
2365}
2366
2367bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2368 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2369 return false;
2370
2371 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2372 return true;
2373
2374 // Determine whether the actual files are equivalent.
2375 OptionalFileEntryRef FEA = getFile(Key: a);
2376 OptionalFileEntryRef FEB = getFile(Key: b);
2377 return FEA && FEA == FEB;
2378}
2379
2380std::pair<unsigned, unsigned>
2381HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2382 return readULEBKeyDataLength(P&: d);
2383}
2384
2385HeaderFileInfoTrait::internal_key_type
2386HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2387 using namespace llvm::support;
2388
2389 internal_key_type ikey;
2390 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2391 ikey.ModTime =
2392 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2393 ikey.Filename = (const char *)d;
2394 ikey.Imported = true;
2395 return ikey;
2396}
2397
2398HeaderFileInfoTrait::data_type
2399HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2400 unsigned DataLen) {
2401 using namespace llvm::support;
2402
2403 const unsigned char *End = d + DataLen;
2404 HeaderFileInfo HFI;
2405 unsigned Flags = *d++;
2406
2407 OptionalFileEntryRef FE;
2408 bool Included = (Flags >> 6) & 0x01;
2409 if (Included)
2410 if ((FE = getFile(Key: key)))
2411 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2412 // deserialize this header file info again.
2413 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2414
2415 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2416 HFI.isImport |= (Flags >> 5) & 0x01;
2417 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2418 HFI.DirInfo = (Flags >> 1) & 0x07;
2419 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2420 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2421
2422 assert((End - d) % 4 == 0 &&
2423 "Wrong data length in HeaderFileInfo deserialization");
2424 while (d != End) {
2425 uint32_t LocalSMID =
2426 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2427 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2428 LocalSMID >>= 3;
2429
2430 // This header is part of a module. Associate it with the module to enable
2431 // implicit module import.
2432 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2433 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2434 ModuleMap &ModMap =
2435 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2436
2437 if (FE || (FE = getFile(Key: key))) {
2438 // FIXME: NameAsWritten
2439 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2440 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2441 }
2442 HFI.mergeModuleMembership(Role: HeaderRole);
2443 }
2444
2445 // This HeaderFileInfo was externally loaded.
2446 HFI.External = true;
2447 HFI.IsValid = true;
2448 return HFI;
2449}
2450
2451void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2452 uint32_t MacroDirectivesOffset) {
2453 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2454 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2455}
2456
2457void ASTReader::ReadDefinedMacros() {
2458 // Note that we are loading defined macros.
2459 Deserializing Macros(this);
2460
2461 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2462 BitstreamCursor &MacroCursor = I.MacroCursor;
2463
2464 // If there was no preprocessor block, skip this file.
2465 if (MacroCursor.getBitcodeBytes().empty())
2466 continue;
2467
2468 BitstreamCursor Cursor = MacroCursor;
2469 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2470 Error(Err: std::move(Err));
2471 return;
2472 }
2473
2474 RecordData Record;
2475 while (true) {
2476 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2477 if (!MaybeE) {
2478 Error(Err: MaybeE.takeError());
2479 return;
2480 }
2481 llvm::BitstreamEntry E = MaybeE.get();
2482
2483 switch (E.Kind) {
2484 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2485 case llvm::BitstreamEntry::Error:
2486 Error(Msg: "malformed block record in AST file");
2487 return;
2488 case llvm::BitstreamEntry::EndBlock:
2489 goto NextCursor;
2490
2491 case llvm::BitstreamEntry::Record: {
2492 Record.clear();
2493 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2494 if (!MaybeRecord) {
2495 Error(Err: MaybeRecord.takeError());
2496 return;
2497 }
2498 switch (MaybeRecord.get()) {
2499 default: // Default behavior: ignore.
2500 break;
2501
2502 case PP_MACRO_OBJECT_LIKE:
2503 case PP_MACRO_FUNCTION_LIKE: {
2504 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2505 if (II->isOutOfDate())
2506 updateOutOfDateIdentifier(II: *II);
2507 break;
2508 }
2509
2510 case PP_TOKEN:
2511 // Ignore tokens.
2512 break;
2513 }
2514 break;
2515 }
2516 }
2517 }
2518 NextCursor: ;
2519 }
2520}
2521
2522namespace {
2523
2524 /// Visitor class used to look up identifirs in an AST file.
2525 class IdentifierLookupVisitor {
2526 StringRef Name;
2527 unsigned NameHash;
2528 unsigned PriorGeneration;
2529 unsigned &NumIdentifierLookups;
2530 unsigned &NumIdentifierLookupHits;
2531 IdentifierInfo *Found = nullptr;
2532
2533 public:
2534 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2535 unsigned &NumIdentifierLookups,
2536 unsigned &NumIdentifierLookupHits)
2537 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2538 PriorGeneration(PriorGeneration),
2539 NumIdentifierLookups(NumIdentifierLookups),
2540 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2541
2542 bool operator()(ModuleFile &M) {
2543 // If we've already searched this module file, skip it now.
2544 if (M.Generation <= PriorGeneration)
2545 return true;
2546
2547 ASTIdentifierLookupTable *IdTable
2548 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2549 if (!IdTable)
2550 return false;
2551
2552 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2553 Found);
2554 ++NumIdentifierLookups;
2555 ASTIdentifierLookupTable::iterator Pos =
2556 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2557 if (Pos == IdTable->end())
2558 return false;
2559
2560 // Dereferencing the iterator has the effect of building the
2561 // IdentifierInfo node and populating it with the various
2562 // declarations it needs.
2563 ++NumIdentifierLookupHits;
2564 Found = *Pos;
2565 if (Trait.hasMoreInformationInDependencies()) {
2566 // Look for the identifier in extra modules as they contain more info.
2567 return false;
2568 }
2569 return true;
2570 }
2571
2572 // Retrieve the identifier info found within the module
2573 // files.
2574 IdentifierInfo *getIdentifierInfo() const { return Found; }
2575 };
2576
2577} // namespace
2578
2579void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2580 // Note that we are loading an identifier.
2581 Deserializing AnIdentifier(this);
2582
2583 unsigned PriorGeneration = 0;
2584 if (getContext().getLangOpts().Modules)
2585 PriorGeneration = IdentifierGeneration[&II];
2586
2587 // If there is a global index, look there first to determine which modules
2588 // provably do not have any results for this identifier.
2589 GlobalModuleIndex::HitSet Hits;
2590 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2591 if (!loadGlobalIndex()) {
2592 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2593 HitsPtr = &Hits;
2594 }
2595 }
2596
2597 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2598 NumIdentifierLookups,
2599 NumIdentifierLookupHits);
2600 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2601 markIdentifierUpToDate(II: &II);
2602}
2603
2604void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2605 if (!II)
2606 return;
2607
2608 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2609
2610 // Update the generation for this identifier.
2611 if (getContext().getLangOpts().Modules)
2612 IdentifierGeneration[II] = getGeneration();
2613}
2614
2615MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2616 unsigned &Idx) {
2617 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2618 uint64_t LocalIndex = Record[Idx++];
2619 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2620}
2621
2622void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2623 const PendingMacroInfo &PMInfo) {
2624 ModuleFile &M = *PMInfo.M;
2625
2626 BitstreamCursor &Cursor = M.MacroCursor;
2627 SavedStreamPosition SavedPosition(Cursor);
2628 if (llvm::Error Err =
2629 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2630 Error(Err: std::move(Err));
2631 return;
2632 }
2633
2634 struct ModuleMacroRecord {
2635 SubmoduleID SubModID;
2636 MacroInfo *MI;
2637 SmallVector<SubmoduleID, 8> Overrides;
2638 };
2639 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2640
2641 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2642 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2643 // macro histroy.
2644 RecordData Record;
2645 while (true) {
2646 Expected<llvm::BitstreamEntry> MaybeEntry =
2647 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2648 if (!MaybeEntry) {
2649 Error(Err: MaybeEntry.takeError());
2650 return;
2651 }
2652 llvm::BitstreamEntry Entry = MaybeEntry.get();
2653
2654 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2655 Error(Msg: "malformed block record in AST file");
2656 return;
2657 }
2658
2659 Record.clear();
2660 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2661 if (!MaybePP) {
2662 Error(Err: MaybePP.takeError());
2663 return;
2664 }
2665 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2666 case PP_MACRO_DIRECTIVE_HISTORY:
2667 break;
2668
2669 case PP_MODULE_MACRO: {
2670 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2671 auto &Info = ModuleMacros.back();
2672 unsigned Idx = 0;
2673 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2674 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2675 for (int I = Idx, N = Record.size(); I != N; ++I)
2676 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2677 continue;
2678 }
2679
2680 default:
2681 Error(Msg: "malformed block record in AST file");
2682 return;
2683 }
2684
2685 // We found the macro directive history; that's the last record
2686 // for this macro.
2687 break;
2688 }
2689
2690 // Module macros are listed in reverse dependency order.
2691 {
2692 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2693 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2694 for (auto &MMR : ModuleMacros) {
2695 Overrides.clear();
2696 for (unsigned ModID : MMR.Overrides) {
2697 Module *Mod = getSubmodule(GlobalID: ModID);
2698 auto *Macro = PP.getModuleMacro(Mod, II);
2699 assert(Macro && "missing definition for overridden macro");
2700 Overrides.push_back(Elt: Macro);
2701 }
2702
2703 bool Inserted = false;
2704 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2705 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2706 }
2707 }
2708
2709 // Don't read the directive history for a module; we don't have anywhere
2710 // to put it.
2711 if (M.isModule())
2712 return;
2713
2714 // Deserialize the macro directives history in reverse source-order.
2715 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2716 unsigned Idx = 0, N = Record.size();
2717 while (Idx < N) {
2718 MacroDirective *MD = nullptr;
2719 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2720 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2721 switch (K) {
2722 case MacroDirective::MD_Define: {
2723 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2724 MD = PP.AllocateDefMacroDirective(MI, Loc);
2725 break;
2726 }
2727 case MacroDirective::MD_Undefine:
2728 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2729 break;
2730 case MacroDirective::MD_Visibility:
2731 bool isPublic = Record[Idx++];
2732 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2733 break;
2734 }
2735
2736 if (!Latest)
2737 Latest = MD;
2738 if (Earliest)
2739 Earliest->setPrevious(MD);
2740 Earliest = MD;
2741 }
2742
2743 if (Latest)
2744 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2745}
2746
2747bool ASTReader::shouldDisableValidationForFile(
2748 const serialization::ModuleFile &M) const {
2749 if (DisableValidationKind == DisableValidationForModuleKind::None)
2750 return false;
2751
2752 // If a PCH is loaded and validation is disabled for PCH then disable
2753 // validation for the PCH and the modules it loads.
2754 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2755
2756 switch (K) {
2757 case MK_MainFile:
2758 case MK_Preamble:
2759 case MK_PCH:
2760 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2761 case MK_ImplicitModule:
2762 case MK_ExplicitModule:
2763 case MK_PrebuiltModule:
2764 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2765 }
2766
2767 return false;
2768}
2769
2770static std::pair<StringRef, StringRef>
2771getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2772 const StringRef InputBlob) {
2773 uint16_t AsRequestedLength = Record[7];
2774 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2775 InputBlob.substr(Start: AsRequestedLength)};
2776}
2777
2778InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2779 // If this ID is bogus, just return an empty input file.
2780 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2781 return InputFileInfo();
2782
2783 // If we've already loaded this input file, return it.
2784 if (F.InputFileInfosLoaded[ID - 1].isValid())
2785 return F.InputFileInfosLoaded[ID - 1];
2786
2787 // Go find this input file.
2788 BitstreamCursor &Cursor = F.InputFilesCursor;
2789 SavedStreamPosition SavedPosition(Cursor);
2790 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2791 F.InputFileOffsets[ID - 1])) {
2792 // FIXME this drops errors on the floor.
2793 consumeError(Err: std::move(Err));
2794 }
2795
2796 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2797 if (!MaybeCode) {
2798 // FIXME this drops errors on the floor.
2799 consumeError(Err: MaybeCode.takeError());
2800 }
2801 unsigned Code = MaybeCode.get();
2802 RecordData Record;
2803 StringRef Blob;
2804
2805 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2806 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2807 "invalid record type for input file");
2808 else {
2809 // FIXME this drops errors on the floor.
2810 consumeError(Err: Maybe.takeError());
2811 }
2812
2813 assert(Record[0] == ID && "Bogus stored ID or offset");
2814 InputFileInfo R;
2815 R.StoredSize = static_cast<off_t>(Record[1]);
2816 R.StoredTime = static_cast<time_t>(Record[2]);
2817 R.Overridden = static_cast<bool>(Record[3]);
2818 R.Transient = static_cast<bool>(Record[4]);
2819 R.TopLevel = static_cast<bool>(Record[5]);
2820 R.ModuleMap = static_cast<bool>(Record[6]);
2821 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2822 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2823 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2824 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2825 ? UnresolvedFilenameAsRequested
2826 : UnresolvedFilename;
2827
2828 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2829 if (!MaybeEntry) // FIXME this drops errors on the floor.
2830 consumeError(Err: MaybeEntry.takeError());
2831 llvm::BitstreamEntry Entry = MaybeEntry.get();
2832 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2833 "expected record type for input file hash");
2834
2835 Record.clear();
2836 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2837 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2838 "invalid record type for input file hash");
2839 else {
2840 // FIXME this drops errors on the floor.
2841 consumeError(Err: Maybe.takeError());
2842 }
2843 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2844 static_cast<uint64_t>(Record[0]);
2845
2846 // Note that we've loaded this input file info.
2847 F.InputFileInfosLoaded[ID - 1] = R;
2848 return R;
2849}
2850
2851static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2852InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2853 // If this ID is bogus, just return an empty input file.
2854 if (ID == 0 || ID > F.InputFilesLoaded.size())
2855 return InputFile();
2856
2857 // If we've already loaded this input file, return it.
2858 if (F.InputFilesLoaded[ID-1].getFile())
2859 return F.InputFilesLoaded[ID-1];
2860
2861 if (F.InputFilesLoaded[ID-1].isNotFound())
2862 return InputFile();
2863
2864 // Go find this input file.
2865 BitstreamCursor &Cursor = F.InputFilesCursor;
2866 SavedStreamPosition SavedPosition(Cursor);
2867 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2868 F.InputFileOffsets[ID - 1])) {
2869 // FIXME this drops errors on the floor.
2870 consumeError(Err: std::move(Err));
2871 }
2872
2873 InputFileInfo FI = getInputFileInfo(F, ID);
2874 off_t StoredSize = FI.StoredSize;
2875 time_t StoredTime = FI.StoredTime;
2876 bool Overridden = FI.Overridden;
2877 bool Transient = FI.Transient;
2878 auto Filename =
2879 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2880 uint64_t StoredContentHash = FI.ContentHash;
2881
2882 // For standard C++ modules, we don't need to check the inputs.
2883 bool SkipChecks = F.StandardCXXModule;
2884
2885 const HeaderSearchOptions &HSOpts =
2886 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2887
2888 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2889 // modules.
2890 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2891 SkipChecks = false;
2892 Overridden = false;
2893 }
2894
2895 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2896
2897 // For an overridden file, create a virtual file with the stored
2898 // size/timestamp.
2899 if ((Overridden || Transient || SkipChecks) && !File)
2900 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2901
2902 if (!File) {
2903 if (Complain) {
2904 std::string ErrorStr = "could not find file '";
2905 ErrorStr += *Filename;
2906 ErrorStr += "' referenced by AST file '";
2907 ErrorStr += F.FileName.str();
2908 ErrorStr += "'";
2909 Error(Msg: ErrorStr);
2910 }
2911 // Record that we didn't find the file.
2912 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2913 return InputFile();
2914 }
2915
2916 // Check if there was a request to override the contents of the file
2917 // that was part of the precompiled header. Overriding such a file
2918 // can lead to problems when lexing using the source locations from the
2919 // PCH.
2920 SourceManager &SM = getSourceManager();
2921 // FIXME: Reject if the overrides are different.
2922 if ((!Overridden && !Transient) && !SkipChecks &&
2923 SM.isFileOverridden(File: *File)) {
2924 if (Complain)
2925 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2926
2927 // After emitting the diagnostic, bypass the overriding file to recover
2928 // (this creates a separate FileEntry).
2929 File = SM.bypassFileContentsOverride(File: *File);
2930 if (!File) {
2931 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2932 return InputFile();
2933 }
2934 }
2935
2936 auto HasInputContentChanged = [&](Change OriginalChange) {
2937 assert(ValidateASTInputFilesContent &&
2938 "We should only check the content of the inputs with "
2939 "ValidateASTInputFilesContent enabled.");
2940
2941 if (StoredContentHash == 0)
2942 return OriginalChange;
2943
2944 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2945 if (!MemBuffOrError) {
2946 if (!Complain)
2947 return OriginalChange;
2948 std::string ErrorStr = "could not get buffer for file '";
2949 ErrorStr += File->getName();
2950 ErrorStr += "'";
2951 Error(Msg: ErrorStr);
2952 return OriginalChange;
2953 }
2954
2955 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2956 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2957 return Change{.Kind: Change::None};
2958
2959 return Change{.Kind: Change::Content};
2960 };
2961 auto HasInputFileChanged = [&]() {
2962 if (StoredSize != File->getSize())
2963 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2964 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2965 StoredTime != File->getModificationTime()) {
2966 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2967 .New: File->getModificationTime()};
2968
2969 // In case the modification time changes but not the content,
2970 // accept the cached file as legit.
2971 if (ValidateASTInputFilesContent)
2972 return HasInputContentChanged(MTimeChange);
2973
2974 return MTimeChange;
2975 }
2976 return Change{.Kind: Change::None};
2977 };
2978
2979 bool IsOutOfDate = false;
2980 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2981 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2982 // enabled, it is better to check the contents of the inputs. Since we can't
2983 // get correct modified time information for inputs from overriden inputs.
2984 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2985 F.StandardCXXModule && FileChange.Kind == Change::None)
2986 FileChange = HasInputContentChanged(FileChange);
2987
2988 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2989 // it is better to check the content of the input files because we cannot rely
2990 // on the file modification time, which will be the same (zero) for these
2991 // files.
2992 if (!StoredTime && ValidateASTInputFilesContent &&
2993 FileChange.Kind == Change::None)
2994 FileChange = HasInputContentChanged(FileChange);
2995
2996 // For an overridden file, there is nothing to validate.
2997 if (!Overridden && FileChange.Kind != Change::None) {
2998 if (Complain) {
2999 // Build a list of the PCH imports that got us here (in reverse).
3000 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
3001 while (!ImportStack.back()->ImportedBy.empty())
3002 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
3003
3004 // The top-level AST file is stale.
3005 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
3006 Diag(DiagID: diag::err_fe_ast_file_modified)
3007 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
3008 << TopLevelASTFileName;
3009 Diag(DiagID: diag::note_fe_ast_file_modified)
3010 << FileChange.Kind << (FileChange.Old && FileChange.New)
3011 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
3012 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3013 if (getModuleManager()
3014 .getModuleCache()
3015 .getInMemoryModuleCache()
3016 .isPCMFinal(Filename: F.FileName))
3017 Diag(DiagID: diag::note_fe_ast_file_modified_finalized) << F.ModuleName;
3018
3019 // Print the import stack.
3020 if (ImportStack.size() > 1) {
3021 Diag(DiagID: diag::note_ast_file_required_by)
3022 << *Filename << ImportStack[0]->FileName;
3023 for (unsigned I = 1; I < ImportStack.size(); ++I)
3024 Diag(DiagID: diag::note_ast_file_required_by)
3025 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3026 }
3027
3028 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3029 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3030 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3031 << F.InputFilesValidationStatus;
3032 }
3033
3034 IsOutOfDate = true;
3035 }
3036 // FIXME: If the file is overridden and we've already opened it,
3037 // issue an error (or split it into a separate FileEntry).
3038
3039 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3040
3041 // Note that we've loaded this input file.
3042 F.InputFilesLoaded[ID-1] = IF;
3043 return IF;
3044}
3045
3046ASTReader::TemporarilyOwnedStringRef
3047ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3048 ModuleFile &ModF) {
3049 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3050}
3051
3052ASTReader::TemporarilyOwnedStringRef
3053ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3054 StringRef Prefix) {
3055 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3056
3057 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3058 Path == "<built-in>" || Path == "<command line>")
3059 return {Path, Buf};
3060
3061 Buf.clear();
3062 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3063 StringRef ResolvedPath{Buf.data(), Buf.size()};
3064 return {ResolvedPath, Buf};
3065}
3066
3067std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3068 StringRef P,
3069 ModuleFile &ModF) {
3070 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3071}
3072
3073std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3074 StringRef P,
3075 StringRef Prefix) {
3076 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3077 return ResolvedPath->str();
3078}
3079
3080static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3081 switch (ARR) {
3082 case ASTReader::Failure: return true;
3083 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3084 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3085 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3086 case ASTReader::ConfigurationMismatch:
3087 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3088 case ASTReader::HadErrors: return true;
3089 case ASTReader::Success: return false;
3090 }
3091
3092 llvm_unreachable("unknown ASTReadResult");
3093}
3094
3095ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3096 BitstreamCursor &Stream, StringRef Filename,
3097 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3098 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3099 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3100 // FIXME this drops errors on the floor.
3101 consumeError(Err: std::move(Err));
3102 return Failure;
3103 }
3104
3105 // Read all of the records in the options block.
3106 RecordData Record;
3107 ASTReadResult Result = Success;
3108 while (true) {
3109 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3110 if (!MaybeEntry) {
3111 // FIXME this drops errors on the floor.
3112 consumeError(Err: MaybeEntry.takeError());
3113 return Failure;
3114 }
3115 llvm::BitstreamEntry Entry = MaybeEntry.get();
3116
3117 switch (Entry.Kind) {
3118 case llvm::BitstreamEntry::Error:
3119 case llvm::BitstreamEntry::SubBlock:
3120 return Failure;
3121
3122 case llvm::BitstreamEntry::EndBlock:
3123 return Result;
3124
3125 case llvm::BitstreamEntry::Record:
3126 // The interesting case.
3127 break;
3128 }
3129
3130 // Read and process a record.
3131 Record.clear();
3132 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3133 if (!MaybeRecordType) {
3134 // FIXME this drops errors on the floor.
3135 consumeError(Err: MaybeRecordType.takeError());
3136 return Failure;
3137 }
3138 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3139 case LANGUAGE_OPTIONS: {
3140 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3141 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3142 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3143 Result = ConfigurationMismatch;
3144 break;
3145 }
3146
3147 case CODEGEN_OPTIONS: {
3148 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3149 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3150 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3151 Result = ConfigurationMismatch;
3152 break;
3153 }
3154
3155 case TARGET_OPTIONS: {
3156 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3157 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3158 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3159 Result = ConfigurationMismatch;
3160 break;
3161 }
3162
3163 case FILE_SYSTEM_OPTIONS: {
3164 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3165 if (!AllowCompatibleConfigurationMismatch &&
3166 ParseFileSystemOptions(Record, Complain, Listener))
3167 Result = ConfigurationMismatch;
3168 break;
3169 }
3170
3171 case HEADER_SEARCH_OPTIONS: {
3172 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3173 if (!AllowCompatibleConfigurationMismatch &&
3174 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3175 Result = ConfigurationMismatch;
3176 break;
3177 }
3178
3179 case PREPROCESSOR_OPTIONS:
3180 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3181 if (!AllowCompatibleConfigurationMismatch &&
3182 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3183 SuggestedPredefines))
3184 Result = ConfigurationMismatch;
3185 break;
3186 }
3187 }
3188}
3189
3190/// Returns {build-session validation applies, MF was validated this session}.
3191static std::pair<bool, bool>
3192wasValidatedInBuildSession(const ModuleFile &MF,
3193 const HeaderSearchOptions &HSOpts) {
3194 const bool EnablesBSValidation =
3195 HSOpts.ModulesValidateOncePerBuildSession && MF.Kind == MK_ImplicitModule;
3196 const bool WasValidated =
3197 EnablesBSValidation &&
3198 MF.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp;
3199 return {EnablesBSValidation, WasValidated};
3200}
3201
3202ASTReader::RelocationResult
3203ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3204 // Don't emit module relocation errors if we have -fno-validate-pch.
3205 const bool IgnoreError =
3206 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3207 DisableValidationForModuleKind::Module);
3208
3209 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3210 return {std::nullopt, IgnoreError};
3211
3212 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3213
3214 if (!DirectoryCheck &&
3215 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3216 return {std::nullopt, IgnoreError};
3217
3218 const HeaderSearchOptions &HSOpts =
3219 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3220
3221 // When only validating modules once per build session,
3222 // Skip check if the timestamp is up to date or module was built in same build
3223 // session.
3224 auto [EnablesBSValidation, WasValidated] =
3225 wasValidatedInBuildSession(MF: F, HSOpts);
3226 if (WasValidated)
3227 return {std::nullopt, IgnoreError};
3228 if (EnablesBSValidation &&
3229 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
3230 return {std::nullopt, IgnoreError};
3231
3232 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3233
3234 // If we've already loaded a module map file covering this module, we may
3235 // have a better path for it (relative to the current build if doing directory
3236 // check).
3237 Module *M = PP.getHeaderSearchInfo().lookupModule(
3238 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3239 /*AllowSearch=*/DirectoryCheck,
3240 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3241
3242 return {M, IgnoreError};
3243}
3244
3245ASTReader::ASTReadResult
3246ASTReader::ReadControlBlock(ModuleFile &F,
3247 SmallVectorImpl<ImportedModule> &Loaded,
3248 const ModuleFile *ImportedBy,
3249 unsigned ClientLoadCapabilities) {
3250 BitstreamCursor &Stream = F.Stream;
3251
3252 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3253 Error(Err: std::move(Err));
3254 return Failure;
3255 }
3256
3257 // Lambda to read the unhashed control block the first time it's called.
3258 //
3259 // For PCM files, the unhashed control block cannot be read until after the
3260 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3261 // need to look ahead before reading the IMPORTS record. For consistency,
3262 // this block is always read somehow (see BitstreamEntry::EndBlock).
3263 bool HasReadUnhashedControlBlock = false;
3264 auto readUnhashedControlBlockOnce = [&]() {
3265 if (!HasReadUnhashedControlBlock) {
3266 HasReadUnhashedControlBlock = true;
3267 if (ASTReadResult Result =
3268 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3269 return Result;
3270 }
3271 return Success;
3272 };
3273
3274 bool DisableValidation = shouldDisableValidationForFile(M: F);
3275
3276 // Read all of the records and blocks in the control block.
3277 RecordData Record;
3278 unsigned NumInputs = 0;
3279 unsigned NumUserInputs = 0;
3280 StringRef BaseDirectoryAsWritten;
3281 while (true) {
3282 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3283 if (!MaybeEntry) {
3284 Error(Err: MaybeEntry.takeError());
3285 return Failure;
3286 }
3287 llvm::BitstreamEntry Entry = MaybeEntry.get();
3288
3289 switch (Entry.Kind) {
3290 case llvm::BitstreamEntry::Error:
3291 Error(Msg: "malformed block record in AST file");
3292 return Failure;
3293 case llvm::BitstreamEntry::EndBlock: {
3294 // Validate the module before returning. This call catches an AST with
3295 // no module name and no imports.
3296 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3297 return Result;
3298
3299 // Validate input files.
3300 const HeaderSearchOptions &HSOpts =
3301 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3302
3303 // All user input files reside at the index range [0, NumUserInputs), and
3304 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3305 // loaded module files, ignore missing inputs.
3306 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3307 F.Kind != MK_PrebuiltModule) {
3308 bool Complain =
3309 !canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities);
3310
3311 // If we are reading a module, we will create a verification timestamp,
3312 // so we verify all input files. Otherwise, verify only user input
3313 // files.
3314
3315 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3316 F.InputFilesValidationStatus = ValidateSystemInputs
3317 ? InputFilesValidation::AllFiles
3318 : InputFilesValidation::UserFiles;
3319 auto [_, WasValidated] = wasValidatedInBuildSession(MF: F, HSOpts);
3320 if (WasValidated) {
3321 N = ForceValidateUserInputs ? NumUserInputs : 0;
3322 F.InputFilesValidationStatus =
3323 ForceValidateUserInputs
3324 ? InputFilesValidation::UserFiles
3325 : InputFilesValidation::SkippedInBuildSession;
3326 }
3327
3328 if (N != 0)
3329 Diag(DiagID: diag::remark_module_validation)
3330 << N << F.ModuleName << F.FileName;
3331
3332 for (unsigned I = 0; I < N; ++I) {
3333 InputFile IF = getInputFile(F, ID: I+1, Complain);
3334 if (!IF.getFile() || IF.isOutOfDate())
3335 return OutOfDate;
3336 }
3337 } else {
3338 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3339 }
3340
3341 if (Listener)
3342 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind, DirectlyImported: F.isDirectlyImported());
3343
3344 if (Listener && Listener->needsInputFileVisitation()) {
3345 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3346 : NumUserInputs;
3347 for (unsigned I = 0; I < N; ++I) {
3348 bool IsSystem = I >= NumUserInputs;
3349 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3350 auto FilenameAsRequested = ResolveImportedPath(
3351 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3352 Listener->visitInputFile(
3353 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3354 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3355 }
3356 }
3357
3358 return Success;
3359 }
3360
3361 case llvm::BitstreamEntry::SubBlock:
3362 switch (Entry.ID) {
3363 case INPUT_FILES_BLOCK_ID:
3364 F.InputFilesCursor = Stream;
3365 if (llvm::Error Err = Stream.SkipBlock()) {
3366 Error(Err: std::move(Err));
3367 return Failure;
3368 }
3369 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3370 Error(Msg: "malformed block record in AST file");
3371 return Failure;
3372 }
3373 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3374 continue;
3375
3376 case OPTIONS_BLOCK_ID:
3377 // If we're reading the first module for this group, check its options
3378 // are compatible with ours. For modules it imports, no further checking
3379 // is required, because we checked them when we built it.
3380 if (Listener && !ImportedBy) {
3381 // Should we allow the configuration of the module file to differ from
3382 // the configuration of the current translation unit in a compatible
3383 // way?
3384 //
3385 // FIXME: Allow this for files explicitly specified with -include-pch.
3386 bool AllowCompatibleConfigurationMismatch =
3387 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3388
3389 ASTReadResult Result =
3390 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3391 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3392 SuggestedPredefines);
3393 if (Result == Failure) {
3394 Error(Msg: "malformed block record in AST file");
3395 return Result;
3396 }
3397
3398 if (DisableValidation ||
3399 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3400 Result = Success;
3401
3402 // If we can't load the module, exit early since we likely
3403 // will rebuild the module anyway. The stream may be in the
3404 // middle of a block.
3405 if (Result != Success)
3406 return Result;
3407 } else if (llvm::Error Err = Stream.SkipBlock()) {
3408 Error(Err: std::move(Err));
3409 return Failure;
3410 }
3411 continue;
3412
3413 default:
3414 if (llvm::Error Err = Stream.SkipBlock()) {
3415 Error(Err: std::move(Err));
3416 return Failure;
3417 }
3418 continue;
3419 }
3420
3421 case llvm::BitstreamEntry::Record:
3422 // The interesting case.
3423 break;
3424 }
3425
3426 // Read and process a record.
3427 Record.clear();
3428 StringRef Blob;
3429 Expected<unsigned> MaybeRecordType =
3430 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3431 if (!MaybeRecordType) {
3432 Error(Err: MaybeRecordType.takeError());
3433 return Failure;
3434 }
3435 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3436 case METADATA: {
3437 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3438 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3439 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3440 : diag::err_ast_file_version_too_new)
3441 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3442 return VersionMismatch;
3443 }
3444
3445 bool hasErrors = Record[7];
3446 if (hasErrors && !DisableValidation) {
3447 // If requested by the caller and the module hasn't already been read
3448 // or compiled, mark modules on error as out-of-date.
3449 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3450 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3451 return OutOfDate;
3452
3453 if (!AllowASTWithCompilerErrors) {
3454 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3455 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3456 return HadErrors;
3457 }
3458 }
3459 if (hasErrors) {
3460 Diags.ErrorOccurred = true;
3461 Diags.UncompilableErrorOccurred = true;
3462 Diags.UnrecoverableErrorOccurred = true;
3463 }
3464
3465 F.RelocatablePCH = Record[4];
3466 // Relative paths in a relocatable PCH are relative to our sysroot.
3467 if (F.RelocatablePCH)
3468 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3469
3470 F.StandardCXXModule = Record[5];
3471
3472 F.HasTimestamps = Record[6];
3473
3474 const std::string &CurBranch = getClangFullRepositoryVersion();
3475 StringRef ASTBranch = Blob;
3476 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3477 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3478 Diag(DiagID: diag::err_ast_file_different_branch)
3479 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3480 << CurBranch;
3481 return VersionMismatch;
3482 }
3483 break;
3484 }
3485
3486 case IMPORT: {
3487 // Validate the AST before processing any imports (otherwise, untangling
3488 // them can be error-prone and expensive). A module will have a name and
3489 // will already have been validated, but this catches the PCH case.
3490 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3491 return Result;
3492
3493 unsigned Idx = 0;
3494 // Read information about the AST file.
3495 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3496
3497 // The import location will be the local one for now; we will adjust
3498 // all import locations of module imports after the global source
3499 // location info are setup, in ReadAST.
3500 auto [ImportLoc, ImportModuleFileIndex] =
3501 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3502 // The import location must belong to the current module file itself.
3503 assert(ImportModuleFileIndex == 0);
3504
3505 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3506
3507 bool IsImportingStdCXXModule = Record[Idx++];
3508
3509 off_t StoredSize = 0;
3510 time_t StoredModTime = 0;
3511 unsigned FileNameKind = 0;
3512 ASTFileSignature StoredSignature;
3513 ModuleFileName ImportedFile;
3514 std::string StoredFile;
3515 bool IgnoreImportedByNote = false;
3516
3517 // For prebuilt and explicit modules first consult the file map for
3518 // an override. Note that here we don't search prebuilt module
3519 // directories if we're not importing standard c++ module, only the
3520 // explicit name to file mappings. Also, we will still verify the
3521 // size/signature making sure it is essentially the same file but
3522 // perhaps in a different location.
3523 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3524 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3525 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3526
3527 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3528 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3529 return Missing;
3530 }
3531
3532 if (!IsImportingStdCXXModule) {
3533 StoredSize = (off_t)Record[Idx++];
3534 StoredModTime = (time_t)Record[Idx++];
3535 FileNameKind = (unsigned)Record[Idx++];
3536
3537 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3538 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3539 Last: SignatureBytes.end());
3540 Blob = Blob.substr(Start: ASTFileSignature::size);
3541
3542 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3543 if (ImportedFile.empty()) {
3544 ImportedFile = ModuleFileName::makeFromRaw(Name: StoredFile, RawKind: FileNameKind);
3545 } else if (!getDiags().isIgnored(
3546 DiagID: diag::warn_module_file_mapping_mismatch,
3547 Loc: CurrentImportLoc)) {
3548 auto ImportedFileRef =
3549 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3550 auto StoredFileRef =
3551 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3552 if ((ImportedFileRef && StoredFileRef) &&
3553 (*ImportedFileRef != *StoredFileRef)) {
3554 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3555 << ImportedFile << StoredFile;
3556 Diag(DiagID: diag::note_module_file_imported_by)
3557 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3558 IgnoreImportedByNote = true;
3559 }
3560 }
3561 }
3562
3563 // If our client can't cope with us being out of date, we can't cope with
3564 // our dependency being missing.
3565 unsigned Capabilities = ClientLoadCapabilities;
3566 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3567 Capabilities &= ~ARR_Missing;
3568
3569 // Load the AST file.
3570 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3571 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3572 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3573
3574 // Check the AST we just read from ImportedFile contains a different
3575 // module than we expected (ImportedName). This can occur for C++20
3576 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3577 if (IsImportingStdCXXModule) {
3578 if (const auto *Imported =
3579 getModuleManager().lookupByFileName(FileName: ImportedFile);
3580 Imported != nullptr && Imported->ModuleName != ImportedName) {
3581 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3582 Result = Missing;
3583 }
3584 }
3585
3586 // If we diagnosed a problem, produce a backtrace.
3587 bool recompilingFinalized = Result == OutOfDate &&
3588 (Capabilities & ARR_OutOfDate) &&
3589 getModuleManager()
3590 .getModuleCache()
3591 .getInMemoryModuleCache()
3592 .isPCMFinal(Filename: F.FileName);
3593 if (!IgnoreImportedByNote &&
3594 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3595 Diag(DiagID: diag::note_module_file_imported_by)
3596 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3597
3598 switch (Result) {
3599 case Failure: return Failure;
3600 // If we have to ignore the dependency, we'll have to ignore this too.
3601 case Missing:
3602 case OutOfDate: return OutOfDate;
3603 case VersionMismatch: return VersionMismatch;
3604 case ConfigurationMismatch: return ConfigurationMismatch;
3605 case HadErrors: return HadErrors;
3606 case Success: break;
3607 }
3608 break;
3609 }
3610
3611 case ORIGINAL_FILE:
3612 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3613 F.ActualOriginalSourceFileName = std::string(Blob);
3614 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3615 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3616 break;
3617
3618 case ORIGINAL_FILE_ID:
3619 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3620 break;
3621
3622 case MODULE_NAME:
3623 F.ModuleName = std::string(Blob);
3624 Diag(DiagID: diag::remark_module_import)
3625 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3626 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3627 if (Listener)
3628 Listener->ReadModuleName(ModuleName: F.ModuleName);
3629
3630 // Validate the AST as soon as we have a name so we can exit early on
3631 // failure.
3632 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3633 return Result;
3634
3635 break;
3636
3637 case MODULE_DIRECTORY: {
3638 // Save the BaseDirectory as written in the PCM for computing the module
3639 // filename for the ModuleCache.
3640 BaseDirectoryAsWritten = Blob;
3641 assert(!F.ModuleName.empty() &&
3642 "MODULE_DIRECTORY found before MODULE_NAME");
3643 F.BaseDirectory = std::string(Blob);
3644
3645 auto [MaybeM, IgnoreError] =
3646 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3647 if (!MaybeM.has_value())
3648 break;
3649
3650 Module *M = MaybeM.value();
3651 if (!M || !M->Directory)
3652 break;
3653 if (IgnoreError) {
3654 F.BaseDirectory = std::string(M->Directory->getName());
3655 break;
3656 }
3657 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3658 break;
3659
3660 // If we're implicitly loading a module, the base directory can't
3661 // change between the build and use.
3662 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3663 if (BuildDir && (*BuildDir == M->Directory)) {
3664 F.BaseDirectory = std::string(M->Directory->getName());
3665 break;
3666 }
3667 Diag(DiagID: diag::remark_module_relocated)
3668 << F.ModuleName << Blob << M->Directory->getName();
3669
3670 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3671 Diag(DiagID: diag::err_imported_module_relocated)
3672 << F.ModuleName << Blob << M->Directory->getName();
3673 return OutOfDate;
3674 }
3675
3676 case MODULE_MAP_FILE:
3677 if (ASTReadResult Result =
3678 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3679 return Result;
3680 break;
3681
3682 case INPUT_FILE_OFFSETS:
3683 NumInputs = Record[0];
3684 NumUserInputs = Record[1];
3685 F.InputFileOffsets =
3686 (const llvm::support::unaligned_uint64_t *)Blob.data();
3687 F.InputFilesLoaded.resize(new_size: NumInputs);
3688 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3689 F.NumUserInputFiles = NumUserInputs;
3690 break;
3691 }
3692 }
3693}
3694
3695llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3696 unsigned ClientLoadCapabilities) {
3697 BitstreamCursor &Stream = F.Stream;
3698
3699 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3700 return Err;
3701 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3702
3703 // Read all of the records and blocks for the AST file.
3704 RecordData Record;
3705 while (true) {
3706 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3707 if (!MaybeEntry)
3708 return MaybeEntry.takeError();
3709 llvm::BitstreamEntry Entry = MaybeEntry.get();
3710
3711 switch (Entry.Kind) {
3712 case llvm::BitstreamEntry::Error:
3713 return llvm::createStringError(
3714 EC: std::errc::illegal_byte_sequence,
3715 Fmt: "error at end of module block in AST file");
3716 case llvm::BitstreamEntry::EndBlock:
3717 // Outside of C++, we do not store a lookup map for the translation unit.
3718 // Instead, mark it as needing a lookup map to be built if this module
3719 // contains any declarations lexically within it (which it always does!).
3720 // This usually has no cost, since we very rarely need the lookup map for
3721 // the translation unit outside C++.
3722 if (ASTContext *Ctx = ContextObj) {
3723 DeclContext *DC = Ctx->getTranslationUnitDecl();
3724 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3725 DC->setMustBuildLookupTable();
3726 }
3727
3728 return llvm::Error::success();
3729 case llvm::BitstreamEntry::SubBlock:
3730 switch (Entry.ID) {
3731 case DECLTYPES_BLOCK_ID:
3732 // We lazily load the decls block, but we want to set up the
3733 // DeclsCursor cursor to point into it. Clone our current bitcode
3734 // cursor to it, enter the block and read the abbrevs in that block.
3735 // With the main cursor, we just skip over it.
3736 F.DeclsCursor = Stream;
3737 if (llvm::Error Err = Stream.SkipBlock())
3738 return Err;
3739 if (llvm::Error Err = ReadBlockAbbrevs(
3740 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3741 return Err;
3742 break;
3743
3744 case PREPROCESSOR_BLOCK_ID:
3745 F.MacroCursor = Stream;
3746 if (!PP.getExternalSource())
3747 PP.setExternalSource(this);
3748
3749 if (llvm::Error Err = Stream.SkipBlock())
3750 return Err;
3751 if (llvm::Error Err =
3752 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3753 return Err;
3754 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3755 break;
3756
3757 case PREPROCESSOR_DETAIL_BLOCK_ID:
3758 F.PreprocessorDetailCursor = Stream;
3759
3760 if (llvm::Error Err = Stream.SkipBlock()) {
3761 return Err;
3762 }
3763 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3764 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3765 return Err;
3766 F.PreprocessorDetailStartOffset
3767 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3768
3769 if (!PP.getPreprocessingRecord())
3770 PP.createPreprocessingRecord();
3771 if (!PP.getPreprocessingRecord()->getExternalSource())
3772 PP.getPreprocessingRecord()->SetExternalSource(*this);
3773 break;
3774
3775 case SOURCE_MANAGER_BLOCK_ID:
3776 if (llvm::Error Err = ReadSourceManagerBlock(F))
3777 return Err;
3778 break;
3779
3780 case SUBMODULE_BLOCK_ID:
3781 F.SubmodulesCursor = Stream;
3782 if (llvm::Error Err = Stream.SkipBlock())
3783 return Err;
3784 if (llvm::Error Err =
3785 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3786 return Err;
3787 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3788 break;
3789
3790 case COMMENTS_BLOCK_ID: {
3791 BitstreamCursor C = Stream;
3792
3793 if (llvm::Error Err = Stream.SkipBlock())
3794 return Err;
3795 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3796 return Err;
3797 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3798 break;
3799 }
3800
3801 default:
3802 if (llvm::Error Err = Stream.SkipBlock())
3803 return Err;
3804 break;
3805 }
3806 continue;
3807
3808 case llvm::BitstreamEntry::Record:
3809 // The interesting case.
3810 break;
3811 }
3812
3813 // Read and process a record.
3814 Record.clear();
3815 StringRef Blob;
3816 Expected<unsigned> MaybeRecordType =
3817 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3818 if (!MaybeRecordType)
3819 return MaybeRecordType.takeError();
3820 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3821
3822 // If we're not loading an AST context, we don't care about most records.
3823 if (!ContextObj) {
3824 switch (RecordType) {
3825 case IDENTIFIER_TABLE:
3826 case IDENTIFIER_OFFSET:
3827 case INTERESTING_IDENTIFIERS:
3828 case STATISTICS:
3829 case PP_ASSUME_NONNULL_LOC:
3830 case PP_CONDITIONAL_STACK:
3831 case PP_COUNTER_VALUE:
3832 case SOURCE_LOCATION_OFFSETS:
3833 case MODULE_OFFSET_MAP:
3834 case SOURCE_MANAGER_LINE_TABLE:
3835 case PPD_ENTITIES_OFFSETS:
3836 case HEADER_SEARCH_TABLE:
3837 case IMPORTED_MODULES:
3838 case MACRO_OFFSET:
3839 case SUBMODULE_METADATA:
3840 break;
3841 default:
3842 continue;
3843 }
3844 }
3845
3846 switch (RecordType) {
3847 default: // Default behavior: ignore.
3848 break;
3849
3850 case SUBMODULE_METADATA: {
3851 F.BaseSubmoduleID = getTotalNumSubmodules();
3852 F.LocalNumSubmodules = Record[0];
3853 F.LocalBaseSubmoduleID = Record[1];
3854 F.LocalTopLevelSubmoduleID = Record[2];
3855 F.SubmoduleOffsets =
3856 (const llvm::support::unaligned_uint64_t *)Blob.data();
3857 if (F.LocalNumSubmodules > 0) {
3858 // Introduce the global -> local mapping for submodules within this
3859 // module.
3860 GlobalSubmoduleMap.insert(
3861 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3862
3863 // Introduce the local -> global mapping for submodules within this
3864 // module.
3865 F.SubmoduleRemap.insertOrReplace(
3866 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3867 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3868
3869 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3870 }
3871
3872 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3873 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3874 };
3875
3876 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3877 // If we already knew about this module, make sure to bring all
3878 // submodules up to date.
3879 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3880 unsigned LocalID =
3881 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3882 ReadSubmodule(LocalID);
3883 }
3884 } else {
3885 // If we didn't know this module, we loaded it transitively. Deserialize
3886 // just the top-level module to register it with ModuleMap, but load the
3887 // rest lazily.
3888 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3889 }
3890
3891 break;
3892 }
3893
3894 case TYPE_OFFSET: {
3895 if (F.LocalNumTypes != 0)
3896 return llvm::createStringError(
3897 EC: std::errc::illegal_byte_sequence,
3898 Fmt: "duplicate TYPE_OFFSET record in AST file");
3899 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3900 F.LocalNumTypes = Record[0];
3901 F.BaseTypeIndex = getTotalNumTypes();
3902
3903 if (F.LocalNumTypes > 0)
3904 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3905
3906 break;
3907 }
3908
3909 case DECL_OFFSET: {
3910 if (F.LocalNumDecls != 0)
3911 return llvm::createStringError(
3912 EC: std::errc::illegal_byte_sequence,
3913 Fmt: "duplicate DECL_OFFSET record in AST file");
3914 F.DeclOffsets = (const DeclOffset *)Blob.data();
3915 F.LocalNumDecls = Record[0];
3916 F.BaseDeclIndex = getTotalNumDecls();
3917
3918 if (F.LocalNumDecls > 0)
3919 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3920
3921 break;
3922 }
3923
3924 case TU_UPDATE_LEXICAL: {
3925 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3926 LexicalContents Contents(
3927 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3928 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3929 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3930 TU->setHasExternalLexicalStorage(true);
3931 break;
3932 }
3933
3934 case UPDATE_VISIBLE: {
3935 unsigned Idx = 0;
3936 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3937 auto *Data = (const unsigned char*)Blob.data();
3938 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3939 // If we've already loaded the decl, perform the updates when we finish
3940 // loading this block.
3941 if (Decl *D = GetExistingDecl(ID))
3942 PendingUpdateRecords.push_back(
3943 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3944 break;
3945 }
3946
3947 case UPDATE_MODULE_LOCAL_VISIBLE: {
3948 unsigned Idx = 0;
3949 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3950 auto *Data = (const unsigned char *)Blob.data();
3951 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3952 // If we've already loaded the decl, perform the updates when we finish
3953 // loading this block.
3954 if (Decl *D = GetExistingDecl(ID))
3955 PendingUpdateRecords.push_back(
3956 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3957 break;
3958 }
3959
3960 case UPDATE_TU_LOCAL_VISIBLE: {
3961 if (F.Kind != MK_MainFile)
3962 break;
3963 unsigned Idx = 0;
3964 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3965 auto *Data = (const unsigned char *)Blob.data();
3966 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3967 // If we've already loaded the decl, perform the updates when we finish
3968 // loading this block.
3969 if (Decl *D = GetExistingDecl(ID))
3970 PendingUpdateRecords.push_back(
3971 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3972 break;
3973 }
3974
3975 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3976 unsigned Idx = 0;
3977 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3978 auto *Data = (const unsigned char *)Blob.data();
3979 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3980 // If we've already loaded the decl, perform the updates when we finish
3981 // loading this block.
3982 if (Decl *D = GetExistingDecl(ID))
3983 PendingUpdateRecords.push_back(
3984 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3985 break;
3986 }
3987
3988 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3989 unsigned Idx = 0;
3990 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3991 auto *Data = (const unsigned char *)Blob.data();
3992 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3993 // If we've already loaded the decl, perform the updates when we finish
3994 // loading this block.
3995 if (Decl *D = GetExistingDecl(ID))
3996 PendingUpdateRecords.push_back(
3997 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3998 break;
3999 }
4000
4001 case IDENTIFIER_TABLE:
4002 F.IdentifierTableData =
4003 reinterpret_cast<const unsigned char *>(Blob.data());
4004 if (Record[0]) {
4005 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
4006 Buckets: F.IdentifierTableData + Record[0],
4007 Payload: F.IdentifierTableData + sizeof(uint32_t),
4008 Base: F.IdentifierTableData,
4009 InfoObj: ASTIdentifierLookupTrait(*this, F));
4010
4011 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4012 }
4013 break;
4014
4015 case IDENTIFIER_OFFSET: {
4016 if (F.LocalNumIdentifiers != 0)
4017 return llvm::createStringError(
4018 EC: std::errc::illegal_byte_sequence,
4019 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4020 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4021 F.LocalNumIdentifiers = Record[0];
4022 F.BaseIdentifierID = getTotalNumIdentifiers();
4023
4024 if (F.LocalNumIdentifiers > 0)
4025 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4026 + F.LocalNumIdentifiers);
4027 break;
4028 }
4029
4030 case INTERESTING_IDENTIFIERS:
4031 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4032 break;
4033
4034 case EAGERLY_DESERIALIZED_DECLS:
4035 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4036 // about "interesting" decls (for instance, if we're building a module).
4037 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4038 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4039 break;
4040
4041 case MODULAR_CODEGEN_DECLS:
4042 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4043 // them (ie: if we're not codegenerating this module).
4044 if (F.Kind == MK_MainFile ||
4045 getContext().getLangOpts().BuildingPCHWithObjectFile)
4046 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4047 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4048 break;
4049
4050 case SPECIAL_TYPES:
4051 if (SpecialTypes.empty()) {
4052 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4053 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4054 break;
4055 }
4056
4057 if (Record.empty())
4058 break;
4059
4060 if (SpecialTypes.size() != Record.size())
4061 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4062 Fmt: "invalid special-types record");
4063
4064 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4065 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4066 if (!SpecialTypes[I])
4067 SpecialTypes[I] = ID;
4068 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4069 // merge step?
4070 }
4071 break;
4072
4073 case STATISTICS:
4074 TotalNumStatements += Record[0];
4075 TotalNumMacros += Record[1];
4076 TotalLexicalDeclContexts += Record[2];
4077 TotalVisibleDeclContexts += Record[3];
4078 TotalModuleLocalVisibleDeclContexts += Record[4];
4079 TotalTULocalVisibleDeclContexts += Record[5];
4080 break;
4081
4082 case UNUSED_FILESCOPED_DECLS:
4083 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4084 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4085 break;
4086
4087 case DELEGATING_CTORS:
4088 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4089 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4090 break;
4091
4092 case WEAK_UNDECLARED_IDENTIFIERS:
4093 if (Record.size() % 3 != 0)
4094 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4095 Fmt: "invalid weak identifiers record");
4096
4097 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4098 // files. This isn't the way to do it :)
4099 WeakUndeclaredIdentifiers.clear();
4100
4101 // Translate the weak, undeclared identifiers into global IDs.
4102 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4103 WeakUndeclaredIdentifiers.push_back(
4104 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4105 WeakUndeclaredIdentifiers.push_back(
4106 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4107 WeakUndeclaredIdentifiers.push_back(
4108 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4109 }
4110 break;
4111
4112 case EXTNAME_UNDECLARED_IDENTIFIERS:
4113 if (Record.size() % 3 != 0)
4114 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4115 Fmt: "invalid extname identifiers record");
4116
4117 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4118 // non-original PCH files. This isn't the way to do it :)
4119 ExtnameUndeclaredIdentifiers.clear();
4120
4121 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4122 // global IDs.
4123 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4124 ExtnameUndeclaredIdentifiers.push_back(
4125 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4126 ExtnameUndeclaredIdentifiers.push_back(
4127 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4128 ExtnameUndeclaredIdentifiers.push_back(
4129 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4130 }
4131 break;
4132
4133 case SELECTOR_OFFSETS: {
4134 F.SelectorOffsets = (const uint32_t *)Blob.data();
4135 F.LocalNumSelectors = Record[0];
4136 unsigned LocalBaseSelectorID = Record[1];
4137 F.BaseSelectorID = getTotalNumSelectors();
4138
4139 if (F.LocalNumSelectors > 0) {
4140 // Introduce the global -> local mapping for selectors within this
4141 // module.
4142 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4143
4144 // Introduce the local -> global mapping for selectors within this
4145 // module.
4146 F.SelectorRemap.insertOrReplace(
4147 Val: std::make_pair(x&: LocalBaseSelectorID,
4148 y: F.BaseSelectorID - LocalBaseSelectorID));
4149
4150 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4151 }
4152 break;
4153 }
4154
4155 case METHOD_POOL:
4156 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4157 if (Record[0])
4158 F.SelectorLookupTable
4159 = ASTSelectorLookupTable::Create(
4160 Buckets: F.SelectorLookupTableData + Record[0],
4161 Base: F.SelectorLookupTableData,
4162 InfoObj: ASTSelectorLookupTrait(*this, F));
4163 TotalNumMethodPoolEntries += Record[1];
4164 break;
4165
4166 case REFERENCED_SELECTOR_POOL:
4167 if (!Record.empty()) {
4168 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4169 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4170 LocalID: Record[Idx++]));
4171 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4172 getRawEncoding());
4173 }
4174 }
4175 break;
4176
4177 case PP_ASSUME_NONNULL_LOC: {
4178 unsigned Idx = 0;
4179 if (!Record.empty())
4180 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4181 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4182 break;
4183 }
4184
4185 case PP_UNSAFE_BUFFER_USAGE: {
4186 if (!Record.empty()) {
4187 SmallVector<SourceLocation, 64> SrcLocs;
4188 unsigned Idx = 0;
4189 while (Idx < Record.size())
4190 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4191 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4192 }
4193 break;
4194 }
4195
4196 case PP_CONDITIONAL_STACK:
4197 if (!Record.empty()) {
4198 unsigned Idx = 0, End = Record.size() - 1;
4199 bool ReachedEOFWhileSkipping = Record[Idx++];
4200 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4201 if (ReachedEOFWhileSkipping) {
4202 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4203 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4204 bool FoundNonSkipPortion = Record[Idx++];
4205 bool FoundElse = Record[Idx++];
4206 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4207 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4208 args&: FoundElse, args&: ElseLoc);
4209 }
4210 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4211 while (Idx < End) {
4212 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4213 bool WasSkipping = Record[Idx++];
4214 bool FoundNonSkip = Record[Idx++];
4215 bool FoundElse = Record[Idx++];
4216 ConditionalStack.push_back(
4217 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4218 }
4219 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4220 }
4221 break;
4222
4223 case PP_COUNTER_VALUE:
4224 if (!Record.empty() && Listener)
4225 Listener->ReadCounter(M: F, Value: Record[0]);
4226 break;
4227
4228 case FILE_SORTED_DECLS:
4229 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4230 F.NumFileSortedDecls = Record[0];
4231 break;
4232
4233 case SOURCE_LOCATION_OFFSETS: {
4234 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4235 F.LocalNumSLocEntries = Record[0];
4236 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4237 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4238 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4239 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4240 TotalSize: SLocSpaceSize);
4241 if (!F.SLocEntryBaseID) {
4242 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4243 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4244 return llvm::createStringError(EC: std::errc::invalid_argument,
4245 Fmt: "ran out of source locations");
4246 }
4247 // Make our entry in the range map. BaseID is negative and growing, so
4248 // we invert it. Because we invert it, though, we need the other end of
4249 // the range.
4250 unsigned RangeStart =
4251 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4252 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4253 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4254
4255 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4256 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4257 GlobalSLocOffsetMap.insert(
4258 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4259 - SLocSpaceSize,y: &F));
4260
4261 TotalNumSLocEntries += F.LocalNumSLocEntries;
4262 break;
4263 }
4264
4265 case MODULE_OFFSET_MAP:
4266 F.ModuleOffsetMap = Blob;
4267 break;
4268
4269 case SOURCE_MANAGER_LINE_TABLE:
4270 ParseLineTable(F, Record);
4271 break;
4272
4273 case EXT_VECTOR_DECLS:
4274 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4275 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4276 break;
4277
4278 case VTABLE_USES:
4279 if (Record.size() % 3 != 0)
4280 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4281 Fmt: "Invalid VTABLE_USES record");
4282
4283 // Later tables overwrite earlier ones.
4284 // FIXME: Modules will have some trouble with this. This is clearly not
4285 // the right way to do this.
4286 VTableUses.clear();
4287
4288 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4289 VTableUses.push_back(
4290 Elt: {.ID: ReadDeclID(F, Record, Idx),
4291 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4292 .Used: (bool)Record[Idx++]});
4293 }
4294 break;
4295
4296 case PENDING_IMPLICIT_INSTANTIATIONS:
4297
4298 if (Record.size() % 2 != 0)
4299 return llvm::createStringError(
4300 EC: std::errc::illegal_byte_sequence,
4301 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4302
4303 // For standard C++20 module, we will only reads the instantiations
4304 // if it is the main file.
4305 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4306 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4307 PendingInstantiations.push_back(
4308 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4309 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4310 }
4311 }
4312 break;
4313
4314 case SEMA_DECL_REFS:
4315 if (Record.size() != 3)
4316 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4317 Fmt: "Invalid SEMA_DECL_REFS block");
4318 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4319 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4320 break;
4321
4322 case PPD_ENTITIES_OFFSETS: {
4323 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4324 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4325 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4326
4327 unsigned StartingID;
4328 if (!PP.getPreprocessingRecord())
4329 PP.createPreprocessingRecord();
4330 if (!PP.getPreprocessingRecord()->getExternalSource())
4331 PP.getPreprocessingRecord()->SetExternalSource(*this);
4332 StartingID
4333 = PP.getPreprocessingRecord()
4334 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4335 F.BasePreprocessedEntityID = StartingID;
4336
4337 if (F.NumPreprocessedEntities > 0) {
4338 // Introduce the global -> local mapping for preprocessed entities in
4339 // this module.
4340 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4341 }
4342
4343 break;
4344 }
4345
4346 case PPD_SKIPPED_RANGES: {
4347 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4348 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4349 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4350
4351 if (!PP.getPreprocessingRecord())
4352 PP.createPreprocessingRecord();
4353 if (!PP.getPreprocessingRecord()->getExternalSource())
4354 PP.getPreprocessingRecord()->SetExternalSource(*this);
4355 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4356 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4357
4358 if (F.NumPreprocessedSkippedRanges > 0)
4359 GlobalSkippedRangeMap.insert(
4360 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4361 break;
4362 }
4363
4364 case DECL_UPDATE_OFFSETS:
4365 if (Record.size() % 2 != 0)
4366 return llvm::createStringError(
4367 EC: std::errc::illegal_byte_sequence,
4368 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4369 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4370 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4371 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4372
4373 // If we've already loaded the decl, perform the updates when we finish
4374 // loading this block.
4375 if (Decl *D = GetExistingDecl(ID))
4376 PendingUpdateRecords.push_back(
4377 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4378 }
4379 break;
4380
4381 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4382 if (Record.size() % 5 != 0)
4383 return llvm::createStringError(
4384 EC: std::errc::illegal_byte_sequence,
4385 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4386 "file");
4387 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4388 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4389
4390 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4391 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4392 uint64_t LocalLexicalOffset = Record[I++];
4393 uint64_t LexicalOffset =
4394 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4395 uint64_t LocalVisibleOffset = Record[I++];
4396 uint64_t VisibleOffset =
4397 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4398 uint64_t LocalModuleLocalOffset = Record[I++];
4399 uint64_t ModuleLocalOffset =
4400 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4401 uint64_t TULocalLocalOffset = Record[I++];
4402 uint64_t TULocalOffset =
4403 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4404
4405 DelayedNamespaceOffsetMap[ID] = {
4406 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4407
4408 assert(!GetExistingDecl(ID) &&
4409 "We shouldn't load the namespace in the front of delayed "
4410 "namespace lexical and visible block");
4411 }
4412 break;
4413 }
4414
4415 case RELATED_DECLS_MAP:
4416 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4417 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4418 auto &RelatedDecls = RelatedDeclsMap[ID];
4419 unsigned NN = Record[I++];
4420 RelatedDecls.reserve(N: NN);
4421 for (unsigned II = 0; II < NN; II++)
4422 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4423 }
4424 break;
4425
4426 case OBJC_CATEGORIES_MAP:
4427 if (F.LocalNumObjCCategoriesInMap != 0)
4428 return llvm::createStringError(
4429 EC: std::errc::illegal_byte_sequence,
4430 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4431
4432 F.LocalNumObjCCategoriesInMap = Record[0];
4433 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4434 break;
4435
4436 case OBJC_CATEGORIES:
4437 F.ObjCCategories.swap(RHS&: Record);
4438 break;
4439
4440 case CUDA_SPECIAL_DECL_REFS:
4441 // Later tables overwrite earlier ones.
4442 // FIXME: Modules will have trouble with this.
4443 CUDASpecialDeclRefs.clear();
4444 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4445 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4446 break;
4447
4448 case HEADER_SEARCH_TABLE:
4449 F.HeaderFileInfoTableData = Blob.data();
4450 F.LocalNumHeaderFileInfos = Record[1];
4451 if (Record[0]) {
4452 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4453 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4454 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4455 InfoObj: HeaderFileInfoTrait(*this, F));
4456
4457 PP.getHeaderSearchInfo().SetExternalSource(this);
4458 if (!PP.getHeaderSearchInfo().getExternalLookup())
4459 PP.getHeaderSearchInfo().SetExternalLookup(this);
4460 }
4461 break;
4462
4463 case FP_PRAGMA_OPTIONS:
4464 // Later tables overwrite earlier ones.
4465 FPPragmaOptions.swap(RHS&: Record);
4466 break;
4467
4468 case DECLS_WITH_EFFECTS_TO_VERIFY:
4469 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4470 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4471 break;
4472
4473 case OPENCL_EXTENSIONS:
4474 for (unsigned I = 0, E = Record.size(); I != E; ) {
4475 auto Name = ReadString(Record, Idx&: I);
4476 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4477 OptInfo.Supported = Record[I++] != 0;
4478 OptInfo.Enabled = Record[I++] != 0;
4479 OptInfo.WithPragma = Record[I++] != 0;
4480 OptInfo.Avail = Record[I++];
4481 OptInfo.Core = Record[I++];
4482 OptInfo.Opt = Record[I++];
4483 }
4484 break;
4485
4486 case TENTATIVE_DEFINITIONS:
4487 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4488 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4489 break;
4490
4491 case KNOWN_NAMESPACES:
4492 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4493 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4494 break;
4495
4496 case UNDEFINED_BUT_USED:
4497 if (Record.size() % 2 != 0)
4498 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4499 Fmt: "invalid undefined-but-used record");
4500 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4501 UndefinedButUsed.push_back(
4502 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4503 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4504 }
4505 break;
4506
4507 case DELETE_EXPRS_TO_ANALYZE:
4508 for (unsigned I = 0, N = Record.size(); I != N;) {
4509 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4510 const uint64_t Count = Record[I++];
4511 DelayedDeleteExprs.push_back(Elt: Count);
4512 for (uint64_t C = 0; C < Count; ++C) {
4513 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4514 bool IsArrayForm = Record[I++] == 1;
4515 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4516 }
4517 }
4518 break;
4519
4520 case VTABLES_TO_EMIT:
4521 if (F.Kind == MK_MainFile ||
4522 getContext().getLangOpts().BuildingPCHWithObjectFile)
4523 for (unsigned I = 0, N = Record.size(); I != N;)
4524 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4525 break;
4526
4527 case IMPORTED_MODULES:
4528 if (!F.isModule()) {
4529 // If we aren't loading a module (which has its own exports), make
4530 // all of the imported modules visible.
4531 // FIXME: Deal with macros-only imports.
4532 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4533 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4534 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4535 if (GlobalID) {
4536 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4537 if (DeserializationListener)
4538 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4539 }
4540 }
4541 }
4542 break;
4543
4544 case MACRO_OFFSET: {
4545 if (F.LocalNumMacros != 0)
4546 return llvm::createStringError(
4547 EC: std::errc::illegal_byte_sequence,
4548 Fmt: "duplicate MACRO_OFFSET record in AST file");
4549 F.MacroOffsets = (const uint32_t *)Blob.data();
4550 F.LocalNumMacros = Record[0];
4551 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4552 F.BaseMacroID = getTotalNumMacros();
4553
4554 if (F.LocalNumMacros > 0)
4555 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4556 break;
4557 }
4558
4559 case LATE_PARSED_TEMPLATE:
4560 LateParsedTemplates.emplace_back(
4561 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4562 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4563 break;
4564
4565 case OPTIMIZE_PRAGMA_OPTIONS:
4566 if (Record.size() != 1)
4567 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4568 Fmt: "invalid pragma optimize record");
4569 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4570 break;
4571
4572 case MSSTRUCT_PRAGMA_OPTIONS:
4573 if (Record.size() != 1)
4574 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4575 Fmt: "invalid pragma ms_struct record");
4576 PragmaMSStructState = Record[0];
4577 break;
4578
4579 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4580 if (Record.size() != 2)
4581 return llvm::createStringError(
4582 EC: std::errc::illegal_byte_sequence,
4583 Fmt: "invalid pragma pointers to members record");
4584 PragmaMSPointersToMembersState = Record[0];
4585 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4586 break;
4587
4588 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4589 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4590 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4591 break;
4592
4593 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4594 if (Record.size() != 1)
4595 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4596 Fmt: "invalid cuda pragma options record");
4597 ForceHostDeviceDepth = Record[0];
4598 break;
4599
4600 case ALIGN_PACK_PRAGMA_OPTIONS: {
4601 if (Record.size() < 3)
4602 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4603 Fmt: "invalid pragma pack record");
4604 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4605 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4606 unsigned NumStackEntries = Record[2];
4607 unsigned Idx = 3;
4608 // Reset the stack when importing a new module.
4609 PragmaAlignPackStack.clear();
4610 for (unsigned I = 0; I < NumStackEntries; ++I) {
4611 PragmaAlignPackStackEntry Entry;
4612 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4613 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4614 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4615 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4616 Entry.SlotLabel = PragmaAlignPackStrings.back();
4617 PragmaAlignPackStack.push_back(Elt: Entry);
4618 }
4619 break;
4620 }
4621
4622 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4623 if (Record.size() < 3)
4624 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4625 Fmt: "invalid pragma float control record");
4626 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4627 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4628 unsigned NumStackEntries = Record[2];
4629 unsigned Idx = 3;
4630 // Reset the stack when importing a new module.
4631 FpPragmaStack.clear();
4632 for (unsigned I = 0; I < NumStackEntries; ++I) {
4633 FpPragmaStackEntry Entry;
4634 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4635 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4636 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4637 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4638 Entry.SlotLabel = FpPragmaStrings.back();
4639 FpPragmaStack.push_back(Elt: Entry);
4640 }
4641 break;
4642 }
4643
4644 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4645 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4646 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4647 break;
4648
4649 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4650 unsigned NumRecords = Record.front();
4651 // Last record which is used to keep number of valid records.
4652 if (Record.size() - 1 != NumRecords)
4653 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4654 Fmt: "invalid rvv intrinsic pragma record");
4655
4656 if (RISCVVecIntrinsicPragma.empty())
4657 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4658 // There might be multiple precompiled modules imported, we need to union
4659 // them all.
4660 for (unsigned i = 0; i < NumRecords; ++i)
4661 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4662 break;
4663 }
4664 }
4665 }
4666}
4667
4668void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4669 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4670
4671 // Additional remapping information.
4672 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4673 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4674 F.ModuleOffsetMap = StringRef();
4675
4676 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4677 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4678 RemapBuilder SelectorRemap(F.SelectorRemap);
4679
4680 auto &ImportedModuleVector = F.TransitiveImports;
4681 assert(ImportedModuleVector.empty());
4682
4683 while (Data < DataEnd) {
4684 // FIXME: Looking up dependency modules by filename is horrible. Let's
4685 // start fixing this with prebuilt, explicit and implicit modules and see
4686 // how it goes...
4687 using namespace llvm::support;
4688 ModuleKind Kind = static_cast<ModuleKind>(
4689 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4690 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4691 StringRef Name = StringRef((const char*)Data, Len);
4692 Data += Len;
4693 ModuleFile *OM =
4694 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4695 Kind == MK_ImplicitModule
4696 ? ModuleMgr.lookupByModuleName(ModName: Name)
4697 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4698 if (!OM)
4699 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4700 if (!OM) {
4701 std::string Msg = "refers to unknown module, cannot find ";
4702 Msg.append(str: std::string(Name));
4703 Error(Msg);
4704 return;
4705 }
4706
4707 ImportedModuleVector.push_back(Elt: OM);
4708
4709 uint32_t SubmoduleIDOffset =
4710 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4711 uint32_t SelectorIDOffset =
4712 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4713
4714 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4715 RemapBuilder &Remap) {
4716 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4717 if (Offset != None)
4718 Remap.insert(Val: std::make_pair(x&: Offset,
4719 y: static_cast<int>(BaseOffset - Offset)));
4720 };
4721
4722 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4723 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4724 }
4725}
4726
4727ASTReader::ASTReadResult
4728ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4729 const ModuleFile *ImportedBy,
4730 unsigned ClientLoadCapabilities) {
4731 unsigned Idx = 0;
4732 F.ModuleMapPath = ReadPath(F, Record, Idx);
4733
4734 // Try to resolve ModuleName in the current header search context and
4735 // verify that it is found in the same module map file as we saved. If the
4736 // top-level AST file is a main file, skip this check because there is no
4737 // usable header search context.
4738 assert(!F.ModuleName.empty() &&
4739 "MODULE_NAME should come before MODULE_MAP_FILE");
4740 auto [MaybeM, IgnoreError] =
4741 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4742 if (MaybeM.has_value()) {
4743 // An implicitly-loaded module file should have its module listed in some
4744 // module map file that we've already loaded.
4745 Module *M = MaybeM.value();
4746 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4747 OptionalFileEntryRef ModMap =
4748 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4749 if (!IgnoreError && !ModMap) {
4750 if (M && M->Directory)
4751 Diag(DiagID: diag::remark_module_relocated)
4752 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4753
4754 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4755 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4756 // This module was defined by an imported (explicit) module.
4757 Diag(DiagID: diag::err_module_file_conflict)
4758 << F.ModuleName << F.FileName << *ASTFileName;
4759 // TODO: Add a note with the module map paths if they differ.
4760 } else {
4761 // This module was built with a different module map.
4762 Diag(DiagID: diag::err_imported_module_not_found)
4763 << F.ModuleName << F.FileName
4764 << (ImportedBy ? ImportedBy->FileName.str() : "")
4765 << F.ModuleMapPath << !ImportedBy;
4766 // In case it was imported by a PCH, there's a chance the user is
4767 // just missing to include the search path to the directory containing
4768 // the modulemap.
4769 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4770 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4771 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4772 }
4773 }
4774 return OutOfDate;
4775 }
4776
4777 assert(M && M->Name == F.ModuleName && "found module with different name");
4778
4779 // Check the primary module map file.
4780 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4781 if (!StoredModMap || *StoredModMap != ModMap) {
4782 assert(ModMap && "found module is missing module map file");
4783 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4784 "top-level import should be verified");
4785 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4786 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4787 Diag(DiagID: diag::err_imported_module_modmap_changed)
4788 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4789 << ModMap->getName() << F.ModuleMapPath << NotImported;
4790 return OutOfDate;
4791 }
4792
4793 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4794 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4795 // FIXME: we should use input files rather than storing names.
4796 std::string Filename = ReadPath(F, Record, Idx);
4797 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4798 if (!SF) {
4799 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4800 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4801 return OutOfDate;
4802 }
4803 AdditionalStoredMaps.insert(V: *SF);
4804 }
4805
4806 // Check any additional module map files (e.g. module.private.modulemap)
4807 // that are not in the pcm.
4808 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4809 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4810 // Remove files that match
4811 // Note: SmallPtrSet::erase is really remove
4812 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4813 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4814 Diag(DiagID: diag::err_module_different_modmap)
4815 << F.ModuleName << /*new*/0 << ModMap.getName();
4816 return OutOfDate;
4817 }
4818 }
4819 }
4820
4821 // Check any additional module map files that are in the pcm, but not
4822 // found in header search. Cases that match are already removed.
4823 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4824 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4825 Diag(DiagID: diag::err_module_different_modmap)
4826 << F.ModuleName << /*not new*/1 << ModMap.getName();
4827 return OutOfDate;
4828 }
4829 }
4830
4831 if (Listener)
4832 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4833 return Success;
4834}
4835
4836/// Move the given method to the back of the global list of methods.
4837static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4838 // Find the entry for this selector in the method pool.
4839 SemaObjC::GlobalMethodPool::iterator Known =
4840 S.ObjC().MethodPool.find(Val: Method->getSelector());
4841 if (Known == S.ObjC().MethodPool.end())
4842 return;
4843
4844 // Retrieve the appropriate method list.
4845 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4846 : Known->second.second;
4847 bool Found = false;
4848 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4849 if (!Found) {
4850 if (List->getMethod() == Method) {
4851 Found = true;
4852 } else {
4853 // Keep searching.
4854 continue;
4855 }
4856 }
4857
4858 if (List->getNext())
4859 List->setMethod(List->getNext()->getMethod());
4860 else
4861 List->setMethod(Method);
4862 }
4863}
4864
4865void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4866 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4867 for (Decl *D : Names) {
4868 bool wasHidden = !D->isUnconditionallyVisible();
4869 D->setVisibleDespiteOwningModule();
4870
4871 if (wasHidden && SemaObj) {
4872 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4873 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4874 }
4875 }
4876 }
4877}
4878
4879void ASTReader::makeModuleVisible(Module *Mod,
4880 Module::NameVisibilityKind NameVisibility,
4881 SourceLocation ImportLoc) {
4882 llvm::SmallPtrSet<Module *, 4> Visited;
4883 SmallVector<Module *, 4> Stack;
4884 Stack.push_back(Elt: Mod);
4885 while (!Stack.empty()) {
4886 Mod = Stack.pop_back_val();
4887
4888 if (NameVisibility <= Mod->NameVisibility) {
4889 // This module already has this level of visibility (or greater), so
4890 // there is nothing more to do.
4891 continue;
4892 }
4893
4894 if (Mod->isUnimportable()) {
4895 // Modules that aren't importable cannot be made visible.
4896 continue;
4897 }
4898
4899 // Update the module's name visibility.
4900 Mod->NameVisibility = NameVisibility;
4901
4902 // If we've already deserialized any names from this module,
4903 // mark them as visible.
4904 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4905 if (Hidden != HiddenNamesMap.end()) {
4906 auto HiddenNames = std::move(*Hidden);
4907 HiddenNamesMap.erase(I: Hidden);
4908 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4909 assert(!HiddenNamesMap.contains(Mod) &&
4910 "making names visible added hidden names");
4911 }
4912
4913 // Push any exported modules onto the stack to be marked as visible.
4914 SmallVector<Module *, 16> Exports;
4915 Mod->getExportedModules(Exported&: Exports);
4916 for (SmallVectorImpl<Module *>::iterator
4917 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4918 Module *Exported = *I;
4919 if (Visited.insert(Ptr: Exported).second)
4920 Stack.push_back(Elt: Exported);
4921 }
4922 }
4923}
4924
4925/// We've merged the definition \p MergedDef into the existing definition
4926/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4927/// visible.
4928void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4929 NamedDecl *MergedDef) {
4930 if (!Def->isUnconditionallyVisible()) {
4931 // If MergedDef is visible or becomes visible, make the definition visible.
4932 if (MergedDef->isUnconditionallyVisible())
4933 Def->setVisibleDespiteOwningModule();
4934 else {
4935 getContext().mergeDefinitionIntoModule(
4936 ND: Def, M: MergedDef->getImportedOwningModule(),
4937 /*NotifyListeners*/ false);
4938 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4939 }
4940 }
4941}
4942
4943bool ASTReader::loadGlobalIndex() {
4944 if (GlobalIndex)
4945 return false;
4946
4947 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4948 !PP.getLangOpts().Modules)
4949 return true;
4950
4951 // Try to load the global index.
4952 TriedLoadingGlobalIndex = true;
4953 StringRef SpecificModuleCachePath =
4954 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4955 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4956 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4957 if (llvm::Error Err = std::move(Result.second)) {
4958 assert(!Result.first);
4959 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4960 return true;
4961 }
4962
4963 GlobalIndex.reset(p: Result.first);
4964 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4965 return false;
4966}
4967
4968bool ASTReader::isGlobalIndexUnavailable() const {
4969 return PP.getLangOpts().Modules && UseGlobalIndex &&
4970 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4971}
4972
4973/// Given a cursor at the start of an AST file, scan ahead and drop the
4974/// cursor into the start of the given block ID, returning false on success and
4975/// true on failure.
4976static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4977 while (true) {
4978 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4979 if (!MaybeEntry) {
4980 // FIXME this drops errors on the floor.
4981 consumeError(Err: MaybeEntry.takeError());
4982 return true;
4983 }
4984 llvm::BitstreamEntry Entry = MaybeEntry.get();
4985
4986 switch (Entry.Kind) {
4987 case llvm::BitstreamEntry::Error:
4988 case llvm::BitstreamEntry::EndBlock:
4989 return true;
4990
4991 case llvm::BitstreamEntry::Record:
4992 // Ignore top-level records.
4993 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4994 break;
4995 else {
4996 // FIXME this drops errors on the floor.
4997 consumeError(Err: Skipped.takeError());
4998 return true;
4999 }
5000
5001 case llvm::BitstreamEntry::SubBlock:
5002 if (Entry.ID == BlockID) {
5003 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
5004 // FIXME this drops the error on the floor.
5005 consumeError(Err: std::move(Err));
5006 return true;
5007 }
5008 // Found it!
5009 return false;
5010 }
5011
5012 if (llvm::Error Err = Cursor.SkipBlock()) {
5013 // FIXME this drops the error on the floor.
5014 consumeError(Err: std::move(Err));
5015 return true;
5016 }
5017 }
5018 }
5019}
5020
5021ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5022 ModuleKind Type,
5023 SourceLocation ImportLoc,
5024 unsigned ClientLoadCapabilities,
5025 ModuleFile **NewLoadedModuleFile) {
5026 llvm::TimeTraceScope scope("ReadAST", FileName);
5027
5028 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5029 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5030 CurrentDeserializingModuleKind, Type);
5031
5032 // Defer any pending actions until we get to the end of reading the AST file.
5033 Deserializing AnASTFile(this);
5034
5035 // Bump the generation number.
5036 unsigned PreviousGeneration = 0;
5037 if (ContextObj)
5038 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5039
5040 unsigned NumModules = ModuleMgr.size();
5041 SmallVector<ImportedModule, 4> Loaded;
5042 if (ASTReadResult ReadResult =
5043 ReadASTCore(FileName, Type, ImportLoc,
5044 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5045 ClientLoadCapabilities)) {
5046 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5047
5048 // If we find that any modules are unusable, the global index is going
5049 // to be out-of-date. Just remove it.
5050 GlobalIndex.reset();
5051 ModuleMgr.setGlobalIndex(nullptr);
5052 return ReadResult;
5053 }
5054
5055 if (NewLoadedModuleFile && !Loaded.empty())
5056 *NewLoadedModuleFile = Loaded.back().Mod;
5057
5058 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5059 // remove modules from this point. Various fields are updated during reading
5060 // the AST block and removing the modules would result in dangling pointers.
5061 // They are generally only incidentally dereferenced, ie. a binary search
5062 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5063 // be dereferenced but it wouldn't actually be used.
5064
5065 // Load the AST blocks of all of the modules that we loaded. We can still
5066 // hit errors parsing the ASTs at this point.
5067 for (ImportedModule &M : Loaded) {
5068 ModuleFile &F = *M.Mod;
5069 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5070
5071 // Read the AST block.
5072 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5073 Error(Err: std::move(Err));
5074 return Failure;
5075 }
5076
5077 // The AST block should always have a definition for the main module.
5078 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5079 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5080 return Failure;
5081 }
5082
5083 // Read the extension blocks.
5084 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5085 if (llvm::Error Err = ReadExtensionBlock(F)) {
5086 Error(Err: std::move(Err));
5087 return Failure;
5088 }
5089 }
5090
5091 // Once read, set the ModuleFile bit base offset and update the size in
5092 // bits of all files we've seen.
5093 F.GlobalBitOffset = TotalModulesSizeInBits;
5094 TotalModulesSizeInBits += F.SizeInBits;
5095 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5096 }
5097
5098 // Preload source locations and interesting indentifiers.
5099 for (ImportedModule &M : Loaded) {
5100 ModuleFile &F = *M.Mod;
5101
5102 // Map the original source file ID into the ID space of the current
5103 // compilation.
5104 if (F.OriginalSourceFileID.isValid())
5105 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5106
5107 for (auto Offset : F.PreloadIdentifierOffsets) {
5108 const unsigned char *Data = F.IdentifierTableData + Offset;
5109
5110 ASTIdentifierLookupTrait Trait(*this, F);
5111 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5112 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5113
5114 IdentifierInfo *II;
5115 if (!PP.getLangOpts().CPlusPlus) {
5116 // Identifiers present in both the module file and the importing
5117 // instance are marked out-of-date so that they can be deserialized
5118 // on next use via ASTReader::updateOutOfDateIdentifier().
5119 // Identifiers present in the module file but not in the importing
5120 // instance are ignored for now, preventing growth of the identifier
5121 // table. They will be deserialized on first use via ASTReader::get().
5122 auto It = PP.getIdentifierTable().find(Name: Key);
5123 if (It == PP.getIdentifierTable().end())
5124 continue;
5125 II = It->second;
5126 } else {
5127 // With C++ modules, not many identifiers are considered interesting.
5128 // All identifiers in the module file can be placed into the identifier
5129 // table of the importing instance and marked as out-of-date. This makes
5130 // ASTReader::get() a no-op, and deserialization will take place on
5131 // first/next use via ASTReader::updateOutOfDateIdentifier().
5132 II = &PP.getIdentifierTable().getOwn(Name: Key);
5133 }
5134
5135 II->setOutOfDate(true);
5136
5137 // Mark this identifier as being from an AST file so that we can track
5138 // whether we need to serialize it.
5139 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5140
5141 // Associate the ID with the identifier so that the writer can reuse it.
5142 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5143 SetIdentifierInfo(ID, II);
5144 }
5145 }
5146
5147 // Builtins and library builtins have already been initialized. Mark all
5148 // identifiers as out-of-date, so that they are deserialized on first use.
5149 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5150 for (auto &Id : PP.getIdentifierTable())
5151 Id.second->setOutOfDate(true);
5152
5153 // Mark selectors as out of date.
5154 for (const auto &Sel : SelectorGeneration)
5155 SelectorOutOfDate[Sel.first] = true;
5156
5157 // Setup the import locations and notify the module manager that we've
5158 // committed to these module files.
5159 for (ImportedModule &M : Loaded) {
5160 ModuleFile &F = *M.Mod;
5161
5162 ModuleMgr.moduleFileAccepted(MF: &F);
5163
5164 // Set the import location.
5165 F.DirectImportLoc = ImportLoc;
5166 // FIXME: We assume that locations from PCH / preamble do not need
5167 // any translation.
5168 if (!M.ImportedBy)
5169 F.ImportLoc = M.ImportLoc;
5170 else
5171 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5172 }
5173
5174 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5175 // Might be unnecessary as use declarations are only used to build the
5176 // module itself.
5177
5178 if (ContextObj)
5179 InitializeContext();
5180
5181 if (SemaObj)
5182 UpdateSema();
5183
5184 if (DeserializationListener)
5185 DeserializationListener->ReaderInitialized(Reader: this);
5186
5187 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5188 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5189 // If this AST file is a precompiled preamble, then set the
5190 // preamble file ID of the source manager to the file source file
5191 // from which the preamble was built.
5192 if (Type == MK_Preamble) {
5193 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5194 } else if (Type == MK_MainFile) {
5195 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5196 }
5197 }
5198
5199 // For any Objective-C class definitions we have already loaded, make sure
5200 // that we load any additional categories.
5201 if (ContextObj) {
5202 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5203 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5204 D: ObjCClassesLoaded[I], PreviousGeneration);
5205 }
5206 }
5207
5208 const HeaderSearchOptions &HSOpts =
5209 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5210 if (HSOpts.ModulesValidateOncePerBuildSession) {
5211 // Now we are certain that the module and all modules it depends on are
5212 // up-to-date. For implicitly-built module files, ensure the corresponding
5213 // timestamp files are up-to-date in this build session.
5214 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5215 ImportedModule &M = Loaded[I];
5216 if (M.Mod->Kind == MK_ImplicitModule &&
5217 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5218 getModuleManager().getModuleCache().updateModuleTimestamp(
5219 ModuleFilename: M.Mod->FileName);
5220 }
5221 }
5222
5223 return Success;
5224}
5225
5226static ASTFileSignature readASTFileSignature(StringRef PCH);
5227
5228/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5229static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5230 // FIXME checking magic headers is done in other places such as
5231 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5232 // always done the same. Unify it all with a helper.
5233 if (!Stream.canSkipToPos(pos: 4))
5234 return llvm::createStringError(
5235 EC: std::errc::illegal_byte_sequence,
5236 Fmt: "file too small to contain precompiled file magic");
5237 for (unsigned C : {'C', 'P', 'C', 'H'})
5238 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5239 if (Res.get() != C)
5240 return llvm::createStringError(
5241 EC: std::errc::illegal_byte_sequence,
5242 Fmt: "file doesn't start with precompiled file magic");
5243 } else
5244 return Res.takeError();
5245 return llvm::Error::success();
5246}
5247
5248static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5249 switch (Kind) {
5250 case MK_PCH:
5251 return 0; // PCH
5252 case MK_ImplicitModule:
5253 case MK_ExplicitModule:
5254 case MK_PrebuiltModule:
5255 return 1; // module
5256 case MK_MainFile:
5257 case MK_Preamble:
5258 return 2; // main source file
5259 }
5260 llvm_unreachable("unknown module kind");
5261}
5262
5263ASTReader::ASTReadResult ASTReader::ReadASTCore(
5264 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5265 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5266 off_t ExpectedSize, time_t ExpectedModTime,
5267 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5268 auto Result = ModuleMgr.addModule(
5269 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5270 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5271 ModuleFile *M = Result.getModule();
5272
5273 switch (Result.getKind()) {
5274 case AddModuleResult::AlreadyLoaded: {
5275 Diag(DiagID: diag::remark_module_import)
5276 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5277 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5278 return Success;
5279 }
5280
5281 case AddModuleResult::NewlyLoaded:
5282 // Load module file below.
5283 break;
5284
5285 case AddModuleResult::Missing:
5286 // The module file was missing; if the client can handle that, return
5287 // it.
5288 if (ClientLoadCapabilities & ARR_Missing)
5289 return Missing;
5290
5291 // Otherwise, return an error.
5292 Diag(DiagID: diag::err_ast_file_not_found)
5293 << moduleKindForDiagnostic(Kind: Type) << FileName;
5294 if (!Result.getBufferError().empty())
5295 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5296 return Failure;
5297
5298 case AddModuleResult::OutOfDate:
5299 // We couldn't load the module file because it is out-of-date. If the
5300 // client can handle out-of-date, return it.
5301 if (ClientLoadCapabilities & ARR_OutOfDate)
5302 return OutOfDate;
5303
5304 // Otherwise, return an error.
5305 Diag(DiagID: diag::err_ast_file_out_of_date)
5306 << moduleKindForDiagnostic(Kind: Type) << FileName;
5307 for (const auto &C : Result.getChanges()) {
5308 Diag(DiagID: diag::note_fe_ast_file_modified)
5309 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5310 << llvm::itostr(X: C.New.value_or(u: 0));
5311 }
5312 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5313 << Result.getValidationStatus();
5314 if (!Result.getSignatureError().empty())
5315 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5316 return Failure;
5317
5318 case AddModuleResult::None:
5319 llvm_unreachable("Unexpected value from adding module.");
5320 }
5321
5322 assert(M && "Missing module file");
5323
5324 bool ShouldFinalizePCM = false;
5325 llvm::scope_exit FinalizeOrDropPCM([&]() {
5326 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5327 if (ShouldFinalizePCM)
5328 MC.finalizePCM(Filename: FileName);
5329 else
5330 MC.tryToDropPCM(Filename: FileName);
5331 });
5332 ModuleFile &F = *M;
5333 BitstreamCursor &Stream = F.Stream;
5334 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5335 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5336
5337 // Sniff for the signature.
5338 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5339 Diag(DiagID: diag::err_ast_file_invalid)
5340 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5341 return Failure;
5342 }
5343
5344 // This is used for compatibility with older PCH formats.
5345 bool HaveReadControlBlock = false;
5346 while (true) {
5347 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5348 if (!MaybeEntry) {
5349 Error(Err: MaybeEntry.takeError());
5350 return Failure;
5351 }
5352 llvm::BitstreamEntry Entry = MaybeEntry.get();
5353
5354 switch (Entry.Kind) {
5355 case llvm::BitstreamEntry::Error:
5356 case llvm::BitstreamEntry::Record:
5357 case llvm::BitstreamEntry::EndBlock:
5358 Error(Msg: "invalid record at top-level of AST file");
5359 return Failure;
5360
5361 case llvm::BitstreamEntry::SubBlock:
5362 break;
5363 }
5364
5365 switch (Entry.ID) {
5366 case CONTROL_BLOCK_ID:
5367 HaveReadControlBlock = true;
5368 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5369 case Success:
5370 // Check that we didn't try to load a non-module AST file as a module.
5371 //
5372 // FIXME: Should we also perform the converse check? Loading a module as
5373 // a PCH file sort of works, but it's a bit wonky.
5374 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5375 Type == MK_PrebuiltModule) &&
5376 F.ModuleName.empty()) {
5377 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5378 if (Result != OutOfDate ||
5379 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5380 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5381 return Result;
5382 }
5383 break;
5384
5385 case Failure: return Failure;
5386 case Missing: return Missing;
5387 case OutOfDate: return OutOfDate;
5388 case VersionMismatch: return VersionMismatch;
5389 case ConfigurationMismatch: return ConfigurationMismatch;
5390 case HadErrors: return HadErrors;
5391 }
5392 break;
5393
5394 case AST_BLOCK_ID:
5395 if (!HaveReadControlBlock) {
5396 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5397 Diag(DiagID: diag::err_ast_file_version_too_old)
5398 << moduleKindForDiagnostic(Kind: Type) << FileName;
5399 return VersionMismatch;
5400 }
5401
5402 // Record that we've loaded this module.
5403 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5404 ShouldFinalizePCM = true;
5405 return Success;
5406
5407 default:
5408 if (llvm::Error Err = Stream.SkipBlock()) {
5409 Error(Err: std::move(Err));
5410 return Failure;
5411 }
5412 break;
5413 }
5414 }
5415
5416 llvm_unreachable("unexpected break; expected return");
5417}
5418
5419ASTReader::ASTReadResult
5420ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5421 unsigned ClientLoadCapabilities) {
5422 const HeaderSearchOptions &HSOpts =
5423 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5424 bool AllowCompatibleConfigurationMismatch =
5425 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5426 bool DisableValidation = shouldDisableValidationForFile(M: F);
5427
5428 ASTReadResult Result = readUnhashedControlBlockImpl(
5429 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5430 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5431 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5432
5433 // If F was directly imported by another module, it's implicitly validated by
5434 // the importing module.
5435 if (DisableValidation || WasImportedBy ||
5436 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5437 return Success;
5438
5439 if (Result == Failure) {
5440 Error(Msg: "malformed block record in AST file");
5441 return Failure;
5442 }
5443
5444 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5445 // If this module has already been finalized in the ModuleCache, we're stuck
5446 // with it; we can only load a single version of each module.
5447 //
5448 // This can happen when a module is imported in two contexts: in one, as a
5449 // user module; in another, as a system module (due to an import from
5450 // another module marked with the [system] flag). It usually indicates a
5451 // bug in the module map: this module should also be marked with [system].
5452 //
5453 // If -Wno-system-headers (the default), and the first import is as a
5454 // system module, then validation will fail during the as-user import,
5455 // since -Werror flags won't have been validated. However, it's reasonable
5456 // to treat this consistently as a system module.
5457 //
5458 // If -Wsystem-headers, the PCM on disk was built with
5459 // -Wno-system-headers, and the first import is as a user module, then
5460 // validation will fail during the as-system import since the PCM on disk
5461 // doesn't guarantee that -Werror was respected. However, the -Werror
5462 // flags were checked during the initial as-user import.
5463 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5464 Filename: F.FileName)) {
5465 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5466 return Success;
5467 }
5468 }
5469
5470 return Result;
5471}
5472
5473ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5474 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5475 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5476 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5477 // Initialize a stream.
5478 BitstreamCursor Stream(StreamData);
5479
5480 // Sniff for the signature.
5481 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5482 // FIXME this drops the error on the floor.
5483 consumeError(Err: std::move(Err));
5484 return Failure;
5485 }
5486
5487 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5488 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5489 return Failure;
5490
5491 // Read all of the records in the options block.
5492 RecordData Record;
5493 ASTReadResult Result = Success;
5494 while (true) {
5495 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5496 if (!MaybeEntry) {
5497 // FIXME this drops the error on the floor.
5498 consumeError(Err: MaybeEntry.takeError());
5499 return Failure;
5500 }
5501 llvm::BitstreamEntry Entry = MaybeEntry.get();
5502
5503 switch (Entry.Kind) {
5504 case llvm::BitstreamEntry::Error:
5505 case llvm::BitstreamEntry::SubBlock:
5506 return Failure;
5507
5508 case llvm::BitstreamEntry::EndBlock:
5509 return Result;
5510
5511 case llvm::BitstreamEntry::Record:
5512 // The interesting case.
5513 break;
5514 }
5515
5516 // Read and process a record.
5517 Record.clear();
5518 StringRef Blob;
5519 Expected<unsigned> MaybeRecordType =
5520 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5521 if (!MaybeRecordType) {
5522 // FIXME this drops the error.
5523 return Failure;
5524 }
5525 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5526 case SIGNATURE:
5527 if (F) {
5528 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5529 assert(F->Signature != ASTFileSignature::createDummy() &&
5530 "Dummy AST file signature not backpatched in ASTWriter.");
5531 }
5532 break;
5533 case AST_BLOCK_HASH:
5534 if (F) {
5535 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5536 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5537 "Dummy AST block hash not backpatched in ASTWriter.");
5538 }
5539 break;
5540 case DIAGNOSTIC_OPTIONS: {
5541 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5542 if (Listener && ValidateDiagnosticOptions &&
5543 !AllowCompatibleConfigurationMismatch &&
5544 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5545 Result = OutOfDate; // Don't return early. Read the signature.
5546 break;
5547 }
5548 case HEADER_SEARCH_PATHS: {
5549 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5550 if (Listener && !AllowCompatibleConfigurationMismatch &&
5551 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5552 Result = ConfigurationMismatch;
5553 break;
5554 }
5555 case DIAG_PRAGMA_MAPPINGS:
5556 if (!F)
5557 break;
5558 if (F->PragmaDiagMappings.empty())
5559 F->PragmaDiagMappings.swap(RHS&: Record);
5560 else
5561 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5562 From: Record.begin(), To: Record.end());
5563 break;
5564 case HEADER_SEARCH_ENTRY_USAGE:
5565 if (F)
5566 F->SearchPathUsage = ReadBitVector(Record, Blob);
5567 break;
5568 case VFS_USAGE:
5569 if (F)
5570 F->VFSUsage = ReadBitVector(Record, Blob);
5571 break;
5572 }
5573 }
5574}
5575
5576/// Parse a record and blob containing module file extension metadata.
5577static bool parseModuleFileExtensionMetadata(
5578 const SmallVectorImpl<uint64_t> &Record,
5579 StringRef Blob,
5580 ModuleFileExtensionMetadata &Metadata) {
5581 if (Record.size() < 4) return true;
5582
5583 Metadata.MajorVersion = Record[0];
5584 Metadata.MinorVersion = Record[1];
5585
5586 unsigned BlockNameLen = Record[2];
5587 unsigned UserInfoLen = Record[3];
5588
5589 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5590
5591 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5592 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5593 Blob.data() + BlockNameLen + UserInfoLen);
5594 return false;
5595}
5596
5597llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5598 BitstreamCursor &Stream = F.Stream;
5599
5600 RecordData Record;
5601 while (true) {
5602 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5603 if (!MaybeEntry)
5604 return MaybeEntry.takeError();
5605 llvm::BitstreamEntry Entry = MaybeEntry.get();
5606
5607 switch (Entry.Kind) {
5608 case llvm::BitstreamEntry::SubBlock:
5609 if (llvm::Error Err = Stream.SkipBlock())
5610 return Err;
5611 continue;
5612 case llvm::BitstreamEntry::EndBlock:
5613 return llvm::Error::success();
5614 case llvm::BitstreamEntry::Error:
5615 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5616 Fmt: "malformed block record in AST file");
5617 case llvm::BitstreamEntry::Record:
5618 break;
5619 }
5620
5621 Record.clear();
5622 StringRef Blob;
5623 Expected<unsigned> MaybeRecCode =
5624 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5625 if (!MaybeRecCode)
5626 return MaybeRecCode.takeError();
5627 switch (MaybeRecCode.get()) {
5628 case EXTENSION_METADATA: {
5629 ModuleFileExtensionMetadata Metadata;
5630 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5631 return llvm::createStringError(
5632 EC: std::errc::illegal_byte_sequence,
5633 Fmt: "malformed EXTENSION_METADATA in AST file");
5634
5635 // Find a module file extension with this block name.
5636 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5637 if (Known == ModuleFileExtensions.end()) break;
5638
5639 // Form a reader.
5640 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5641 Mod&: F, Stream)) {
5642 F.ExtensionReaders.push_back(x: std::move(Reader));
5643 }
5644
5645 break;
5646 }
5647 }
5648 }
5649
5650 llvm_unreachable("ReadExtensionBlock should return from while loop");
5651}
5652
5653void ASTReader::InitializeContext() {
5654 assert(ContextObj && "no context to initialize");
5655 ASTContext &Context = *ContextObj;
5656
5657 // If there's a listener, notify them that we "read" the translation unit.
5658 if (DeserializationListener)
5659 DeserializationListener->DeclRead(
5660 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5661 D: Context.getTranslationUnitDecl());
5662
5663 // FIXME: Find a better way to deal with collisions between these
5664 // built-in types. Right now, we just ignore the problem.
5665
5666 // Load the special types.
5667 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5668 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5669 if (!Context.CFConstantStringTypeDecl)
5670 Context.setCFConstantStringType(GetType(ID: String));
5671 }
5672
5673 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5674 QualType FileType = GetType(ID: File);
5675 if (FileType.isNull()) {
5676 Error(Msg: "FILE type is NULL");
5677 return;
5678 }
5679
5680 if (!Context.FILEDecl) {
5681 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5682 Context.setFILEDecl(Typedef->getDecl());
5683 else {
5684 const TagType *Tag = FileType->getAs<TagType>();
5685 if (!Tag) {
5686 Error(Msg: "Invalid FILE type in AST file");
5687 return;
5688 }
5689 Context.setFILEDecl(Tag->getDecl());
5690 }
5691 }
5692 }
5693
5694 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5695 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5696 if (Jmp_bufType.isNull()) {
5697 Error(Msg: "jmp_buf type is NULL");
5698 return;
5699 }
5700
5701 if (!Context.jmp_bufDecl) {
5702 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5703 Context.setjmp_bufDecl(Typedef->getDecl());
5704 else {
5705 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5706 if (!Tag) {
5707 Error(Msg: "Invalid jmp_buf type in AST file");
5708 return;
5709 }
5710 Context.setjmp_bufDecl(Tag->getDecl());
5711 }
5712 }
5713 }
5714
5715 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5716 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5717 if (Sigjmp_bufType.isNull()) {
5718 Error(Msg: "sigjmp_buf type is NULL");
5719 return;
5720 }
5721
5722 if (!Context.sigjmp_bufDecl) {
5723 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5724 Context.setsigjmp_bufDecl(Typedef->getDecl());
5725 else {
5726 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5727 assert(Tag && "Invalid sigjmp_buf type in AST file");
5728 Context.setsigjmp_bufDecl(Tag->getDecl());
5729 }
5730 }
5731 }
5732
5733 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5734 if (Context.ObjCIdRedefinitionType.isNull())
5735 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5736 }
5737
5738 if (TypeID ObjCClassRedef =
5739 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5740 if (Context.ObjCClassRedefinitionType.isNull())
5741 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5742 }
5743
5744 if (TypeID ObjCSelRedef =
5745 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5746 if (Context.ObjCSelRedefinitionType.isNull())
5747 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5748 }
5749
5750 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5751 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5752 if (Ucontext_tType.isNull()) {
5753 Error(Msg: "ucontext_t type is NULL");
5754 return;
5755 }
5756
5757 if (!Context.ucontext_tDecl) {
5758 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5759 Context.setucontext_tDecl(Typedef->getDecl());
5760 else {
5761 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5762 assert(Tag && "Invalid ucontext_t type in AST file");
5763 Context.setucontext_tDecl(Tag->getDecl());
5764 }
5765 }
5766 }
5767 }
5768
5769 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5770
5771 // If there were any CUDA special declarations, deserialize them.
5772 if (!CUDASpecialDeclRefs.empty()) {
5773 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5774 Context.setcudaConfigureCallDecl(
5775 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5776 Context.setcudaGetParameterBufferDecl(
5777 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5778 Context.setcudaLaunchDeviceDecl(
5779 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5780 }
5781
5782 // Re-export any modules that were imported by a non-module AST file.
5783 // FIXME: This does not make macro-only imports visible again.
5784 for (auto &Import : PendingImportedModules) {
5785 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5786 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5787 /*ImportLoc=*/Import.ImportLoc);
5788 if (Import.ImportLoc.isValid())
5789 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5790 // This updates visibility for Preprocessor only. For Sema, which can be
5791 // nullptr here, we do the same later, in UpdateSema().
5792 }
5793 }
5794
5795 // Hand off these modules to Sema.
5796 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5797 PendingImportedModules.clear();
5798}
5799
5800void ASTReader::finalizeForWriting() {
5801 // Nothing to do for now.
5802}
5803
5804/// Reads and return the signature record from \p PCH's control block, or
5805/// else returns 0.
5806static ASTFileSignature readASTFileSignature(StringRef PCH) {
5807 BitstreamCursor Stream(PCH);
5808 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5809 // FIXME this drops the error on the floor.
5810 consumeError(Err: std::move(Err));
5811 return ASTFileSignature();
5812 }
5813
5814 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5815 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5816 return ASTFileSignature();
5817
5818 // Scan for SIGNATURE inside the diagnostic options block.
5819 ASTReader::RecordData Record;
5820 while (true) {
5821 Expected<llvm::BitstreamEntry> MaybeEntry =
5822 Stream.advanceSkippingSubblocks();
5823 if (!MaybeEntry) {
5824 // FIXME this drops the error on the floor.
5825 consumeError(Err: MaybeEntry.takeError());
5826 return ASTFileSignature();
5827 }
5828 llvm::BitstreamEntry Entry = MaybeEntry.get();
5829
5830 if (Entry.Kind != llvm::BitstreamEntry::Record)
5831 return ASTFileSignature();
5832
5833 Record.clear();
5834 StringRef Blob;
5835 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5836 if (!MaybeRecord) {
5837 // FIXME this drops the error on the floor.
5838 consumeError(Err: MaybeRecord.takeError());
5839 return ASTFileSignature();
5840 }
5841 if (SIGNATURE == MaybeRecord.get()) {
5842 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5843 assert(Signature != ASTFileSignature::createDummy() &&
5844 "Dummy AST file signature not backpatched in ASTWriter.");
5845 return Signature;
5846 }
5847 }
5848}
5849
5850/// Retrieve the name of the original source file name
5851/// directly from the AST file, without actually loading the AST
5852/// file.
5853std::string ASTReader::getOriginalSourceFile(
5854 const std::string &ASTFileName, FileManager &FileMgr,
5855 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5856 // Open the AST file.
5857 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5858 /*RequiresNullTerminator=*/false,
5859 /*MaybeLimit=*/std::nullopt,
5860 /*IsText=*/false);
5861 if (!Buffer) {
5862 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5863 << ASTFileName << Buffer.getError().message();
5864 return std::string();
5865 }
5866
5867 // Initialize the stream
5868 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5869
5870 // Sniff for the signature.
5871 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5872 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5873 return std::string();
5874 }
5875
5876 // Scan for the CONTROL_BLOCK_ID block.
5877 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5878 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5879 return std::string();
5880 }
5881
5882 // Scan for ORIGINAL_FILE inside the control block.
5883 RecordData Record;
5884 while (true) {
5885 Expected<llvm::BitstreamEntry> MaybeEntry =
5886 Stream.advanceSkippingSubblocks();
5887 if (!MaybeEntry) {
5888 // FIXME this drops errors on the floor.
5889 consumeError(Err: MaybeEntry.takeError());
5890 return std::string();
5891 }
5892 llvm::BitstreamEntry Entry = MaybeEntry.get();
5893
5894 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5895 return std::string();
5896
5897 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5898 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5899 return std::string();
5900 }
5901
5902 Record.clear();
5903 StringRef Blob;
5904 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5905 if (!MaybeRecord) {
5906 // FIXME this drops the errors on the floor.
5907 consumeError(Err: MaybeRecord.takeError());
5908 return std::string();
5909 }
5910 if (ORIGINAL_FILE == MaybeRecord.get())
5911 return Blob.str();
5912 }
5913}
5914
5915namespace {
5916
5917 class SimplePCHValidator : public ASTReaderListener {
5918 const LangOptions &ExistingLangOpts;
5919 const CodeGenOptions &ExistingCGOpts;
5920 const TargetOptions &ExistingTargetOpts;
5921 const PreprocessorOptions &ExistingPPOpts;
5922 const HeaderSearchOptions &ExistingHSOpts;
5923 std::string ExistingSpecificModuleCachePath;
5924 FileManager &FileMgr;
5925 bool StrictOptionMatches;
5926
5927 public:
5928 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5929 const CodeGenOptions &ExistingCGOpts,
5930 const TargetOptions &ExistingTargetOpts,
5931 const PreprocessorOptions &ExistingPPOpts,
5932 const HeaderSearchOptions &ExistingHSOpts,
5933 StringRef ExistingSpecificModuleCachePath,
5934 FileManager &FileMgr, bool StrictOptionMatches)
5935 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5936 ExistingTargetOpts(ExistingTargetOpts),
5937 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5938 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5939 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5940
5941 bool ReadLanguageOptions(const LangOptions &LangOpts,
5942 StringRef ModuleFilename, bool Complain,
5943 bool AllowCompatibleDifferences) override {
5944 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5945 Diags: nullptr, AllowCompatibleDifferences);
5946 }
5947
5948 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5949 StringRef ModuleFilename, bool Complain,
5950 bool AllowCompatibleDifferences) override {
5951 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5952 Diags: nullptr, AllowCompatibleDifferences);
5953 }
5954
5955 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5956 StringRef ModuleFilename, bool Complain,
5957 bool AllowCompatibleDifferences) override {
5958 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
5959 Diags: nullptr, AllowCompatibleDifferences);
5960 }
5961
5962 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5963 StringRef ASTFilename, StringRef ContextHash,
5964 bool Complain) override {
5965 return checkModuleCachePath(
5966 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
5967 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5968 }
5969
5970 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5971 StringRef ModuleFilename, bool ReadMacros,
5972 bool Complain,
5973 std::string &SuggestedPredefines) override {
5974 return checkPreprocessorOptions(
5975 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5976 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5977 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5978 : OptionValidateContradictions);
5979 }
5980 };
5981
5982} // namespace
5983
5984bool ASTReader::readASTFileControlBlock(
5985 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5986 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5987 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5988 unsigned ClientLoadCapabilities) {
5989 // Open the AST file.
5990 off_t Size;
5991 time_t ModTime;
5992 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5993 llvm::MemoryBuffer *Buffer =
5994 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
5995 if (!Buffer) {
5996 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5997 // read again later, but we do not have the context here to determine if it
5998 // is safe to change the result of InMemoryModuleCache::getPCMState().
5999
6000 // FIXME: This allows use of the VFS; we do not allow use of the
6001 // VFS when actually loading a module.
6002 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
6003 : FileMgr.getFileRef(Filename,
6004 /*OpenFile=*/false,
6005 /*CacheFailure=*/true,
6006 /*IsText=*/false);
6007 if (!Entry) {
6008 llvm::consumeError(Err: Entry.takeError());
6009 return true;
6010 }
6011 auto BufferOrErr =
6012 FileMgr.getBufferForFile(Entry: *Entry,
6013 /*IsVolatile=*/isVolatile: false,
6014 /*RequiresNullTerminator=*/false,
6015 /*MaybeLimit=*/std::nullopt,
6016 /*IsText=*/false);
6017 if (!BufferOrErr)
6018 return true;
6019 OwnedBuffer = std::move(*BufferOrErr);
6020 Buffer = OwnedBuffer.get();
6021 }
6022
6023 // Initialize the stream
6024 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6025 BitstreamCursor Stream(Bytes);
6026
6027 // Sniff for the signature.
6028 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6029 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6030 return true;
6031 }
6032
6033 // Scan for the CONTROL_BLOCK_ID block.
6034 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6035 return true;
6036
6037 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6038 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6039 bool NeedsImports = Listener.needsImportVisitation();
6040 BitstreamCursor InputFilesCursor;
6041 uint64_t InputFilesOffsetBase = 0;
6042
6043 RecordData Record;
6044 std::string ModuleDir;
6045 bool DoneWithControlBlock = false;
6046 SmallString<0> PathBuf;
6047 PathBuf.reserve(N: 256);
6048 // Additional path buffer to use when multiple paths need to be resolved.
6049 // For example, when deserializing input files that contains a path that was
6050 // resolved from a vfs overlay and an external location.
6051 SmallString<0> AdditionalPathBuf;
6052 AdditionalPathBuf.reserve(N: 256);
6053 while (!DoneWithControlBlock) {
6054 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6055 if (!MaybeEntry) {
6056 // FIXME this drops the error on the floor.
6057 consumeError(Err: MaybeEntry.takeError());
6058 return true;
6059 }
6060 llvm::BitstreamEntry Entry = MaybeEntry.get();
6061
6062 switch (Entry.Kind) {
6063 case llvm::BitstreamEntry::SubBlock: {
6064 switch (Entry.ID) {
6065 case OPTIONS_BLOCK_ID: {
6066 std::string IgnoredSuggestedPredefines;
6067 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6068 /*AllowCompatibleConfigurationMismatch*/ false,
6069 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6070 return true;
6071 break;
6072 }
6073
6074 case INPUT_FILES_BLOCK_ID:
6075 InputFilesCursor = Stream;
6076 if (llvm::Error Err = Stream.SkipBlock()) {
6077 // FIXME this drops the error on the floor.
6078 consumeError(Err: std::move(Err));
6079 return true;
6080 }
6081 if (NeedsInputFiles &&
6082 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6083 return true;
6084 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6085 break;
6086
6087 default:
6088 if (llvm::Error Err = Stream.SkipBlock()) {
6089 // FIXME this drops the error on the floor.
6090 consumeError(Err: std::move(Err));
6091 return true;
6092 }
6093 break;
6094 }
6095
6096 continue;
6097 }
6098
6099 case llvm::BitstreamEntry::EndBlock:
6100 DoneWithControlBlock = true;
6101 break;
6102
6103 case llvm::BitstreamEntry::Error:
6104 return true;
6105
6106 case llvm::BitstreamEntry::Record:
6107 break;
6108 }
6109
6110 if (DoneWithControlBlock) break;
6111
6112 Record.clear();
6113 StringRef Blob;
6114 Expected<unsigned> MaybeRecCode =
6115 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6116 if (!MaybeRecCode) {
6117 // FIXME this drops the error.
6118 return Failure;
6119 }
6120 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6121 case METADATA:
6122 if (Record[0] != VERSION_MAJOR)
6123 return true;
6124 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6125 return true;
6126 break;
6127 case MODULE_NAME:
6128 Listener.ReadModuleName(ModuleName: Blob);
6129 break;
6130 case MODULE_DIRECTORY:
6131 ModuleDir = std::string(Blob);
6132 break;
6133 case MODULE_MAP_FILE: {
6134 unsigned Idx = 0;
6135 std::string PathStr = ReadString(Record, Idx);
6136 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6137 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6138 break;
6139 }
6140 case INPUT_FILE_OFFSETS: {
6141 if (!NeedsInputFiles)
6142 break;
6143
6144 unsigned NumInputFiles = Record[0];
6145 unsigned NumUserFiles = Record[1];
6146 const llvm::support::unaligned_uint64_t *InputFileOffs =
6147 (const llvm::support::unaligned_uint64_t *)Blob.data();
6148 for (unsigned I = 0; I != NumInputFiles; ++I) {
6149 // Go find this input file.
6150 bool isSystemFile = I >= NumUserFiles;
6151
6152 if (isSystemFile && !NeedsSystemInputFiles)
6153 break; // the rest are system input files
6154
6155 BitstreamCursor &Cursor = InputFilesCursor;
6156 SavedStreamPosition SavedPosition(Cursor);
6157 if (llvm::Error Err =
6158 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6159 // FIXME this drops errors on the floor.
6160 consumeError(Err: std::move(Err));
6161 }
6162
6163 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6164 if (!MaybeCode) {
6165 // FIXME this drops errors on the floor.
6166 consumeError(Err: MaybeCode.takeError());
6167 }
6168 unsigned Code = MaybeCode.get();
6169
6170 RecordData Record;
6171 StringRef Blob;
6172 bool shouldContinue = false;
6173 Expected<unsigned> MaybeRecordType =
6174 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6175 if (!MaybeRecordType) {
6176 // FIXME this drops errors on the floor.
6177 consumeError(Err: MaybeRecordType.takeError());
6178 }
6179 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6180 case INPUT_FILE_HASH:
6181 break;
6182 case INPUT_FILE:
6183 time_t StoredTime = static_cast<time_t>(Record[2]);
6184 bool Overridden = static_cast<bool>(Record[3]);
6185 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6186 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6187 auto FilenameAsRequestedBuf = ResolveImportedPath(
6188 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6189 StringRef Filename;
6190 if (UnresolvedFilename.empty())
6191 Filename = *FilenameAsRequestedBuf;
6192 else {
6193 auto FilenameBuf = ResolveImportedPath(
6194 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6195 Filename = *FilenameBuf;
6196 }
6197 shouldContinue = Listener.visitInputFileAsRequested(
6198 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6199 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6200 break;
6201 }
6202 if (!shouldContinue)
6203 break;
6204 }
6205 break;
6206 }
6207
6208 case IMPORT: {
6209 if (!NeedsImports)
6210 break;
6211
6212 unsigned Idx = 0;
6213 // Read information about the AST file.
6214
6215 // Skip Kind
6216 Idx++;
6217
6218 // Skip ImportLoc
6219 Idx++;
6220
6221 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6222
6223 bool IsStandardCXXModule = Record[Idx++];
6224
6225 // In C++20 Modules, we don't record the path to imported
6226 // modules in the BMI files.
6227 if (IsStandardCXXModule) {
6228 Listener.visitImport(ModuleName, /*Filename=*/"");
6229 continue;
6230 }
6231
6232 // Skip Size, ModTime and ImplicitModuleSuffix.
6233 Idx += 1 + 1 + 1;
6234 // Skip signature.
6235 Blob = Blob.substr(Start: ASTFileSignature::size);
6236
6237 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6238 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6239 Listener.visitImport(ModuleName, Filename: *Filename);
6240 break;
6241 }
6242
6243 default:
6244 // No other validation to perform.
6245 break;
6246 }
6247 }
6248
6249 // Look for module file extension blocks, if requested.
6250 if (FindModuleFileExtensions) {
6251 BitstreamCursor SavedStream = Stream;
6252 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6253 bool DoneWithExtensionBlock = false;
6254 while (!DoneWithExtensionBlock) {
6255 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6256 if (!MaybeEntry) {
6257 // FIXME this drops the error.
6258 return true;
6259 }
6260 llvm::BitstreamEntry Entry = MaybeEntry.get();
6261
6262 switch (Entry.Kind) {
6263 case llvm::BitstreamEntry::SubBlock:
6264 if (llvm::Error Err = Stream.SkipBlock()) {
6265 // FIXME this drops the error on the floor.
6266 consumeError(Err: std::move(Err));
6267 return true;
6268 }
6269 continue;
6270
6271 case llvm::BitstreamEntry::EndBlock:
6272 DoneWithExtensionBlock = true;
6273 continue;
6274
6275 case llvm::BitstreamEntry::Error:
6276 return true;
6277
6278 case llvm::BitstreamEntry::Record:
6279 break;
6280 }
6281
6282 Record.clear();
6283 StringRef Blob;
6284 Expected<unsigned> MaybeRecCode =
6285 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6286 if (!MaybeRecCode) {
6287 // FIXME this drops the error.
6288 return true;
6289 }
6290 switch (MaybeRecCode.get()) {
6291 case EXTENSION_METADATA: {
6292 ModuleFileExtensionMetadata Metadata;
6293 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6294 return true;
6295
6296 Listener.readModuleFileExtension(Metadata);
6297 break;
6298 }
6299 }
6300 }
6301 }
6302 Stream = std::move(SavedStream);
6303 }
6304
6305 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6306 if (readUnhashedControlBlockImpl(
6307 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6308 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6309 ValidateDiagnosticOptions) != Success)
6310 return true;
6311
6312 return false;
6313}
6314
6315bool ASTReader::isAcceptableASTFile(
6316 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6317 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6318 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6319 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6320 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6321 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6322 SpecificModuleCachePath, FileMgr,
6323 RequireStrictOptionMatches);
6324 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6325 /*FindModuleFileExtensions=*/false, Listener&: validator,
6326 /*ValidateDiagnosticOptions=*/true);
6327}
6328
6329Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6330 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6331 assert(GlobalID == 0 && "Unhandled global submodule ID");
6332 return nullptr;
6333 }
6334
6335 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6336 if (GlobalIndex >= SubmodulesLoaded.size()) {
6337 Error(Msg: "submodule ID out of range in AST file");
6338 return nullptr;
6339 }
6340
6341 if (SubmodulesLoaded[GlobalIndex])
6342 return SubmodulesLoaded[GlobalIndex];
6343
6344 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6345 assert(It != GlobalSubmoduleMap.end());
6346 ModuleFile &F = *It->second;
6347 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6348 [[maybe_unused]] unsigned LocalID =
6349 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6350
6351 BitstreamCursor &Cursor = F.SubmodulesCursor;
6352 SavedStreamPosition SavedPosition(Cursor);
6353 unsigned Offset = F.SubmoduleOffsets[Index];
6354 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.SubmodulesOffsetBase + Offset)) {
6355 Error(Err: std::move(Err));
6356 return nullptr;
6357 }
6358
6359 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6360 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6361 // If we don't know the top-level module, there's no point in doing qualified
6362 // lookup of its submodules; it won't find anything anywhere within this tree.
6363 // Let's skip that and avoid some string lookups.
6364 auto CreateModule = !KnowsTopLevelModule
6365 ? &ModuleMap::createModule
6366 : &ModuleMap::findOrCreateModuleFirst;
6367
6368 Module *CurrentModule = nullptr;
6369 RecordData Record;
6370 while (true) {
6371 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6372 if (!MaybeEntry) {
6373 Error(Err: MaybeEntry.takeError());
6374 return nullptr;
6375 }
6376 llvm::BitstreamEntry Entry = MaybeEntry.get();
6377
6378 switch (Entry.Kind) {
6379 case llvm::BitstreamEntry::SubBlock:
6380 case llvm::BitstreamEntry::Error:
6381 case llvm::BitstreamEntry::EndBlock: {
6382 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6383 Fmt: "malformed block record in AST file"));
6384 return nullptr;
6385 }
6386 case llvm::BitstreamEntry::Record:
6387 // The interesting case.
6388 break;
6389 }
6390
6391 // Read a record.
6392 StringRef Blob;
6393 Record.clear();
6394 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6395 if (!MaybeKind) {
6396 Error(Err: MaybeKind.takeError());
6397 return nullptr;
6398 }
6399 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6400
6401 switch (Kind) {
6402 case SUBMODULE_END:
6403 if (!CurrentModule) {
6404 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6405 Fmt: "malformed module definition"));
6406 return nullptr;
6407 }
6408 return CurrentModule;
6409
6410 case SUBMODULE_DEFINITION: {
6411 if (Record.size() < 13) {
6412 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6413 Fmt: "malformed module definition"));
6414 return nullptr;
6415 }
6416
6417 StringRef Name = Blob;
6418 unsigned Idx = 0;
6419 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6420 assert(LocalID == ReadLocalID);
6421 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6422 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6423 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6424 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6425 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6426 bool IsFramework = Record[Idx++];
6427 bool IsExplicit = Record[Idx++];
6428 bool IsSystem = Record[Idx++];
6429 bool IsExternC = Record[Idx++];
6430 bool InferSubmodules = Record[Idx++];
6431 bool InferExplicitSubmodules = Record[Idx++];
6432 bool InferExportWildcard = Record[Idx++];
6433 bool ConfigMacrosExhaustive = Record[Idx++];
6434 bool ModuleMapIsPrivate = Record[Idx++];
6435 bool NamedModuleHasInit = Record[Idx++];
6436
6437 Module *ParentModule = nullptr;
6438 if (Parent) {
6439 ParentModule = getSubmodule(GlobalID: Parent);
6440 if (!ParentModule)
6441 return nullptr;
6442 }
6443
6444 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6445 args&: IsFramework, args&: IsExplicit);
6446
6447 if (!ParentModule) {
6448 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6449 CurrentModule->getASTFileKey()) {
6450 // Don't emit module relocation error if we have -fno-validate-pch
6451 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6452 DisableValidationForModuleKind::Module)) {
6453 assert(*CurFileKey != F.FileKey &&
6454 "ModuleManager did not de-duplicate");
6455
6456 Diag(DiagID: diag::err_module_file_conflict)
6457 << CurrentModule->getTopLevelModuleName()
6458 << *CurrentModule->getASTFileName() << F.FileName;
6459
6460 auto CurModMapFile =
6461 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6462 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6463 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6464 Diag(DiagID: diag::note_module_file_conflict)
6465 << CurModMapFile->getName() << ModMapFile->getName();
6466
6467 return nullptr;
6468 }
6469 }
6470
6471 F.DidReadTopLevelSubmodule = true;
6472 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6473 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6474 }
6475
6476 CurrentModule->Kind = Kind;
6477 // Note that we may be rewriting an existing location and it is important
6478 // to keep doing that. In particular, we would like to prefer a
6479 // `DefinitionLoc` loaded from the module file instead of the location
6480 // created in the current source manager, because it allows the new
6481 // location to be marked as "unaffecting" when writing and avoid creating
6482 // duplicate locations for the same module map file.
6483 CurrentModule->DefinitionLoc = DefinitionLoc;
6484 CurrentModule->Signature = F.Signature;
6485 CurrentModule->IsFromModuleFile = true;
6486 if (InferredAllowedBy.isValid())
6487 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6488 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6489 CurrentModule->IsExternC = IsExternC;
6490 CurrentModule->InferSubmodules = InferSubmodules;
6491 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6492 CurrentModule->InferExportWildcard = InferExportWildcard;
6493 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6494 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6495 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6496
6497 if (!ParentModule && !F.BaseDirectory.empty()) {
6498 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6499 CurrentModule->Directory = *Dir;
6500 } else if (ParentModule && ParentModule->Directory) {
6501 // Submodules inherit the directory from their parent.
6502 CurrentModule->Directory = ParentModule->Directory;
6503 }
6504
6505 if (DeserializationListener)
6506 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6507
6508 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6509
6510 // Clear out data that will be replaced by what is in the module file.
6511 CurrentModule->LinkLibraries.clear();
6512 CurrentModule->ConfigMacros.clear();
6513 CurrentModule->UnresolvedConflicts.clear();
6514 CurrentModule->Conflicts.clear();
6515
6516 // The module is available unless it's missing a requirement; relevant
6517 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6518 // Missing headers that were present when the module was built do not
6519 // make it unavailable -- if we got this far, this must be an explicitly
6520 // imported module file.
6521 CurrentModule->Requirements.clear();
6522 CurrentModule->MissingHeaders.clear();
6523 CurrentModule->IsUnimportable =
6524 ParentModule && ParentModule->IsUnimportable;
6525 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6526 break;
6527 }
6528
6529 case SUBMODULE_UMBRELLA_HEADER: {
6530 SmallString<128> RelativePathName;
6531 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6532 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6533 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6534 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6535 PathRelativeToRootModuleDirectory: RelativePathName);
6536 }
6537 // Note that it's too late at this point to return out of date if the
6538 // name from the PCM doesn't match up with the one in the module map,
6539 // but also quite unlikely since we will have already checked the
6540 // modification time and size of the module map file itself.
6541 }
6542 break;
6543 }
6544
6545 case SUBMODULE_HEADER:
6546 case SUBMODULE_EXCLUDED_HEADER:
6547 case SUBMODULE_PRIVATE_HEADER:
6548 // We lazily associate headers with their modules via the HeaderInfo table.
6549 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6550 // of complete filenames or remove it entirely.
6551 break;
6552
6553 case SUBMODULE_TEXTUAL_HEADER:
6554 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6555 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6556 // them here.
6557 break;
6558
6559 case SUBMODULE_TOPHEADER: {
6560 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6561 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6562 break;
6563 }
6564
6565 case SUBMODULE_UMBRELLA_DIR: {
6566 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6567 if (auto Umbrella =
6568 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6569 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6570 // FIXME: NameAsWritten
6571 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6572 }
6573 }
6574 break;
6575 }
6576
6577 case SUBMODULE_IMPORTS:
6578 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6579 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6580 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6581 }
6582 break;
6583
6584 case SUBMODULE_AFFECTING_MODULES:
6585 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6586 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6587 CurrentModule->AffectingClangModules.push_back(
6588 Elt: ModuleRef(this, GlobalID));
6589 }
6590 break;
6591
6592 case SUBMODULE_EXPORTS:
6593 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6594 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6595 bool IsWildcard = Record[Idx + 1];
6596 ModuleRef ExportedMod =
6597 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6598 if (ExportedMod || IsWildcard)
6599 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6600 }
6601
6602 // Once we've loaded the set of exports, there's no reason to keep
6603 // the parsed, unresolved exports around.
6604 CurrentModule->UnresolvedExports.clear();
6605 break;
6606
6607 case SUBMODULE_REQUIRES:
6608 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6609 Target: PP.getTargetInfo());
6610 break;
6611
6612 case SUBMODULE_LINK_LIBRARY:
6613 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6614 CurrentModule->LinkLibraries.push_back(
6615 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6616 break;
6617
6618 case SUBMODULE_CONFIG_MACRO:
6619 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6620 break;
6621
6622 case SUBMODULE_CONFLICT: {
6623 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6624 Module::Conflict Conflict;
6625 Conflict.Other = ModuleRef(this, GlobalID);
6626 Conflict.Message = Blob.str();
6627 CurrentModule->Conflicts.push_back(x: Conflict);
6628 break;
6629 }
6630
6631 case SUBMODULE_INITIALIZERS: {
6632 if (!ContextObj)
6633 break;
6634 // Standard C++ module has its own way to initialize variables.
6635 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6636 SmallVector<GlobalDeclID, 16> Inits;
6637 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6638 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6639 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6640 }
6641 break;
6642 }
6643
6644 case SUBMODULE_EXPORT_AS:
6645 CurrentModule->ExportAsModule = Blob.str();
6646 ModMap.addLinkAsDependency(Mod: CurrentModule);
6647 break;
6648
6649 case SUBMODULE_CHILD: {
6650 // Record a not-yet-loaded direct child for on-demand deserialization.
6651 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6652 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6653 break;
6654 }
6655 }
6656 }
6657}
6658
6659/// Parse the record that corresponds to a LangOptions data
6660/// structure.
6661///
6662/// This routine parses the language options from the AST file and then gives
6663/// them to the AST listener if one is set.
6664///
6665/// \returns true if the listener deems the file unacceptable, false otherwise.
6666bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6667 StringRef ModuleFilename, bool Complain,
6668 ASTReaderListener &Listener,
6669 bool AllowCompatibleDifferences) {
6670 LangOptions LangOpts;
6671 unsigned Idx = 0;
6672#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6673 LangOpts.Name = Record[Idx++];
6674#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6675 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6676#include "clang/Basic/LangOptions.def"
6677#define SANITIZER(NAME, ID) \
6678 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6679#include "clang/Basic/Sanitizers.def"
6680
6681 for (unsigned N = Record[Idx++]; N; --N)
6682 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6683
6684 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6685 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6686 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6687
6688 LangOpts.CurrentModule = ReadString(Record, Idx);
6689
6690 // Comment options.
6691 for (unsigned N = Record[Idx++]; N; --N) {
6692 LangOpts.CommentOpts.BlockCommandNames.push_back(
6693 x: ReadString(Record, Idx));
6694 }
6695 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6696
6697 // OpenMP offloading options.
6698 for (unsigned N = Record[Idx++]; N; --N) {
6699 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6700 }
6701
6702 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6703
6704 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6705 AllowCompatibleDifferences);
6706}
6707
6708bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6709 StringRef ModuleFilename, bool Complain,
6710 ASTReaderListener &Listener,
6711 bool AllowCompatibleDifferences) {
6712 unsigned Idx = 0;
6713 CodeGenOptions CGOpts;
6714 using CK = CodeGenOptions::CompatibilityKind;
6715#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6716 if constexpr (CK::Compatibility != CK::Benign) \
6717 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6718#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6719 if constexpr (CK::Compatibility != CK::Benign) \
6720 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6721#define DEBUGOPT(Name, Bits, Default, Compatibility)
6722#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6723#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6724#include "clang/Basic/CodeGenOptions.def"
6725
6726 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6727 AllowCompatibleDifferences);
6728}
6729
6730bool ASTReader::ParseTargetOptions(const RecordData &Record,
6731 StringRef ModuleFilename, bool Complain,
6732 ASTReaderListener &Listener,
6733 bool AllowCompatibleDifferences) {
6734 unsigned Idx = 0;
6735 TargetOptions TargetOpts;
6736 TargetOpts.Triple = ReadString(Record, Idx);
6737 TargetOpts.CPU = ReadString(Record, Idx);
6738 TargetOpts.TuneCPU = ReadString(Record, Idx);
6739 TargetOpts.ABI = ReadString(Record, Idx);
6740 for (unsigned N = Record[Idx++]; N; --N) {
6741 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6742 }
6743 for (unsigned N = Record[Idx++]; N; --N) {
6744 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6745 }
6746
6747 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6748 AllowCompatibleDifferences);
6749}
6750
6751bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6752 StringRef ModuleFilename, bool Complain,
6753 ASTReaderListener &Listener) {
6754 DiagnosticOptions DiagOpts;
6755 unsigned Idx = 0;
6756#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6757#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6758 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6759#include "clang/Basic/DiagnosticOptions.def"
6760
6761 for (unsigned N = Record[Idx++]; N; --N)
6762 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6763 for (unsigned N = Record[Idx++]; N; --N)
6764 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6765
6766 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6767}
6768
6769bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6770 ASTReaderListener &Listener) {
6771 FileSystemOptions FSOpts;
6772 unsigned Idx = 0;
6773 FSOpts.WorkingDir = ReadString(Record, Idx);
6774 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6775}
6776
6777bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6778 StringRef ModuleFilename,
6779 bool Complain,
6780 ASTReaderListener &Listener) {
6781 HeaderSearchOptions HSOpts;
6782 unsigned Idx = 0;
6783 HSOpts.Sysroot = ReadString(Record, Idx);
6784
6785 HSOpts.ResourceDir = ReadString(Record, Idx);
6786 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6787 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6788 HSOpts.DisableModuleHash = Record[Idx++];
6789 HSOpts.ImplicitModuleMaps = Record[Idx++];
6790 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6791 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6792 HSOpts.UseBuiltinIncludes = Record[Idx++];
6793 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6794 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6795 HSOpts.UseLibcxx = Record[Idx++];
6796 std::string ContextHash = ReadString(Record, Idx);
6797
6798 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6799 Complain);
6800}
6801
6802bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6803 ASTReaderListener &Listener) {
6804 HeaderSearchOptions HSOpts;
6805 unsigned Idx = 0;
6806
6807 // Include entries.
6808 for (unsigned N = Record[Idx++]; N; --N) {
6809 std::string Path = ReadString(Record, Idx);
6810 frontend::IncludeDirGroup Group
6811 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6812 bool IsFramework = Record[Idx++];
6813 bool IgnoreSysRoot = Record[Idx++];
6814 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6815 args&: IgnoreSysRoot);
6816 }
6817
6818 // System header prefixes.
6819 for (unsigned N = Record[Idx++]; N; --N) {
6820 std::string Prefix = ReadString(Record, Idx);
6821 bool IsSystemHeader = Record[Idx++];
6822 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6823 }
6824
6825 // VFS overlay files.
6826 for (unsigned N = Record[Idx++]; N; --N) {
6827 std::string VFSOverlayFile = ReadString(Record, Idx);
6828 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6829 }
6830
6831 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6832}
6833
6834bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6835 StringRef ModuleFilename,
6836 bool Complain,
6837 ASTReaderListener &Listener,
6838 std::string &SuggestedPredefines) {
6839 PreprocessorOptions PPOpts;
6840 unsigned Idx = 0;
6841
6842 // Macro definitions/undefs
6843 bool ReadMacros = Record[Idx++];
6844 if (ReadMacros) {
6845 for (unsigned N = Record[Idx++]; N; --N) {
6846 std::string Macro = ReadString(Record, Idx);
6847 bool IsUndef = Record[Idx++];
6848 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6849 }
6850 }
6851
6852 // Includes
6853 for (unsigned N = Record[Idx++]; N; --N) {
6854 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6855 }
6856
6857 // Macro Includes
6858 for (unsigned N = Record[Idx++]; N; --N) {
6859 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6860 }
6861
6862 PPOpts.UsePredefines = Record[Idx++];
6863 PPOpts.DetailedRecord = Record[Idx++];
6864 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6865 PPOpts.ObjCXXARCStandardLibrary =
6866 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6867 SuggestedPredefines.clear();
6868 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6869 Complain, SuggestedPredefines);
6870}
6871
6872std::pair<ModuleFile *, unsigned>
6873ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6874 GlobalPreprocessedEntityMapType::iterator
6875 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6876 assert(I != GlobalPreprocessedEntityMap.end() &&
6877 "Corrupted global preprocessed entity map");
6878 ModuleFile *M = I->second;
6879 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6880 return std::make_pair(x&: M, y&: LocalIndex);
6881}
6882
6883llvm::iterator_range<PreprocessingRecord::iterator>
6884ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6885 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6886 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6887 count: Mod.NumPreprocessedEntities);
6888
6889 return llvm::make_range(x: PreprocessingRecord::iterator(),
6890 y: PreprocessingRecord::iterator());
6891}
6892
6893bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6894 unsigned int ClientLoadCapabilities) {
6895 return ClientLoadCapabilities & ARR_OutOfDate &&
6896 !getModuleManager()
6897 .getModuleCache()
6898 .getInMemoryModuleCache()
6899 .isPCMFinal(Filename: ModuleFileName);
6900}
6901
6902llvm::iterator_range<ASTReader::ModuleDeclIterator>
6903ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6904 return llvm::make_range(
6905 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6906 y: ModuleDeclIterator(this, &Mod,
6907 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6908}
6909
6910SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6911 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6912 assert(I != GlobalSkippedRangeMap.end() &&
6913 "Corrupted global skipped range map");
6914 ModuleFile *M = I->second;
6915 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6916 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6917 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6918 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6919 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6920 assert(Range.isValid());
6921 return Range;
6922}
6923
6924unsigned
6925ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6926 unsigned ModuleFileIndex = ID >> 32;
6927 assert(ModuleFileIndex && "not translating loaded MacroID?");
6928 assert(getModuleManager().size() > ModuleFileIndex - 1);
6929 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6930
6931 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6932 return MF.BasePreprocessedEntityID + ID;
6933}
6934
6935PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6936 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6937 ModuleFile &M = *PPInfo.first;
6938 unsigned LocalIndex = PPInfo.second;
6939 PreprocessedEntityID PPID =
6940 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6941 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6942
6943 if (!PP.getPreprocessingRecord()) {
6944 Error(Msg: "no preprocessing record");
6945 return nullptr;
6946 }
6947
6948 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6949 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6950 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6951 Error(Err: std::move(Err));
6952 return nullptr;
6953 }
6954
6955 Expected<llvm::BitstreamEntry> MaybeEntry =
6956 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6957 if (!MaybeEntry) {
6958 Error(Err: MaybeEntry.takeError());
6959 return nullptr;
6960 }
6961 llvm::BitstreamEntry Entry = MaybeEntry.get();
6962
6963 if (Entry.Kind != llvm::BitstreamEntry::Record)
6964 return nullptr;
6965
6966 // Read the record.
6967 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6968 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6969 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6970 StringRef Blob;
6971 RecordData Record;
6972 Expected<unsigned> MaybeRecType =
6973 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6974 if (!MaybeRecType) {
6975 Error(Err: MaybeRecType.takeError());
6976 return nullptr;
6977 }
6978 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6979 case PPD_MACRO_EXPANSION: {
6980 bool isBuiltin = Record[0];
6981 IdentifierInfo *Name = nullptr;
6982 MacroDefinitionRecord *Def = nullptr;
6983 if (isBuiltin)
6984 Name = getLocalIdentifier(M, LocalID: Record[1]);
6985 else {
6986 PreprocessedEntityID GlobalID =
6987 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6988 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
6989 Def =
6990 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
6991 }
6992
6993 MacroExpansion *ME;
6994 if (isBuiltin)
6995 ME = new (PPRec) MacroExpansion(Name, Range);
6996 else
6997 ME = new (PPRec) MacroExpansion(Def, Range);
6998
6999 return ME;
7000 }
7001
7002 case PPD_MACRO_DEFINITION: {
7003 // Decode the identifier info and then check again; if the macro is
7004 // still defined and associated with the identifier,
7005 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
7006 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
7007
7008 if (DeserializationListener)
7009 DeserializationListener->MacroDefinitionRead(PPID, MD);
7010
7011 return MD;
7012 }
7013
7014 case PPD_INCLUSION_DIRECTIVE: {
7015 const char *FullFileNameStart = Blob.data() + Record[0];
7016 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7017 OptionalFileEntryRef File;
7018 if (!FullFileName.empty())
7019 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7020
7021 // FIXME: Stable encoding
7022 InclusionDirective::InclusionKind Kind
7023 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7024 InclusionDirective *ID
7025 = new (PPRec) InclusionDirective(PPRec, Kind,
7026 StringRef(Blob.data(), Record[0]),
7027 Record[1], Record[3],
7028 File,
7029 Range);
7030 return ID;
7031 }
7032 }
7033
7034 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7035}
7036
7037/// Find the next module that contains entities and return the ID
7038/// of the first entry.
7039///
7040/// \param SLocMapI points at a chunk of a module that contains no
7041/// preprocessed entities or the entities it contains are not the ones we are
7042/// looking for.
7043unsigned ASTReader::findNextPreprocessedEntity(
7044 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7045 ++SLocMapI;
7046 for (GlobalSLocOffsetMapType::const_iterator
7047 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7048 ModuleFile &M = *SLocMapI->second;
7049 if (M.NumPreprocessedEntities)
7050 return M.BasePreprocessedEntityID;
7051 }
7052
7053 return getTotalNumPreprocessedEntities();
7054}
7055
7056namespace {
7057
7058struct PPEntityComp {
7059 const ASTReader &Reader;
7060 ModuleFile &M;
7061
7062 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7063
7064 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7065 SourceLocation LHS = getLoc(PPE: L);
7066 SourceLocation RHS = getLoc(PPE: R);
7067 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7068 }
7069
7070 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7071 SourceLocation LHS = getLoc(PPE: L);
7072 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7073 }
7074
7075 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7076 SourceLocation RHS = getLoc(PPE: R);
7077 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7078 }
7079
7080 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7081 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7082 }
7083};
7084
7085} // namespace
7086
7087unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7088 bool EndsAfter) const {
7089 if (SourceMgr.isLocalSourceLocation(Loc))
7090 return getTotalNumPreprocessedEntities();
7091
7092 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7093 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7094 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7095 "Corrupted global sloc offset map");
7096
7097 if (SLocMapI->second->NumPreprocessedEntities == 0)
7098 return findNextPreprocessedEntity(SLocMapI);
7099
7100 ModuleFile &M = *SLocMapI->second;
7101
7102 using pp_iterator = const PPEntityOffset *;
7103
7104 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7105 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7106
7107 size_t Count = M.NumPreprocessedEntities;
7108 size_t Half;
7109 pp_iterator First = pp_begin;
7110 pp_iterator PPI;
7111
7112 if (EndsAfter) {
7113 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7114 comp: PPEntityComp(*this, M));
7115 } else {
7116 // Do a binary search manually instead of using std::lower_bound because
7117 // The end locations of entities may be unordered (when a macro expansion
7118 // is inside another macro argument), but for this case it is not important
7119 // whether we get the first macro expansion or its containing macro.
7120 while (Count > 0) {
7121 Half = Count / 2;
7122 PPI = First;
7123 std::advance(i&: PPI, n: Half);
7124 if (SourceMgr.isBeforeInTranslationUnit(
7125 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7126 First = PPI;
7127 ++First;
7128 Count = Count - Half - 1;
7129 } else
7130 Count = Half;
7131 }
7132 }
7133
7134 if (PPI == pp_end)
7135 return findNextPreprocessedEntity(SLocMapI);
7136
7137 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7138}
7139
7140/// Returns a pair of [Begin, End) indices of preallocated
7141/// preprocessed entities that \arg Range encompasses.
7142std::pair<unsigned, unsigned>
7143 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7144 if (Range.isInvalid())
7145 return std::make_pair(x: 0,y: 0);
7146 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7147
7148 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7149 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7150 return std::make_pair(x&: BeginID, y&: EndID);
7151}
7152
7153/// Optionally returns true or false if the preallocated preprocessed
7154/// entity with index \arg Index came from file \arg FID.
7155std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7156 FileID FID) {
7157 if (FID.isInvalid())
7158 return false;
7159
7160 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7161 ModuleFile &M = *PPInfo.first;
7162 unsigned LocalIndex = PPInfo.second;
7163 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7164
7165 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7166 if (Loc.isInvalid())
7167 return false;
7168
7169 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7170 return true;
7171 else
7172 return false;
7173}
7174
7175namespace {
7176
7177 /// Visitor used to search for information about a header file.
7178 class HeaderFileInfoVisitor {
7179 FileEntryRef FE;
7180 std::optional<HeaderFileInfo> HFI;
7181
7182 public:
7183 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7184
7185 bool operator()(ModuleFile &M) {
7186 HeaderFileInfoLookupTable *Table
7187 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7188 if (!Table)
7189 return false;
7190
7191 // Look in the on-disk hash table for an entry for this file name.
7192 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7193 if (Pos == Table->end())
7194 return false;
7195
7196 HFI = *Pos;
7197 return true;
7198 }
7199
7200 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7201 };
7202
7203} // namespace
7204
7205HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7206 HeaderFileInfoVisitor Visitor(FE);
7207 ModuleMgr.visit(Visitor);
7208 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7209 return *HFI;
7210
7211 return HeaderFileInfo();
7212}
7213
7214void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7215 using DiagState = DiagnosticsEngine::DiagState;
7216 SmallVector<DiagState *, 32> DiagStates;
7217
7218 for (ModuleFile &F : ModuleMgr) {
7219 unsigned Idx = 0;
7220 auto &Record = F.PragmaDiagMappings;
7221 if (Record.empty())
7222 continue;
7223
7224 DiagStates.clear();
7225
7226 auto ReadDiagState = [&](const DiagState &BasedOn,
7227 bool IncludeNonPragmaStates) {
7228 unsigned BackrefID = Record[Idx++];
7229 if (BackrefID != 0)
7230 return DiagStates[BackrefID - 1];
7231
7232 // A new DiagState was created here.
7233 Diag.DiagStates.push_back(x: BasedOn);
7234 DiagState *NewState = &Diag.DiagStates.back();
7235 DiagStates.push_back(Elt: NewState);
7236 unsigned Size = Record[Idx++];
7237 assert(Idx + Size * 2 <= Record.size() &&
7238 "Invalid data, not enough diag/map pairs");
7239 while (Size--) {
7240 unsigned DiagID = Record[Idx++];
7241 DiagnosticMapping NewMapping =
7242 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7243 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7244 continue;
7245
7246 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7247
7248 // If this mapping was specified as a warning but the severity was
7249 // upgraded due to diagnostic settings, simulate the current diagnostic
7250 // settings (and use a warning).
7251 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7252 NewMapping.setSeverity(diag::Severity::Warning);
7253 NewMapping.setUpgradedFromWarning(false);
7254 }
7255
7256 Mapping = NewMapping;
7257 }
7258 return NewState;
7259 };
7260
7261 // Read the first state.
7262 DiagState *FirstState;
7263 if (F.Kind == MK_ImplicitModule) {
7264 // Implicitly-built modules are reused with different diagnostic
7265 // settings. Use the initial diagnostic state from Diag to simulate this
7266 // compilation's diagnostic settings.
7267 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7268 DiagStates.push_back(Elt: FirstState);
7269
7270 // Skip the initial diagnostic state from the serialized module.
7271 assert(Record[1] == 0 &&
7272 "Invalid data, unexpected backref in initial state");
7273 Idx = 3 + Record[2] * 2;
7274 assert(Idx < Record.size() &&
7275 "Invalid data, not enough state change pairs in initial state");
7276 } else if (F.isModule()) {
7277 // For an explicit module, preserve the flags from the module build
7278 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7279 // -Wblah flags.
7280 unsigned Flags = Record[Idx++];
7281 DiagState Initial(*Diag.getDiagnosticIDs());
7282 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7283 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7284 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7285 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7286 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7287 Initial.ExtBehavior = (diag::Severity)Flags;
7288 FirstState = ReadDiagState(Initial, true);
7289
7290 assert(F.OriginalSourceFileID.isValid());
7291
7292 // Set up the root buffer of the module to start with the initial
7293 // diagnostic state of the module itself, to cover files that contain no
7294 // explicit transitions (for which we did not serialize anything).
7295 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7296 .StateTransitions.push_back(Elt: {FirstState, 0});
7297 } else {
7298 // For prefix ASTs, start with whatever the user configured on the
7299 // command line.
7300 Idx++; // Skip flags.
7301 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7302 }
7303
7304 // Read the state transitions.
7305 unsigned NumLocations = Record[Idx++];
7306 while (NumLocations--) {
7307 assert(Idx < Record.size() &&
7308 "Invalid data, missing pragma diagnostic states");
7309 FileID FID = ReadFileID(F, Record, Idx);
7310 assert(FID.isValid() && "invalid FileID for transition");
7311 unsigned Transitions = Record[Idx++];
7312
7313 // Note that we don't need to set up Parent/ParentOffset here, because
7314 // we won't be changing the diagnostic state within imported FileIDs
7315 // (other than perhaps appending to the main source file, which has no
7316 // parent).
7317 auto &F = Diag.DiagStatesByLoc.Files[FID];
7318 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7319 for (unsigned I = 0; I != Transitions; ++I) {
7320 unsigned Offset = Record[Idx++];
7321 auto *State = ReadDiagState(*FirstState, false);
7322 F.StateTransitions.push_back(Elt: {State, Offset});
7323 }
7324 }
7325
7326 // Read the final state.
7327 assert(Idx < Record.size() &&
7328 "Invalid data, missing final pragma diagnostic state");
7329 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7330 auto *CurState = ReadDiagState(*FirstState, false);
7331
7332 if (!F.isModule()) {
7333 Diag.DiagStatesByLoc.CurDiagState = CurState;
7334 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7335
7336 // Preserve the property that the imaginary root file describes the
7337 // current state.
7338 FileID NullFile;
7339 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7340 if (T.empty())
7341 T.push_back(Elt: {CurState, 0});
7342 else
7343 T[0].State = CurState;
7344 }
7345
7346 // Restore the push stack so that unmatched pushes from a preamble are
7347 // visible when the main file is parsed, allowing the corresponding
7348 // `#pragma diagnostic pop` to succeed.
7349 assert(Idx < Record.size() &&
7350 "Invalid data, missing diagnostic push stack");
7351 unsigned NumPushes = Record[Idx++];
7352 for (unsigned I = 0; I != NumPushes; ++I) {
7353 auto *State = ReadDiagState(*FirstState, false);
7354 if (!F.isModule())
7355 Diag.DiagStateOnPushStack.push_back(x: State);
7356 }
7357
7358 // Don't try to read these mappings again.
7359 Record.clear();
7360 }
7361}
7362
7363/// Get the correct cursor and offset for loading a type.
7364ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7365 auto [M, Index] = translateTypeIDToIndex(ID);
7366 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7367 M->DeclsBlockStartOffset);
7368}
7369
7370static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7371 switch (code) {
7372#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7373 case TYPE_##CODE_ID: return Type::CLASS_ID;
7374#include "clang/Serialization/TypeBitCodes.def"
7375 default:
7376 return std::nullopt;
7377 }
7378}
7379
7380/// Read and return the type with the given index..
7381///
7382/// The index is the type ID, shifted and minus the number of predefs. This
7383/// routine actually reads the record corresponding to the type at the given
7384/// location. It is a helper routine for GetType, which deals with reading type
7385/// IDs.
7386QualType ASTReader::readTypeRecord(TypeID ID) {
7387 assert(ContextObj && "reading type with no AST context");
7388 ASTContext &Context = *ContextObj;
7389 RecordLocation Loc = TypeCursorForIndex(ID);
7390 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7391
7392 // Keep track of where we are in the stream, then jump back there
7393 // after reading this type.
7394 SavedStreamPosition SavedPosition(DeclsCursor);
7395
7396 ReadingKindTracker ReadingKind(Read_Type, *this);
7397
7398 // Note that we are loading a type record.
7399 Deserializing AType(this);
7400
7401 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7402 Error(Err: std::move(Err));
7403 return QualType();
7404 }
7405 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7406 if (!RawCode) {
7407 Error(Err: RawCode.takeError());
7408 return QualType();
7409 }
7410
7411 ASTRecordReader Record(*this, *Loc.F);
7412 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7413 if (!Code) {
7414 Error(Err: Code.takeError());
7415 return QualType();
7416 }
7417 if (Code.get() == TYPE_EXT_QUAL) {
7418 QualType baseType = Record.readQualType();
7419 Qualifiers quals = Record.readQualifiers();
7420 return Context.getQualifiedType(T: baseType, Qs: quals);
7421 }
7422
7423 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7424 if (!maybeClass) {
7425 Error(Msg: "Unexpected code for type");
7426 return QualType();
7427 }
7428
7429 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7430 return TypeReader.read(kind: *maybeClass);
7431}
7432
7433namespace clang {
7434
7435class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7436 ASTRecordReader &Reader;
7437
7438 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7439 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7440
7441 TypeSourceInfo *GetTypeSourceInfo() {
7442 return Reader.readTypeSourceInfo();
7443 }
7444
7445 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7446 return Reader.readNestedNameSpecifierLoc();
7447 }
7448
7449 Attr *ReadAttr() {
7450 return Reader.readAttr();
7451 }
7452
7453public:
7454 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7455
7456 // We want compile-time assurance that we've enumerated all of
7457 // these, so unfortunately we have to declare them first, then
7458 // define them out-of-line.
7459#define ABSTRACT_TYPELOC(CLASS, PARENT)
7460#define TYPELOC(CLASS, PARENT) \
7461 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7462#include "clang/AST/TypeLocNodes.def"
7463
7464 void VisitFunctionTypeLoc(FunctionTypeLoc);
7465 void VisitArrayTypeLoc(ArrayTypeLoc);
7466 void VisitTagTypeLoc(TagTypeLoc TL);
7467};
7468
7469} // namespace clang
7470
7471void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7472 // nothing to do
7473}
7474
7475void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7476 TL.setBuiltinLoc(readSourceLocation());
7477 if (TL.needsExtraLocalData()) {
7478 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7479 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7480 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7481 TL.setModeAttr(Reader.readInt());
7482 }
7483}
7484
7485void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7486 TL.setNameLoc(readSourceLocation());
7487}
7488
7489void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7490 TL.setStarLoc(readSourceLocation());
7491}
7492
7493void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7494 // nothing to do
7495}
7496
7497void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7498 // nothing to do
7499}
7500
7501void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7502 // nothing to do
7503}
7504
7505void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7506 TL.setExpansionLoc(readSourceLocation());
7507}
7508
7509void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7510 TL.setCaretLoc(readSourceLocation());
7511}
7512
7513void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7514 TL.setAmpLoc(readSourceLocation());
7515}
7516
7517void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7518 TL.setAmpAmpLoc(readSourceLocation());
7519}
7520
7521void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7522 TL.setStarLoc(readSourceLocation());
7523 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7524}
7525
7526void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7527 TL.setLBracketLoc(readSourceLocation());
7528 TL.setRBracketLoc(readSourceLocation());
7529 if (Reader.readBool())
7530 TL.setSizeExpr(Reader.readExpr());
7531 else
7532 TL.setSizeExpr(nullptr);
7533}
7534
7535void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7536 VisitArrayTypeLoc(TL);
7537}
7538
7539void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7540 VisitArrayTypeLoc(TL);
7541}
7542
7543void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7544 VisitArrayTypeLoc(TL);
7545}
7546
7547void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7548 DependentSizedArrayTypeLoc TL) {
7549 VisitArrayTypeLoc(TL);
7550}
7551
7552void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7553 DependentAddressSpaceTypeLoc TL) {
7554
7555 TL.setAttrNameLoc(readSourceLocation());
7556 TL.setAttrOperandParensRange(readSourceRange());
7557 TL.setAttrExprOperand(Reader.readExpr());
7558}
7559
7560void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7561 DependentSizedExtVectorTypeLoc TL) {
7562 TL.setNameLoc(readSourceLocation());
7563}
7564
7565void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7566 TL.setNameLoc(readSourceLocation());
7567}
7568
7569void TypeLocReader::VisitDependentVectorTypeLoc(
7570 DependentVectorTypeLoc TL) {
7571 TL.setNameLoc(readSourceLocation());
7572}
7573
7574void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7575 TL.setNameLoc(readSourceLocation());
7576}
7577
7578void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7579 TL.setAttrNameLoc(readSourceLocation());
7580 TL.setAttrOperandParensRange(readSourceRange());
7581 TL.setAttrRowOperand(Reader.readExpr());
7582 TL.setAttrColumnOperand(Reader.readExpr());
7583}
7584
7585void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7586 DependentSizedMatrixTypeLoc TL) {
7587 TL.setAttrNameLoc(readSourceLocation());
7588 TL.setAttrOperandParensRange(readSourceRange());
7589 TL.setAttrRowOperand(Reader.readExpr());
7590 TL.setAttrColumnOperand(Reader.readExpr());
7591}
7592
7593void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7594 TL.setLocalRangeBegin(readSourceLocation());
7595 TL.setLParenLoc(readSourceLocation());
7596 TL.setRParenLoc(readSourceLocation());
7597 TL.setExceptionSpecRange(readSourceRange());
7598 TL.setLocalRangeEnd(readSourceLocation());
7599 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7600 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7601 }
7602}
7603
7604void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7605 VisitFunctionTypeLoc(TL);
7606}
7607
7608void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7609 VisitFunctionTypeLoc(TL);
7610}
7611
7612void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7613 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7614 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7615 SourceLocation NameLoc = readSourceLocation();
7616 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7617}
7618
7619void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7620 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7621 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7622 SourceLocation NameLoc = readSourceLocation();
7623 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7624}
7625
7626void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7627 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7628 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7629 SourceLocation NameLoc = readSourceLocation();
7630 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7631}
7632
7633void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7634 TL.setTypeofLoc(readSourceLocation());
7635 TL.setLParenLoc(readSourceLocation());
7636 TL.setRParenLoc(readSourceLocation());
7637}
7638
7639void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7640 TL.setTypeofLoc(readSourceLocation());
7641 TL.setLParenLoc(readSourceLocation());
7642 TL.setRParenLoc(readSourceLocation());
7643 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7644}
7645
7646void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7647 TL.setDecltypeLoc(readSourceLocation());
7648 TL.setRParenLoc(readSourceLocation());
7649}
7650
7651void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7652 TL.setEllipsisLoc(readSourceLocation());
7653}
7654
7655void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7656 TL.setKWLoc(readSourceLocation());
7657 TL.setLParenLoc(readSourceLocation());
7658 TL.setRParenLoc(readSourceLocation());
7659 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7660}
7661
7662ConceptReference *ASTRecordReader::readConceptReference() {
7663 auto NNS = readNestedNameSpecifierLoc();
7664 auto TemplateKWLoc = readSourceLocation();
7665 auto ConceptNameLoc = readDeclarationNameInfo();
7666 auto FoundDecl = readDeclAs<NamedDecl>();
7667 auto NamedConcept = readDeclAs<ConceptDecl>();
7668 auto *CR = ConceptReference::Create(
7669 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7670 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7671 return CR;
7672}
7673
7674void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7675 TL.setNameLoc(readSourceLocation());
7676 if (Reader.readBool())
7677 TL.setConceptReference(Reader.readConceptReference());
7678 if (Reader.readBool())
7679 TL.setRParenLoc(readSourceLocation());
7680}
7681
7682void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7683 DeducedTemplateSpecializationTypeLoc TL) {
7684 TL.setElaboratedKeywordLoc(readSourceLocation());
7685 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7686 TL.setTemplateNameLoc(readSourceLocation());
7687}
7688
7689void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7690 TL.setElaboratedKeywordLoc(readSourceLocation());
7691 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7692 TL.setNameLoc(readSourceLocation());
7693}
7694
7695void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7696 VisitTagTypeLoc(TL);
7697}
7698
7699void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7700 VisitTagTypeLoc(TL);
7701}
7702
7703void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7704
7705void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7706 TL.setAttr(ReadAttr());
7707}
7708
7709void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7710 // Nothing to do
7711}
7712
7713void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7714 // Nothing to do.
7715}
7716
7717void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7718 TL.setAttrLoc(readSourceLocation());
7719}
7720
7721void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7722 HLSLAttributedResourceTypeLoc TL) {
7723 // Nothing to do.
7724}
7725
7726void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7727 // Nothing to do.
7728}
7729
7730void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7731 TL.setNameLoc(readSourceLocation());
7732}
7733
7734void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7735 SubstTemplateTypeParmTypeLoc TL) {
7736 TL.setNameLoc(readSourceLocation());
7737}
7738
7739void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7740 SubstTemplateTypeParmPackTypeLoc TL) {
7741 TL.setNameLoc(readSourceLocation());
7742}
7743
7744void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7745 SubstBuiltinTemplatePackTypeLoc TL) {
7746 TL.setNameLoc(readSourceLocation());
7747}
7748
7749void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7750 TemplateSpecializationTypeLoc TL) {
7751 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7752 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7753 SourceLocation TemplateKeywordLoc = readSourceLocation();
7754 SourceLocation NameLoc = readSourceLocation();
7755 SourceLocation LAngleLoc = readSourceLocation();
7756 SourceLocation RAngleLoc = readSourceLocation();
7757 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7758 LAngleLoc, RAngleLoc);
7759 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7760 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7761 Args[I] = Reader.readTemplateArgumentLocInfo(
7762 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7763}
7764
7765void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7766 TL.setLParenLoc(readSourceLocation());
7767 TL.setRParenLoc(readSourceLocation());
7768}
7769
7770void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7771 TL.setElaboratedKeywordLoc(readSourceLocation());
7772 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7773 TL.setNameLoc(readSourceLocation());
7774}
7775
7776void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7777 TL.setEllipsisLoc(readSourceLocation());
7778}
7779
7780void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7781 TL.setNameLoc(readSourceLocation());
7782 TL.setNameEndLoc(readSourceLocation());
7783}
7784
7785void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7786 if (TL.getNumProtocols()) {
7787 TL.setProtocolLAngleLoc(readSourceLocation());
7788 TL.setProtocolRAngleLoc(readSourceLocation());
7789 }
7790 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7791 TL.setProtocolLoc(i, Loc: readSourceLocation());
7792}
7793
7794void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7795 TL.setHasBaseTypeAsWritten(Reader.readBool());
7796 TL.setTypeArgsLAngleLoc(readSourceLocation());
7797 TL.setTypeArgsRAngleLoc(readSourceLocation());
7798 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7799 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7800 TL.setProtocolLAngleLoc(readSourceLocation());
7801 TL.setProtocolRAngleLoc(readSourceLocation());
7802 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7803 TL.setProtocolLoc(i, Loc: readSourceLocation());
7804}
7805
7806void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7807 TL.setStarLoc(readSourceLocation());
7808}
7809
7810void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7811 TL.setKWLoc(readSourceLocation());
7812 TL.setLParenLoc(readSourceLocation());
7813 TL.setRParenLoc(readSourceLocation());
7814}
7815
7816void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7817 TL.setKWLoc(readSourceLocation());
7818}
7819
7820void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7821 TL.setNameLoc(readSourceLocation());
7822}
7823
7824void TypeLocReader::VisitDependentBitIntTypeLoc(
7825 clang::DependentBitIntTypeLoc TL) {
7826 TL.setNameLoc(readSourceLocation());
7827}
7828
7829void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7830 // Nothing to do.
7831}
7832
7833void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7834 TypeLocReader TLR(*this);
7835 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7836 TLR.Visit(TyLoc: TL);
7837}
7838
7839TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7840 QualType InfoTy = readType();
7841 if (InfoTy.isNull())
7842 return nullptr;
7843
7844 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7845 readTypeLoc(TL: TInfo->getTypeLoc());
7846 return TInfo;
7847}
7848
7849static unsigned getIndexForTypeID(serialization::TypeID ID) {
7850 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7851}
7852
7853static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7854 return ID >> 32;
7855}
7856
7857static bool isPredefinedType(serialization::TypeID ID) {
7858 // We don't need to erase the higher bits since if these bits are not 0,
7859 // it must be larger than NUM_PREDEF_TYPE_IDS.
7860 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7861}
7862
7863std::pair<ModuleFile *, unsigned>
7864ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7865 assert(!isPredefinedType(ID) &&
7866 "Predefined type shouldn't be in TypesLoaded");
7867 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7868 assert(ModuleFileIndex && "Untranslated Local Decl?");
7869
7870 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7871 assert(OwningModuleFile &&
7872 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7873
7874 return {OwningModuleFile,
7875 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7876}
7877
7878QualType ASTReader::GetType(TypeID ID) {
7879 assert(ContextObj && "reading type with no AST context");
7880 ASTContext &Context = *ContextObj;
7881
7882 unsigned FastQuals = ID & Qualifiers::FastMask;
7883
7884 if (isPredefinedType(ID)) {
7885 QualType T;
7886 unsigned Index = getIndexForTypeID(ID);
7887 switch ((PredefinedTypeIDs)Index) {
7888 case PREDEF_TYPE_LAST_ID:
7889 // We should never use this one.
7890 llvm_unreachable("Invalid predefined type");
7891 break;
7892 case PREDEF_TYPE_NULL_ID:
7893 return QualType();
7894 case PREDEF_TYPE_VOID_ID:
7895 T = Context.VoidTy;
7896 break;
7897 case PREDEF_TYPE_BOOL_ID:
7898 T = Context.BoolTy;
7899 break;
7900 case PREDEF_TYPE_CHAR_U_ID:
7901 case PREDEF_TYPE_CHAR_S_ID:
7902 // FIXME: Check that the signedness of CharTy is correct!
7903 T = Context.CharTy;
7904 break;
7905 case PREDEF_TYPE_UCHAR_ID:
7906 T = Context.UnsignedCharTy;
7907 break;
7908 case PREDEF_TYPE_USHORT_ID:
7909 T = Context.UnsignedShortTy;
7910 break;
7911 case PREDEF_TYPE_UINT_ID:
7912 T = Context.UnsignedIntTy;
7913 break;
7914 case PREDEF_TYPE_ULONG_ID:
7915 T = Context.UnsignedLongTy;
7916 break;
7917 case PREDEF_TYPE_ULONGLONG_ID:
7918 T = Context.UnsignedLongLongTy;
7919 break;
7920 case PREDEF_TYPE_UINT128_ID:
7921 T = Context.UnsignedInt128Ty;
7922 break;
7923 case PREDEF_TYPE_SCHAR_ID:
7924 T = Context.SignedCharTy;
7925 break;
7926 case PREDEF_TYPE_WCHAR_ID:
7927 T = Context.WCharTy;
7928 break;
7929 case PREDEF_TYPE_SHORT_ID:
7930 T = Context.ShortTy;
7931 break;
7932 case PREDEF_TYPE_INT_ID:
7933 T = Context.IntTy;
7934 break;
7935 case PREDEF_TYPE_LONG_ID:
7936 T = Context.LongTy;
7937 break;
7938 case PREDEF_TYPE_LONGLONG_ID:
7939 T = Context.LongLongTy;
7940 break;
7941 case PREDEF_TYPE_INT128_ID:
7942 T = Context.Int128Ty;
7943 break;
7944 case PREDEF_TYPE_BFLOAT16_ID:
7945 T = Context.BFloat16Ty;
7946 break;
7947 case PREDEF_TYPE_HALF_ID:
7948 T = Context.HalfTy;
7949 break;
7950 case PREDEF_TYPE_FLOAT_ID:
7951 T = Context.FloatTy;
7952 break;
7953 case PREDEF_TYPE_DOUBLE_ID:
7954 T = Context.DoubleTy;
7955 break;
7956 case PREDEF_TYPE_LONGDOUBLE_ID:
7957 T = Context.LongDoubleTy;
7958 break;
7959 case PREDEF_TYPE_SHORT_ACCUM_ID:
7960 T = Context.ShortAccumTy;
7961 break;
7962 case PREDEF_TYPE_ACCUM_ID:
7963 T = Context.AccumTy;
7964 break;
7965 case PREDEF_TYPE_LONG_ACCUM_ID:
7966 T = Context.LongAccumTy;
7967 break;
7968 case PREDEF_TYPE_USHORT_ACCUM_ID:
7969 T = Context.UnsignedShortAccumTy;
7970 break;
7971 case PREDEF_TYPE_UACCUM_ID:
7972 T = Context.UnsignedAccumTy;
7973 break;
7974 case PREDEF_TYPE_ULONG_ACCUM_ID:
7975 T = Context.UnsignedLongAccumTy;
7976 break;
7977 case PREDEF_TYPE_SHORT_FRACT_ID:
7978 T = Context.ShortFractTy;
7979 break;
7980 case PREDEF_TYPE_FRACT_ID:
7981 T = Context.FractTy;
7982 break;
7983 case PREDEF_TYPE_LONG_FRACT_ID:
7984 T = Context.LongFractTy;
7985 break;
7986 case PREDEF_TYPE_USHORT_FRACT_ID:
7987 T = Context.UnsignedShortFractTy;
7988 break;
7989 case PREDEF_TYPE_UFRACT_ID:
7990 T = Context.UnsignedFractTy;
7991 break;
7992 case PREDEF_TYPE_ULONG_FRACT_ID:
7993 T = Context.UnsignedLongFractTy;
7994 break;
7995 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7996 T = Context.SatShortAccumTy;
7997 break;
7998 case PREDEF_TYPE_SAT_ACCUM_ID:
7999 T = Context.SatAccumTy;
8000 break;
8001 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
8002 T = Context.SatLongAccumTy;
8003 break;
8004 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
8005 T = Context.SatUnsignedShortAccumTy;
8006 break;
8007 case PREDEF_TYPE_SAT_UACCUM_ID:
8008 T = Context.SatUnsignedAccumTy;
8009 break;
8010 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8011 T = Context.SatUnsignedLongAccumTy;
8012 break;
8013 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8014 T = Context.SatShortFractTy;
8015 break;
8016 case PREDEF_TYPE_SAT_FRACT_ID:
8017 T = Context.SatFractTy;
8018 break;
8019 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8020 T = Context.SatLongFractTy;
8021 break;
8022 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8023 T = Context.SatUnsignedShortFractTy;
8024 break;
8025 case PREDEF_TYPE_SAT_UFRACT_ID:
8026 T = Context.SatUnsignedFractTy;
8027 break;
8028 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8029 T = Context.SatUnsignedLongFractTy;
8030 break;
8031 case PREDEF_TYPE_FLOAT16_ID:
8032 T = Context.Float16Ty;
8033 break;
8034 case PREDEF_TYPE_FLOAT128_ID:
8035 T = Context.Float128Ty;
8036 break;
8037 case PREDEF_TYPE_IBM128_ID:
8038 T = Context.Ibm128Ty;
8039 break;
8040 case PREDEF_TYPE_OVERLOAD_ID:
8041 T = Context.OverloadTy;
8042 break;
8043 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8044 T = Context.UnresolvedTemplateTy;
8045 break;
8046 case PREDEF_TYPE_BOUND_MEMBER:
8047 T = Context.BoundMemberTy;
8048 break;
8049 case PREDEF_TYPE_PSEUDO_OBJECT:
8050 T = Context.PseudoObjectTy;
8051 break;
8052 case PREDEF_TYPE_DEPENDENT_ID:
8053 T = Context.DependentTy;
8054 break;
8055 case PREDEF_TYPE_UNKNOWN_ANY:
8056 T = Context.UnknownAnyTy;
8057 break;
8058 case PREDEF_TYPE_NULLPTR_ID:
8059 T = Context.NullPtrTy;
8060 break;
8061 case PREDEF_TYPE_CHAR8_ID:
8062 T = Context.Char8Ty;
8063 break;
8064 case PREDEF_TYPE_CHAR16_ID:
8065 T = Context.Char16Ty;
8066 break;
8067 case PREDEF_TYPE_CHAR32_ID:
8068 T = Context.Char32Ty;
8069 break;
8070 case PREDEF_TYPE_OBJC_ID:
8071 T = Context.ObjCBuiltinIdTy;
8072 break;
8073 case PREDEF_TYPE_OBJC_CLASS:
8074 T = Context.ObjCBuiltinClassTy;
8075 break;
8076 case PREDEF_TYPE_OBJC_SEL:
8077 T = Context.ObjCBuiltinSelTy;
8078 break;
8079#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8080 case PREDEF_TYPE_##Id##_ID: \
8081 T = Context.SingletonId; \
8082 break;
8083#include "clang/Basic/OpenCLImageTypes.def"
8084#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8085 case PREDEF_TYPE_##Id##_ID: \
8086 T = Context.Id##Ty; \
8087 break;
8088#include "clang/Basic/OpenCLExtensionTypes.def"
8089 case PREDEF_TYPE_SAMPLER_ID:
8090 T = Context.OCLSamplerTy;
8091 break;
8092 case PREDEF_TYPE_EVENT_ID:
8093 T = Context.OCLEventTy;
8094 break;
8095 case PREDEF_TYPE_CLK_EVENT_ID:
8096 T = Context.OCLClkEventTy;
8097 break;
8098 case PREDEF_TYPE_QUEUE_ID:
8099 T = Context.OCLQueueTy;
8100 break;
8101 case PREDEF_TYPE_RESERVE_ID_ID:
8102 T = Context.OCLReserveIDTy;
8103 break;
8104 case PREDEF_TYPE_AUTO_DEDUCT:
8105 T = Context.getAutoDeductType();
8106 break;
8107 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8108 T = Context.getAutoRRefDeductType();
8109 break;
8110 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8111 T = Context.ARCUnbridgedCastTy;
8112 break;
8113 case PREDEF_TYPE_BUILTIN_FN:
8114 T = Context.BuiltinFnTy;
8115 break;
8116 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8117 T = Context.IncompleteMatrixIdxTy;
8118 break;
8119 case PREDEF_TYPE_ARRAY_SECTION:
8120 T = Context.ArraySectionTy;
8121 break;
8122 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8123 T = Context.OMPArrayShapingTy;
8124 break;
8125 case PREDEF_TYPE_OMP_ITERATOR:
8126 T = Context.OMPIteratorTy;
8127 break;
8128#define SVE_TYPE(Name, Id, SingletonId) \
8129 case PREDEF_TYPE_##Id##_ID: \
8130 T = Context.SingletonId; \
8131 break;
8132#include "clang/Basic/AArch64ACLETypes.def"
8133#define PPC_VECTOR_TYPE(Name, Id, Size) \
8134 case PREDEF_TYPE_##Id##_ID: \
8135 T = Context.Id##Ty; \
8136 break;
8137#include "clang/Basic/PPCTypes.def"
8138#define RVV_TYPE(Name, Id, SingletonId) \
8139 case PREDEF_TYPE_##Id##_ID: \
8140 T = Context.SingletonId; \
8141 break;
8142#include "clang/Basic/RISCVVTypes.def"
8143#define WASM_TYPE(Name, Id, SingletonId) \
8144 case PREDEF_TYPE_##Id##_ID: \
8145 T = Context.SingletonId; \
8146 break;
8147#include "clang/Basic/WebAssemblyReferenceTypes.def"
8148#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8149 case PREDEF_TYPE_##Id##_ID: \
8150 T = Context.SingletonId; \
8151 break;
8152#include "clang/Basic/AMDGPUTypes.def"
8153#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8154 case PREDEF_TYPE_##Id##_ID: \
8155 T = Context.SingletonId; \
8156 break;
8157#include "clang/Basic/HLSLIntangibleTypes.def"
8158 }
8159
8160 assert(!T.isNull() && "Unknown predefined type");
8161 return T.withFastQualifiers(TQs: FastQuals);
8162 }
8163
8164 unsigned Index = translateTypeIDToIndex(ID).second;
8165
8166 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8167 if (TypesLoaded[Index].isNull()) {
8168 TypesLoaded[Index] = readTypeRecord(ID);
8169 if (TypesLoaded[Index].isNull())
8170 return QualType();
8171
8172 TypesLoaded[Index]->setFromAST();
8173 if (DeserializationListener)
8174 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8175 T: TypesLoaded[Index]);
8176 }
8177
8178 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8179}
8180
8181QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8182 return GetType(ID: getGlobalTypeID(F, LocalID));
8183}
8184
8185serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8186 LocalTypeID LocalID) const {
8187 if (isPredefinedType(ID: LocalID))
8188 return LocalID;
8189
8190 if (!F.ModuleOffsetMap.empty())
8191 ReadModuleOffsetMap(F);
8192
8193 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8194 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8195
8196 if (ModuleFileIndex == 0)
8197 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8198
8199 ModuleFile &MF =
8200 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8201 ModuleFileIndex = MF.Index + 1;
8202 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8203}
8204
8205TemplateArgumentLocInfo
8206ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8207 switch (Kind) {
8208 case TemplateArgument::Expression:
8209 return readExpr();
8210 case TemplateArgument::Type:
8211 return readTypeSourceInfo();
8212 case TemplateArgument::Template:
8213 case TemplateArgument::TemplateExpansion: {
8214 SourceLocation TemplateKWLoc = readSourceLocation();
8215 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8216 SourceLocation TemplateNameLoc = readSourceLocation();
8217 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8218 ? readSourceLocation()
8219 : SourceLocation();
8220 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8221 TemplateNameLoc, EllipsisLoc);
8222 }
8223 case TemplateArgument::Null:
8224 case TemplateArgument::Integral:
8225 case TemplateArgument::Declaration:
8226 case TemplateArgument::NullPtr:
8227 case TemplateArgument::StructuralValue:
8228 case TemplateArgument::Pack:
8229 // FIXME: Is this right?
8230 return TemplateArgumentLocInfo();
8231 }
8232 llvm_unreachable("unexpected template argument loc");
8233}
8234
8235TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8236 TemplateArgument Arg = readTemplateArgument();
8237
8238 if (Arg.getKind() == TemplateArgument::Expression) {
8239 if (readBool()) // bool InfoHasSameExpr.
8240 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8241 }
8242 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8243}
8244
8245void ASTRecordReader::readTemplateArgumentListInfo(
8246 TemplateArgumentListInfo &Result) {
8247 Result.setLAngleLoc(readSourceLocation());
8248 Result.setRAngleLoc(readSourceLocation());
8249 unsigned NumArgsAsWritten = readInt();
8250 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8251 Result.addArgument(Loc: readTemplateArgumentLoc());
8252}
8253
8254const ASTTemplateArgumentListInfo *
8255ASTRecordReader::readASTTemplateArgumentListInfo() {
8256 TemplateArgumentListInfo Result;
8257 readTemplateArgumentListInfo(Result);
8258 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8259}
8260
8261Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8262
8263void ASTReader::CompleteRedeclChain(const Decl *D) {
8264 if (NumCurrentElementsDeserializing) {
8265 // We arrange to not care about the complete redeclaration chain while we're
8266 // deserializing. Just remember that the AST has marked this one as complete
8267 // but that it's not actually complete yet, so we know we still need to
8268 // complete it later.
8269 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8270 return;
8271 }
8272
8273 if (!D->getDeclContext()) {
8274 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8275 return;
8276 }
8277
8278 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8279
8280 // If this is a named declaration, complete it by looking it up
8281 // within its context.
8282 //
8283 // FIXME: Merging a function definition should merge
8284 // all mergeable entities within it.
8285 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8286 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8287 if (!getContext().getLangOpts().CPlusPlus &&
8288 isa<TranslationUnitDecl>(Val: DC)) {
8289 // Outside of C++, we don't have a lookup table for the TU, so update
8290 // the identifier instead. (For C++ modules, we don't store decls
8291 // in the serialized identifier table, so we do the lookup in the TU.)
8292 auto *II = Name.getAsIdentifierInfo();
8293 assert(II && "non-identifier name in C?");
8294 if (II->isOutOfDate())
8295 updateOutOfDateIdentifier(II: *II);
8296 } else
8297 DC->lookup(Name);
8298 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8299 // Find all declarations of this kind from the relevant context.
8300 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8301 auto *DC = cast<DeclContext>(Val: DCDecl);
8302 SmallVector<Decl*, 8> Decls;
8303 FindExternalLexicalDecls(
8304 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8305 }
8306 }
8307 }
8308
8309 RedeclarableTemplateDecl *Template = nullptr;
8310 ArrayRef<TemplateArgument> Args;
8311 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8312 Template = CTSD->getSpecializedTemplate();
8313 Args = CTSD->getTemplateArgs().asArray();
8314 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8315 Template = VTSD->getSpecializedTemplate();
8316 Args = VTSD->getTemplateArgs().asArray();
8317 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8318 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8319 Template = Tmplt;
8320 Args = FD->getTemplateSpecializationArgs()->asArray();
8321 }
8322 }
8323
8324 if (Template)
8325 Template->loadLazySpecializationsImpl(Args);
8326}
8327
8328CXXCtorInitializer **
8329ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8330 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8331 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8332 SavedStreamPosition SavedPosition(Cursor);
8333 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8334 Error(Err: std::move(Err));
8335 return nullptr;
8336 }
8337 ReadingKindTracker ReadingKind(Read_Decl, *this);
8338 Deserializing D(this);
8339
8340 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8341 if (!MaybeCode) {
8342 Error(Err: MaybeCode.takeError());
8343 return nullptr;
8344 }
8345 unsigned Code = MaybeCode.get();
8346
8347 ASTRecordReader Record(*this, *Loc.F);
8348 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8349 if (!MaybeRecCode) {
8350 Error(Err: MaybeRecCode.takeError());
8351 return nullptr;
8352 }
8353 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8354 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8355 return nullptr;
8356 }
8357
8358 return Record.readCXXCtorInitializers();
8359}
8360
8361CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8362 assert(ContextObj && "reading base specifiers with no AST context");
8363 ASTContext &Context = *ContextObj;
8364
8365 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8366 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8367 SavedStreamPosition SavedPosition(Cursor);
8368 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8369 Error(Err: std::move(Err));
8370 return nullptr;
8371 }
8372 ReadingKindTracker ReadingKind(Read_Decl, *this);
8373 Deserializing D(this);
8374
8375 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8376 if (!MaybeCode) {
8377 Error(Err: MaybeCode.takeError());
8378 return nullptr;
8379 }
8380 unsigned Code = MaybeCode.get();
8381
8382 ASTRecordReader Record(*this, *Loc.F);
8383 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8384 if (!MaybeRecCode) {
8385 Error(Err: MaybeCode.takeError());
8386 return nullptr;
8387 }
8388 unsigned RecCode = MaybeRecCode.get();
8389
8390 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8391 Error(Msg: "malformed AST file: missing C++ base specifiers");
8392 return nullptr;
8393 }
8394
8395 unsigned NumBases = Record.readInt();
8396 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8397 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8398 for (unsigned I = 0; I != NumBases; ++I)
8399 Bases[I] = Record.readCXXBaseSpecifier();
8400 return Bases;
8401}
8402
8403GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8404 LocalDeclID LocalID) const {
8405 if (LocalID < NUM_PREDEF_DECL_IDS)
8406 return GlobalDeclID(LocalID.getRawValue());
8407
8408 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8409 DeclID ID = LocalID.getLocalDeclIndex();
8410
8411 if (!F.ModuleOffsetMap.empty())
8412 ReadModuleOffsetMap(F);
8413
8414 ModuleFile *OwningModuleFile =
8415 OwningModuleFileIndex == 0
8416 ? &F
8417 : F.TransitiveImports[OwningModuleFileIndex - 1];
8418
8419 if (OwningModuleFileIndex == 0)
8420 ID -= NUM_PREDEF_DECL_IDS;
8421
8422 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8423 return GlobalDeclID(NewModuleFileIndex, ID);
8424}
8425
8426bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8427 // Predefined decls aren't from any module.
8428 if (ID < NUM_PREDEF_DECL_IDS)
8429 return false;
8430
8431 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8432 return M.Index == ModuleFileIndex - 1;
8433}
8434
8435ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8436 // Predefined decls aren't from any module.
8437 if (ID < NUM_PREDEF_DECL_IDS)
8438 return nullptr;
8439
8440 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8441 assert(ModuleFileIndex && "Untranslated Local Decl?");
8442
8443 return &getModuleManager()[ModuleFileIndex - 1];
8444}
8445
8446ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8447 if (!D->isFromASTFile())
8448 return nullptr;
8449
8450 return getOwningModuleFile(ID: D->getGlobalID());
8451}
8452
8453SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8454 if (ID < NUM_PREDEF_DECL_IDS)
8455 return SourceLocation();
8456
8457 if (Decl *D = GetExistingDecl(ID))
8458 return D->getLocation();
8459
8460 SourceLocation Loc;
8461 DeclCursorForID(ID, Location&: Loc);
8462 return Loc;
8463}
8464
8465Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8466 assert(ContextObj && "reading predefined decl without AST context");
8467 ASTContext &Context = *ContextObj;
8468 Decl *NewLoaded = nullptr;
8469 switch (ID) {
8470 case PREDEF_DECL_NULL_ID:
8471 return nullptr;
8472
8473 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8474 return Context.getTranslationUnitDecl();
8475
8476 case PREDEF_DECL_OBJC_ID_ID:
8477 if (Context.ObjCIdDecl)
8478 return Context.ObjCIdDecl;
8479 NewLoaded = Context.getObjCIdDecl();
8480 break;
8481
8482 case PREDEF_DECL_OBJC_SEL_ID:
8483 if (Context.ObjCSelDecl)
8484 return Context.ObjCSelDecl;
8485 NewLoaded = Context.getObjCSelDecl();
8486 break;
8487
8488 case PREDEF_DECL_OBJC_CLASS_ID:
8489 if (Context.ObjCClassDecl)
8490 return Context.ObjCClassDecl;
8491 NewLoaded = Context.getObjCClassDecl();
8492 break;
8493
8494 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8495 if (Context.ObjCProtocolClassDecl)
8496 return Context.ObjCProtocolClassDecl;
8497 NewLoaded = Context.getObjCProtocolDecl();
8498 break;
8499
8500 case PREDEF_DECL_INT_128_ID:
8501 if (Context.Int128Decl)
8502 return Context.Int128Decl;
8503 NewLoaded = Context.getInt128Decl();
8504 break;
8505
8506 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8507 if (Context.UInt128Decl)
8508 return Context.UInt128Decl;
8509 NewLoaded = Context.getUInt128Decl();
8510 break;
8511
8512 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8513 if (Context.ObjCInstanceTypeDecl)
8514 return Context.ObjCInstanceTypeDecl;
8515 NewLoaded = Context.getObjCInstanceTypeDecl();
8516 break;
8517
8518 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8519 if (Context.BuiltinVaListDecl)
8520 return Context.BuiltinVaListDecl;
8521 NewLoaded = Context.getBuiltinVaListDecl();
8522 break;
8523
8524 case PREDEF_DECL_VA_LIST_TAG:
8525 if (Context.VaListTagDecl)
8526 return Context.VaListTagDecl;
8527 NewLoaded = Context.getVaListTagDecl();
8528 break;
8529
8530 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8531 if (Context.BuiltinMSVaListDecl)
8532 return Context.BuiltinMSVaListDecl;
8533 NewLoaded = Context.getBuiltinMSVaListDecl();
8534 break;
8535
8536 case PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID:
8537 if (Context.BuiltinZOSVaListDecl)
8538 return Context.BuiltinZOSVaListDecl;
8539 NewLoaded = Context.getBuiltinZOSVaListDecl();
8540 break;
8541
8542 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8543 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8544 return Context.getMSGuidTagDecl();
8545
8546 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8547 if (Context.ExternCContext)
8548 return Context.ExternCContext;
8549 NewLoaded = Context.getExternCContextDecl();
8550 break;
8551
8552 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8553 if (Context.CFConstantStringTypeDecl)
8554 return Context.CFConstantStringTypeDecl;
8555 NewLoaded = Context.getCFConstantStringDecl();
8556 break;
8557
8558 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8559 if (Context.CFConstantStringTagDecl)
8560 return Context.CFConstantStringTagDecl;
8561 NewLoaded = Context.getCFConstantStringTagDecl();
8562 break;
8563
8564 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8565 return Context.getMSTypeInfoTagDecl();
8566
8567#define BuiltinTemplate(BTName) \
8568 case PREDEF_DECL##BTName##_ID: \
8569 if (Context.Decl##BTName) \
8570 return Context.Decl##BTName; \
8571 NewLoaded = Context.get##BTName##Decl(); \
8572 break;
8573#include "clang/Basic/BuiltinTemplates.inc"
8574
8575 case NUM_PREDEF_DECL_IDS:
8576 llvm_unreachable("Invalid decl ID");
8577 break;
8578 }
8579
8580 assert(NewLoaded && "Failed to load predefined decl?");
8581
8582 if (DeserializationListener)
8583 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8584
8585 return NewLoaded;
8586}
8587
8588unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8589 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8590 if (!OwningModuleFile) {
8591 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8592 return GlobalID.getRawValue();
8593 }
8594
8595 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8596}
8597
8598Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8599 assert(ContextObj && "reading decl with no AST context");
8600
8601 if (ID < NUM_PREDEF_DECL_IDS) {
8602 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8603 if (D) {
8604 // Track that we have merged the declaration with ID \p ID into the
8605 // pre-existing predefined declaration \p D.
8606 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8607 if (Merged.empty())
8608 Merged.push_back(Elt: ID);
8609 }
8610 return D;
8611 }
8612
8613 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8614
8615 if (Index >= DeclsLoaded.size()) {
8616 assert(0 && "declaration ID out-of-range for AST file");
8617 Error(Msg: "declaration ID out-of-range for AST file");
8618 return nullptr;
8619 }
8620
8621 return DeclsLoaded[Index];
8622}
8623
8624Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8625 if (ID < NUM_PREDEF_DECL_IDS)
8626 return GetExistingDecl(ID);
8627
8628 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8629
8630 if (Index >= DeclsLoaded.size()) {
8631 assert(0 && "declaration ID out-of-range for AST file");
8632 Error(Msg: "declaration ID out-of-range for AST file");
8633 return nullptr;
8634 }
8635
8636 if (!DeclsLoaded[Index]) {
8637 ReadDeclRecord(ID);
8638 if (DeserializationListener)
8639 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8640 }
8641
8642 return DeclsLoaded[Index];
8643}
8644
8645LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8646 GlobalDeclID GlobalID) {
8647 if (GlobalID < NUM_PREDEF_DECL_IDS)
8648 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8649
8650 if (!M.ModuleOffsetMap.empty())
8651 ReadModuleOffsetMap(F&: M);
8652
8653 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8654 DeclID ID = GlobalID.getLocalDeclIndex();
8655
8656 if (Owner == &M) {
8657 ID += NUM_PREDEF_DECL_IDS;
8658 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8659 }
8660
8661 uint64_t OrignalModuleFileIndex = 0;
8662 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8663 if (M.TransitiveImports[I] == Owner) {
8664 OrignalModuleFileIndex = I + 1;
8665 break;
8666 }
8667
8668 if (!OrignalModuleFileIndex)
8669 return LocalDeclID();
8670
8671 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8672}
8673
8674GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8675 unsigned &Idx) {
8676 if (Idx >= Record.size()) {
8677 Error(Msg: "Corrupted AST file");
8678 return GlobalDeclID(0);
8679 }
8680
8681 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8682}
8683
8684/// Resolve the offset of a statement into a statement.
8685///
8686/// This operation will read a new statement from the external
8687/// source each time it is called, and is meant to be used via a
8688/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8689Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8690 // Switch case IDs are per Decl.
8691 ClearSwitchCaseIDs();
8692
8693 // Offset here is a global offset across the entire chain.
8694 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8695 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8696 Error(Err: std::move(Err));
8697 return nullptr;
8698 }
8699 assert(NumCurrentElementsDeserializing == 0 &&
8700 "should not be called while already deserializing");
8701 Deserializing D(this);
8702 return ReadStmtFromStream(F&: *Loc.F);
8703}
8704
8705bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8706 const Decl *D) {
8707 assert(D);
8708
8709 auto It = SpecLookups.find(Val: D);
8710 if (It == SpecLookups.end())
8711 return false;
8712
8713 // Get Decl may violate the iterator from SpecializationsLookups so we store
8714 // the DeclIDs in ahead.
8715 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8716 It->second.Table.findAll();
8717
8718 // Since we've loaded all the specializations, we can erase it from
8719 // the lookup table.
8720 SpecLookups.erase(I: It);
8721
8722 bool NewSpecsFound = false;
8723 Deserializing LookupResults(this);
8724 for (auto &Info : Infos) {
8725 if (GetExistingDecl(ID: Info))
8726 continue;
8727 NewSpecsFound = true;
8728 GetDecl(ID: Info);
8729 }
8730
8731 return NewSpecsFound;
8732}
8733
8734bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8735 assert(D);
8736
8737 CompleteRedeclChain(D);
8738 bool NewSpecsFound =
8739 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8740 if (OnlyPartial)
8741 return NewSpecsFound;
8742
8743 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8744 return NewSpecsFound;
8745}
8746
8747bool ASTReader::LoadExternalSpecializationsImpl(
8748 SpecLookupTableTy &SpecLookups, const Decl *D,
8749 ArrayRef<TemplateArgument> TemplateArgs) {
8750 assert(D);
8751
8752 auto It = SpecLookups.find(Val: D);
8753 if (It == SpecLookups.end())
8754 return false;
8755
8756 Deserializing LookupResults(this);
8757 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8758
8759 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8760 It->second.Table.find(EKey: HashValue);
8761
8762 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8763 std::string Name;
8764 llvm::raw_string_ostream OS(Name);
8765 auto *ND = cast<NamedDecl>(Val: D);
8766 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8767 /*Qualified=*/true);
8768 return Name;
8769 });
8770
8771 bool NewSpecsFound = false;
8772 for (auto &Info : Infos) {
8773 if (GetExistingDecl(ID: Info))
8774 continue;
8775 NewSpecsFound = true;
8776 GetDecl(ID: Info);
8777 }
8778
8779 return NewSpecsFound;
8780}
8781
8782bool ASTReader::LoadExternalSpecializations(
8783 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8784 assert(D);
8785
8786 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8787 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8788 NewDeclsFound |=
8789 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8790
8791 return NewDeclsFound;
8792}
8793
8794void ASTReader::FindExternalLexicalDecls(
8795 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8796 SmallVectorImpl<Decl *> &Decls) {
8797 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8798
8799 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8800 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8801 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8802 auto K = (Decl::Kind)+LexicalDecls[I];
8803 if (!IsKindWeWant(K))
8804 continue;
8805
8806 auto ID = (DeclID) + LexicalDecls[I + 1];
8807
8808 // Don't add predefined declarations to the lexical context more
8809 // than once.
8810 if (ID < NUM_PREDEF_DECL_IDS) {
8811 if (PredefsVisited[ID])
8812 continue;
8813
8814 PredefsVisited[ID] = true;
8815 }
8816
8817 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8818 assert(D->getKind() == K && "wrong kind for lexical decl");
8819 if (!DC->isDeclInLexicalTraversal(D))
8820 Decls.push_back(Elt: D);
8821 }
8822 }
8823 };
8824
8825 if (isa<TranslationUnitDecl>(Val: DC)) {
8826 for (const auto &Lexical : TULexicalDecls)
8827 Visit(Lexical.first, Lexical.second);
8828 } else {
8829 auto I = LexicalDecls.find(Val: DC);
8830 if (I != LexicalDecls.end())
8831 Visit(I->second.first, I->second.second);
8832 }
8833
8834 ++NumLexicalDeclContextsRead;
8835}
8836
8837namespace {
8838
8839class UnalignedDeclIDComp {
8840 ASTReader &Reader;
8841 ModuleFile &Mod;
8842
8843public:
8844 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8845 : Reader(Reader), Mod(M) {}
8846
8847 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8848 SourceLocation LHS = getLocation(ID: L);
8849 SourceLocation RHS = getLocation(ID: R);
8850 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8851 }
8852
8853 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8854 SourceLocation RHS = getLocation(ID: R);
8855 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8856 }
8857
8858 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8859 SourceLocation LHS = getLocation(ID: L);
8860 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8861 }
8862
8863 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8864 return Reader.getSourceManager().getFileLoc(
8865 Loc: Reader.getSourceLocationForDeclID(
8866 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8867 }
8868};
8869
8870} // namespace
8871
8872void ASTReader::FindFileRegionDecls(FileID File,
8873 unsigned Offset, unsigned Length,
8874 SmallVectorImpl<Decl *> &Decls) {
8875 SourceManager &SM = getSourceManager();
8876
8877 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8878 if (I == FileDeclIDs.end())
8879 return;
8880
8881 FileDeclsInfo &DInfo = I->second;
8882 if (DInfo.Decls.empty())
8883 return;
8884
8885 SourceLocation
8886 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8887 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8888
8889 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8890 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8891 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8892 if (BeginIt != DInfo.Decls.begin())
8893 --BeginIt;
8894
8895 // If we are pointing at a top-level decl inside an objc container, we need
8896 // to backtrack until we find it otherwise we will fail to report that the
8897 // region overlaps with an objc container.
8898 while (BeginIt != DInfo.Decls.begin() &&
8899 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8900 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8901 ->isTopLevelDeclInObjCContainer())
8902 --BeginIt;
8903
8904 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8905 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8906 if (EndIt != DInfo.Decls.end())
8907 ++EndIt;
8908
8909 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8910 ++DIt)
8911 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8912 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8913}
8914
8915bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8916 DeclarationName Name,
8917 const DeclContext *OriginalDC) {
8918 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8919 "DeclContext has no visible decls in storage");
8920 if (!Name)
8921 return false;
8922
8923 // Load the list of declarations.
8924 DeclsSet DS;
8925
8926 auto Find = [&, this](auto &&Table, auto &&Key) {
8927 for (GlobalDeclID ID : Table.find(Key)) {
8928 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8929 if (ND->getDeclName() != Name)
8930 continue;
8931 // Special case for namespaces: There can be a lot of redeclarations of
8932 // some namespaces, and we import a "key declaration" per imported module.
8933 // Since all declarations of a namespace are essentially interchangeable,
8934 // we can optimize namespace look-up by only storing the key declaration
8935 // of the current TU, rather than storing N key declarations where N is
8936 // the # of imported modules that declare that namespace.
8937 // TODO: Try to generalize this optimization to other redeclarable decls.
8938 if (isa<NamespaceDecl>(Val: ND))
8939 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8940 DS.insert(ND);
8941 }
8942 };
8943
8944 Deserializing LookupResults(this);
8945
8946 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8947 // available.
8948 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8949 ++NumVisibleDeclContextsRead;
8950 Find(It->second.Table, Name);
8951 }
8952
8953 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8954 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8955 ++NumModuleLocalVisibleDeclContexts;
8956 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8957 }
8958 };
8959 if (auto *NamedModule =
8960 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8961 : nullptr)
8962 FindModuleLocalLookup(NamedModule);
8963 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8964 // We're going to find a decl but the decl context of the lookup is
8965 // unspecified. In this case, the OriginalDC may be the decl context in other
8966 // module.
8967 if (ContextObj && ContextObj->getCurrentNamedModule())
8968 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8969
8970 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8971 ++NumTULocalVisibleDeclContexts;
8972 Find(It->second.Table, Name);
8973 }
8974
8975 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8976 return !DS.empty();
8977}
8978
8979void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8980 if (!DC->hasExternalVisibleStorage())
8981 return;
8982
8983 DeclsMap Decls;
8984
8985 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8986 auto It = LookupTables.find(DC);
8987 if (It == LookupTables.end())
8988 return;
8989
8990 NumRead++;
8991
8992 for (GlobalDeclID ID : It->second.Table.findAll()) {
8993 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8994 // Special case for namespaces: There can be a lot of redeclarations of
8995 // some namespaces, and we import a "key declaration" per imported module.
8996 // Since all declarations of a namespace are essentially interchangeable,
8997 // we can optimize namespace look-up by only storing the key declaration
8998 // of the current TU, rather than storing N key declarations where N is
8999 // the # of imported modules that declare that namespace.
9000 // TODO: Try to generalize this optimization to other redeclarable decls.
9001 if (isa<NamespaceDecl>(Val: ND))
9002 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9003 Decls[ND->getDeclName()].insert(ND);
9004 }
9005
9006 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
9007 };
9008
9009 findAll(Lookups, NumVisibleDeclContextsRead);
9010 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
9011 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
9012
9013 for (auto &[Name, DS] : Decls)
9014 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9015
9016 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9017}
9018
9019const serialization::reader::DeclContextLookupTable *
9020ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9021 auto I = Lookups.find(Val: Primary);
9022 return I == Lookups.end() ? nullptr : &I->second;
9023}
9024
9025const serialization::reader::ModuleLocalLookupTable *
9026ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9027 auto I = ModuleLocalLookups.find(Val: Primary);
9028 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9029}
9030
9031const serialization::reader::DeclContextLookupTable *
9032ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9033 auto I = TULocalLookups.find(Val: Primary);
9034 return I == TULocalLookups.end() ? nullptr : &I->second;
9035}
9036
9037serialization::reader::LazySpecializationInfoLookupTable *
9038ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9039 assert(D->isCanonicalDecl());
9040 auto &LookupTable =
9041 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9042 auto I = LookupTable.find(Val: D);
9043 return I == LookupTable.end() ? nullptr : &I->second;
9044}
9045
9046bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9047 assert(D->isCanonicalDecl());
9048 return PartialSpecializationsLookups.contains(Val: D) ||
9049 SpecializationsLookups.contains(Val: D);
9050}
9051
9052/// Under non-PCH compilation the consumer receives the objc methods
9053/// before receiving the implementation, and codegen depends on this.
9054/// We simulate this by deserializing and passing to consumer the methods of the
9055/// implementation before passing the deserialized implementation decl.
9056static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9057 ASTConsumer *Consumer) {
9058 assert(ImplD && Consumer);
9059
9060 for (auto *I : ImplD->methods())
9061 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9062
9063 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9064}
9065
9066void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9067 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9068 PassObjCImplDeclToConsumer(ImplD, Consumer);
9069 else
9070 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9071}
9072
9073void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9074 Consumer->HandleVTable(RD);
9075}
9076
9077void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9078 this->Consumer = Consumer;
9079
9080 if (Consumer)
9081 PassInterestingDeclsToConsumer();
9082
9083 if (DeserializationListener)
9084 DeserializationListener->ReaderInitialized(Reader: this);
9085}
9086
9087void ASTReader::PrintStats() {
9088 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9089
9090 unsigned NumTypesLoaded =
9091 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9092 unsigned NumDeclsLoaded =
9093 DeclsLoaded.size() -
9094 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9095 unsigned NumIdentifiersLoaded =
9096 IdentifiersLoaded.size() -
9097 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9098 unsigned NumMacrosLoaded =
9099 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9100 unsigned NumSelectorsLoaded =
9101 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9102
9103 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9104 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9105 NumSLocEntriesRead, TotalNumSLocEntries,
9106 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9107 if (!TypesLoaded.empty())
9108 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9109 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9110 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9111 if (!DeclsLoaded.empty())
9112 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9113 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9114 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9115 if (!IdentifiersLoaded.empty())
9116 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9117 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9118 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9119 if (!MacrosLoaded.empty())
9120 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9121 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9122 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9123 if (!SelectorsLoaded.empty())
9124 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9125 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9126 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9127 if (TotalNumStatements)
9128 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9129 NumStatementsRead, TotalNumStatements,
9130 ((float)NumStatementsRead/TotalNumStatements * 100));
9131 if (TotalNumMacros)
9132 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9133 NumMacrosRead, TotalNumMacros,
9134 ((float)NumMacrosRead/TotalNumMacros * 100));
9135 if (TotalLexicalDeclContexts)
9136 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9137 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9138 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9139 * 100));
9140 if (TotalVisibleDeclContexts)
9141 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9142 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9143 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9144 * 100));
9145 if (TotalModuleLocalVisibleDeclContexts)
9146 std::fprintf(
9147 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9148 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9149 ((float)NumModuleLocalVisibleDeclContexts /
9150 TotalModuleLocalVisibleDeclContexts * 100));
9151 if (TotalTULocalVisibleDeclContexts)
9152 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9153 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9154 ((float)NumTULocalVisibleDeclContexts /
9155 TotalTULocalVisibleDeclContexts * 100));
9156 if (TotalNumMethodPoolEntries)
9157 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9158 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9159 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9160 * 100));
9161 if (NumMethodPoolLookups)
9162 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9163 NumMethodPoolHits, NumMethodPoolLookups,
9164 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9165 if (NumMethodPoolTableLookups)
9166 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9167 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9168 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9169 * 100.0));
9170 if (NumIdentifierLookupHits)
9171 std::fprintf(stderr,
9172 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9173 NumIdentifierLookupHits, NumIdentifierLookups,
9174 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9175
9176 if (GlobalIndex) {
9177 std::fprintf(stderr, format: "\n");
9178 GlobalIndex->printStats();
9179 }
9180
9181 std::fprintf(stderr, format: "\n");
9182 dump();
9183 std::fprintf(stderr, format: "\n");
9184}
9185
9186template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9187LLVM_DUMP_METHOD static void
9188dumpModuleIDMap(StringRef Name,
9189 const ContinuousRangeMap<Key, ModuleFile *,
9190 InitialCapacity> &Map) {
9191 if (Map.begin() == Map.end())
9192 return;
9193
9194 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9195
9196 llvm::errs() << Name << ":\n";
9197 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9198 I != IEnd; ++I)
9199 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9200 << "\n";
9201}
9202
9203LLVM_DUMP_METHOD void ASTReader::dump() {
9204 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9205 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9206 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9207 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9208 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9209 dumpModuleIDMap(Name: "Global preprocessed entity map",
9210 Map: GlobalPreprocessedEntityMap);
9211
9212 llvm::errs() << "\n*** PCH/Modules Loaded:";
9213 for (ModuleFile &M : ModuleMgr)
9214 M.dump();
9215}
9216
9217/// Return the amount of memory used by memory buffers, breaking down
9218/// by heap-backed versus mmap'ed memory.
9219void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9220 for (ModuleFile &I : ModuleMgr) {
9221 if (llvm::MemoryBuffer *buf = I.Buffer) {
9222 size_t bytes = buf->getBufferSize();
9223 switch (buf->getBufferKind()) {
9224 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9225 sizes.malloc_bytes += bytes;
9226 break;
9227 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9228 sizes.mmap_bytes += bytes;
9229 break;
9230 }
9231 }
9232 }
9233}
9234
9235void ASTReader::InitializeSema(Sema &S) {
9236 SemaObj = &S;
9237 S.addExternalSource(E: this);
9238
9239 // Makes sure any declarations that were deserialized "too early"
9240 // still get added to the identifier's declaration chains.
9241 for (GlobalDeclID ID : PreloadedDeclIDs) {
9242 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9243 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9244 }
9245 PreloadedDeclIDs.clear();
9246
9247 // FIXME: What happens if these are changed by a module import?
9248 if (!FPPragmaOptions.empty()) {
9249 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9250 FPOptionsOverride NewOverrides =
9251 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9252 SemaObj->CurFPFeatures =
9253 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9254 }
9255
9256 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9257 Decl *D = GetDecl(ID);
9258 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9259 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9260 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9261 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9262 else
9263 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9264 }
9265 DeclsWithEffectsToVerify.clear();
9266
9267 SemaObj->OpenCLFeatures = OpenCLExtensions;
9268
9269 UpdateSema();
9270}
9271
9272void ASTReader::UpdateSema() {
9273 assert(SemaObj && "no Sema to update");
9274
9275 // Load the offsets of the declarations that Sema references.
9276 // They will be lazily deserialized when needed.
9277 if (!SemaDeclRefs.empty()) {
9278 assert(SemaDeclRefs.size() % 3 == 0);
9279 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9280 if (!SemaObj->StdNamespace)
9281 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9282 if (!SemaObj->StdBadAlloc)
9283 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9284 if (!SemaObj->StdAlignValT)
9285 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9286 }
9287 SemaDeclRefs.clear();
9288 }
9289
9290 // Update the state of pragmas. Use the same API as if we had encountered the
9291 // pragma in the source.
9292 if(OptimizeOffPragmaLocation.isValid())
9293 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9294 if (PragmaMSStructState != -1)
9295 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9296 if (PointersToMembersPragmaLocation.isValid()) {
9297 SemaObj->ActOnPragmaMSPointersToMembers(
9298 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9299 PragmaMSPointersToMembersState,
9300 PragmaLoc: PointersToMembersPragmaLocation);
9301 }
9302 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9303 if (!RISCVVecIntrinsicPragma.empty()) {
9304 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9305 "Wrong number of RISCVVecIntrinsicPragma");
9306 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9307 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9308 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9309 }
9310
9311 if (PragmaAlignPackCurrentValue) {
9312 // The bottom of the stack might have a default value. It must be adjusted
9313 // to the current value to ensure that the packing state is preserved after
9314 // popping entries that were included/imported from a PCH/module.
9315 bool DropFirst = false;
9316 if (!PragmaAlignPackStack.empty() &&
9317 PragmaAlignPackStack.front().Location.isInvalid()) {
9318 assert(PragmaAlignPackStack.front().Value ==
9319 SemaObj->AlignPackStack.DefaultValue &&
9320 "Expected a default alignment value");
9321 SemaObj->AlignPackStack.Stack.emplace_back(
9322 Args&: PragmaAlignPackStack.front().SlotLabel,
9323 Args&: SemaObj->AlignPackStack.CurrentValue,
9324 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9325 Args&: PragmaAlignPackStack.front().PushLocation);
9326 DropFirst = true;
9327 }
9328 for (const auto &Entry :
9329 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9330 SemaObj->AlignPackStack.Stack.emplace_back(
9331 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9332 }
9333 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9334 assert(*PragmaAlignPackCurrentValue ==
9335 SemaObj->AlignPackStack.DefaultValue &&
9336 "Expected a default align and pack value");
9337 // Keep the current values.
9338 } else {
9339 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9340 SemaObj->AlignPackStack.CurrentPragmaLocation =
9341 PragmaAlignPackCurrentLocation;
9342 }
9343 }
9344 if (FpPragmaCurrentValue) {
9345 // The bottom of the stack might have a default value. It must be adjusted
9346 // to the current value to ensure that fp-pragma state is preserved after
9347 // popping entries that were included/imported from a PCH/module.
9348 bool DropFirst = false;
9349 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9350 assert(FpPragmaStack.front().Value ==
9351 SemaObj->FpPragmaStack.DefaultValue &&
9352 "Expected a default pragma float_control value");
9353 SemaObj->FpPragmaStack.Stack.emplace_back(
9354 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9355 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9356 Args&: FpPragmaStack.front().PushLocation);
9357 DropFirst = true;
9358 }
9359 for (const auto &Entry :
9360 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9361 SemaObj->FpPragmaStack.Stack.emplace_back(
9362 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9363 if (FpPragmaCurrentLocation.isInvalid()) {
9364 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9365 "Expected a default pragma float_control value");
9366 // Keep the current values.
9367 } else {
9368 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9369 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9370 }
9371 }
9372
9373 // For non-modular AST files, restore visiblity of modules.
9374 for (auto &Import : PendingImportedModulesSema) {
9375 if (Import.ImportLoc.isInvalid())
9376 continue;
9377 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9378 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9379 }
9380 }
9381 PendingImportedModulesSema.clear();
9382}
9383
9384IdentifierInfo *ASTReader::get(StringRef Name) {
9385 // Note that we are loading an identifier.
9386 Deserializing AnIdentifier(this);
9387
9388 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9389 NumIdentifierLookups,
9390 NumIdentifierLookupHits);
9391
9392 // We don't need to do identifier table lookups in C++ modules (we preload
9393 // all interesting declarations, and don't need to use the scope for name
9394 // lookups). Perform the lookup in PCH files, though, since we don't build
9395 // a complete initial identifier table if we're carrying on from a PCH.
9396 if (PP.getLangOpts().CPlusPlus) {
9397 for (auto *F : ModuleMgr.pch_modules())
9398 if (Visitor(*F))
9399 break;
9400 } else {
9401 // If there is a global index, look there first to determine which modules
9402 // provably do not have any results for this identifier.
9403 GlobalModuleIndex::HitSet Hits;
9404 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9405 if (!loadGlobalIndex()) {
9406 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9407 HitsPtr = &Hits;
9408 }
9409 }
9410
9411 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9412 }
9413
9414 IdentifierInfo *II = Visitor.getIdentifierInfo();
9415 markIdentifierUpToDate(II);
9416 return II;
9417}
9418
9419namespace clang {
9420
9421 /// An identifier-lookup iterator that enumerates all of the
9422 /// identifiers stored within a set of AST files.
9423 class ASTIdentifierIterator : public IdentifierIterator {
9424 /// The AST reader whose identifiers are being enumerated.
9425 const ASTReader &Reader;
9426
9427 /// The current index into the chain of AST files stored in
9428 /// the AST reader.
9429 unsigned Index;
9430
9431 /// The current position within the identifier lookup table
9432 /// of the current AST file.
9433 ASTIdentifierLookupTable::key_iterator Current;
9434
9435 /// The end position within the identifier lookup table of
9436 /// the current AST file.
9437 ASTIdentifierLookupTable::key_iterator End;
9438
9439 /// Whether to skip any modules in the ASTReader.
9440 bool SkipModules;
9441
9442 public:
9443 explicit ASTIdentifierIterator(const ASTReader &Reader,
9444 bool SkipModules = false);
9445
9446 StringRef Next() override;
9447 };
9448
9449} // namespace clang
9450
9451ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9452 bool SkipModules)
9453 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9454}
9455
9456StringRef ASTIdentifierIterator::Next() {
9457 while (Current == End) {
9458 // If we have exhausted all of our AST files, we're done.
9459 if (Index == 0)
9460 return StringRef();
9461
9462 --Index;
9463 ModuleFile &F = Reader.ModuleMgr[Index];
9464 if (SkipModules && F.isModule())
9465 continue;
9466
9467 ASTIdentifierLookupTable *IdTable =
9468 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9469 Current = IdTable->key_begin();
9470 End = IdTable->key_end();
9471 }
9472
9473 // We have any identifiers remaining in the current AST file; return
9474 // the next one.
9475 StringRef Result = *Current;
9476 ++Current;
9477 return Result;
9478}
9479
9480namespace {
9481
9482/// A utility for appending two IdentifierIterators.
9483class ChainedIdentifierIterator : public IdentifierIterator {
9484 std::unique_ptr<IdentifierIterator> Current;
9485 std::unique_ptr<IdentifierIterator> Queued;
9486
9487public:
9488 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9489 std::unique_ptr<IdentifierIterator> Second)
9490 : Current(std::move(First)), Queued(std::move(Second)) {}
9491
9492 StringRef Next() override {
9493 if (!Current)
9494 return StringRef();
9495
9496 StringRef result = Current->Next();
9497 if (!result.empty())
9498 return result;
9499
9500 // Try the queued iterator, which may itself be empty.
9501 Current.reset();
9502 std::swap(x&: Current, y&: Queued);
9503 return Next();
9504 }
9505};
9506
9507} // namespace
9508
9509IdentifierIterator *ASTReader::getIdentifiers() {
9510 if (!loadGlobalIndex()) {
9511 std::unique_ptr<IdentifierIterator> ReaderIter(
9512 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9513 std::unique_ptr<IdentifierIterator> ModulesIter(
9514 GlobalIndex->createIdentifierIterator());
9515 return new ChainedIdentifierIterator(std::move(ReaderIter),
9516 std::move(ModulesIter));
9517 }
9518
9519 return new ASTIdentifierIterator(*this);
9520}
9521
9522namespace clang {
9523namespace serialization {
9524
9525 class ReadMethodPoolVisitor {
9526 ASTReader &Reader;
9527 Selector Sel;
9528 unsigned PriorGeneration;
9529 unsigned InstanceBits = 0;
9530 unsigned FactoryBits = 0;
9531 bool InstanceHasMoreThanOneDecl = false;
9532 bool FactoryHasMoreThanOneDecl = false;
9533 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9534 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9535
9536 public:
9537 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9538 unsigned PriorGeneration)
9539 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9540
9541 bool operator()(ModuleFile &M) {
9542 if (!M.SelectorLookupTable)
9543 return false;
9544
9545 // If we've already searched this module file, skip it now.
9546 if (M.Generation <= PriorGeneration)
9547 return true;
9548
9549 ++Reader.NumMethodPoolTableLookups;
9550 ASTSelectorLookupTable *PoolTable
9551 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9552 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9553 if (Pos == PoolTable->end())
9554 return false;
9555
9556 ++Reader.NumMethodPoolTableHits;
9557 ++Reader.NumSelectorsRead;
9558 // FIXME: Not quite happy with the statistics here. We probably should
9559 // disable this tracking when called via LoadSelector.
9560 // Also, should entries without methods count as misses?
9561 ++Reader.NumMethodPoolEntriesRead;
9562 ASTSelectorLookupTrait::data_type Data = *Pos;
9563 if (Reader.DeserializationListener)
9564 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9565
9566 // Append methods in the reverse order, so that later we can process them
9567 // in the order they appear in the source code by iterating through
9568 // the vector in the reverse order.
9569 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9570 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9571 InstanceBits = Data.InstanceBits;
9572 FactoryBits = Data.FactoryBits;
9573 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9574 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9575 return false;
9576 }
9577
9578 /// Retrieve the instance methods found by this visitor.
9579 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9580 return InstanceMethods;
9581 }
9582
9583 /// Retrieve the instance methods found by this visitor.
9584 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9585 return FactoryMethods;
9586 }
9587
9588 unsigned getInstanceBits() const { return InstanceBits; }
9589 unsigned getFactoryBits() const { return FactoryBits; }
9590
9591 bool instanceHasMoreThanOneDecl() const {
9592 return InstanceHasMoreThanOneDecl;
9593 }
9594
9595 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9596 };
9597
9598} // namespace serialization
9599} // namespace clang
9600
9601/// Add the given set of methods to the method list.
9602static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9603 ObjCMethodList &List) {
9604 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9605 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9606}
9607
9608void ASTReader::ReadMethodPool(Selector Sel) {
9609 // Get the selector generation and update it to the current generation.
9610 unsigned &Generation = SelectorGeneration[Sel];
9611 unsigned PriorGeneration = Generation;
9612 Generation = getGeneration();
9613 SelectorOutOfDate[Sel] = false;
9614
9615 // Search for methods defined with this selector.
9616 ++NumMethodPoolLookups;
9617 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9618 ModuleMgr.visit(Visitor);
9619
9620 if (Visitor.getInstanceMethods().empty() &&
9621 Visitor.getFactoryMethods().empty())
9622 return;
9623
9624 ++NumMethodPoolHits;
9625
9626 if (!getSema())
9627 return;
9628
9629 Sema &S = *getSema();
9630 auto &Methods = S.ObjC().MethodPool[Sel];
9631
9632 Methods.first.setBits(Visitor.getInstanceBits());
9633 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9634 Methods.second.setBits(Visitor.getFactoryBits());
9635 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9636
9637 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9638 // when building a module we keep every method individually and may need to
9639 // update hasMoreThanOneDecl as we add the methods.
9640 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9641 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9642}
9643
9644void ASTReader::updateOutOfDateSelector(Selector Sel) {
9645 if (SelectorOutOfDate[Sel])
9646 ReadMethodPool(Sel);
9647}
9648
9649void ASTReader::ReadKnownNamespaces(
9650 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9651 Namespaces.clear();
9652
9653 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9654 if (NamespaceDecl *Namespace
9655 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9656 Namespaces.push_back(Elt: Namespace);
9657 }
9658}
9659
9660void ASTReader::ReadUndefinedButUsed(
9661 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9662 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9663 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9664 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9665 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9666 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9667 }
9668 UndefinedButUsed.clear();
9669}
9670
9671void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9672 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9673 Exprs) {
9674 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9675 FieldDecl *FD =
9676 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9677 uint64_t Count = DelayedDeleteExprs[Idx++];
9678 for (uint64_t C = 0; C < Count; ++C) {
9679 SourceLocation DeleteLoc =
9680 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9681 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9682 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9683 }
9684 }
9685}
9686
9687void ASTReader::ReadTentativeDefinitions(
9688 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9689 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9690 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9691 if (Var)
9692 TentativeDefs.push_back(Elt: Var);
9693 }
9694 TentativeDefinitions.clear();
9695}
9696
9697void ASTReader::ReadUnusedFileScopedDecls(
9698 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9699 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9700 DeclaratorDecl *D
9701 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9702 if (D)
9703 Decls.push_back(Elt: D);
9704 }
9705 UnusedFileScopedDecls.clear();
9706}
9707
9708void ASTReader::ReadDelegatingConstructors(
9709 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9710 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9711 CXXConstructorDecl *D
9712 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9713 if (D)
9714 Decls.push_back(Elt: D);
9715 }
9716 DelegatingCtorDecls.clear();
9717}
9718
9719void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9720 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9721 TypedefNameDecl *D
9722 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9723 if (D)
9724 Decls.push_back(Elt: D);
9725 }
9726 ExtVectorDecls.clear();
9727}
9728
9729void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9730 llvm::SmallPtrSetImpl<const TypedefNameDecl *> &Decls) {
9731 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9732 ++I) {
9733 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9734 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9735 if (D)
9736 Decls.insert(Ptr: D);
9737 }
9738 UnusedLocalTypedefNameCandidates.clear();
9739}
9740
9741void ASTReader::ReadDeclsToCheckForDeferredDiags(
9742 llvm::SmallSetVector<Decl *, 4> &Decls) {
9743 for (auto I : DeclsToCheckForDeferredDiags) {
9744 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9745 if (D)
9746 Decls.insert(X: D);
9747 }
9748 DeclsToCheckForDeferredDiags.clear();
9749}
9750
9751void ASTReader::ReadReferencedSelectors(
9752 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9753 if (ReferencedSelectorsData.empty())
9754 return;
9755
9756 // If there are @selector references added them to its pool. This is for
9757 // implementation of -Wselector.
9758 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9759 unsigned I = 0;
9760 while (I < DataSize) {
9761 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9762 SourceLocation SelLoc
9763 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9764 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9765 }
9766 ReferencedSelectorsData.clear();
9767}
9768
9769void ASTReader::ReadWeakUndeclaredIdentifiers(
9770 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9771 if (WeakUndeclaredIdentifiers.empty())
9772 return;
9773
9774 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9775 IdentifierInfo *WeakId
9776 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9777 IdentifierInfo *AliasId
9778 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9779 SourceLocation Loc =
9780 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9781 WeakInfo WI(AliasId, Loc);
9782 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9783 }
9784 WeakUndeclaredIdentifiers.clear();
9785}
9786
9787void ASTReader::ReadExtnameUndeclaredIdentifiers(
9788 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9789 if (ExtnameUndeclaredIdentifiers.empty())
9790 return;
9791
9792 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9793 IdentifierInfo *NameId =
9794 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9795 IdentifierInfo *ExtnameId =
9796 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9797 SourceLocation Loc =
9798 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9799 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9800 Ctx&: getContext(), Label: ExtnameId->getName(),
9801 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9802 AttributeCommonInfo::Form::Pragma()));
9803 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9804 }
9805 ExtnameUndeclaredIdentifiers.clear();
9806}
9807
9808void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9809 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9810 ExternalVTableUse VT;
9811 VTableUse &TableInfo = VTableUses[Idx++];
9812 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9813 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9814 VT.DefinitionRequired = TableInfo.Used;
9815 VTables.push_back(Elt: VT);
9816 }
9817
9818 VTableUses.clear();
9819}
9820
9821void ASTReader::ReadPendingInstantiations(
9822 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9823 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9824 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9825 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9826 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9827
9828 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9829 }
9830 PendingInstantiations.clear();
9831}
9832
9833void ASTReader::ReadLateParsedTemplates(
9834 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9835 &LPTMap) {
9836 for (auto &LPT : LateParsedTemplates) {
9837 ModuleFile *FMod = LPT.first;
9838 RecordDataImpl &LateParsed = LPT.second;
9839 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9840 /* In loop */) {
9841 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9842
9843 auto LT = std::make_unique<LateParsedTemplate>();
9844 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9845 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9846
9847 ModuleFile *F = getOwningModuleFile(D: LT->D);
9848 assert(F && "No module");
9849
9850 unsigned TokN = LateParsed[Idx++];
9851 LT->Toks.reserve(N: TokN);
9852 for (unsigned T = 0; T < TokN; ++T)
9853 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9854
9855 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9856 }
9857 }
9858
9859 LateParsedTemplates.clear();
9860}
9861
9862void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9863 if (!Lambda->getLambdaContextDecl())
9864 return;
9865
9866 auto LambdaInfo =
9867 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9868 y: Lambda->getLambdaIndexInContext());
9869
9870 // Handle the import and then include case for lambdas.
9871 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9872 Iter != LambdaDeclarationsForMerging.end() &&
9873 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9874 CXXRecordDecl *Previous =
9875 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9876 Lambda->setPreviousDecl(Previous);
9877 return;
9878 }
9879
9880 // Keep track of this lambda so it can be merged with another lambda that
9881 // is loaded later.
9882 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9883}
9884
9885void ASTReader::LoadSelector(Selector Sel) {
9886 // It would be complicated to avoid reading the methods anyway. So don't.
9887 ReadMethodPool(Sel);
9888}
9889
9890void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9891 assert(ID && "Non-zero identifier ID required");
9892 unsigned Index = translateIdentifierIDToIndex(ID).second;
9893 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9894 IdentifiersLoaded[Index] = II;
9895 if (DeserializationListener)
9896 DeserializationListener->IdentifierRead(ID, II);
9897}
9898
9899/// Set the globally-visible declarations associated with the given
9900/// identifier.
9901///
9902/// If the AST reader is currently in a state where the given declaration IDs
9903/// cannot safely be resolved, they are queued until it is safe to resolve
9904/// them.
9905///
9906/// \param II an IdentifierInfo that refers to one or more globally-visible
9907/// declarations.
9908///
9909/// \param DeclIDs the set of declaration IDs with the name @p II that are
9910/// visible at global scope.
9911///
9912/// \param Decls if non-null, this vector will be populated with the set of
9913/// deserialized declarations. These declarations will not be pushed into
9914/// scope.
9915void ASTReader::SetGloballyVisibleDecls(
9916 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9917 SmallVectorImpl<Decl *> *Decls) {
9918 if (NumCurrentElementsDeserializing && !Decls) {
9919 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9920 return;
9921 }
9922
9923 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9924 if (!SemaObj) {
9925 // Queue this declaration so that it will be added to the
9926 // translation unit scope and identifier's declaration chain
9927 // once a Sema object is known.
9928 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9929 continue;
9930 }
9931
9932 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9933
9934 // If we're simply supposed to record the declarations, do so now.
9935 if (Decls) {
9936 Decls->push_back(Elt: D);
9937 continue;
9938 }
9939
9940 // Introduce this declaration into the translation-unit scope
9941 // and add it to the declaration chain for this identifier, so
9942 // that (unqualified) name lookup will find it.
9943 pushExternalDeclIntoScope(D, Name: II);
9944 }
9945}
9946
9947std::pair<ModuleFile *, unsigned>
9948ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9949 if (ID == 0)
9950 return {nullptr, 0};
9951
9952 unsigned ModuleFileIndex = ID >> 32;
9953 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9954
9955 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9956 assert(getModuleManager().size() > ModuleFileIndex - 1);
9957
9958 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9959 assert(LocalID < MF.LocalNumIdentifiers);
9960 return {&MF, MF.BaseIdentifierID + LocalID};
9961}
9962
9963IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9964 if (ID == 0)
9965 return nullptr;
9966
9967 if (IdentifiersLoaded.empty()) {
9968 Error(Msg: "no identifier table in AST file");
9969 return nullptr;
9970 }
9971
9972 auto [M, Index] = translateIdentifierIDToIndex(ID);
9973 if (!IdentifiersLoaded[Index]) {
9974 assert(M != nullptr && "Untranslated Identifier ID?");
9975 assert(Index >= M->BaseIdentifierID);
9976 unsigned LocalIndex = Index - M->BaseIdentifierID;
9977 const unsigned char *Data =
9978 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9979
9980 ASTIdentifierLookupTrait Trait(*this, *M);
9981 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9982 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9983 auto &II = PP.getIdentifierTable().get(Name: Key);
9984 IdentifiersLoaded[Index] = &II;
9985 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9986 markIdentifierFromAST(Reader&: *this, II, IsModule);
9987 if (DeserializationListener)
9988 DeserializationListener->IdentifierRead(ID, II: &II);
9989 }
9990
9991 return IdentifiersLoaded[Index];
9992}
9993
9994IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9995 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9996}
9997
9998IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9999 if (LocalID < NUM_PREDEF_IDENT_IDS)
10000 return LocalID;
10001
10002 if (!M.ModuleOffsetMap.empty())
10003 ReadModuleOffsetMap(F&: M);
10004
10005 unsigned ModuleFileIndex = LocalID >> 32;
10006 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
10007 ModuleFile *MF =
10008 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10009 assert(MF && "malformed identifier ID encoding?");
10010
10011 if (!ModuleFileIndex)
10012 LocalID -= NUM_PREDEF_IDENT_IDS;
10013
10014 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10015}
10016
10017std::pair<ModuleFile *, unsigned>
10018ASTReader::translateMacroIDToIndex(MacroID ID) const {
10019 if (ID == 0)
10020 return {nullptr, 0};
10021
10022 unsigned ModuleFileIndex = ID >> 32;
10023 assert(ModuleFileIndex && "not translating loaded MacroID?");
10024 assert(getModuleManager().size() > ModuleFileIndex - 1);
10025 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10026
10027 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10028 assert(LocalID < MF.LocalNumMacros);
10029 return {&MF, MF.BaseMacroID + LocalID};
10030}
10031
10032MacroInfo *ASTReader::getMacro(MacroID ID) {
10033 if (ID == 0)
10034 return nullptr;
10035
10036 if (MacrosLoaded.empty()) {
10037 Error(Msg: "no macro table in AST file");
10038 return nullptr;
10039 }
10040
10041 auto [M, Index] = translateMacroIDToIndex(ID);
10042 if (!MacrosLoaded[Index]) {
10043 assert(M != nullptr && "Untranslated Macro ID?");
10044 assert(Index >= M->BaseMacroID);
10045 unsigned LocalIndex = Index - M->BaseMacroID;
10046 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10047 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10048
10049 if (DeserializationListener)
10050 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10051 }
10052
10053 return MacrosLoaded[Index];
10054}
10055
10056MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10057 if (LocalID < NUM_PREDEF_MACRO_IDS)
10058 return LocalID;
10059
10060 if (!M.ModuleOffsetMap.empty())
10061 ReadModuleOffsetMap(F&: M);
10062
10063 unsigned ModuleFileIndex = LocalID >> 32;
10064 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10065 ModuleFile *MF =
10066 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10067 assert(MF && "malformed identifier ID encoding?");
10068
10069 if (!ModuleFileIndex) {
10070 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10071 LocalID -= NUM_PREDEF_MACRO_IDS;
10072 }
10073
10074 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10075}
10076
10077serialization::SubmoduleID
10078ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10079 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10080 return LocalID;
10081
10082 if (!M.ModuleOffsetMap.empty())
10083 ReadModuleOffsetMap(F&: M);
10084
10085 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10086 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10087 assert(I != M.SubmoduleRemap.end()
10088 && "Invalid index into submodule index remap");
10089
10090 return LocalID + I->second;
10091}
10092
10093Module *ASTReader::getModule(unsigned ID) {
10094 return getSubmodule(GlobalID: ID);
10095}
10096
10097ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10098 if (ID & 1) {
10099 // It's a module, look it up by submodule ID.
10100 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10101 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10102 } else {
10103 // It's a prefix (preamble, PCH, ...). Look it up by index.
10104 int IndexFromEnd = static_cast<int>(ID >> 1);
10105 assert(IndexFromEnd && "got reference to unknown module file");
10106 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10107 }
10108}
10109
10110unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10111 if (!M)
10112 return 1;
10113
10114 // For a file representing a module, use the submodule ID of the top-level
10115 // module as the file ID. For any other kind of file, the number of such
10116 // files loaded beforehand will be the same on reload.
10117 // FIXME: Is this true even if we have an explicit module file and a PCH?
10118 if (M->isModule())
10119 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10120
10121 auto PCHModules = getModuleManager().pch_modules();
10122 auto I = llvm::find(Range&: PCHModules, Val: M);
10123 assert(I != PCHModules.end() && "emitting reference to unknown file");
10124 return std::distance(first: I, last: PCHModules.end()) << 1;
10125}
10126
10127std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10128 if (Module *M = getSubmodule(GlobalID: ID))
10129 return ASTSourceDescriptor(*M);
10130
10131 // If there is only a single PCH, return it instead.
10132 // Chained PCH are not supported.
10133 const auto &PCHChain = ModuleMgr.pch_modules();
10134 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10135 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10136 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10137 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10138 return ASTSourceDescriptor(ModuleName,
10139 llvm::sys::path::parent_path(path: MF.FileName),
10140 FileName, MF.Signature);
10141 }
10142 return std::nullopt;
10143}
10144
10145ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10146 auto I = DefinitionSource.find(Val: FD);
10147 if (I == DefinitionSource.end())
10148 return EK_ReplyHazy;
10149 return I->second ? EK_Never : EK_Always;
10150}
10151
10152bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10153 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10154}
10155
10156Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10157 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10158}
10159
10160Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10161 if (ID == 0)
10162 return Selector();
10163
10164 if (ID > SelectorsLoaded.size()) {
10165 Error(Msg: "selector ID out of range in AST file");
10166 return Selector();
10167 }
10168
10169 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10170 // Load this selector from the selector table.
10171 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10172 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10173 ModuleFile &M = *I->second;
10174 ASTSelectorLookupTrait Trait(*this, M);
10175 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10176 SelectorsLoaded[ID - 1] =
10177 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10178 if (DeserializationListener)
10179 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10180 }
10181
10182 return SelectorsLoaded[ID - 1];
10183}
10184
10185Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10186 return DecodeSelector(ID);
10187}
10188
10189uint32_t ASTReader::GetNumExternalSelectors() {
10190 // ID 0 (the null selector) is considered an external selector.
10191 return getTotalNumSelectors() + 1;
10192}
10193
10194serialization::SelectorID
10195ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10196 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10197 return LocalID;
10198
10199 if (!M.ModuleOffsetMap.empty())
10200 ReadModuleOffsetMap(F&: M);
10201
10202 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10203 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10204 assert(I != M.SelectorRemap.end()
10205 && "Invalid index into selector index remap");
10206
10207 return LocalID + I->second;
10208}
10209
10210DeclarationNameLoc
10211ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10212 switch (Name.getNameKind()) {
10213 case DeclarationName::CXXConstructorName:
10214 case DeclarationName::CXXDestructorName:
10215 case DeclarationName::CXXConversionFunctionName:
10216 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10217
10218 case DeclarationName::CXXOperatorName:
10219 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10220
10221 case DeclarationName::CXXLiteralOperatorName:
10222 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10223 Loc: readSourceLocation());
10224
10225 case DeclarationName::Identifier:
10226 case DeclarationName::ObjCZeroArgSelector:
10227 case DeclarationName::ObjCOneArgSelector:
10228 case DeclarationName::ObjCMultiArgSelector:
10229 case DeclarationName::CXXUsingDirective:
10230 case DeclarationName::CXXDeductionGuideName:
10231 break;
10232 }
10233 return DeclarationNameLoc();
10234}
10235
10236DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10237 DeclarationNameInfo NameInfo;
10238 NameInfo.setName(readDeclarationName());
10239 NameInfo.setLoc(readSourceLocation());
10240 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10241 return NameInfo;
10242}
10243
10244TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10245 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10246}
10247
10248SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10249 auto Kind = readInt();
10250 auto ResultType = readQualType();
10251 auto Value = readAPInt();
10252 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10253 assert(Op.isValid());
10254 return Op;
10255}
10256
10257void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10258 Info.QualifierLoc = readNestedNameSpecifierLoc();
10259 unsigned NumTPLists = readInt();
10260 Info.NumTemplParamLists = NumTPLists;
10261 if (NumTPLists) {
10262 Info.TemplParamLists =
10263 new (getContext()) TemplateParameterList *[NumTPLists];
10264 for (unsigned i = 0; i != NumTPLists; ++i)
10265 Info.TemplParamLists[i] = readTemplateParameterList();
10266 }
10267}
10268
10269TemplateParameterList *
10270ASTRecordReader::readTemplateParameterList() {
10271 SourceLocation TemplateLoc = readSourceLocation();
10272 SourceLocation LAngleLoc = readSourceLocation();
10273 SourceLocation RAngleLoc = readSourceLocation();
10274
10275 unsigned NumParams = readInt();
10276 SmallVector<NamedDecl *, 16> Params;
10277 Params.reserve(N: NumParams);
10278 while (NumParams--)
10279 Params.push_back(Elt: readDeclAs<NamedDecl>());
10280
10281 bool HasRequiresClause = readBool();
10282 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10283
10284 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10285 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10286 return TemplateParams;
10287}
10288
10289void ASTRecordReader::readTemplateArgumentList(
10290 SmallVectorImpl<TemplateArgument> &TemplArgs,
10291 bool Canonicalize) {
10292 unsigned NumTemplateArgs = readInt();
10293 TemplArgs.reserve(N: NumTemplateArgs);
10294 while (NumTemplateArgs--)
10295 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10296}
10297
10298/// Read a UnresolvedSet structure.
10299void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10300 unsigned NumDecls = readInt();
10301 Set.reserve(C&: getContext(), N: NumDecls);
10302 while (NumDecls--) {
10303 GlobalDeclID ID = readDeclID();
10304 AccessSpecifier AS = (AccessSpecifier) readInt();
10305 Set.addLazyDecl(C&: getContext(), ID, AS);
10306 }
10307}
10308
10309CXXBaseSpecifier
10310ASTRecordReader::readCXXBaseSpecifier() {
10311 bool isVirtual = readBool();
10312 bool isBaseOfClass = readBool();
10313 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10314 bool inheritConstructors = readBool();
10315 TypeSourceInfo *TInfo = readTypeSourceInfo();
10316 SourceRange Range = readSourceRange();
10317 SourceLocation EllipsisLoc = readSourceLocation();
10318 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10319 EllipsisLoc);
10320 Result.setInheritConstructors(inheritConstructors);
10321 return Result;
10322}
10323
10324CXXCtorInitializer **
10325ASTRecordReader::readCXXCtorInitializers() {
10326 ASTContext &Context = getContext();
10327 unsigned NumInitializers = readInt();
10328 assert(NumInitializers && "wrote ctor initializers but have no inits");
10329 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10330 for (unsigned i = 0; i != NumInitializers; ++i) {
10331 TypeSourceInfo *TInfo = nullptr;
10332 bool IsBaseVirtual = false;
10333 FieldDecl *Member = nullptr;
10334 IndirectFieldDecl *IndirectMember = nullptr;
10335
10336 CtorInitializerType Type = (CtorInitializerType) readInt();
10337 switch (Type) {
10338 case CTOR_INITIALIZER_BASE:
10339 TInfo = readTypeSourceInfo();
10340 IsBaseVirtual = readBool();
10341 break;
10342
10343 case CTOR_INITIALIZER_DELEGATING:
10344 TInfo = readTypeSourceInfo();
10345 break;
10346
10347 case CTOR_INITIALIZER_MEMBER:
10348 Member = readDeclAs<FieldDecl>();
10349 break;
10350
10351 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10352 IndirectMember = readDeclAs<IndirectFieldDecl>();
10353 break;
10354 }
10355
10356 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10357 Expr *Init = readExpr();
10358 SourceLocation LParenLoc = readSourceLocation();
10359 SourceLocation RParenLoc = readSourceLocation();
10360
10361 CXXCtorInitializer *BOMInit;
10362 if (Type == CTOR_INITIALIZER_BASE)
10363 BOMInit = new (Context)
10364 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10365 RParenLoc, MemberOrEllipsisLoc);
10366 else if (Type == CTOR_INITIALIZER_DELEGATING)
10367 BOMInit = new (Context)
10368 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10369 else if (Member)
10370 BOMInit = new (Context)
10371 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10372 Init, RParenLoc);
10373 else
10374 BOMInit = new (Context)
10375 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10376 LParenLoc, Init, RParenLoc);
10377
10378 if (/*IsWritten*/readBool()) {
10379 unsigned SourceOrder = readInt();
10380 BOMInit->setSourceOrder(SourceOrder);
10381 }
10382
10383 CtorInitializers[i] = BOMInit;
10384 }
10385
10386 return CtorInitializers;
10387}
10388
10389NestedNameSpecifierLoc
10390ASTRecordReader::readNestedNameSpecifierLoc() {
10391 ASTContext &Context = getContext();
10392 unsigned N = readInt();
10393 NestedNameSpecifierLocBuilder Builder;
10394 for (unsigned I = 0; I != N; ++I) {
10395 auto Kind = readNestedNameSpecifierKind();
10396 switch (Kind) {
10397 case NestedNameSpecifier::Kind::Namespace: {
10398 auto *NS = readDeclAs<NamespaceBaseDecl>();
10399 SourceRange Range = readSourceRange();
10400 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10401 break;
10402 }
10403
10404 case NestedNameSpecifier::Kind::Type: {
10405 TypeSourceInfo *T = readTypeSourceInfo();
10406 if (!T)
10407 return NestedNameSpecifierLoc();
10408 SourceLocation ColonColonLoc = readSourceLocation();
10409 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10410 break;
10411 }
10412
10413 case NestedNameSpecifier::Kind::Global: {
10414 SourceLocation ColonColonLoc = readSourceLocation();
10415 Builder.MakeGlobal(Context, ColonColonLoc);
10416 break;
10417 }
10418
10419 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10420 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10421 SourceRange Range = readSourceRange();
10422 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10423 break;
10424 }
10425
10426 case NestedNameSpecifier::Kind::Null:
10427 llvm_unreachable("unexpected null nested name specifier");
10428 }
10429 }
10430
10431 return Builder.getWithLocInContext(Context);
10432}
10433
10434SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10435 unsigned &Idx) {
10436 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10437 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10438 return SourceRange(beg, end);
10439}
10440
10441llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10442 const StringRef Blob) {
10443 unsigned Count = Record[0];
10444 const char *Byte = Blob.data();
10445 llvm::BitVector Ret = llvm::BitVector(Count, false);
10446 for (unsigned I = 0; I < Count; ++Byte)
10447 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10448 if (*Byte & (1 << Bit))
10449 Ret[I] = true;
10450 return Ret;
10451}
10452
10453/// Read a floating-point value
10454llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10455 return llvm::APFloat(Sem, readAPInt());
10456}
10457
10458// Read a string
10459std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10460 unsigned Len = Record[Idx++];
10461 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10462 Idx += Len;
10463 return Result;
10464}
10465
10466StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10467 StringRef &Blob) {
10468 unsigned Len = Record[Idx++];
10469 StringRef Result = Blob.substr(Start: 0, N: Len);
10470 Blob = Blob.substr(Start: Len);
10471 return Result;
10472}
10473
10474std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10475 unsigned &Idx) {
10476 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10477}
10478
10479std::string ASTReader::ReadPath(StringRef BaseDirectory,
10480 const RecordData &Record, unsigned &Idx) {
10481 std::string Filename = ReadString(Record, Idx);
10482 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10483}
10484
10485std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10486 const RecordData &Record, unsigned &Idx,
10487 StringRef &Blob) {
10488 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10489 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10490}
10491
10492VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10493 unsigned &Idx) {
10494 unsigned Major = Record[Idx++];
10495 unsigned Minor = Record[Idx++];
10496 unsigned Subminor = Record[Idx++];
10497 if (Minor == 0)
10498 return VersionTuple(Major);
10499 if (Subminor == 0)
10500 return VersionTuple(Major, Minor - 1);
10501 return VersionTuple(Major, Minor - 1, Subminor - 1);
10502}
10503
10504CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10505 const RecordData &Record,
10506 unsigned &Idx) {
10507 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10508 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10509}
10510
10511DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10512 return Diag(Loc: CurrentImportLoc, DiagID);
10513}
10514
10515DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10516 return Diags.Report(Loc, DiagID);
10517}
10518
10519void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10520 llvm::function_ref<void()> Fn) {
10521 // When Sema is available, avoid duplicate errors.
10522 if (SemaObj) {
10523 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10524 return;
10525 }
10526
10527 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10528}
10529
10530/// Retrieve the identifier table associated with the
10531/// preprocessor.
10532IdentifierTable &ASTReader::getIdentifierTable() {
10533 return PP.getIdentifierTable();
10534}
10535
10536/// Record that the given ID maps to the given switch-case
10537/// statement.
10538void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10539 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10540 "Already have a SwitchCase with this ID");
10541 (*CurrSwitchCaseStmts)[ID] = SC;
10542}
10543
10544/// Retrieve the switch-case statement with the given ID.
10545SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10546 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10547 return (*CurrSwitchCaseStmts)[ID];
10548}
10549
10550void ASTReader::ClearSwitchCaseIDs() {
10551 CurrSwitchCaseStmts->clear();
10552}
10553
10554void ASTReader::ReadComments() {
10555 ASTContext &Context = getContext();
10556 std::vector<RawComment *> Comments;
10557 for (SmallVectorImpl<std::pair<BitstreamCursor,
10558 serialization::ModuleFile *>>::iterator
10559 I = CommentsCursors.begin(),
10560 E = CommentsCursors.end();
10561 I != E; ++I) {
10562 Comments.clear();
10563 BitstreamCursor &Cursor = I->first;
10564 serialization::ModuleFile &F = *I->second;
10565 SavedStreamPosition SavedPosition(Cursor);
10566
10567 RecordData Record;
10568 while (true) {
10569 Expected<llvm::BitstreamEntry> MaybeEntry =
10570 Cursor.advanceSkippingSubblocks(
10571 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10572 if (!MaybeEntry) {
10573 Error(Err: MaybeEntry.takeError());
10574 return;
10575 }
10576 llvm::BitstreamEntry Entry = MaybeEntry.get();
10577
10578 switch (Entry.Kind) {
10579 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10580 case llvm::BitstreamEntry::Error:
10581 Error(Msg: "malformed block record in AST file");
10582 return;
10583 case llvm::BitstreamEntry::EndBlock:
10584 goto NextCursor;
10585 case llvm::BitstreamEntry::Record:
10586 // The interesting case.
10587 break;
10588 }
10589
10590 // Read a record.
10591 Record.clear();
10592 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10593 if (!MaybeComment) {
10594 Error(Err: MaybeComment.takeError());
10595 return;
10596 }
10597 switch ((CommentRecordTypes)MaybeComment.get()) {
10598 case COMMENTS_RAW_COMMENT: {
10599 unsigned Idx = 0;
10600 SourceRange SR = ReadSourceRange(F, Record, Idx);
10601 RawComment::CommentKind Kind =
10602 (RawComment::CommentKind) Record[Idx++];
10603 bool IsTrailingComment = Record[Idx++];
10604 bool IsAlmostTrailingComment = Record[Idx++];
10605 Comments.push_back(x: new (Context) RawComment(
10606 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10607 break;
10608 }
10609 }
10610 }
10611 NextCursor:
10612 for (RawComment *C : Comments) {
10613 SourceLocation CommentLoc = C->getBeginLoc();
10614 if (CommentLoc.isValid()) {
10615 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10616 if (Loc.first.isValid())
10617 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10618 }
10619 }
10620 }
10621}
10622
10623void ASTReader::visitInputFileInfos(
10624 serialization::ModuleFile &MF, bool IncludeSystem,
10625 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10626 bool IsSystem)>
10627 Visitor) {
10628 unsigned NumUserInputs = MF.NumUserInputFiles;
10629 unsigned NumInputs = MF.InputFilesLoaded.size();
10630 assert(NumUserInputs <= NumInputs);
10631 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10632 for (unsigned I = 0; I < N; ++I) {
10633 bool IsSystem = I >= NumUserInputs;
10634 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10635 Visitor(IFI, IsSystem);
10636 }
10637}
10638
10639void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10640 bool IncludeSystem, bool Complain,
10641 llvm::function_ref<void(const serialization::InputFile &IF,
10642 bool isSystem)> Visitor) {
10643 unsigned NumUserInputs = MF.NumUserInputFiles;
10644 unsigned NumInputs = MF.InputFilesLoaded.size();
10645 assert(NumUserInputs <= NumInputs);
10646 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10647 for (unsigned I = 0; I < N; ++I) {
10648 bool IsSystem = I >= NumUserInputs;
10649 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10650 Visitor(IF, IsSystem);
10651 }
10652}
10653
10654void ASTReader::visitTopLevelModuleMaps(
10655 serialization::ModuleFile &MF,
10656 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10657 unsigned NumInputs = MF.InputFilesLoaded.size();
10658 for (unsigned I = 0; I < NumInputs; ++I) {
10659 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10660 if (IFI.TopLevel && IFI.ModuleMap)
10661 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10662 Visitor(*FE);
10663 }
10664}
10665
10666void ASTReader::finishPendingActions() {
10667 while (!PendingIdentifierInfos.empty() ||
10668 !PendingDeducedFunctionTypes.empty() ||
10669 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10670 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10671 !PendingUpdateRecords.empty() ||
10672 !PendingObjCExtensionIvarRedeclarations.empty()) {
10673 // If any identifiers with corresponding top-level declarations have
10674 // been loaded, load those declarations now.
10675 using TopLevelDeclsMap =
10676 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10677 TopLevelDeclsMap TopLevelDecls;
10678
10679 while (!PendingIdentifierInfos.empty()) {
10680 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10681 SmallVector<GlobalDeclID, 4> DeclIDs =
10682 std::move(PendingIdentifierInfos.back().second);
10683 PendingIdentifierInfos.pop_back();
10684
10685 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10686 }
10687
10688 // Load each function type that we deferred loading because it was a
10689 // deduced type that might refer to a local type declared within itself.
10690 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10691 auto *FD = PendingDeducedFunctionTypes[I].first;
10692 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10693
10694 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10695 // If we gave a function a deduced return type, remember that we need to
10696 // propagate that along the redeclaration chain.
10697 if (DT->isDeduced()) {
10698 PendingDeducedTypeUpdates.insert(
10699 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10700 continue;
10701 }
10702
10703 // The function has undeduced DeduceType return type. We hope we can
10704 // find the deduced type by iterating the redecls in other modules
10705 // later.
10706 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10707 continue;
10708 }
10709 }
10710 PendingDeducedFunctionTypes.clear();
10711
10712 // Load each variable type that we deferred loading because it was a
10713 // deduced type that might refer to a local type declared within itself.
10714 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10715 auto *VD = PendingDeducedVarTypes[I].first;
10716 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10717 }
10718 PendingDeducedVarTypes.clear();
10719
10720 // Load pending declaration chains.
10721 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10722 loadPendingDeclChain(D: PendingDeclChains[I].first,
10723 LocalOffset: PendingDeclChains[I].second);
10724 PendingDeclChains.clear();
10725
10726 // Make the most recent of the top-level declarations visible.
10727 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10728 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10729 IdentifierInfo *II = TLD->first;
10730 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10731 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10732 }
10733 }
10734
10735 // Load any pending macro definitions.
10736 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10737 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10738 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10739 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10740 // Initialize the macro history from chained-PCHs ahead of module imports.
10741 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10742 ++IDIdx) {
10743 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10744 if (!Info.M->isModule())
10745 resolvePendingMacro(II, PMInfo: Info);
10746 }
10747 // Handle module imports.
10748 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10749 ++IDIdx) {
10750 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10751 if (Info.M->isModule())
10752 resolvePendingMacro(II, PMInfo: Info);
10753 }
10754 }
10755 PendingMacroIDs.clear();
10756
10757 // Wire up the DeclContexts for Decls that we delayed setting until
10758 // recursive loading is completed.
10759 while (!PendingDeclContextInfos.empty()) {
10760 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10761 PendingDeclContextInfos.pop_front();
10762 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10763 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10764 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10765 }
10766
10767 // Perform any pending declaration updates.
10768 while (!PendingUpdateRecords.empty()) {
10769 auto Update = PendingUpdateRecords.pop_back_val();
10770 ReadingKindTracker ReadingKind(Read_Decl, *this);
10771 loadDeclUpdateRecords(Record&: Update);
10772 }
10773
10774 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10775 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10776 auto DuplicateIvars =
10777 PendingObjCExtensionIvarRedeclarations.back().second;
10778 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10779 StructuralEquivalenceContext Ctx(
10780 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10781 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10782 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10783 /*Complain =*/false,
10784 /*ErrorOnTagTypeMismatch =*/true);
10785 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10786 // Merge redeclared ivars with their predecessors.
10787 for (auto IvarPair : DuplicateIvars) {
10788 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10789 // Change semantic DeclContext but keep the lexical one.
10790 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10791 LexicalDC: Ivar->getLexicalDeclContext(),
10792 Ctx&: getContext());
10793 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10794 }
10795 // Invalidate duplicate extension and the cached ivar list.
10796 ExtensionsPair.first->setInvalidDecl();
10797 ExtensionsPair.second->getClassInterface()
10798 ->getDefinition()
10799 ->setIvarList(nullptr);
10800 } else {
10801 for (auto IvarPair : DuplicateIvars) {
10802 Diag(Loc: IvarPair.first->getLocation(),
10803 DiagID: diag::err_duplicate_ivar_declaration)
10804 << IvarPair.first->getIdentifier();
10805 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10806 }
10807 }
10808 PendingObjCExtensionIvarRedeclarations.pop_back();
10809 }
10810 }
10811
10812 // At this point, all update records for loaded decls are in place, so any
10813 // fake class definitions should have become real.
10814 assert(PendingFakeDefinitionData.empty() &&
10815 "faked up a class definition but never saw the real one");
10816
10817 // If we deserialized any C++ or Objective-C class definitions, any
10818 // Objective-C protocol definitions, or any redeclarable templates, make sure
10819 // that all redeclarations point to the definitions. Note that this can only
10820 // happen now, after the redeclaration chains have been fully wired.
10821 for (Decl *D : PendingDefinitions) {
10822 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10823 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10824 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10825 R = R->getPreviousDecl()) {
10826 assert((R == D) ==
10827 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10828 "declaration thinks it's the definition but it isn't");
10829 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10830 }
10831 }
10832
10833 continue;
10834 }
10835
10836 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10837 // Make sure that the ObjCInterfaceType points at the definition.
10838 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10839 ->Decl = ID;
10840
10841 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10842 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10843
10844 continue;
10845 }
10846
10847 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10848 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10849 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10850
10851 continue;
10852 }
10853
10854 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10855 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10856 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10857 }
10858 PendingDefinitions.clear();
10859
10860 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10861 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10862 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10863 return VD->isThisDeclarationADefinition() ||
10864 VD->isThisDeclarationADemotedDefinition();
10865
10866 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10867 return TD->isThisDeclarationADefinition() ||
10868 TD->isThisDeclarationADemotedDefinition();
10869
10870 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10871 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10872
10873 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10874 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10875
10876 // Conservatively return false here.
10877 return false;
10878 };
10879
10880 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10881 return hasDefinitionImpl(D, hasDefinitionImpl);
10882 };
10883
10884 // It is not good to prevent multiple declarations since the forward
10885 // declaration is common. Let's try to avoid duplicated definitions
10886 // only.
10887 if (!hasDefinition(D) || !hasDefinition(Previous))
10888 continue;
10889
10890 Module *PM = Previous->getOwningModule();
10891 Module *DM = D->getOwningModule();
10892 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10893 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10894 << (DM ? DM->getTopLevelModuleName() : "global module");
10895 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10896 }
10897 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10898
10899 // Load the bodies of any functions or methods we've encountered. We do
10900 // this now (delayed) so that we can be sure that the declaration chains
10901 // have been fully wired up (hasBody relies on this).
10902 // FIXME: We shouldn't require complete redeclaration chains here.
10903 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10904 PBEnd = PendingBodies.end();
10905 PB != PBEnd; ++PB) {
10906 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10907 // FIXME: Check for =delete/=default?
10908 const FunctionDecl *Defn = nullptr;
10909 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10910 FD->setLazyBody(PB->second);
10911 } else {
10912 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10913 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10914
10915 if (!FD->isLateTemplateParsed() &&
10916 !NonConstDefn->isLateTemplateParsed() &&
10917 // We only perform ODR checks for decls not in the explicit
10918 // global module fragment.
10919 !shouldSkipCheckingODR(D: FD) &&
10920 !shouldSkipCheckingODR(D: NonConstDefn) &&
10921 FD->getODRHash() != NonConstDefn->getODRHash()) {
10922 if (!isa<CXXMethodDecl>(Val: FD)) {
10923 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10924 } else if (FD->getLexicalParent()->isFileContext() &&
10925 NonConstDefn->getLexicalParent()->isFileContext()) {
10926 // Only diagnose out-of-line method definitions. If they are
10927 // in class definitions, then an error will be generated when
10928 // processing the class bodies.
10929 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10930 }
10931 }
10932 }
10933 continue;
10934 }
10935
10936 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10937 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10938 MD->setLazyBody(PB->second);
10939 }
10940 PendingBodies.clear();
10941
10942 // Inform any classes that had members added that they now have more members.
10943 for (auto [RD, MD] : PendingAddedClassMembers) {
10944 RD->addedMember(D: MD);
10945 }
10946 PendingAddedClassMembers.clear();
10947
10948 // Do some cleanup.
10949 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10950 getContext().deduplicateMergedDefinitionsFor(ND);
10951 PendingMergedDefinitionsToDeduplicate.clear();
10952
10953 // For each decl chain that we wanted to complete while deserializing, mark
10954 // it as "still needs to be completed".
10955 for (Decl *D : PendingIncompleteDeclChains)
10956 markIncompleteDeclChain(D);
10957 PendingIncompleteDeclChains.clear();
10958
10959 assert(PendingIdentifierInfos.empty() &&
10960 "Should be empty at the end of finishPendingActions");
10961 assert(PendingDeducedFunctionTypes.empty() &&
10962 "Should be empty at the end of finishPendingActions");
10963 assert(PendingDeducedVarTypes.empty() &&
10964 "Should be empty at the end of finishPendingActions");
10965 assert(PendingDeclChains.empty() &&
10966 "Should be empty at the end of finishPendingActions");
10967 assert(PendingMacroIDs.empty() &&
10968 "Should be empty at the end of finishPendingActions");
10969 assert(PendingDeclContextInfos.empty() &&
10970 "Should be empty at the end of finishPendingActions");
10971 assert(PendingUpdateRecords.empty() &&
10972 "Should be empty at the end of finishPendingActions");
10973 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10974 "Should be empty at the end of finishPendingActions");
10975 assert(PendingFakeDefinitionData.empty() &&
10976 "Should be empty at the end of finishPendingActions");
10977 assert(PendingDefinitions.empty() &&
10978 "Should be empty at the end of finishPendingActions");
10979 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10980 "Should be empty at the end of finishPendingActions");
10981 assert(PendingBodies.empty() &&
10982 "Should be empty at the end of finishPendingActions");
10983 assert(PendingAddedClassMembers.empty() &&
10984 "Should be empty at the end of finishPendingActions");
10985 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10986 "Should be empty at the end of finishPendingActions");
10987 assert(PendingIncompleteDeclChains.empty() &&
10988 "Should be empty at the end of finishPendingActions");
10989}
10990
10991void ASTReader::diagnoseOdrViolations() {
10992 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10993 PendingRecordOdrMergeFailures.empty() &&
10994 PendingFunctionOdrMergeFailures.empty() &&
10995 PendingEnumOdrMergeFailures.empty() &&
10996 PendingObjCInterfaceOdrMergeFailures.empty() &&
10997 PendingObjCProtocolOdrMergeFailures.empty())
10998 return;
10999
11000 // Trigger the import of the full definition of each class that had any
11001 // odr-merging problems, so we can produce better diagnostics for them.
11002 // These updates may in turn find and diagnose some ODR failures, so take
11003 // ownership of the set first.
11004 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
11005 PendingOdrMergeFailures.clear();
11006 for (auto &Merge : OdrMergeFailures) {
11007 Merge.first->buildLookup();
11008 Merge.first->decls_begin();
11009 Merge.first->bases_begin();
11010 Merge.first->vbases_begin();
11011 for (auto &RecordPair : Merge.second) {
11012 auto *RD = RecordPair.first;
11013 RD->decls_begin();
11014 RD->bases_begin();
11015 RD->vbases_begin();
11016 }
11017 }
11018
11019 // Trigger the import of the full definition of each record in C/ObjC.
11020 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11021 PendingRecordOdrMergeFailures.clear();
11022 for (auto &Merge : RecordOdrMergeFailures) {
11023 Merge.first->decls_begin();
11024 for (auto &D : Merge.second)
11025 D->decls_begin();
11026 }
11027
11028 // Trigger the import of the full interface definition.
11029 auto ObjCInterfaceOdrMergeFailures =
11030 std::move(PendingObjCInterfaceOdrMergeFailures);
11031 PendingObjCInterfaceOdrMergeFailures.clear();
11032 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11033 Merge.first->decls_begin();
11034 for (auto &InterfacePair : Merge.second)
11035 InterfacePair.first->decls_begin();
11036 }
11037
11038 // Trigger the import of functions.
11039 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11040 PendingFunctionOdrMergeFailures.clear();
11041 for (auto &Merge : FunctionOdrMergeFailures) {
11042 Merge.first->buildLookup();
11043 Merge.first->decls_begin();
11044 Merge.first->getBody();
11045 for (auto &FD : Merge.second) {
11046 FD->buildLookup();
11047 FD->decls_begin();
11048 FD->getBody();
11049 }
11050 }
11051
11052 // Trigger the import of enums.
11053 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11054 PendingEnumOdrMergeFailures.clear();
11055 for (auto &Merge : EnumOdrMergeFailures) {
11056 Merge.first->decls_begin();
11057 for (auto &Enum : Merge.second) {
11058 Enum->decls_begin();
11059 }
11060 }
11061
11062 // Trigger the import of the full protocol definition.
11063 auto ObjCProtocolOdrMergeFailures =
11064 std::move(PendingObjCProtocolOdrMergeFailures);
11065 PendingObjCProtocolOdrMergeFailures.clear();
11066 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11067 Merge.first->decls_begin();
11068 for (auto &ProtocolPair : Merge.second)
11069 ProtocolPair.first->decls_begin();
11070 }
11071
11072 // For each declaration from a merged context, check that the canonical
11073 // definition of that context also contains a declaration of the same
11074 // entity.
11075 //
11076 // Caution: this loop does things that might invalidate iterators into
11077 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11078 while (!PendingOdrMergeChecks.empty()) {
11079 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11080
11081 // FIXME: Skip over implicit declarations for now. This matters for things
11082 // like implicitly-declared special member functions. This isn't entirely
11083 // correct; we can end up with multiple unmerged declarations of the same
11084 // implicit entity.
11085 if (D->isImplicit())
11086 continue;
11087
11088 DeclContext *CanonDef = D->getDeclContext();
11089
11090 bool Found = false;
11091 const Decl *DCanon = D->getCanonicalDecl();
11092
11093 for (auto *RI : D->redecls()) {
11094 if (RI->getLexicalDeclContext() == CanonDef) {
11095 Found = true;
11096 break;
11097 }
11098 }
11099 if (Found)
11100 continue;
11101
11102 // Quick check failed, time to do the slow thing. Note, we can't just
11103 // look up the name of D in CanonDef here, because the member that is
11104 // in CanonDef might not be found by name lookup (it might have been
11105 // replaced by a more recent declaration in the lookup table), and we
11106 // can't necessarily find it in the redeclaration chain because it might
11107 // be merely mergeable, not redeclarable.
11108 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11109 for (auto *CanonMember : CanonDef->decls()) {
11110 if (CanonMember->getCanonicalDecl() == DCanon) {
11111 // This can happen if the declaration is merely mergeable and not
11112 // actually redeclarable (we looked for redeclarations earlier).
11113 //
11114 // FIXME: We should be able to detect this more efficiently, without
11115 // pulling in all of the members of CanonDef.
11116 Found = true;
11117 break;
11118 }
11119 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11120 if (ND->getDeclName() == D->getDeclName())
11121 Candidates.push_back(Elt: ND);
11122 }
11123
11124 if (!Found) {
11125 // The AST doesn't like TagDecls becoming invalid after they've been
11126 // completed. We only really need to mark FieldDecls as invalid here.
11127 if (!isa<TagDecl>(Val: D))
11128 D->setInvalidDecl();
11129
11130 // Ensure we don't accidentally recursively enter deserialization while
11131 // we're producing our diagnostic.
11132 Deserializing RecursionGuard(this);
11133
11134 std::string CanonDefModule =
11135 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11136 D: cast<Decl>(Val: CanonDef));
11137 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11138 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11139 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11140
11141 if (Candidates.empty())
11142 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11143 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11144 else {
11145 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11146 Diag(Loc: Candidates[I]->getLocation(),
11147 DiagID: diag::note_module_odr_violation_possible_decl)
11148 << Candidates[I];
11149 }
11150
11151 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11152 }
11153 }
11154
11155 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11156 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11157 ObjCInterfaceOdrMergeFailures.empty() &&
11158 ObjCProtocolOdrMergeFailures.empty())
11159 return;
11160
11161 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11162 getPreprocessor().getLangOpts());
11163
11164 // Issue any pending ODR-failure diagnostics.
11165 for (auto &Merge : OdrMergeFailures) {
11166 // If we've already pointed out a specific problem with this class, don't
11167 // bother issuing a general "something's different" diagnostic.
11168 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11169 continue;
11170
11171 bool Diagnosed = false;
11172 CXXRecordDecl *FirstRecord = Merge.first;
11173 for (auto &RecordPair : Merge.second) {
11174 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11175 SecondDD: RecordPair.second)) {
11176 Diagnosed = true;
11177 break;
11178 }
11179 }
11180
11181 if (!Diagnosed) {
11182 // All definitions are updates to the same declaration. This happens if a
11183 // module instantiates the declaration of a class template specialization
11184 // and two or more other modules instantiate its definition.
11185 //
11186 // FIXME: Indicate which modules had instantiations of this definition.
11187 // FIXME: How can this even happen?
11188 Diag(Loc: Merge.first->getLocation(),
11189 DiagID: diag::err_module_odr_violation_different_instantiations)
11190 << Merge.first;
11191 }
11192 }
11193
11194 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11195 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11196 for (auto &Merge : RecordOdrMergeFailures) {
11197 // If we've already pointed out a specific problem with this class, don't
11198 // bother issuing a general "something's different" diagnostic.
11199 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11200 continue;
11201
11202 RecordDecl *FirstRecord = Merge.first;
11203 bool Diagnosed = false;
11204 for (auto *SecondRecord : Merge.second) {
11205 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11206 Diagnosed = true;
11207 break;
11208 }
11209 }
11210 (void)Diagnosed;
11211 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11212 }
11213
11214 // Issue ODR failures diagnostics for functions.
11215 for (auto &Merge : FunctionOdrMergeFailures) {
11216 FunctionDecl *FirstFunction = Merge.first;
11217 bool Diagnosed = false;
11218 for (auto &SecondFunction : Merge.second) {
11219 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11220 Diagnosed = true;
11221 break;
11222 }
11223 }
11224 (void)Diagnosed;
11225 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11226 }
11227
11228 // Issue ODR failures diagnostics for enums.
11229 for (auto &Merge : EnumOdrMergeFailures) {
11230 // If we've already pointed out a specific problem with this enum, don't
11231 // bother issuing a general "something's different" diagnostic.
11232 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11233 continue;
11234
11235 EnumDecl *FirstEnum = Merge.first;
11236 bool Diagnosed = false;
11237 for (auto &SecondEnum : Merge.second) {
11238 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11239 Diagnosed = true;
11240 break;
11241 }
11242 }
11243 (void)Diagnosed;
11244 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11245 }
11246
11247 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11248 // If we've already pointed out a specific problem with this interface,
11249 // don't bother issuing a general "something's different" diagnostic.
11250 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11251 continue;
11252
11253 bool Diagnosed = false;
11254 ObjCInterfaceDecl *FirstID = Merge.first;
11255 for (auto &InterfacePair : Merge.second) {
11256 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11257 SecondDD: InterfacePair.second)) {
11258 Diagnosed = true;
11259 break;
11260 }
11261 }
11262 (void)Diagnosed;
11263 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11264 }
11265
11266 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11267 // If we've already pointed out a specific problem with this protocol,
11268 // don't bother issuing a general "something's different" diagnostic.
11269 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11270 continue;
11271
11272 ObjCProtocolDecl *FirstProtocol = Merge.first;
11273 bool Diagnosed = false;
11274 for (auto &ProtocolPair : Merge.second) {
11275 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11276 SecondDD: ProtocolPair.second)) {
11277 Diagnosed = true;
11278 break;
11279 }
11280 }
11281 (void)Diagnosed;
11282 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11283 }
11284}
11285
11286void ASTReader::StartedDeserializing() {
11287 if (llvm::Timer *T = ReadTimer.get();
11288 ++NumCurrentElementsDeserializing == 1 && T)
11289 ReadTimeRegion.emplace(args&: T);
11290}
11291
11292void ASTReader::FinishedDeserializing() {
11293 assert(NumCurrentElementsDeserializing &&
11294 "FinishedDeserializing not paired with StartedDeserializing");
11295 if (NumCurrentElementsDeserializing == 1) {
11296 // We decrease NumCurrentElementsDeserializing only after pending actions
11297 // are finished, to avoid recursively re-calling finishPendingActions().
11298 finishPendingActions();
11299 }
11300 --NumCurrentElementsDeserializing;
11301
11302 if (NumCurrentElementsDeserializing == 0) {
11303 {
11304 // Guard variable to avoid recursively entering the process of passing
11305 // decls to consumer.
11306 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11307 /*NewValue=*/false);
11308
11309 // Propagate exception specification and deduced type updates along
11310 // redeclaration chains.
11311 //
11312 // We do this now rather than in finishPendingActions because we want to
11313 // be able to walk the complete redeclaration chains of the updated decls.
11314 while (!PendingExceptionSpecUpdates.empty() ||
11315 !PendingDeducedTypeUpdates.empty() ||
11316 !PendingUndeducedFunctionDecls.empty()) {
11317 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11318 PendingExceptionSpecUpdates.clear();
11319 for (auto Update : ESUpdates) {
11320 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11321 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11322 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11323 if (auto *Listener = getContext().getASTMutationListener())
11324 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11325 for (auto *Redecl : Update.second->redecls())
11326 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11327 }
11328
11329 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11330 PendingDeducedTypeUpdates.clear();
11331 for (auto Update : DTUpdates) {
11332 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11333 // FIXME: If the return type is already deduced, check that it
11334 // matches.
11335 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11336 ResultType: Update.second);
11337 }
11338
11339 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11340 PendingUndeducedFunctionDecls.clear();
11341 // We hope we can find the deduced type for the functions by iterating
11342 // redeclarations in other modules.
11343 for (FunctionDecl *UndeducedFD : UDTUpdates)
11344 (void)UndeducedFD->getMostRecentDecl();
11345 }
11346
11347 ReadTimeRegion.reset();
11348
11349 diagnoseOdrViolations();
11350 }
11351
11352 // We are not in recursive loading, so it's safe to pass the "interesting"
11353 // decls to the consumer.
11354 if (Consumer)
11355 PassInterestingDeclsToConsumer();
11356 }
11357}
11358
11359void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11360 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11361 // Remove any fake results before adding any real ones.
11362 auto It = PendingFakeLookupResults.find(Key: II);
11363 if (It != PendingFakeLookupResults.end()) {
11364 for (auto *ND : It->second)
11365 SemaObj->IdResolver.RemoveDecl(D: ND);
11366 // FIXME: this works around module+PCH performance issue.
11367 // Rather than erase the result from the map, which is O(n), just clear
11368 // the vector of NamedDecls.
11369 It->second.clear();
11370 }
11371 }
11372
11373 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11374 SemaObj->TUScope->AddDecl(D);
11375 } else if (SemaObj->TUScope) {
11376 // Adding the decl to IdResolver may have failed because it was already in
11377 // (even though it was not added in scope). If it is already in, make sure
11378 // it gets in the scope as well.
11379 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11380 SemaObj->TUScope->AddDecl(D);
11381 }
11382}
11383
11384ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11385 ASTContext *Context,
11386 const PCHContainerReader &PCHContainerRdr,
11387 const CodeGenOptions &CodeGenOpts,
11388 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11389 StringRef isysroot,
11390 DisableValidationForModuleKind DisableValidationKind,
11391 bool AllowASTWithCompilerErrors,
11392 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11393 bool ForceValidateUserInputs,
11394 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11395 std::unique_ptr<llvm::Timer> ReadTimer)
11396 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11397 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11398 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11399 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11400 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11401 StackHandler(Diags), PP(PP), ContextObj(Context),
11402 CodeGenOpts(CodeGenOpts),
11403 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11404 PP.getHeaderSearchInfo()),
11405 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11406 DisableValidationKind(DisableValidationKind),
11407 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11408 AllowConfigurationMismatch(AllowConfigurationMismatch),
11409 ValidateSystemInputs(ValidateSystemInputs),
11410 ForceValidateUserInputs(ForceValidateUserInputs),
11411 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11412 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11413 SourceMgr.setExternalSLocEntrySource(this);
11414
11415 PathBuf.reserve(N: 256);
11416
11417 for (const auto &Ext : Extensions) {
11418 auto BlockName = Ext->getExtensionMetadata().BlockName;
11419 auto Known = ModuleFileExtensions.find(Key: BlockName);
11420 if (Known != ModuleFileExtensions.end()) {
11421 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11422 << BlockName;
11423 continue;
11424 }
11425
11426 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11427 }
11428}
11429
11430ASTReader::~ASTReader() {
11431 if (OwnsDeserializationListener)
11432 delete DeserializationListener;
11433}
11434
11435IdentifierResolver &ASTReader::getIdResolver() {
11436 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11437}
11438
11439Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11440 unsigned AbbrevID) {
11441 Idx = 0;
11442 Record.clear();
11443 return Cursor.readRecord(AbbrevID, Vals&: Record);
11444}
11445//===----------------------------------------------------------------------===//
11446//// OMPClauseReader implementation
11447////===----------------------------------------------------------------------===//
11448
11449// This has to be in namespace clang because it's friended by all
11450// of the OMP clauses.
11451namespace clang {
11452
11453class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11454 ASTRecordReader &Record;
11455 ASTContext &Context;
11456
11457public:
11458 OMPClauseReader(ASTRecordReader &Record)
11459 : Record(Record), Context(Record.getContext()) {}
11460#define GEN_CLANG_CLAUSE_CLASS
11461#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11462#include "llvm/Frontend/OpenMP/OMP.inc"
11463 OMPClause *readClause();
11464 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11465 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11466};
11467
11468} // end namespace clang
11469
11470OMPClause *ASTRecordReader::readOMPClause() {
11471 return OMPClauseReader(*this).readClause();
11472}
11473
11474OMPClause *OMPClauseReader::readClause() {
11475 OMPClause *C = nullptr;
11476 switch (llvm::omp::Clause(Record.readInt())) {
11477 case llvm::omp::OMPC_if:
11478 C = new (Context) OMPIfClause();
11479 break;
11480 case llvm::omp::OMPC_final:
11481 C = new (Context) OMPFinalClause();
11482 break;
11483 case llvm::omp::OMPC_num_threads:
11484 C = new (Context) OMPNumThreadsClause();
11485 break;
11486 case llvm::omp::OMPC_safelen:
11487 C = new (Context) OMPSafelenClause();
11488 break;
11489 case llvm::omp::OMPC_simdlen:
11490 C = new (Context) OMPSimdlenClause();
11491 break;
11492 case llvm::omp::OMPC_sizes: {
11493 unsigned NumSizes = Record.readInt();
11494 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11495 break;
11496 }
11497 case llvm::omp::OMPC_counts: {
11498 unsigned NumCounts = Record.readInt();
11499 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11500 break;
11501 }
11502 case llvm::omp::OMPC_permutation: {
11503 unsigned NumLoops = Record.readInt();
11504 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11505 break;
11506 }
11507 case llvm::omp::OMPC_full:
11508 C = OMPFullClause::CreateEmpty(C: Context);
11509 break;
11510 case llvm::omp::OMPC_partial:
11511 C = OMPPartialClause::CreateEmpty(C: Context);
11512 break;
11513 case llvm::omp::OMPC_looprange:
11514 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11515 break;
11516 case llvm::omp::OMPC_allocator:
11517 C = new (Context) OMPAllocatorClause();
11518 break;
11519 case llvm::omp::OMPC_collapse:
11520 C = new (Context) OMPCollapseClause();
11521 break;
11522 case llvm::omp::OMPC_default:
11523 C = new (Context) OMPDefaultClause();
11524 break;
11525 case llvm::omp::OMPC_proc_bind:
11526 C = new (Context) OMPProcBindClause();
11527 break;
11528 case llvm::omp::OMPC_schedule:
11529 C = new (Context) OMPScheduleClause();
11530 break;
11531 case llvm::omp::OMPC_ordered:
11532 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11533 break;
11534 case llvm::omp::OMPC_nowait:
11535 C = new (Context) OMPNowaitClause();
11536 break;
11537 case llvm::omp::OMPC_untied:
11538 C = new (Context) OMPUntiedClause();
11539 break;
11540 case llvm::omp::OMPC_mergeable:
11541 C = new (Context) OMPMergeableClause();
11542 break;
11543 case llvm::omp::OMPC_threadset:
11544 C = new (Context) OMPThreadsetClause();
11545 break;
11546 case llvm::omp::OMPC_transparent:
11547 C = new (Context) OMPTransparentClause();
11548 break;
11549 case llvm::omp::OMPC_read:
11550 C = new (Context) OMPReadClause();
11551 break;
11552 case llvm::omp::OMPC_write:
11553 C = new (Context) OMPWriteClause();
11554 break;
11555 case llvm::omp::OMPC_update:
11556 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11557 break;
11558 case llvm::omp::OMPC_capture:
11559 C = new (Context) OMPCaptureClause();
11560 break;
11561 case llvm::omp::OMPC_compare:
11562 C = new (Context) OMPCompareClause();
11563 break;
11564 case llvm::omp::OMPC_fail:
11565 C = new (Context) OMPFailClause();
11566 break;
11567 case llvm::omp::OMPC_seq_cst:
11568 C = new (Context) OMPSeqCstClause();
11569 break;
11570 case llvm::omp::OMPC_acq_rel:
11571 C = new (Context) OMPAcqRelClause();
11572 break;
11573 case llvm::omp::OMPC_absent: {
11574 unsigned NumKinds = Record.readInt();
11575 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11576 break;
11577 }
11578 case llvm::omp::OMPC_holds:
11579 C = new (Context) OMPHoldsClause();
11580 break;
11581 case llvm::omp::OMPC_contains: {
11582 unsigned NumKinds = Record.readInt();
11583 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11584 break;
11585 }
11586 case llvm::omp::OMPC_no_openmp:
11587 C = new (Context) OMPNoOpenMPClause();
11588 break;
11589 case llvm::omp::OMPC_no_openmp_routines:
11590 C = new (Context) OMPNoOpenMPRoutinesClause();
11591 break;
11592 case llvm::omp::OMPC_no_openmp_constructs:
11593 C = new (Context) OMPNoOpenMPConstructsClause();
11594 break;
11595 case llvm::omp::OMPC_no_parallelism:
11596 C = new (Context) OMPNoParallelismClause();
11597 break;
11598 case llvm::omp::OMPC_acquire:
11599 C = new (Context) OMPAcquireClause();
11600 break;
11601 case llvm::omp::OMPC_release:
11602 C = new (Context) OMPReleaseClause();
11603 break;
11604 case llvm::omp::OMPC_relaxed:
11605 C = new (Context) OMPRelaxedClause();
11606 break;
11607 case llvm::omp::OMPC_weak:
11608 C = new (Context) OMPWeakClause();
11609 break;
11610 case llvm::omp::OMPC_threads:
11611 C = new (Context) OMPThreadsClause();
11612 break;
11613 case llvm::omp::OMPC_simd:
11614 C = new (Context) OMPSIMDClause();
11615 break;
11616 case llvm::omp::OMPC_nogroup:
11617 C = new (Context) OMPNogroupClause();
11618 break;
11619 case llvm::omp::OMPC_unified_address:
11620 C = new (Context) OMPUnifiedAddressClause();
11621 break;
11622 case llvm::omp::OMPC_unified_shared_memory:
11623 C = new (Context) OMPUnifiedSharedMemoryClause();
11624 break;
11625 case llvm::omp::OMPC_reverse_offload:
11626 C = new (Context) OMPReverseOffloadClause();
11627 break;
11628 case llvm::omp::OMPC_dynamic_allocators:
11629 C = new (Context) OMPDynamicAllocatorsClause();
11630 break;
11631 case llvm::omp::OMPC_atomic_default_mem_order:
11632 C = new (Context) OMPAtomicDefaultMemOrderClause();
11633 break;
11634 case llvm::omp::OMPC_self_maps:
11635 C = new (Context) OMPSelfMapsClause();
11636 break;
11637 case llvm::omp::OMPC_at:
11638 C = new (Context) OMPAtClause();
11639 break;
11640 case llvm::omp::OMPC_severity:
11641 C = new (Context) OMPSeverityClause();
11642 break;
11643 case llvm::omp::OMPC_message:
11644 C = new (Context) OMPMessageClause();
11645 break;
11646 case llvm::omp::OMPC_private:
11647 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11648 break;
11649 case llvm::omp::OMPC_firstprivate:
11650 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11651 break;
11652 case llvm::omp::OMPC_lastprivate:
11653 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11654 break;
11655 case llvm::omp::OMPC_shared:
11656 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11657 break;
11658 case llvm::omp::OMPC_reduction: {
11659 unsigned N = Record.readInt();
11660 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11661 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11662 break;
11663 }
11664 case llvm::omp::OMPC_task_reduction:
11665 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11666 break;
11667 case llvm::omp::OMPC_in_reduction:
11668 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11669 break;
11670 case llvm::omp::OMPC_linear:
11671 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11672 break;
11673 case llvm::omp::OMPC_aligned:
11674 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11675 break;
11676 case llvm::omp::OMPC_copyin:
11677 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11678 break;
11679 case llvm::omp::OMPC_copyprivate:
11680 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11681 break;
11682 case llvm::omp::OMPC_flush:
11683 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11684 break;
11685 case llvm::omp::OMPC_depobj:
11686 C = OMPDepobjClause::CreateEmpty(C: Context);
11687 break;
11688 case llvm::omp::OMPC_depend: {
11689 unsigned NumVars = Record.readInt();
11690 unsigned NumLoops = Record.readInt();
11691 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11692 break;
11693 }
11694 case llvm::omp::OMPC_device:
11695 C = new (Context) OMPDeviceClause();
11696 break;
11697 case llvm::omp::OMPC_map: {
11698 OMPMappableExprListSizeTy Sizes;
11699 Sizes.NumVars = Record.readInt();
11700 Sizes.NumUniqueDeclarations = Record.readInt();
11701 Sizes.NumComponentLists = Record.readInt();
11702 Sizes.NumComponents = Record.readInt();
11703 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11704 break;
11705 }
11706 case llvm::omp::OMPC_num_teams:
11707 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11708 break;
11709 case llvm::omp::OMPC_thread_limit:
11710 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11711 break;
11712 case llvm::omp::OMPC_priority:
11713 C = new (Context) OMPPriorityClause();
11714 break;
11715 case llvm::omp::OMPC_grainsize:
11716 C = new (Context) OMPGrainsizeClause();
11717 break;
11718 case llvm::omp::OMPC_num_tasks:
11719 C = new (Context) OMPNumTasksClause();
11720 break;
11721 case llvm::omp::OMPC_hint:
11722 C = new (Context) OMPHintClause();
11723 break;
11724 case llvm::omp::OMPC_dist_schedule:
11725 C = new (Context) OMPDistScheduleClause();
11726 break;
11727 case llvm::omp::OMPC_defaultmap:
11728 C = new (Context) OMPDefaultmapClause();
11729 break;
11730 case llvm::omp::OMPC_to: {
11731 OMPMappableExprListSizeTy Sizes;
11732 Sizes.NumVars = Record.readInt();
11733 Sizes.NumUniqueDeclarations = Record.readInt();
11734 Sizes.NumComponentLists = Record.readInt();
11735 Sizes.NumComponents = Record.readInt();
11736 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11737 break;
11738 }
11739 case llvm::omp::OMPC_from: {
11740 OMPMappableExprListSizeTy Sizes;
11741 Sizes.NumVars = Record.readInt();
11742 Sizes.NumUniqueDeclarations = Record.readInt();
11743 Sizes.NumComponentLists = Record.readInt();
11744 Sizes.NumComponents = Record.readInt();
11745 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11746 break;
11747 }
11748 case llvm::omp::OMPC_use_device_ptr: {
11749 OMPMappableExprListSizeTy Sizes;
11750 Sizes.NumVars = Record.readInt();
11751 Sizes.NumUniqueDeclarations = Record.readInt();
11752 Sizes.NumComponentLists = Record.readInt();
11753 Sizes.NumComponents = Record.readInt();
11754 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11755 break;
11756 }
11757 case llvm::omp::OMPC_use_device_addr: {
11758 OMPMappableExprListSizeTy Sizes;
11759 Sizes.NumVars = Record.readInt();
11760 Sizes.NumUniqueDeclarations = Record.readInt();
11761 Sizes.NumComponentLists = Record.readInt();
11762 Sizes.NumComponents = Record.readInt();
11763 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11764 break;
11765 }
11766 case llvm::omp::OMPC_is_device_ptr: {
11767 OMPMappableExprListSizeTy Sizes;
11768 Sizes.NumVars = Record.readInt();
11769 Sizes.NumUniqueDeclarations = Record.readInt();
11770 Sizes.NumComponentLists = Record.readInt();
11771 Sizes.NumComponents = Record.readInt();
11772 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11773 break;
11774 }
11775 case llvm::omp::OMPC_has_device_addr: {
11776 OMPMappableExprListSizeTy Sizes;
11777 Sizes.NumVars = Record.readInt();
11778 Sizes.NumUniqueDeclarations = Record.readInt();
11779 Sizes.NumComponentLists = Record.readInt();
11780 Sizes.NumComponents = Record.readInt();
11781 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11782 break;
11783 }
11784 case llvm::omp::OMPC_allocate:
11785 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11786 break;
11787 case llvm::omp::OMPC_nontemporal:
11788 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11789 break;
11790 case llvm::omp::OMPC_inclusive:
11791 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11792 break;
11793 case llvm::omp::OMPC_exclusive:
11794 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11795 break;
11796 case llvm::omp::OMPC_order:
11797 C = new (Context) OMPOrderClause();
11798 break;
11799 case llvm::omp::OMPC_init: {
11800 unsigned VarListSize = Record.readInt();
11801 unsigned NumAttrs = Record.readInt();
11802 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11803 NumAttrs);
11804 break;
11805 }
11806 case llvm::omp::OMPC_use:
11807 C = new (Context) OMPUseClause();
11808 break;
11809 case llvm::omp::OMPC_destroy:
11810 C = new (Context) OMPDestroyClause();
11811 break;
11812 case llvm::omp::OMPC_novariants:
11813 C = new (Context) OMPNovariantsClause();
11814 break;
11815 case llvm::omp::OMPC_nocontext:
11816 C = new (Context) OMPNocontextClause();
11817 break;
11818 case llvm::omp::OMPC_detach:
11819 C = new (Context) OMPDetachClause();
11820 break;
11821 case llvm::omp::OMPC_uses_allocators:
11822 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11823 break;
11824 case llvm::omp::OMPC_affinity:
11825 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11826 break;
11827 case llvm::omp::OMPC_filter:
11828 C = new (Context) OMPFilterClause();
11829 break;
11830 case llvm::omp::OMPC_bind:
11831 C = OMPBindClause::CreateEmpty(C: Context);
11832 break;
11833 case llvm::omp::OMPC_align:
11834 C = new (Context) OMPAlignClause();
11835 break;
11836 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11837 C = new (Context) OMPXDynCGroupMemClause();
11838 break;
11839 case llvm::omp::OMPC_dyn_groupprivate:
11840 C = new (Context) OMPDynGroupprivateClause();
11841 break;
11842 case llvm::omp::OMPC_doacross: {
11843 unsigned NumVars = Record.readInt();
11844 unsigned NumLoops = Record.readInt();
11845 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11846 break;
11847 }
11848 case llvm::omp::OMPC_ompx_attribute:
11849 C = new (Context) OMPXAttributeClause();
11850 break;
11851 case llvm::omp::OMPC_ompx_bare:
11852 C = new (Context) OMPXBareClause();
11853 break;
11854#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11855 case llvm::omp::Enum: \
11856 break;
11857#include "llvm/Frontend/OpenMP/OMPKinds.def"
11858 default:
11859 break;
11860 }
11861 assert(C && "Unknown OMPClause type");
11862
11863 Visit(S: C);
11864 C->setLocStart(Record.readSourceLocation());
11865 C->setLocEnd(Record.readSourceLocation());
11866
11867 return C;
11868}
11869
11870void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11871 C->setPreInitStmt(S: Record.readSubStmt(),
11872 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11873}
11874
11875void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11876 VisitOMPClauseWithPreInit(C);
11877 C->setPostUpdateExpr(Record.readSubExpr());
11878}
11879
11880void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11881 VisitOMPClauseWithPreInit(C);
11882 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11883 C->setNameModifierLoc(Record.readSourceLocation());
11884 C->setColonLoc(Record.readSourceLocation());
11885 C->setCondition(Record.readSubExpr());
11886 C->setLParenLoc(Record.readSourceLocation());
11887}
11888
11889void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11890 VisitOMPClauseWithPreInit(C);
11891 C->setCondition(Record.readSubExpr());
11892 C->setLParenLoc(Record.readSourceLocation());
11893}
11894
11895void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11896 VisitOMPClauseWithPreInit(C);
11897 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11898 C->setNumThreads(Record.readSubExpr());
11899 C->setModifierLoc(Record.readSourceLocation());
11900 C->setLParenLoc(Record.readSourceLocation());
11901}
11902
11903void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11904 C->setSafelen(Record.readSubExpr());
11905 C->setLParenLoc(Record.readSourceLocation());
11906}
11907
11908void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11909 C->setSimdlen(Record.readSubExpr());
11910 C->setLParenLoc(Record.readSourceLocation());
11911}
11912
11913void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11914 for (Expr *&E : C->getSizesRefs())
11915 E = Record.readSubExpr();
11916 C->setLParenLoc(Record.readSourceLocation());
11917}
11918
11919void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
11920 bool HasFill = Record.readBool();
11921 if (HasFill)
11922 C->setOmpFillIndex(Record.readInt());
11923 C->setOmpFillLoc(Record.readSourceLocation());
11924 for (Expr *&E : C->getCountsRefs())
11925 E = Record.readSubExpr();
11926 C->setLParenLoc(Record.readSourceLocation());
11927}
11928
11929void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11930 for (Expr *&E : C->getArgsRefs())
11931 E = Record.readSubExpr();
11932 C->setLParenLoc(Record.readSourceLocation());
11933}
11934
11935void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11936
11937void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11938 C->setFactor(Record.readSubExpr());
11939 C->setLParenLoc(Record.readSourceLocation());
11940}
11941
11942void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11943 C->setFirst(Record.readSubExpr());
11944 C->setCount(Record.readSubExpr());
11945 C->setLParenLoc(Record.readSourceLocation());
11946 C->setFirstLoc(Record.readSourceLocation());
11947 C->setCountLoc(Record.readSourceLocation());
11948}
11949
11950void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11951 C->setAllocator(Record.readExpr());
11952 C->setLParenLoc(Record.readSourceLocation());
11953}
11954
11955void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11956 C->setNumForLoops(Record.readSubExpr());
11957 C->setLParenLoc(Record.readSourceLocation());
11958}
11959
11960void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11961 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11962 C->setLParenLoc(Record.readSourceLocation());
11963 C->setDefaultKindKwLoc(Record.readSourceLocation());
11964 C->setDefaultVariableCategory(
11965 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11966 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11967}
11968
11969// Read the parameter of threadset clause. This will have been saved when
11970// OMPClauseWriter is called.
11971void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11972 C->setLParenLoc(Record.readSourceLocation());
11973 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11974 C->setThreadsetKindLoc(ThreadsetKindLoc);
11975 OpenMPThreadsetKind TKind =
11976 static_cast<OpenMPThreadsetKind>(Record.readInt());
11977 C->setThreadsetKind(TKind);
11978}
11979
11980void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11981 C->setLParenLoc(Record.readSourceLocation());
11982 C->setImpexTypeKind(Record.readSubExpr());
11983}
11984
11985void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11986 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11987 C->setLParenLoc(Record.readSourceLocation());
11988 C->setProcBindKindKwLoc(Record.readSourceLocation());
11989}
11990
11991void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11992 VisitOMPClauseWithPreInit(C);
11993 C->setScheduleKind(
11994 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11995 C->setFirstScheduleModifier(
11996 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11997 C->setSecondScheduleModifier(
11998 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11999 C->setChunkSize(Record.readSubExpr());
12000 C->setLParenLoc(Record.readSourceLocation());
12001 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12002 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12003 C->setScheduleKindLoc(Record.readSourceLocation());
12004 C->setCommaLoc(Record.readSourceLocation());
12005}
12006
12007void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12008 C->setNumForLoops(Record.readSubExpr());
12009 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12010 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
12011 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12012 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
12013 C->setLParenLoc(Record.readSourceLocation());
12014}
12015
12016void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12017 C->setEventHandler(Record.readSubExpr());
12018 C->setLParenLoc(Record.readSourceLocation());
12019}
12020
12021void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12022 C->setCondition(Record.readSubExpr());
12023 C->setLParenLoc(Record.readSourceLocation());
12024}
12025
12026void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12027
12028void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12029
12030void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12031
12032void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12033
12034void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12035 if (C->isExtended()) {
12036 C->setLParenLoc(Record.readSourceLocation());
12037 C->setArgumentLoc(Record.readSourceLocation());
12038 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12039 }
12040}
12041
12042void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12043
12044void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12045
12046// Read the parameter of fail clause. This will have been saved when
12047// OMPClauseWriter is called.
12048void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12049 C->setLParenLoc(Record.readSourceLocation());
12050 SourceLocation FailParameterLoc = Record.readSourceLocation();
12051 C->setFailParameterLoc(FailParameterLoc);
12052 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12053 C->setFailParameter(CKind);
12054}
12055
12056void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12057 unsigned Count = C->getDirectiveKinds().size();
12058 C->setLParenLoc(Record.readSourceLocation());
12059 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12060 DKVec.reserve(N: Count);
12061 for (unsigned I = 0; I < Count; I++) {
12062 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12063 }
12064 C->setDirectiveKinds(DKVec);
12065}
12066
12067void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12068 C->setExpr(Record.readExpr());
12069 C->setLParenLoc(Record.readSourceLocation());
12070}
12071
12072void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12073 unsigned Count = C->getDirectiveKinds().size();
12074 C->setLParenLoc(Record.readSourceLocation());
12075 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12076 DKVec.reserve(N: Count);
12077 for (unsigned I = 0; I < Count; I++) {
12078 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12079 }
12080 C->setDirectiveKinds(DKVec);
12081}
12082
12083void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12084
12085void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12086 OMPNoOpenMPRoutinesClause *) {}
12087
12088void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12089 OMPNoOpenMPConstructsClause *) {}
12090
12091void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12092
12093void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12094
12095void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12096
12097void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12098
12099void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12100
12101void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12102
12103void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12104
12105void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12106
12107void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12108
12109void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12110
12111void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12112 unsigned NumVars = C->varlist_size();
12113 SmallVector<Expr *, 16> Vars;
12114 Vars.reserve(N: NumVars);
12115 for (unsigned I = 0; I != NumVars; ++I)
12116 Vars.push_back(Elt: Record.readSubExpr());
12117 C->setVarRefs(Vars);
12118 C->setIsTarget(Record.readBool());
12119 C->setIsTargetSync(Record.readBool());
12120 C->setHasPreferAttrs(Record.readBool());
12121
12122 unsigned NumPrefs = C->varlist_size() - 1;
12123 SmallVector<unsigned, 4> Counts;
12124 SmallVector<Expr *, 8> Attrs;
12125 Counts.reserve(N: NumPrefs);
12126 for (unsigned I = 0; I < NumPrefs; ++I) {
12127 unsigned NA = Record.readInt();
12128 Counts.push_back(Elt: NA);
12129 for (unsigned J = 0; J < NA; ++J)
12130 Attrs.push_back(Elt: Record.readSubExpr());
12131 }
12132 C->setAttrs(Counts, Attrs);
12133
12134 C->setLParenLoc(Record.readSourceLocation());
12135 C->setVarLoc(Record.readSourceLocation());
12136}
12137
12138void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12139 C->setInteropVar(Record.readSubExpr());
12140 C->setLParenLoc(Record.readSourceLocation());
12141 C->setVarLoc(Record.readSourceLocation());
12142}
12143
12144void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12145 C->setInteropVar(Record.readSubExpr());
12146 C->setLParenLoc(Record.readSourceLocation());
12147 C->setVarLoc(Record.readSourceLocation());
12148}
12149
12150void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12151 VisitOMPClauseWithPreInit(C);
12152 C->setCondition(Record.readSubExpr());
12153 C->setLParenLoc(Record.readSourceLocation());
12154}
12155
12156void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12157 VisitOMPClauseWithPreInit(C);
12158 C->setCondition(Record.readSubExpr());
12159 C->setLParenLoc(Record.readSourceLocation());
12160}
12161
12162void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12163
12164void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12165 OMPUnifiedSharedMemoryClause *) {}
12166
12167void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12168
12169void
12170OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12171}
12172
12173void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12174 OMPAtomicDefaultMemOrderClause *C) {
12175 C->setAtomicDefaultMemOrderKind(
12176 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12177 C->setLParenLoc(Record.readSourceLocation());
12178 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12179}
12180
12181void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12182
12183void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12184 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12185 C->setLParenLoc(Record.readSourceLocation());
12186 C->setAtKindKwLoc(Record.readSourceLocation());
12187}
12188
12189void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12190 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12191 C->setLParenLoc(Record.readSourceLocation());
12192 C->setSeverityKindKwLoc(Record.readSourceLocation());
12193}
12194
12195void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12196 VisitOMPClauseWithPreInit(C);
12197 C->setMessageString(Record.readSubExpr());
12198 C->setLParenLoc(Record.readSourceLocation());
12199}
12200
12201void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12202 C->setLParenLoc(Record.readSourceLocation());
12203 unsigned NumVars = C->varlist_size();
12204 SmallVector<Expr *, 16> Vars;
12205 Vars.reserve(N: NumVars);
12206 for (unsigned i = 0; i != NumVars; ++i)
12207 Vars.push_back(Elt: Record.readSubExpr());
12208 C->setVarRefs(Vars);
12209 Vars.clear();
12210 for (unsigned i = 0; i != NumVars; ++i)
12211 Vars.push_back(Elt: Record.readSubExpr());
12212 C->setPrivateCopies(Vars);
12213}
12214
12215void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12216 VisitOMPClauseWithPreInit(C);
12217 C->setLParenLoc(Record.readSourceLocation());
12218 unsigned NumVars = C->varlist_size();
12219 SmallVector<Expr *, 16> Vars;
12220 Vars.reserve(N: NumVars);
12221 for (unsigned i = 0; i != NumVars; ++i)
12222 Vars.push_back(Elt: Record.readSubExpr());
12223 C->setVarRefs(Vars);
12224 Vars.clear();
12225 for (unsigned i = 0; i != NumVars; ++i)
12226 Vars.push_back(Elt: Record.readSubExpr());
12227 C->setPrivateCopies(Vars);
12228 Vars.clear();
12229 for (unsigned i = 0; i != NumVars; ++i)
12230 Vars.push_back(Elt: Record.readSubExpr());
12231 C->setInits(Vars);
12232}
12233
12234void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12235 VisitOMPClauseWithPostUpdate(C);
12236 C->setLParenLoc(Record.readSourceLocation());
12237 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12238 C->setKindLoc(Record.readSourceLocation());
12239 C->setColonLoc(Record.readSourceLocation());
12240 unsigned NumVars = C->varlist_size();
12241 SmallVector<Expr *, 16> Vars;
12242 Vars.reserve(N: NumVars);
12243 for (unsigned i = 0; i != NumVars; ++i)
12244 Vars.push_back(Elt: Record.readSubExpr());
12245 C->setVarRefs(Vars);
12246 Vars.clear();
12247 for (unsigned i = 0; i != NumVars; ++i)
12248 Vars.push_back(Elt: Record.readSubExpr());
12249 C->setPrivateCopies(Vars);
12250 Vars.clear();
12251 for (unsigned i = 0; i != NumVars; ++i)
12252 Vars.push_back(Elt: Record.readSubExpr());
12253 C->setSourceExprs(Vars);
12254 Vars.clear();
12255 for (unsigned i = 0; i != NumVars; ++i)
12256 Vars.push_back(Elt: Record.readSubExpr());
12257 C->setDestinationExprs(Vars);
12258 Vars.clear();
12259 for (unsigned i = 0; i != NumVars; ++i)
12260 Vars.push_back(Elt: Record.readSubExpr());
12261 C->setAssignmentOps(Vars);
12262}
12263
12264void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12265 C->setLParenLoc(Record.readSourceLocation());
12266 unsigned NumVars = C->varlist_size();
12267 SmallVector<Expr *, 16> Vars;
12268 Vars.reserve(N: NumVars);
12269 for (unsigned i = 0; i != NumVars; ++i)
12270 Vars.push_back(Elt: Record.readSubExpr());
12271 C->setVarRefs(Vars);
12272}
12273
12274void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12275 VisitOMPClauseWithPostUpdate(C);
12276 C->setLParenLoc(Record.readSourceLocation());
12277 C->setModifierLoc(Record.readSourceLocation());
12278 C->setColonLoc(Record.readSourceLocation());
12279 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12280 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12281 C->setQualifierLoc(NNSL);
12282 C->setNameInfo(DNI);
12283
12284 unsigned NumVars = C->varlist_size();
12285 SmallVector<Expr *, 16> Vars;
12286 Vars.reserve(N: NumVars);
12287 for (unsigned i = 0; i != NumVars; ++i)
12288 Vars.push_back(Elt: Record.readSubExpr());
12289 C->setVarRefs(Vars);
12290 Vars.clear();
12291 for (unsigned i = 0; i != NumVars; ++i)
12292 Vars.push_back(Elt: Record.readSubExpr());
12293 C->setPrivates(Vars);
12294 Vars.clear();
12295 for (unsigned i = 0; i != NumVars; ++i)
12296 Vars.push_back(Elt: Record.readSubExpr());
12297 C->setLHSExprs(Vars);
12298 Vars.clear();
12299 for (unsigned i = 0; i != NumVars; ++i)
12300 Vars.push_back(Elt: Record.readSubExpr());
12301 C->setRHSExprs(Vars);
12302 Vars.clear();
12303 for (unsigned i = 0; i != NumVars; ++i)
12304 Vars.push_back(Elt: Record.readSubExpr());
12305 C->setReductionOps(Vars);
12306 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12307 Vars.clear();
12308 for (unsigned i = 0; i != NumVars; ++i)
12309 Vars.push_back(Elt: Record.readSubExpr());
12310 C->setInscanCopyOps(Vars);
12311 Vars.clear();
12312 for (unsigned i = 0; i != NumVars; ++i)
12313 Vars.push_back(Elt: Record.readSubExpr());
12314 C->setInscanCopyArrayTemps(Vars);
12315 Vars.clear();
12316 for (unsigned i = 0; i != NumVars; ++i)
12317 Vars.push_back(Elt: Record.readSubExpr());
12318 C->setInscanCopyArrayElems(Vars);
12319 }
12320 unsigned NumFlags = Record.readInt();
12321 SmallVector<bool, 16> Flags;
12322 Flags.reserve(N: NumFlags);
12323 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12324 Flags.push_back(Elt: Record.readInt());
12325 C->setPrivateVariableReductionFlags(Flags);
12326}
12327
12328void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12329 VisitOMPClauseWithPostUpdate(C);
12330 C->setLParenLoc(Record.readSourceLocation());
12331 C->setColonLoc(Record.readSourceLocation());
12332 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12333 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12334 C->setQualifierLoc(NNSL);
12335 C->setNameInfo(DNI);
12336
12337 unsigned NumVars = C->varlist_size();
12338 SmallVector<Expr *, 16> Vars;
12339 Vars.reserve(N: NumVars);
12340 for (unsigned I = 0; I != NumVars; ++I)
12341 Vars.push_back(Elt: Record.readSubExpr());
12342 C->setVarRefs(Vars);
12343 Vars.clear();
12344 for (unsigned I = 0; I != NumVars; ++I)
12345 Vars.push_back(Elt: Record.readSubExpr());
12346 C->setPrivates(Vars);
12347 Vars.clear();
12348 for (unsigned I = 0; I != NumVars; ++I)
12349 Vars.push_back(Elt: Record.readSubExpr());
12350 C->setLHSExprs(Vars);
12351 Vars.clear();
12352 for (unsigned I = 0; I != NumVars; ++I)
12353 Vars.push_back(Elt: Record.readSubExpr());
12354 C->setRHSExprs(Vars);
12355 Vars.clear();
12356 for (unsigned I = 0; I != NumVars; ++I)
12357 Vars.push_back(Elt: Record.readSubExpr());
12358 C->setReductionOps(Vars);
12359}
12360
12361void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12362 VisitOMPClauseWithPostUpdate(C);
12363 C->setLParenLoc(Record.readSourceLocation());
12364 C->setColonLoc(Record.readSourceLocation());
12365 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12366 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12367 C->setQualifierLoc(NNSL);
12368 C->setNameInfo(DNI);
12369
12370 unsigned NumVars = C->varlist_size();
12371 SmallVector<Expr *, 16> Vars;
12372 Vars.reserve(N: NumVars);
12373 for (unsigned I = 0; I != NumVars; ++I)
12374 Vars.push_back(Elt: Record.readSubExpr());
12375 C->setVarRefs(Vars);
12376 Vars.clear();
12377 for (unsigned I = 0; I != NumVars; ++I)
12378 Vars.push_back(Elt: Record.readSubExpr());
12379 C->setPrivates(Vars);
12380 Vars.clear();
12381 for (unsigned I = 0; I != NumVars; ++I)
12382 Vars.push_back(Elt: Record.readSubExpr());
12383 C->setLHSExprs(Vars);
12384 Vars.clear();
12385 for (unsigned I = 0; I != NumVars; ++I)
12386 Vars.push_back(Elt: Record.readSubExpr());
12387 C->setRHSExprs(Vars);
12388 Vars.clear();
12389 for (unsigned I = 0; I != NumVars; ++I)
12390 Vars.push_back(Elt: Record.readSubExpr());
12391 C->setReductionOps(Vars);
12392 Vars.clear();
12393 for (unsigned I = 0; I != NumVars; ++I)
12394 Vars.push_back(Elt: Record.readSubExpr());
12395 C->setTaskgroupDescriptors(Vars);
12396}
12397
12398void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12399 VisitOMPClauseWithPostUpdate(C);
12400 C->setLParenLoc(Record.readSourceLocation());
12401 C->setColonLoc(Record.readSourceLocation());
12402 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12403 C->setModifierLoc(Record.readSourceLocation());
12404 unsigned NumVars = C->varlist_size();
12405 SmallVector<Expr *, 16> Vars;
12406 Vars.reserve(N: NumVars);
12407 for (unsigned i = 0; i != NumVars; ++i)
12408 Vars.push_back(Elt: Record.readSubExpr());
12409 C->setVarRefs(Vars);
12410 Vars.clear();
12411 for (unsigned i = 0; i != NumVars; ++i)
12412 Vars.push_back(Elt: Record.readSubExpr());
12413 C->setPrivates(Vars);
12414 Vars.clear();
12415 for (unsigned i = 0; i != NumVars; ++i)
12416 Vars.push_back(Elt: Record.readSubExpr());
12417 C->setInits(Vars);
12418 Vars.clear();
12419 for (unsigned i = 0; i != NumVars; ++i)
12420 Vars.push_back(Elt: Record.readSubExpr());
12421 C->setUpdates(Vars);
12422 Vars.clear();
12423 for (unsigned i = 0; i != NumVars; ++i)
12424 Vars.push_back(Elt: Record.readSubExpr());
12425 C->setFinals(Vars);
12426 C->setStep(Record.readSubExpr());
12427 C->setCalcStep(Record.readSubExpr());
12428 Vars.clear();
12429 for (unsigned I = 0; I != NumVars + 1; ++I)
12430 Vars.push_back(Elt: Record.readSubExpr());
12431 C->setUsedExprs(Vars);
12432}
12433
12434void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12435 C->setLParenLoc(Record.readSourceLocation());
12436 C->setColonLoc(Record.readSourceLocation());
12437 unsigned NumVars = C->varlist_size();
12438 SmallVector<Expr *, 16> Vars;
12439 Vars.reserve(N: NumVars);
12440 for (unsigned i = 0; i != NumVars; ++i)
12441 Vars.push_back(Elt: Record.readSubExpr());
12442 C->setVarRefs(Vars);
12443 C->setAlignment(Record.readSubExpr());
12444}
12445
12446void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12447 C->setLParenLoc(Record.readSourceLocation());
12448 unsigned NumVars = C->varlist_size();
12449 SmallVector<Expr *, 16> Exprs;
12450 Exprs.reserve(N: NumVars);
12451 for (unsigned i = 0; i != NumVars; ++i)
12452 Exprs.push_back(Elt: Record.readSubExpr());
12453 C->setVarRefs(Exprs);
12454 Exprs.clear();
12455 for (unsigned i = 0; i != NumVars; ++i)
12456 Exprs.push_back(Elt: Record.readSubExpr());
12457 C->setSourceExprs(Exprs);
12458 Exprs.clear();
12459 for (unsigned i = 0; i != NumVars; ++i)
12460 Exprs.push_back(Elt: Record.readSubExpr());
12461 C->setDestinationExprs(Exprs);
12462 Exprs.clear();
12463 for (unsigned i = 0; i != NumVars; ++i)
12464 Exprs.push_back(Elt: Record.readSubExpr());
12465 C->setAssignmentOps(Exprs);
12466}
12467
12468void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12469 C->setLParenLoc(Record.readSourceLocation());
12470 unsigned NumVars = C->varlist_size();
12471 SmallVector<Expr *, 16> Exprs;
12472 Exprs.reserve(N: NumVars);
12473 for (unsigned i = 0; i != NumVars; ++i)
12474 Exprs.push_back(Elt: Record.readSubExpr());
12475 C->setVarRefs(Exprs);
12476 Exprs.clear();
12477 for (unsigned i = 0; i != NumVars; ++i)
12478 Exprs.push_back(Elt: Record.readSubExpr());
12479 C->setSourceExprs(Exprs);
12480 Exprs.clear();
12481 for (unsigned i = 0; i != NumVars; ++i)
12482 Exprs.push_back(Elt: Record.readSubExpr());
12483 C->setDestinationExprs(Exprs);
12484 Exprs.clear();
12485 for (unsigned i = 0; i != NumVars; ++i)
12486 Exprs.push_back(Elt: Record.readSubExpr());
12487 C->setAssignmentOps(Exprs);
12488}
12489
12490void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12491 C->setLParenLoc(Record.readSourceLocation());
12492 unsigned NumVars = C->varlist_size();
12493 SmallVector<Expr *, 16> Vars;
12494 Vars.reserve(N: NumVars);
12495 for (unsigned i = 0; i != NumVars; ++i)
12496 Vars.push_back(Elt: Record.readSubExpr());
12497 C->setVarRefs(Vars);
12498}
12499
12500void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12501 C->setDepobj(Record.readSubExpr());
12502 C->setLParenLoc(Record.readSourceLocation());
12503}
12504
12505void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12506 C->setLParenLoc(Record.readSourceLocation());
12507 C->setModifier(Record.readSubExpr());
12508 C->setDependencyKind(
12509 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12510 C->setDependencyLoc(Record.readSourceLocation());
12511 C->setColonLoc(Record.readSourceLocation());
12512 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12513 unsigned NumVars = C->varlist_size();
12514 SmallVector<Expr *, 16> Vars;
12515 Vars.reserve(N: NumVars);
12516 for (unsigned I = 0; I != NumVars; ++I)
12517 Vars.push_back(Elt: Record.readSubExpr());
12518 C->setVarRefs(Vars);
12519 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12520 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12521}
12522
12523void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12524 VisitOMPClauseWithPreInit(C);
12525 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12526 C->setDevice(Record.readSubExpr());
12527 C->setModifierLoc(Record.readSourceLocation());
12528 C->setLParenLoc(Record.readSourceLocation());
12529}
12530
12531void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12532 C->setLParenLoc(Record.readSourceLocation());
12533 bool HasIteratorModifier = false;
12534 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12535 C->setMapTypeModifier(
12536 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12537 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12538 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12539 HasIteratorModifier = true;
12540 }
12541 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12542 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12543 C->setMapType(
12544 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12545 C->setMapLoc(Record.readSourceLocation());
12546 C->setColonLoc(Record.readSourceLocation());
12547 auto NumVars = C->varlist_size();
12548 auto UniqueDecls = C->getUniqueDeclarationsNum();
12549 auto TotalLists = C->getTotalComponentListNum();
12550 auto TotalComponents = C->getTotalComponentsNum();
12551
12552 SmallVector<Expr *, 16> Vars;
12553 Vars.reserve(N: NumVars);
12554 for (unsigned i = 0; i != NumVars; ++i)
12555 Vars.push_back(Elt: Record.readExpr());
12556 C->setVarRefs(Vars);
12557
12558 SmallVector<Expr *, 16> UDMappers;
12559 UDMappers.reserve(N: NumVars);
12560 for (unsigned I = 0; I < NumVars; ++I)
12561 UDMappers.push_back(Elt: Record.readExpr());
12562 C->setUDMapperRefs(UDMappers);
12563
12564 if (HasIteratorModifier)
12565 C->setIteratorModifier(Record.readExpr());
12566
12567 SmallVector<ValueDecl *, 16> Decls;
12568 Decls.reserve(N: UniqueDecls);
12569 for (unsigned i = 0; i < UniqueDecls; ++i)
12570 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12571 C->setUniqueDecls(Decls);
12572
12573 SmallVector<unsigned, 16> ListsPerDecl;
12574 ListsPerDecl.reserve(N: UniqueDecls);
12575 for (unsigned i = 0; i < UniqueDecls; ++i)
12576 ListsPerDecl.push_back(Elt: Record.readInt());
12577 C->setDeclNumLists(ListsPerDecl);
12578
12579 SmallVector<unsigned, 32> ListSizes;
12580 ListSizes.reserve(N: TotalLists);
12581 for (unsigned i = 0; i < TotalLists; ++i)
12582 ListSizes.push_back(Elt: Record.readInt());
12583 C->setComponentListSizes(ListSizes);
12584
12585 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12586 Components.reserve(N: TotalComponents);
12587 for (unsigned i = 0; i < TotalComponents; ++i) {
12588 Expr *AssociatedExprPr = Record.readExpr();
12589 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12590 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12591 /*IsNonContiguous=*/Args: false);
12592 }
12593 C->setComponents(Components, CLSs: ListSizes);
12594}
12595
12596void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12597 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12598 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12599 C->setLParenLoc(Record.readSourceLocation());
12600 C->setColonLoc(Record.readSourceLocation());
12601 C->setAllocator(Record.readSubExpr());
12602 C->setAlignment(Record.readSubExpr());
12603 unsigned NumVars = C->varlist_size();
12604 SmallVector<Expr *, 16> Vars;
12605 Vars.reserve(N: NumVars);
12606 for (unsigned i = 0; i != NumVars; ++i)
12607 Vars.push_back(Elt: Record.readSubExpr());
12608 C->setVarRefs(Vars);
12609}
12610
12611void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12612 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12613 C->setModifierLoc(Record.readSourceLocation());
12614 C->setModifierExpr(Record.readSubExpr());
12615 VisitOMPClauseWithPreInit(C);
12616 C->setLParenLoc(Record.readSourceLocation());
12617 unsigned NumVars = C->varlist_size();
12618 SmallVector<Expr *, 16> Vars;
12619 Vars.reserve(N: NumVars);
12620 for (unsigned I = 0; I != NumVars; ++I)
12621 Vars.push_back(Elt: Record.readSubExpr());
12622 C->setVarRefs(Vars);
12623}
12624
12625void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12626 C->setModifier(Record.readEnum<OpenMPThreadLimitClauseModifier>());
12627 C->setModifierLoc(Record.readSourceLocation());
12628 C->setModifierExpr(Record.readSubExpr());
12629 VisitOMPClauseWithPreInit(C);
12630 C->setLParenLoc(Record.readSourceLocation());
12631 unsigned NumVars = C->varlist_size();
12632 SmallVector<Expr *, 16> Vars;
12633 Vars.reserve(N: NumVars);
12634 for (unsigned I = 0; I != NumVars; ++I)
12635 Vars.push_back(Elt: Record.readSubExpr());
12636 C->setVarRefs(Vars);
12637}
12638
12639void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12640 VisitOMPClauseWithPreInit(C);
12641 C->setPriority(Record.readSubExpr());
12642 C->setLParenLoc(Record.readSourceLocation());
12643}
12644
12645void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12646 VisitOMPClauseWithPreInit(C);
12647 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12648 C->setGrainsize(Record.readSubExpr());
12649 C->setModifierLoc(Record.readSourceLocation());
12650 C->setLParenLoc(Record.readSourceLocation());
12651}
12652
12653void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12654 VisitOMPClauseWithPreInit(C);
12655 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12656 C->setNumTasks(Record.readSubExpr());
12657 C->setModifierLoc(Record.readSourceLocation());
12658 C->setLParenLoc(Record.readSourceLocation());
12659}
12660
12661void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12662 C->setHint(Record.readSubExpr());
12663 C->setLParenLoc(Record.readSourceLocation());
12664}
12665
12666void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12667 VisitOMPClauseWithPreInit(C);
12668 C->setDistScheduleKind(
12669 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12670 C->setChunkSize(Record.readSubExpr());
12671 C->setLParenLoc(Record.readSourceLocation());
12672 C->setDistScheduleKindLoc(Record.readSourceLocation());
12673 C->setCommaLoc(Record.readSourceLocation());
12674}
12675
12676void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12677 C->setDefaultmapKind(
12678 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12679 C->setDefaultmapModifier(
12680 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12681 C->setLParenLoc(Record.readSourceLocation());
12682 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12683 C->setDefaultmapKindLoc(Record.readSourceLocation());
12684}
12685
12686void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12687 C->setLParenLoc(Record.readSourceLocation());
12688 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12689 C->setMotionModifier(
12690 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12691 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12692 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12693 C->setIteratorModifier(Record.readExpr());
12694 }
12695 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12696 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12697 C->setColonLoc(Record.readSourceLocation());
12698 auto NumVars = C->varlist_size();
12699 auto UniqueDecls = C->getUniqueDeclarationsNum();
12700 auto TotalLists = C->getTotalComponentListNum();
12701 auto TotalComponents = C->getTotalComponentsNum();
12702
12703 SmallVector<Expr *, 16> Vars;
12704 Vars.reserve(N: NumVars);
12705 for (unsigned i = 0; i != NumVars; ++i)
12706 Vars.push_back(Elt: Record.readSubExpr());
12707 C->setVarRefs(Vars);
12708
12709 SmallVector<Expr *, 16> UDMappers;
12710 UDMappers.reserve(N: NumVars);
12711 for (unsigned I = 0; I < NumVars; ++I)
12712 UDMappers.push_back(Elt: Record.readSubExpr());
12713 C->setUDMapperRefs(UDMappers);
12714
12715 SmallVector<ValueDecl *, 16> Decls;
12716 Decls.reserve(N: UniqueDecls);
12717 for (unsigned i = 0; i < UniqueDecls; ++i)
12718 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12719 C->setUniqueDecls(Decls);
12720
12721 SmallVector<unsigned, 16> ListsPerDecl;
12722 ListsPerDecl.reserve(N: UniqueDecls);
12723 for (unsigned i = 0; i < UniqueDecls; ++i)
12724 ListsPerDecl.push_back(Elt: Record.readInt());
12725 C->setDeclNumLists(ListsPerDecl);
12726
12727 SmallVector<unsigned, 32> ListSizes;
12728 ListSizes.reserve(N: TotalLists);
12729 for (unsigned i = 0; i < TotalLists; ++i)
12730 ListSizes.push_back(Elt: Record.readInt());
12731 C->setComponentListSizes(ListSizes);
12732
12733 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12734 Components.reserve(N: TotalComponents);
12735 for (unsigned i = 0; i < TotalComponents; ++i) {
12736 Expr *AssociatedExprPr = Record.readSubExpr();
12737 bool IsNonContiguous = Record.readBool();
12738 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12739 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12740 }
12741 C->setComponents(Components, CLSs: ListSizes);
12742}
12743
12744void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12745 C->setLParenLoc(Record.readSourceLocation());
12746 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12747 C->setMotionModifier(
12748 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12749 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12750 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12751 C->setIteratorModifier(Record.readExpr());
12752 }
12753 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12754 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12755 C->setColonLoc(Record.readSourceLocation());
12756 auto NumVars = C->varlist_size();
12757 auto UniqueDecls = C->getUniqueDeclarationsNum();
12758 auto TotalLists = C->getTotalComponentListNum();
12759 auto TotalComponents = C->getTotalComponentsNum();
12760
12761 SmallVector<Expr *, 16> Vars;
12762 Vars.reserve(N: NumVars);
12763 for (unsigned i = 0; i != NumVars; ++i)
12764 Vars.push_back(Elt: Record.readSubExpr());
12765 C->setVarRefs(Vars);
12766
12767 SmallVector<Expr *, 16> UDMappers;
12768 UDMappers.reserve(N: NumVars);
12769 for (unsigned I = 0; I < NumVars; ++I)
12770 UDMappers.push_back(Elt: Record.readSubExpr());
12771 C->setUDMapperRefs(UDMappers);
12772
12773 SmallVector<ValueDecl *, 16> Decls;
12774 Decls.reserve(N: UniqueDecls);
12775 for (unsigned i = 0; i < UniqueDecls; ++i)
12776 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12777 C->setUniqueDecls(Decls);
12778
12779 SmallVector<unsigned, 16> ListsPerDecl;
12780 ListsPerDecl.reserve(N: UniqueDecls);
12781 for (unsigned i = 0; i < UniqueDecls; ++i)
12782 ListsPerDecl.push_back(Elt: Record.readInt());
12783 C->setDeclNumLists(ListsPerDecl);
12784
12785 SmallVector<unsigned, 32> ListSizes;
12786 ListSizes.reserve(N: TotalLists);
12787 for (unsigned i = 0; i < TotalLists; ++i)
12788 ListSizes.push_back(Elt: Record.readInt());
12789 C->setComponentListSizes(ListSizes);
12790
12791 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12792 Components.reserve(N: TotalComponents);
12793 for (unsigned i = 0; i < TotalComponents; ++i) {
12794 Expr *AssociatedExprPr = Record.readSubExpr();
12795 bool IsNonContiguous = Record.readBool();
12796 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12797 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12798 }
12799 C->setComponents(Components, CLSs: ListSizes);
12800}
12801
12802void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12803 C->setLParenLoc(Record.readSourceLocation());
12804 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12805 C->setFallbackModifierLoc(Record.readSourceLocation());
12806 auto NumVars = C->varlist_size();
12807 auto UniqueDecls = C->getUniqueDeclarationsNum();
12808 auto TotalLists = C->getTotalComponentListNum();
12809 auto TotalComponents = C->getTotalComponentsNum();
12810
12811 SmallVector<Expr *, 16> Vars;
12812 Vars.reserve(N: NumVars);
12813 for (unsigned i = 0; i != NumVars; ++i)
12814 Vars.push_back(Elt: Record.readSubExpr());
12815 C->setVarRefs(Vars);
12816 Vars.clear();
12817 for (unsigned i = 0; i != NumVars; ++i)
12818 Vars.push_back(Elt: Record.readSubExpr());
12819 C->setPrivateCopies(Vars);
12820 Vars.clear();
12821 for (unsigned i = 0; i != NumVars; ++i)
12822 Vars.push_back(Elt: Record.readSubExpr());
12823 C->setInits(Vars);
12824
12825 SmallVector<ValueDecl *, 16> Decls;
12826 Decls.reserve(N: UniqueDecls);
12827 for (unsigned i = 0; i < UniqueDecls; ++i)
12828 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12829 C->setUniqueDecls(Decls);
12830
12831 SmallVector<unsigned, 16> ListsPerDecl;
12832 ListsPerDecl.reserve(N: UniqueDecls);
12833 for (unsigned i = 0; i < UniqueDecls; ++i)
12834 ListsPerDecl.push_back(Elt: Record.readInt());
12835 C->setDeclNumLists(ListsPerDecl);
12836
12837 SmallVector<unsigned, 32> ListSizes;
12838 ListSizes.reserve(N: TotalLists);
12839 for (unsigned i = 0; i < TotalLists; ++i)
12840 ListSizes.push_back(Elt: Record.readInt());
12841 C->setComponentListSizes(ListSizes);
12842
12843 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12844 Components.reserve(N: TotalComponents);
12845 for (unsigned i = 0; i < TotalComponents; ++i) {
12846 auto *AssociatedExprPr = Record.readSubExpr();
12847 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12848 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12849 /*IsNonContiguous=*/Args: false);
12850 }
12851 C->setComponents(Components, CLSs: ListSizes);
12852}
12853
12854void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12855 C->setLParenLoc(Record.readSourceLocation());
12856 auto NumVars = C->varlist_size();
12857 auto UniqueDecls = C->getUniqueDeclarationsNum();
12858 auto TotalLists = C->getTotalComponentListNum();
12859 auto TotalComponents = C->getTotalComponentsNum();
12860
12861 SmallVector<Expr *, 16> Vars;
12862 Vars.reserve(N: NumVars);
12863 for (unsigned i = 0; i != NumVars; ++i)
12864 Vars.push_back(Elt: Record.readSubExpr());
12865 C->setVarRefs(Vars);
12866
12867 SmallVector<ValueDecl *, 16> Decls;
12868 Decls.reserve(N: UniqueDecls);
12869 for (unsigned i = 0; i < UniqueDecls; ++i)
12870 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12871 C->setUniqueDecls(Decls);
12872
12873 SmallVector<unsigned, 16> ListsPerDecl;
12874 ListsPerDecl.reserve(N: UniqueDecls);
12875 for (unsigned i = 0; i < UniqueDecls; ++i)
12876 ListsPerDecl.push_back(Elt: Record.readInt());
12877 C->setDeclNumLists(ListsPerDecl);
12878
12879 SmallVector<unsigned, 32> ListSizes;
12880 ListSizes.reserve(N: TotalLists);
12881 for (unsigned i = 0; i < TotalLists; ++i)
12882 ListSizes.push_back(Elt: Record.readInt());
12883 C->setComponentListSizes(ListSizes);
12884
12885 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12886 Components.reserve(N: TotalComponents);
12887 for (unsigned i = 0; i < TotalComponents; ++i) {
12888 Expr *AssociatedExpr = Record.readSubExpr();
12889 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12890 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12891 /*IsNonContiguous*/ Args: false);
12892 }
12893 C->setComponents(Components, CLSs: ListSizes);
12894}
12895
12896void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12897 C->setLParenLoc(Record.readSourceLocation());
12898 auto NumVars = C->varlist_size();
12899 auto UniqueDecls = C->getUniqueDeclarationsNum();
12900 auto TotalLists = C->getTotalComponentListNum();
12901 auto TotalComponents = C->getTotalComponentsNum();
12902
12903 SmallVector<Expr *, 16> Vars;
12904 Vars.reserve(N: NumVars);
12905 for (unsigned i = 0; i != NumVars; ++i)
12906 Vars.push_back(Elt: Record.readSubExpr());
12907 C->setVarRefs(Vars);
12908 Vars.clear();
12909
12910 SmallVector<ValueDecl *, 16> Decls;
12911 Decls.reserve(N: UniqueDecls);
12912 for (unsigned i = 0; i < UniqueDecls; ++i)
12913 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12914 C->setUniqueDecls(Decls);
12915
12916 SmallVector<unsigned, 16> ListsPerDecl;
12917 ListsPerDecl.reserve(N: UniqueDecls);
12918 for (unsigned i = 0; i < UniqueDecls; ++i)
12919 ListsPerDecl.push_back(Elt: Record.readInt());
12920 C->setDeclNumLists(ListsPerDecl);
12921
12922 SmallVector<unsigned, 32> ListSizes;
12923 ListSizes.reserve(N: TotalLists);
12924 for (unsigned i = 0; i < TotalLists; ++i)
12925 ListSizes.push_back(Elt: Record.readInt());
12926 C->setComponentListSizes(ListSizes);
12927
12928 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12929 Components.reserve(N: TotalComponents);
12930 for (unsigned i = 0; i < TotalComponents; ++i) {
12931 Expr *AssociatedExpr = Record.readSubExpr();
12932 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12933 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12934 /*IsNonContiguous=*/Args: false);
12935 }
12936 C->setComponents(Components, CLSs: ListSizes);
12937}
12938
12939void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12940 C->setLParenLoc(Record.readSourceLocation());
12941 auto NumVars = C->varlist_size();
12942 auto UniqueDecls = C->getUniqueDeclarationsNum();
12943 auto TotalLists = C->getTotalComponentListNum();
12944 auto TotalComponents = C->getTotalComponentsNum();
12945
12946 SmallVector<Expr *, 16> Vars;
12947 Vars.reserve(N: NumVars);
12948 for (unsigned I = 0; I != NumVars; ++I)
12949 Vars.push_back(Elt: Record.readSubExpr());
12950 C->setVarRefs(Vars);
12951 Vars.clear();
12952
12953 SmallVector<ValueDecl *, 16> Decls;
12954 Decls.reserve(N: UniqueDecls);
12955 for (unsigned I = 0; I < UniqueDecls; ++I)
12956 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12957 C->setUniqueDecls(Decls);
12958
12959 SmallVector<unsigned, 16> ListsPerDecl;
12960 ListsPerDecl.reserve(N: UniqueDecls);
12961 for (unsigned I = 0; I < UniqueDecls; ++I)
12962 ListsPerDecl.push_back(Elt: Record.readInt());
12963 C->setDeclNumLists(ListsPerDecl);
12964
12965 SmallVector<unsigned, 32> ListSizes;
12966 ListSizes.reserve(N: TotalLists);
12967 for (unsigned i = 0; i < TotalLists; ++i)
12968 ListSizes.push_back(Elt: Record.readInt());
12969 C->setComponentListSizes(ListSizes);
12970
12971 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12972 Components.reserve(N: TotalComponents);
12973 for (unsigned I = 0; I < TotalComponents; ++I) {
12974 Expr *AssociatedExpr = Record.readSubExpr();
12975 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12976 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12977 /*IsNonContiguous=*/Args: false);
12978 }
12979 C->setComponents(Components, CLSs: ListSizes);
12980}
12981
12982void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12983 C->setLParenLoc(Record.readSourceLocation());
12984 unsigned NumVars = C->varlist_size();
12985 SmallVector<Expr *, 16> Vars;
12986 Vars.reserve(N: NumVars);
12987 for (unsigned i = 0; i != NumVars; ++i)
12988 Vars.push_back(Elt: Record.readSubExpr());
12989 C->setVarRefs(Vars);
12990 Vars.clear();
12991 Vars.reserve(N: NumVars);
12992 for (unsigned i = 0; i != NumVars; ++i)
12993 Vars.push_back(Elt: Record.readSubExpr());
12994 C->setPrivateRefs(Vars);
12995}
12996
12997void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12998 C->setLParenLoc(Record.readSourceLocation());
12999 unsigned NumVars = C->varlist_size();
13000 SmallVector<Expr *, 16> Vars;
13001 Vars.reserve(N: NumVars);
13002 for (unsigned i = 0; i != NumVars; ++i)
13003 Vars.push_back(Elt: Record.readSubExpr());
13004 C->setVarRefs(Vars);
13005}
13006
13007void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
13008 C->setLParenLoc(Record.readSourceLocation());
13009 unsigned NumVars = C->varlist_size();
13010 SmallVector<Expr *, 16> Vars;
13011 Vars.reserve(N: NumVars);
13012 for (unsigned i = 0; i != NumVars; ++i)
13013 Vars.push_back(Elt: Record.readSubExpr());
13014 C->setVarRefs(Vars);
13015}
13016
13017void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13018 C->setLParenLoc(Record.readSourceLocation());
13019 unsigned NumOfAllocators = C->getNumberOfAllocators();
13020 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13021 Data.reserve(N: NumOfAllocators);
13022 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13023 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13024 D.Allocator = Record.readSubExpr();
13025 D.AllocatorTraits = Record.readSubExpr();
13026 D.LParenLoc = Record.readSourceLocation();
13027 D.RParenLoc = Record.readSourceLocation();
13028 }
13029 C->setAllocatorsData(Data);
13030}
13031
13032void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13033 C->setLParenLoc(Record.readSourceLocation());
13034 C->setModifier(Record.readSubExpr());
13035 C->setColonLoc(Record.readSourceLocation());
13036 unsigned NumOfLocators = C->varlist_size();
13037 SmallVector<Expr *, 4> Locators;
13038 Locators.reserve(N: NumOfLocators);
13039 for (unsigned I = 0; I != NumOfLocators; ++I)
13040 Locators.push_back(Elt: Record.readSubExpr());
13041 C->setVarRefs(Locators);
13042}
13043
13044void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13045 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13046 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13047 C->setLParenLoc(Record.readSourceLocation());
13048 C->setKindKwLoc(Record.readSourceLocation());
13049 C->setModifierKwLoc(Record.readSourceLocation());
13050}
13051
13052void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13053 VisitOMPClauseWithPreInit(C);
13054 C->setThreadID(Record.readSubExpr());
13055 C->setLParenLoc(Record.readSourceLocation());
13056}
13057
13058void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13059 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13060 C->setLParenLoc(Record.readSourceLocation());
13061 C->setBindKindLoc(Record.readSourceLocation());
13062}
13063
13064void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13065 C->setAlignment(Record.readExpr());
13066 C->setLParenLoc(Record.readSourceLocation());
13067}
13068
13069void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13070 VisitOMPClauseWithPreInit(C);
13071 C->setSize(Record.readSubExpr());
13072 C->setLParenLoc(Record.readSourceLocation());
13073}
13074
13075void OMPClauseReader::VisitOMPDynGroupprivateClause(
13076 OMPDynGroupprivateClause *C) {
13077 VisitOMPClauseWithPreInit(C);
13078 C->setDynGroupprivateModifier(
13079 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13080 C->setDynGroupprivateFallbackModifier(
13081 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13082 C->setSize(Record.readSubExpr());
13083 C->setLParenLoc(Record.readSourceLocation());
13084 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13085 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13086}
13087
13088void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13089 C->setLParenLoc(Record.readSourceLocation());
13090 C->setDependenceType(
13091 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13092 C->setDependenceLoc(Record.readSourceLocation());
13093 C->setColonLoc(Record.readSourceLocation());
13094 unsigned NumVars = C->varlist_size();
13095 SmallVector<Expr *, 16> Vars;
13096 Vars.reserve(N: NumVars);
13097 for (unsigned I = 0; I != NumVars; ++I)
13098 Vars.push_back(Elt: Record.readSubExpr());
13099 C->setVarRefs(Vars);
13100 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13101 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13102}
13103
13104void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13105 AttrVec Attrs;
13106 Record.readAttributes(Attrs);
13107 C->setAttrs(Attrs);
13108 C->setLocStart(Record.readSourceLocation());
13109 C->setLParenLoc(Record.readSourceLocation());
13110 C->setLocEnd(Record.readSourceLocation());
13111}
13112
13113void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13114
13115OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13116 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13117 TI.Sets.resize(N: readUInt32());
13118 for (auto &Set : TI.Sets) {
13119 Set.Kind = readEnum<llvm::omp::TraitSet>();
13120 Set.Selectors.resize(N: readUInt32());
13121 for (auto &Selector : Set.Selectors) {
13122 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13123 Selector.ScoreOrCondition = nullptr;
13124 if (readBool())
13125 Selector.ScoreOrCondition = readExprRef();
13126 Selector.Properties.resize(N: readUInt32());
13127 for (auto &Property : Selector.Properties)
13128 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13129 }
13130 }
13131 return &TI;
13132}
13133
13134void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13135 if (!Data)
13136 return;
13137 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13138 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13139 skipInts(N: 3);
13140 }
13141 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13142 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13143 Clauses[I] = readOMPClause();
13144 Data->setClauses(Clauses);
13145 if (Data->hasAssociatedStmt())
13146 Data->setAssociatedStmt(readStmt());
13147 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13148 Data->getChildren()[I] = readStmt();
13149}
13150
13151SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13152 unsigned NumVars = readInt();
13153 llvm::SmallVector<Expr *> VarList;
13154 for (unsigned I = 0; I < NumVars; ++I)
13155 VarList.push_back(Elt: readExpr());
13156 return VarList;
13157}
13158
13159SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13160 unsigned NumExprs = readInt();
13161 llvm::SmallVector<Expr *> ExprList;
13162 for (unsigned I = 0; I < NumExprs; ++I)
13163 ExprList.push_back(Elt: readSubExpr());
13164 return ExprList;
13165}
13166
13167OpenACCClause *ASTRecordReader::readOpenACCClause() {
13168 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13169 SourceLocation BeginLoc = readSourceLocation();
13170 SourceLocation EndLoc = readSourceLocation();
13171
13172 switch (ClauseKind) {
13173 case OpenACCClauseKind::Default: {
13174 SourceLocation LParenLoc = readSourceLocation();
13175 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13176 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13177 EndLoc);
13178 }
13179 case OpenACCClauseKind::If: {
13180 SourceLocation LParenLoc = readSourceLocation();
13181 Expr *CondExpr = readSubExpr();
13182 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13183 EndLoc);
13184 }
13185 case OpenACCClauseKind::Self: {
13186 SourceLocation LParenLoc = readSourceLocation();
13187 bool isConditionExprClause = readBool();
13188 if (isConditionExprClause) {
13189 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13190 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13191 ConditionExpr: CondExpr, EndLoc);
13192 }
13193 unsigned NumVars = readInt();
13194 llvm::SmallVector<Expr *> VarList;
13195 for (unsigned I = 0; I < NumVars; ++I)
13196 VarList.push_back(Elt: readSubExpr());
13197 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13198 EndLoc);
13199 }
13200 case OpenACCClauseKind::NumGangs: {
13201 SourceLocation LParenLoc = readSourceLocation();
13202 unsigned NumClauses = readInt();
13203 llvm::SmallVector<Expr *> IntExprs;
13204 for (unsigned I = 0; I < NumClauses; ++I)
13205 IntExprs.push_back(Elt: readSubExpr());
13206 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13207 IntExprs, EndLoc);
13208 }
13209 case OpenACCClauseKind::NumWorkers: {
13210 SourceLocation LParenLoc = readSourceLocation();
13211 Expr *IntExpr = readSubExpr();
13212 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13213 IntExpr, EndLoc);
13214 }
13215 case OpenACCClauseKind::DeviceNum: {
13216 SourceLocation LParenLoc = readSourceLocation();
13217 Expr *IntExpr = readSubExpr();
13218 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13219 IntExpr, EndLoc);
13220 }
13221 case OpenACCClauseKind::DefaultAsync: {
13222 SourceLocation LParenLoc = readSourceLocation();
13223 Expr *IntExpr = readSubExpr();
13224 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13225 IntExpr, EndLoc);
13226 }
13227 case OpenACCClauseKind::VectorLength: {
13228 SourceLocation LParenLoc = readSourceLocation();
13229 Expr *IntExpr = readSubExpr();
13230 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13231 IntExpr, EndLoc);
13232 }
13233 case OpenACCClauseKind::Private: {
13234 SourceLocation LParenLoc = readSourceLocation();
13235 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13236
13237 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13238 for (unsigned I = 0; I < VarList.size(); ++I) {
13239 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13240 VarDecl *Alloca = readDeclAs<VarDecl>();
13241 RecipeList.push_back(Elt: {Alloca});
13242 }
13243
13244 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13245 VarList, InitRecipes: RecipeList, EndLoc);
13246 }
13247 case OpenACCClauseKind::Host: {
13248 SourceLocation LParenLoc = readSourceLocation();
13249 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13250 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13251 EndLoc);
13252 }
13253 case OpenACCClauseKind::Device: {
13254 SourceLocation LParenLoc = readSourceLocation();
13255 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13256 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13257 VarList, EndLoc);
13258 }
13259 case OpenACCClauseKind::FirstPrivate: {
13260 SourceLocation LParenLoc = readSourceLocation();
13261 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13262 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13263 for (unsigned I = 0; I < VarList.size(); ++I) {
13264 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13265 VarDecl *Recipe = readDeclAs<VarDecl>();
13266 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13267 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13268 }
13269
13270 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13271 VarList, InitRecipes: RecipeList, EndLoc);
13272 }
13273 case OpenACCClauseKind::Attach: {
13274 SourceLocation LParenLoc = readSourceLocation();
13275 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13276 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13277 VarList, EndLoc);
13278 }
13279 case OpenACCClauseKind::Detach: {
13280 SourceLocation LParenLoc = readSourceLocation();
13281 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13282 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13283 VarList, EndLoc);
13284 }
13285 case OpenACCClauseKind::Delete: {
13286 SourceLocation LParenLoc = readSourceLocation();
13287 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13288 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13289 VarList, EndLoc);
13290 }
13291 case OpenACCClauseKind::UseDevice: {
13292 SourceLocation LParenLoc = readSourceLocation();
13293 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13294 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13295 VarList, EndLoc);
13296 }
13297 case OpenACCClauseKind::DevicePtr: {
13298 SourceLocation LParenLoc = readSourceLocation();
13299 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13300 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13301 VarList, EndLoc);
13302 }
13303 case OpenACCClauseKind::NoCreate: {
13304 SourceLocation LParenLoc = readSourceLocation();
13305 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13306 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13307 VarList, EndLoc);
13308 }
13309 case OpenACCClauseKind::Present: {
13310 SourceLocation LParenLoc = readSourceLocation();
13311 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13312 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13313 VarList, EndLoc);
13314 }
13315 case OpenACCClauseKind::PCopy:
13316 case OpenACCClauseKind::PresentOrCopy:
13317 case OpenACCClauseKind::Copy: {
13318 SourceLocation LParenLoc = readSourceLocation();
13319 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13320 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13321 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13322 LParenLoc, Mods: ModList, VarList, EndLoc);
13323 }
13324 case OpenACCClauseKind::CopyIn:
13325 case OpenACCClauseKind::PCopyIn:
13326 case OpenACCClauseKind::PresentOrCopyIn: {
13327 SourceLocation LParenLoc = readSourceLocation();
13328 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13329 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13330 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13331 LParenLoc, Mods: ModList, VarList, EndLoc);
13332 }
13333 case OpenACCClauseKind::CopyOut:
13334 case OpenACCClauseKind::PCopyOut:
13335 case OpenACCClauseKind::PresentOrCopyOut: {
13336 SourceLocation LParenLoc = readSourceLocation();
13337 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13338 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13339 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13340 LParenLoc, Mods: ModList, VarList, EndLoc);
13341 }
13342 case OpenACCClauseKind::Create:
13343 case OpenACCClauseKind::PCreate:
13344 case OpenACCClauseKind::PresentOrCreate: {
13345 SourceLocation LParenLoc = readSourceLocation();
13346 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13347 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13348 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13349 LParenLoc, Mods: ModList, VarList, EndLoc);
13350 }
13351 case OpenACCClauseKind::Async: {
13352 SourceLocation LParenLoc = readSourceLocation();
13353 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13354 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13355 IntExpr: AsyncExpr, EndLoc);
13356 }
13357 case OpenACCClauseKind::Wait: {
13358 SourceLocation LParenLoc = readSourceLocation();
13359 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13360 SourceLocation QueuesLoc = readSourceLocation();
13361 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13362 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13363 DevNumExpr, QueuesLoc, QueueIdExprs,
13364 EndLoc);
13365 }
13366 case OpenACCClauseKind::DeviceType:
13367 case OpenACCClauseKind::DType: {
13368 SourceLocation LParenLoc = readSourceLocation();
13369 llvm::SmallVector<DeviceTypeArgument> Archs;
13370 unsigned NumArchs = readInt();
13371
13372 for (unsigned I = 0; I < NumArchs; ++I) {
13373 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13374 SourceLocation Loc = readSourceLocation();
13375 Archs.emplace_back(Args&: Loc, Args&: Ident);
13376 }
13377
13378 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13379 LParenLoc, Archs, EndLoc);
13380 }
13381 case OpenACCClauseKind::Reduction: {
13382 SourceLocation LParenLoc = readSourceLocation();
13383 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13384 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13385 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13386
13387 for (unsigned I = 0; I < VarList.size(); ++I) {
13388 VarDecl *Recipe = readDeclAs<VarDecl>();
13389
13390 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13391 3 * sizeof(int *));
13392
13393 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13394 unsigned NumCombiners = readInt();
13395 for (unsigned I = 0; I < NumCombiners; ++I) {
13396 VarDecl *LHS = readDeclAs<VarDecl>();
13397 VarDecl *RHS = readDeclAs<VarDecl>();
13398 Expr *Op = readExpr();
13399
13400 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13401 }
13402
13403 RecipeList.push_back(Elt: {Recipe, Combiners});
13404 }
13405
13406 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13407 VarList, Recipes: RecipeList, EndLoc);
13408 }
13409 case OpenACCClauseKind::Seq:
13410 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13411 case OpenACCClauseKind::NoHost:
13412 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13413 case OpenACCClauseKind::Finalize:
13414 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13415 case OpenACCClauseKind::IfPresent:
13416 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13417 case OpenACCClauseKind::Independent:
13418 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13419 case OpenACCClauseKind::Auto:
13420 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13421 case OpenACCClauseKind::Collapse: {
13422 SourceLocation LParenLoc = readSourceLocation();
13423 bool HasForce = readBool();
13424 Expr *LoopCount = readSubExpr();
13425 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13426 HasForce, LoopCount, EndLoc);
13427 }
13428 case OpenACCClauseKind::Tile: {
13429 SourceLocation LParenLoc = readSourceLocation();
13430 unsigned NumClauses = readInt();
13431 llvm::SmallVector<Expr *> SizeExprs;
13432 for (unsigned I = 0; I < NumClauses; ++I)
13433 SizeExprs.push_back(Elt: readSubExpr());
13434 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13435 SizeExprs, EndLoc);
13436 }
13437 case OpenACCClauseKind::Gang: {
13438 SourceLocation LParenLoc = readSourceLocation();
13439 unsigned NumExprs = readInt();
13440 llvm::SmallVector<OpenACCGangKind> GangKinds;
13441 llvm::SmallVector<Expr *> Exprs;
13442 for (unsigned I = 0; I < NumExprs; ++I) {
13443 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13444 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13445 Exprs.push_back(Elt: readExpr());
13446 }
13447 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13448 GangKinds, IntExprs: Exprs, EndLoc);
13449 }
13450 case OpenACCClauseKind::Worker: {
13451 SourceLocation LParenLoc = readSourceLocation();
13452 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13453 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13454 IntExpr: WorkerExpr, EndLoc);
13455 }
13456 case OpenACCClauseKind::Vector: {
13457 SourceLocation LParenLoc = readSourceLocation();
13458 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13459 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13460 IntExpr: VectorExpr, EndLoc);
13461 }
13462 case OpenACCClauseKind::Link: {
13463 SourceLocation LParenLoc = readSourceLocation();
13464 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13465 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13466 EndLoc);
13467 }
13468 case OpenACCClauseKind::DeviceResident: {
13469 SourceLocation LParenLoc = readSourceLocation();
13470 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13471 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13472 LParenLoc, VarList, EndLoc);
13473 }
13474
13475 case OpenACCClauseKind::Bind: {
13476 SourceLocation LParenLoc = readSourceLocation();
13477 bool IsString = readBool();
13478 if (IsString)
13479 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13480 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13481 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13482 ID: readIdentifier(), EndLoc);
13483 }
13484 case OpenACCClauseKind::Shortloop:
13485 case OpenACCClauseKind::Invalid:
13486 llvm_unreachable("Clause serialization not yet implemented");
13487 }
13488 llvm_unreachable("Invalid Clause Kind");
13489}
13490
13491void ASTRecordReader::readOpenACCClauseList(
13492 MutableArrayRef<const OpenACCClause *> Clauses) {
13493 for (unsigned I = 0; I < Clauses.size(); ++I)
13494 Clauses[I] = readOpenACCClause();
13495}
13496
13497void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13498 unsigned NumVars = readInt();
13499 A->Clauses.resize(N: NumVars);
13500 readOpenACCClauseList(Clauses: A->Clauses);
13501}
13502
13503static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13504 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13505 // module name?
13506 llvm::FoldingSetNodeID ID;
13507 ID.AddString(String: PrimaryModuleName);
13508 return ID.computeStableHash();
13509}
13510
13511UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13512 if (!M)
13513 return std::nullopt;
13514
13515 if (M->isHeaderLikeModule())
13516 return std::nullopt;
13517
13518 if (M->isGlobalModule())
13519 return std::nullopt;
13520
13521 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13522 return getStableHashForModuleName(PrimaryModuleName);
13523}
13524