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/SemaRISCV.h"
85#include "clang/Sema/Weak.h"
86#include "clang/Serialization/ASTBitCodes.h"
87#include "clang/Serialization/ASTDeserializationListener.h"
88#include "clang/Serialization/ASTRecordReader.h"
89#include "clang/Serialization/ContinuousRangeMap.h"
90#include "clang/Serialization/GlobalModuleIndex.h"
91#include "clang/Serialization/InMemoryModuleCache.h"
92#include "clang/Serialization/ModuleCache.h"
93#include "clang/Serialization/ModuleFile.h"
94#include "clang/Serialization/ModuleFileExtension.h"
95#include "clang/Serialization/ModuleManager.h"
96#include "clang/Serialization/PCHContainerOperations.h"
97#include "clang/Serialization/SerializationDiagnostic.h"
98#include "llvm/ADT/APFloat.h"
99#include "llvm/ADT/APInt.h"
100#include "llvm/ADT/ArrayRef.h"
101#include "llvm/ADT/DenseMap.h"
102#include "llvm/ADT/FoldingSet.h"
103#include "llvm/ADT/IntrusiveRefCntPtr.h"
104#include "llvm/ADT/STLExtras.h"
105#include "llvm/ADT/ScopeExit.h"
106#include "llvm/ADT/Sequence.h"
107#include "llvm/ADT/SmallPtrSet.h"
108#include "llvm/ADT/SmallVector.h"
109#include "llvm/ADT/StringExtras.h"
110#include "llvm/ADT/StringMap.h"
111#include "llvm/ADT/StringRef.h"
112#include "llvm/ADT/iterator_range.h"
113#include "llvm/Bitstream/BitstreamReader.h"
114#include "llvm/Support/Compiler.h"
115#include "llvm/Support/Compression.h"
116#include "llvm/Support/DJB.h"
117#include "llvm/Support/Endian.h"
118#include "llvm/Support/Error.h"
119#include "llvm/Support/ErrorHandling.h"
120#include "llvm/Support/LEB128.h"
121#include "llvm/Support/MemoryBuffer.h"
122#include "llvm/Support/Path.h"
123#include "llvm/Support/SaveAndRestore.h"
124#include "llvm/Support/TimeProfiler.h"
125#include "llvm/Support/Timer.h"
126#include "llvm/Support/VersionTuple.h"
127#include "llvm/Support/VirtualFileSystem.h"
128#include "llvm/Support/raw_ostream.h"
129#include "llvm/TargetParser/Triple.h"
130#include <algorithm>
131#include <cassert>
132#include <cstddef>
133#include <cstdint>
134#include <cstdio>
135#include <ctime>
136#include <iterator>
137#include <limits>
138#include <map>
139#include <memory>
140#include <optional>
141#include <string>
142#include <system_error>
143#include <tuple>
144#include <utility>
145#include <vector>
146
147using namespace clang;
148using namespace clang::serialization;
149using namespace clang::serialization::reader;
150using llvm::BitstreamCursor;
151
152//===----------------------------------------------------------------------===//
153// ChainedASTReaderListener implementation
154//===----------------------------------------------------------------------===//
155
156bool
157ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
158 return First->ReadFullVersionInformation(FullVersion) ||
159 Second->ReadFullVersionInformation(FullVersion);
160}
161
162void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
163 First->ReadModuleName(ModuleName);
164 Second->ReadModuleName(ModuleName);
165}
166
167void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
168 First->ReadModuleMapFile(ModuleMapPath);
169 Second->ReadModuleMapFile(ModuleMapPath);
170}
171
172bool ChainedASTReaderListener::ReadLanguageOptions(
173 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
174 bool AllowCompatibleDifferences) {
175 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
176 AllowCompatibleDifferences) ||
177 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
178 AllowCompatibleDifferences);
179}
180
181bool ChainedASTReaderListener::ReadCodeGenOptions(
182 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
183 bool AllowCompatibleDifferences) {
184 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
185 AllowCompatibleDifferences) ||
186 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
187 AllowCompatibleDifferences);
188}
189
190bool ChainedASTReaderListener::ReadTargetOptions(
191 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
192 bool AllowCompatibleDifferences) {
193 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
194 AllowCompatibleDifferences) ||
195 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
196 AllowCompatibleDifferences);
197}
198
199bool ChainedASTReaderListener::ReadDiagnosticOptions(
200 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
201 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
202 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
203}
204
205bool
206ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
207 bool Complain) {
208 return First->ReadFileSystemOptions(FSOpts, Complain) ||
209 Second->ReadFileSystemOptions(FSOpts, Complain);
210}
211
212bool ChainedASTReaderListener::ReadHeaderSearchOptions(
213 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
214 StringRef ContextHash, bool Complain) {
215 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
216 Complain) ||
217 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
218 Complain);
219}
220
221bool ChainedASTReaderListener::ReadPreprocessorOptions(
222 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
223 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
224 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
225 Complain, SuggestedPredefines) ||
226 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
227 Complain, SuggestedPredefines);
228}
229
230void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
231 uint32_t Value) {
232 First->ReadCounter(M, Value);
233 Second->ReadCounter(M, Value);
234}
235
236bool ChainedASTReaderListener::needsInputFileVisitation() {
237 return First->needsInputFileVisitation() ||
238 Second->needsInputFileVisitation();
239}
240
241bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
242 return First->needsSystemInputFileVisitation() ||
243 Second->needsSystemInputFileVisitation();
244}
245
246void ChainedASTReaderListener::visitModuleFile(ModuleFileName Filename,
247 ModuleKind Kind,
248 bool DirectlyImported) {
249 First->visitModuleFile(Filename, Kind, DirectlyImported);
250 Second->visitModuleFile(Filename, Kind, DirectlyImported);
251}
252
253bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
254 bool isSystem,
255 bool isOverridden,
256 bool isExplicitModule) {
257 bool Continue = false;
258 if (First->needsInputFileVisitation() &&
259 (!isSystem || First->needsSystemInputFileVisitation()))
260 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
261 isExplicitModule);
262 if (Second->needsInputFileVisitation() &&
263 (!isSystem || Second->needsSystemInputFileVisitation()))
264 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
265 isExplicitModule);
266 return Continue;
267}
268
269void ChainedASTReaderListener::readModuleFileExtension(
270 const ModuleFileExtensionMetadata &Metadata) {
271 First->readModuleFileExtension(Metadata);
272 Second->readModuleFileExtension(Metadata);
273}
274
275//===----------------------------------------------------------------------===//
276// PCH validator implementation
277//===----------------------------------------------------------------------===//
278
279ASTReaderListener::~ASTReaderListener() = default;
280
281static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionFlagMismatch(
282 DiagnosticsEngine *Diags, StringRef Description, bool SerializedValue,
283 bool CurrentValue, StringRef ModuleFilename) {
284 if (!Diags)
285 return true;
286 return Diags->Report(DiagID: diag::err_ast_file_langopt_mismatch)
287 << Description << SerializedValue << CurrentValue << ModuleFilename;
288}
289
290static LLVM_ATTRIBUTE_NOINLINE bool diagnoseLanguageOptionValueMismatch(
291 DiagnosticsEngine *Diags, StringRef Description, StringRef ModuleFilename) {
292 if (!Diags)
293 return true;
294 return Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
295 << Description << ModuleFilename;
296}
297
298/// Compare the given set of language options against an existing set of
299/// language options.
300///
301/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
302/// \param AllowCompatibleDifferences If true, differences between compatible
303/// language options will be permitted.
304///
305/// \returns true if the languagae options mis-match, false otherwise.
306static bool checkLanguageOptions(const LangOptions &LangOpts,
307 const LangOptions &ExistingLangOpts,
308 StringRef ModuleFilename,
309 DiagnosticsEngine *Diags,
310 bool AllowCompatibleDifferences = true) {
311 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
312 using CK = LangOptions::CompatibilityKind;
313
314#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
315 if constexpr (CK::Compatibility != CK::Benign) { \
316 if ((CK::Compatibility == CK::NotCompatible) || \
317 (CK::Compatibility == CK::Compatible && \
318 !AllowCompatibleDifferences)) { \
319 if (ExistingLangOpts.Name != LangOpts.Name) { \
320 if (Bits == 1) \
321 return diagnoseLanguageOptionFlagMismatch( \
322 Diags, Description, LangOpts.Name, ExistingLangOpts.Name, \
323 ModuleFilename); \
324 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
325 ModuleFilename); \
326 } \
327 } \
328 }
329
330#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
331 if constexpr (CK::Compatibility != CK::Benign) { \
332 if ((CK::Compatibility == CK::NotCompatible) || \
333 (CK::Compatibility == CK::Compatible && \
334 !AllowCompatibleDifferences)) { \
335 if (ExistingLangOpts.Name != LangOpts.Name) { \
336 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
337 ModuleFilename); \
338 } \
339 } \
340 }
341
342#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
343 if constexpr (CK::Compatibility != CK::Benign) { \
344 if ((CK::Compatibility == CK::NotCompatible) || \
345 (CK::Compatibility == CK::Compatible && \
346 !AllowCompatibleDifferences)) { \
347 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
348 return diagnoseLanguageOptionValueMismatch(Diags, Description, \
349 ModuleFilename); \
350 } \
351 } \
352 }
353
354#include "clang/Basic/LangOptions.def"
355
356 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
357 return diagnoseLanguageOptionValueMismatch(Diags, Description: "module features",
358 ModuleFilename);
359 }
360
361 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
362 return diagnoseLanguageOptionValueMismatch(
363 Diags, Description: "target Objective-C runtime", ModuleFilename);
364 }
365
366 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
367 LangOpts.CommentOpts.BlockCommandNames) {
368 return diagnoseLanguageOptionValueMismatch(Diags, Description: "block command names",
369 ModuleFilename);
370 }
371
372 // Sanitizer feature mismatches are treated as compatible differences. If
373 // compatible differences aren't allowed, we still only want to check for
374 // mismatches of non-modular sanitizers (the only ones which can affect AST
375 // generation).
376 if (!AllowCompatibleDifferences) {
377 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
378 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
379 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
380 ExistingSanitizers.clear(K: ModularSanitizers);
381 ImportedSanitizers.clear(K: ModularSanitizers);
382 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
383 const std::string Flag = "-fsanitize=";
384 if (Diags) {
385#define SANITIZER(NAME, ID) \
386 { \
387 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
388 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
389 if (InExistingModule != InImportedModule) \
390 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
391 << InExistingModule << ModuleFilename << (Flag + NAME); \
392 }
393#include "clang/Basic/Sanitizers.def"
394 }
395 return true;
396 }
397 }
398
399 return false;
400}
401
402static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
403 const CodeGenOptions &ExistingCGOpts,
404 StringRef ModuleFilename,
405 DiagnosticsEngine *Diags,
406 bool AllowCompatibleDifferences = true) {
407 // FIXME: Specify and print a description for each option instead of the name.
408 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
409 using CK = CodeGenOptions::CompatibilityKind;
410#define CODEGENOPT(Name, Bits, Default, Compatibility) \
411 if constexpr (CK::Compatibility != CK::Benign) { \
412 if ((CK::Compatibility == CK::NotCompatible) || \
413 (CK::Compatibility == CK::Compatible && \
414 !AllowCompatibleDifferences)) { \
415 if (ExistingCGOpts.Name != CGOpts.Name) { \
416 if (Diags) { \
417 if (Bits == 1) \
418 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
419 << #Name << CGOpts.Name << ExistingCGOpts.Name \
420 << ModuleFilename; \
421 else \
422 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
423 << #Name << ModuleFilename; \
424 } \
425 return true; \
426 } \
427 } \
428 }
429
430#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
431 if constexpr (CK::Compatibility != CK::Benign) { \
432 if ((CK::Compatibility == CK::NotCompatible) || \
433 (CK::Compatibility == CK::Compatible && \
434 !AllowCompatibleDifferences)) { \
435 if (ExistingCGOpts.Name != CGOpts.Name) { \
436 if (Diags) \
437 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
438 << #Name << ModuleFilename; \
439 return true; \
440 } \
441 } \
442 }
443#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
444 if constexpr (CK::Compatibility != CK::Benign) { \
445 if ((CK::Compatibility == CK::NotCompatible) || \
446 (CK::Compatibility == CK::Compatible && \
447 !AllowCompatibleDifferences)) { \
448 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
449 if (Diags) \
450 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
451 << #Name << ModuleFilename; \
452 return true; \
453 } \
454 } \
455 }
456#define DEBUGOPT(Name, Bits, Default, Compatibility)
457#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
458#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
459#include "clang/Basic/CodeGenOptions.def"
460
461 return false;
462}
463
464static std::vector<std::string>
465accumulateFeaturesAsWritten(std::vector<std::string> FeaturesAsWritten) {
466 llvm::erase_if(C&: FeaturesAsWritten, P: [](const std::string &S) {
467 return S.empty() || (S[0] != '+' && S[0] != '-');
468 });
469 llvm::stable_sort(Range&: FeaturesAsWritten,
470 C: [](const std::string &A, const std::string &B) {
471 return A.substr(pos: 1) < B.substr(pos: 1);
472 });
473 auto NewRend =
474 std::unique(first: FeaturesAsWritten.rbegin(), last: FeaturesAsWritten.rend(),
475 binary_pred: [](const std::string &A, const std::string &B) {
476 return A.substr(pos: 1) == B.substr(pos: 1);
477 });
478 // Because we are operating on reverse iterators, the duplicate elements
479 // are actually at the beginning.
480 FeaturesAsWritten.erase(first: FeaturesAsWritten.begin(), last: NewRend.base());
481 return FeaturesAsWritten;
482}
483
484/// Compare the given set of target options against an existing set of
485/// target options.
486///
487/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
488///
489/// \returns true if the target options mis-match, false otherwise.
490static bool checkTargetOptions(const TargetOptions &TargetOpts,
491 const TargetOptions &ExistingTargetOpts,
492 StringRef ModuleFilename,
493 DiagnosticsEngine *Diags,
494 bool AllowCompatibleDifferences = true) {
495#define CHECK_TARGET_OPT(Field, Name) \
496 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
497 if (Diags) \
498 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
499 << ModuleFilename << Name << TargetOpts.Field \
500 << ExistingTargetOpts.Field; \
501 return true; \
502 }
503
504 // The triple and ABI must match exactly.
505 CHECK_TARGET_OPT(Triple, "target");
506 CHECK_TARGET_OPT(ABI, "target ABI");
507
508 // We can tolerate different CPUs in many cases, notably when one CPU
509 // supports a strict superset of another. When allowing compatible
510 // differences skip this check.
511 if (!AllowCompatibleDifferences) {
512 CHECK_TARGET_OPT(CPU, "target CPU");
513 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
514 }
515
516#undef CHECK_TARGET_OPT
517
518 // Compare feature sets.
519 // Alternatively, we could be diffing TargetOpts.Features, but that would
520 // clutter the output with implied features.
521 std::vector<std::string> ExistingFeatures =
522 accumulateFeaturesAsWritten(FeaturesAsWritten: ExistingTargetOpts.FeaturesAsWritten);
523 std::vector<std::string> ReadFeatures =
524 accumulateFeaturesAsWritten(FeaturesAsWritten: TargetOpts.FeaturesAsWritten);
525
526 // We compute the set difference in both directions explicitly so that we can
527 // diagnose the differences differently.
528 auto FeatureLess = [](StringRef A, StringRef B) {
529 return A.substr(Start: 1) < B.substr(Start: 1);
530 };
531
532 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
533 std::set_difference(first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(),
534 first2: ReadFeatures.begin(), last2: ReadFeatures.end(),
535 result: std::back_inserter(x&: UnmatchedExistingFeatures),
536 comp: FeatureLess);
537 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
538 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
539 result: std::back_inserter(x&: UnmatchedReadFeatures), comp: FeatureLess);
540
541 // If we are allowing compatible differences and the read feature set is
542 // a strict subset of the existing feature set, there is nothing to diagnose.
543 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
544 return false;
545
546 if (Diags) {
547 for (StringRef Feature : UnmatchedReadFeatures)
548 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
549 << /* is-existing-feature */ false << ModuleFilename << Feature;
550 for (StringRef Feature : UnmatchedExistingFeatures)
551 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
552 << /* is-existing-feature */ true << ModuleFilename << Feature;
553 }
554
555 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
556}
557
558bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
559 StringRef ModuleFilename, bool Complain,
560 bool AllowCompatibleDifferences) {
561 const LangOptions &ExistingLangOpts = PP.getLangOpts();
562 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
563 Diags: Complain ? &Reader.Diags : nullptr,
564 AllowCompatibleDifferences);
565}
566
567bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
568 StringRef ModuleFilename, bool Complain,
569 bool AllowCompatibleDifferences) {
570 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
571 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
572 Diags: Complain ? &Reader.Diags : nullptr,
573 AllowCompatibleDifferences);
574}
575
576bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
577 StringRef ModuleFilename, bool Complain,
578 bool AllowCompatibleDifferences) {
579 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
580 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
581 Diags: Complain ? &Reader.Diags : nullptr,
582 AllowCompatibleDifferences);
583}
584
585namespace {
586
587using MacroDefinitionsMap =
588 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
589
590class DeclsSet {
591 SmallVector<NamedDecl *, 64> Decls;
592 llvm::SmallPtrSet<NamedDecl *, 8> Found;
593
594public:
595 operator ArrayRef<NamedDecl *>() const { return Decls; }
596
597 bool empty() const { return Decls.empty(); }
598
599 bool insert(NamedDecl *ND) {
600 auto [_, Inserted] = Found.insert(Ptr: ND);
601 if (Inserted)
602 Decls.push_back(Elt: ND);
603 return Inserted;
604 }
605};
606
607using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
608
609} // namespace
610
611static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
612 DiagnosticsEngine &Diags,
613 StringRef ModuleFilename,
614 bool Complain) {
615 using Level = DiagnosticsEngine::Level;
616
617 // Check current mappings for new -Werror mappings, and the stored mappings
618 // for cases that were explicitly mapped to *not* be errors that are now
619 // errors because of options like -Werror.
620 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
621
622 for (DiagnosticsEngine *MappingSource : MappingSources) {
623 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
624 diag::kind DiagID = DiagIDMappingPair.first;
625 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
626 if (CurLevel < DiagnosticsEngine::Error)
627 continue; // not significant
628 Level StoredLevel =
629 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
630 if (StoredLevel < DiagnosticsEngine::Error) {
631 if (Complain)
632 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
633 << "-Werror=" + Diags.getDiagnosticIDs()
634 ->getWarningOptionForDiag(DiagID)
635 .str()
636 << ModuleFilename;
637 return true;
638 }
639 }
640 }
641
642 return false;
643}
644
645static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
646 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
647 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
648 return true;
649 return Ext >= diag::Severity::Error;
650}
651
652static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
653 DiagnosticsEngine &Diags,
654 StringRef ModuleFilename, bool IsSystem,
655 bool SystemHeaderWarningsInModule,
656 bool Complain) {
657 // Top-level options
658 if (IsSystem) {
659 if (Diags.getSuppressSystemWarnings())
660 return false;
661 // If -Wsystem-headers was not enabled before, and it was not explicit,
662 // be conservative
663 if (StoredDiags.getSuppressSystemWarnings() &&
664 !SystemHeaderWarningsInModule) {
665 if (Complain)
666 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
667 << "-Wsystem-headers" << ModuleFilename;
668 return true;
669 }
670 }
671
672 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
673 if (Complain)
674 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
675 << "-Werror" << ModuleFilename;
676 return true;
677 }
678
679 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
680 !StoredDiags.getEnableAllWarnings()) {
681 if (Complain)
682 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
683 << "-Weverything -Werror" << ModuleFilename;
684 return true;
685 }
686
687 if (isExtHandlingFromDiagsError(Diags) &&
688 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
689 if (Complain)
690 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
691 << "-pedantic-errors" << ModuleFilename;
692 return true;
693 }
694
695 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
696 Complain);
697}
698
699/// Return the top import module if it is implicit, nullptr otherwise.
700static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
701 Preprocessor &PP) {
702 // If the original import came from a file explicitly generated by the user,
703 // don't check the diagnostic mappings.
704 // FIXME: currently this is approximated by checking whether this is not a
705 // module import of an implicitly-loaded module file.
706 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
707 // the transitive closure of its imports, since unrelated modules cannot be
708 // imported until after this module finishes validation.
709 ModuleFile *TopImport = &*ModuleMgr.rbegin();
710 while (!TopImport->ImportedBy.empty())
711 TopImport = TopImport->ImportedBy[0];
712 if (TopImport->Kind != MK_ImplicitModule)
713 return nullptr;
714
715 StringRef ModuleName = TopImport->ModuleName;
716 assert(!ModuleName.empty() && "diagnostic options read before module name");
717
718 Module *M =
719 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
720 assert(M && "missing module");
721 return M;
722}
723
724bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
725 StringRef ModuleFilename,
726 bool Complain) {
727 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
728 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
729 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
730 // This should never fail, because we would have processed these options
731 // before writing them to an ASTFile.
732 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
733 VFS&: PP.getFileManager().getVirtualFileSystem(),
734 /*Report*/ ReportDiags: false);
735
736 ModuleManager &ModuleMgr = Reader.getModuleManager();
737 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
738
739 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
740 if (!TopM)
741 return false;
742
743 Module *Importer = PP.getCurrentModule();
744
745 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
746 bool SystemHeaderWarningsInModule =
747 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
748 Element: Importer->Name);
749
750 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
751 // contains the union of their flags.
752 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
753 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
754 Complain);
755}
756
757/// Collect the macro definitions provided by the given preprocessor
758/// options.
759static void
760collectMacroDefinitions(const PreprocessorOptions &PPOpts,
761 MacroDefinitionsMap &Macros,
762 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
763 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
764 StringRef Macro = PPOpts.Macros[I].first;
765 bool IsUndef = PPOpts.Macros[I].second;
766
767 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
768 StringRef MacroName = MacroPair.first;
769 StringRef MacroBody = MacroPair.second;
770
771 // For an #undef'd macro, we only care about the name.
772 if (IsUndef) {
773 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
774 if (MacroNames && Inserted)
775 MacroNames->push_back(Elt: MacroName);
776
777 It->second = std::make_pair(x: "", y: true);
778 continue;
779 }
780
781 // For a #define'd macro, figure out the actual definition.
782 if (MacroName.size() == Macro.size())
783 MacroBody = "1";
784 else {
785 // Note: GCC drops anything following an end-of-line character.
786 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
787 MacroBody = MacroBody.substr(Start: 0, N: End);
788 }
789
790 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
791 if (MacroNames && Inserted)
792 MacroNames->push_back(Elt: MacroName);
793 It->second = std::make_pair(x&: MacroBody, y: false);
794 }
795}
796
797enum OptionValidation {
798 OptionValidateNone,
799 OptionValidateContradictions,
800 OptionValidateStrictMatches,
801};
802
803/// Check the preprocessor options deserialized from the control block
804/// against the preprocessor options in an existing preprocessor.
805///
806/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
807/// \param Validation If set to OptionValidateNone, ignore differences in
808/// preprocessor options. If set to OptionValidateContradictions,
809/// require that options passed both in the AST file and on the command
810/// line (-D or -U) match, but tolerate options missing in one or the
811/// other. If set to OptionValidateContradictions, require that there
812/// are no differences in the options between the two.
813static bool checkPreprocessorOptions(
814 const PreprocessorOptions &PPOpts,
815 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
816 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
817 std::string &SuggestedPredefines, const LangOptions &LangOpts,
818 OptionValidation Validation = OptionValidateContradictions) {
819 if (ReadMacros) {
820 // Check macro definitions.
821 MacroDefinitionsMap ASTFileMacros;
822 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
823 MacroDefinitionsMap ExistingMacros;
824 SmallVector<StringRef, 4> ExistingMacroNames;
825 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
826 MacroNames: &ExistingMacroNames);
827
828 // Use a line marker to enter the <command line> file, as the defines and
829 // undefines here will have come from the command line.
830 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
831
832 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
833 // Dig out the macro definition in the existing preprocessor options.
834 StringRef MacroName = ExistingMacroNames[I];
835 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
836
837 // Check whether we know anything about this macro name or not.
838 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
839 ASTFileMacros.find(Key: MacroName);
840 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
841 if (Validation == OptionValidateStrictMatches) {
842 // If strict matches are requested, don't tolerate any extra defines
843 // on the command line that are missing in the AST file.
844 if (Diags) {
845 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
846 << MacroName << true << ModuleFilename;
847 }
848 return true;
849 }
850 // FIXME: Check whether this identifier was referenced anywhere in the
851 // AST file. If so, we should reject the AST file. Unfortunately, this
852 // information isn't in the control block. What shall we do about it?
853
854 if (Existing.second) {
855 SuggestedPredefines += "#undef ";
856 SuggestedPredefines += MacroName.str();
857 SuggestedPredefines += '\n';
858 } else {
859 SuggestedPredefines += "#define ";
860 SuggestedPredefines += MacroName.str();
861 SuggestedPredefines += ' ';
862 SuggestedPredefines += Existing.first.str();
863 SuggestedPredefines += '\n';
864 }
865 continue;
866 }
867
868 // If the macro was defined in one but undef'd in the other, we have a
869 // conflict.
870 if (Existing.second != Known->second.second) {
871 if (Diags) {
872 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
873 << MacroName << Known->second.second << ModuleFilename;
874 }
875 return true;
876 }
877
878 // If the macro was #undef'd in both, or if the macro bodies are
879 // identical, it's fine.
880 if (Existing.second || Existing.first == Known->second.first) {
881 ASTFileMacros.erase(I: Known);
882 continue;
883 }
884
885 // The macro bodies differ; complain.
886 if (Diags) {
887 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
888 << MacroName << Known->second.first << Existing.first
889 << ModuleFilename;
890 }
891 return true;
892 }
893
894 // Leave the <command line> file and return to <built-in>.
895 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
896
897 if (Validation == OptionValidateStrictMatches) {
898 // If strict matches are requested, don't tolerate any extra defines in
899 // the AST file that are missing on the command line.
900 for (const auto &MacroName : ASTFileMacros.keys()) {
901 if (Diags) {
902 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
903 << MacroName << false << ModuleFilename;
904 }
905 return true;
906 }
907 }
908 }
909
910 // Check whether we're using predefines.
911 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
912 Validation != OptionValidateNone) {
913 if (Diags) {
914 Diags->Report(DiagID: diag::err_ast_file_undef)
915 << ExistingPPOpts.UsePredefines << ModuleFilename;
916 }
917 return true;
918 }
919
920 // Detailed record is important since it is used for the module cache hash.
921 if (LangOpts.Modules &&
922 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
923 Validation != OptionValidateNone) {
924 if (Diags) {
925 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
926 << PPOpts.DetailedRecord << ModuleFilename;
927 }
928 return true;
929 }
930
931 // Compute the #include and #include_macros lines we need.
932 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
933 StringRef File = ExistingPPOpts.MacroIncludes[I];
934 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
935 continue;
936
937 SuggestedPredefines += "#__include_macros \"";
938 SuggestedPredefines += File;
939 SuggestedPredefines += "\"\n##\n";
940 }
941
942 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
943 StringRef File = ExistingPPOpts.Includes[I];
944
945 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
946 !ExistingPPOpts.PCHThroughHeader.empty()) {
947 // In case the through header is an include, we must add all the includes
948 // to the predefines so the start point can be determined.
949 SuggestedPredefines += "#include \"";
950 SuggestedPredefines += File;
951 SuggestedPredefines += "\"\n";
952 continue;
953 }
954
955 if (File == ExistingPPOpts.ImplicitPCHInclude)
956 continue;
957
958 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
959 continue;
960
961 SuggestedPredefines += "#include \"";
962 SuggestedPredefines += File;
963 SuggestedPredefines += "\"\n";
964 }
965
966 return false;
967}
968
969bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
970 StringRef ModuleFilename,
971 bool ReadMacros, bool Complain,
972 std::string &SuggestedPredefines) {
973 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
974
975 return checkPreprocessorOptions(
976 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
977 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
978 SuggestedPredefines, LangOpts: PP.getLangOpts());
979}
980
981bool SimpleASTReaderListener::ReadPreprocessorOptions(
982 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
983 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
984 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
985 ModuleFilename, ReadMacros, Diags: nullptr,
986 FileMgr&: PP.getFileManager(), SuggestedPredefines,
987 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
988}
989
990/// Check that the specified and the existing module cache paths are equivalent.
991///
992/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
993/// \returns true when the module cache paths differ.
994static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
995 StringRef ExistingSpecificModuleCachePath,
996 StringRef ASTFilename,
997 DiagnosticsEngine *Diags,
998 const LangOptions &LangOpts,
999 const PreprocessorOptions &PPOpts,
1000 const HeaderSearchOptions &HSOpts,
1001 const HeaderSearchOptions &ASTFileHSOpts) {
1002 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
1003 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
1004 ContextHash: std::string(ContextHash));
1005
1006 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
1007 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
1008 return false;
1009 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1010 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
1011 if (EqualOrErr && *EqualOrErr)
1012 return false;
1013 if (Diags) {
1014 // If the module cache arguments provided from the command line are the
1015 // same, the mismatch must come from other arguments of the configuration
1016 // and not directly the cache path.
1017 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1018 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
1019 if (EqualOrErr && *EqualOrErr)
1020 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
1021 else
1022 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
1023 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
1024 << ASTFilename;
1025 }
1026 return true;
1027}
1028
1029bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1030 StringRef ASTFilename,
1031 StringRef ContextHash,
1032 bool Complain) {
1033 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1034 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1035 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1036 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1037 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1038 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1039}
1040
1041void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1042 PP.setCounterValue(Value);
1043}
1044
1045//===----------------------------------------------------------------------===//
1046// AST reader implementation
1047//===----------------------------------------------------------------------===//
1048
1049static uint64_t readULEB(const unsigned char *&P) {
1050 unsigned Length = 0;
1051 const char *Error = nullptr;
1052
1053 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1054 if (Error)
1055 llvm::report_fatal_error(reason: Error);
1056 P += Length;
1057 return Val;
1058}
1059
1060/// Read ULEB-encoded key length and data length.
1061static std::pair<unsigned, unsigned>
1062readULEBKeyDataLength(const unsigned char *&P) {
1063 unsigned KeyLen = readULEB(P);
1064 if ((unsigned)KeyLen != KeyLen)
1065 llvm::report_fatal_error(reason: "key too large");
1066
1067 unsigned DataLen = readULEB(P);
1068 if ((unsigned)DataLen != DataLen)
1069 llvm::report_fatal_error(reason: "data too large");
1070
1071 return std::make_pair(x&: KeyLen, y&: DataLen);
1072}
1073
1074void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1075 bool TakeOwnership) {
1076 DeserializationListener = Listener;
1077 OwnsDeserializationListener = TakeOwnership;
1078}
1079
1080unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1081 return serialization::ComputeHash(Sel);
1082}
1083
1084LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1085 LocalDeclID ID(Value);
1086#ifndef NDEBUG
1087 if (!MF.ModuleOffsetMap.empty())
1088 Reader.ReadModuleOffsetMap(MF);
1089
1090 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1091 unsigned LocalDeclID = ID.getLocalDeclIndex();
1092
1093 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1094
1095 ModuleFile *OwningModuleFile =
1096 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1097 assert(OwningModuleFile);
1098
1099 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1100
1101 if (!ModuleFileIndex)
1102 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1103
1104 assert(LocalDeclID < LocalNumDecls);
1105#endif
1106 (void)Reader;
1107 (void)MF;
1108 return ID;
1109}
1110
1111LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1112 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1113 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1114 return LocalDeclID::get(Reader, MF, Value);
1115}
1116
1117std::pair<unsigned, unsigned>
1118ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1119 return readULEBKeyDataLength(P&: d);
1120}
1121
1122ASTSelectorLookupTrait::internal_key_type
1123ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1124 using namespace llvm::support;
1125
1126 SelectorTable &SelTable = Reader.getContext().Selectors;
1127 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1128 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1129 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1130 if (N == 0)
1131 return SelTable.getNullarySelector(ID: FirstII);
1132 else if (N == 1)
1133 return SelTable.getUnarySelector(ID: FirstII);
1134
1135 SmallVector<const IdentifierInfo *, 16> Args;
1136 Args.push_back(Elt: FirstII);
1137 for (unsigned I = 1; I != N; ++I)
1138 Args.push_back(Elt: Reader.getLocalIdentifier(
1139 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1140
1141 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1142}
1143
1144ASTSelectorLookupTrait::data_type
1145ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1146 unsigned DataLen) {
1147 using namespace llvm::support;
1148
1149 data_type Result;
1150
1151 Result.ID = Reader.getGlobalSelectorID(
1152 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1153 unsigned FullInstanceBits =
1154 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1155 unsigned FullFactoryBits =
1156 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1157 Result.InstanceBits = FullInstanceBits & 0x3;
1158 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1159 Result.FactoryBits = FullFactoryBits & 0x3;
1160 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1161 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1162 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1163
1164 // Load instance methods
1165 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1166 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1167 F, LocalID: LocalDeclID::get(
1168 Reader, MF&: F,
1169 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1170 Result.Instance.push_back(Elt: Method);
1171 }
1172
1173 // Load factory methods
1174 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1175 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1176 F, LocalID: LocalDeclID::get(
1177 Reader, MF&: F,
1178 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1179 Result.Factory.push_back(Elt: Method);
1180 }
1181
1182 return Result;
1183}
1184
1185unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1186 return llvm::djbHash(Buffer: a);
1187}
1188
1189std::pair<unsigned, unsigned>
1190ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1191 return readULEBKeyDataLength(P&: d);
1192}
1193
1194ASTIdentifierLookupTraitBase::internal_key_type
1195ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1196 assert(n >= 2 && d[n-1] == '\0');
1197 return StringRef((const char*) d, n-1);
1198}
1199
1200/// Whether the given identifier is "interesting".
1201static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1202 bool IsModule) {
1203 bool IsInteresting =
1204 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1205 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1206 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1207 return II.hadMacroDefinition() || II.isPoisoned() ||
1208 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1209 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1210 II.getFETokenInfo());
1211}
1212
1213static bool readBit(unsigned &Bits) {
1214 bool Value = Bits & 0x1;
1215 Bits >>= 1;
1216 return Value;
1217}
1218
1219IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1220 using namespace llvm::support;
1221
1222 IdentifierID RawID =
1223 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1224 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1225}
1226
1227static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1228 bool IsModule) {
1229 if (!II.isFromAST()) {
1230 II.setIsFromAST();
1231 if (isInterestingIdentifier(Reader, II, IsModule))
1232 II.setChangedSinceDeserialization();
1233 }
1234}
1235
1236IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1237 const unsigned char* d,
1238 unsigned DataLen) {
1239 using namespace llvm::support;
1240
1241 IdentifierID RawID =
1242 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1243 bool IsInteresting = RawID & 0x01;
1244
1245 DataLen -= sizeof(IdentifierID);
1246
1247 // Wipe out the "is interesting" bit.
1248 RawID = RawID >> 1;
1249
1250 // Build the IdentifierInfo and link the identifier ID with it.
1251 IdentifierInfo *II = KnownII;
1252 if (!II) {
1253 II = &Reader.getIdentifierTable().getOwn(Name: k);
1254 KnownII = II;
1255 }
1256 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1257 markIdentifierFromAST(Reader, II&: *II, IsModule);
1258 Reader.markIdentifierUpToDate(II);
1259
1260 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1261 if (!IsInteresting) {
1262 // For uninteresting identifiers, there's nothing else to do. Just notify
1263 // the reader that we've finished loading this identifier.
1264 Reader.SetIdentifierInfo(ID, II);
1265 return II;
1266 }
1267
1268 unsigned ObjCOrBuiltinID =
1269 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1270 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1271 bool CPlusPlusOperatorKeyword = readBit(Bits);
1272 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1273 bool Poisoned = readBit(Bits);
1274 bool ExtensionToken = readBit(Bits);
1275 bool HasMacroDefinition = readBit(Bits);
1276
1277 assert(Bits == 0 && "Extra bits in the identifier?");
1278 DataLen -= sizeof(uint16_t) * 2;
1279
1280 // Set or check the various bits in the IdentifierInfo structure.
1281 // Token IDs are read-only.
1282 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1283 II->revertTokenIDToIdentifier();
1284 if (!F.isModule())
1285 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1286 assert(II->isExtensionToken() == ExtensionToken &&
1287 "Incorrect extension token flag");
1288 (void)ExtensionToken;
1289 if (Poisoned)
1290 II->setIsPoisoned(true);
1291 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1292 "Incorrect C++ operator keyword flag");
1293 (void)CPlusPlusOperatorKeyword;
1294
1295 // If this identifier has a macro definition, deserialize it or notify the
1296 // visitor the actual definition is in a different module.
1297 if (HasMacroDefinition) {
1298 uint32_t MacroDirectivesOffset =
1299 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1300 DataLen -= 4;
1301
1302 if (MacroDirectivesOffset)
1303 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1304 else
1305 hasMacroDefinitionInDependencies = true;
1306 }
1307
1308 Reader.SetIdentifierInfo(ID, II);
1309
1310 // Read all of the declarations visible at global scope with this
1311 // name.
1312 if (DataLen > 0) {
1313 SmallVector<GlobalDeclID, 4> DeclIDs;
1314 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1315 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1316 F, LocalID: LocalDeclID::get(
1317 Reader, MF&: F,
1318 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1319 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1320 }
1321
1322 return II;
1323}
1324
1325DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1326 : Kind(Name.getNameKind()) {
1327 switch (Kind) {
1328 case DeclarationName::Identifier:
1329 Data = (uint64_t)Name.getAsIdentifierInfo();
1330 break;
1331 case DeclarationName::ObjCZeroArgSelector:
1332 case DeclarationName::ObjCOneArgSelector:
1333 case DeclarationName::ObjCMultiArgSelector:
1334 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1335 break;
1336 case DeclarationName::CXXOperatorName:
1337 Data = Name.getCXXOverloadedOperator();
1338 break;
1339 case DeclarationName::CXXLiteralOperatorName:
1340 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1341 break;
1342 case DeclarationName::CXXDeductionGuideName:
1343 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1344 ->getDeclName().getAsIdentifierInfo();
1345 break;
1346 case DeclarationName::CXXConstructorName:
1347 case DeclarationName::CXXDestructorName:
1348 case DeclarationName::CXXConversionFunctionName:
1349 case DeclarationName::CXXUsingDirective:
1350 Data = 0;
1351 break;
1352 }
1353}
1354
1355unsigned DeclarationNameKey::getHash() const {
1356 llvm::FoldingSetNodeID ID;
1357 ID.AddInteger(I: Kind);
1358
1359 switch (Kind) {
1360 case DeclarationName::Identifier:
1361 case DeclarationName::CXXLiteralOperatorName:
1362 case DeclarationName::CXXDeductionGuideName:
1363 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1364 break;
1365 case DeclarationName::ObjCZeroArgSelector:
1366 case DeclarationName::ObjCOneArgSelector:
1367 case DeclarationName::ObjCMultiArgSelector:
1368 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1369 break;
1370 case DeclarationName::CXXOperatorName:
1371 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1372 break;
1373 case DeclarationName::CXXConstructorName:
1374 case DeclarationName::CXXDestructorName:
1375 case DeclarationName::CXXConversionFunctionName:
1376 case DeclarationName::CXXUsingDirective:
1377 break;
1378 }
1379
1380 return ID.computeStableHash();
1381}
1382
1383ModuleFile *
1384ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1385 using namespace llvm::support;
1386
1387 uint32_t ModuleFileID =
1388 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1389 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1390}
1391
1392std::pair<unsigned, unsigned>
1393ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1394 return readULEBKeyDataLength(P&: d);
1395}
1396
1397DeclarationNameKey
1398ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1399 using namespace llvm::support;
1400
1401 auto Kind = (DeclarationName::NameKind)*d++;
1402 uint64_t Data;
1403 switch (Kind) {
1404 case DeclarationName::Identifier:
1405 case DeclarationName::CXXLiteralOperatorName:
1406 case DeclarationName::CXXDeductionGuideName:
1407 Data = (uint64_t)Reader.getLocalIdentifier(
1408 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1409 break;
1410 case DeclarationName::ObjCZeroArgSelector:
1411 case DeclarationName::ObjCOneArgSelector:
1412 case DeclarationName::ObjCMultiArgSelector:
1413 Data = (uint64_t)Reader
1414 .getLocalSelector(
1415 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1416 .getAsOpaquePtr();
1417 break;
1418 case DeclarationName::CXXOperatorName:
1419 Data = *d++; // OverloadedOperatorKind
1420 break;
1421 case DeclarationName::CXXConstructorName:
1422 case DeclarationName::CXXDestructorName:
1423 case DeclarationName::CXXConversionFunctionName:
1424 case DeclarationName::CXXUsingDirective:
1425 Data = 0;
1426 break;
1427 }
1428
1429 return DeclarationNameKey(Kind, Data);
1430}
1431
1432ASTDeclContextNameLookupTrait::internal_key_type
1433ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1434 return ReadKeyBase(d);
1435}
1436
1437void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1438 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1439 using namespace llvm::support;
1440
1441 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1442 LocalDeclID ID = LocalDeclID::get(
1443 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1444 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1445 }
1446}
1447
1448void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1449 const unsigned char *d,
1450 unsigned DataLen,
1451 data_type_builder &Val) {
1452 ReadDataIntoImpl(d, DataLen, Val);
1453}
1454
1455ModuleLocalNameLookupTrait::hash_value_type
1456ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1457 llvm::FoldingSetNodeID ID;
1458 ID.AddInteger(I: Key.first.getHash());
1459 ID.AddInteger(I: Key.second);
1460 return ID.computeStableHash();
1461}
1462
1463ModuleLocalNameLookupTrait::internal_key_type
1464ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1465 DeclarationNameKey Name(Key.first);
1466
1467 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1468 if (!ModuleHash)
1469 return {Name, 0};
1470
1471 return {Name, *ModuleHash};
1472}
1473
1474ModuleLocalNameLookupTrait::internal_key_type
1475ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1476 DeclarationNameKey Name = ReadKeyBase(d);
1477 unsigned PrimaryModuleHash =
1478 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1479 return {Name, PrimaryModuleHash};
1480}
1481
1482void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1483 const unsigned char *d,
1484 unsigned DataLen,
1485 data_type_builder &Val) {
1486 ReadDataIntoImpl(d, DataLen, Val);
1487}
1488
1489ModuleFile *
1490LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1491 using namespace llvm::support;
1492
1493 uint32_t ModuleFileID =
1494 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1495 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1496}
1497
1498LazySpecializationInfoLookupTrait::internal_key_type
1499LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1500 using namespace llvm::support;
1501 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1502}
1503
1504std::pair<unsigned, unsigned>
1505LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1506 return readULEBKeyDataLength(P&: d);
1507}
1508
1509void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1510 const unsigned char *d,
1511 unsigned DataLen,
1512 data_type_builder &Val) {
1513 using namespace llvm::support;
1514
1515 for (unsigned NumDecls =
1516 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1517 NumDecls; --NumDecls) {
1518 LocalDeclID LocalID = LocalDeclID::get(
1519 Reader, MF&: F,
1520 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1521 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1522 }
1523}
1524
1525bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1526 BitstreamCursor &Cursor,
1527 uint64_t Offset,
1528 DeclContext *DC) {
1529 assert(Offset != 0);
1530
1531 SavedStreamPosition SavedPosition(Cursor);
1532 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1533 Error(Err: std::move(Err));
1534 return true;
1535 }
1536
1537 RecordData Record;
1538 StringRef Blob;
1539 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1540 if (!MaybeCode) {
1541 Error(Err: MaybeCode.takeError());
1542 return true;
1543 }
1544 unsigned Code = MaybeCode.get();
1545
1546 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1547 if (!MaybeRecCode) {
1548 Error(Err: MaybeRecCode.takeError());
1549 return true;
1550 }
1551 unsigned RecCode = MaybeRecCode.get();
1552 if (RecCode != DECL_CONTEXT_LEXICAL) {
1553 Error(Msg: "Expected lexical block");
1554 return true;
1555 }
1556
1557 assert(!isa<TranslationUnitDecl>(DC) &&
1558 "expected a TU_UPDATE_LEXICAL record for TU");
1559 // If we are handling a C++ class template instantiation, we can see multiple
1560 // lexical updates for the same record. It's important that we select only one
1561 // of them, so that field numbering works properly. Just pick the first one we
1562 // see.
1563 auto &Lex = LexicalDecls[DC];
1564 if (!Lex.first) {
1565 Lex = std::make_pair(
1566 x: &M, y: llvm::ArrayRef(
1567 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1568 Blob.size() / sizeof(DeclID)));
1569 }
1570 DC->setHasExternalLexicalStorage(true);
1571 return false;
1572}
1573
1574bool ASTReader::ReadVisibleDeclContextStorage(
1575 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1576 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1577 assert(Offset != 0);
1578
1579 SavedStreamPosition SavedPosition(Cursor);
1580 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1581 Error(Err: std::move(Err));
1582 return true;
1583 }
1584
1585 RecordData Record;
1586 StringRef Blob;
1587 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1588 if (!MaybeCode) {
1589 Error(Err: MaybeCode.takeError());
1590 return true;
1591 }
1592 unsigned Code = MaybeCode.get();
1593
1594 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1595 if (!MaybeRecCode) {
1596 Error(Err: MaybeRecCode.takeError());
1597 return true;
1598 }
1599 unsigned RecCode = MaybeRecCode.get();
1600 switch (VisibleKind) {
1601 case VisibleDeclContextStorageKind::GenerallyVisible:
1602 if (RecCode != DECL_CONTEXT_VISIBLE) {
1603 Error(Msg: "Expected visible lookup table block");
1604 return true;
1605 }
1606 break;
1607 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1608 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1609 Error(Msg: "Expected module local visible lookup table block");
1610 return true;
1611 }
1612 break;
1613 case VisibleDeclContextStorageKind::TULocalVisible:
1614 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1615 Error(Msg: "Expected TU local lookup table block");
1616 return true;
1617 }
1618 break;
1619 }
1620
1621 // We can't safely determine the primary context yet, so delay attaching the
1622 // lookup table until we're done with recursive deserialization.
1623 auto *Data = (const unsigned char*)Blob.data();
1624 switch (VisibleKind) {
1625 case VisibleDeclContextStorageKind::GenerallyVisible:
1626 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1627 break;
1628 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1629 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1630 break;
1631 case VisibleDeclContextStorageKind::TULocalVisible:
1632 if (M.Kind == MK_MainFile)
1633 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1634 break;
1635 }
1636 return false;
1637}
1638
1639void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1640 ModuleFile &M, bool IsPartial) {
1641 D = D->getCanonicalDecl();
1642 auto &SpecLookups =
1643 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1644 SpecLookups[D].Table.add(File: &M, Data,
1645 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1646}
1647
1648bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1649 uint64_t Offset, Decl *D, bool IsPartial) {
1650 assert(Offset != 0);
1651
1652 SavedStreamPosition SavedPosition(Cursor);
1653 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1654 Error(Err: std::move(Err));
1655 return true;
1656 }
1657
1658 RecordData Record;
1659 StringRef Blob;
1660 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1661 if (!MaybeCode) {
1662 Error(Err: MaybeCode.takeError());
1663 return true;
1664 }
1665 unsigned Code = MaybeCode.get();
1666
1667 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1668 if (!MaybeRecCode) {
1669 Error(Err: MaybeRecCode.takeError());
1670 return true;
1671 }
1672 unsigned RecCode = MaybeRecCode.get();
1673 if (RecCode != DECL_SPECIALIZATIONS &&
1674 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1675 Error(Msg: "Expected decl specs block");
1676 return true;
1677 }
1678
1679 auto *Data = (const unsigned char *)Blob.data();
1680 AddSpecializations(D, Data, M, IsPartial);
1681 return false;
1682}
1683
1684void ASTReader::Error(StringRef Msg) const {
1685 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1686 if (PP.getLangOpts().Modules &&
1687 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1688 Diag(DiagID: diag::note_module_cache_path)
1689 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1690 }
1691}
1692
1693void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1694 StringRef Arg3) const {
1695 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1696}
1697
1698namespace {
1699struct AlreadyReportedDiagnosticError
1700 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1701 static char ID;
1702
1703 void log(raw_ostream &OS) const override {
1704 llvm_unreachable("reporting an already-reported diagnostic error");
1705 }
1706
1707 std::error_code convertToErrorCode() const override {
1708 return llvm::inconvertibleErrorCode();
1709 }
1710};
1711
1712char AlreadyReportedDiagnosticError::ID = 0;
1713} // namespace
1714
1715void ASTReader::Error(llvm::Error &&Err) const {
1716 handleAllErrors(
1717 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1718 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1719}
1720
1721//===----------------------------------------------------------------------===//
1722// Source Manager Deserialization
1723//===----------------------------------------------------------------------===//
1724
1725/// Read the line table in the source manager block.
1726void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1727 unsigned Idx = 0;
1728 LineTableInfo &LineTable = SourceMgr.getLineTable();
1729
1730 // Parse the file names
1731 std::map<int, int> FileIDs;
1732 FileIDs[-1] = -1; // For unspecified filenames.
1733 for (unsigned I = 0; Record[Idx]; ++I) {
1734 // Extract the file name
1735 auto Filename = ReadPath(F, Record, Idx);
1736 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1737 }
1738 ++Idx;
1739
1740 // Parse the line entries
1741 std::vector<LineEntry> Entries;
1742 while (Idx < Record.size()) {
1743 FileID FID = ReadFileID(F, Record, Idx);
1744
1745 // Extract the line entries
1746 unsigned NumEntries = Record[Idx++];
1747 assert(NumEntries && "no line entries for file ID");
1748 Entries.clear();
1749 Entries.reserve(n: NumEntries);
1750 for (unsigned I = 0; I != NumEntries; ++I) {
1751 unsigned FileOffset = Record[Idx++];
1752 unsigned LineNo = Record[Idx++];
1753 int FilenameID = FileIDs[Record[Idx++]];
1754 SrcMgr::CharacteristicKind FileKind
1755 = (SrcMgr::CharacteristicKind)Record[Idx++];
1756 unsigned IncludeOffset = Record[Idx++];
1757 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1758 FileKind, IncludeOffset));
1759 }
1760 LineTable.AddEntry(FID, Entries);
1761 }
1762}
1763
1764/// Read a source manager block
1765llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1766 using namespace SrcMgr;
1767
1768 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1769
1770 // Set the source-location entry cursor to the current position in
1771 // the stream. This cursor will be used to read the contents of the
1772 // source manager block initially, and then lazily read
1773 // source-location entries as needed.
1774 SLocEntryCursor = F.Stream;
1775
1776 // The stream itself is going to skip over the source manager block.
1777 if (llvm::Error Err = F.Stream.SkipBlock())
1778 return Err;
1779
1780 // Enter the source manager block.
1781 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1782 return Err;
1783 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1784
1785 RecordData Record;
1786 while (true) {
1787 Expected<llvm::BitstreamEntry> MaybeE =
1788 SLocEntryCursor.advanceSkippingSubblocks();
1789 if (!MaybeE)
1790 return MaybeE.takeError();
1791 llvm::BitstreamEntry E = MaybeE.get();
1792
1793 switch (E.Kind) {
1794 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1795 case llvm::BitstreamEntry::Error:
1796 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1797 Fmt: "malformed block record in AST file");
1798 case llvm::BitstreamEntry::EndBlock:
1799 return llvm::Error::success();
1800 case llvm::BitstreamEntry::Record:
1801 // The interesting case.
1802 break;
1803 }
1804
1805 // Read a record.
1806 Record.clear();
1807 StringRef Blob;
1808 Expected<unsigned> MaybeRecord =
1809 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1810 if (!MaybeRecord)
1811 return MaybeRecord.takeError();
1812 switch (MaybeRecord.get()) {
1813 default: // Default behavior: ignore.
1814 break;
1815
1816 case SM_SLOC_FILE_ENTRY:
1817 case SM_SLOC_BUFFER_ENTRY:
1818 case SM_SLOC_EXPANSION_ENTRY:
1819 // Once we hit one of the source location entries, we're done.
1820 return llvm::Error::success();
1821 }
1822 }
1823}
1824
1825llvm::Expected<SourceLocation::UIntTy>
1826ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1827 BitstreamCursor &Cursor = F->SLocEntryCursor;
1828 SavedStreamPosition SavedPosition(Cursor);
1829 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1830 F->SLocEntryOffsets[Index]))
1831 return std::move(Err);
1832
1833 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1834 if (!MaybeEntry)
1835 return MaybeEntry.takeError();
1836
1837 llvm::BitstreamEntry Entry = MaybeEntry.get();
1838 if (Entry.Kind != llvm::BitstreamEntry::Record)
1839 return llvm::createStringError(
1840 EC: std::errc::illegal_byte_sequence,
1841 Fmt: "incorrectly-formatted source location entry in AST file");
1842
1843 RecordData Record;
1844 StringRef Blob;
1845 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1846 if (!MaybeSLOC)
1847 return MaybeSLOC.takeError();
1848
1849 switch (MaybeSLOC.get()) {
1850 default:
1851 return llvm::createStringError(
1852 EC: std::errc::illegal_byte_sequence,
1853 Fmt: "incorrectly-formatted source location entry in AST file");
1854 case SM_SLOC_FILE_ENTRY:
1855 case SM_SLOC_BUFFER_ENTRY:
1856 case SM_SLOC_EXPANSION_ENTRY:
1857 return F->SLocEntryBaseOffset + Record[0];
1858 }
1859}
1860
1861int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1862 auto SLocMapI =
1863 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1864 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1865 "Corrupted global sloc offset map");
1866 ModuleFile *F = SLocMapI->second;
1867
1868 bool Invalid = false;
1869
1870 auto It = llvm::upper_bound(
1871 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1872 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1873 int ID = F->SLocEntryBaseID + LocalIndex;
1874 std::size_t Index = -ID - 2;
1875 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1876 assert(!SourceMgr.SLocEntryLoaded[Index]);
1877 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1878 if (!MaybeEntryOffset) {
1879 Error(Err: MaybeEntryOffset.takeError());
1880 Invalid = true;
1881 return true;
1882 }
1883 SourceMgr.LoadedSLocEntryTable[Index] =
1884 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1885 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1886 }
1887 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1888 });
1889
1890 if (Invalid)
1891 return 0;
1892
1893 // The iterator points to the first entry with start offset greater than the
1894 // offset of interest. The previous entry must contain the offset of interest.
1895 return F->SLocEntryBaseID + *std::prev(x: It);
1896}
1897
1898bool ASTReader::ReadSLocEntry(int ID) {
1899 if (ID == 0)
1900 return false;
1901
1902 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1903 Error(Msg: "source location entry ID out-of-range for AST file");
1904 return true;
1905 }
1906
1907 // Local helper to read the (possibly-compressed) buffer data following the
1908 // entry record.
1909 auto ReadBuffer = [this](
1910 BitstreamCursor &SLocEntryCursor,
1911 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1912 RecordData Record;
1913 StringRef Blob;
1914 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1915 if (!MaybeCode) {
1916 Error(Err: MaybeCode.takeError());
1917 return nullptr;
1918 }
1919 unsigned Code = MaybeCode.get();
1920
1921 Expected<unsigned> MaybeRecCode =
1922 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1923 if (!MaybeRecCode) {
1924 Error(Err: MaybeRecCode.takeError());
1925 return nullptr;
1926 }
1927 unsigned RecCode = MaybeRecCode.get();
1928
1929 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1930 // Inspect the first byte to differentiate zlib (\x78) and zstd
1931 // (little-endian 0xFD2FB528).
1932 const llvm::compression::Format F =
1933 Blob.size() > 0 && Blob.data()[0] == 0x78
1934 ? llvm::compression::Format::Zlib
1935 : llvm::compression::Format::Zstd;
1936 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1937 Error(Msg: Reason);
1938 return nullptr;
1939 }
1940 SmallVector<uint8_t, 0> Decompressed;
1941 if (llvm::Error E = llvm::compression::decompress(
1942 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1943 Error(Msg: "could not decompress embedded file contents: " +
1944 llvm::toString(E: std::move(E)));
1945 return nullptr;
1946 }
1947 return llvm::MemoryBuffer::getMemBufferCopy(
1948 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1949 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1950 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1951 } else {
1952 Error(Msg: "AST record has invalid code");
1953 return nullptr;
1954 }
1955 };
1956
1957 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1958 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1959 BitNo: F->SLocEntryOffsetsBase +
1960 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1961 Error(Err: std::move(Err));
1962 return true;
1963 }
1964
1965 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1966 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1967
1968 ++NumSLocEntriesRead;
1969 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1970 if (!MaybeEntry) {
1971 Error(Err: MaybeEntry.takeError());
1972 return true;
1973 }
1974 llvm::BitstreamEntry Entry = MaybeEntry.get();
1975
1976 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1977 Error(Msg: "incorrectly-formatted source location entry in AST file");
1978 return true;
1979 }
1980
1981 RecordData Record;
1982 StringRef Blob;
1983 Expected<unsigned> MaybeSLOC =
1984 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1985 if (!MaybeSLOC) {
1986 Error(Err: MaybeSLOC.takeError());
1987 return true;
1988 }
1989 switch (MaybeSLOC.get()) {
1990 default:
1991 Error(Msg: "incorrectly-formatted source location entry in AST file");
1992 return true;
1993
1994 case SM_SLOC_FILE_ENTRY: {
1995 // We will detect whether a file changed and return 'Failure' for it, but
1996 // we will also try to fail gracefully by setting up the SLocEntry.
1997 unsigned InputID = Record[4];
1998 InputFile IF = getInputFile(F&: *F, ID: InputID);
1999 OptionalFileEntryRef File = IF.getFile();
2000 bool OverriddenBuffer = IF.isOverridden();
2001
2002 // Note that we only check if a File was returned. If it was out-of-date
2003 // we have complained but we will continue creating a FileID to recover
2004 // gracefully.
2005 if (!File)
2006 return true;
2007
2008 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2009 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
2010 // This is the module's main file.
2011 IncludeLoc = getImportLocation(F);
2012 }
2013 SrcMgr::CharacteristicKind
2014 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2015 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
2016 LoadedOffset: BaseOffset + Record[0]);
2017 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2018 FileInfo.NumCreatedFIDs = Record[5];
2019 if (Record[3])
2020 FileInfo.setHasLineDirectives();
2021
2022 unsigned NumFileDecls = Record[7];
2023 if (NumFileDecls && ContextObj) {
2024 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
2025 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
2026 FileDeclIDs[FID] =
2027 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
2028 }
2029
2030 const SrcMgr::ContentCache &ContentCache =
2031 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2032 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2033 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2034 !ContentCache.getBufferIfLoaded()) {
2035 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2036 if (!Buffer)
2037 return true;
2038 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2039 }
2040
2041 break;
2042 }
2043
2044 case SM_SLOC_BUFFER_ENTRY: {
2045 const char *Name = Blob.data();
2046 unsigned Offset = Record[0];
2047 SrcMgr::CharacteristicKind
2048 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2049 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2050 if (IncludeLoc.isInvalid() && F->isModule()) {
2051 IncludeLoc = getImportLocation(F);
2052 }
2053
2054 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2055 if (!Buffer)
2056 return true;
2057 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2058 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2059 if (Record[3]) {
2060 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2061 FileInfo.setHasLineDirectives();
2062 }
2063 break;
2064 }
2065
2066 case SM_SLOC_EXPANSION_ENTRY: {
2067 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2068 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2069 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2070 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2071 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2072 LoadedOffset: BaseOffset + Record[0]);
2073 break;
2074 }
2075 }
2076
2077 return false;
2078}
2079
2080std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2081 if (ID == 0)
2082 return std::make_pair(x: SourceLocation(), y: "");
2083
2084 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2085 Error(Msg: "source location entry ID out-of-range for AST file");
2086 return std::make_pair(x: SourceLocation(), y: "");
2087 }
2088
2089 // Find which module file this entry lands in.
2090 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2091 if (!M->isModule())
2092 return std::make_pair(x: SourceLocation(), y: "");
2093
2094 // FIXME: Can we map this down to a particular submodule? That would be
2095 // ideal.
2096 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2097}
2098
2099/// Find the location where the module F is imported.
2100SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2101 if (F->ImportLoc.isValid())
2102 return F->ImportLoc;
2103
2104 // Otherwise we have a PCH. It's considered to be "imported" at the first
2105 // location of its includer.
2106 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2107 // Main file is the importer.
2108 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2109 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2110 }
2111 return F->ImportedBy[0]->FirstLoc;
2112}
2113
2114/// Enter a subblock of the specified BlockID with the specified cursor. Read
2115/// the abbreviations that are at the top of the block and then leave the cursor
2116/// pointing into the block.
2117llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2118 unsigned BlockID,
2119 uint64_t *StartOfBlockOffset) {
2120 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2121 return Err;
2122
2123 if (StartOfBlockOffset)
2124 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2125
2126 while (true) {
2127 uint64_t Offset = Cursor.GetCurrentBitNo();
2128 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2129 if (!MaybeCode)
2130 return MaybeCode.takeError();
2131 unsigned Code = MaybeCode.get();
2132
2133 // We expect all abbrevs to be at the start of the block.
2134 if (Code != llvm::bitc::DEFINE_ABBREV) {
2135 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2136 return Err;
2137 return llvm::Error::success();
2138 }
2139 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2140 return Err;
2141 }
2142}
2143
2144Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2145 unsigned &Idx) {
2146 Token Tok;
2147 Tok.startToken();
2148 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2149 Tok.setKind((tok::TokenKind)Record[Idx++]);
2150 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2151
2152 if (Tok.isAnnotation()) {
2153 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2154 switch (Tok.getKind()) {
2155 case tok::annot_pragma_loop_hint: {
2156 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2157 Info->PragmaName = ReadToken(M, Record, Idx);
2158 Info->Option = ReadToken(M, Record, Idx);
2159 unsigned NumTokens = Record[Idx++];
2160 SmallVector<Token, 4> Toks;
2161 Toks.reserve(N: NumTokens);
2162 for (unsigned I = 0; I < NumTokens; ++I)
2163 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2164 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2165 Tok.setAnnotationValue(static_cast<void *>(Info));
2166 break;
2167 }
2168 case tok::annot_pragma_pack: {
2169 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2170 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2171 auto SlotLabel = ReadString(Record, Idx);
2172 Info->SlotLabel =
2173 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2174 Info->Alignment = ReadToken(M, Record, Idx);
2175 Tok.setAnnotationValue(static_cast<void *>(Info));
2176 break;
2177 }
2178 // Some annotation tokens do not use the PtrData field.
2179 case tok::annot_pragma_openmp:
2180 case tok::annot_pragma_openmp_end:
2181 case tok::annot_pragma_unused:
2182 case tok::annot_pragma_openacc:
2183 case tok::annot_pragma_openacc_end:
2184 case tok::annot_repl_input_end:
2185 break;
2186 default:
2187 llvm_unreachable("missing deserialization code for annotation token");
2188 }
2189 } else {
2190 Tok.setLength(Record[Idx++]);
2191 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2192 Tok.setIdentifierInfo(II);
2193 }
2194 return Tok;
2195}
2196
2197MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2198 BitstreamCursor &Stream = F.MacroCursor;
2199
2200 // Keep track of where we are in the stream, then jump back there
2201 // after reading this macro.
2202 SavedStreamPosition SavedPosition(Stream);
2203
2204 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2205 // FIXME this drops errors on the floor.
2206 consumeError(Err: std::move(Err));
2207 return nullptr;
2208 }
2209 RecordData Record;
2210 SmallVector<IdentifierInfo*, 16> MacroParams;
2211 MacroInfo *Macro = nullptr;
2212 llvm::MutableArrayRef<Token> MacroTokens;
2213
2214 while (true) {
2215 // Advance to the next record, but if we get to the end of the block, don't
2216 // pop it (removing all the abbreviations from the cursor) since we want to
2217 // be able to reseek within the block and read entries.
2218 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2219 Expected<llvm::BitstreamEntry> MaybeEntry =
2220 Stream.advanceSkippingSubblocks(Flags);
2221 if (!MaybeEntry) {
2222 Error(Err: MaybeEntry.takeError());
2223 return Macro;
2224 }
2225 llvm::BitstreamEntry Entry = MaybeEntry.get();
2226
2227 switch (Entry.Kind) {
2228 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2229 case llvm::BitstreamEntry::Error:
2230 Error(Msg: "malformed block record in AST file");
2231 return Macro;
2232 case llvm::BitstreamEntry::EndBlock:
2233 return Macro;
2234 case llvm::BitstreamEntry::Record:
2235 // The interesting case.
2236 break;
2237 }
2238
2239 // Read a record.
2240 Record.clear();
2241 PreprocessorRecordTypes RecType;
2242 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2243 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2244 else {
2245 Error(Err: MaybeRecType.takeError());
2246 return Macro;
2247 }
2248 switch (RecType) {
2249 case PP_MODULE_MACRO:
2250 case PP_MACRO_DIRECTIVE_HISTORY:
2251 return Macro;
2252
2253 case PP_MACRO_OBJECT_LIKE:
2254 case PP_MACRO_FUNCTION_LIKE: {
2255 // If we already have a macro, that means that we've hit the end
2256 // of the definition of the macro we were looking for. We're
2257 // done.
2258 if (Macro)
2259 return Macro;
2260
2261 unsigned NextIndex = 1; // Skip identifier ID.
2262 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2263 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2264 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2265 MI->setIsUsed(Record[NextIndex++]);
2266 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2267 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2268 PPAllocator&: PP.getPreprocessorAllocator());
2269 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2270 // Decode function-like macro info.
2271 bool isC99VarArgs = Record[NextIndex++];
2272 bool isGNUVarArgs = Record[NextIndex++];
2273 bool hasCommaPasting = Record[NextIndex++];
2274 MacroParams.clear();
2275 unsigned NumArgs = Record[NextIndex++];
2276 for (unsigned i = 0; i != NumArgs; ++i)
2277 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2278
2279 // Install function-like macro info.
2280 MI->setIsFunctionLike();
2281 if (isC99VarArgs) MI->setIsC99Varargs();
2282 if (isGNUVarArgs) MI->setIsGNUVarargs();
2283 if (hasCommaPasting) MI->setHasCommaPasting();
2284 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2285 }
2286
2287 // Remember that we saw this macro last so that we add the tokens that
2288 // form its body to it.
2289 Macro = MI;
2290
2291 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2292 Record[NextIndex]) {
2293 // We have a macro definition. Register the association
2294 PreprocessedEntityID
2295 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2296 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2297 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2298 PreprocessingRecord::PPEntityID PPID =
2299 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2300 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2301 Val: PPRec.getPreprocessedEntity(PPID));
2302 if (PPDef)
2303 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2304 }
2305
2306 ++NumMacrosRead;
2307 break;
2308 }
2309
2310 case PP_TOKEN: {
2311 // If we see a TOKEN before a PP_MACRO_*, then the file is
2312 // erroneous, just pretend we didn't see this.
2313 if (!Macro) break;
2314 if (MacroTokens.empty()) {
2315 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2316 return Macro;
2317 }
2318
2319 unsigned Idx = 0;
2320 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2321 MacroTokens = MacroTokens.drop_front();
2322 break;
2323 }
2324 }
2325 }
2326}
2327
2328PreprocessedEntityID
2329ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2330 PreprocessedEntityID LocalID) const {
2331 if (!M.ModuleOffsetMap.empty())
2332 ReadModuleOffsetMap(F&: M);
2333
2334 unsigned ModuleFileIndex = LocalID >> 32;
2335 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2336 ModuleFile *MF =
2337 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2338 assert(MF && "malformed identifier ID encoding?");
2339
2340 if (!ModuleFileIndex) {
2341 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2342 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2343 }
2344
2345 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2346}
2347
2348OptionalFileEntryRef
2349HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2350 FileManager &FileMgr = Reader.getFileManager();
2351 if (!Key.Imported)
2352 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2353
2354 auto Resolved =
2355 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2356 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2357}
2358
2359unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2360 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2361 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2362 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2363 return llvm::xxh3_64bits(data: buf);
2364}
2365
2366HeaderFileInfoTrait::internal_key_type
2367HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2368 internal_key_type ikey = {.Size: ekey.getSize(),
2369 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2370 .Filename: ekey.getName(), /*Imported*/ false};
2371 return ikey;
2372}
2373
2374bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2375 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2376 return false;
2377
2378 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2379 return true;
2380
2381 // Determine whether the actual files are equivalent.
2382 OptionalFileEntryRef FEA = getFile(Key: a);
2383 OptionalFileEntryRef FEB = getFile(Key: b);
2384 return FEA && FEA == FEB;
2385}
2386
2387std::pair<unsigned, unsigned>
2388HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2389 return readULEBKeyDataLength(P&: d);
2390}
2391
2392HeaderFileInfoTrait::internal_key_type
2393HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2394 using namespace llvm::support;
2395
2396 internal_key_type ikey;
2397 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2398 ikey.ModTime =
2399 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2400 ikey.Filename = (const char *)d;
2401 ikey.Imported = true;
2402 return ikey;
2403}
2404
2405HeaderFileInfoTrait::data_type
2406HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2407 unsigned DataLen) {
2408 using namespace llvm::support;
2409
2410 const unsigned char *End = d + DataLen;
2411 HeaderFileInfo HFI;
2412 unsigned Flags = *d++;
2413
2414 OptionalFileEntryRef FE;
2415 bool Included = (Flags >> 6) & 0x01;
2416 if (Included)
2417 if ((FE = getFile(Key: key)))
2418 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2419 // deserialize this header file info again.
2420 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2421
2422 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2423 HFI.isImport |= (Flags >> 5) & 0x01;
2424 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2425 HFI.DirInfo = (Flags >> 1) & 0x07;
2426 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2427 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2428
2429 assert((End - d) % 4 == 0 &&
2430 "Wrong data length in HeaderFileInfo deserialization");
2431 while (d != End) {
2432 uint32_t LocalSMID =
2433 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2434 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2435 LocalSMID >>= 3;
2436
2437 // This header is part of a module. Associate it with the module to enable
2438 // implicit module import.
2439 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2440 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2441 ModuleMap &ModMap =
2442 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2443
2444 if (FE || (FE = getFile(Key: key))) {
2445 // FIXME: NameAsWritten
2446 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2447 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2448 }
2449 HFI.mergeModuleMembership(Role: HeaderRole);
2450 }
2451
2452 // This HeaderFileInfo was externally loaded.
2453 HFI.External = true;
2454 HFI.IsValid = true;
2455 return HFI;
2456}
2457
2458void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2459 uint32_t MacroDirectivesOffset) {
2460 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2461 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2462}
2463
2464void ASTReader::ReadDefinedMacros() {
2465 // Note that we are loading defined macros.
2466 Deserializing Macros(this);
2467
2468 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2469 BitstreamCursor &MacroCursor = I.MacroCursor;
2470
2471 // If there was no preprocessor block, skip this file.
2472 if (MacroCursor.getBitcodeBytes().empty())
2473 continue;
2474
2475 BitstreamCursor Cursor = MacroCursor;
2476 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2477 Error(Err: std::move(Err));
2478 return;
2479 }
2480
2481 RecordData Record;
2482 while (true) {
2483 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2484 if (!MaybeE) {
2485 Error(Err: MaybeE.takeError());
2486 return;
2487 }
2488 llvm::BitstreamEntry E = MaybeE.get();
2489
2490 switch (E.Kind) {
2491 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2492 case llvm::BitstreamEntry::Error:
2493 Error(Msg: "malformed block record in AST file");
2494 return;
2495 case llvm::BitstreamEntry::EndBlock:
2496 goto NextCursor;
2497
2498 case llvm::BitstreamEntry::Record: {
2499 Record.clear();
2500 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2501 if (!MaybeRecord) {
2502 Error(Err: MaybeRecord.takeError());
2503 return;
2504 }
2505 switch (MaybeRecord.get()) {
2506 default: // Default behavior: ignore.
2507 break;
2508
2509 case PP_MACRO_OBJECT_LIKE:
2510 case PP_MACRO_FUNCTION_LIKE: {
2511 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2512 if (II->isOutOfDate())
2513 updateOutOfDateIdentifier(II: *II);
2514 break;
2515 }
2516
2517 case PP_TOKEN:
2518 // Ignore tokens.
2519 break;
2520 }
2521 break;
2522 }
2523 }
2524 }
2525 NextCursor: ;
2526 }
2527}
2528
2529namespace {
2530
2531 /// Visitor class used to look up identifirs in an AST file.
2532 class IdentifierLookupVisitor {
2533 StringRef Name;
2534 unsigned NameHash;
2535 unsigned PriorGeneration;
2536 unsigned &NumIdentifierLookups;
2537 unsigned &NumIdentifierLookupHits;
2538 IdentifierInfo *Found = nullptr;
2539
2540 public:
2541 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2542 unsigned &NumIdentifierLookups,
2543 unsigned &NumIdentifierLookupHits)
2544 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2545 PriorGeneration(PriorGeneration),
2546 NumIdentifierLookups(NumIdentifierLookups),
2547 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2548
2549 bool operator()(ModuleFile &M) {
2550 // If we've already searched this module file, skip it now.
2551 if (M.Generation <= PriorGeneration)
2552 return true;
2553
2554 ASTIdentifierLookupTable *IdTable
2555 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2556 if (!IdTable)
2557 return false;
2558
2559 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2560 Found);
2561 ++NumIdentifierLookups;
2562 ASTIdentifierLookupTable::iterator Pos =
2563 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2564 if (Pos == IdTable->end())
2565 return false;
2566
2567 // Dereferencing the iterator has the effect of building the
2568 // IdentifierInfo node and populating it with the various
2569 // declarations it needs.
2570 ++NumIdentifierLookupHits;
2571 Found = *Pos;
2572 if (Trait.hasMoreInformationInDependencies()) {
2573 // Look for the identifier in extra modules as they contain more info.
2574 return false;
2575 }
2576 return true;
2577 }
2578
2579 // Retrieve the identifier info found within the module
2580 // files.
2581 IdentifierInfo *getIdentifierInfo() const { return Found; }
2582 };
2583
2584} // namespace
2585
2586void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2587 // Note that we are loading an identifier.
2588 Deserializing AnIdentifier(this);
2589
2590 unsigned PriorGeneration = 0;
2591 if (getContext().getLangOpts().Modules)
2592 PriorGeneration = IdentifierGeneration[&II];
2593
2594 // If there is a global index, look there first to determine which modules
2595 // provably do not have any results for this identifier.
2596 GlobalModuleIndex::HitSet Hits;
2597 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2598 if (!loadGlobalIndex()) {
2599 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2600 HitsPtr = &Hits;
2601 }
2602 }
2603
2604 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2605 NumIdentifierLookups,
2606 NumIdentifierLookupHits);
2607 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2608 markIdentifierUpToDate(II: &II);
2609}
2610
2611void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2612 if (!II)
2613 return;
2614
2615 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2616
2617 // Update the generation for this identifier.
2618 if (getContext().getLangOpts().Modules)
2619 IdentifierGeneration[II] = getGeneration();
2620}
2621
2622MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2623 unsigned &Idx) {
2624 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2625 uint64_t LocalIndex = Record[Idx++];
2626 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2627}
2628
2629void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2630 const PendingMacroInfo &PMInfo) {
2631 ModuleFile &M = *PMInfo.M;
2632
2633 BitstreamCursor &Cursor = M.MacroCursor;
2634 SavedStreamPosition SavedPosition(Cursor);
2635 if (llvm::Error Err =
2636 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2637 Error(Err: std::move(Err));
2638 return;
2639 }
2640
2641 struct ModuleMacroRecord {
2642 SubmoduleID SubModID;
2643 MacroInfo *MI;
2644 SmallVector<SubmoduleID, 8> Overrides;
2645 };
2646 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2647
2648 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2649 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2650 // macro histroy.
2651 RecordData Record;
2652 while (true) {
2653 Expected<llvm::BitstreamEntry> MaybeEntry =
2654 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2655 if (!MaybeEntry) {
2656 Error(Err: MaybeEntry.takeError());
2657 return;
2658 }
2659 llvm::BitstreamEntry Entry = MaybeEntry.get();
2660
2661 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2662 Error(Msg: "malformed block record in AST file");
2663 return;
2664 }
2665
2666 Record.clear();
2667 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2668 if (!MaybePP) {
2669 Error(Err: MaybePP.takeError());
2670 return;
2671 }
2672 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2673 case PP_MACRO_DIRECTIVE_HISTORY:
2674 break;
2675
2676 case PP_MODULE_MACRO: {
2677 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2678 auto &Info = ModuleMacros.back();
2679 unsigned Idx = 0;
2680 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2681 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2682 for (int I = Idx, N = Record.size(); I != N; ++I)
2683 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2684 continue;
2685 }
2686
2687 default:
2688 Error(Msg: "malformed block record in AST file");
2689 return;
2690 }
2691
2692 // We found the macro directive history; that's the last record
2693 // for this macro.
2694 break;
2695 }
2696
2697 // Module macros are listed in reverse dependency order.
2698 {
2699 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2700 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2701 for (auto &MMR : ModuleMacros) {
2702 Overrides.clear();
2703 for (unsigned ModID : MMR.Overrides) {
2704 Module *Mod = getSubmodule(GlobalID: ModID);
2705 auto *Macro = PP.getModuleMacro(Mod, II);
2706 assert(Macro && "missing definition for overridden macro");
2707 Overrides.push_back(Elt: Macro);
2708 }
2709
2710 bool Inserted = false;
2711 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2712 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2713 }
2714 }
2715
2716 // Don't read the directive history for a module; we don't have anywhere
2717 // to put it.
2718 if (M.isModule())
2719 return;
2720
2721 // Deserialize the macro directives history in reverse source-order.
2722 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2723 unsigned Idx = 0, N = Record.size();
2724 while (Idx < N) {
2725 MacroDirective *MD = nullptr;
2726 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2727 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2728 switch (K) {
2729 case MacroDirective::MD_Define: {
2730 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2731 MD = PP.AllocateDefMacroDirective(MI, Loc);
2732 break;
2733 }
2734 case MacroDirective::MD_Undefine:
2735 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2736 break;
2737 case MacroDirective::MD_Visibility:
2738 bool isPublic = Record[Idx++];
2739 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2740 break;
2741 }
2742
2743 if (!Latest)
2744 Latest = MD;
2745 if (Earliest)
2746 Earliest->setPrevious(MD);
2747 Earliest = MD;
2748 }
2749
2750 if (Latest)
2751 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2752}
2753
2754bool ASTReader::shouldDisableValidationForFile(
2755 const serialization::ModuleFile &M) const {
2756 if (DisableValidationKind == DisableValidationForModuleKind::None)
2757 return false;
2758
2759 // If a PCH is loaded and validation is disabled for PCH then disable
2760 // validation for the PCH and the modules it loads.
2761 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2762
2763 switch (K) {
2764 case MK_MainFile:
2765 case MK_Preamble:
2766 case MK_PCH:
2767 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2768 case MK_ImplicitModule:
2769 case MK_ExplicitModule:
2770 case MK_PrebuiltModule:
2771 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2772 }
2773
2774 return false;
2775}
2776
2777static std::pair<StringRef, StringRef>
2778getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2779 const StringRef InputBlob) {
2780 uint16_t AsRequestedLength = Record[7];
2781 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2782 InputBlob.substr(Start: AsRequestedLength)};
2783}
2784
2785InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2786 // If this ID is bogus, just return an empty input file.
2787 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2788 return InputFileInfo();
2789
2790 // If we've already loaded this input file, return it.
2791 if (F.InputFileInfosLoaded[ID - 1].isValid())
2792 return F.InputFileInfosLoaded[ID - 1];
2793
2794 // Go find this input file.
2795 BitstreamCursor &Cursor = F.InputFilesCursor;
2796 SavedStreamPosition SavedPosition(Cursor);
2797 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2798 F.InputFileOffsets[ID - 1])) {
2799 // FIXME this drops errors on the floor.
2800 consumeError(Err: std::move(Err));
2801 }
2802
2803 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2804 if (!MaybeCode) {
2805 // FIXME this drops errors on the floor.
2806 consumeError(Err: MaybeCode.takeError());
2807 }
2808 unsigned Code = MaybeCode.get();
2809 RecordData Record;
2810 StringRef Blob;
2811
2812 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2813 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2814 "invalid record type for input file");
2815 else {
2816 // FIXME this drops errors on the floor.
2817 consumeError(Err: Maybe.takeError());
2818 }
2819
2820 assert(Record[0] == ID && "Bogus stored ID or offset");
2821 InputFileInfo R;
2822 R.StoredSize = static_cast<off_t>(Record[1]);
2823 R.StoredTime = static_cast<time_t>(Record[2]);
2824 R.Overridden = static_cast<bool>(Record[3]);
2825 R.Transient = static_cast<bool>(Record[4]);
2826 R.TopLevel = static_cast<bool>(Record[5]);
2827 R.ModuleMap = static_cast<bool>(Record[6]);
2828 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2829 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2830 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2831 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2832 ? UnresolvedFilenameAsRequested
2833 : UnresolvedFilename;
2834
2835 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2836 if (!MaybeEntry) // FIXME this drops errors on the floor.
2837 consumeError(Err: MaybeEntry.takeError());
2838 llvm::BitstreamEntry Entry = MaybeEntry.get();
2839 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2840 "expected record type for input file hash");
2841
2842 Record.clear();
2843 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2844 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2845 "invalid record type for input file hash");
2846 else {
2847 // FIXME this drops errors on the floor.
2848 consumeError(Err: Maybe.takeError());
2849 }
2850 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2851 static_cast<uint64_t>(Record[0]);
2852
2853 // Note that we've loaded this input file info.
2854 F.InputFileInfosLoaded[ID - 1] = R;
2855 return R;
2856}
2857
2858static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2859InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2860 // If this ID is bogus, just return an empty input file.
2861 if (ID == 0 || ID > F.InputFilesLoaded.size())
2862 return InputFile();
2863
2864 // If we've already loaded this input file, return it.
2865 if (F.InputFilesLoaded[ID-1].getFile())
2866 return F.InputFilesLoaded[ID-1];
2867
2868 if (F.InputFilesLoaded[ID-1].isNotFound())
2869 return InputFile();
2870
2871 // Go find this input file.
2872 BitstreamCursor &Cursor = F.InputFilesCursor;
2873 SavedStreamPosition SavedPosition(Cursor);
2874 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2875 F.InputFileOffsets[ID - 1])) {
2876 // FIXME this drops errors on the floor.
2877 consumeError(Err: std::move(Err));
2878 }
2879
2880 InputFileInfo FI = getInputFileInfo(F, ID);
2881 off_t StoredSize = FI.StoredSize;
2882 time_t StoredTime = FI.StoredTime;
2883 bool Overridden = FI.Overridden;
2884 bool Transient = FI.Transient;
2885 auto Filename =
2886 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2887 uint64_t StoredContentHash = FI.ContentHash;
2888
2889 // For standard C++ modules, we don't need to check the inputs.
2890 bool SkipChecks = F.StandardCXXModule;
2891
2892 const HeaderSearchOptions &HSOpts =
2893 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2894
2895 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2896 // modules.
2897 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2898 SkipChecks = false;
2899 Overridden = false;
2900 }
2901
2902 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2903
2904 // For an overridden file, create a virtual file with the stored
2905 // size/timestamp.
2906 if ((Overridden || Transient || SkipChecks) && !File)
2907 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2908
2909 if (!File) {
2910 if (Complain) {
2911 std::string ErrorStr = "could not find file '";
2912 ErrorStr += *Filename;
2913 ErrorStr += "' referenced by AST file '";
2914 ErrorStr += F.FileName.str();
2915 ErrorStr += "'";
2916 Error(Msg: ErrorStr);
2917 }
2918 // Record that we didn't find the file.
2919 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2920 return InputFile();
2921 }
2922
2923 // Check if there was a request to override the contents of the file
2924 // that was part of the precompiled header. Overriding such a file
2925 // can lead to problems when lexing using the source locations from the
2926 // PCH.
2927 SourceManager &SM = getSourceManager();
2928 // FIXME: Reject if the overrides are different.
2929 if ((!Overridden && !Transient) && !SkipChecks &&
2930 SM.isFileOverridden(File: *File)) {
2931 if (Complain)
2932 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2933
2934 // After emitting the diagnostic, bypass the overriding file to recover
2935 // (this creates a separate FileEntry).
2936 File = SM.bypassFileContentsOverride(File: *File);
2937 if (!File) {
2938 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2939 return InputFile();
2940 }
2941 }
2942
2943 auto HasInputContentChanged = [&](Change OriginalChange) {
2944 assert(ValidateASTInputFilesContent &&
2945 "We should only check the content of the inputs with "
2946 "ValidateASTInputFilesContent enabled.");
2947
2948 if (StoredContentHash == 0)
2949 return OriginalChange;
2950
2951 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2952 if (!MemBuffOrError) {
2953 if (!Complain)
2954 return OriginalChange;
2955 std::string ErrorStr = "could not get buffer for file '";
2956 ErrorStr += File->getName();
2957 ErrorStr += "'";
2958 Error(Msg: ErrorStr);
2959 return OriginalChange;
2960 }
2961
2962 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2963 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2964 return Change{.Kind: Change::None};
2965
2966 return Change{.Kind: Change::Content};
2967 };
2968 auto HasInputFileChanged = [&]() {
2969 if (StoredSize != File->getSize())
2970 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2971 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2972 StoredTime != File->getModificationTime()) {
2973 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2974 .New: File->getModificationTime()};
2975
2976 // In case the modification time changes but not the content,
2977 // accept the cached file as legit.
2978 if (ValidateASTInputFilesContent)
2979 return HasInputContentChanged(MTimeChange);
2980
2981 return MTimeChange;
2982 }
2983 return Change{.Kind: Change::None};
2984 };
2985
2986 bool IsOutOfDate = false;
2987 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2988 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2989 // enabled, it is better to check the contents of the inputs. Since we can't
2990 // get correct modified time information for inputs from overriden inputs.
2991 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2992 F.StandardCXXModule && FileChange.Kind == Change::None)
2993 FileChange = HasInputContentChanged(FileChange);
2994
2995 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2996 // it is better to check the content of the input files because we cannot rely
2997 // on the file modification time, which will be the same (zero) for these
2998 // files.
2999 if (!StoredTime && ValidateASTInputFilesContent &&
3000 FileChange.Kind == Change::None)
3001 FileChange = HasInputContentChanged(FileChange);
3002
3003 // For an overridden file, there is nothing to validate.
3004 if (!Overridden && FileChange.Kind != Change::None) {
3005 if (Complain) {
3006 // Build a list of the PCH imports that got us here (in reverse).
3007 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
3008 while (!ImportStack.back()->ImportedBy.empty())
3009 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
3010
3011 // The top-level AST file is stale.
3012 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
3013 Diag(DiagID: diag::err_fe_ast_file_modified)
3014 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
3015 << TopLevelASTFileName;
3016 Diag(DiagID: diag::note_fe_ast_file_modified)
3017 << FileChange.Kind << (FileChange.Old && FileChange.New)
3018 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
3019 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3020 if (getModuleManager()
3021 .getModuleCache()
3022 .getInMemoryModuleCache()
3023 .isPCMFinal(Filename: F.FileName))
3024 Diag(DiagID: diag::note_fe_ast_file_modified_finalized) << F.ModuleName;
3025
3026 // Print the import stack.
3027 if (ImportStack.size() > 1) {
3028 Diag(DiagID: diag::note_ast_file_required_by)
3029 << *Filename << ImportStack[0]->FileName;
3030 for (unsigned I = 1; I < ImportStack.size(); ++I)
3031 Diag(DiagID: diag::note_ast_file_required_by)
3032 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3033 }
3034
3035 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3036 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3037 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3038 << F.InputFilesValidationStatus;
3039 }
3040
3041 IsOutOfDate = true;
3042 }
3043 // FIXME: If the file is overridden and we've already opened it,
3044 // issue an error (or split it into a separate FileEntry).
3045
3046 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3047
3048 // Note that we've loaded this input file.
3049 F.InputFilesLoaded[ID-1] = IF;
3050 return IF;
3051}
3052
3053ASTReader::TemporarilyOwnedStringRef
3054ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3055 ModuleFile &ModF) {
3056 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3057}
3058
3059ASTReader::TemporarilyOwnedStringRef
3060ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3061 StringRef Prefix) {
3062 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3063
3064 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3065 Path == "<built-in>" || Path == "<command line>")
3066 return {Path, Buf};
3067
3068 Buf.clear();
3069 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3070 StringRef ResolvedPath{Buf.data(), Buf.size()};
3071 return {ResolvedPath, Buf};
3072}
3073
3074std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3075 StringRef P,
3076 ModuleFile &ModF) {
3077 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3078}
3079
3080std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3081 StringRef P,
3082 StringRef Prefix) {
3083 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3084 return ResolvedPath->str();
3085}
3086
3087static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3088 switch (ARR) {
3089 case ASTReader::Failure: return true;
3090 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3091 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3092 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3093 case ASTReader::ConfigurationMismatch:
3094 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3095 case ASTReader::HadErrors: return true;
3096 case ASTReader::Success: return false;
3097 }
3098
3099 llvm_unreachable("unknown ASTReadResult");
3100}
3101
3102ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3103 BitstreamCursor &Stream, StringRef Filename,
3104 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3105 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3106 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3107 // FIXME this drops errors on the floor.
3108 consumeError(Err: std::move(Err));
3109 return Failure;
3110 }
3111
3112 // Read all of the records in the options block.
3113 RecordData Record;
3114 ASTReadResult Result = Success;
3115 while (true) {
3116 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3117 if (!MaybeEntry) {
3118 // FIXME this drops errors on the floor.
3119 consumeError(Err: MaybeEntry.takeError());
3120 return Failure;
3121 }
3122 llvm::BitstreamEntry Entry = MaybeEntry.get();
3123
3124 switch (Entry.Kind) {
3125 case llvm::BitstreamEntry::Error:
3126 case llvm::BitstreamEntry::SubBlock:
3127 return Failure;
3128
3129 case llvm::BitstreamEntry::EndBlock:
3130 return Result;
3131
3132 case llvm::BitstreamEntry::Record:
3133 // The interesting case.
3134 break;
3135 }
3136
3137 // Read and process a record.
3138 Record.clear();
3139 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3140 if (!MaybeRecordType) {
3141 // FIXME this drops errors on the floor.
3142 consumeError(Err: MaybeRecordType.takeError());
3143 return Failure;
3144 }
3145 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3146 case LANGUAGE_OPTIONS: {
3147 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3148 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3149 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3150 Result = ConfigurationMismatch;
3151 break;
3152 }
3153
3154 case CODEGEN_OPTIONS: {
3155 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3156 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3157 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3158 Result = ConfigurationMismatch;
3159 break;
3160 }
3161
3162 case TARGET_OPTIONS: {
3163 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3164 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3165 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3166 Result = ConfigurationMismatch;
3167 break;
3168 }
3169
3170 case FILE_SYSTEM_OPTIONS: {
3171 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3172 if (!AllowCompatibleConfigurationMismatch &&
3173 ParseFileSystemOptions(Record, Complain, Listener))
3174 Result = ConfigurationMismatch;
3175 break;
3176 }
3177
3178 case HEADER_SEARCH_OPTIONS: {
3179 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3180 if (!AllowCompatibleConfigurationMismatch &&
3181 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3182 Result = ConfigurationMismatch;
3183 break;
3184 }
3185
3186 case PREPROCESSOR_OPTIONS:
3187 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3188 if (!AllowCompatibleConfigurationMismatch &&
3189 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3190 SuggestedPredefines))
3191 Result = ConfigurationMismatch;
3192 break;
3193 }
3194 }
3195}
3196
3197/// Returns {build-session validation applies, MF was validated this session}.
3198static std::pair<bool, bool>
3199wasValidatedInBuildSession(const ModuleFile &MF,
3200 const HeaderSearchOptions &HSOpts) {
3201 const bool EnablesBSValidation =
3202 HSOpts.ModulesValidateOncePerBuildSession && MF.Kind == MK_ImplicitModule;
3203 const bool WasValidated =
3204 EnablesBSValidation &&
3205 MF.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp;
3206 return {EnablesBSValidation, WasValidated};
3207}
3208
3209ASTReader::RelocationResult
3210ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3211 // Don't emit module relocation errors if we have -fno-validate-pch.
3212 const bool IgnoreError =
3213 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3214 DisableValidationForModuleKind::Module);
3215
3216 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3217 return {std::nullopt, IgnoreError};
3218
3219 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3220
3221 if (!DirectoryCheck &&
3222 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3223 return {std::nullopt, IgnoreError};
3224
3225 const HeaderSearchOptions &HSOpts =
3226 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3227
3228 // When only validating modules once per build session,
3229 // Skip check if the timestamp is up to date or module was built in same build
3230 // session.
3231 auto [EnablesBSValidation, WasValidated] =
3232 wasValidatedInBuildSession(MF: F, HSOpts);
3233 const bool SkipModuleLookup =
3234 !PP.getPreprocessorOpts().ModulesForceRedundantLookup &&
3235 (WasValidated ||
3236 (EnablesBSValidation &&
3237 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp));
3238
3239 if (SkipModuleLookup)
3240 return {std::nullopt, IgnoreError};
3241
3242 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3243
3244 // If we've already loaded a module map file covering this module, we may
3245 // have a better path for it (relative to the current build if doing directory
3246 // check).
3247 Module *M = PP.getHeaderSearchInfo().lookupModule(
3248 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3249 /*AllowSearch=*/true,
3250 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3251
3252 return {M, IgnoreError};
3253}
3254
3255ASTReader::ASTReadResult
3256ASTReader::ReadControlBlock(ModuleFile &F,
3257 SmallVectorImpl<ImportedModule> &Loaded,
3258 const ModuleFile *ImportedBy,
3259 unsigned ClientLoadCapabilities) {
3260 BitstreamCursor &Stream = F.Stream;
3261
3262 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3263 Error(Err: std::move(Err));
3264 return Failure;
3265 }
3266
3267 // Lambda to read the unhashed control block the first time it's called.
3268 //
3269 // For PCM files, the unhashed control block cannot be read until after the
3270 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3271 // need to look ahead before reading the IMPORTS record. For consistency,
3272 // this block is always read somehow (see BitstreamEntry::EndBlock).
3273 bool HasReadUnhashedControlBlock = false;
3274 auto readUnhashedControlBlockOnce = [&]() {
3275 if (!HasReadUnhashedControlBlock) {
3276 HasReadUnhashedControlBlock = true;
3277 if (ASTReadResult Result =
3278 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3279 return Result;
3280 }
3281 return Success;
3282 };
3283
3284 bool DisableValidation = shouldDisableValidationForFile(M: F);
3285
3286 // Read all of the records and blocks in the control block.
3287 RecordData Record;
3288 unsigned NumInputs = 0;
3289 unsigned NumUserInputs = 0;
3290 StringRef BaseDirectoryAsWritten;
3291 while (true) {
3292 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3293 if (!MaybeEntry) {
3294 Error(Err: MaybeEntry.takeError());
3295 return Failure;
3296 }
3297 llvm::BitstreamEntry Entry = MaybeEntry.get();
3298
3299 switch (Entry.Kind) {
3300 case llvm::BitstreamEntry::Error:
3301 Error(Msg: "malformed block record in AST file");
3302 return Failure;
3303 case llvm::BitstreamEntry::EndBlock: {
3304 // Validate the module before returning. This call catches an AST with
3305 // no module name and no imports.
3306 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3307 return Result;
3308
3309 // Validate input files.
3310 const HeaderSearchOptions &HSOpts =
3311 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3312
3313 // All user input files reside at the index range [0, NumUserInputs), and
3314 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3315 // loaded module files, ignore missing inputs.
3316 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3317 F.Kind != MK_PrebuiltModule) {
3318 bool Complain =
3319 !canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities);
3320
3321 // If we are reading a module, we will create a verification timestamp,
3322 // so we verify all input files. Otherwise, verify only user input
3323 // files.
3324
3325 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3326 F.InputFilesValidationStatus = ValidateSystemInputs
3327 ? InputFilesValidation::AllFiles
3328 : InputFilesValidation::UserFiles;
3329 auto [_, WasValidated] = wasValidatedInBuildSession(MF: F, HSOpts);
3330 if (WasValidated) {
3331 N = ForceValidateUserInputs ? NumUserInputs : 0;
3332 F.InputFilesValidationStatus =
3333 ForceValidateUserInputs
3334 ? InputFilesValidation::UserFiles
3335 : InputFilesValidation::SkippedInBuildSession;
3336 }
3337
3338 if (N != 0)
3339 Diag(DiagID: diag::remark_module_validation)
3340 << N << F.ModuleName << F.FileName;
3341
3342 for (unsigned I = 0; I < N; ++I) {
3343 InputFile IF = getInputFile(F, ID: I+1, Complain);
3344 if (!IF.getFile() || IF.isOutOfDate())
3345 return OutOfDate;
3346 }
3347 } else {
3348 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3349 }
3350
3351 if (Listener)
3352 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind, DirectlyImported: F.isDirectlyImported());
3353
3354 if (Listener && Listener->needsInputFileVisitation()) {
3355 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3356 : NumUserInputs;
3357 for (unsigned I = 0; I < N; ++I) {
3358 bool IsSystem = I >= NumUserInputs;
3359 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3360 auto FilenameAsRequested = ResolveImportedPath(
3361 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3362 Listener->visitInputFile(
3363 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3364 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3365 }
3366 }
3367
3368 return Success;
3369 }
3370
3371 case llvm::BitstreamEntry::SubBlock:
3372 switch (Entry.ID) {
3373 case INPUT_FILES_BLOCK_ID:
3374 F.InputFilesCursor = Stream;
3375 if (llvm::Error Err = Stream.SkipBlock()) {
3376 Error(Err: std::move(Err));
3377 return Failure;
3378 }
3379 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3380 Error(Msg: "malformed block record in AST file");
3381 return Failure;
3382 }
3383 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3384 continue;
3385
3386 case OPTIONS_BLOCK_ID:
3387 // If we're reading the first module for this group, check its options
3388 // are compatible with ours. For modules it imports, no further checking
3389 // is required, because we checked them when we built it.
3390 if (Listener && !ImportedBy) {
3391 // Should we allow the configuration of the module file to differ from
3392 // the configuration of the current translation unit in a compatible
3393 // way?
3394 //
3395 // FIXME: Allow this for files explicitly specified with -include-pch.
3396 bool AllowCompatibleConfigurationMismatch =
3397 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3398
3399 ASTReadResult Result =
3400 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3401 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3402 SuggestedPredefines);
3403 if (Result == Failure) {
3404 Error(Msg: "malformed block record in AST file");
3405 return Result;
3406 }
3407
3408 if (DisableValidation ||
3409 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3410 Result = Success;
3411
3412 // If we can't load the module, exit early since we likely
3413 // will rebuild the module anyway. The stream may be in the
3414 // middle of a block.
3415 if (Result != Success)
3416 return Result;
3417 } else if (llvm::Error Err = Stream.SkipBlock()) {
3418 Error(Err: std::move(Err));
3419 return Failure;
3420 }
3421 continue;
3422
3423 default:
3424 if (llvm::Error Err = Stream.SkipBlock()) {
3425 Error(Err: std::move(Err));
3426 return Failure;
3427 }
3428 continue;
3429 }
3430
3431 case llvm::BitstreamEntry::Record:
3432 // The interesting case.
3433 break;
3434 }
3435
3436 // Read and process a record.
3437 Record.clear();
3438 StringRef Blob;
3439 Expected<unsigned> MaybeRecordType =
3440 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3441 if (!MaybeRecordType) {
3442 Error(Err: MaybeRecordType.takeError());
3443 return Failure;
3444 }
3445 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3446 case METADATA: {
3447 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3448 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3449 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3450 : diag::err_ast_file_version_too_new)
3451 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3452 return VersionMismatch;
3453 }
3454
3455 bool hasErrors = Record[7];
3456 if (hasErrors && !DisableValidation) {
3457 // If requested by the caller and the module hasn't already been read
3458 // or compiled, mark modules on error as out-of-date.
3459 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3460 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3461 return OutOfDate;
3462
3463 if (!AllowASTWithCompilerErrors) {
3464 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3465 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3466 return HadErrors;
3467 }
3468 }
3469 if (hasErrors) {
3470 Diags.ErrorOccurred = true;
3471 Diags.UncompilableErrorOccurred = true;
3472 Diags.UnrecoverableErrorOccurred = true;
3473 }
3474
3475 F.RelocatablePCH = Record[4];
3476 // Relative paths in a relocatable PCH are relative to our sysroot.
3477 if (F.RelocatablePCH)
3478 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3479
3480 F.StandardCXXModule = Record[5];
3481
3482 F.HasTimestamps = Record[6];
3483
3484 const std::string &CurBranch = getClangFullRepositoryVersion();
3485 StringRef ASTBranch = Blob;
3486 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3487 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3488 Diag(DiagID: diag::err_ast_file_different_branch)
3489 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3490 << CurBranch;
3491 return VersionMismatch;
3492 }
3493 break;
3494 }
3495
3496 case IMPORT: {
3497 // Validate the AST before processing any imports (otherwise, untangling
3498 // them can be error-prone and expensive). A module will have a name and
3499 // will already have been validated, but this catches the PCH case.
3500 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3501 return Result;
3502
3503 unsigned Idx = 0;
3504 // Read information about the AST file.
3505 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3506
3507 // The import location will be the local one for now; we will adjust
3508 // all import locations of module imports after the global source
3509 // location info are setup, in ReadAST.
3510 auto [ImportLoc, ImportModuleFileIndex] =
3511 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3512 // The import location must belong to the current module file itself.
3513 assert(ImportModuleFileIndex == 0);
3514
3515 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3516
3517 bool IsImportingStdCXXModule = Record[Idx++];
3518
3519 off_t StoredSize = 0;
3520 time_t StoredModTime = 0;
3521 unsigned FileNameKind = 0;
3522 ASTFileSignature StoredSignature;
3523 ModuleFileName ImportedFile;
3524 std::string StoredFile;
3525 bool IgnoreImportedByNote = false;
3526
3527 // For prebuilt and explicit modules first consult the file map for
3528 // an override. Note that here we don't search prebuilt module
3529 // directories if we're not importing standard c++ module, only the
3530 // explicit name to file mappings. Also, we will still verify the
3531 // size/signature making sure it is essentially the same file but
3532 // perhaps in a different location.
3533 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3534 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3535 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3536
3537 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3538 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3539 return Missing;
3540 }
3541
3542 if (!IsImportingStdCXXModule) {
3543 StoredSize = (off_t)Record[Idx++];
3544 StoredModTime = (time_t)Record[Idx++];
3545 FileNameKind = (unsigned)Record[Idx++];
3546
3547 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3548 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3549 Last: SignatureBytes.end());
3550 Blob = Blob.substr(Start: ASTFileSignature::size);
3551
3552 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3553 if (ImportedFile.empty()) {
3554 ImportedFile = ModuleFileName::makeFromRaw(Name: StoredFile, RawKind: FileNameKind);
3555 } else if (!getDiags().isIgnored(
3556 DiagID: diag::warn_module_file_mapping_mismatch,
3557 Loc: CurrentImportLoc)) {
3558 auto ImportedFileRef =
3559 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3560 auto StoredFileRef =
3561 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3562 if ((ImportedFileRef && StoredFileRef) &&
3563 (*ImportedFileRef != *StoredFileRef)) {
3564 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3565 << ImportedFile << StoredFile;
3566 Diag(DiagID: diag::note_module_file_imported_by)
3567 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3568 IgnoreImportedByNote = true;
3569 }
3570 }
3571 }
3572
3573 // If our client can't cope with us being out of date, we can't cope with
3574 // our dependency being missing.
3575 unsigned Capabilities = ClientLoadCapabilities;
3576 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3577 Capabilities &= ~ARR_Missing;
3578
3579 // Load the AST file.
3580 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3581 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3582 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3583
3584 // Check the AST we just read from ImportedFile contains a different
3585 // module than we expected (ImportedName). This can occur for C++20
3586 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3587 if (IsImportingStdCXXModule) {
3588 if (const auto *Imported =
3589 getModuleManager().lookupByFileName(FileName: ImportedFile);
3590 Imported != nullptr && Imported->ModuleName != ImportedName) {
3591 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3592 Result = Missing;
3593 }
3594 }
3595
3596 // If we diagnosed a problem, produce a backtrace.
3597 bool recompilingFinalized = Result == OutOfDate &&
3598 (Capabilities & ARR_OutOfDate) &&
3599 getModuleManager()
3600 .getModuleCache()
3601 .getInMemoryModuleCache()
3602 .isPCMFinal(Filename: F.FileName);
3603 if (!IgnoreImportedByNote &&
3604 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3605 Diag(DiagID: diag::note_module_file_imported_by)
3606 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3607
3608 switch (Result) {
3609 case Failure: return Failure;
3610 // If we have to ignore the dependency, we'll have to ignore this too.
3611 case Missing:
3612 case OutOfDate: return OutOfDate;
3613 case VersionMismatch: return VersionMismatch;
3614 case ConfigurationMismatch: return ConfigurationMismatch;
3615 case HadErrors: return HadErrors;
3616 case Success: break;
3617 }
3618 break;
3619 }
3620
3621 case ORIGINAL_FILE:
3622 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3623 F.ActualOriginalSourceFileName = std::string(Blob);
3624 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3625 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3626 break;
3627
3628 case ORIGINAL_FILE_ID:
3629 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3630 break;
3631
3632 case MODULE_NAME:
3633 F.ModuleName = std::string(Blob);
3634 Diag(DiagID: diag::remark_module_import)
3635 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3636 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3637 if (Listener)
3638 Listener->ReadModuleName(ModuleName: F.ModuleName);
3639
3640 // Validate the AST as soon as we have a name so we can exit early on
3641 // failure.
3642 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3643 return Result;
3644
3645 break;
3646
3647 case MODULE_DIRECTORY: {
3648 // Save the BaseDirectory as written in the PCM for computing the module
3649 // filename for the ModuleCache.
3650 BaseDirectoryAsWritten = Blob;
3651 assert(!F.ModuleName.empty() &&
3652 "MODULE_DIRECTORY found before MODULE_NAME");
3653 F.BaseDirectory = std::string(Blob);
3654
3655 auto [MaybeM, IgnoreError] =
3656 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3657 if (!MaybeM.has_value())
3658 break;
3659
3660 Module *M = MaybeM.value();
3661 if (!M || !M->Directory)
3662 break;
3663 if (IgnoreError) {
3664 F.BaseDirectory = std::string(M->Directory->getName());
3665 break;
3666 }
3667 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3668 break;
3669
3670 // If we're implicitly loading a module, the base directory can't
3671 // change between the build and use.
3672 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3673 if (BuildDir && (*BuildDir == M->Directory)) {
3674 F.BaseDirectory = std::string(M->Directory->getName());
3675 break;
3676 }
3677 Diag(DiagID: diag::remark_module_relocated)
3678 << F.ModuleName << Blob << M->Directory->getName();
3679
3680 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3681 Diag(DiagID: diag::err_imported_module_relocated)
3682 << F.ModuleName << Blob << M->Directory->getName();
3683 return OutOfDate;
3684 }
3685
3686 case MODULE_MAP_FILE:
3687 if (ASTReadResult Result =
3688 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3689 return Result;
3690 break;
3691
3692 case INPUT_FILE_OFFSETS:
3693 NumInputs = Record[0];
3694 NumUserInputs = Record[1];
3695 F.InputFileOffsets =
3696 (const llvm::support::unaligned_uint64_t *)Blob.data();
3697 F.InputFilesLoaded.resize(new_size: NumInputs);
3698 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3699 F.NumUserInputFiles = NumUserInputs;
3700 break;
3701 }
3702 }
3703}
3704
3705llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3706 unsigned ClientLoadCapabilities) {
3707 BitstreamCursor &Stream = F.Stream;
3708
3709 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3710 return Err;
3711 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3712
3713 // Read all of the records and blocks for the AST file.
3714 RecordData Record;
3715 while (true) {
3716 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3717 if (!MaybeEntry)
3718 return MaybeEntry.takeError();
3719 llvm::BitstreamEntry Entry = MaybeEntry.get();
3720
3721 switch (Entry.Kind) {
3722 case llvm::BitstreamEntry::Error:
3723 return llvm::createStringError(
3724 EC: std::errc::illegal_byte_sequence,
3725 Fmt: "error at end of module block in AST file");
3726 case llvm::BitstreamEntry::EndBlock:
3727 // Outside of C++, we do not store a lookup map for the translation unit.
3728 // Instead, mark it as needing a lookup map to be built if this module
3729 // contains any declarations lexically within it (which it always does!).
3730 // This usually has no cost, since we very rarely need the lookup map for
3731 // the translation unit outside C++.
3732 if (ASTContext *Ctx = ContextObj) {
3733 DeclContext *DC = Ctx->getTranslationUnitDecl();
3734 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3735 DC->setMustBuildLookupTable();
3736 }
3737
3738 return llvm::Error::success();
3739 case llvm::BitstreamEntry::SubBlock:
3740 switch (Entry.ID) {
3741 case DECLTYPES_BLOCK_ID:
3742 // We lazily load the decls block, but we want to set up the
3743 // DeclsCursor cursor to point into it. Clone our current bitcode
3744 // cursor to it, enter the block and read the abbrevs in that block.
3745 // With the main cursor, we just skip over it.
3746 F.DeclsCursor = Stream;
3747 if (llvm::Error Err = Stream.SkipBlock())
3748 return Err;
3749 if (llvm::Error Err = ReadBlockAbbrevs(
3750 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3751 return Err;
3752 break;
3753
3754 case PREPROCESSOR_BLOCK_ID:
3755 F.MacroCursor = Stream;
3756 if (!PP.getExternalSource())
3757 PP.setExternalSource(this);
3758
3759 if (llvm::Error Err = Stream.SkipBlock())
3760 return Err;
3761 if (llvm::Error Err =
3762 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3763 return Err;
3764 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3765 break;
3766
3767 case PREPROCESSOR_DETAIL_BLOCK_ID:
3768 F.PreprocessorDetailCursor = Stream;
3769
3770 if (llvm::Error Err = Stream.SkipBlock()) {
3771 return Err;
3772 }
3773 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3774 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3775 return Err;
3776 F.PreprocessorDetailStartOffset
3777 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3778
3779 if (!PP.getPreprocessingRecord())
3780 PP.createPreprocessingRecord();
3781 if (!PP.getPreprocessingRecord()->getExternalSource())
3782 PP.getPreprocessingRecord()->SetExternalSource(*this);
3783 break;
3784
3785 case SOURCE_MANAGER_BLOCK_ID:
3786 if (llvm::Error Err = ReadSourceManagerBlock(F))
3787 return Err;
3788 break;
3789
3790 case SUBMODULE_BLOCK_ID:
3791 F.SubmodulesCursor = Stream;
3792 if (llvm::Error Err = Stream.SkipBlock())
3793 return Err;
3794 if (llvm::Error Err =
3795 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3796 return Err;
3797 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3798 break;
3799
3800 case COMMENTS_BLOCK_ID: {
3801 BitstreamCursor C = Stream;
3802
3803 if (llvm::Error Err = Stream.SkipBlock())
3804 return Err;
3805 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3806 return Err;
3807 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3808 break;
3809 }
3810
3811 default:
3812 if (llvm::Error Err = Stream.SkipBlock())
3813 return Err;
3814 break;
3815 }
3816 continue;
3817
3818 case llvm::BitstreamEntry::Record:
3819 // The interesting case.
3820 break;
3821 }
3822
3823 // Read and process a record.
3824 Record.clear();
3825 StringRef Blob;
3826 Expected<unsigned> MaybeRecordType =
3827 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3828 if (!MaybeRecordType)
3829 return MaybeRecordType.takeError();
3830 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3831
3832 // If we're not loading an AST context, we don't care about most records.
3833 if (!ContextObj) {
3834 switch (RecordType) {
3835 case IDENTIFIER_TABLE:
3836 case IDENTIFIER_OFFSET:
3837 case INTERESTING_IDENTIFIERS:
3838 case STATISTICS:
3839 case PP_ASSUME_NONNULL_LOC:
3840 case PP_CONDITIONAL_STACK:
3841 case PP_COUNTER_VALUE:
3842 case SOURCE_LOCATION_OFFSETS:
3843 case MODULE_OFFSET_MAP:
3844 case SOURCE_MANAGER_LINE_TABLE:
3845 case PPD_ENTITIES_OFFSETS:
3846 case HEADER_SEARCH_TABLE:
3847 case IMPORTED_MODULES:
3848 case MACRO_OFFSET:
3849 case SUBMODULE_METADATA:
3850 break;
3851 default:
3852 continue;
3853 }
3854 }
3855
3856 switch (RecordType) {
3857 default: // Default behavior: ignore.
3858 break;
3859
3860 case SUBMODULE_METADATA: {
3861 F.BaseSubmoduleID = getTotalNumSubmodules();
3862 F.LocalNumSubmodules = Record[0];
3863 F.LocalBaseSubmoduleID = Record[1];
3864 F.LocalTopLevelSubmoduleID = Record[2];
3865 F.SubmoduleOffsets =
3866 (const llvm::support::unaligned_uint64_t *)Blob.data();
3867 if (F.LocalNumSubmodules > 0) {
3868 // Introduce the global -> local mapping for submodules within this
3869 // module.
3870 GlobalSubmoduleMap.insert(
3871 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3872
3873 // Introduce the local -> global mapping for submodules within this
3874 // module.
3875 F.SubmoduleRemap.insertOrReplace(
3876 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3877 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3878
3879 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3880 }
3881
3882 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3883 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3884 };
3885
3886 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3887 // If we already knew about this module, make sure to bring all
3888 // submodules up to date.
3889 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3890 unsigned LocalID =
3891 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3892 ReadSubmodule(LocalID);
3893 }
3894 } else {
3895 // If we didn't know this module, we loaded it transitively. Deserialize
3896 // just the top-level module to register it with ModuleMap, but load the
3897 // rest lazily.
3898 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3899 }
3900
3901 break;
3902 }
3903
3904 case TYPE_OFFSET: {
3905 if (F.LocalNumTypes != 0)
3906 return llvm::createStringError(
3907 EC: std::errc::illegal_byte_sequence,
3908 Fmt: "duplicate TYPE_OFFSET record in AST file");
3909 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3910 F.LocalNumTypes = Record[0];
3911 F.BaseTypeIndex = getTotalNumTypes();
3912
3913 if (F.LocalNumTypes > 0)
3914 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3915
3916 break;
3917 }
3918
3919 case DECL_OFFSET: {
3920 if (F.LocalNumDecls != 0)
3921 return llvm::createStringError(
3922 EC: std::errc::illegal_byte_sequence,
3923 Fmt: "duplicate DECL_OFFSET record in AST file");
3924 F.DeclOffsets = (const DeclOffset *)Blob.data();
3925 F.LocalNumDecls = Record[0];
3926 F.BaseDeclIndex = getTotalNumDecls();
3927
3928 if (F.LocalNumDecls > 0)
3929 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3930
3931 break;
3932 }
3933
3934 case TU_UPDATE_LEXICAL: {
3935 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3936 LexicalContents Contents(
3937 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3938 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3939 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3940 TU->setHasExternalLexicalStorage(true);
3941 break;
3942 }
3943
3944 case UPDATE_VISIBLE: {
3945 unsigned Idx = 0;
3946 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3947 auto *Data = (const unsigned char*)Blob.data();
3948 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3949 // If we've already loaded the decl, perform the updates when we finish
3950 // loading this block.
3951 if (Decl *D = GetExistingDecl(ID))
3952 PendingUpdateRecords.push_back(
3953 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3954 break;
3955 }
3956
3957 case UPDATE_MODULE_LOCAL_VISIBLE: {
3958 unsigned Idx = 0;
3959 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3960 auto *Data = (const unsigned char *)Blob.data();
3961 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3962 // If we've already loaded the decl, perform the updates when we finish
3963 // loading this block.
3964 if (Decl *D = GetExistingDecl(ID))
3965 PendingUpdateRecords.push_back(
3966 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3967 break;
3968 }
3969
3970 case UPDATE_TU_LOCAL_VISIBLE: {
3971 if (F.Kind != MK_MainFile)
3972 break;
3973 unsigned Idx = 0;
3974 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3975 auto *Data = (const unsigned char *)Blob.data();
3976 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3977 // If we've already loaded the decl, perform the updates when we finish
3978 // loading this block.
3979 if (Decl *D = GetExistingDecl(ID))
3980 PendingUpdateRecords.push_back(
3981 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3982 break;
3983 }
3984
3985 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3986 unsigned Idx = 0;
3987 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3988 auto *Data = (const unsigned char *)Blob.data();
3989 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3990 // If we've already loaded the decl, perform the updates when we finish
3991 // loading this block.
3992 if (Decl *D = GetExistingDecl(ID))
3993 PendingUpdateRecords.push_back(
3994 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3995 break;
3996 }
3997
3998 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3999 unsigned Idx = 0;
4000 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
4001 auto *Data = (const unsigned char *)Blob.data();
4002 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
4003 // If we've already loaded the decl, perform the updates when we finish
4004 // loading this block.
4005 if (Decl *D = GetExistingDecl(ID))
4006 PendingUpdateRecords.push_back(
4007 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4008 break;
4009 }
4010
4011 case IDENTIFIER_TABLE:
4012 F.IdentifierTableData =
4013 reinterpret_cast<const unsigned char *>(Blob.data());
4014 if (Record[0]) {
4015 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
4016 Buckets: F.IdentifierTableData + Record[0],
4017 Payload: F.IdentifierTableData + sizeof(uint32_t),
4018 Base: F.IdentifierTableData,
4019 InfoObj: ASTIdentifierLookupTrait(*this, F));
4020
4021 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4022 }
4023 break;
4024
4025 case IDENTIFIER_OFFSET: {
4026 if (F.LocalNumIdentifiers != 0)
4027 return llvm::createStringError(
4028 EC: std::errc::illegal_byte_sequence,
4029 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4030 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4031 F.LocalNumIdentifiers = Record[0];
4032 F.BaseIdentifierID = getTotalNumIdentifiers();
4033
4034 if (F.LocalNumIdentifiers > 0)
4035 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4036 + F.LocalNumIdentifiers);
4037 break;
4038 }
4039
4040 case INTERESTING_IDENTIFIERS:
4041 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4042 break;
4043
4044 case EAGERLY_DESERIALIZED_DECLS:
4045 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4046 // about "interesting" decls (for instance, if we're building a module).
4047 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4048 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4049 break;
4050
4051 case MODULAR_CODEGEN_DECLS:
4052 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4053 // them (ie: if we're not codegenerating this module).
4054 if (F.Kind == MK_MainFile ||
4055 getContext().getLangOpts().BuildingPCHWithObjectFile)
4056 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4057 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4058 break;
4059
4060 case SPECIAL_TYPES:
4061 if (SpecialTypes.empty()) {
4062 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4063 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4064 break;
4065 }
4066
4067 if (Record.empty())
4068 break;
4069
4070 if (SpecialTypes.size() != Record.size())
4071 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4072 Fmt: "invalid special-types record");
4073
4074 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4075 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4076 if (!SpecialTypes[I])
4077 SpecialTypes[I] = ID;
4078 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4079 // merge step?
4080 }
4081 break;
4082
4083 case STATISTICS:
4084 TotalNumStatements += Record[0];
4085 TotalNumMacros += Record[1];
4086 TotalLexicalDeclContexts += Record[2];
4087 TotalVisibleDeclContexts += Record[3];
4088 TotalModuleLocalVisibleDeclContexts += Record[4];
4089 TotalTULocalVisibleDeclContexts += Record[5];
4090 break;
4091
4092 case UNUSED_FILESCOPED_DECLS:
4093 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4094 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4095 break;
4096
4097 case DELEGATING_CTORS:
4098 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4099 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4100 break;
4101
4102 case WEAK_UNDECLARED_IDENTIFIERS:
4103 if (Record.size() % 3 != 0)
4104 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4105 Fmt: "invalid weak identifiers record");
4106
4107 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4108 // files. This isn't the way to do it :)
4109 WeakUndeclaredIdentifiers.clear();
4110
4111 // Translate the weak, undeclared identifiers into global IDs.
4112 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4113 WeakUndeclaredIdentifiers.push_back(
4114 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4115 WeakUndeclaredIdentifiers.push_back(
4116 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4117 WeakUndeclaredIdentifiers.push_back(
4118 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4119 }
4120 break;
4121
4122 case EXTNAME_UNDECLARED_IDENTIFIERS:
4123 if (Record.size() % 3 != 0)
4124 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4125 Fmt: "invalid extname identifiers record");
4126
4127 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4128 // non-original PCH files. This isn't the way to do it :)
4129 ExtnameUndeclaredIdentifiers.clear();
4130
4131 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4132 // global IDs.
4133 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4134 ExtnameUndeclaredIdentifiers.push_back(
4135 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4136 ExtnameUndeclaredIdentifiers.push_back(
4137 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4138 ExtnameUndeclaredIdentifiers.push_back(
4139 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4140 }
4141 break;
4142
4143 case SELECTOR_OFFSETS: {
4144 F.SelectorOffsets = (const uint32_t *)Blob.data();
4145 F.LocalNumSelectors = Record[0];
4146 unsigned LocalBaseSelectorID = Record[1];
4147 F.BaseSelectorID = getTotalNumSelectors();
4148
4149 if (F.LocalNumSelectors > 0) {
4150 // Introduce the global -> local mapping for selectors within this
4151 // module.
4152 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4153
4154 // Introduce the local -> global mapping for selectors within this
4155 // module.
4156 F.SelectorRemap.insertOrReplace(
4157 Val: std::make_pair(x&: LocalBaseSelectorID,
4158 y: F.BaseSelectorID - LocalBaseSelectorID));
4159
4160 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4161 }
4162 break;
4163 }
4164
4165 case METHOD_POOL:
4166 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4167 if (Record[0])
4168 F.SelectorLookupTable
4169 = ASTSelectorLookupTable::Create(
4170 Buckets: F.SelectorLookupTableData + Record[0],
4171 Base: F.SelectorLookupTableData,
4172 InfoObj: ASTSelectorLookupTrait(*this, F));
4173 TotalNumMethodPoolEntries += Record[1];
4174 break;
4175
4176 case REFERENCED_SELECTOR_POOL:
4177 if (!Record.empty()) {
4178 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4179 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4180 LocalID: Record[Idx++]));
4181 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4182 getRawEncoding());
4183 }
4184 }
4185 break;
4186
4187 case PP_ASSUME_NONNULL_LOC: {
4188 unsigned Idx = 0;
4189 if (!Record.empty())
4190 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4191 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4192 break;
4193 }
4194
4195 case PP_UNSAFE_BUFFER_USAGE: {
4196 if (!Record.empty()) {
4197 SmallVector<SourceLocation, 64> SrcLocs;
4198 unsigned Idx = 0;
4199 while (Idx < Record.size())
4200 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4201 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4202 }
4203 break;
4204 }
4205
4206 case PP_CONDITIONAL_STACK:
4207 if (!Record.empty()) {
4208 unsigned Idx = 0, End = Record.size() - 1;
4209 bool ReachedEOFWhileSkipping = Record[Idx++];
4210 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4211 if (ReachedEOFWhileSkipping) {
4212 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4213 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4214 bool FoundNonSkipPortion = Record[Idx++];
4215 bool FoundElse = Record[Idx++];
4216 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4217 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4218 args&: FoundElse, args&: ElseLoc);
4219 }
4220 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4221 while (Idx < End) {
4222 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4223 bool WasSkipping = Record[Idx++];
4224 bool FoundNonSkip = Record[Idx++];
4225 bool FoundElse = Record[Idx++];
4226 ConditionalStack.push_back(
4227 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4228 }
4229 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4230 }
4231 break;
4232
4233 case PP_COUNTER_VALUE:
4234 if (!Record.empty() && Listener)
4235 Listener->ReadCounter(M: F, Value: Record[0]);
4236 break;
4237
4238 case FILE_SORTED_DECLS:
4239 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4240 F.NumFileSortedDecls = Record[0];
4241 break;
4242
4243 case SOURCE_LOCATION_OFFSETS: {
4244 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4245 F.LocalNumSLocEntries = Record[0];
4246 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4247 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4248 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4249 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4250 TotalSize: SLocSpaceSize);
4251 if (!F.SLocEntryBaseID) {
4252 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4253 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4254 return llvm::createStringError(EC: std::errc::invalid_argument,
4255 Fmt: "ran out of source locations");
4256 }
4257 // Make our entry in the range map. BaseID is negative and growing, so
4258 // we invert it. Because we invert it, though, we need the other end of
4259 // the range.
4260 unsigned RangeStart =
4261 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4262 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4263 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4264
4265 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4266 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4267 GlobalSLocOffsetMap.insert(
4268 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4269 - SLocSpaceSize,y: &F));
4270
4271 TotalNumSLocEntries += F.LocalNumSLocEntries;
4272 break;
4273 }
4274
4275 case MODULE_OFFSET_MAP:
4276 F.ModuleOffsetMap = Blob;
4277 break;
4278
4279 case SOURCE_MANAGER_LINE_TABLE:
4280 ParseLineTable(F, Record);
4281 break;
4282
4283 case EXT_VECTOR_DECLS:
4284 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4285 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4286 break;
4287
4288 case VTABLE_USES:
4289 if (Record.size() % 3 != 0)
4290 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4291 Fmt: "Invalid VTABLE_USES record");
4292
4293 // Later tables overwrite earlier ones.
4294 // FIXME: Modules will have some trouble with this. This is clearly not
4295 // the right way to do this.
4296 VTableUses.clear();
4297
4298 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4299 VTableUses.push_back(
4300 Elt: {.ID: ReadDeclID(F, Record, Idx),
4301 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4302 .Used: (bool)Record[Idx++]});
4303 }
4304 break;
4305
4306 case PENDING_IMPLICIT_INSTANTIATIONS:
4307
4308 if (Record.size() % 2 != 0)
4309 return llvm::createStringError(
4310 EC: std::errc::illegal_byte_sequence,
4311 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4312
4313 // For standard C++20 module, we will only reads the instantiations
4314 // if it is the main file.
4315 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4316 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4317 PendingInstantiations.push_back(
4318 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4319 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4320 }
4321 }
4322 break;
4323
4324 case SEMA_DECL_REFS:
4325 if (Record.size() != 3)
4326 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4327 Fmt: "Invalid SEMA_DECL_REFS block");
4328 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4329 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4330 break;
4331
4332 case PPD_ENTITIES_OFFSETS: {
4333 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4334 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4335 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4336
4337 unsigned StartingID;
4338 if (!PP.getPreprocessingRecord())
4339 PP.createPreprocessingRecord();
4340 if (!PP.getPreprocessingRecord()->getExternalSource())
4341 PP.getPreprocessingRecord()->SetExternalSource(*this);
4342 StartingID
4343 = PP.getPreprocessingRecord()
4344 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4345 F.BasePreprocessedEntityID = StartingID;
4346
4347 if (F.NumPreprocessedEntities > 0) {
4348 // Introduce the global -> local mapping for preprocessed entities in
4349 // this module.
4350 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4351 }
4352
4353 break;
4354 }
4355
4356 case PPD_SKIPPED_RANGES: {
4357 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4358 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4359 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4360
4361 if (!PP.getPreprocessingRecord())
4362 PP.createPreprocessingRecord();
4363 if (!PP.getPreprocessingRecord()->getExternalSource())
4364 PP.getPreprocessingRecord()->SetExternalSource(*this);
4365 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4366 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4367
4368 if (F.NumPreprocessedSkippedRanges > 0)
4369 GlobalSkippedRangeMap.insert(
4370 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4371 break;
4372 }
4373
4374 case DECL_UPDATE_OFFSETS:
4375 if (Record.size() % 2 != 0)
4376 return llvm::createStringError(
4377 EC: std::errc::illegal_byte_sequence,
4378 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4379 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4380 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4381 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4382
4383 // If we've already loaded the decl, perform the updates when we finish
4384 // loading this block.
4385 if (Decl *D = GetExistingDecl(ID))
4386 PendingUpdateRecords.push_back(
4387 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4388 }
4389 break;
4390
4391 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4392 if (Record.size() % 5 != 0)
4393 return llvm::createStringError(
4394 EC: std::errc::illegal_byte_sequence,
4395 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4396 "file");
4397 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4398 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4399
4400 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4401 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4402 uint64_t LocalLexicalOffset = Record[I++];
4403 uint64_t LexicalOffset =
4404 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4405 uint64_t LocalVisibleOffset = Record[I++];
4406 uint64_t VisibleOffset =
4407 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4408 uint64_t LocalModuleLocalOffset = Record[I++];
4409 uint64_t ModuleLocalOffset =
4410 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4411 uint64_t TULocalLocalOffset = Record[I++];
4412 uint64_t TULocalOffset =
4413 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4414
4415 DelayedNamespaceOffsetMap[ID] = {
4416 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4417
4418 assert(!GetExistingDecl(ID) &&
4419 "We shouldn't load the namespace in the front of delayed "
4420 "namespace lexical and visible block");
4421 }
4422 break;
4423 }
4424
4425 case RELATED_DECLS_MAP:
4426 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4427 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4428 auto &RelatedDecls = RelatedDeclsMap[ID];
4429 unsigned NN = Record[I++];
4430 RelatedDecls.reserve(N: NN);
4431 for (unsigned II = 0; II < NN; II++)
4432 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4433 }
4434 break;
4435
4436 case OBJC_CATEGORIES_MAP:
4437 if (F.LocalNumObjCCategoriesInMap != 0)
4438 return llvm::createStringError(
4439 EC: std::errc::illegal_byte_sequence,
4440 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4441
4442 F.LocalNumObjCCategoriesInMap = Record[0];
4443 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4444 break;
4445
4446 case OBJC_CATEGORIES:
4447 F.ObjCCategories.swap(RHS&: Record);
4448 break;
4449
4450 case CUDA_SPECIAL_DECL_REFS:
4451 // Later tables overwrite earlier ones.
4452 // FIXME: Modules will have trouble with this.
4453 CUDASpecialDeclRefs.clear();
4454 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4455 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4456 break;
4457
4458 case HEADER_SEARCH_TABLE:
4459 F.HeaderFileInfoTableData = Blob.data();
4460 F.LocalNumHeaderFileInfos = Record[1];
4461 if (Record[0]) {
4462 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4463 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4464 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4465 InfoObj: HeaderFileInfoTrait(*this, F));
4466
4467 PP.getHeaderSearchInfo().SetExternalSource(this);
4468 if (!PP.getHeaderSearchInfo().getExternalLookup())
4469 PP.getHeaderSearchInfo().SetExternalLookup(this);
4470 }
4471 break;
4472
4473 case FP_PRAGMA_OPTIONS:
4474 // Later tables overwrite earlier ones.
4475 FPPragmaOptions.swap(RHS&: Record);
4476 break;
4477
4478 case DECLS_WITH_EFFECTS_TO_VERIFY:
4479 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4480 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4481 break;
4482
4483 case OPENCL_EXTENSIONS:
4484 for (unsigned I = 0, E = Record.size(); I != E; ) {
4485 auto Name = ReadString(Record, Idx&: I);
4486 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4487 OptInfo.Supported = Record[I++] != 0;
4488 OptInfo.Enabled = Record[I++] != 0;
4489 OptInfo.WithPragma = Record[I++] != 0;
4490 OptInfo.Avail = Record[I++];
4491 OptInfo.Core = Record[I++];
4492 OptInfo.Opt = Record[I++];
4493 }
4494 break;
4495
4496 case TENTATIVE_DEFINITIONS:
4497 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4498 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4499 break;
4500
4501 case KNOWN_NAMESPACES:
4502 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4503 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4504 break;
4505
4506 case UNDEFINED_BUT_USED:
4507 if (Record.size() % 2 != 0)
4508 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4509 Fmt: "invalid undefined-but-used record");
4510 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4511 UndefinedButUsed.push_back(
4512 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4513 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4514 }
4515 break;
4516
4517 case DELETE_EXPRS_TO_ANALYZE:
4518 for (unsigned I = 0, N = Record.size(); I != N;) {
4519 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4520 const uint64_t Count = Record[I++];
4521 DelayedDeleteExprs.push_back(Elt: Count);
4522 for (uint64_t C = 0; C < Count; ++C) {
4523 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4524 bool IsArrayForm = Record[I++] == 1;
4525 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4526 }
4527 }
4528 break;
4529
4530 case VTABLES_TO_EMIT:
4531 if (F.Kind == MK_MainFile ||
4532 getContext().getLangOpts().BuildingPCHWithObjectFile)
4533 for (unsigned I = 0, N = Record.size(); I != N;)
4534 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4535 break;
4536
4537 case IMPORTED_MODULES:
4538 if (!F.isModule()) {
4539 // If we aren't loading a module (which has its own exports), make
4540 // all of the imported modules visible.
4541 // FIXME: Deal with macros-only imports.
4542 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4543 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4544 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4545 if (GlobalID) {
4546 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4547 if (DeserializationListener)
4548 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4549 }
4550 }
4551 }
4552 break;
4553
4554 case MACRO_OFFSET: {
4555 if (F.LocalNumMacros != 0)
4556 return llvm::createStringError(
4557 EC: std::errc::illegal_byte_sequence,
4558 Fmt: "duplicate MACRO_OFFSET record in AST file");
4559 F.MacroOffsets = (const uint32_t *)Blob.data();
4560 F.LocalNumMacros = Record[0];
4561 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4562 F.BaseMacroID = getTotalNumMacros();
4563
4564 if (F.LocalNumMacros > 0)
4565 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4566 break;
4567 }
4568
4569 case LATE_PARSED_TEMPLATE:
4570 LateParsedTemplates.emplace_back(
4571 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4572 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4573 break;
4574
4575 case OPTIMIZE_PRAGMA_OPTIONS:
4576 if (Record.size() != 1)
4577 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4578 Fmt: "invalid pragma optimize record");
4579 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4580 break;
4581
4582 case MSSTRUCT_PRAGMA_OPTIONS:
4583 if (Record.size() != 1)
4584 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4585 Fmt: "invalid pragma ms_struct record");
4586 PragmaMSStructState = Record[0];
4587 break;
4588
4589 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4590 if (Record.size() != 2)
4591 return llvm::createStringError(
4592 EC: std::errc::illegal_byte_sequence,
4593 Fmt: "invalid pragma pointers to members record");
4594 PragmaMSPointersToMembersState = Record[0];
4595 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4596 break;
4597
4598 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4599 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4600 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4601 break;
4602
4603 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4604 if (Record.size() != 1)
4605 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4606 Fmt: "invalid cuda pragma options record");
4607 ForceHostDeviceDepth = Record[0];
4608 break;
4609
4610 case ALIGN_PACK_PRAGMA_OPTIONS: {
4611 if (Record.size() < 3)
4612 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4613 Fmt: "invalid pragma pack record");
4614 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4615 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4616 unsigned NumStackEntries = Record[2];
4617 unsigned Idx = 3;
4618 // Reset the stack when importing a new module.
4619 PragmaAlignPackStack.clear();
4620 for (unsigned I = 0; I < NumStackEntries; ++I) {
4621 PragmaAlignPackStackEntry Entry;
4622 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4623 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4624 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4625 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4626 Entry.SlotLabel = PragmaAlignPackStrings.back();
4627 PragmaAlignPackStack.push_back(Elt: Entry);
4628 }
4629 break;
4630 }
4631
4632 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4633 if (Record.size() < 3)
4634 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4635 Fmt: "invalid pragma float control record");
4636 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4637 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4638 unsigned NumStackEntries = Record[2];
4639 unsigned Idx = 3;
4640 // Reset the stack when importing a new module.
4641 FpPragmaStack.clear();
4642 for (unsigned I = 0; I < NumStackEntries; ++I) {
4643 FpPragmaStackEntry Entry;
4644 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4645 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4646 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4647 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4648 Entry.SlotLabel = FpPragmaStrings.back();
4649 FpPragmaStack.push_back(Elt: Entry);
4650 }
4651 break;
4652 }
4653
4654 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4655 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4656 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4657 break;
4658
4659 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4660 unsigned NumRecords = Record.front();
4661 // Last record which is used to keep number of valid records.
4662 if (Record.size() - 1 != NumRecords)
4663 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4664 Fmt: "invalid rvv intrinsic pragma record");
4665
4666 if (RISCVVecIntrinsicPragma.empty())
4667 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4668 // There might be multiple precompiled modules imported, we need to union
4669 // them all.
4670 for (unsigned i = 0; i < NumRecords; ++i)
4671 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4672 break;
4673 }
4674 }
4675 }
4676}
4677
4678void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4679 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4680
4681 // Additional remapping information.
4682 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4683 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4684 F.ModuleOffsetMap = StringRef();
4685
4686 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4687 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4688 RemapBuilder SelectorRemap(F.SelectorRemap);
4689
4690 auto &ImportedModuleVector = F.TransitiveImports;
4691 assert(ImportedModuleVector.empty());
4692
4693 while (Data < DataEnd) {
4694 // FIXME: Looking up dependency modules by filename is horrible. Let's
4695 // start fixing this with prebuilt, explicit and implicit modules and see
4696 // how it goes...
4697 using namespace llvm::support;
4698 ModuleKind Kind = static_cast<ModuleKind>(
4699 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4700 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4701 StringRef Name = StringRef((const char*)Data, Len);
4702 Data += Len;
4703 ModuleFile *OM =
4704 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4705 Kind == MK_ImplicitModule
4706 ? ModuleMgr.lookupByModuleName(ModName: Name)
4707 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4708 if (!OM)
4709 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4710 if (!OM) {
4711 std::string Msg = "refers to unknown module, cannot find ";
4712 Msg.append(str: std::string(Name));
4713 Error(Msg);
4714 return;
4715 }
4716
4717 ImportedModuleVector.push_back(Elt: OM);
4718
4719 uint32_t SubmoduleIDOffset =
4720 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4721 uint32_t SelectorIDOffset =
4722 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4723
4724 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4725 RemapBuilder &Remap) {
4726 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4727 if (Offset != None)
4728 Remap.insert(Val: std::make_pair(x&: Offset,
4729 y: static_cast<int>(BaseOffset - Offset)));
4730 };
4731
4732 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4733 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4734 }
4735}
4736
4737ASTReader::ASTReadResult
4738ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4739 const ModuleFile *ImportedBy,
4740 unsigned ClientLoadCapabilities) {
4741 unsigned Idx = 0;
4742 F.ModuleMapPath = ReadPath(F, Record, Idx);
4743
4744 // Try to resolve ModuleName in the current header search context and
4745 // verify that it is found in the same module map file as we saved. If the
4746 // top-level AST file is a main file, skip this check because there is no
4747 // usable header search context.
4748 assert(!F.ModuleName.empty() &&
4749 "MODULE_NAME should come before MODULE_MAP_FILE");
4750 auto [MaybeM, IgnoreError] =
4751 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4752 if (MaybeM.has_value()) {
4753 // An implicitly-loaded module file should have its module listed in some
4754 // module map file that we've already loaded.
4755 Module *M = MaybeM.value();
4756 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4757 OptionalFileEntryRef ModMap =
4758 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4759 if (!IgnoreError && !ModMap) {
4760 if (M && M->Directory)
4761 Diag(DiagID: diag::remark_module_relocated)
4762 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4763
4764 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4765 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4766 // This module was defined by an imported (explicit) module.
4767 Diag(DiagID: diag::err_module_file_conflict)
4768 << F.ModuleName << F.FileName << *ASTFileName;
4769 // TODO: Add a note with the module map paths if they differ.
4770 } else {
4771 // This module was built with a different module map.
4772 Diag(DiagID: diag::err_imported_module_not_found)
4773 << F.ModuleName << F.FileName
4774 << (ImportedBy ? ImportedBy->FileName.str() : "")
4775 << F.ModuleMapPath << !ImportedBy;
4776 // In case it was imported by a PCH, there's a chance the user is
4777 // just missing to include the search path to the directory containing
4778 // the modulemap.
4779 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4780 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4781 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4782 }
4783 }
4784 return OutOfDate;
4785 }
4786
4787 assert(M && M->Name == F.ModuleName && "found module with different name");
4788
4789 // Check the primary module map file.
4790 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4791 if (!StoredModMap || *StoredModMap != ModMap) {
4792 assert(ModMap && "found module is missing module map file");
4793 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4794 "top-level import should be verified");
4795 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4796 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4797 Diag(DiagID: diag::err_imported_module_modmap_changed)
4798 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4799 << ModMap->getName() << F.ModuleMapPath << NotImported;
4800 return OutOfDate;
4801 }
4802
4803 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4804 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4805 // FIXME: we should use input files rather than storing names.
4806 std::string Filename = ReadPath(F, Record, Idx);
4807 auto SF = FileMgr.getOptionalFileRef(Filename);
4808 if (!SF) {
4809 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4810 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4811 return OutOfDate;
4812 }
4813 AdditionalStoredMaps.insert(V: *SF);
4814 }
4815
4816 // Check any additional module map files (e.g. module.private.modulemap)
4817 // that are not in the pcm.
4818 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4819 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4820 // Remove files that match
4821 // Note: SmallPtrSet::erase is really remove
4822 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4823 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4824 Diag(DiagID: diag::err_module_different_modmap)
4825 << F.ModuleName << /*new*/0 << ModMap.getName();
4826 return OutOfDate;
4827 }
4828 }
4829 }
4830
4831 // Check any additional module map files that are in the pcm, but not
4832 // found in header search. Cases that match are already removed.
4833 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4834 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4835 Diag(DiagID: diag::err_module_different_modmap)
4836 << F.ModuleName << /*not new*/1 << ModMap.getName();
4837 return OutOfDate;
4838 }
4839 }
4840
4841 if (Listener)
4842 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4843 return Success;
4844}
4845
4846/// Move the given method to the back of the global list of methods.
4847static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4848 // Find the entry for this selector in the method pool.
4849 SemaObjC::GlobalMethodPool::iterator Known =
4850 S.ObjC().MethodPool.find(Val: Method->getSelector());
4851 if (Known == S.ObjC().MethodPool.end())
4852 return;
4853
4854 // Retrieve the appropriate method list.
4855 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4856 : Known->second.second;
4857 bool Found = false;
4858 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4859 if (!Found) {
4860 if (List->getMethod() == Method) {
4861 Found = true;
4862 } else {
4863 // Keep searching.
4864 continue;
4865 }
4866 }
4867
4868 if (List->getNext())
4869 List->setMethod(List->getNext()->getMethod());
4870 else
4871 List->setMethod(Method);
4872 }
4873}
4874
4875void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4876 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4877 for (Decl *D : Names) {
4878 bool wasHidden = !D->isUnconditionallyVisible();
4879 D->setVisibleDespiteOwningModule();
4880
4881 if (wasHidden && SemaObj) {
4882 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4883 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4884 }
4885 }
4886 }
4887}
4888
4889void ASTReader::makeModuleVisible(Module *Mod,
4890 Module::NameVisibilityKind NameVisibility,
4891 SourceLocation ImportLoc) {
4892 llvm::SmallPtrSet<Module *, 4> Visited;
4893 SmallVector<Module *, 4> Stack;
4894 Stack.push_back(Elt: Mod);
4895 while (!Stack.empty()) {
4896 Mod = Stack.pop_back_val();
4897
4898 if (NameVisibility <= Mod->NameVisibility) {
4899 // This module already has this level of visibility (or greater), so
4900 // there is nothing more to do.
4901 continue;
4902 }
4903
4904 if (Mod->isUnimportable()) {
4905 // Modules that aren't importable cannot be made visible.
4906 continue;
4907 }
4908
4909 // Update the module's name visibility.
4910 Mod->NameVisibility = NameVisibility;
4911
4912 // If we've already deserialized any names from this module,
4913 // mark them as visible.
4914 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4915 if (Hidden != HiddenNamesMap.end()) {
4916 auto HiddenNames = std::move(*Hidden);
4917 HiddenNamesMap.erase(I: Hidden);
4918 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4919 assert(!HiddenNamesMap.contains(Mod) &&
4920 "making names visible added hidden names");
4921 }
4922
4923 // Push any exported modules onto the stack to be marked as visible.
4924 SmallVector<Module *, 16> Exports;
4925 Mod->getExportedModules(Exported&: Exports);
4926 for (SmallVectorImpl<Module *>::iterator
4927 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4928 Module *Exported = *I;
4929 if (Visited.insert(Ptr: Exported).second)
4930 Stack.push_back(Elt: Exported);
4931 }
4932 }
4933}
4934
4935/// We've merged the definition \p MergedDef into the existing definition
4936/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4937/// visible.
4938void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4939 NamedDecl *MergedDef) {
4940 if (!Def->isUnconditionallyVisible()) {
4941 // If MergedDef is visible or becomes visible, make the definition visible.
4942 if (MergedDef->isUnconditionallyVisible())
4943 Def->setVisibleDespiteOwningModule();
4944 else {
4945 getContext().mergeDefinitionIntoModule(
4946 ND: Def, M: MergedDef->getImportedOwningModule(),
4947 /*NotifyListeners*/ false);
4948 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4949 }
4950 }
4951}
4952
4953bool ASTReader::loadGlobalIndex() {
4954 if (GlobalIndex)
4955 return false;
4956
4957 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4958 !PP.getLangOpts().Modules)
4959 return true;
4960
4961 // Try to load the global index.
4962 TriedLoadingGlobalIndex = true;
4963 StringRef SpecificModuleCachePath =
4964 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4965 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4966 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4967 if (llvm::Error Err = std::move(Result.second)) {
4968 assert(!Result.first);
4969 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4970 return true;
4971 }
4972
4973 GlobalIndex.reset(p: Result.first);
4974 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4975 return false;
4976}
4977
4978bool ASTReader::isGlobalIndexUnavailable() const {
4979 return PP.getLangOpts().Modules && UseGlobalIndex &&
4980 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4981}
4982
4983/// Given a cursor at the start of an AST file, scan ahead and drop the
4984/// cursor into the start of the given block ID, returning false on success and
4985/// true on failure.
4986static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4987 while (true) {
4988 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4989 if (!MaybeEntry) {
4990 // FIXME this drops errors on the floor.
4991 consumeError(Err: MaybeEntry.takeError());
4992 return true;
4993 }
4994 llvm::BitstreamEntry Entry = MaybeEntry.get();
4995
4996 switch (Entry.Kind) {
4997 case llvm::BitstreamEntry::Error:
4998 case llvm::BitstreamEntry::EndBlock:
4999 return true;
5000
5001 case llvm::BitstreamEntry::Record:
5002 // Ignore top-level records.
5003 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
5004 break;
5005 else {
5006 // FIXME this drops errors on the floor.
5007 consumeError(Err: Skipped.takeError());
5008 return true;
5009 }
5010
5011 case llvm::BitstreamEntry::SubBlock:
5012 if (Entry.ID == BlockID) {
5013 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
5014 // FIXME this drops the error on the floor.
5015 consumeError(Err: std::move(Err));
5016 return true;
5017 }
5018 // Found it!
5019 return false;
5020 }
5021
5022 if (llvm::Error Err = Cursor.SkipBlock()) {
5023 // FIXME this drops the error on the floor.
5024 consumeError(Err: std::move(Err));
5025 return true;
5026 }
5027 }
5028 }
5029}
5030
5031ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5032 ModuleKind Type,
5033 SourceLocation ImportLoc,
5034 unsigned ClientLoadCapabilities,
5035 ModuleFile **NewLoadedModuleFile) {
5036 llvm::TimeTraceScope scope("ReadAST", FileName);
5037
5038 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5039 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5040 CurrentDeserializingModuleKind, Type);
5041
5042 // Defer any pending actions until we get to the end of reading the AST file.
5043 Deserializing AnASTFile(this);
5044
5045 // Bump the generation number.
5046 unsigned PreviousGeneration = 0;
5047 if (ContextObj)
5048 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5049
5050 unsigned NumModules = ModuleMgr.size();
5051 SmallVector<ImportedModule, 4> Loaded;
5052 if (ASTReadResult ReadResult =
5053 ReadASTCore(FileName, Type, ImportLoc,
5054 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5055 ClientLoadCapabilities)) {
5056 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5057
5058 // If we find that any modules are unusable, the global index is going
5059 // to be out-of-date. Just remove it.
5060 GlobalIndex.reset();
5061 ModuleMgr.setGlobalIndex(nullptr);
5062 return ReadResult;
5063 }
5064
5065 if (NewLoadedModuleFile && !Loaded.empty())
5066 *NewLoadedModuleFile = Loaded.back().Mod;
5067
5068 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5069 // remove modules from this point. Various fields are updated during reading
5070 // the AST block and removing the modules would result in dangling pointers.
5071 // They are generally only incidentally dereferenced, ie. a binary search
5072 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5073 // be dereferenced but it wouldn't actually be used.
5074
5075 // Load the AST blocks of all of the modules that we loaded. We can still
5076 // hit errors parsing the ASTs at this point.
5077 for (ImportedModule &M : Loaded) {
5078 ModuleFile &F = *M.Mod;
5079 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5080
5081 // Read the AST block.
5082 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5083 Error(Err: std::move(Err));
5084 return Failure;
5085 }
5086
5087 // The AST block should always have a definition for the main module.
5088 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5089 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5090 return Failure;
5091 }
5092
5093 // Read the extension blocks.
5094 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5095 if (llvm::Error Err = ReadExtensionBlock(F)) {
5096 Error(Err: std::move(Err));
5097 return Failure;
5098 }
5099 }
5100
5101 // Once read, set the ModuleFile bit base offset and update the size in
5102 // bits of all files we've seen.
5103 F.GlobalBitOffset = TotalModulesSizeInBits;
5104 TotalModulesSizeInBits += F.SizeInBits;
5105 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5106 }
5107
5108 // Preload source locations and interesting indentifiers.
5109 for (ImportedModule &M : Loaded) {
5110 ModuleFile &F = *M.Mod;
5111
5112 // Map the original source file ID into the ID space of the current
5113 // compilation.
5114 if (F.OriginalSourceFileID.isValid())
5115 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5116
5117 for (auto Offset : F.PreloadIdentifierOffsets) {
5118 const unsigned char *Data = F.IdentifierTableData + Offset;
5119
5120 ASTIdentifierLookupTrait Trait(*this, F);
5121 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5122 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5123
5124 IdentifierInfo *II;
5125 if (!PP.getLangOpts().CPlusPlus) {
5126 // Identifiers present in both the module file and the importing
5127 // instance are marked out-of-date so that they can be deserialized
5128 // on next use via ASTReader::updateOutOfDateIdentifier().
5129 // Identifiers present in the module file but not in the importing
5130 // instance are ignored for now, preventing growth of the identifier
5131 // table. They will be deserialized on first use via ASTReader::get().
5132 auto It = PP.getIdentifierTable().find(Name: Key);
5133 if (It == PP.getIdentifierTable().end())
5134 continue;
5135 II = It->second;
5136 } else {
5137 // With C++ modules, not many identifiers are considered interesting.
5138 // All identifiers in the module file can be placed into the identifier
5139 // table of the importing instance and marked as out-of-date. This makes
5140 // ASTReader::get() a no-op, and deserialization will take place on
5141 // first/next use via ASTReader::updateOutOfDateIdentifier().
5142 II = &PP.getIdentifierTable().getOwn(Name: Key);
5143 }
5144
5145 II->setOutOfDate(true);
5146
5147 // Mark this identifier as being from an AST file so that we can track
5148 // whether we need to serialize it.
5149 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5150
5151 // Associate the ID with the identifier so that the writer can reuse it.
5152 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5153 SetIdentifierInfo(ID, II);
5154 }
5155 }
5156
5157 // Builtins and library builtins have already been initialized. Mark all
5158 // identifiers as out-of-date, so that they are deserialized on first use.
5159 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5160 for (auto &Id : PP.getIdentifierTable())
5161 Id.second->setOutOfDate(true);
5162
5163 // Mark selectors as out of date.
5164 for (const auto &Sel : SelectorGeneration)
5165 SelectorOutOfDate[Sel.first] = true;
5166
5167 // Setup the import locations and notify the module manager that we've
5168 // committed to these module files.
5169 for (ImportedModule &M : Loaded) {
5170 ModuleFile &F = *M.Mod;
5171
5172 ModuleMgr.moduleFileAccepted(MF: &F);
5173
5174 // Set the import location.
5175 F.DirectImportLoc = ImportLoc;
5176 // FIXME: We assume that locations from PCH / preamble do not need
5177 // any translation.
5178 if (!M.ImportedBy)
5179 F.ImportLoc = M.ImportLoc;
5180 else
5181 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5182 }
5183
5184 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5185 // Might be unnecessary as use declarations are only used to build the
5186 // module itself.
5187
5188 if (ContextObj)
5189 InitializeContext();
5190
5191 if (SemaObj)
5192 UpdateSema();
5193
5194 if (DeserializationListener)
5195 DeserializationListener->ReaderInitialized(Reader: this);
5196
5197 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5198 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5199 // If this AST file is a precompiled preamble, then set the
5200 // preamble file ID of the source manager to the file source file
5201 // from which the preamble was built.
5202 if (Type == MK_Preamble) {
5203 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5204 } else if (Type == MK_MainFile) {
5205 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5206 }
5207 }
5208
5209 // For any Objective-C class definitions we have already loaded, make sure
5210 // that we load any additional categories.
5211 if (ContextObj) {
5212 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5213 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5214 D: ObjCClassesLoaded[I], PreviousGeneration);
5215 }
5216 }
5217
5218 const HeaderSearchOptions &HSOpts =
5219 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5220 if (HSOpts.ModulesValidateOncePerBuildSession) {
5221 // Now we are certain that the module and all modules it depends on are
5222 // up-to-date. For implicitly-built module files, ensure the corresponding
5223 // timestamp files are up-to-date in this build session.
5224 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5225 ImportedModule &M = Loaded[I];
5226 if (M.Mod->Kind == MK_ImplicitModule &&
5227 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5228 getModuleManager().getModuleCache().updateModuleTimestamp(
5229 ModuleFilename: M.Mod->FileName);
5230 }
5231 }
5232
5233 return Success;
5234}
5235
5236static ASTFileSignature readASTFileSignature(StringRef PCH);
5237
5238/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5239static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5240 // FIXME checking magic headers is done in other places such as
5241 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5242 // always done the same. Unify it all with a helper.
5243 if (!Stream.canSkipToPos(pos: 4))
5244 return llvm::createStringError(
5245 EC: std::errc::illegal_byte_sequence,
5246 Fmt: "file too small to contain precompiled file magic");
5247 for (unsigned C : {'C', 'P', 'C', 'H'})
5248 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5249 if (Res.get() != C)
5250 return llvm::createStringError(
5251 EC: std::errc::illegal_byte_sequence,
5252 Fmt: "file doesn't start with precompiled file magic");
5253 } else
5254 return Res.takeError();
5255 return llvm::Error::success();
5256}
5257
5258static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5259 switch (Kind) {
5260 case MK_PCH:
5261 return 0; // PCH
5262 case MK_ImplicitModule:
5263 case MK_ExplicitModule:
5264 case MK_PrebuiltModule:
5265 return 1; // module
5266 case MK_MainFile:
5267 case MK_Preamble:
5268 return 2; // main source file
5269 }
5270 llvm_unreachable("unknown module kind");
5271}
5272
5273ASTReader::ASTReadResult ASTReader::ReadASTCore(
5274 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5275 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5276 off_t ExpectedSize, time_t ExpectedModTime,
5277 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5278 auto Result = ModuleMgr.addModule(
5279 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5280 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5281 ModuleFile *M = Result.getModule();
5282
5283 switch (Result.getKind()) {
5284 case AddModuleResult::AlreadyLoaded: {
5285 Diag(DiagID: diag::remark_module_import)
5286 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5287 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5288 return Success;
5289 }
5290
5291 case AddModuleResult::NewlyLoaded:
5292 // Load module file below.
5293 break;
5294
5295 case AddModuleResult::Missing:
5296 // The module file was missing; if the client can handle that, return
5297 // it.
5298 if (ClientLoadCapabilities & ARR_Missing)
5299 return Missing;
5300
5301 // Otherwise, return an error.
5302 Diag(DiagID: diag::err_ast_file_not_found)
5303 << moduleKindForDiagnostic(Kind: Type) << FileName;
5304 if (!Result.getBufferError().empty())
5305 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5306 return Failure;
5307
5308 case AddModuleResult::OutOfDate:
5309 // We couldn't load the module file because it is out-of-date. If the
5310 // client can handle out-of-date, return it.
5311 if (ClientLoadCapabilities & ARR_OutOfDate)
5312 return OutOfDate;
5313
5314 // Otherwise, return an error.
5315 Diag(DiagID: diag::err_ast_file_out_of_date)
5316 << moduleKindForDiagnostic(Kind: Type) << FileName;
5317 for (const auto &C : Result.getChanges()) {
5318 Diag(DiagID: diag::note_fe_ast_file_modified)
5319 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5320 << llvm::itostr(X: C.New.value_or(u: 0));
5321 }
5322 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5323 << Result.getValidationStatus();
5324 if (!Result.getSignatureError().empty())
5325 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5326 return Failure;
5327
5328 case AddModuleResult::None:
5329 llvm_unreachable("Unexpected value from adding module.");
5330 }
5331
5332 assert(M && "Missing module file");
5333
5334 bool ShouldFinalizePCM = false;
5335 llvm::scope_exit FinalizeOrDropPCM([&]() {
5336 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5337 if (ShouldFinalizePCM)
5338 MC.finalizePCM(Filename: FileName);
5339 else
5340 MC.tryToDropPCM(Filename: FileName);
5341 });
5342 ModuleFile &F = *M;
5343 BitstreamCursor &Stream = F.Stream;
5344 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5345 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5346
5347 // Sniff for the signature.
5348 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5349 Diag(DiagID: diag::err_ast_file_invalid)
5350 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5351 return Failure;
5352 }
5353
5354 // This is used for compatibility with older PCH formats.
5355 bool HaveReadControlBlock = false;
5356 while (true) {
5357 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5358 if (!MaybeEntry) {
5359 Error(Err: MaybeEntry.takeError());
5360 return Failure;
5361 }
5362 llvm::BitstreamEntry Entry = MaybeEntry.get();
5363
5364 switch (Entry.Kind) {
5365 case llvm::BitstreamEntry::Error:
5366 case llvm::BitstreamEntry::Record:
5367 case llvm::BitstreamEntry::EndBlock:
5368 Error(Msg: "invalid record at top-level of AST file");
5369 return Failure;
5370
5371 case llvm::BitstreamEntry::SubBlock:
5372 break;
5373 }
5374
5375 switch (Entry.ID) {
5376 case CONTROL_BLOCK_ID:
5377 HaveReadControlBlock = true;
5378 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5379 case Success:
5380 // Check that we didn't try to load a non-module AST file as a module.
5381 //
5382 // FIXME: Should we also perform the converse check? Loading a module as
5383 // a PCH file sort of works, but it's a bit wonky.
5384 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5385 Type == MK_PrebuiltModule) &&
5386 F.ModuleName.empty()) {
5387 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5388 if (Result != OutOfDate ||
5389 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5390 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5391 return Result;
5392 }
5393 break;
5394
5395 case Failure: return Failure;
5396 case Missing: return Missing;
5397 case OutOfDate: return OutOfDate;
5398 case VersionMismatch: return VersionMismatch;
5399 case ConfigurationMismatch: return ConfigurationMismatch;
5400 case HadErrors: return HadErrors;
5401 }
5402 break;
5403
5404 case AST_BLOCK_ID:
5405 if (!HaveReadControlBlock) {
5406 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5407 Diag(DiagID: diag::err_ast_file_version_too_old)
5408 << moduleKindForDiagnostic(Kind: Type) << FileName;
5409 return VersionMismatch;
5410 }
5411
5412 // Record that we've loaded this module.
5413 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5414 ShouldFinalizePCM = true;
5415 return Success;
5416
5417 default:
5418 if (llvm::Error Err = Stream.SkipBlock()) {
5419 Error(Err: std::move(Err));
5420 return Failure;
5421 }
5422 break;
5423 }
5424 }
5425
5426 llvm_unreachable("unexpected break; expected return");
5427}
5428
5429ASTReader::ASTReadResult
5430ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5431 unsigned ClientLoadCapabilities) {
5432 const HeaderSearchOptions &HSOpts =
5433 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5434 bool AllowCompatibleConfigurationMismatch =
5435 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5436 bool DisableValidation = shouldDisableValidationForFile(M: F);
5437
5438 ASTReadResult Result = readUnhashedControlBlockImpl(
5439 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5440 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5441 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5442
5443 // If F was directly imported by another module, it's implicitly validated by
5444 // the importing module.
5445 if (DisableValidation || WasImportedBy ||
5446 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5447 return Success;
5448
5449 if (Result == Failure) {
5450 Error(Msg: "malformed block record in AST file");
5451 return Failure;
5452 }
5453
5454 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5455 // If this module has already been finalized in the ModuleCache, we're stuck
5456 // with it; we can only load a single version of each module.
5457 //
5458 // This can happen when a module is imported in two contexts: in one, as a
5459 // user module; in another, as a system module (due to an import from
5460 // another module marked with the [system] flag). It usually indicates a
5461 // bug in the module map: this module should also be marked with [system].
5462 //
5463 // If -Wno-system-headers (the default), and the first import is as a
5464 // system module, then validation will fail during the as-user import,
5465 // since -Werror flags won't have been validated. However, it's reasonable
5466 // to treat this consistently as a system module.
5467 //
5468 // If -Wsystem-headers, the PCM on disk was built with
5469 // -Wno-system-headers, and the first import is as a user module, then
5470 // validation will fail during the as-system import since the PCM on disk
5471 // doesn't guarantee that -Werror was respected. However, the -Werror
5472 // flags were checked during the initial as-user import.
5473 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5474 Filename: F.FileName)) {
5475 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5476 return Success;
5477 }
5478 }
5479
5480 return Result;
5481}
5482
5483ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5484 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5485 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5486 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5487 // Initialize a stream.
5488 BitstreamCursor Stream(StreamData);
5489
5490 // Sniff for the signature.
5491 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5492 // FIXME this drops the error on the floor.
5493 consumeError(Err: std::move(Err));
5494 return Failure;
5495 }
5496
5497 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5498 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5499 return Failure;
5500
5501 // Read all of the records in the options block.
5502 RecordData Record;
5503 ASTReadResult Result = Success;
5504 while (true) {
5505 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5506 if (!MaybeEntry) {
5507 // FIXME this drops the error on the floor.
5508 consumeError(Err: MaybeEntry.takeError());
5509 return Failure;
5510 }
5511 llvm::BitstreamEntry Entry = MaybeEntry.get();
5512
5513 switch (Entry.Kind) {
5514 case llvm::BitstreamEntry::Error:
5515 case llvm::BitstreamEntry::SubBlock:
5516 return Failure;
5517
5518 case llvm::BitstreamEntry::EndBlock:
5519 return Result;
5520
5521 case llvm::BitstreamEntry::Record:
5522 // The interesting case.
5523 break;
5524 }
5525
5526 // Read and process a record.
5527 Record.clear();
5528 StringRef Blob;
5529 Expected<unsigned> MaybeRecordType =
5530 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5531 if (!MaybeRecordType) {
5532 // FIXME this drops the error.
5533 return Failure;
5534 }
5535 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5536 case SIGNATURE:
5537 if (F) {
5538 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5539 assert(F->Signature != ASTFileSignature::createDummy() &&
5540 "Dummy AST file signature not backpatched in ASTWriter.");
5541 }
5542 break;
5543 case AST_BLOCK_HASH:
5544 if (F) {
5545 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5546 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5547 "Dummy AST block hash not backpatched in ASTWriter.");
5548 }
5549 break;
5550 case DIAGNOSTIC_OPTIONS: {
5551 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5552 if (Listener && ValidateDiagnosticOptions &&
5553 !AllowCompatibleConfigurationMismatch &&
5554 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5555 Result = OutOfDate; // Don't return early. Read the signature.
5556 break;
5557 }
5558 case HEADER_SEARCH_PATHS: {
5559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5560 if (Listener && !AllowCompatibleConfigurationMismatch &&
5561 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5562 Result = ConfigurationMismatch;
5563 break;
5564 }
5565 case DIAG_PRAGMA_MAPPINGS:
5566 if (!F)
5567 break;
5568 if (F->PragmaDiagMappings.empty())
5569 F->PragmaDiagMappings.swap(RHS&: Record);
5570 else
5571 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5572 From: Record.begin(), To: Record.end());
5573 break;
5574 case HEADER_SEARCH_ENTRY_USAGE:
5575 if (F)
5576 F->SearchPathUsage = ReadBitVector(Record, Blob);
5577 break;
5578 case VFS_USAGE:
5579 if (F)
5580 F->VFSUsage = ReadBitVector(Record, Blob);
5581 break;
5582 }
5583 }
5584}
5585
5586/// Parse a record and blob containing module file extension metadata.
5587static bool parseModuleFileExtensionMetadata(
5588 const SmallVectorImpl<uint64_t> &Record,
5589 StringRef Blob,
5590 ModuleFileExtensionMetadata &Metadata) {
5591 if (Record.size() < 4) return true;
5592
5593 Metadata.MajorVersion = Record[0];
5594 Metadata.MinorVersion = Record[1];
5595
5596 unsigned BlockNameLen = Record[2];
5597 unsigned UserInfoLen = Record[3];
5598
5599 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5600
5601 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5602 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5603 Blob.data() + BlockNameLen + UserInfoLen);
5604 return false;
5605}
5606
5607llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5608 BitstreamCursor &Stream = F.Stream;
5609
5610 RecordData Record;
5611 while (true) {
5612 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5613 if (!MaybeEntry)
5614 return MaybeEntry.takeError();
5615 llvm::BitstreamEntry Entry = MaybeEntry.get();
5616
5617 switch (Entry.Kind) {
5618 case llvm::BitstreamEntry::SubBlock:
5619 if (llvm::Error Err = Stream.SkipBlock())
5620 return Err;
5621 continue;
5622 case llvm::BitstreamEntry::EndBlock:
5623 return llvm::Error::success();
5624 case llvm::BitstreamEntry::Error:
5625 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5626 Fmt: "malformed block record in AST file");
5627 case llvm::BitstreamEntry::Record:
5628 break;
5629 }
5630
5631 Record.clear();
5632 StringRef Blob;
5633 Expected<unsigned> MaybeRecCode =
5634 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5635 if (!MaybeRecCode)
5636 return MaybeRecCode.takeError();
5637 switch (MaybeRecCode.get()) {
5638 case EXTENSION_METADATA: {
5639 ModuleFileExtensionMetadata Metadata;
5640 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5641 return llvm::createStringError(
5642 EC: std::errc::illegal_byte_sequence,
5643 Fmt: "malformed EXTENSION_METADATA in AST file");
5644
5645 // Find a module file extension with this block name.
5646 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5647 if (Known == ModuleFileExtensions.end()) break;
5648
5649 // Form a reader.
5650 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5651 Mod&: F, Stream)) {
5652 F.ExtensionReaders.push_back(x: std::move(Reader));
5653 }
5654
5655 break;
5656 }
5657 }
5658 }
5659
5660 llvm_unreachable("ReadExtensionBlock should return from while loop");
5661}
5662
5663void ASTReader::InitializeContext() {
5664 assert(ContextObj && "no context to initialize");
5665 ASTContext &Context = *ContextObj;
5666
5667 // If there's a listener, notify them that we "read" the translation unit.
5668 if (DeserializationListener)
5669 DeserializationListener->DeclRead(
5670 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5671 D: Context.getTranslationUnitDecl());
5672
5673 // FIXME: Find a better way to deal with collisions between these
5674 // built-in types. Right now, we just ignore the problem.
5675
5676 // Load the special types.
5677 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5678 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5679 if (!Context.CFConstantStringTypeDecl)
5680 Context.setCFConstantStringType(GetType(ID: String));
5681 }
5682
5683 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5684 QualType FileType = GetType(ID: File);
5685 if (FileType.isNull()) {
5686 Error(Msg: "FILE type is NULL");
5687 return;
5688 }
5689
5690 if (!Context.FILEDecl) {
5691 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5692 Context.setFILEDecl(Typedef->getDecl());
5693 else {
5694 const TagType *Tag = FileType->getAs<TagType>();
5695 if (!Tag) {
5696 Error(Msg: "Invalid FILE type in AST file");
5697 return;
5698 }
5699 Context.setFILEDecl(Tag->getDecl());
5700 }
5701 }
5702 }
5703
5704 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5705 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5706 if (Jmp_bufType.isNull()) {
5707 Error(Msg: "jmp_buf type is NULL");
5708 return;
5709 }
5710
5711 if (!Context.jmp_bufDecl) {
5712 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5713 Context.setjmp_bufDecl(Typedef->getDecl());
5714 else {
5715 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5716 if (!Tag) {
5717 Error(Msg: "Invalid jmp_buf type in AST file");
5718 return;
5719 }
5720 Context.setjmp_bufDecl(Tag->getDecl());
5721 }
5722 }
5723 }
5724
5725 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5726 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5727 if (Sigjmp_bufType.isNull()) {
5728 Error(Msg: "sigjmp_buf type is NULL");
5729 return;
5730 }
5731
5732 if (!Context.sigjmp_bufDecl) {
5733 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5734 Context.setsigjmp_bufDecl(Typedef->getDecl());
5735 else {
5736 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5737 assert(Tag && "Invalid sigjmp_buf type in AST file");
5738 Context.setsigjmp_bufDecl(Tag->getDecl());
5739 }
5740 }
5741 }
5742
5743 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5744 if (Context.ObjCIdRedefinitionType.isNull())
5745 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5746 }
5747
5748 if (TypeID ObjCClassRedef =
5749 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5750 if (Context.ObjCClassRedefinitionType.isNull())
5751 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5752 }
5753
5754 if (TypeID ObjCSelRedef =
5755 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5756 if (Context.ObjCSelRedefinitionType.isNull())
5757 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5758 }
5759
5760 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5761 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5762 if (Ucontext_tType.isNull()) {
5763 Error(Msg: "ucontext_t type is NULL");
5764 return;
5765 }
5766
5767 if (!Context.ucontext_tDecl) {
5768 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5769 Context.setucontext_tDecl(Typedef->getDecl());
5770 else {
5771 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5772 assert(Tag && "Invalid ucontext_t type in AST file");
5773 Context.setucontext_tDecl(Tag->getDecl());
5774 }
5775 }
5776 }
5777 }
5778
5779 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5780
5781 // If there were any CUDA special declarations, deserialize them.
5782 if (!CUDASpecialDeclRefs.empty()) {
5783 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5784 Context.setcudaConfigureCallDecl(
5785 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5786 Context.setcudaGetParameterBufferDecl(
5787 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5788 Context.setcudaLaunchDeviceDecl(
5789 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5790 }
5791
5792 // Re-export any modules that were imported by a non-module AST file.
5793 // FIXME: This does not make macro-only imports visible again.
5794 for (auto &Import : PendingImportedModules) {
5795 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5796 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5797 /*ImportLoc=*/Import.ImportLoc);
5798 if (Import.ImportLoc.isValid())
5799 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5800 // This updates visibility for Preprocessor only. For Sema, which can be
5801 // nullptr here, we do the same later, in UpdateSema().
5802 }
5803 }
5804
5805 // Hand off these modules to Sema.
5806 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5807 PendingImportedModules.clear();
5808}
5809
5810void ASTReader::finalizeForWriting() {
5811 // Nothing to do for now.
5812}
5813
5814/// Reads and return the signature record from \p PCH's control block, or
5815/// else returns 0.
5816static ASTFileSignature readASTFileSignature(StringRef PCH) {
5817 BitstreamCursor Stream(PCH);
5818 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5819 // FIXME this drops the error on the floor.
5820 consumeError(Err: std::move(Err));
5821 return ASTFileSignature();
5822 }
5823
5824 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5825 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5826 return ASTFileSignature();
5827
5828 // Scan for SIGNATURE inside the diagnostic options block.
5829 ASTReader::RecordData Record;
5830 while (true) {
5831 Expected<llvm::BitstreamEntry> MaybeEntry =
5832 Stream.advanceSkippingSubblocks();
5833 if (!MaybeEntry) {
5834 // FIXME this drops the error on the floor.
5835 consumeError(Err: MaybeEntry.takeError());
5836 return ASTFileSignature();
5837 }
5838 llvm::BitstreamEntry Entry = MaybeEntry.get();
5839
5840 if (Entry.Kind != llvm::BitstreamEntry::Record)
5841 return ASTFileSignature();
5842
5843 Record.clear();
5844 StringRef Blob;
5845 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5846 if (!MaybeRecord) {
5847 // FIXME this drops the error on the floor.
5848 consumeError(Err: MaybeRecord.takeError());
5849 return ASTFileSignature();
5850 }
5851 if (SIGNATURE == MaybeRecord.get()) {
5852 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5853 assert(Signature != ASTFileSignature::createDummy() &&
5854 "Dummy AST file signature not backpatched in ASTWriter.");
5855 return Signature;
5856 }
5857 }
5858}
5859
5860/// Retrieve the name of the original source file name
5861/// directly from the AST file, without actually loading the AST
5862/// file.
5863std::string ASTReader::getOriginalSourceFile(
5864 const std::string &ASTFileName, FileManager &FileMgr,
5865 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5866 // Open the AST file.
5867 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5868 /*RequiresNullTerminator=*/false,
5869 /*MaybeLimit=*/std::nullopt,
5870 /*IsText=*/false);
5871 if (!Buffer) {
5872 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5873 << ASTFileName << Buffer.getError().message();
5874 return std::string();
5875 }
5876
5877 // Initialize the stream
5878 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5879
5880 // Sniff for the signature.
5881 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5882 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5883 return std::string();
5884 }
5885
5886 // Scan for the CONTROL_BLOCK_ID block.
5887 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5888 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5889 return std::string();
5890 }
5891
5892 // Scan for ORIGINAL_FILE inside the control block.
5893 RecordData Record;
5894 while (true) {
5895 Expected<llvm::BitstreamEntry> MaybeEntry =
5896 Stream.advanceSkippingSubblocks();
5897 if (!MaybeEntry) {
5898 // FIXME this drops errors on the floor.
5899 consumeError(Err: MaybeEntry.takeError());
5900 return std::string();
5901 }
5902 llvm::BitstreamEntry Entry = MaybeEntry.get();
5903
5904 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5905 return std::string();
5906
5907 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5908 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5909 return std::string();
5910 }
5911
5912 Record.clear();
5913 StringRef Blob;
5914 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5915 if (!MaybeRecord) {
5916 // FIXME this drops the errors on the floor.
5917 consumeError(Err: MaybeRecord.takeError());
5918 return std::string();
5919 }
5920 if (ORIGINAL_FILE == MaybeRecord.get())
5921 return Blob.str();
5922 }
5923}
5924
5925namespace {
5926
5927 class SimplePCHValidator : public ASTReaderListener {
5928 const LangOptions &ExistingLangOpts;
5929 const CodeGenOptions &ExistingCGOpts;
5930 const TargetOptions &ExistingTargetOpts;
5931 const PreprocessorOptions &ExistingPPOpts;
5932 const HeaderSearchOptions &ExistingHSOpts;
5933 std::string ExistingSpecificModuleCachePath;
5934 FileManager &FileMgr;
5935 bool StrictOptionMatches;
5936
5937 public:
5938 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5939 const CodeGenOptions &ExistingCGOpts,
5940 const TargetOptions &ExistingTargetOpts,
5941 const PreprocessorOptions &ExistingPPOpts,
5942 const HeaderSearchOptions &ExistingHSOpts,
5943 StringRef ExistingSpecificModuleCachePath,
5944 FileManager &FileMgr, bool StrictOptionMatches)
5945 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5946 ExistingTargetOpts(ExistingTargetOpts),
5947 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5948 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5949 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5950
5951 bool ReadLanguageOptions(const LangOptions &LangOpts,
5952 StringRef ModuleFilename, bool Complain,
5953 bool AllowCompatibleDifferences) override {
5954 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5955 Diags: nullptr, AllowCompatibleDifferences);
5956 }
5957
5958 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5959 StringRef ModuleFilename, bool Complain,
5960 bool AllowCompatibleDifferences) override {
5961 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5962 Diags: nullptr, AllowCompatibleDifferences);
5963 }
5964
5965 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5966 StringRef ModuleFilename, bool Complain,
5967 bool AllowCompatibleDifferences) override {
5968 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
5969 Diags: nullptr, AllowCompatibleDifferences);
5970 }
5971
5972 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5973 StringRef ASTFilename, StringRef ContextHash,
5974 bool Complain) override {
5975 return checkModuleCachePath(
5976 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
5977 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5978 }
5979
5980 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5981 StringRef ModuleFilename, bool ReadMacros,
5982 bool Complain,
5983 std::string &SuggestedPredefines) override {
5984 return checkPreprocessorOptions(
5985 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5986 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5987 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5988 : OptionValidateContradictions);
5989 }
5990 };
5991
5992} // namespace
5993
5994bool ASTReader::readASTFileControlBlock(
5995 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5996 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5997 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5998 unsigned ClientLoadCapabilities) {
5999 // Open the AST file.
6000 off_t Size;
6001 time_t ModTime;
6002 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
6003 llvm::MemoryBuffer *Buffer =
6004 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
6005 if (!Buffer) {
6006 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
6007 // read again later, but we do not have the context here to determine if it
6008 // is safe to change the result of InMemoryModuleCache::getPCMState().
6009
6010 // FIXME: This allows use of the VFS; we do not allow use of the
6011 // VFS when actually loading a module.
6012 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
6013 : FileMgr.getFileRef(Filename,
6014 /*OpenFile=*/false,
6015 /*CacheFailure=*/true,
6016 /*IsText=*/false);
6017 if (!Entry) {
6018 llvm::consumeError(Err: Entry.takeError());
6019 return true;
6020 }
6021 auto BufferOrErr =
6022 FileMgr.getBufferForFile(Entry: *Entry,
6023 /*IsVolatile=*/isVolatile: false,
6024 /*RequiresNullTerminator=*/false,
6025 /*MaybeLimit=*/std::nullopt,
6026 /*IsText=*/false);
6027 if (!BufferOrErr)
6028 return true;
6029 OwnedBuffer = std::move(*BufferOrErr);
6030 Buffer = OwnedBuffer.get();
6031 }
6032
6033 // Initialize the stream
6034 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6035 BitstreamCursor Stream(Bytes);
6036
6037 // Sniff for the signature.
6038 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6039 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6040 return true;
6041 }
6042
6043 // Scan for the CONTROL_BLOCK_ID block.
6044 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6045 return true;
6046
6047 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6048 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6049 bool NeedsImports = Listener.needsImportVisitation();
6050 BitstreamCursor InputFilesCursor;
6051 uint64_t InputFilesOffsetBase = 0;
6052
6053 RecordData Record;
6054 std::string ModuleDir;
6055 bool DoneWithControlBlock = false;
6056 SmallString<0> PathBuf;
6057 PathBuf.reserve(N: 256);
6058 // Additional path buffer to use when multiple paths need to be resolved.
6059 // For example, when deserializing input files that contains a path that was
6060 // resolved from a vfs overlay and an external location.
6061 SmallString<0> AdditionalPathBuf;
6062 AdditionalPathBuf.reserve(N: 256);
6063 while (!DoneWithControlBlock) {
6064 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6065 if (!MaybeEntry) {
6066 // FIXME this drops the error on the floor.
6067 consumeError(Err: MaybeEntry.takeError());
6068 return true;
6069 }
6070 llvm::BitstreamEntry Entry = MaybeEntry.get();
6071
6072 switch (Entry.Kind) {
6073 case llvm::BitstreamEntry::SubBlock: {
6074 switch (Entry.ID) {
6075 case OPTIONS_BLOCK_ID: {
6076 std::string IgnoredSuggestedPredefines;
6077 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6078 /*AllowCompatibleConfigurationMismatch*/ false,
6079 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6080 return true;
6081 break;
6082 }
6083
6084 case INPUT_FILES_BLOCK_ID:
6085 InputFilesCursor = Stream;
6086 if (llvm::Error Err = Stream.SkipBlock()) {
6087 // FIXME this drops the error on the floor.
6088 consumeError(Err: std::move(Err));
6089 return true;
6090 }
6091 if (NeedsInputFiles &&
6092 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6093 return true;
6094 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6095 break;
6096
6097 default:
6098 if (llvm::Error Err = Stream.SkipBlock()) {
6099 // FIXME this drops the error on the floor.
6100 consumeError(Err: std::move(Err));
6101 return true;
6102 }
6103 break;
6104 }
6105
6106 continue;
6107 }
6108
6109 case llvm::BitstreamEntry::EndBlock:
6110 DoneWithControlBlock = true;
6111 break;
6112
6113 case llvm::BitstreamEntry::Error:
6114 return true;
6115
6116 case llvm::BitstreamEntry::Record:
6117 break;
6118 }
6119
6120 if (DoneWithControlBlock) break;
6121
6122 Record.clear();
6123 StringRef Blob;
6124 Expected<unsigned> MaybeRecCode =
6125 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6126 if (!MaybeRecCode) {
6127 // FIXME this drops the error.
6128 return Failure;
6129 }
6130 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6131 case METADATA:
6132 if (Record[0] != VERSION_MAJOR)
6133 return true;
6134 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6135 return true;
6136 break;
6137 case MODULE_NAME:
6138 Listener.ReadModuleName(ModuleName: Blob);
6139 break;
6140 case MODULE_DIRECTORY:
6141 ModuleDir = std::string(Blob);
6142 break;
6143 case MODULE_MAP_FILE: {
6144 unsigned Idx = 0;
6145 std::string PathStr = ReadString(Record, Idx);
6146 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6147 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6148 break;
6149 }
6150 case INPUT_FILE_OFFSETS: {
6151 if (!NeedsInputFiles)
6152 break;
6153
6154 unsigned NumInputFiles = Record[0];
6155 unsigned NumUserFiles = Record[1];
6156 const llvm::support::unaligned_uint64_t *InputFileOffs =
6157 (const llvm::support::unaligned_uint64_t *)Blob.data();
6158 for (unsigned I = 0; I != NumInputFiles; ++I) {
6159 // Go find this input file.
6160 bool isSystemFile = I >= NumUserFiles;
6161
6162 if (isSystemFile && !NeedsSystemInputFiles)
6163 break; // the rest are system input files
6164
6165 BitstreamCursor &Cursor = InputFilesCursor;
6166 SavedStreamPosition SavedPosition(Cursor);
6167 if (llvm::Error Err =
6168 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6169 // FIXME this drops errors on the floor.
6170 consumeError(Err: std::move(Err));
6171 }
6172
6173 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6174 if (!MaybeCode) {
6175 // FIXME this drops errors on the floor.
6176 consumeError(Err: MaybeCode.takeError());
6177 }
6178 unsigned Code = MaybeCode.get();
6179
6180 RecordData Record;
6181 StringRef Blob;
6182 bool shouldContinue = false;
6183 Expected<unsigned> MaybeRecordType =
6184 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6185 if (!MaybeRecordType) {
6186 // FIXME this drops errors on the floor.
6187 consumeError(Err: MaybeRecordType.takeError());
6188 }
6189 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6190 case INPUT_FILE_HASH:
6191 break;
6192 case INPUT_FILE:
6193 time_t StoredTime = static_cast<time_t>(Record[2]);
6194 bool Overridden = static_cast<bool>(Record[3]);
6195 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6196 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6197 auto FilenameAsRequestedBuf = ResolveImportedPath(
6198 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6199 StringRef Filename;
6200 if (UnresolvedFilename.empty())
6201 Filename = *FilenameAsRequestedBuf;
6202 else {
6203 auto FilenameBuf = ResolveImportedPath(
6204 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6205 Filename = *FilenameBuf;
6206 }
6207 shouldContinue = Listener.visitInputFileAsRequested(
6208 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6209 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6210 break;
6211 }
6212 if (!shouldContinue)
6213 break;
6214 }
6215 break;
6216 }
6217
6218 case IMPORT: {
6219 if (!NeedsImports)
6220 break;
6221
6222 unsigned Idx = 0;
6223 // Read information about the AST file.
6224
6225 // Skip Kind
6226 Idx++;
6227
6228 // Skip ImportLoc
6229 Idx++;
6230
6231 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6232
6233 bool IsStandardCXXModule = Record[Idx++];
6234
6235 // In C++20 Modules, we don't record the path to imported
6236 // modules in the BMI files.
6237 if (IsStandardCXXModule) {
6238 Listener.visitImport(ModuleName, /*Filename=*/"");
6239 continue;
6240 }
6241
6242 // Skip Size, ModTime and ImplicitModuleSuffix.
6243 Idx += 1 + 1 + 1;
6244 // Skip signature.
6245 Blob = Blob.substr(Start: ASTFileSignature::size);
6246
6247 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6248 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6249 Listener.visitImport(ModuleName, Filename: *Filename);
6250 break;
6251 }
6252
6253 default:
6254 // No other validation to perform.
6255 break;
6256 }
6257 }
6258
6259 // Look for module file extension blocks, if requested.
6260 if (FindModuleFileExtensions) {
6261 BitstreamCursor SavedStream = Stream;
6262 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6263 bool DoneWithExtensionBlock = false;
6264 while (!DoneWithExtensionBlock) {
6265 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6266 if (!MaybeEntry) {
6267 // FIXME this drops the error.
6268 return true;
6269 }
6270 llvm::BitstreamEntry Entry = MaybeEntry.get();
6271
6272 switch (Entry.Kind) {
6273 case llvm::BitstreamEntry::SubBlock:
6274 if (llvm::Error Err = Stream.SkipBlock()) {
6275 // FIXME this drops the error on the floor.
6276 consumeError(Err: std::move(Err));
6277 return true;
6278 }
6279 continue;
6280
6281 case llvm::BitstreamEntry::EndBlock:
6282 DoneWithExtensionBlock = true;
6283 continue;
6284
6285 case llvm::BitstreamEntry::Error:
6286 return true;
6287
6288 case llvm::BitstreamEntry::Record:
6289 break;
6290 }
6291
6292 Record.clear();
6293 StringRef Blob;
6294 Expected<unsigned> MaybeRecCode =
6295 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6296 if (!MaybeRecCode) {
6297 // FIXME this drops the error.
6298 return true;
6299 }
6300 switch (MaybeRecCode.get()) {
6301 case EXTENSION_METADATA: {
6302 ModuleFileExtensionMetadata Metadata;
6303 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6304 return true;
6305
6306 Listener.readModuleFileExtension(Metadata);
6307 break;
6308 }
6309 }
6310 }
6311 }
6312 Stream = std::move(SavedStream);
6313 }
6314
6315 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6316 if (readUnhashedControlBlockImpl(
6317 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6318 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6319 ValidateDiagnosticOptions) != Success)
6320 return true;
6321
6322 return false;
6323}
6324
6325bool ASTReader::isAcceptableASTFile(
6326 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6327 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6328 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6329 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6330 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6331 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6332 SpecificModuleCachePath, FileMgr,
6333 RequireStrictOptionMatches);
6334 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6335 /*FindModuleFileExtensions=*/false, Listener&: validator,
6336 /*ValidateDiagnosticOptions=*/true);
6337}
6338
6339Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6340 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6341 assert(GlobalID == 0 && "Unhandled global submodule ID");
6342 return nullptr;
6343 }
6344
6345 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6346 if (GlobalIndex >= SubmodulesLoaded.size()) {
6347 Error(Msg: "submodule ID out of range in AST file");
6348 return nullptr;
6349 }
6350
6351 if (SubmodulesLoaded[GlobalIndex])
6352 return SubmodulesLoaded[GlobalIndex];
6353
6354 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6355 assert(It != GlobalSubmoduleMap.end());
6356 ModuleFile &F = *It->second;
6357 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6358 [[maybe_unused]] unsigned LocalID =
6359 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6360
6361 BitstreamCursor &Cursor = F.SubmodulesCursor;
6362 SavedStreamPosition SavedPosition(Cursor);
6363 unsigned Offset = F.SubmoduleOffsets[Index];
6364 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.SubmodulesOffsetBase + Offset)) {
6365 Error(Err: std::move(Err));
6366 return nullptr;
6367 }
6368
6369 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6370 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6371 // If we don't know the top-level module, there's no point in doing qualified
6372 // lookup of its submodules; it won't find anything anywhere within this tree.
6373 // Let's skip that and avoid some string lookups.
6374 auto CreateModule = !KnowsTopLevelModule
6375 ? &ModuleMap::createModule
6376 : &ModuleMap::findOrCreateModuleFirst;
6377
6378 Module *CurrentModule = nullptr;
6379 RecordData Record;
6380 while (true) {
6381 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6382 if (!MaybeEntry) {
6383 Error(Err: MaybeEntry.takeError());
6384 return nullptr;
6385 }
6386 llvm::BitstreamEntry Entry = MaybeEntry.get();
6387
6388 switch (Entry.Kind) {
6389 case llvm::BitstreamEntry::SubBlock:
6390 case llvm::BitstreamEntry::Error:
6391 case llvm::BitstreamEntry::EndBlock: {
6392 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6393 Fmt: "malformed block record in AST file"));
6394 return nullptr;
6395 }
6396 case llvm::BitstreamEntry::Record:
6397 // The interesting case.
6398 break;
6399 }
6400
6401 // Read a record.
6402 StringRef Blob;
6403 Record.clear();
6404 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6405 if (!MaybeKind) {
6406 Error(Err: MaybeKind.takeError());
6407 return nullptr;
6408 }
6409 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6410
6411 switch (Kind) {
6412 case SUBMODULE_END:
6413 if (!CurrentModule) {
6414 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6415 Fmt: "malformed module definition"));
6416 return nullptr;
6417 }
6418 return CurrentModule;
6419
6420 case SUBMODULE_DEFINITION: {
6421 if (Record.size() < 13) {
6422 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6423 Fmt: "malformed module definition"));
6424 return nullptr;
6425 }
6426
6427 StringRef Name = Blob;
6428 unsigned Idx = 0;
6429 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6430 assert(LocalID == ReadLocalID);
6431 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6432 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6433 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6434 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6435 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6436 bool IsFramework = Record[Idx++];
6437 bool IsExplicit = Record[Idx++];
6438 bool IsSystem = Record[Idx++];
6439 bool IsExternC = Record[Idx++];
6440 bool InferSubmodules = Record[Idx++];
6441 bool InferExplicitSubmodules = Record[Idx++];
6442 bool InferExportWildcard = Record[Idx++];
6443 bool ConfigMacrosExhaustive = Record[Idx++];
6444 bool ModuleMapIsPrivate = Record[Idx++];
6445 bool NamedModuleHasInit = Record[Idx++];
6446
6447 Module *ParentModule = nullptr;
6448 if (Parent) {
6449 ParentModule = getSubmodule(GlobalID: Parent);
6450 if (!ParentModule)
6451 return nullptr;
6452 }
6453
6454 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6455 args&: IsFramework, args&: IsExplicit);
6456
6457 if (!ParentModule) {
6458 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6459 CurrentModule->getASTFileKey()) {
6460 // Don't emit module relocation error if we have -fno-validate-pch
6461 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6462 DisableValidationForModuleKind::Module)) {
6463 assert(*CurFileKey != F.FileKey &&
6464 "ModuleManager did not de-duplicate");
6465
6466 Diag(DiagID: diag::err_module_file_conflict)
6467 << CurrentModule->getTopLevelModuleName()
6468 << *CurrentModule->getASTFileName() << F.FileName;
6469
6470 auto CurModMapFile =
6471 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6472 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6473 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6474 Diag(DiagID: diag::note_module_file_conflict)
6475 << CurModMapFile->getName() << ModMapFile->getName();
6476
6477 return nullptr;
6478 }
6479 }
6480
6481 F.DidReadTopLevelSubmodule = true;
6482 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6483 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6484 }
6485
6486 CurrentModule->Kind = Kind;
6487 // Note that we may be rewriting an existing location and it is important
6488 // to keep doing that. In particular, we would like to prefer a
6489 // `DefinitionLoc` loaded from the module file instead of the location
6490 // created in the current source manager, because it allows the new
6491 // location to be marked as "unaffecting" when writing and avoid creating
6492 // duplicate locations for the same module map file.
6493 CurrentModule->DefinitionLoc = DefinitionLoc;
6494 CurrentModule->Signature = F.Signature;
6495 CurrentModule->IsFromModuleFile = true;
6496 if (InferredAllowedBy.isValid())
6497 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6498 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6499 CurrentModule->IsExternC = IsExternC;
6500 CurrentModule->InferSubmodules = InferSubmodules;
6501 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6502 CurrentModule->InferExportWildcard = InferExportWildcard;
6503 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6504 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6505 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6506
6507 if (!ParentModule && !F.BaseDirectory.empty()) {
6508 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6509 CurrentModule->Directory = *Dir;
6510 } else if (ParentModule && ParentModule->Directory) {
6511 // Submodules inherit the directory from their parent.
6512 CurrentModule->Directory = ParentModule->Directory;
6513 }
6514
6515 if (DeserializationListener)
6516 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6517
6518 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6519
6520 // Clear out data that will be replaced by what is in the module file.
6521 CurrentModule->LinkLibraries.clear();
6522 CurrentModule->ConfigMacros.clear();
6523 CurrentModule->UnresolvedConflicts.clear();
6524 CurrentModule->Conflicts.clear();
6525
6526 // The module is available unless it's missing a requirement; relevant
6527 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6528 // Missing headers that were present when the module was built do not
6529 // make it unavailable -- if we got this far, this must be an explicitly
6530 // imported module file.
6531 CurrentModule->Requirements.clear();
6532 CurrentModule->MissingHeaders.clear();
6533 CurrentModule->IsUnimportable =
6534 ParentModule && ParentModule->IsUnimportable;
6535 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6536 break;
6537 }
6538
6539 case SUBMODULE_UMBRELLA_HEADER: {
6540 SmallString<128> RelativePathName;
6541 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6542 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6543 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6544 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6545 PathRelativeToRootModuleDirectory: RelativePathName);
6546 }
6547 // Note that it's too late at this point to return out of date if the
6548 // name from the PCM doesn't match up with the one in the module map,
6549 // but also quite unlikely since we will have already checked the
6550 // modification time and size of the module map file itself.
6551 }
6552 break;
6553 }
6554
6555 case SUBMODULE_HEADER:
6556 case SUBMODULE_EXCLUDED_HEADER:
6557 case SUBMODULE_PRIVATE_HEADER:
6558 // We lazily associate headers with their modules via the HeaderInfo table.
6559 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6560 // of complete filenames or remove it entirely.
6561 break;
6562
6563 case SUBMODULE_TEXTUAL_HEADER:
6564 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6565 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6566 // them here.
6567 break;
6568
6569 case SUBMODULE_TOPHEADER: {
6570 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6571 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6572 break;
6573 }
6574
6575 case SUBMODULE_UMBRELLA_DIR: {
6576 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6577 if (auto Umbrella =
6578 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6579 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6580 // FIXME: NameAsWritten
6581 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6582 }
6583 }
6584 break;
6585 }
6586
6587 case SUBMODULE_IMPORTS:
6588 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6589 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6590 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6591 }
6592 break;
6593
6594 case SUBMODULE_AFFECTING_MODULES:
6595 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6596 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6597 CurrentModule->AffectingClangModules.push_back(
6598 Elt: ModuleRef(this, GlobalID));
6599 }
6600 break;
6601
6602 case SUBMODULE_EXPORTS:
6603 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6604 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6605 bool IsWildcard = Record[Idx + 1];
6606 ModuleRef ExportedMod =
6607 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6608 if (ExportedMod || IsWildcard)
6609 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6610 }
6611
6612 // Once we've loaded the set of exports, there's no reason to keep
6613 // the parsed, unresolved exports around.
6614 CurrentModule->UnresolvedExports.clear();
6615 break;
6616
6617 case SUBMODULE_REQUIRES:
6618 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6619 Target: PP.getTargetInfo());
6620 break;
6621
6622 case SUBMODULE_LINK_LIBRARY:
6623 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6624 CurrentModule->LinkLibraries.push_back(
6625 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6626 break;
6627
6628 case SUBMODULE_CONFIG_MACRO:
6629 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6630 break;
6631
6632 case SUBMODULE_CONFLICT: {
6633 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6634 Module::Conflict Conflict;
6635 Conflict.Other = ModuleRef(this, GlobalID);
6636 Conflict.Message = Blob.str();
6637 CurrentModule->Conflicts.push_back(x: Conflict);
6638 break;
6639 }
6640
6641 case SUBMODULE_INITIALIZERS: {
6642 if (!ContextObj)
6643 break;
6644 // Standard C++ module has its own way to initialize variables.
6645 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6646 SmallVector<GlobalDeclID, 16> Inits;
6647 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6648 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6649 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6650 }
6651 break;
6652 }
6653
6654 case SUBMODULE_EXPORT_AS:
6655 CurrentModule->ExportAsModule = Blob.str();
6656 ModMap.addLinkAsDependency(Mod: CurrentModule);
6657 break;
6658
6659 case SUBMODULE_CHILD: {
6660 // Record a not-yet-loaded direct child for on-demand deserialization.
6661 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6662 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6663 break;
6664 }
6665 }
6666 }
6667}
6668
6669/// Parse the record that corresponds to a LangOptions data
6670/// structure.
6671///
6672/// This routine parses the language options from the AST file and then gives
6673/// them to the AST listener if one is set.
6674///
6675/// \returns true if the listener deems the file unacceptable, false otherwise.
6676bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6677 StringRef ModuleFilename, bool Complain,
6678 ASTReaderListener &Listener,
6679 bool AllowCompatibleDifferences) {
6680 LangOptions LangOpts;
6681 unsigned Idx = 0;
6682 LangOpts.LangStd = static_cast<LangStandard::Kind>(Record[Idx++]);
6683#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6684 LangOpts.Name = Record[Idx++];
6685#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6686 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6687#include "clang/Basic/LangOptions.def"
6688#define SANITIZER(NAME, ID) \
6689 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6690#include "clang/Basic/Sanitizers.def"
6691
6692 for (unsigned N = Record[Idx++]; N; --N)
6693 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6694
6695 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6696 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6697 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6698
6699 LangOpts.CurrentModule = ReadString(Record, Idx);
6700
6701 // Comment options.
6702 for (unsigned N = Record[Idx++]; N; --N) {
6703 LangOpts.CommentOpts.BlockCommandNames.push_back(
6704 x: ReadString(Record, Idx));
6705 }
6706 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6707
6708 // OpenMP offloading options.
6709 for (unsigned N = Record[Idx++]; N; --N) {
6710 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6711 }
6712
6713 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6714
6715 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6716 AllowCompatibleDifferences);
6717}
6718
6719bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6720 StringRef ModuleFilename, bool Complain,
6721 ASTReaderListener &Listener,
6722 bool AllowCompatibleDifferences) {
6723 unsigned Idx = 0;
6724 CodeGenOptions CGOpts;
6725 using CK = CodeGenOptions::CompatibilityKind;
6726#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6727 if constexpr (CK::Compatibility != CK::Benign) \
6728 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6729#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6730 if constexpr (CK::Compatibility != CK::Benign) \
6731 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6732#define DEBUGOPT(Name, Bits, Default, Compatibility)
6733#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6734#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6735#include "clang/Basic/CodeGenOptions.def"
6736
6737 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6738 AllowCompatibleDifferences);
6739}
6740
6741bool ASTReader::ParseTargetOptions(const RecordData &Record,
6742 StringRef ModuleFilename, bool Complain,
6743 ASTReaderListener &Listener,
6744 bool AllowCompatibleDifferences) {
6745 unsigned Idx = 0;
6746 TargetOptions TargetOpts;
6747 TargetOpts.Triple = ReadString(Record, Idx);
6748 TargetOpts.CPU = ReadString(Record, Idx);
6749 TargetOpts.TuneCPU = ReadString(Record, Idx);
6750 TargetOpts.ABI = ReadString(Record, Idx);
6751 for (unsigned N = Record[Idx++]; N; --N) {
6752 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6753 }
6754 for (unsigned N = Record[Idx++]; N; --N) {
6755 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6756 }
6757
6758 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6759 AllowCompatibleDifferences);
6760}
6761
6762bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6763 StringRef ModuleFilename, bool Complain,
6764 ASTReaderListener &Listener) {
6765 DiagnosticOptions DiagOpts;
6766 unsigned Idx = 0;
6767#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6768#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6769 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6770#include "clang/Basic/DiagnosticOptions.def"
6771
6772 for (unsigned N = Record[Idx++]; N; --N)
6773 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6774 for (unsigned N = Record[Idx++]; N; --N)
6775 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6776
6777 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6778}
6779
6780bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6781 ASTReaderListener &Listener) {
6782 FileSystemOptions FSOpts;
6783 unsigned Idx = 0;
6784 FSOpts.WorkingDir = ReadString(Record, Idx);
6785 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6786}
6787
6788bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6789 StringRef ModuleFilename,
6790 bool Complain,
6791 ASTReaderListener &Listener) {
6792 HeaderSearchOptions HSOpts;
6793 unsigned Idx = 0;
6794 HSOpts.Sysroot = ReadString(Record, Idx);
6795
6796 HSOpts.ResourceDir = ReadString(Record, Idx);
6797 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6798 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6799 HSOpts.DisableModuleHash = Record[Idx++];
6800 HSOpts.ImplicitModuleMaps = Record[Idx++];
6801 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6802 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6803 HSOpts.UseBuiltinIncludes = Record[Idx++];
6804 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6805 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6806 HSOpts.UseLibcxx = Record[Idx++];
6807 std::string ContextHash = ReadString(Record, Idx);
6808
6809 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6810 Complain);
6811}
6812
6813bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6814 ASTReaderListener &Listener) {
6815 HeaderSearchOptions HSOpts;
6816 unsigned Idx = 0;
6817
6818 // Include entries.
6819 for (unsigned N = Record[Idx++]; N; --N) {
6820 std::string Path = ReadString(Record, Idx);
6821 frontend::IncludeDirGroup Group
6822 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6823 bool IsFramework = Record[Idx++];
6824 bool IgnoreSysRoot = Record[Idx++];
6825 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6826 args&: IgnoreSysRoot);
6827 }
6828
6829 // System header prefixes.
6830 for (unsigned N = Record[Idx++]; N; --N) {
6831 std::string Prefix = ReadString(Record, Idx);
6832 bool IsSystemHeader = Record[Idx++];
6833 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6834 }
6835
6836 // VFS overlay files.
6837 for (unsigned N = Record[Idx++]; N; --N) {
6838 std::string VFSOverlayFile = ReadString(Record, Idx);
6839 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6840 }
6841
6842 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6843}
6844
6845bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6846 StringRef ModuleFilename,
6847 bool Complain,
6848 ASTReaderListener &Listener,
6849 std::string &SuggestedPredefines) {
6850 PreprocessorOptions PPOpts;
6851 unsigned Idx = 0;
6852
6853 // Macro definitions/undefs
6854 bool ReadMacros = Record[Idx++];
6855 if (ReadMacros) {
6856 for (unsigned N = Record[Idx++]; N; --N) {
6857 std::string Macro = ReadString(Record, Idx);
6858 bool IsUndef = Record[Idx++];
6859 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6860 }
6861 }
6862
6863 // Includes
6864 for (unsigned N = Record[Idx++]; N; --N) {
6865 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6866 }
6867
6868 // Macro Includes
6869 for (unsigned N = Record[Idx++]; N; --N) {
6870 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6871 }
6872
6873 PPOpts.UsePredefines = Record[Idx++];
6874 PPOpts.DetailedRecord = Record[Idx++];
6875 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6876 PPOpts.ObjCXXARCStandardLibrary =
6877 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6878 SuggestedPredefines.clear();
6879 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6880 Complain, SuggestedPredefines);
6881}
6882
6883std::pair<ModuleFile *, unsigned>
6884ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6885 GlobalPreprocessedEntityMapType::iterator
6886 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6887 assert(I != GlobalPreprocessedEntityMap.end() &&
6888 "Corrupted global preprocessed entity map");
6889 ModuleFile *M = I->second;
6890 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6891 return std::make_pair(x&: M, y&: LocalIndex);
6892}
6893
6894llvm::iterator_range<PreprocessingRecord::iterator>
6895ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6896 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6897 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6898 count: Mod.NumPreprocessedEntities);
6899
6900 return llvm::make_range(x: PreprocessingRecord::iterator(),
6901 y: PreprocessingRecord::iterator());
6902}
6903
6904bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6905 unsigned int ClientLoadCapabilities) {
6906 return ClientLoadCapabilities & ARR_OutOfDate &&
6907 !getModuleManager()
6908 .getModuleCache()
6909 .getInMemoryModuleCache()
6910 .isPCMFinal(Filename: ModuleFileName);
6911}
6912
6913llvm::iterator_range<ASTReader::ModuleDeclIterator>
6914ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6915 return llvm::make_range(
6916 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6917 y: ModuleDeclIterator(this, &Mod,
6918 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6919}
6920
6921SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6922 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6923 assert(I != GlobalSkippedRangeMap.end() &&
6924 "Corrupted global skipped range map");
6925 ModuleFile *M = I->second;
6926 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6927 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6928 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6929 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6930 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6931 assert(Range.isValid());
6932 return Range;
6933}
6934
6935unsigned
6936ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6937 unsigned ModuleFileIndex = ID >> 32;
6938 assert(ModuleFileIndex && "not translating loaded MacroID?");
6939 assert(getModuleManager().size() > ModuleFileIndex - 1);
6940 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6941
6942 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6943 return MF.BasePreprocessedEntityID + ID;
6944}
6945
6946PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6947 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6948 ModuleFile &M = *PPInfo.first;
6949 unsigned LocalIndex = PPInfo.second;
6950 PreprocessedEntityID PPID =
6951 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6952 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6953
6954 if (!PP.getPreprocessingRecord()) {
6955 Error(Msg: "no preprocessing record");
6956 return nullptr;
6957 }
6958
6959 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6960 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6961 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6962 Error(Err: std::move(Err));
6963 return nullptr;
6964 }
6965
6966 Expected<llvm::BitstreamEntry> MaybeEntry =
6967 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6968 if (!MaybeEntry) {
6969 Error(Err: MaybeEntry.takeError());
6970 return nullptr;
6971 }
6972 llvm::BitstreamEntry Entry = MaybeEntry.get();
6973
6974 if (Entry.Kind != llvm::BitstreamEntry::Record)
6975 return nullptr;
6976
6977 // Read the record.
6978 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6979 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6980 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6981 StringRef Blob;
6982 RecordData Record;
6983 Expected<unsigned> MaybeRecType =
6984 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6985 if (!MaybeRecType) {
6986 Error(Err: MaybeRecType.takeError());
6987 return nullptr;
6988 }
6989 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6990 case PPD_MACRO_EXPANSION: {
6991 bool isBuiltin = Record[0];
6992 IdentifierInfo *Name = nullptr;
6993 MacroDefinitionRecord *Def = nullptr;
6994 if (isBuiltin)
6995 Name = getLocalIdentifier(M, LocalID: Record[1]);
6996 else {
6997 PreprocessedEntityID GlobalID =
6998 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6999 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
7000 Def =
7001 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
7002 }
7003
7004 MacroExpansion *ME;
7005 if (isBuiltin)
7006 ME = new (PPRec) MacroExpansion(Name, Range);
7007 else
7008 ME = new (PPRec) MacroExpansion(Def, Range);
7009
7010 return ME;
7011 }
7012
7013 case PPD_MACRO_DEFINITION: {
7014 // Decode the identifier info and then check again; if the macro is
7015 // still defined and associated with the identifier,
7016 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
7017 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
7018
7019 if (DeserializationListener)
7020 DeserializationListener->MacroDefinitionRead(PPID, MD);
7021
7022 return MD;
7023 }
7024
7025 case PPD_INCLUSION_DIRECTIVE: {
7026 const char *FullFileNameStart = Blob.data() + Record[0];
7027 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7028 OptionalFileEntryRef File;
7029 if (!FullFileName.empty())
7030 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7031
7032 // FIXME: Stable encoding
7033 InclusionDirective::InclusionKind Kind
7034 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7035 InclusionDirective *ID
7036 = new (PPRec) InclusionDirective(PPRec, Kind,
7037 StringRef(Blob.data(), Record[0]),
7038 Record[1], Record[3],
7039 File,
7040 Range);
7041 return ID;
7042 }
7043 }
7044
7045 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7046}
7047
7048/// Find the next module that contains entities and return the ID
7049/// of the first entry.
7050///
7051/// \param SLocMapI points at a chunk of a module that contains no
7052/// preprocessed entities or the entities it contains are not the ones we are
7053/// looking for.
7054unsigned ASTReader::findNextPreprocessedEntity(
7055 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7056 ++SLocMapI;
7057 for (GlobalSLocOffsetMapType::const_iterator
7058 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7059 ModuleFile &M = *SLocMapI->second;
7060 if (M.NumPreprocessedEntities)
7061 return M.BasePreprocessedEntityID;
7062 }
7063
7064 return getTotalNumPreprocessedEntities();
7065}
7066
7067namespace {
7068
7069struct PPEntityComp {
7070 const ASTReader &Reader;
7071 ModuleFile &M;
7072
7073 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7074
7075 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7076 SourceLocation LHS = getLoc(PPE: L);
7077 SourceLocation RHS = getLoc(PPE: R);
7078 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7079 }
7080
7081 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7082 SourceLocation LHS = getLoc(PPE: L);
7083 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7084 }
7085
7086 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7087 SourceLocation RHS = getLoc(PPE: R);
7088 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7089 }
7090
7091 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7092 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7093 }
7094};
7095
7096} // namespace
7097
7098unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7099 bool EndsAfter) const {
7100 if (SourceMgr.isLocalSourceLocation(Loc))
7101 return getTotalNumPreprocessedEntities();
7102
7103 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7104 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7105 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7106 "Corrupted global sloc offset map");
7107
7108 if (SLocMapI->second->NumPreprocessedEntities == 0)
7109 return findNextPreprocessedEntity(SLocMapI);
7110
7111 ModuleFile &M = *SLocMapI->second;
7112
7113 using pp_iterator = const PPEntityOffset *;
7114
7115 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7116 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7117
7118 size_t Count = M.NumPreprocessedEntities;
7119 size_t Half;
7120 pp_iterator First = pp_begin;
7121 pp_iterator PPI;
7122
7123 if (EndsAfter) {
7124 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7125 comp: PPEntityComp(*this, M));
7126 } else {
7127 // Do a binary search manually instead of using std::lower_bound because
7128 // The end locations of entities may be unordered (when a macro expansion
7129 // is inside another macro argument), but for this case it is not important
7130 // whether we get the first macro expansion or its containing macro.
7131 while (Count > 0) {
7132 Half = Count / 2;
7133 PPI = First;
7134 std::advance(i&: PPI, n: Half);
7135 if (SourceMgr.isBeforeInTranslationUnit(
7136 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7137 First = PPI;
7138 ++First;
7139 Count = Count - Half - 1;
7140 } else
7141 Count = Half;
7142 }
7143 }
7144
7145 if (PPI == pp_end)
7146 return findNextPreprocessedEntity(SLocMapI);
7147
7148 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7149}
7150
7151/// Returns a pair of [Begin, End) indices of preallocated
7152/// preprocessed entities that \arg Range encompasses.
7153std::pair<unsigned, unsigned>
7154 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7155 if (Range.isInvalid())
7156 return std::make_pair(x: 0,y: 0);
7157 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7158
7159 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7160 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7161 return std::make_pair(x&: BeginID, y&: EndID);
7162}
7163
7164/// Optionally returns true or false if the preallocated preprocessed
7165/// entity with index \arg Index came from file \arg FID.
7166std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7167 FileID FID) {
7168 if (FID.isInvalid())
7169 return false;
7170
7171 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7172 ModuleFile &M = *PPInfo.first;
7173 unsigned LocalIndex = PPInfo.second;
7174 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7175
7176 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7177 if (Loc.isInvalid())
7178 return false;
7179
7180 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7181 return true;
7182 else
7183 return false;
7184}
7185
7186namespace {
7187
7188 /// Visitor used to search for information about a header file.
7189 class HeaderFileInfoVisitor {
7190 FileEntryRef FE;
7191 std::optional<HeaderFileInfo> HFI;
7192
7193 public:
7194 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7195
7196 bool operator()(ModuleFile &M) {
7197 HeaderFileInfoLookupTable *Table
7198 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7199 if (!Table)
7200 return false;
7201
7202 // Look in the on-disk hash table for an entry for this file name.
7203 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7204 if (Pos == Table->end())
7205 return false;
7206
7207 HFI = *Pos;
7208 return true;
7209 }
7210
7211 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7212 };
7213
7214} // namespace
7215
7216HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7217 HeaderFileInfoVisitor Visitor(FE);
7218 ModuleMgr.visit(Visitor);
7219 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7220 return *HFI;
7221
7222 return HeaderFileInfo();
7223}
7224
7225void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7226 using DiagState = DiagnosticsEngine::DiagState;
7227 SmallVector<DiagState *, 32> DiagStates;
7228
7229 for (ModuleFile &F : ModuleMgr) {
7230 unsigned Idx = 0;
7231 auto &Record = F.PragmaDiagMappings;
7232 if (Record.empty())
7233 continue;
7234
7235 DiagStates.clear();
7236
7237 auto ReadDiagState = [&](const DiagState &BasedOn,
7238 bool IncludeNonPragmaStates) {
7239 unsigned BackrefID = Record[Idx++];
7240 if (BackrefID != 0)
7241 return DiagStates[BackrefID - 1];
7242
7243 // A new DiagState was created here.
7244 Diag.DiagStates.push_back(x: BasedOn);
7245 DiagState *NewState = &Diag.DiagStates.back();
7246 DiagStates.push_back(Elt: NewState);
7247 unsigned Size = Record[Idx++];
7248 assert(Idx + Size * 2 <= Record.size() &&
7249 "Invalid data, not enough diag/map pairs");
7250 while (Size--) {
7251 unsigned DiagID = Record[Idx++];
7252 DiagnosticMapping NewMapping =
7253 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7254 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7255 continue;
7256
7257 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7258
7259 // If this mapping was specified as a warning but the severity was
7260 // upgraded due to diagnostic settings, simulate the current diagnostic
7261 // settings (and use a warning).
7262 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7263 NewMapping.setSeverity(diag::Severity::Warning);
7264 NewMapping.setUpgradedFromWarning(false);
7265 }
7266
7267 Mapping = NewMapping;
7268 }
7269 return NewState;
7270 };
7271
7272 // Read the first state.
7273 DiagState *FirstState;
7274 if (F.Kind == MK_ImplicitModule) {
7275 // Implicitly-built modules are reused with different diagnostic
7276 // settings. Use the initial diagnostic state from Diag to simulate this
7277 // compilation's diagnostic settings.
7278 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7279 DiagStates.push_back(Elt: FirstState);
7280
7281 // Skip the initial diagnostic state from the serialized module.
7282 assert(Record[1] == 0 &&
7283 "Invalid data, unexpected backref in initial state");
7284 Idx = 3 + Record[2] * 2;
7285 assert(Idx < Record.size() &&
7286 "Invalid data, not enough state change pairs in initial state");
7287 } else if (F.isModule()) {
7288 // For an explicit module, preserve the flags from the module build
7289 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7290 // -Wblah flags.
7291 unsigned Flags = Record[Idx++];
7292 DiagState Initial(*Diag.getDiagnosticIDs());
7293 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7294 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7295 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7296 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7297 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7298 Initial.ExtBehavior = (diag::Severity)Flags;
7299 FirstState = ReadDiagState(Initial, true);
7300
7301 assert(F.OriginalSourceFileID.isValid());
7302
7303 // Set up the root buffer of the module to start with the initial
7304 // diagnostic state of the module itself, to cover files that contain no
7305 // explicit transitions (for which we did not serialize anything).
7306 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7307 .StateTransitions.push_back(Elt: {FirstState, 0});
7308 } else {
7309 // For prefix ASTs, start with whatever the user configured on the
7310 // command line.
7311 Idx++; // Skip flags.
7312 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7313 }
7314
7315 // Read the state transitions.
7316 unsigned NumLocations = Record[Idx++];
7317 while (NumLocations--) {
7318 assert(Idx < Record.size() &&
7319 "Invalid data, missing pragma diagnostic states");
7320 FileID FID = ReadFileID(F, Record, Idx);
7321 assert(FID.isValid() && "invalid FileID for transition");
7322 unsigned Transitions = Record[Idx++];
7323
7324 // Note that we don't need to set up Parent/ParentOffset here, because
7325 // we won't be changing the diagnostic state within imported FileIDs
7326 // (other than perhaps appending to the main source file, which has no
7327 // parent).
7328 auto &F = Diag.DiagStatesByLoc.Files[FID];
7329 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7330 for (unsigned I = 0; I != Transitions; ++I) {
7331 unsigned Offset = Record[Idx++];
7332 auto *State = ReadDiagState(*FirstState, false);
7333 F.StateTransitions.push_back(Elt: {State, Offset});
7334 }
7335 }
7336
7337 // Read the final state.
7338 assert(Idx < Record.size() &&
7339 "Invalid data, missing final pragma diagnostic state");
7340 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7341 auto *CurState = ReadDiagState(*FirstState, false);
7342
7343 if (!F.isModule()) {
7344 Diag.DiagStatesByLoc.CurDiagState = CurState;
7345 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7346
7347 // Preserve the property that the imaginary root file describes the
7348 // current state.
7349 FileID NullFile;
7350 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7351 if (T.empty())
7352 T.push_back(Elt: {CurState, 0});
7353 else
7354 T[0].State = CurState;
7355 }
7356
7357 // Restore the push stack so that unmatched pushes from a preamble are
7358 // visible when the main file is parsed, allowing the corresponding
7359 // `#pragma diagnostic pop` to succeed.
7360 assert(Idx < Record.size() &&
7361 "Invalid data, missing diagnostic push stack");
7362 unsigned NumPushes = Record[Idx++];
7363 for (unsigned I = 0; I != NumPushes; ++I) {
7364 auto *State = ReadDiagState(*FirstState, false);
7365 if (!F.isModule())
7366 Diag.DiagStateOnPushStack.push_back(x: State);
7367 }
7368
7369 // Don't try to read these mappings again.
7370 Record.clear();
7371 }
7372}
7373
7374/// Get the correct cursor and offset for loading a type.
7375ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7376 auto [M, Index] = translateTypeIDToIndex(ID);
7377 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7378 M->DeclsBlockStartOffset);
7379}
7380
7381static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7382 switch (code) {
7383#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7384 case TYPE_##CODE_ID: return Type::CLASS_ID;
7385#include "clang/Serialization/TypeBitCodes.def"
7386 default:
7387 return std::nullopt;
7388 }
7389}
7390
7391/// Read and return the type with the given index..
7392///
7393/// The index is the type ID, shifted and minus the number of predefs. This
7394/// routine actually reads the record corresponding to the type at the given
7395/// location. It is a helper routine for GetType, which deals with reading type
7396/// IDs.
7397QualType ASTReader::readTypeRecord(TypeID ID) {
7398 assert(ContextObj && "reading type with no AST context");
7399 ASTContext &Context = *ContextObj;
7400 RecordLocation Loc = TypeCursorForIndex(ID);
7401 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7402
7403 // Keep track of where we are in the stream, then jump back there
7404 // after reading this type.
7405 SavedStreamPosition SavedPosition(DeclsCursor);
7406
7407 ReadingKindTracker ReadingKind(Read_Type, *this);
7408
7409 // Note that we are loading a type record.
7410 Deserializing AType(this);
7411
7412 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7413 Error(Err: std::move(Err));
7414 return QualType();
7415 }
7416 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7417 if (!RawCode) {
7418 Error(Err: RawCode.takeError());
7419 return QualType();
7420 }
7421
7422 ASTRecordReader Record(*this, *Loc.F);
7423 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7424 if (!Code) {
7425 Error(Err: Code.takeError());
7426 return QualType();
7427 }
7428 if (Code.get() == TYPE_EXT_QUAL) {
7429 QualType baseType = Record.readQualType();
7430 Qualifiers quals = Record.readQualifiers();
7431 return Context.getQualifiedType(T: baseType, Qs: quals);
7432 }
7433
7434 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7435 if (!maybeClass) {
7436 Error(Msg: "Unexpected code for type");
7437 return QualType();
7438 }
7439
7440 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7441 return TypeReader.read(kind: *maybeClass);
7442}
7443
7444namespace clang {
7445
7446class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7447 ASTRecordReader &Reader;
7448
7449 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7450 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7451
7452 TypeSourceInfo *GetTypeSourceInfo() {
7453 return Reader.readTypeSourceInfo();
7454 }
7455
7456 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7457 return Reader.readNestedNameSpecifierLoc();
7458 }
7459
7460 Attr *ReadAttr() {
7461 return Reader.readAttr();
7462 }
7463
7464public:
7465 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7466
7467 // We want compile-time assurance that we've enumerated all of
7468 // these, so unfortunately we have to declare them first, then
7469 // define them out-of-line.
7470#define ABSTRACT_TYPELOC(CLASS, PARENT)
7471#define TYPELOC(CLASS, PARENT) \
7472 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7473#include "clang/AST/TypeLocNodes.def"
7474
7475 void VisitFunctionTypeLoc(FunctionTypeLoc);
7476 void VisitArrayTypeLoc(ArrayTypeLoc);
7477 void VisitTagTypeLoc(TagTypeLoc TL);
7478};
7479
7480} // namespace clang
7481
7482void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7483 // nothing to do
7484}
7485
7486void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7487 TL.setBuiltinLoc(readSourceLocation());
7488 if (TL.needsExtraLocalData()) {
7489 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7490 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7491 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7492 TL.setModeAttr(Reader.readInt());
7493 }
7494}
7495
7496void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7497 TL.setNameLoc(readSourceLocation());
7498}
7499
7500void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7501 TL.setStarLoc(readSourceLocation());
7502}
7503
7504void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7505 // nothing to do
7506}
7507
7508void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7509 // nothing to do
7510}
7511
7512void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7513 // nothing to do
7514}
7515
7516void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7517 TL.setExpansionLoc(readSourceLocation());
7518}
7519
7520void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7521 TL.setCaretLoc(readSourceLocation());
7522}
7523
7524void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7525 TL.setAmpLoc(readSourceLocation());
7526}
7527
7528void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7529 TL.setAmpAmpLoc(readSourceLocation());
7530}
7531
7532void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7533 TL.setStarLoc(readSourceLocation());
7534 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7535}
7536
7537void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7538 TL.setLBracketLoc(readSourceLocation());
7539 TL.setRBracketLoc(readSourceLocation());
7540 if (Reader.readBool())
7541 TL.setSizeExpr(Reader.readExpr());
7542 else
7543 TL.setSizeExpr(nullptr);
7544}
7545
7546void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7547 VisitArrayTypeLoc(TL);
7548}
7549
7550void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7551 VisitArrayTypeLoc(TL);
7552}
7553
7554void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7555 VisitArrayTypeLoc(TL);
7556}
7557
7558void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7559 DependentSizedArrayTypeLoc TL) {
7560 VisitArrayTypeLoc(TL);
7561}
7562
7563void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7564 DependentAddressSpaceTypeLoc TL) {
7565
7566 TL.setAttrNameLoc(readSourceLocation());
7567 TL.setAttrOperandParensRange(readSourceRange());
7568 TL.setAttrExprOperand(Reader.readExpr());
7569}
7570
7571void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7572 DependentSizedExtVectorTypeLoc TL) {
7573 TL.setNameLoc(readSourceLocation());
7574}
7575
7576void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7577 TL.setNameLoc(readSourceLocation());
7578}
7579
7580void TypeLocReader::VisitDependentVectorTypeLoc(
7581 DependentVectorTypeLoc TL) {
7582 TL.setNameLoc(readSourceLocation());
7583}
7584
7585void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7586 TL.setNameLoc(readSourceLocation());
7587}
7588
7589void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7590 TL.setAttrNameLoc(readSourceLocation());
7591 TL.setAttrOperandParensRange(readSourceRange());
7592 TL.setAttrRowOperand(Reader.readExpr());
7593 TL.setAttrColumnOperand(Reader.readExpr());
7594}
7595
7596void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7597 DependentSizedMatrixTypeLoc TL) {
7598 TL.setAttrNameLoc(readSourceLocation());
7599 TL.setAttrOperandParensRange(readSourceRange());
7600 TL.setAttrRowOperand(Reader.readExpr());
7601 TL.setAttrColumnOperand(Reader.readExpr());
7602}
7603
7604void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7605 TL.setLocalRangeBegin(readSourceLocation());
7606 TL.setLParenLoc(readSourceLocation());
7607 TL.setRParenLoc(readSourceLocation());
7608 TL.setExceptionSpecRange(readSourceRange());
7609 TL.setLocalRangeEnd(readSourceLocation());
7610 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7611 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7612 }
7613}
7614
7615void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7616 VisitFunctionTypeLoc(TL);
7617}
7618
7619void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7620 VisitFunctionTypeLoc(TL);
7621}
7622
7623void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7624 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7625 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7626 SourceLocation NameLoc = readSourceLocation();
7627 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7628}
7629
7630void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7631 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7632 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7633 SourceLocation NameLoc = readSourceLocation();
7634 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7635}
7636
7637void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7638 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7639 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7640 SourceLocation NameLoc = readSourceLocation();
7641 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7642}
7643
7644void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7645 TL.setTypeofLoc(readSourceLocation());
7646 TL.setLParenLoc(readSourceLocation());
7647 TL.setRParenLoc(readSourceLocation());
7648}
7649
7650void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7651 TL.setTypeofLoc(readSourceLocation());
7652 TL.setLParenLoc(readSourceLocation());
7653 TL.setRParenLoc(readSourceLocation());
7654 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7655}
7656
7657void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7658 TL.setDecltypeLoc(readSourceLocation());
7659 TL.setRParenLoc(readSourceLocation());
7660}
7661
7662void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7663 TL.setEllipsisLoc(readSourceLocation());
7664}
7665
7666void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7667 TL.setKWLoc(readSourceLocation());
7668 TL.setLParenLoc(readSourceLocation());
7669 TL.setRParenLoc(readSourceLocation());
7670 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7671}
7672
7673ConceptReference *ASTRecordReader::readConceptReference() {
7674 auto NNS = readNestedNameSpecifierLoc();
7675 auto TemplateKWLoc = readSourceLocation();
7676 auto ConceptNameLoc = readDeclarationNameInfo();
7677 auto FoundDecl = readDeclAs<NamedDecl>();
7678 auto NamedConcept = readTemplateName();
7679 auto *CR = ConceptReference::Create(
7680 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7681 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7682 return CR;
7683}
7684
7685void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7686 TL.setNameLoc(readSourceLocation());
7687 if (Reader.readBool())
7688 TL.setConceptReference(Reader.readConceptReference());
7689 if (Reader.readBool())
7690 TL.setRParenLoc(readSourceLocation());
7691}
7692
7693void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7694 DeducedTemplateSpecializationTypeLoc TL) {
7695 TL.setElaboratedKeywordLoc(readSourceLocation());
7696 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7697 TL.setTemplateNameLoc(readSourceLocation());
7698}
7699
7700void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7701 TL.setElaboratedKeywordLoc(readSourceLocation());
7702 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7703 TL.setNameLoc(readSourceLocation());
7704}
7705
7706void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7707 VisitTagTypeLoc(TL);
7708}
7709
7710void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7711 VisitTagTypeLoc(TL);
7712}
7713
7714void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7715
7716void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7717 TL.setAttr(ReadAttr());
7718}
7719
7720void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7721 // Nothing to do
7722}
7723
7724void TypeLocReader::VisitLateParsedAttrTypeLoc(LateParsedAttrTypeLoc TL) {
7725 llvm_unreachable(
7726 "should be replaced with a concrete type before serialization");
7727}
7728
7729void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7730 // Nothing to do.
7731}
7732
7733void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7734 TL.setAttrLoc(readSourceLocation());
7735}
7736
7737void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7738 HLSLAttributedResourceTypeLoc TL) {
7739 // Nothing to do.
7740}
7741
7742void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7743 // Nothing to do.
7744}
7745
7746void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7747 TL.setNameLoc(readSourceLocation());
7748}
7749
7750void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7751 SubstTemplateTypeParmTypeLoc TL) {
7752 TL.setNameLoc(readSourceLocation());
7753}
7754
7755void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7756 SubstTemplateTypeParmPackTypeLoc TL) {
7757 TL.setNameLoc(readSourceLocation());
7758}
7759
7760void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7761 SubstBuiltinTemplatePackTypeLoc TL) {
7762 TL.setNameLoc(readSourceLocation());
7763}
7764
7765void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7766 TemplateSpecializationTypeLoc TL) {
7767 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7768 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7769 SourceLocation TemplateKeywordLoc = readSourceLocation();
7770 SourceLocation NameLoc = readSourceLocation();
7771 SourceLocation LAngleLoc = readSourceLocation();
7772 SourceLocation RAngleLoc = readSourceLocation();
7773 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7774 LAngleLoc, RAngleLoc);
7775 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7776 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7777 Args[I] = Reader.readTemplateArgumentLocInfo(
7778 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7779}
7780
7781void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7782 TL.setLParenLoc(readSourceLocation());
7783 TL.setRParenLoc(readSourceLocation());
7784}
7785
7786void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7787 TL.setElaboratedKeywordLoc(readSourceLocation());
7788 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7789 TL.setNameLoc(readSourceLocation());
7790}
7791
7792void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7793 TL.setEllipsisLoc(readSourceLocation());
7794}
7795
7796void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7797 TL.setNameLoc(readSourceLocation());
7798 TL.setNameEndLoc(readSourceLocation());
7799}
7800
7801void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7802 if (TL.getNumProtocols()) {
7803 TL.setProtocolLAngleLoc(readSourceLocation());
7804 TL.setProtocolRAngleLoc(readSourceLocation());
7805 }
7806 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7807 TL.setProtocolLoc(i, Loc: readSourceLocation());
7808}
7809
7810void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7811 TL.setHasBaseTypeAsWritten(Reader.readBool());
7812 TL.setTypeArgsLAngleLoc(readSourceLocation());
7813 TL.setTypeArgsRAngleLoc(readSourceLocation());
7814 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7815 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7816 TL.setProtocolLAngleLoc(readSourceLocation());
7817 TL.setProtocolRAngleLoc(readSourceLocation());
7818 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7819 TL.setProtocolLoc(i, Loc: readSourceLocation());
7820}
7821
7822void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7823 TL.setStarLoc(readSourceLocation());
7824}
7825
7826void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7827 TL.setKWLoc(readSourceLocation());
7828 TL.setLParenLoc(readSourceLocation());
7829 TL.setRParenLoc(readSourceLocation());
7830}
7831
7832void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7833 TL.setKWLoc(readSourceLocation());
7834}
7835
7836void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7837 TL.setNameLoc(readSourceLocation());
7838}
7839
7840void TypeLocReader::VisitDependentBitIntTypeLoc(
7841 clang::DependentBitIntTypeLoc TL) {
7842 TL.setNameLoc(readSourceLocation());
7843}
7844
7845void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7846 // Nothing to do.
7847}
7848
7849void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7850 TypeLocReader TLR(*this);
7851 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7852 TLR.Visit(TyLoc: TL);
7853}
7854
7855TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7856 QualType InfoTy = readType();
7857 if (InfoTy.isNull())
7858 return nullptr;
7859
7860 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7861 readTypeLoc(TL: TInfo->getTypeLoc());
7862 return TInfo;
7863}
7864
7865static unsigned getIndexForTypeID(serialization::TypeID ID) {
7866 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7867}
7868
7869static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7870 return ID >> 32;
7871}
7872
7873static bool isPredefinedType(serialization::TypeID ID) {
7874 // We don't need to erase the higher bits since if these bits are not 0,
7875 // it must be larger than NUM_PREDEF_TYPE_IDS.
7876 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7877}
7878
7879std::pair<ModuleFile *, unsigned>
7880ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7881 assert(!isPredefinedType(ID) &&
7882 "Predefined type shouldn't be in TypesLoaded");
7883 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7884 assert(ModuleFileIndex && "Untranslated Local Decl?");
7885
7886 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7887 assert(OwningModuleFile &&
7888 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7889
7890 return {OwningModuleFile,
7891 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7892}
7893
7894QualType ASTReader::GetType(TypeID ID) {
7895 assert(ContextObj && "reading type with no AST context");
7896 ASTContext &Context = *ContextObj;
7897
7898 unsigned FastQuals = ID & Qualifiers::FastMask;
7899
7900 if (isPredefinedType(ID)) {
7901 QualType T;
7902 unsigned Index = getIndexForTypeID(ID);
7903 switch ((PredefinedTypeIDs)Index) {
7904 case PREDEF_TYPE_LAST_ID:
7905 // We should never use this one.
7906 llvm_unreachable("Invalid predefined type");
7907 break;
7908 case PREDEF_TYPE_NULL_ID:
7909 return QualType();
7910 case PREDEF_TYPE_VOID_ID:
7911 T = Context.VoidTy;
7912 break;
7913 case PREDEF_TYPE_BOOL_ID:
7914 T = Context.BoolTy;
7915 break;
7916 case PREDEF_TYPE_CHAR_U_ID:
7917 case PREDEF_TYPE_CHAR_S_ID:
7918 // FIXME: Check that the signedness of CharTy is correct!
7919 T = Context.CharTy;
7920 break;
7921 case PREDEF_TYPE_UCHAR_ID:
7922 T = Context.UnsignedCharTy;
7923 break;
7924 case PREDEF_TYPE_USHORT_ID:
7925 T = Context.UnsignedShortTy;
7926 break;
7927 case PREDEF_TYPE_UINT_ID:
7928 T = Context.UnsignedIntTy;
7929 break;
7930 case PREDEF_TYPE_ULONG_ID:
7931 T = Context.UnsignedLongTy;
7932 break;
7933 case PREDEF_TYPE_ULONGLONG_ID:
7934 T = Context.UnsignedLongLongTy;
7935 break;
7936 case PREDEF_TYPE_UINT128_ID:
7937 T = Context.UnsignedInt128Ty;
7938 break;
7939 case PREDEF_TYPE_SCHAR_ID:
7940 T = Context.SignedCharTy;
7941 break;
7942 case PREDEF_TYPE_WCHAR_ID:
7943 T = Context.WCharTy;
7944 break;
7945 case PREDEF_TYPE_SHORT_ID:
7946 T = Context.ShortTy;
7947 break;
7948 case PREDEF_TYPE_INT_ID:
7949 T = Context.IntTy;
7950 break;
7951 case PREDEF_TYPE_LONG_ID:
7952 T = Context.LongTy;
7953 break;
7954 case PREDEF_TYPE_LONGLONG_ID:
7955 T = Context.LongLongTy;
7956 break;
7957 case PREDEF_TYPE_INT128_ID:
7958 T = Context.Int128Ty;
7959 break;
7960 case PREDEF_TYPE_BFLOAT16_ID:
7961 T = Context.BFloat16Ty;
7962 break;
7963 case PREDEF_TYPE_HALF_ID:
7964 T = Context.HalfTy;
7965 break;
7966 case PREDEF_TYPE_FLOAT_ID:
7967 T = Context.FloatTy;
7968 break;
7969 case PREDEF_TYPE_DOUBLE_ID:
7970 T = Context.DoubleTy;
7971 break;
7972 case PREDEF_TYPE_LONGDOUBLE_ID:
7973 T = Context.LongDoubleTy;
7974 break;
7975 case PREDEF_TYPE_SHORT_ACCUM_ID:
7976 T = Context.ShortAccumTy;
7977 break;
7978 case PREDEF_TYPE_ACCUM_ID:
7979 T = Context.AccumTy;
7980 break;
7981 case PREDEF_TYPE_LONG_ACCUM_ID:
7982 T = Context.LongAccumTy;
7983 break;
7984 case PREDEF_TYPE_USHORT_ACCUM_ID:
7985 T = Context.UnsignedShortAccumTy;
7986 break;
7987 case PREDEF_TYPE_UACCUM_ID:
7988 T = Context.UnsignedAccumTy;
7989 break;
7990 case PREDEF_TYPE_ULONG_ACCUM_ID:
7991 T = Context.UnsignedLongAccumTy;
7992 break;
7993 case PREDEF_TYPE_SHORT_FRACT_ID:
7994 T = Context.ShortFractTy;
7995 break;
7996 case PREDEF_TYPE_FRACT_ID:
7997 T = Context.FractTy;
7998 break;
7999 case PREDEF_TYPE_LONG_FRACT_ID:
8000 T = Context.LongFractTy;
8001 break;
8002 case PREDEF_TYPE_USHORT_FRACT_ID:
8003 T = Context.UnsignedShortFractTy;
8004 break;
8005 case PREDEF_TYPE_UFRACT_ID:
8006 T = Context.UnsignedFractTy;
8007 break;
8008 case PREDEF_TYPE_ULONG_FRACT_ID:
8009 T = Context.UnsignedLongFractTy;
8010 break;
8011 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
8012 T = Context.SatShortAccumTy;
8013 break;
8014 case PREDEF_TYPE_SAT_ACCUM_ID:
8015 T = Context.SatAccumTy;
8016 break;
8017 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
8018 T = Context.SatLongAccumTy;
8019 break;
8020 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
8021 T = Context.SatUnsignedShortAccumTy;
8022 break;
8023 case PREDEF_TYPE_SAT_UACCUM_ID:
8024 T = Context.SatUnsignedAccumTy;
8025 break;
8026 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8027 T = Context.SatUnsignedLongAccumTy;
8028 break;
8029 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8030 T = Context.SatShortFractTy;
8031 break;
8032 case PREDEF_TYPE_SAT_FRACT_ID:
8033 T = Context.SatFractTy;
8034 break;
8035 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8036 T = Context.SatLongFractTy;
8037 break;
8038 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8039 T = Context.SatUnsignedShortFractTy;
8040 break;
8041 case PREDEF_TYPE_SAT_UFRACT_ID:
8042 T = Context.SatUnsignedFractTy;
8043 break;
8044 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8045 T = Context.SatUnsignedLongFractTy;
8046 break;
8047 case PREDEF_TYPE_FLOAT16_ID:
8048 T = Context.Float16Ty;
8049 break;
8050 case PREDEF_TYPE_FLOAT128_ID:
8051 T = Context.Float128Ty;
8052 break;
8053 case PREDEF_TYPE_IBM128_ID:
8054 T = Context.Ibm128Ty;
8055 break;
8056 case PREDEF_TYPE_OVERLOAD_ID:
8057 T = Context.OverloadTy;
8058 break;
8059 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8060 T = Context.UnresolvedTemplateTy;
8061 break;
8062 case PREDEF_TYPE_BOUND_MEMBER:
8063 T = Context.BoundMemberTy;
8064 break;
8065 case PREDEF_TYPE_PSEUDO_OBJECT:
8066 T = Context.PseudoObjectTy;
8067 break;
8068 case PREDEF_TYPE_DEPENDENT_ID:
8069 T = Context.DependentTy;
8070 break;
8071 case PREDEF_TYPE_UNKNOWN_ANY:
8072 T = Context.UnknownAnyTy;
8073 break;
8074 case PREDEF_TYPE_NULLPTR_ID:
8075 T = Context.NullPtrTy;
8076 break;
8077 case PREDEF_TYPE_CHAR8_ID:
8078 T = Context.Char8Ty;
8079 break;
8080 case PREDEF_TYPE_CHAR16_ID:
8081 T = Context.Char16Ty;
8082 break;
8083 case PREDEF_TYPE_CHAR32_ID:
8084 T = Context.Char32Ty;
8085 break;
8086 case PREDEF_TYPE_OBJC_ID:
8087 T = Context.ObjCBuiltinIdTy;
8088 break;
8089 case PREDEF_TYPE_OBJC_CLASS:
8090 T = Context.ObjCBuiltinClassTy;
8091 break;
8092 case PREDEF_TYPE_OBJC_SEL:
8093 T = Context.ObjCBuiltinSelTy;
8094 break;
8095#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8096 case PREDEF_TYPE_##Id##_ID: \
8097 T = Context.SingletonId; \
8098 break;
8099#include "clang/Basic/OpenCLImageTypes.def"
8100#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8101 case PREDEF_TYPE_##Id##_ID: \
8102 T = Context.Id##Ty; \
8103 break;
8104#include "clang/Basic/OpenCLExtensionTypes.def"
8105 case PREDEF_TYPE_SAMPLER_ID:
8106 T = Context.OCLSamplerTy;
8107 break;
8108 case PREDEF_TYPE_EVENT_ID:
8109 T = Context.OCLEventTy;
8110 break;
8111 case PREDEF_TYPE_CLK_EVENT_ID:
8112 T = Context.OCLClkEventTy;
8113 break;
8114 case PREDEF_TYPE_QUEUE_ID:
8115 T = Context.OCLQueueTy;
8116 break;
8117 case PREDEF_TYPE_RESERVE_ID_ID:
8118 T = Context.OCLReserveIDTy;
8119 break;
8120 case PREDEF_TYPE_AUTO_DEDUCT:
8121 T = Context.getAutoDeductType();
8122 break;
8123 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8124 T = Context.getAutoRRefDeductType();
8125 break;
8126 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8127 T = Context.ARCUnbridgedCastTy;
8128 break;
8129 case PREDEF_TYPE_BUILTIN_FN:
8130 T = Context.BuiltinFnTy;
8131 break;
8132 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8133 T = Context.IncompleteMatrixIdxTy;
8134 break;
8135 case PREDEF_TYPE_ARRAY_SECTION:
8136 T = Context.ArraySectionTy;
8137 break;
8138 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8139 T = Context.OMPArrayShapingTy;
8140 break;
8141 case PREDEF_TYPE_OMP_ITERATOR:
8142 T = Context.OMPIteratorTy;
8143 break;
8144#define SVE_TYPE(Name, Id, SingletonId) \
8145 case PREDEF_TYPE_##Id##_ID: \
8146 T = Context.SingletonId; \
8147 break;
8148#include "clang/Basic/AArch64ACLETypes.def"
8149#define PPC_VECTOR_TYPE(Name, Id, Size) \
8150 case PREDEF_TYPE_##Id##_ID: \
8151 T = Context.Id##Ty; \
8152 break;
8153#include "clang/Basic/PPCTypes.def"
8154#define RVV_TYPE(Name, Id, SingletonId) \
8155 case PREDEF_TYPE_##Id##_ID: \
8156 T = Context.SingletonId; \
8157 break;
8158#include "clang/Basic/RISCVVTypes.def"
8159#define WASM_TYPE(Name, Id, SingletonId) \
8160 case PREDEF_TYPE_##Id##_ID: \
8161 T = Context.SingletonId; \
8162 break;
8163#include "clang/Basic/WebAssemblyReferenceTypes.def"
8164#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8165 case PREDEF_TYPE_##Id##_ID: \
8166 T = Context.SingletonId; \
8167 break;
8168#include "clang/Basic/AMDGPUTypes.def"
8169#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8170 case PREDEF_TYPE_##Id##_ID: \
8171 T = Context.SingletonId; \
8172 break;
8173#include "clang/Basic/HLSLIntangibleTypes.def"
8174#define SPIRV_TYPE(Name, Id, SingletonId) \
8175 case PREDEF_TYPE_##Id##_ID: \
8176 T = Context.SingletonId; \
8177 break;
8178#include "clang/Basic/SPIRVTypes.def"
8179 }
8180
8181 assert(!T.isNull() && "Unknown predefined type");
8182 return T.withFastQualifiers(TQs: FastQuals);
8183 }
8184
8185 unsigned Index = translateTypeIDToIndex(ID).second;
8186
8187 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8188 if (TypesLoaded[Index].isNull()) {
8189 TypesLoaded[Index] = readTypeRecord(ID);
8190 if (TypesLoaded[Index].isNull())
8191 return QualType();
8192
8193 TypesLoaded[Index]->setFromAST();
8194 if (DeserializationListener)
8195 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8196 T: TypesLoaded[Index]);
8197 }
8198
8199 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8200}
8201
8202QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8203 return GetType(ID: getGlobalTypeID(F, LocalID));
8204}
8205
8206serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8207 LocalTypeID LocalID) const {
8208 if (isPredefinedType(ID: LocalID))
8209 return LocalID;
8210
8211 if (!F.ModuleOffsetMap.empty())
8212 ReadModuleOffsetMap(F);
8213
8214 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8215 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8216
8217 if (ModuleFileIndex == 0)
8218 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8219
8220 ModuleFile &MF =
8221 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8222 ModuleFileIndex = MF.Index + 1;
8223 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8224}
8225
8226TemplateArgumentLocInfo
8227ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8228 switch (Kind) {
8229 case TemplateArgument::Expression:
8230 return readExpr();
8231 case TemplateArgument::Type:
8232 return readTypeSourceInfo();
8233 case TemplateArgument::Template:
8234 case TemplateArgument::TemplateExpansion: {
8235 SourceLocation TemplateKWLoc = readSourceLocation();
8236 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8237 SourceLocation TemplateNameLoc = readSourceLocation();
8238 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8239 ? readSourceLocation()
8240 : SourceLocation();
8241 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8242 TemplateNameLoc, EllipsisLoc);
8243 }
8244 case TemplateArgument::Null:
8245 case TemplateArgument::Integral:
8246 case TemplateArgument::Declaration:
8247 case TemplateArgument::NullPtr:
8248 case TemplateArgument::StructuralValue:
8249 case TemplateArgument::Pack:
8250 // FIXME: Is this right?
8251 return TemplateArgumentLocInfo();
8252 }
8253 llvm_unreachable("unexpected template argument loc");
8254}
8255
8256TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8257 TemplateArgument Arg = readTemplateArgument();
8258
8259 if (Arg.getKind() == TemplateArgument::Expression) {
8260 if (readBool()) // bool InfoHasSameExpr.
8261 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8262 }
8263 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8264}
8265
8266void ASTRecordReader::readTemplateArgumentListInfo(
8267 TemplateArgumentListInfo &Result) {
8268 Result.setLAngleLoc(readSourceLocation());
8269 Result.setRAngleLoc(readSourceLocation());
8270 unsigned NumArgsAsWritten = readInt();
8271 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8272 Result.addArgument(Loc: readTemplateArgumentLoc());
8273}
8274
8275const ASTTemplateArgumentListInfo *
8276ASTRecordReader::readASTTemplateArgumentListInfo() {
8277 TemplateArgumentListInfo Result;
8278 readTemplateArgumentListInfo(Result);
8279 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8280}
8281
8282Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8283
8284void ASTReader::CompleteRedeclChain(const Decl *D) {
8285 if (NumCurrentElementsDeserializing) {
8286 // We arrange to not care about the complete redeclaration chain while we're
8287 // deserializing. Just remember that the AST has marked this one as complete
8288 // but that it's not actually complete yet, so we know we still need to
8289 // complete it later.
8290 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8291 return;
8292 }
8293
8294 if (!D->getDeclContext()) {
8295 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8296 return;
8297 }
8298
8299 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8300
8301 // If this is a named declaration, complete it by looking it up
8302 // within its context.
8303 //
8304 // FIXME: Merging a function definition should merge
8305 // all mergeable entities within it.
8306 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8307 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8308 if (!getContext().getLangOpts().CPlusPlus &&
8309 isa<TranslationUnitDecl>(Val: DC)) {
8310 // Outside of C++, we don't have a lookup table for the TU, so update
8311 // the identifier instead. (For C++ modules, we don't store decls
8312 // in the serialized identifier table, so we do the lookup in the TU.)
8313 auto *II = Name.getAsIdentifierInfo();
8314 assert(II && "non-identifier name in C?");
8315 if (II->isOutOfDate())
8316 updateOutOfDateIdentifier(II: *II);
8317 } else
8318 DC->lookup(Name);
8319 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8320 // Find all declarations of this kind from the relevant context.
8321 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8322 auto *DC = cast<DeclContext>(Val: DCDecl);
8323 SmallVector<Decl*, 8> Decls;
8324 FindExternalLexicalDecls(
8325 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8326 }
8327 }
8328 }
8329
8330 RedeclarableTemplateDecl *Template = nullptr;
8331 ArrayRef<TemplateArgument> Args;
8332 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8333 Template = CTSD->getSpecializedTemplate();
8334 Args = CTSD->getTemplateArgs().asArray();
8335 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8336 Template = VTSD->getSpecializedTemplate();
8337 Args = VTSD->getTemplateArgs().asArray();
8338 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8339 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8340 Template = Tmplt;
8341 Args = FD->getTemplateSpecializationArgs()->asArray();
8342 }
8343 }
8344
8345 if (Template)
8346 Template->loadLazySpecializationsImpl(Args);
8347}
8348
8349CXXCtorInitializer **
8350ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8351 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8352 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8353 SavedStreamPosition SavedPosition(Cursor);
8354 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8355 Error(Err: std::move(Err));
8356 return nullptr;
8357 }
8358 ReadingKindTracker ReadingKind(Read_Decl, *this);
8359 Deserializing D(this);
8360
8361 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8362 if (!MaybeCode) {
8363 Error(Err: MaybeCode.takeError());
8364 return nullptr;
8365 }
8366 unsigned Code = MaybeCode.get();
8367
8368 ASTRecordReader Record(*this, *Loc.F);
8369 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8370 if (!MaybeRecCode) {
8371 Error(Err: MaybeRecCode.takeError());
8372 return nullptr;
8373 }
8374 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8375 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8376 return nullptr;
8377 }
8378
8379 return Record.readCXXCtorInitializers();
8380}
8381
8382CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8383 assert(ContextObj && "reading base specifiers with no AST context");
8384 ASTContext &Context = *ContextObj;
8385
8386 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8387 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8388 SavedStreamPosition SavedPosition(Cursor);
8389 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8390 Error(Err: std::move(Err));
8391 return nullptr;
8392 }
8393 ReadingKindTracker ReadingKind(Read_Decl, *this);
8394 Deserializing D(this);
8395
8396 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8397 if (!MaybeCode) {
8398 Error(Err: MaybeCode.takeError());
8399 return nullptr;
8400 }
8401 unsigned Code = MaybeCode.get();
8402
8403 ASTRecordReader Record(*this, *Loc.F);
8404 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8405 if (!MaybeRecCode) {
8406 Error(Err: MaybeCode.takeError());
8407 return nullptr;
8408 }
8409 unsigned RecCode = MaybeRecCode.get();
8410
8411 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8412 Error(Msg: "malformed AST file: missing C++ base specifiers");
8413 return nullptr;
8414 }
8415
8416 unsigned NumBases = Record.readInt();
8417 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8418 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8419 for (unsigned I = 0; I != NumBases; ++I)
8420 Bases[I] = Record.readCXXBaseSpecifier();
8421 return Bases;
8422}
8423
8424GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8425 LocalDeclID LocalID) const {
8426 if (LocalID < NUM_PREDEF_DECL_IDS)
8427 return GlobalDeclID(LocalID.getRawValue());
8428
8429 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8430 DeclID ID = LocalID.getLocalDeclIndex();
8431
8432 if (!F.ModuleOffsetMap.empty())
8433 ReadModuleOffsetMap(F);
8434
8435 ModuleFile *OwningModuleFile =
8436 OwningModuleFileIndex == 0
8437 ? &F
8438 : F.TransitiveImports[OwningModuleFileIndex - 1];
8439
8440 if (OwningModuleFileIndex == 0)
8441 ID -= NUM_PREDEF_DECL_IDS;
8442
8443 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8444 return GlobalDeclID(NewModuleFileIndex, ID);
8445}
8446
8447bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8448 // Predefined decls aren't from any module.
8449 if (ID < NUM_PREDEF_DECL_IDS)
8450 return false;
8451
8452 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8453 return M.Index == ModuleFileIndex - 1;
8454}
8455
8456ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8457 // Predefined decls aren't from any module.
8458 if (ID < NUM_PREDEF_DECL_IDS)
8459 return nullptr;
8460
8461 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8462 assert(ModuleFileIndex && "Untranslated Local Decl?");
8463
8464 return &getModuleManager()[ModuleFileIndex - 1];
8465}
8466
8467ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8468 if (!D->isFromASTFile())
8469 return nullptr;
8470
8471 return getOwningModuleFile(ID: D->getGlobalID());
8472}
8473
8474SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8475 if (ID < NUM_PREDEF_DECL_IDS)
8476 return SourceLocation();
8477
8478 if (Decl *D = GetExistingDecl(ID))
8479 return D->getLocation();
8480
8481 SourceLocation Loc;
8482 DeclCursorForID(ID, Location&: Loc);
8483 return Loc;
8484}
8485
8486Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8487 assert(ContextObj && "reading predefined decl without AST context");
8488 ASTContext &Context = *ContextObj;
8489 Decl *NewLoaded = nullptr;
8490 switch (ID) {
8491 case PREDEF_DECL_NULL_ID:
8492 return nullptr;
8493
8494 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8495 return Context.getTranslationUnitDecl();
8496
8497 case PREDEF_DECL_OBJC_ID_ID:
8498 if (Context.ObjCIdDecl)
8499 return Context.ObjCIdDecl;
8500 NewLoaded = Context.getObjCIdDecl();
8501 break;
8502
8503 case PREDEF_DECL_OBJC_SEL_ID:
8504 if (Context.ObjCSelDecl)
8505 return Context.ObjCSelDecl;
8506 NewLoaded = Context.getObjCSelDecl();
8507 break;
8508
8509 case PREDEF_DECL_OBJC_CLASS_ID:
8510 if (Context.ObjCClassDecl)
8511 return Context.ObjCClassDecl;
8512 NewLoaded = Context.getObjCClassDecl();
8513 break;
8514
8515 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8516 if (Context.ObjCProtocolClassDecl)
8517 return Context.ObjCProtocolClassDecl;
8518 NewLoaded = Context.getObjCProtocolDecl();
8519 break;
8520
8521 case PREDEF_DECL_INT_128_ID:
8522 if (Context.Int128Decl)
8523 return Context.Int128Decl;
8524 NewLoaded = Context.getInt128Decl();
8525 break;
8526
8527 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8528 if (Context.UInt128Decl)
8529 return Context.UInt128Decl;
8530 NewLoaded = Context.getUInt128Decl();
8531 break;
8532
8533 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8534 if (Context.ObjCInstanceTypeDecl)
8535 return Context.ObjCInstanceTypeDecl;
8536 NewLoaded = Context.getObjCInstanceTypeDecl();
8537 break;
8538
8539 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8540 if (Context.BuiltinVaListDecl)
8541 return Context.BuiltinVaListDecl;
8542 NewLoaded = Context.getBuiltinVaListDecl();
8543 break;
8544
8545 case PREDEF_DECL_VA_LIST_TAG:
8546 if (Context.VaListTagDecl)
8547 return Context.VaListTagDecl;
8548 NewLoaded = Context.getVaListTagDecl();
8549 break;
8550
8551 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8552 if (Context.BuiltinMSVaListDecl)
8553 return Context.BuiltinMSVaListDecl;
8554 NewLoaded = Context.getBuiltinMSVaListDecl();
8555 break;
8556
8557 case PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID:
8558 if (Context.BuiltinZOSVaListDecl)
8559 return Context.BuiltinZOSVaListDecl;
8560 NewLoaded = Context.getBuiltinZOSVaListDecl();
8561 break;
8562
8563 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8564 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8565 return Context.getMSGuidTagDecl();
8566
8567 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8568 if (Context.ExternCContext)
8569 return Context.ExternCContext;
8570 NewLoaded = Context.getExternCContextDecl();
8571 break;
8572
8573 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8574 if (Context.CFConstantStringTypeDecl)
8575 return Context.CFConstantStringTypeDecl;
8576 NewLoaded = Context.getCFConstantStringDecl();
8577 break;
8578
8579 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8580 if (Context.CFConstantStringTagDecl)
8581 return Context.CFConstantStringTagDecl;
8582 NewLoaded = Context.getCFConstantStringTagDecl();
8583 break;
8584
8585 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8586 return Context.getMSTypeInfoTagDecl();
8587
8588#define BuiltinTemplate(BTName) \
8589 case PREDEF_DECL##BTName##_ID: \
8590 if (Context.Decl##BTName) \
8591 return Context.Decl##BTName; \
8592 NewLoaded = Context.get##BTName##Decl(); \
8593 break;
8594#include "clang/Basic/BuiltinTemplates.inc"
8595
8596 case NUM_PREDEF_DECL_IDS:
8597 llvm_unreachable("Invalid decl ID");
8598 break;
8599 }
8600
8601 assert(NewLoaded && "Failed to load predefined decl?");
8602
8603 if (DeserializationListener)
8604 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8605
8606 return NewLoaded;
8607}
8608
8609unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8610 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8611 if (!OwningModuleFile) {
8612 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8613 return GlobalID.getRawValue();
8614 }
8615
8616 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8617}
8618
8619Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8620 assert(ContextObj && "reading decl with no AST context");
8621
8622 if (ID < NUM_PREDEF_DECL_IDS) {
8623 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8624 if (D) {
8625 // Track that we have merged the declaration with ID \p ID into the
8626 // pre-existing predefined declaration \p D.
8627 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8628 if (Merged.empty())
8629 Merged.push_back(Elt: ID);
8630 }
8631 return D;
8632 }
8633
8634 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8635
8636 if (Index >= DeclsLoaded.size()) {
8637 assert(0 && "declaration ID out-of-range for AST file");
8638 Error(Msg: "declaration ID out-of-range for AST file");
8639 return nullptr;
8640 }
8641
8642 return DeclsLoaded[Index];
8643}
8644
8645Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8646 if (ID < NUM_PREDEF_DECL_IDS)
8647 return GetExistingDecl(ID);
8648
8649 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8650
8651 if (Index >= DeclsLoaded.size()) {
8652 assert(0 && "declaration ID out-of-range for AST file");
8653 Error(Msg: "declaration ID out-of-range for AST file");
8654 return nullptr;
8655 }
8656
8657 if (!DeclsLoaded[Index]) {
8658 ReadDeclRecord(ID);
8659 if (DeserializationListener)
8660 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8661 }
8662
8663 return DeclsLoaded[Index];
8664}
8665
8666LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8667 GlobalDeclID GlobalID) {
8668 if (GlobalID < NUM_PREDEF_DECL_IDS)
8669 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8670
8671 if (!M.ModuleOffsetMap.empty())
8672 ReadModuleOffsetMap(F&: M);
8673
8674 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8675 DeclID ID = GlobalID.getLocalDeclIndex();
8676
8677 if (Owner == &M) {
8678 ID += NUM_PREDEF_DECL_IDS;
8679 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8680 }
8681
8682 uint64_t OrignalModuleFileIndex = 0;
8683 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8684 if (M.TransitiveImports[I] == Owner) {
8685 OrignalModuleFileIndex = I + 1;
8686 break;
8687 }
8688
8689 if (!OrignalModuleFileIndex)
8690 return LocalDeclID();
8691
8692 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8693}
8694
8695GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8696 unsigned &Idx) {
8697 if (Idx >= Record.size()) {
8698 Error(Msg: "Corrupted AST file");
8699 return GlobalDeclID(0);
8700 }
8701
8702 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8703}
8704
8705/// Resolve the offset of a statement into a statement.
8706///
8707/// This operation will read a new statement from the external
8708/// source each time it is called, and is meant to be used via a
8709/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8710Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8711 // Switch case IDs are per Decl.
8712 ClearSwitchCaseIDs();
8713
8714 // Offset here is a global offset across the entire chain.
8715 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8716 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8717 Error(Err: std::move(Err));
8718 return nullptr;
8719 }
8720 assert(NumCurrentElementsDeserializing == 0 &&
8721 "should not be called while already deserializing");
8722 Deserializing D(this);
8723 return ReadStmtFromStream(F&: *Loc.F);
8724}
8725
8726bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8727 const Decl *D) {
8728 assert(D);
8729
8730 auto It = SpecLookups.find(Val: D);
8731 if (It == SpecLookups.end())
8732 return false;
8733
8734 // Get Decl may violate the iterator from SpecializationsLookups so we store
8735 // the DeclIDs in ahead.
8736 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8737 It->second.Table.findAll();
8738
8739 // Since we've loaded all the specializations, we can erase it from
8740 // the lookup table.
8741 SpecLookups.erase(I: It);
8742
8743 bool NewSpecsFound = false;
8744 Deserializing LookupResults(this);
8745 for (auto &Info : Infos) {
8746 if (GetExistingDecl(ID: Info))
8747 continue;
8748 NewSpecsFound = true;
8749 GetDecl(ID: Info);
8750 }
8751
8752 return NewSpecsFound;
8753}
8754
8755bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8756 assert(D);
8757
8758 CompleteRedeclChain(D);
8759 bool NewSpecsFound =
8760 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8761 if (OnlyPartial)
8762 return NewSpecsFound;
8763
8764 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8765 return NewSpecsFound;
8766}
8767
8768bool ASTReader::LoadExternalSpecializationsImpl(
8769 SpecLookupTableTy &SpecLookups, const Decl *D,
8770 ArrayRef<TemplateArgument> TemplateArgs) {
8771 assert(D);
8772
8773 auto It = SpecLookups.find(Val: D);
8774 if (It == SpecLookups.end())
8775 return false;
8776
8777 Deserializing LookupResults(this);
8778 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8779
8780 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8781 It->second.Table.find(EKey: HashValue);
8782
8783 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8784 std::string Name;
8785 llvm::raw_string_ostream OS(Name);
8786 auto *ND = cast<NamedDecl>(Val: D);
8787 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8788 /*Qualified=*/true);
8789 return Name;
8790 });
8791
8792 bool NewSpecsFound = false;
8793 for (auto &Info : Infos) {
8794 if (GetExistingDecl(ID: Info))
8795 continue;
8796 NewSpecsFound = true;
8797 GetDecl(ID: Info);
8798 }
8799
8800 return NewSpecsFound;
8801}
8802
8803bool ASTReader::LoadExternalSpecializations(
8804 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8805 assert(D);
8806
8807 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8808 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8809 NewDeclsFound |=
8810 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8811
8812 return NewDeclsFound;
8813}
8814
8815void ASTReader::FindExternalLexicalDecls(
8816 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8817 SmallVectorImpl<Decl *> &Decls) {
8818 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8819
8820 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8821 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8822 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8823 auto K = (Decl::Kind)+LexicalDecls[I];
8824 if (!IsKindWeWant(K))
8825 continue;
8826
8827 auto ID = (DeclID) + LexicalDecls[I + 1];
8828
8829 // Don't add predefined declarations to the lexical context more
8830 // than once.
8831 if (ID < NUM_PREDEF_DECL_IDS) {
8832 if (PredefsVisited[ID])
8833 continue;
8834
8835 PredefsVisited[ID] = true;
8836 }
8837
8838 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8839 assert(D->getKind() == K && "wrong kind for lexical decl");
8840 if (!DC->isDeclInLexicalTraversal(D))
8841 Decls.push_back(Elt: D);
8842 }
8843 }
8844 };
8845
8846 if (isa<TranslationUnitDecl>(Val: DC)) {
8847 for (const auto &Lexical : TULexicalDecls)
8848 Visit(Lexical.first, Lexical.second);
8849 } else {
8850 auto I = LexicalDecls.find(Val: DC);
8851 if (I != LexicalDecls.end())
8852 Visit(I->second.first, I->second.second);
8853 }
8854
8855 ++NumLexicalDeclContextsRead;
8856}
8857
8858namespace {
8859
8860class UnalignedDeclIDComp {
8861 ASTReader &Reader;
8862 ModuleFile &Mod;
8863
8864public:
8865 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8866 : Reader(Reader), Mod(M) {}
8867
8868 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8869 SourceLocation LHS = getLocation(ID: L);
8870 SourceLocation RHS = getLocation(ID: R);
8871 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8872 }
8873
8874 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8875 SourceLocation RHS = getLocation(ID: R);
8876 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8877 }
8878
8879 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8880 SourceLocation LHS = getLocation(ID: L);
8881 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8882 }
8883
8884 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8885 return Reader.getSourceManager().getFileLoc(
8886 Loc: Reader.getSourceLocationForDeclID(
8887 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8888 }
8889};
8890
8891} // namespace
8892
8893void ASTReader::FindFileRegionDecls(FileID File,
8894 unsigned Offset, unsigned Length,
8895 SmallVectorImpl<Decl *> &Decls) {
8896 SourceManager &SM = getSourceManager();
8897
8898 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8899 if (I == FileDeclIDs.end())
8900 return;
8901
8902 FileDeclsInfo &DInfo = I->second;
8903 if (DInfo.Decls.empty())
8904 return;
8905
8906 SourceLocation
8907 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8908 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8909
8910 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8911 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8912 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8913 if (BeginIt != DInfo.Decls.begin())
8914 --BeginIt;
8915
8916 // If we are pointing at a top-level decl inside an objc container, we need
8917 // to backtrack until we find it otherwise we will fail to report that the
8918 // region overlaps with an objc container.
8919 while (BeginIt != DInfo.Decls.begin() &&
8920 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8921 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8922 ->isTopLevelDeclInObjCContainer())
8923 --BeginIt;
8924
8925 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8926 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8927 if (EndIt != DInfo.Decls.end())
8928 ++EndIt;
8929
8930 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8931 ++DIt)
8932 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8933 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8934}
8935
8936bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8937 DeclarationName Name,
8938 const DeclContext *OriginalDC) {
8939 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8940 "DeclContext has no visible decls in storage");
8941 if (!Name)
8942 return false;
8943
8944 // Load the list of declarations.
8945 DeclsSet DS;
8946
8947 auto Find = [&, this](auto &&Table, auto &&Key) {
8948 for (GlobalDeclID ID : Table.find(Key)) {
8949 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8950 if (ND->getDeclName() != Name)
8951 continue;
8952 // Special case for namespaces: There can be a lot of redeclarations of
8953 // some namespaces, and we import a "key declaration" per imported module.
8954 // Since all declarations of a namespace are essentially interchangeable,
8955 // we can optimize namespace look-up by only storing the key declaration
8956 // of the current TU, rather than storing N key declarations where N is
8957 // the # of imported modules that declare that namespace.
8958 // TODO: Try to generalize this optimization to other redeclarable decls.
8959 if (isa<NamespaceDecl>(Val: ND))
8960 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8961 DS.insert(ND);
8962 }
8963 };
8964
8965 Deserializing LookupResults(this);
8966
8967 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8968 // available.
8969 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8970 ++NumVisibleDeclContextsRead;
8971 Find(It->second.Table, Name);
8972 }
8973
8974 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8975 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8976 ++NumModuleLocalVisibleDeclContexts;
8977 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8978 }
8979 };
8980 if (auto *NamedModule =
8981 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8982 : nullptr)
8983 FindModuleLocalLookup(NamedModule);
8984 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8985 // We're going to find a decl but the decl context of the lookup is
8986 // unspecified. In this case, the OriginalDC may be the decl context in other
8987 // module.
8988 if (ContextObj && ContextObj->getCurrentNamedModule())
8989 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8990
8991 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8992 ++NumTULocalVisibleDeclContexts;
8993 Find(It->second.Table, Name);
8994 }
8995
8996 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8997 return !DS.empty();
8998}
8999
9000void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
9001 if (!DC->hasExternalVisibleStorage())
9002 return;
9003
9004 DeclsMap Decls;
9005
9006 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
9007 auto It = LookupTables.find(DC);
9008 if (It == LookupTables.end())
9009 return;
9010
9011 NumRead++;
9012
9013 for (GlobalDeclID ID : It->second.Table.findAll()) {
9014 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
9015 // Special case for namespaces: There can be a lot of redeclarations of
9016 // some namespaces, and we import a "key declaration" per imported module.
9017 // Since all declarations of a namespace are essentially interchangeable,
9018 // we can optimize namespace look-up by only storing the key declaration
9019 // of the current TU, rather than storing N key declarations where N is
9020 // the # of imported modules that declare that namespace.
9021 // TODO: Try to generalize this optimization to other redeclarable decls.
9022 if (isa<NamespaceDecl>(Val: ND))
9023 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
9024 Decls[ND->getDeclName()].insert(ND);
9025 }
9026
9027 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
9028 };
9029
9030 findAll(Lookups, NumVisibleDeclContextsRead);
9031 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
9032 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
9033
9034 for (auto &[Name, DS] : Decls)
9035 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9036
9037 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9038}
9039
9040const serialization::reader::DeclContextLookupTable *
9041ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9042 auto I = Lookups.find(Val: Primary);
9043 return I == Lookups.end() ? nullptr : &I->second;
9044}
9045
9046const serialization::reader::ModuleLocalLookupTable *
9047ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9048 auto I = ModuleLocalLookups.find(Val: Primary);
9049 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9050}
9051
9052const serialization::reader::DeclContextLookupTable *
9053ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9054 auto I = TULocalLookups.find(Val: Primary);
9055 return I == TULocalLookups.end() ? nullptr : &I->second;
9056}
9057
9058serialization::reader::LazySpecializationInfoLookupTable *
9059ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9060 assert(D->isCanonicalDecl());
9061 auto &LookupTable =
9062 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9063 auto I = LookupTable.find(Val: D);
9064 return I == LookupTable.end() ? nullptr : &I->second;
9065}
9066
9067bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9068 assert(D->isCanonicalDecl());
9069 return PartialSpecializationsLookups.contains(Val: D) ||
9070 SpecializationsLookups.contains(Val: D);
9071}
9072
9073/// Under non-PCH compilation the consumer receives the objc methods
9074/// before receiving the implementation, and codegen depends on this.
9075/// We simulate this by deserializing and passing to consumer the methods of the
9076/// implementation before passing the deserialized implementation decl.
9077static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9078 ASTConsumer *Consumer) {
9079 assert(ImplD && Consumer);
9080
9081 for (auto *I : ImplD->methods())
9082 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9083
9084 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9085}
9086
9087void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9088 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9089 PassObjCImplDeclToConsumer(ImplD, Consumer);
9090 else
9091 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9092}
9093
9094void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9095 Consumer->HandleVTable(RD);
9096}
9097
9098void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9099 this->Consumer = Consumer;
9100
9101 if (Consumer)
9102 PassInterestingDeclsToConsumer();
9103
9104 if (DeserializationListener)
9105 DeserializationListener->ReaderInitialized(Reader: this);
9106}
9107
9108void ASTReader::PrintStats() {
9109 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9110
9111 unsigned NumTypesLoaded =
9112 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9113 unsigned NumDeclsLoaded =
9114 DeclsLoaded.size() -
9115 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9116 unsigned NumIdentifiersLoaded =
9117 IdentifiersLoaded.size() -
9118 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9119 unsigned NumMacrosLoaded =
9120 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9121 unsigned NumSelectorsLoaded =
9122 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9123
9124 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9125 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9126 NumSLocEntriesRead, TotalNumSLocEntries,
9127 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9128 if (!TypesLoaded.empty())
9129 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9130 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9131 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9132 if (!DeclsLoaded.empty())
9133 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9134 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9135 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9136 if (!IdentifiersLoaded.empty())
9137 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9138 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9139 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9140 if (!MacrosLoaded.empty())
9141 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9142 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9143 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9144 if (!SelectorsLoaded.empty())
9145 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9146 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9147 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9148 if (TotalNumStatements)
9149 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9150 NumStatementsRead, TotalNumStatements,
9151 ((float)NumStatementsRead/TotalNumStatements * 100));
9152 if (TotalNumMacros)
9153 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9154 NumMacrosRead, TotalNumMacros,
9155 ((float)NumMacrosRead/TotalNumMacros * 100));
9156 if (TotalLexicalDeclContexts)
9157 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9158 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9159 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9160 * 100));
9161 if (TotalVisibleDeclContexts)
9162 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9163 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9164 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9165 * 100));
9166 if (TotalModuleLocalVisibleDeclContexts)
9167 std::fprintf(
9168 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9169 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9170 ((float)NumModuleLocalVisibleDeclContexts /
9171 TotalModuleLocalVisibleDeclContexts * 100));
9172 if (TotalTULocalVisibleDeclContexts)
9173 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9174 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9175 ((float)NumTULocalVisibleDeclContexts /
9176 TotalTULocalVisibleDeclContexts * 100));
9177 if (TotalNumMethodPoolEntries)
9178 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9179 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9180 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9181 * 100));
9182 if (NumMethodPoolLookups)
9183 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9184 NumMethodPoolHits, NumMethodPoolLookups,
9185 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9186 if (NumMethodPoolTableLookups)
9187 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9188 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9189 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9190 * 100.0));
9191 if (NumIdentifierLookupHits)
9192 std::fprintf(stderr,
9193 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9194 NumIdentifierLookupHits, NumIdentifierLookups,
9195 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9196
9197 if (GlobalIndex) {
9198 std::fprintf(stderr, format: "\n");
9199 GlobalIndex->printStats();
9200 }
9201
9202 std::fprintf(stderr, format: "\n");
9203 dump();
9204 std::fprintf(stderr, format: "\n");
9205}
9206
9207template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9208LLVM_DUMP_METHOD static void
9209dumpModuleIDMap(StringRef Name,
9210 const ContinuousRangeMap<Key, ModuleFile *,
9211 InitialCapacity> &Map) {
9212 if (Map.begin() == Map.end())
9213 return;
9214
9215 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9216
9217 llvm::errs() << Name << ":\n";
9218 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9219 I != IEnd; ++I)
9220 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9221 << "\n";
9222}
9223
9224LLVM_DUMP_METHOD void ASTReader::dump() {
9225 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9226 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9227 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9228 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9229 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9230 dumpModuleIDMap(Name: "Global preprocessed entity map",
9231 Map: GlobalPreprocessedEntityMap);
9232
9233 llvm::errs() << "\n*** PCH/Modules Loaded:";
9234 for (ModuleFile &M : ModuleMgr)
9235 M.dump();
9236}
9237
9238/// Return the amount of memory used by memory buffers, breaking down
9239/// by heap-backed versus mmap'ed memory.
9240void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9241 for (ModuleFile &I : ModuleMgr) {
9242 if (llvm::MemoryBuffer *buf = I.Buffer) {
9243 size_t bytes = buf->getBufferSize();
9244 switch (buf->getBufferKind()) {
9245 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9246 sizes.malloc_bytes += bytes;
9247 break;
9248 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9249 sizes.mmap_bytes += bytes;
9250 break;
9251 }
9252 }
9253 }
9254}
9255
9256void ASTReader::InitializeSema(Sema &S) {
9257 SemaObj = &S;
9258 S.addExternalSource(E: this);
9259
9260 // Makes sure any declarations that were deserialized "too early"
9261 // still get added to the identifier's declaration chains.
9262 for (GlobalDeclID ID : PreloadedDeclIDs) {
9263 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9264 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9265 }
9266 PreloadedDeclIDs.clear();
9267
9268 // FIXME: What happens if these are changed by a module import?
9269 if (!FPPragmaOptions.empty()) {
9270 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9271 FPOptionsOverride NewOverrides =
9272 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9273 SemaObj->CurFPFeatures =
9274 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9275 }
9276
9277 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9278 Decl *D = GetDecl(ID);
9279 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9280 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9281 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9282 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9283 else
9284 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9285 }
9286 DeclsWithEffectsToVerify.clear();
9287
9288 SemaObj->OpenCLFeatures = OpenCLExtensions;
9289
9290 UpdateSema();
9291}
9292
9293void ASTReader::UpdateSema() {
9294 assert(SemaObj && "no Sema to update");
9295
9296 // Load the offsets of the declarations that Sema references.
9297 // They will be lazily deserialized when needed.
9298 if (!SemaDeclRefs.empty()) {
9299 assert(SemaDeclRefs.size() % 3 == 0);
9300 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9301 if (!SemaObj->StdNamespace)
9302 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9303 if (!SemaObj->StdBadAlloc)
9304 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9305 if (!SemaObj->StdAlignValT)
9306 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9307 }
9308 SemaDeclRefs.clear();
9309 }
9310
9311 // Update the state of pragmas. Use the same API as if we had encountered the
9312 // pragma in the source.
9313 if(OptimizeOffPragmaLocation.isValid())
9314 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9315 if (PragmaMSStructState != -1)
9316 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9317 if (PointersToMembersPragmaLocation.isValid()) {
9318 SemaObj->ActOnPragmaMSPointersToMembers(
9319 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9320 PragmaMSPointersToMembersState,
9321 PragmaLoc: PointersToMembersPragmaLocation);
9322 }
9323 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9324 if (!RISCVVecIntrinsicPragma.empty()) {
9325 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9326 "Wrong number of RISCVVecIntrinsicPragma");
9327 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9328 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9329 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9330 }
9331
9332 if (PragmaAlignPackCurrentValue) {
9333 // The bottom of the stack might have a default value. It must be adjusted
9334 // to the current value to ensure that the packing state is preserved after
9335 // popping entries that were included/imported from a PCH/module.
9336 bool DropFirst = false;
9337 if (!PragmaAlignPackStack.empty() &&
9338 PragmaAlignPackStack.front().Location.isInvalid()) {
9339 assert(PragmaAlignPackStack.front().Value ==
9340 SemaObj->AlignPackStack.DefaultValue &&
9341 "Expected a default alignment value");
9342 SemaObj->AlignPackStack.Stack.emplace_back(
9343 Args&: PragmaAlignPackStack.front().SlotLabel,
9344 Args&: SemaObj->AlignPackStack.CurrentValue,
9345 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9346 Args&: PragmaAlignPackStack.front().PushLocation);
9347 DropFirst = true;
9348 }
9349 for (const auto &Entry :
9350 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9351 SemaObj->AlignPackStack.Stack.emplace_back(
9352 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9353 }
9354 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9355 assert(*PragmaAlignPackCurrentValue ==
9356 SemaObj->AlignPackStack.DefaultValue &&
9357 "Expected a default align and pack value");
9358 // Keep the current values.
9359 } else {
9360 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9361 SemaObj->AlignPackStack.CurrentPragmaLocation =
9362 PragmaAlignPackCurrentLocation;
9363 }
9364 }
9365 if (FpPragmaCurrentValue) {
9366 // The bottom of the stack might have a default value. It must be adjusted
9367 // to the current value to ensure that fp-pragma state is preserved after
9368 // popping entries that were included/imported from a PCH/module.
9369 bool DropFirst = false;
9370 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9371 assert(FpPragmaStack.front().Value ==
9372 SemaObj->FpPragmaStack.DefaultValue &&
9373 "Expected a default pragma float_control value");
9374 SemaObj->FpPragmaStack.Stack.emplace_back(
9375 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9376 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9377 Args&: FpPragmaStack.front().PushLocation);
9378 DropFirst = true;
9379 }
9380 for (const auto &Entry :
9381 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9382 SemaObj->FpPragmaStack.Stack.emplace_back(
9383 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9384 if (FpPragmaCurrentLocation.isInvalid()) {
9385 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9386 "Expected a default pragma float_control value");
9387 // Keep the current values.
9388 } else {
9389 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9390 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9391 }
9392 }
9393
9394 // For non-modular AST files, restore visiblity of modules.
9395 for (auto &Import : PendingImportedModulesSema) {
9396 if (Import.ImportLoc.isInvalid())
9397 continue;
9398 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9399 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9400 }
9401 }
9402 PendingImportedModulesSema.clear();
9403}
9404
9405IdentifierInfo *ASTReader::get(StringRef Name) {
9406 // Note that we are loading an identifier.
9407 Deserializing AnIdentifier(this);
9408
9409 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9410 NumIdentifierLookups,
9411 NumIdentifierLookupHits);
9412
9413 // We don't need to do identifier table lookups in C++ modules (we preload
9414 // all interesting declarations, and don't need to use the scope for name
9415 // lookups). Perform the lookup in PCH files, though, since we don't build
9416 // a complete initial identifier table if we're carrying on from a PCH.
9417 if (PP.getLangOpts().CPlusPlus) {
9418 for (auto *F : ModuleMgr.pch_modules())
9419 if (Visitor(*F))
9420 break;
9421 } else {
9422 // If there is a global index, look there first to determine which modules
9423 // provably do not have any results for this identifier.
9424 GlobalModuleIndex::HitSet Hits;
9425 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9426 if (!loadGlobalIndex()) {
9427 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9428 HitsPtr = &Hits;
9429 }
9430 }
9431
9432 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9433 }
9434
9435 IdentifierInfo *II = Visitor.getIdentifierInfo();
9436 markIdentifierUpToDate(II);
9437 return II;
9438}
9439
9440namespace clang {
9441
9442 /// An identifier-lookup iterator that enumerates all of the
9443 /// identifiers stored within a set of AST files.
9444 class ASTIdentifierIterator : public IdentifierIterator {
9445 /// The AST reader whose identifiers are being enumerated.
9446 const ASTReader &Reader;
9447
9448 /// The current index into the chain of AST files stored in
9449 /// the AST reader.
9450 unsigned Index;
9451
9452 /// The current position within the identifier lookup table
9453 /// of the current AST file.
9454 ASTIdentifierLookupTable::key_iterator Current;
9455
9456 /// The end position within the identifier lookup table of
9457 /// the current AST file.
9458 ASTIdentifierLookupTable::key_iterator End;
9459
9460 /// Whether to skip any modules in the ASTReader.
9461 bool SkipModules;
9462
9463 public:
9464 explicit ASTIdentifierIterator(const ASTReader &Reader,
9465 bool SkipModules = false);
9466
9467 StringRef Next() override;
9468 };
9469
9470} // namespace clang
9471
9472ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9473 bool SkipModules)
9474 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9475}
9476
9477StringRef ASTIdentifierIterator::Next() {
9478 while (Current == End) {
9479 // If we have exhausted all of our AST files, we're done.
9480 if (Index == 0)
9481 return StringRef();
9482
9483 --Index;
9484 ModuleFile &F = Reader.ModuleMgr[Index];
9485 if (SkipModules && F.isModule())
9486 continue;
9487
9488 ASTIdentifierLookupTable *IdTable =
9489 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9490 Current = IdTable->key_begin();
9491 End = IdTable->key_end();
9492 }
9493
9494 // We have any identifiers remaining in the current AST file; return
9495 // the next one.
9496 StringRef Result = *Current;
9497 ++Current;
9498 return Result;
9499}
9500
9501namespace {
9502
9503/// A utility for appending two IdentifierIterators.
9504class ChainedIdentifierIterator : public IdentifierIterator {
9505 std::unique_ptr<IdentifierIterator> Current;
9506 std::unique_ptr<IdentifierIterator> Queued;
9507
9508public:
9509 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9510 std::unique_ptr<IdentifierIterator> Second)
9511 : Current(std::move(First)), Queued(std::move(Second)) {}
9512
9513 StringRef Next() override {
9514 if (!Current)
9515 return StringRef();
9516
9517 StringRef result = Current->Next();
9518 if (!result.empty())
9519 return result;
9520
9521 // Try the queued iterator, which may itself be empty.
9522 Current.reset();
9523 std::swap(x&: Current, y&: Queued);
9524 return Next();
9525 }
9526};
9527
9528} // namespace
9529
9530IdentifierIterator *ASTReader::getIdentifiers() {
9531 if (!loadGlobalIndex()) {
9532 std::unique_ptr<IdentifierIterator> ReaderIter(
9533 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9534 std::unique_ptr<IdentifierIterator> ModulesIter(
9535 GlobalIndex->createIdentifierIterator());
9536 return new ChainedIdentifierIterator(std::move(ReaderIter),
9537 std::move(ModulesIter));
9538 }
9539
9540 return new ASTIdentifierIterator(*this);
9541}
9542
9543namespace clang {
9544namespace serialization {
9545
9546 class ReadMethodPoolVisitor {
9547 ASTReader &Reader;
9548 Selector Sel;
9549 unsigned PriorGeneration;
9550 unsigned InstanceBits = 0;
9551 unsigned FactoryBits = 0;
9552 bool InstanceHasMoreThanOneDecl = false;
9553 bool FactoryHasMoreThanOneDecl = false;
9554 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9555 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9556
9557 public:
9558 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9559 unsigned PriorGeneration)
9560 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9561
9562 bool operator()(ModuleFile &M) {
9563 if (!M.SelectorLookupTable)
9564 return false;
9565
9566 // If we've already searched this module file, skip it now.
9567 if (M.Generation <= PriorGeneration)
9568 return true;
9569
9570 ++Reader.NumMethodPoolTableLookups;
9571 ASTSelectorLookupTable *PoolTable
9572 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9573 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9574 if (Pos == PoolTable->end())
9575 return false;
9576
9577 ++Reader.NumMethodPoolTableHits;
9578 ++Reader.NumSelectorsRead;
9579 // FIXME: Not quite happy with the statistics here. We probably should
9580 // disable this tracking when called via LoadSelector.
9581 // Also, should entries without methods count as misses?
9582 ++Reader.NumMethodPoolEntriesRead;
9583 ASTSelectorLookupTrait::data_type Data = *Pos;
9584 if (Reader.DeserializationListener)
9585 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9586
9587 // Append methods in the reverse order, so that later we can process them
9588 // in the order they appear in the source code by iterating through
9589 // the vector in the reverse order.
9590 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9591 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9592 InstanceBits = Data.InstanceBits;
9593 FactoryBits = Data.FactoryBits;
9594 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9595 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9596 return false;
9597 }
9598
9599 /// Retrieve the instance methods found by this visitor.
9600 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9601 return InstanceMethods;
9602 }
9603
9604 /// Retrieve the instance methods found by this visitor.
9605 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9606 return FactoryMethods;
9607 }
9608
9609 unsigned getInstanceBits() const { return InstanceBits; }
9610 unsigned getFactoryBits() const { return FactoryBits; }
9611
9612 bool instanceHasMoreThanOneDecl() const {
9613 return InstanceHasMoreThanOneDecl;
9614 }
9615
9616 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9617 };
9618
9619} // namespace serialization
9620} // namespace clang
9621
9622/// Add the given set of methods to the method list.
9623static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9624 ObjCMethodList &List) {
9625 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9626 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9627}
9628
9629void ASTReader::ReadMethodPool(Selector Sel) {
9630 // Get the selector generation and update it to the current generation.
9631 unsigned &Generation = SelectorGeneration[Sel];
9632 unsigned PriorGeneration = Generation;
9633 Generation = getGeneration();
9634 SelectorOutOfDate[Sel] = false;
9635
9636 // Search for methods defined with this selector.
9637 ++NumMethodPoolLookups;
9638 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9639 ModuleMgr.visit(Visitor);
9640
9641 if (Visitor.getInstanceMethods().empty() &&
9642 Visitor.getFactoryMethods().empty())
9643 return;
9644
9645 ++NumMethodPoolHits;
9646
9647 if (!getSema())
9648 return;
9649
9650 Sema &S = *getSema();
9651 auto &Methods = S.ObjC().MethodPool[Sel];
9652
9653 Methods.first.setBits(Visitor.getInstanceBits());
9654 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9655 Methods.second.setBits(Visitor.getFactoryBits());
9656 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9657
9658 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9659 // when building a module we keep every method individually and may need to
9660 // update hasMoreThanOneDecl as we add the methods.
9661 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9662 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9663}
9664
9665void ASTReader::updateOutOfDateSelector(Selector Sel) {
9666 if (SelectorOutOfDate[Sel])
9667 ReadMethodPool(Sel);
9668}
9669
9670void ASTReader::ReadKnownNamespaces(
9671 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9672 Namespaces.clear();
9673
9674 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9675 if (NamespaceDecl *Namespace
9676 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9677 Namespaces.push_back(Elt: Namespace);
9678 }
9679}
9680
9681void ASTReader::ReadUndefinedButUsed(
9682 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9683 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9684 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9685 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9686 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9687 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9688 }
9689 UndefinedButUsed.clear();
9690}
9691
9692void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9693 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9694 Exprs) {
9695 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9696 FieldDecl *FD =
9697 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9698 uint64_t Count = DelayedDeleteExprs[Idx++];
9699 for (uint64_t C = 0; C < Count; ++C) {
9700 SourceLocation DeleteLoc =
9701 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9702 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9703 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9704 }
9705 }
9706}
9707
9708void ASTReader::ReadTentativeDefinitions(
9709 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9710 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9711 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9712 if (Var)
9713 TentativeDefs.push_back(Elt: Var);
9714 }
9715 TentativeDefinitions.clear();
9716}
9717
9718void ASTReader::ReadUnusedFileScopedDecls(
9719 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9720 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9721 DeclaratorDecl *D
9722 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9723 if (D)
9724 Decls.push_back(Elt: D);
9725 }
9726 UnusedFileScopedDecls.clear();
9727}
9728
9729void ASTReader::ReadDelegatingConstructors(
9730 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9731 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9732 CXXConstructorDecl *D
9733 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9734 if (D)
9735 Decls.push_back(Elt: D);
9736 }
9737 DelegatingCtorDecls.clear();
9738}
9739
9740void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9741 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9742 TypedefNameDecl *D
9743 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9744 if (D)
9745 Decls.push_back(Elt: D);
9746 }
9747 ExtVectorDecls.clear();
9748}
9749
9750void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9751 llvm::SmallPtrSetImpl<const TypedefNameDecl *> &Decls) {
9752 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9753 ++I) {
9754 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9755 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9756 if (D)
9757 Decls.insert(Ptr: D);
9758 }
9759 UnusedLocalTypedefNameCandidates.clear();
9760}
9761
9762void ASTReader::ReadDeclsToCheckForDeferredDiags(
9763 llvm::SmallSetVector<Decl *, 4> &Decls) {
9764 for (auto I : DeclsToCheckForDeferredDiags) {
9765 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9766 if (D)
9767 Decls.insert(X: D);
9768 }
9769 DeclsToCheckForDeferredDiags.clear();
9770}
9771
9772void ASTReader::ReadReferencedSelectors(
9773 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9774 if (ReferencedSelectorsData.empty())
9775 return;
9776
9777 // If there are @selector references added them to its pool. This is for
9778 // implementation of -Wselector.
9779 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9780 unsigned I = 0;
9781 while (I < DataSize) {
9782 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9783 SourceLocation SelLoc
9784 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9785 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9786 }
9787 ReferencedSelectorsData.clear();
9788}
9789
9790void ASTReader::ReadWeakUndeclaredIdentifiers(
9791 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9792 if (WeakUndeclaredIdentifiers.empty())
9793 return;
9794
9795 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9796 IdentifierInfo *WeakId
9797 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9798 IdentifierInfo *AliasId
9799 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9800 SourceLocation Loc =
9801 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9802 WeakInfo WI(AliasId, Loc);
9803 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9804 }
9805 WeakUndeclaredIdentifiers.clear();
9806}
9807
9808void ASTReader::ReadExtnameUndeclaredIdentifiers(
9809 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9810 if (ExtnameUndeclaredIdentifiers.empty())
9811 return;
9812
9813 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9814 IdentifierInfo *NameId =
9815 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9816 IdentifierInfo *ExtnameId =
9817 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9818 SourceLocation Loc =
9819 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9820 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9821 Ctx&: getContext(), Label: ExtnameId->getName(),
9822 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9823 AttributeCommonInfo::Form::Pragma()));
9824 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9825 }
9826 ExtnameUndeclaredIdentifiers.clear();
9827}
9828
9829void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9830 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9831 ExternalVTableUse VT;
9832 VTableUse &TableInfo = VTableUses[Idx++];
9833 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9834 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9835 VT.DefinitionRequired = TableInfo.Used;
9836 VTables.push_back(Elt: VT);
9837 }
9838
9839 VTableUses.clear();
9840}
9841
9842void ASTReader::ReadPendingInstantiations(
9843 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9844 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9845 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9846 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9847 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9848
9849 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9850 }
9851 PendingInstantiations.clear();
9852}
9853
9854void ASTReader::ReadLateParsedTemplates(
9855 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9856 &LPTMap) {
9857 for (auto &LPT : LateParsedTemplates) {
9858 ModuleFile *FMod = LPT.first;
9859 RecordDataImpl &LateParsed = LPT.second;
9860 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9861 /* In loop */) {
9862 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9863
9864 auto LT = std::make_unique<LateParsedTemplate>();
9865 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9866 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9867
9868 ModuleFile *F = getOwningModuleFile(D: LT->D);
9869 assert(F && "No module");
9870
9871 unsigned TokN = LateParsed[Idx++];
9872 LT->Toks.reserve(N: TokN);
9873 for (unsigned T = 0; T < TokN; ++T)
9874 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9875
9876 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9877 }
9878 }
9879
9880 LateParsedTemplates.clear();
9881}
9882
9883void ASTReader::LoadSelector(Selector Sel) {
9884 // It would be complicated to avoid reading the methods anyway. So don't.
9885 ReadMethodPool(Sel);
9886}
9887
9888void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9889 assert(ID && "Non-zero identifier ID required");
9890 unsigned Index = translateIdentifierIDToIndex(ID).second;
9891 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9892 IdentifiersLoaded[Index] = II;
9893 if (DeserializationListener)
9894 DeserializationListener->IdentifierRead(ID, II);
9895}
9896
9897/// Set the globally-visible declarations associated with the given
9898/// identifier.
9899///
9900/// If the AST reader is currently in a state where the given declaration IDs
9901/// cannot safely be resolved, they are queued until it is safe to resolve
9902/// them.
9903///
9904/// \param II an IdentifierInfo that refers to one or more globally-visible
9905/// declarations.
9906///
9907/// \param DeclIDs the set of declaration IDs with the name @p II that are
9908/// visible at global scope.
9909///
9910/// \param Decls if non-null, this vector will be populated with the set of
9911/// deserialized declarations. These declarations will not be pushed into
9912/// scope.
9913void ASTReader::SetGloballyVisibleDecls(
9914 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9915 SmallVectorImpl<Decl *> *Decls) {
9916 if (NumCurrentElementsDeserializing && !Decls) {
9917 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9918 return;
9919 }
9920
9921 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9922 if (!SemaObj) {
9923 // Queue this declaration so that it will be added to the
9924 // translation unit scope and identifier's declaration chain
9925 // once a Sema object is known.
9926 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9927 continue;
9928 }
9929
9930 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9931
9932 // If we're simply supposed to record the declarations, do so now.
9933 if (Decls) {
9934 Decls->push_back(Elt: D);
9935 continue;
9936 }
9937
9938 // Introduce this declaration into the translation-unit scope
9939 // and add it to the declaration chain for this identifier, so
9940 // that (unqualified) name lookup will find it.
9941 pushExternalDeclIntoScope(D, Name: II);
9942 }
9943}
9944
9945std::pair<ModuleFile *, unsigned>
9946ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9947 if (ID == 0)
9948 return {nullptr, 0};
9949
9950 unsigned ModuleFileIndex = ID >> 32;
9951 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9952
9953 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9954 assert(getModuleManager().size() > ModuleFileIndex - 1);
9955
9956 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9957 assert(LocalID < MF.LocalNumIdentifiers);
9958 return {&MF, MF.BaseIdentifierID + LocalID};
9959}
9960
9961IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9962 if (ID == 0)
9963 return nullptr;
9964
9965 if (IdentifiersLoaded.empty()) {
9966 Error(Msg: "no identifier table in AST file");
9967 return nullptr;
9968 }
9969
9970 auto [M, Index] = translateIdentifierIDToIndex(ID);
9971 if (!IdentifiersLoaded[Index]) {
9972 assert(M != nullptr && "Untranslated Identifier ID?");
9973 assert(Index >= M->BaseIdentifierID);
9974 unsigned LocalIndex = Index - M->BaseIdentifierID;
9975 const unsigned char *Data =
9976 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9977
9978 ASTIdentifierLookupTrait Trait(*this, *M);
9979 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9980 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9981 auto &II = PP.getIdentifierTable().get(Name: Key);
9982 IdentifiersLoaded[Index] = &II;
9983 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9984 markIdentifierFromAST(Reader&: *this, II, IsModule);
9985 if (DeserializationListener)
9986 DeserializationListener->IdentifierRead(ID, II: &II);
9987 }
9988
9989 return IdentifiersLoaded[Index];
9990}
9991
9992IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9993 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9994}
9995
9996IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9997 if (LocalID < NUM_PREDEF_IDENT_IDS)
9998 return LocalID;
9999
10000 if (!M.ModuleOffsetMap.empty())
10001 ReadModuleOffsetMap(F&: M);
10002
10003 unsigned ModuleFileIndex = LocalID >> 32;
10004 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
10005 ModuleFile *MF =
10006 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10007 assert(MF && "malformed identifier ID encoding?");
10008
10009 if (!ModuleFileIndex)
10010 LocalID -= NUM_PREDEF_IDENT_IDS;
10011
10012 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10013}
10014
10015std::pair<ModuleFile *, unsigned>
10016ASTReader::translateMacroIDToIndex(MacroID ID) const {
10017 if (ID == 0)
10018 return {nullptr, 0};
10019
10020 unsigned ModuleFileIndex = ID >> 32;
10021 assert(ModuleFileIndex && "not translating loaded MacroID?");
10022 assert(getModuleManager().size() > ModuleFileIndex - 1);
10023 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10024
10025 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10026 assert(LocalID < MF.LocalNumMacros);
10027 return {&MF, MF.BaseMacroID + LocalID};
10028}
10029
10030MacroInfo *ASTReader::getMacro(MacroID ID) {
10031 if (ID == 0)
10032 return nullptr;
10033
10034 if (MacrosLoaded.empty()) {
10035 Error(Msg: "no macro table in AST file");
10036 return nullptr;
10037 }
10038
10039 auto [M, Index] = translateMacroIDToIndex(ID);
10040 if (!MacrosLoaded[Index]) {
10041 assert(M != nullptr && "Untranslated Macro ID?");
10042 assert(Index >= M->BaseMacroID);
10043 unsigned LocalIndex = Index - M->BaseMacroID;
10044 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10045 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10046
10047 if (DeserializationListener)
10048 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10049 }
10050
10051 return MacrosLoaded[Index];
10052}
10053
10054MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10055 if (LocalID < NUM_PREDEF_MACRO_IDS)
10056 return LocalID;
10057
10058 if (!M.ModuleOffsetMap.empty())
10059 ReadModuleOffsetMap(F&: M);
10060
10061 unsigned ModuleFileIndex = LocalID >> 32;
10062 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10063 ModuleFile *MF =
10064 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10065 assert(MF && "malformed identifier ID encoding?");
10066
10067 if (!ModuleFileIndex) {
10068 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10069 LocalID -= NUM_PREDEF_MACRO_IDS;
10070 }
10071
10072 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10073}
10074
10075serialization::SubmoduleID
10076ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10077 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10078 return LocalID;
10079
10080 if (!M.ModuleOffsetMap.empty())
10081 ReadModuleOffsetMap(F&: M);
10082
10083 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10084 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10085 assert(I != M.SubmoduleRemap.end()
10086 && "Invalid index into submodule index remap");
10087
10088 return LocalID + I->second;
10089}
10090
10091Module *ASTReader::getModule(unsigned ID) {
10092 return getSubmodule(GlobalID: ID);
10093}
10094
10095ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10096 if (ID & 1) {
10097 // It's a module, look it up by submodule ID.
10098 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10099 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10100 } else {
10101 // It's a prefix (preamble, PCH, ...). Look it up by index.
10102 int IndexFromEnd = static_cast<int>(ID >> 1);
10103 assert(IndexFromEnd && "got reference to unknown module file");
10104 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10105 }
10106}
10107
10108unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10109 if (!M)
10110 return 1;
10111
10112 // For a file representing a module, use the submodule ID of the top-level
10113 // module as the file ID. For any other kind of file, the number of such
10114 // files loaded beforehand will be the same on reload.
10115 // FIXME: Is this true even if we have an explicit module file and a PCH?
10116 if (M->isModule())
10117 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10118
10119 auto PCHModules = getModuleManager().pch_modules();
10120 auto I = llvm::find(Range&: PCHModules, Val: M);
10121 assert(I != PCHModules.end() && "emitting reference to unknown file");
10122 return std::distance(first: I, last: PCHModules.end()) << 1;
10123}
10124
10125std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10126 if (Module *M = getSubmodule(GlobalID: ID))
10127 return ASTSourceDescriptor(*M);
10128
10129 // If there is only a single PCH, return it instead.
10130 // Chained PCH are not supported.
10131 const auto &PCHChain = ModuleMgr.pch_modules();
10132 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10133 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10134 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10135 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10136 return ASTSourceDescriptor(ModuleName,
10137 llvm::sys::path::parent_path(path: MF.FileName),
10138 FileName, MF.Signature);
10139 }
10140 return std::nullopt;
10141}
10142
10143ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10144 auto I = DefinitionSource.find(Val: FD);
10145 if (I == DefinitionSource.end())
10146 return EK_ReplyHazy;
10147 return I->second ? EK_Never : EK_Always;
10148}
10149
10150bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10151 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10152}
10153
10154Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10155 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10156}
10157
10158Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10159 if (ID == 0)
10160 return Selector();
10161
10162 if (ID > SelectorsLoaded.size()) {
10163 Error(Msg: "selector ID out of range in AST file");
10164 return Selector();
10165 }
10166
10167 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10168 // Load this selector from the selector table.
10169 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10170 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10171 ModuleFile &M = *I->second;
10172 ASTSelectorLookupTrait Trait(*this, M);
10173 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10174 SelectorsLoaded[ID - 1] =
10175 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10176 if (DeserializationListener)
10177 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10178 }
10179
10180 return SelectorsLoaded[ID - 1];
10181}
10182
10183Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10184 return DecodeSelector(ID);
10185}
10186
10187uint32_t ASTReader::GetNumExternalSelectors() {
10188 // ID 0 (the null selector) is considered an external selector.
10189 return getTotalNumSelectors() + 1;
10190}
10191
10192serialization::SelectorID
10193ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10194 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10195 return LocalID;
10196
10197 if (!M.ModuleOffsetMap.empty())
10198 ReadModuleOffsetMap(F&: M);
10199
10200 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10201 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10202 assert(I != M.SelectorRemap.end()
10203 && "Invalid index into selector index remap");
10204
10205 return LocalID + I->second;
10206}
10207
10208DeclarationNameLoc
10209ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10210 switch (Name.getNameKind()) {
10211 case DeclarationName::CXXConstructorName:
10212 case DeclarationName::CXXDestructorName:
10213 case DeclarationName::CXXConversionFunctionName:
10214 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10215
10216 case DeclarationName::CXXOperatorName:
10217 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10218
10219 case DeclarationName::CXXLiteralOperatorName:
10220 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10221 Loc: readSourceLocation());
10222
10223 case DeclarationName::Identifier:
10224 case DeclarationName::ObjCZeroArgSelector:
10225 case DeclarationName::ObjCOneArgSelector:
10226 case DeclarationName::ObjCMultiArgSelector:
10227 case DeclarationName::CXXUsingDirective:
10228 case DeclarationName::CXXDeductionGuideName:
10229 break;
10230 }
10231 return DeclarationNameLoc();
10232}
10233
10234DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10235 DeclarationNameInfo NameInfo;
10236 NameInfo.setName(readDeclarationName());
10237 NameInfo.setLoc(readSourceLocation());
10238 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10239 return NameInfo;
10240}
10241
10242TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10243 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10244}
10245
10246SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10247 auto Kind = readInt();
10248 auto ResultType = readQualType();
10249 auto Value = readAPInt();
10250 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10251 assert(Op.isValid());
10252 return Op;
10253}
10254
10255void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10256 Info.QualifierLoc = readNestedNameSpecifierLoc();
10257 unsigned NumTPLists = readInt();
10258 Info.NumTemplParamLists = NumTPLists;
10259 if (NumTPLists) {
10260 Info.TemplParamLists =
10261 new (getContext()) TemplateParameterList *[NumTPLists];
10262 for (unsigned i = 0; i != NumTPLists; ++i)
10263 Info.TemplParamLists[i] = readTemplateParameterList();
10264 }
10265}
10266
10267TemplateParameterList *
10268ASTRecordReader::readTemplateParameterList() {
10269 SourceLocation TemplateLoc = readSourceLocation();
10270 SourceLocation LAngleLoc = readSourceLocation();
10271 SourceLocation RAngleLoc = readSourceLocation();
10272
10273 unsigned NumParams = readInt();
10274 SmallVector<NamedDecl *, 16> Params;
10275 Params.reserve(N: NumParams);
10276 while (NumParams--)
10277 Params.push_back(Elt: readDeclAs<NamedDecl>());
10278
10279 bool HasRequiresClause = readBool();
10280 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10281
10282 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10283 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10284 return TemplateParams;
10285}
10286
10287void ASTRecordReader::readTemplateArgumentList(
10288 SmallVectorImpl<TemplateArgument> &TemplArgs,
10289 bool Canonicalize) {
10290 unsigned NumTemplateArgs = readInt();
10291 TemplArgs.reserve(N: NumTemplateArgs);
10292 while (NumTemplateArgs--)
10293 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10294}
10295
10296/// Read a UnresolvedSet structure.
10297void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10298 unsigned NumDecls = readInt();
10299 Set.reserve(C&: getContext(), N: NumDecls);
10300 while (NumDecls--) {
10301 GlobalDeclID ID = readDeclID();
10302 AccessSpecifier AS = (AccessSpecifier) readInt();
10303 Set.addLazyDecl(C&: getContext(), ID, AS);
10304 }
10305}
10306
10307CXXBaseSpecifier
10308ASTRecordReader::readCXXBaseSpecifier() {
10309 bool isVirtual = readBool();
10310 bool isBaseOfClass = readBool();
10311 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10312 bool inheritConstructors = readBool();
10313 TypeSourceInfo *TInfo = readTypeSourceInfo();
10314 SourceRange Range = readSourceRange();
10315 SourceLocation EllipsisLoc = readSourceLocation();
10316 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10317 EllipsisLoc);
10318 Result.setInheritConstructors(inheritConstructors);
10319 return Result;
10320}
10321
10322CXXCtorInitializer **
10323ASTRecordReader::readCXXCtorInitializers() {
10324 ASTContext &Context = getContext();
10325 unsigned NumInitializers = readInt();
10326 assert(NumInitializers && "wrote ctor initializers but have no inits");
10327 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10328 for (unsigned i = 0; i != NumInitializers; ++i) {
10329 TypeSourceInfo *TInfo = nullptr;
10330 bool IsBaseVirtual = false;
10331 FieldDecl *Member = nullptr;
10332 IndirectFieldDecl *IndirectMember = nullptr;
10333
10334 CtorInitializerType Type = (CtorInitializerType) readInt();
10335 switch (Type) {
10336 case CTOR_INITIALIZER_BASE:
10337 TInfo = readTypeSourceInfo();
10338 IsBaseVirtual = readBool();
10339 break;
10340
10341 case CTOR_INITIALIZER_DELEGATING:
10342 TInfo = readTypeSourceInfo();
10343 break;
10344
10345 case CTOR_INITIALIZER_MEMBER:
10346 Member = readDeclAs<FieldDecl>();
10347 break;
10348
10349 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10350 IndirectMember = readDeclAs<IndirectFieldDecl>();
10351 break;
10352 }
10353
10354 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10355 Expr *Init = readExpr();
10356 SourceLocation LParenLoc = readSourceLocation();
10357 SourceLocation RParenLoc = readSourceLocation();
10358
10359 CXXCtorInitializer *BOMInit;
10360 if (Type == CTOR_INITIALIZER_BASE)
10361 BOMInit = new (Context)
10362 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10363 RParenLoc, MemberOrEllipsisLoc);
10364 else if (Type == CTOR_INITIALIZER_DELEGATING)
10365 BOMInit = new (Context)
10366 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10367 else if (Member)
10368 BOMInit = new (Context)
10369 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10370 Init, RParenLoc);
10371 else
10372 BOMInit = new (Context)
10373 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10374 LParenLoc, Init, RParenLoc);
10375
10376 if (/*IsWritten*/readBool()) {
10377 unsigned SourceOrder = readInt();
10378 BOMInit->setSourceOrder(SourceOrder);
10379 }
10380
10381 CtorInitializers[i] = BOMInit;
10382 }
10383
10384 return CtorInitializers;
10385}
10386
10387NestedNameSpecifierLoc
10388ASTRecordReader::readNestedNameSpecifierLoc() {
10389 ASTContext &Context = getContext();
10390 unsigned N = readInt();
10391 NestedNameSpecifierLocBuilder Builder;
10392 for (unsigned I = 0; I != N; ++I) {
10393 auto Kind = readNestedNameSpecifierKind();
10394 switch (Kind) {
10395 case NestedNameSpecifier::Kind::Namespace: {
10396 auto *NS = readDeclAs<NamespaceBaseDecl>();
10397 SourceRange Range = readSourceRange();
10398 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10399 break;
10400 }
10401
10402 case NestedNameSpecifier::Kind::Type: {
10403 TypeSourceInfo *T = readTypeSourceInfo();
10404 if (!T)
10405 return NestedNameSpecifierLoc();
10406 SourceLocation ColonColonLoc = readSourceLocation();
10407 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10408 break;
10409 }
10410
10411 case NestedNameSpecifier::Kind::Global: {
10412 SourceLocation ColonColonLoc = readSourceLocation();
10413 Builder.MakeGlobal(Context, ColonColonLoc);
10414 break;
10415 }
10416
10417 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10418 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10419 SourceRange Range = readSourceRange();
10420 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10421 break;
10422 }
10423
10424 case NestedNameSpecifier::Kind::Null:
10425 llvm_unreachable("unexpected null nested name specifier");
10426 }
10427 }
10428
10429 return Builder.getWithLocInContext(Context);
10430}
10431
10432SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10433 unsigned &Idx) {
10434 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10435 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10436 return SourceRange(beg, end);
10437}
10438
10439llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10440 const StringRef Blob) {
10441 unsigned Count = Record[0];
10442 const char *Byte = Blob.data();
10443 llvm::BitVector Ret = llvm::BitVector(Count, false);
10444 for (unsigned I = 0; I < Count; ++Byte)
10445 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10446 if (*Byte & (1 << Bit))
10447 Ret[I] = true;
10448 return Ret;
10449}
10450
10451/// Read a floating-point value
10452llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10453 return llvm::APFloat(Sem, readAPInt());
10454}
10455
10456// Read a string
10457std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10458 unsigned Len = Record[Idx++];
10459 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10460 Idx += Len;
10461 return Result;
10462}
10463
10464StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10465 StringRef &Blob) {
10466 unsigned Len = Record[Idx++];
10467 StringRef Result = Blob.substr(Start: 0, N: Len);
10468 Blob = Blob.substr(Start: Len);
10469 return Result;
10470}
10471
10472std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10473 unsigned &Idx) {
10474 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10475}
10476
10477std::string ASTReader::ReadPath(StringRef BaseDirectory,
10478 const RecordData &Record, unsigned &Idx) {
10479 std::string Filename = ReadString(Record, Idx);
10480 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10481}
10482
10483std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10484 const RecordData &Record, unsigned &Idx,
10485 StringRef &Blob) {
10486 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10487 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10488}
10489
10490VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10491 unsigned &Idx) {
10492 unsigned Major = Record[Idx++];
10493 unsigned Minor = Record[Idx++];
10494 unsigned Subminor = Record[Idx++];
10495 if (Minor == 0)
10496 return VersionTuple(Major);
10497 if (Subminor == 0)
10498 return VersionTuple(Major, Minor - 1);
10499 return VersionTuple(Major, Minor - 1, Subminor - 1);
10500}
10501
10502CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10503 const RecordData &Record,
10504 unsigned &Idx) {
10505 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10506 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10507}
10508
10509DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10510 return Diag(Loc: CurrentImportLoc, DiagID);
10511}
10512
10513DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10514 return Diags.Report(Loc, DiagID);
10515}
10516
10517void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10518 llvm::function_ref<void()> Fn) {
10519 // When Sema is available, avoid duplicate errors.
10520 if (SemaObj) {
10521 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10522 return;
10523 }
10524
10525 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10526}
10527
10528/// Retrieve the identifier table associated with the
10529/// preprocessor.
10530IdentifierTable &ASTReader::getIdentifierTable() {
10531 return PP.getIdentifierTable();
10532}
10533
10534/// Record that the given ID maps to the given switch-case
10535/// statement.
10536void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10537 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10538 "Already have a SwitchCase with this ID");
10539 (*CurrSwitchCaseStmts)[ID] = SC;
10540}
10541
10542/// Retrieve the switch-case statement with the given ID.
10543SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10544 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10545 return (*CurrSwitchCaseStmts)[ID];
10546}
10547
10548void ASTReader::ClearSwitchCaseIDs() {
10549 CurrSwitchCaseStmts->clear();
10550}
10551
10552void ASTReader::ReadComments() {
10553 ASTContext &Context = getContext();
10554 std::vector<RawComment *> Comments;
10555 for (SmallVectorImpl<std::pair<BitstreamCursor,
10556 serialization::ModuleFile *>>::iterator
10557 I = CommentsCursors.begin(),
10558 E = CommentsCursors.end();
10559 I != E; ++I) {
10560 Comments.clear();
10561 BitstreamCursor &Cursor = I->first;
10562 serialization::ModuleFile &F = *I->second;
10563 SavedStreamPosition SavedPosition(Cursor);
10564
10565 RecordData Record;
10566 while (true) {
10567 Expected<llvm::BitstreamEntry> MaybeEntry =
10568 Cursor.advanceSkippingSubblocks(
10569 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10570 if (!MaybeEntry) {
10571 Error(Err: MaybeEntry.takeError());
10572 return;
10573 }
10574 llvm::BitstreamEntry Entry = MaybeEntry.get();
10575
10576 switch (Entry.Kind) {
10577 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10578 case llvm::BitstreamEntry::Error:
10579 Error(Msg: "malformed block record in AST file");
10580 return;
10581 case llvm::BitstreamEntry::EndBlock:
10582 goto NextCursor;
10583 case llvm::BitstreamEntry::Record:
10584 // The interesting case.
10585 break;
10586 }
10587
10588 // Read a record.
10589 Record.clear();
10590 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10591 if (!MaybeComment) {
10592 Error(Err: MaybeComment.takeError());
10593 return;
10594 }
10595 switch ((CommentRecordTypes)MaybeComment.get()) {
10596 case COMMENTS_RAW_COMMENT: {
10597 unsigned Idx = 0;
10598 SourceRange SR = ReadSourceRange(F, Record, Idx);
10599 RawComment::CommentKind Kind =
10600 (RawComment::CommentKind) Record[Idx++];
10601 bool IsTrailingComment = Record[Idx++];
10602 bool IsAlmostTrailingComment = Record[Idx++];
10603 Comments.push_back(x: new (Context) RawComment(
10604 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10605 break;
10606 }
10607 }
10608 }
10609 NextCursor:
10610 for (RawComment *C : Comments) {
10611 SourceLocation CommentLoc = C->getBeginLoc();
10612 if (CommentLoc.isValid()) {
10613 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10614 if (Loc.first.isValid())
10615 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10616 }
10617 }
10618 }
10619}
10620
10621void ASTReader::visitInputFileInfos(
10622 serialization::ModuleFile &MF, bool IncludeSystem,
10623 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10624 bool IsSystem)>
10625 Visitor) {
10626 unsigned NumUserInputs = MF.NumUserInputFiles;
10627 unsigned NumInputs = MF.InputFilesLoaded.size();
10628 assert(NumUserInputs <= NumInputs);
10629 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10630 for (unsigned I = 0; I < N; ++I) {
10631 bool IsSystem = I >= NumUserInputs;
10632 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10633 Visitor(IFI, IsSystem);
10634 }
10635}
10636
10637void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10638 bool IncludeSystem, bool Complain,
10639 llvm::function_ref<void(const serialization::InputFile &IF,
10640 bool isSystem)> Visitor) {
10641 unsigned NumUserInputs = MF.NumUserInputFiles;
10642 unsigned NumInputs = MF.InputFilesLoaded.size();
10643 assert(NumUserInputs <= NumInputs);
10644 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10645 for (unsigned I = 0; I < N; ++I) {
10646 bool IsSystem = I >= NumUserInputs;
10647 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10648 Visitor(IF, IsSystem);
10649 }
10650}
10651
10652void ASTReader::visitTopLevelModuleMaps(
10653 serialization::ModuleFile &MF,
10654 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10655 unsigned NumInputs = MF.InputFilesLoaded.size();
10656 for (unsigned I = 0; I < NumInputs; ++I) {
10657 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10658 if (IFI.TopLevel && IFI.ModuleMap)
10659 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10660 Visitor(*FE);
10661 }
10662}
10663
10664void ASTReader::finishPendingActions() {
10665 while (!PendingIdentifierInfos.empty() ||
10666 !PendingDeducedFunctionTypes.empty() ||
10667 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10668 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10669 !PendingUpdateRecords.empty() ||
10670 !PendingObjCExtensionIvarRedeclarations.empty()) {
10671 // If any identifiers with corresponding top-level declarations have
10672 // been loaded, load those declarations now.
10673 using TopLevelDeclsMap =
10674 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10675 TopLevelDeclsMap TopLevelDecls;
10676
10677 while (!PendingIdentifierInfos.empty()) {
10678 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10679 SmallVector<GlobalDeclID, 4> DeclIDs =
10680 std::move(PendingIdentifierInfos.back().second);
10681 PendingIdentifierInfos.pop_back();
10682
10683 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10684 }
10685
10686 // Load each function type that we deferred loading because it was a
10687 // deduced type that might refer to a local type declared within itself.
10688 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10689 auto *FD = PendingDeducedFunctionTypes[I].first;
10690 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10691
10692 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10693 // If we gave a function a deduced return type, remember that we need to
10694 // propagate that along the redeclaration chain.
10695 if (DT->isDeduced()) {
10696 PendingDeducedTypeUpdates.insert(
10697 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10698 continue;
10699 }
10700
10701 // The function has undeduced DeduceType return type. We hope we can
10702 // find the deduced type by iterating the redecls in other modules
10703 // later.
10704 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10705 continue;
10706 }
10707 }
10708 PendingDeducedFunctionTypes.clear();
10709
10710 // Load each variable type that we deferred loading because it was a
10711 // deduced type that might refer to a local type declared within itself.
10712 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10713 auto *VD = PendingDeducedVarTypes[I].first;
10714 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10715 }
10716 PendingDeducedVarTypes.clear();
10717
10718 // Load pending declaration chains.
10719 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10720 loadPendingDeclChain(D: PendingDeclChains[I].first,
10721 LocalOffset: PendingDeclChains[I].second);
10722 PendingDeclChains.clear();
10723
10724 // Make the most recent of the top-level declarations visible.
10725 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10726 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10727 IdentifierInfo *II = TLD->first;
10728 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10729 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10730 }
10731 }
10732
10733 // Load any pending macro definitions.
10734 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10735 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10736 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10737 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10738 // Initialize the macro history from chained-PCHs ahead of module imports.
10739 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10740 ++IDIdx) {
10741 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10742 if (!Info.M->isModule())
10743 resolvePendingMacro(II, PMInfo: Info);
10744 }
10745 // Handle module imports.
10746 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10747 ++IDIdx) {
10748 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10749 if (Info.M->isModule())
10750 resolvePendingMacro(II, PMInfo: Info);
10751 }
10752 }
10753 PendingMacroIDs.clear();
10754
10755 // Wire up the DeclContexts for Decls that we delayed setting until
10756 // recursive loading is completed.
10757 while (!PendingDeclContextInfos.empty()) {
10758 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10759 PendingDeclContextInfos.pop_front();
10760 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10761 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10762 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10763 }
10764
10765 // Perform any pending declaration updates.
10766 while (!PendingUpdateRecords.empty()) {
10767 auto Update = PendingUpdateRecords.pop_back_val();
10768 ReadingKindTracker ReadingKind(Read_Decl, *this);
10769 loadDeclUpdateRecords(Record&: Update);
10770 }
10771
10772 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10773 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10774 auto DuplicateIvars =
10775 PendingObjCExtensionIvarRedeclarations.back().second;
10776 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10777 StructuralEquivalenceContext Ctx(
10778 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10779 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10780 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10781 /*Complain =*/false,
10782 /*ErrorOnTagTypeMismatch =*/true);
10783 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10784 // Merge redeclared ivars with their predecessors.
10785 for (auto IvarPair : DuplicateIvars) {
10786 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10787 // Change semantic DeclContext but keep the lexical one.
10788 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10789 LexicalDC: Ivar->getLexicalDeclContext(),
10790 Ctx&: getContext());
10791 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10792 }
10793 // Invalidate duplicate extension and the cached ivar list.
10794 ExtensionsPair.first->setInvalidDecl();
10795 ExtensionsPair.second->getClassInterface()
10796 ->getDefinition()
10797 ->setIvarList(nullptr);
10798 } else {
10799 for (auto IvarPair : DuplicateIvars) {
10800 Diag(Loc: IvarPair.first->getLocation(),
10801 DiagID: diag::err_duplicate_ivar_declaration)
10802 << IvarPair.first->getIdentifier();
10803 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10804 }
10805 }
10806 PendingObjCExtensionIvarRedeclarations.pop_back();
10807 }
10808 }
10809
10810 // At this point, all update records for loaded decls are in place, so any
10811 // fake class definitions should have become real.
10812 assert(PendingFakeDefinitionData.empty() &&
10813 "faked up a class definition but never saw the real one");
10814
10815 // If we deserialized any C++ or Objective-C class definitions, any
10816 // Objective-C protocol definitions, or any redeclarable templates, make sure
10817 // that all redeclarations point to the definitions. Note that this can only
10818 // happen now, after the redeclaration chains have been fully wired.
10819 for (Decl *D : PendingDefinitions) {
10820 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10821 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10822 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10823 R = R->getPreviousDecl()) {
10824 assert((R == D) ==
10825 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10826 "declaration thinks it's the definition but it isn't");
10827 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10828 }
10829 }
10830
10831 continue;
10832 }
10833
10834 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10835 // Make sure that the ObjCInterfaceType points at the definition.
10836 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10837 ->Decl = ID;
10838
10839 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10840 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10841
10842 continue;
10843 }
10844
10845 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10846 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10847 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10848
10849 continue;
10850 }
10851
10852 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10853 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10854 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10855 }
10856 PendingDefinitions.clear();
10857
10858 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10859 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10860 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10861 return VD->isThisDeclarationADefinition() ||
10862 VD->isThisDeclarationADemotedDefinition();
10863
10864 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10865 return TD->isThisDeclarationADefinition() ||
10866 TD->isThisDeclarationADemotedDefinition();
10867
10868 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10869 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10870
10871 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10872 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10873
10874 // Conservatively return false here.
10875 return false;
10876 };
10877
10878 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10879 return hasDefinitionImpl(D, hasDefinitionImpl);
10880 };
10881
10882 // It is not good to prevent multiple declarations since the forward
10883 // declaration is common. Let's try to avoid duplicated definitions
10884 // only.
10885 if (!hasDefinition(D) || !hasDefinition(Previous))
10886 continue;
10887
10888 Module *PM = Previous->getOwningModule();
10889 Module *DM = D->getOwningModule();
10890 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10891 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10892 << (DM ? DM->getTopLevelModuleName() : "global module");
10893 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10894 }
10895 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10896
10897 // Load the bodies of any functions or methods we've encountered. We do
10898 // this now (delayed) so that we can be sure that the declaration chains
10899 // have been fully wired up (hasBody relies on this).
10900 // FIXME: We shouldn't require complete redeclaration chains here.
10901 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10902 PBEnd = PendingBodies.end();
10903 PB != PBEnd; ++PB) {
10904 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10905 // FIXME: Check for =delete/=default?
10906 const FunctionDecl *Defn = nullptr;
10907 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10908 FD->setLazyBody(PB->second);
10909 } else {
10910 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10911 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10912
10913 if (!FD->isLateTemplateParsed() &&
10914 !NonConstDefn->isLateTemplateParsed() &&
10915 // We only perform ODR checks for decls not in the explicit
10916 // global module fragment.
10917 !shouldSkipCheckingODR(D: FD) &&
10918 !shouldSkipCheckingODR(D: NonConstDefn) &&
10919 FD->getODRHash() != NonConstDefn->getODRHash()) {
10920 if (!isa<CXXMethodDecl>(Val: FD)) {
10921 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10922 } else if (FD->getLexicalParent()->isFileContext() &&
10923 NonConstDefn->getLexicalParent()->isFileContext()) {
10924 // Only diagnose out-of-line method definitions. If they are
10925 // in class definitions, then an error will be generated when
10926 // processing the class bodies.
10927 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10928 }
10929 }
10930 }
10931 continue;
10932 }
10933
10934 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10935 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10936 MD->setLazyBody(PB->second);
10937 }
10938 PendingBodies.clear();
10939
10940 // Inform any classes that had members added that they now have more members.
10941 for (auto [RD, MD] : PendingAddedClassMembers) {
10942 RD->addedMember(D: MD);
10943 }
10944 PendingAddedClassMembers.clear();
10945
10946 // Do some cleanup.
10947 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10948 getContext().deduplicateMergedDefinitionsFor(ND);
10949 PendingMergedDefinitionsToDeduplicate.clear();
10950
10951 // For each decl chain that we wanted to complete while deserializing, mark
10952 // it as "still needs to be completed".
10953 for (Decl *D : PendingIncompleteDeclChains)
10954 markIncompleteDeclChain(D);
10955 PendingIncompleteDeclChains.clear();
10956
10957 assert(PendingIdentifierInfos.empty() &&
10958 "Should be empty at the end of finishPendingActions");
10959 assert(PendingDeducedFunctionTypes.empty() &&
10960 "Should be empty at the end of finishPendingActions");
10961 assert(PendingDeducedVarTypes.empty() &&
10962 "Should be empty at the end of finishPendingActions");
10963 assert(PendingDeclChains.empty() &&
10964 "Should be empty at the end of finishPendingActions");
10965 assert(PendingMacroIDs.empty() &&
10966 "Should be empty at the end of finishPendingActions");
10967 assert(PendingDeclContextInfos.empty() &&
10968 "Should be empty at the end of finishPendingActions");
10969 assert(PendingUpdateRecords.empty() &&
10970 "Should be empty at the end of finishPendingActions");
10971 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10972 "Should be empty at the end of finishPendingActions");
10973 assert(PendingFakeDefinitionData.empty() &&
10974 "Should be empty at the end of finishPendingActions");
10975 assert(PendingDefinitions.empty() &&
10976 "Should be empty at the end of finishPendingActions");
10977 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10978 "Should be empty at the end of finishPendingActions");
10979 assert(PendingBodies.empty() &&
10980 "Should be empty at the end of finishPendingActions");
10981 assert(PendingAddedClassMembers.empty() &&
10982 "Should be empty at the end of finishPendingActions");
10983 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10984 "Should be empty at the end of finishPendingActions");
10985 assert(PendingIncompleteDeclChains.empty() &&
10986 "Should be empty at the end of finishPendingActions");
10987}
10988
10989void ASTReader::diagnoseOdrViolations() {
10990 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10991 PendingRecordOdrMergeFailures.empty() &&
10992 PendingFunctionOdrMergeFailures.empty() &&
10993 PendingEnumOdrMergeFailures.empty() &&
10994 PendingObjCInterfaceOdrMergeFailures.empty() &&
10995 PendingObjCProtocolOdrMergeFailures.empty())
10996 return;
10997
10998 // Trigger the import of the full definition of each class that had any
10999 // odr-merging problems, so we can produce better diagnostics for them.
11000 // These updates may in turn find and diagnose some ODR failures, so take
11001 // ownership of the set first.
11002 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
11003 PendingOdrMergeFailures.clear();
11004 for (auto &Merge : OdrMergeFailures) {
11005 Merge.first->buildLookup();
11006 Merge.first->decls_begin();
11007 Merge.first->bases_begin();
11008 Merge.first->vbases_begin();
11009 for (auto &RecordPair : Merge.second) {
11010 auto *RD = RecordPair.first;
11011 RD->decls_begin();
11012 RD->bases_begin();
11013 RD->vbases_begin();
11014 }
11015 }
11016
11017 // Trigger the import of the full definition of each record in C/ObjC.
11018 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11019 PendingRecordOdrMergeFailures.clear();
11020 for (auto &Merge : RecordOdrMergeFailures) {
11021 Merge.first->decls_begin();
11022 for (auto &D : Merge.second)
11023 D->decls_begin();
11024 }
11025
11026 // Trigger the import of the full interface definition.
11027 auto ObjCInterfaceOdrMergeFailures =
11028 std::move(PendingObjCInterfaceOdrMergeFailures);
11029 PendingObjCInterfaceOdrMergeFailures.clear();
11030 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11031 Merge.first->decls_begin();
11032 for (auto &InterfacePair : Merge.second)
11033 InterfacePair.first->decls_begin();
11034 }
11035
11036 // Trigger the import of functions.
11037 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11038 PendingFunctionOdrMergeFailures.clear();
11039 for (auto &Merge : FunctionOdrMergeFailures) {
11040 Merge.first->buildLookup();
11041 Merge.first->decls_begin();
11042 Merge.first->getBody();
11043 for (auto &FD : Merge.second) {
11044 FD->buildLookup();
11045 FD->decls_begin();
11046 FD->getBody();
11047 }
11048 }
11049
11050 // Trigger the import of enums.
11051 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11052 PendingEnumOdrMergeFailures.clear();
11053 for (auto &Merge : EnumOdrMergeFailures) {
11054 Merge.first->decls_begin();
11055 for (auto &Enum : Merge.second) {
11056 Enum->decls_begin();
11057 }
11058 }
11059
11060 // Trigger the import of the full protocol definition.
11061 auto ObjCProtocolOdrMergeFailures =
11062 std::move(PendingObjCProtocolOdrMergeFailures);
11063 PendingObjCProtocolOdrMergeFailures.clear();
11064 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11065 Merge.first->decls_begin();
11066 for (auto &ProtocolPair : Merge.second)
11067 ProtocolPair.first->decls_begin();
11068 }
11069
11070 // For each declaration from a merged context, check that the canonical
11071 // definition of that context also contains a declaration of the same
11072 // entity.
11073 //
11074 // Caution: this loop does things that might invalidate iterators into
11075 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11076 while (!PendingOdrMergeChecks.empty()) {
11077 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11078
11079 // FIXME: Skip over implicit declarations for now. This matters for things
11080 // like implicitly-declared special member functions. This isn't entirely
11081 // correct; we can end up with multiple unmerged declarations of the same
11082 // implicit entity.
11083 if (D->isImplicit())
11084 continue;
11085
11086 DeclContext *CanonDef = D->getDeclContext();
11087
11088 bool Found = false;
11089 const Decl *DCanon = D->getCanonicalDecl();
11090
11091 for (auto *RI : D->redecls()) {
11092 if (RI->getLexicalDeclContext() == CanonDef) {
11093 Found = true;
11094 break;
11095 }
11096 }
11097 if (Found)
11098 continue;
11099
11100 // Quick check failed, time to do the slow thing. Note, we can't just
11101 // look up the name of D in CanonDef here, because the member that is
11102 // in CanonDef might not be found by name lookup (it might have been
11103 // replaced by a more recent declaration in the lookup table), and we
11104 // can't necessarily find it in the redeclaration chain because it might
11105 // be merely mergeable, not redeclarable.
11106 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11107 for (auto *CanonMember : CanonDef->decls()) {
11108 if (CanonMember->getCanonicalDecl() == DCanon) {
11109 // This can happen if the declaration is merely mergeable and not
11110 // actually redeclarable (we looked for redeclarations earlier).
11111 //
11112 // FIXME: We should be able to detect this more efficiently, without
11113 // pulling in all of the members of CanonDef.
11114 Found = true;
11115 break;
11116 }
11117 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11118 if (ND->getDeclName() == D->getDeclName())
11119 Candidates.push_back(Elt: ND);
11120 }
11121
11122 if (!Found) {
11123 // The AST doesn't like TagDecls becoming invalid after they've been
11124 // completed. We only really need to mark FieldDecls as invalid here.
11125 if (!isa<TagDecl>(Val: D))
11126 D->setInvalidDecl();
11127
11128 // Ensure we don't accidentally recursively enter deserialization while
11129 // we're producing our diagnostic.
11130 Deserializing RecursionGuard(this);
11131
11132 std::string CanonDefModule =
11133 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11134 D: cast<Decl>(Val: CanonDef));
11135 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11136 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11137 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11138
11139 if (Candidates.empty())
11140 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11141 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11142 else {
11143 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11144 Diag(Loc: Candidates[I]->getLocation(),
11145 DiagID: diag::note_module_odr_violation_possible_decl)
11146 << Candidates[I];
11147 }
11148
11149 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11150 }
11151 }
11152
11153 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11154 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11155 ObjCInterfaceOdrMergeFailures.empty() &&
11156 ObjCProtocolOdrMergeFailures.empty())
11157 return;
11158
11159 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11160 getPreprocessor().getLangOpts());
11161
11162 // Issue any pending ODR-failure diagnostics.
11163 for (auto &Merge : OdrMergeFailures) {
11164 // If we've already pointed out a specific problem with this class, don't
11165 // bother issuing a general "something's different" diagnostic.
11166 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11167 continue;
11168
11169 bool Diagnosed = false;
11170 CXXRecordDecl *FirstRecord = Merge.first;
11171 for (auto &RecordPair : Merge.second) {
11172 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11173 SecondDD: RecordPair.second)) {
11174 Diagnosed = true;
11175 break;
11176 }
11177 }
11178
11179 if (!Diagnosed) {
11180 // All definitions are updates to the same declaration. This happens if a
11181 // module instantiates the declaration of a class template specialization
11182 // and two or more other modules instantiate its definition.
11183 //
11184 // FIXME: Indicate which modules had instantiations of this definition.
11185 // FIXME: How can this even happen?
11186 Diag(Loc: Merge.first->getLocation(),
11187 DiagID: diag::err_module_odr_violation_different_instantiations)
11188 << Merge.first;
11189 }
11190 }
11191
11192 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11193 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11194 for (auto &Merge : RecordOdrMergeFailures) {
11195 // If we've already pointed out a specific problem with this class, don't
11196 // bother issuing a general "something's different" diagnostic.
11197 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11198 continue;
11199
11200 RecordDecl *FirstRecord = Merge.first;
11201 bool Diagnosed = false;
11202 for (auto *SecondRecord : Merge.second) {
11203 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11204 Diagnosed = true;
11205 break;
11206 }
11207 }
11208 (void)Diagnosed;
11209 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11210 }
11211
11212 // Issue ODR failures diagnostics for functions.
11213 for (auto &Merge : FunctionOdrMergeFailures) {
11214 FunctionDecl *FirstFunction = Merge.first;
11215 bool Diagnosed = false;
11216 for (auto &SecondFunction : Merge.second) {
11217 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11218 Diagnosed = true;
11219 break;
11220 }
11221 }
11222 (void)Diagnosed;
11223 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11224 }
11225
11226 // Issue ODR failures diagnostics for enums.
11227 for (auto &Merge : EnumOdrMergeFailures) {
11228 // If we've already pointed out a specific problem with this enum, don't
11229 // bother issuing a general "something's different" diagnostic.
11230 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11231 continue;
11232
11233 EnumDecl *FirstEnum = Merge.first;
11234 bool Diagnosed = false;
11235 for (auto &SecondEnum : Merge.second) {
11236 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11237 Diagnosed = true;
11238 break;
11239 }
11240 }
11241 (void)Diagnosed;
11242 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11243 }
11244
11245 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11246 // If we've already pointed out a specific problem with this interface,
11247 // don't bother issuing a general "something's different" diagnostic.
11248 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11249 continue;
11250
11251 bool Diagnosed = false;
11252 ObjCInterfaceDecl *FirstID = Merge.first;
11253 for (auto &InterfacePair : Merge.second) {
11254 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11255 SecondDD: InterfacePair.second)) {
11256 Diagnosed = true;
11257 break;
11258 }
11259 }
11260 (void)Diagnosed;
11261 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11262 }
11263
11264 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11265 // If we've already pointed out a specific problem with this protocol,
11266 // don't bother issuing a general "something's different" diagnostic.
11267 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11268 continue;
11269
11270 ObjCProtocolDecl *FirstProtocol = Merge.first;
11271 bool Diagnosed = false;
11272 for (auto &ProtocolPair : Merge.second) {
11273 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11274 SecondDD: ProtocolPair.second)) {
11275 Diagnosed = true;
11276 break;
11277 }
11278 }
11279 (void)Diagnosed;
11280 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11281 }
11282}
11283
11284void ASTReader::StartedDeserializing() {
11285 if (llvm::Timer *T = ReadTimer.get();
11286 ++NumCurrentElementsDeserializing == 1 && T)
11287 ReadTimeRegion.emplace(args&: T);
11288}
11289
11290void ASTReader::FinishedDeserializing() {
11291 assert(NumCurrentElementsDeserializing &&
11292 "FinishedDeserializing not paired with StartedDeserializing");
11293 if (NumCurrentElementsDeserializing == 1) {
11294 // We decrease NumCurrentElementsDeserializing only after pending actions
11295 // are finished, to avoid recursively re-calling finishPendingActions().
11296 finishPendingActions();
11297 }
11298 --NumCurrentElementsDeserializing;
11299
11300 if (NumCurrentElementsDeserializing == 0) {
11301 {
11302 // Guard variable to avoid recursively entering the process of passing
11303 // decls to consumer.
11304 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11305 /*NewValue=*/false);
11306
11307 // Propagate exception specification and deduced type updates along
11308 // redeclaration chains.
11309 //
11310 // We do this now rather than in finishPendingActions because we want to
11311 // be able to walk the complete redeclaration chains of the updated decls.
11312 while (!PendingExceptionSpecUpdates.empty() ||
11313 !PendingDeducedTypeUpdates.empty() ||
11314 !PendingUndeducedFunctionDecls.empty()) {
11315 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11316 PendingExceptionSpecUpdates.clear();
11317 for (auto Update : ESUpdates) {
11318 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11319 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11320 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11321 if (auto *Listener = getContext().getASTMutationListener())
11322 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11323 for (auto *Redecl : Update.second->redecls())
11324 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11325 }
11326
11327 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11328 PendingDeducedTypeUpdates.clear();
11329 for (auto Update : DTUpdates) {
11330 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11331 // FIXME: If the return type is already deduced, check that it
11332 // matches.
11333 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11334 ResultType: Update.second);
11335 }
11336
11337 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11338 PendingUndeducedFunctionDecls.clear();
11339 // We hope we can find the deduced type for the functions by iterating
11340 // redeclarations in other modules.
11341 for (FunctionDecl *UndeducedFD : UDTUpdates)
11342 (void)UndeducedFD->getMostRecentDecl();
11343 }
11344
11345 ReadTimeRegion.reset();
11346
11347 diagnoseOdrViolations();
11348 }
11349
11350 // We are not in recursive loading, so it's safe to pass the "interesting"
11351 // decls to the consumer.
11352 if (Consumer)
11353 PassInterestingDeclsToConsumer();
11354 }
11355}
11356
11357void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11358 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11359 // Remove any fake results before adding any real ones.
11360 auto It = PendingFakeLookupResults.find(Key: II);
11361 if (It != PendingFakeLookupResults.end()) {
11362 for (auto *ND : It->second)
11363 SemaObj->IdResolver.RemoveDecl(D: ND);
11364 // FIXME: this works around module+PCH performance issue.
11365 // Rather than erase the result from the map, which is O(n), just clear
11366 // the vector of NamedDecls.
11367 It->second.clear();
11368 }
11369 }
11370
11371 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11372 SemaObj->TUScope->AddDecl(D);
11373 } else if (SemaObj->TUScope) {
11374 // Adding the decl to IdResolver may have failed because it was already in
11375 // (even though it was not added in scope). If it is already in, make sure
11376 // it gets in the scope as well.
11377 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11378 SemaObj->TUScope->AddDecl(D);
11379 }
11380}
11381
11382ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11383 ASTContext *Context,
11384 const PCHContainerReader &PCHContainerRdr,
11385 const CodeGenOptions &CodeGenOpts,
11386 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11387 StringRef isysroot,
11388 DisableValidationForModuleKind DisableValidationKind,
11389 bool AllowASTWithCompilerErrors,
11390 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11391 bool ForceValidateUserInputs,
11392 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11393 std::unique_ptr<llvm::Timer> ReadTimer)
11394 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11395 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11396 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11397 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11398 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11399 StackHandler(Diags), PP(PP), ContextObj(Context),
11400 CodeGenOpts(CodeGenOpts),
11401 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11402 PP.getHeaderSearchInfo()),
11403 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11404 DisableValidationKind(DisableValidationKind),
11405 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11406 AllowConfigurationMismatch(AllowConfigurationMismatch),
11407 ValidateSystemInputs(ValidateSystemInputs),
11408 ForceValidateUserInputs(ForceValidateUserInputs),
11409 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11410 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11411 SourceMgr.setExternalSLocEntrySource(this);
11412
11413 PathBuf.reserve(N: 256);
11414
11415 for (const auto &Ext : Extensions) {
11416 auto BlockName = Ext->getExtensionMetadata().BlockName;
11417 auto Known = ModuleFileExtensions.find(Key: BlockName);
11418 if (Known != ModuleFileExtensions.end()) {
11419 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11420 << BlockName;
11421 continue;
11422 }
11423
11424 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11425 }
11426}
11427
11428ASTReader::~ASTReader() {
11429 if (OwnsDeserializationListener)
11430 delete DeserializationListener;
11431}
11432
11433IdentifierResolver &ASTReader::getIdResolver() {
11434 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11435}
11436
11437Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11438 unsigned AbbrevID) {
11439 Idx = 0;
11440 Record.clear();
11441 return Cursor.readRecord(AbbrevID, Vals&: Record);
11442}
11443//===----------------------------------------------------------------------===//
11444//// OMPClauseReader implementation
11445////===----------------------------------------------------------------------===//
11446
11447// This has to be in namespace clang because it's friended by all
11448// of the OMP clauses.
11449namespace clang {
11450
11451class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11452 ASTRecordReader &Record;
11453 ASTContext &Context;
11454
11455public:
11456 OMPClauseReader(ASTRecordReader &Record)
11457 : Record(Record), Context(Record.getContext()) {}
11458#define GEN_CLANG_CLAUSE_CLASS
11459#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11460#include "llvm/Frontend/OpenMP/OMP.inc"
11461 OMPClause *readClause();
11462 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11463 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11464};
11465
11466} // end namespace clang
11467
11468OMPClause *ASTRecordReader::readOMPClause() {
11469 return OMPClauseReader(*this).readClause();
11470}
11471
11472OMPClause *OMPClauseReader::readClause() {
11473 OMPClause *C = nullptr;
11474 switch (llvm::omp::Clause(Record.readInt())) {
11475 case llvm::omp::OMPC_if:
11476 C = new (Context) OMPIfClause();
11477 break;
11478 case llvm::omp::OMPC_final:
11479 C = new (Context) OMPFinalClause();
11480 break;
11481 case llvm::omp::OMPC_num_threads:
11482 C = OMPNumThreadsClause::CreateEmpty(C: Context, N: Record.readInt());
11483 break;
11484 case llvm::omp::OMPC_safelen:
11485 C = new (Context) OMPSafelenClause();
11486 break;
11487 case llvm::omp::OMPC_simdlen:
11488 C = new (Context) OMPSimdlenClause();
11489 break;
11490 case llvm::omp::OMPC_sizes: {
11491 unsigned NumSizes = Record.readInt();
11492 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11493 break;
11494 }
11495 case llvm::omp::OMPC_counts: {
11496 unsigned NumCounts = Record.readInt();
11497 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11498 break;
11499 }
11500 case llvm::omp::OMPC_permutation: {
11501 unsigned NumLoops = Record.readInt();
11502 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11503 break;
11504 }
11505 case llvm::omp::OMPC_full:
11506 C = OMPFullClause::CreateEmpty(C: Context);
11507 break;
11508 case llvm::omp::OMPC_partial:
11509 C = OMPPartialClause::CreateEmpty(C: Context);
11510 break;
11511 case llvm::omp::OMPC_looprange:
11512 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11513 break;
11514 case llvm::omp::OMPC_allocator:
11515 C = new (Context) OMPAllocatorClause();
11516 break;
11517 case llvm::omp::OMPC_collapse:
11518 C = new (Context) OMPCollapseClause();
11519 break;
11520 case llvm::omp::OMPC_default:
11521 C = new (Context) OMPDefaultClause();
11522 break;
11523 case llvm::omp::OMPC_proc_bind:
11524 C = new (Context) OMPProcBindClause();
11525 break;
11526 case llvm::omp::OMPC_schedule:
11527 C = new (Context) OMPScheduleClause();
11528 break;
11529 case llvm::omp::OMPC_ordered:
11530 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11531 break;
11532 case llvm::omp::OMPC_nowait:
11533 C = new (Context) OMPNowaitClause();
11534 break;
11535 case llvm::omp::OMPC_untied:
11536 C = new (Context) OMPUntiedClause();
11537 break;
11538 case llvm::omp::OMPC_mergeable:
11539 C = new (Context) OMPMergeableClause();
11540 break;
11541 case llvm::omp::OMPC_threadset:
11542 C = new (Context) OMPThreadsetClause();
11543 break;
11544 case llvm::omp::OMPC_transparent:
11545 C = new (Context) OMPTransparentClause();
11546 break;
11547 case llvm::omp::OMPC_read:
11548 C = new (Context) OMPReadClause();
11549 break;
11550 case llvm::omp::OMPC_write:
11551 C = new (Context) OMPWriteClause();
11552 break;
11553 case llvm::omp::OMPC_update:
11554 C = new (Context) OMPUpdateClause();
11555 break;
11556 case llvm::omp::OMPC_update_depend_objects:
11557 C = OMPUpdateDependObjectsClause::CreateEmpty(C: Context);
11558 break;
11559 case llvm::omp::OMPC_capture:
11560 C = new (Context) OMPCaptureClause();
11561 break;
11562 case llvm::omp::OMPC_compare:
11563 C = new (Context) OMPCompareClause();
11564 break;
11565 case llvm::omp::OMPC_fail:
11566 C = new (Context) OMPFailClause();
11567 break;
11568 case llvm::omp::OMPC_seq_cst:
11569 C = new (Context) OMPSeqCstClause();
11570 break;
11571 case llvm::omp::OMPC_acq_rel:
11572 C = new (Context) OMPAcqRelClause();
11573 break;
11574 case llvm::omp::OMPC_absent: {
11575 unsigned NumKinds = Record.readInt();
11576 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11577 break;
11578 }
11579 case llvm::omp::OMPC_holds:
11580 C = new (Context) OMPHoldsClause();
11581 break;
11582 case llvm::omp::OMPC_contains: {
11583 unsigned NumKinds = Record.readInt();
11584 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11585 break;
11586 }
11587 case llvm::omp::OMPC_no_openmp:
11588 C = new (Context) OMPNoOpenMPClause();
11589 break;
11590 case llvm::omp::OMPC_no_openmp_routines:
11591 C = new (Context) OMPNoOpenMPRoutinesClause();
11592 break;
11593 case llvm::omp::OMPC_no_openmp_constructs:
11594 C = new (Context) OMPNoOpenMPConstructsClause();
11595 break;
11596 case llvm::omp::OMPC_no_parallelism:
11597 C = new (Context) OMPNoParallelismClause();
11598 break;
11599 case llvm::omp::OMPC_acquire:
11600 C = new (Context) OMPAcquireClause();
11601 break;
11602 case llvm::omp::OMPC_release:
11603 C = new (Context) OMPReleaseClause();
11604 break;
11605 case llvm::omp::OMPC_relaxed:
11606 C = new (Context) OMPRelaxedClause();
11607 break;
11608 case llvm::omp::OMPC_weak:
11609 C = new (Context) OMPWeakClause();
11610 break;
11611 case llvm::omp::OMPC_threads:
11612 C = new (Context) OMPThreadsClause();
11613 break;
11614 case llvm::omp::OMPC_simd:
11615 C = new (Context) OMPSIMDClause();
11616 break;
11617 case llvm::omp::OMPC_nogroup:
11618 C = new (Context) OMPNogroupClause();
11619 break;
11620 case llvm::omp::OMPC_unified_address:
11621 C = new (Context) OMPUnifiedAddressClause();
11622 break;
11623 case llvm::omp::OMPC_unified_shared_memory:
11624 C = new (Context) OMPUnifiedSharedMemoryClause();
11625 break;
11626 case llvm::omp::OMPC_reverse_offload:
11627 C = new (Context) OMPReverseOffloadClause();
11628 break;
11629 case llvm::omp::OMPC_dynamic_allocators:
11630 C = new (Context) OMPDynamicAllocatorsClause();
11631 break;
11632 case llvm::omp::OMPC_atomic_default_mem_order:
11633 C = new (Context) OMPAtomicDefaultMemOrderClause();
11634 break;
11635 case llvm::omp::OMPC_self_maps:
11636 C = new (Context) OMPSelfMapsClause();
11637 break;
11638 case llvm::omp::OMPC_at:
11639 C = new (Context) OMPAtClause();
11640 break;
11641 case llvm::omp::OMPC_severity:
11642 C = new (Context) OMPSeverityClause();
11643 break;
11644 case llvm::omp::OMPC_message:
11645 C = new (Context) OMPMessageClause();
11646 break;
11647 case llvm::omp::OMPC_private:
11648 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11649 break;
11650 case llvm::omp::OMPC_firstprivate:
11651 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11652 break;
11653 case llvm::omp::OMPC_lastprivate:
11654 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11655 break;
11656 case llvm::omp::OMPC_shared:
11657 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11658 break;
11659 case llvm::omp::OMPC_reduction: {
11660 unsigned N = Record.readInt();
11661 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11662 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11663 break;
11664 }
11665 case llvm::omp::OMPC_task_reduction:
11666 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11667 break;
11668 case llvm::omp::OMPC_in_reduction:
11669 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11670 break;
11671 case llvm::omp::OMPC_linear:
11672 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11673 break;
11674 case llvm::omp::OMPC_aligned:
11675 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11676 break;
11677 case llvm::omp::OMPC_copyin:
11678 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11679 break;
11680 case llvm::omp::OMPC_copyprivate:
11681 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11682 break;
11683 case llvm::omp::OMPC_flush:
11684 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11685 break;
11686 case llvm::omp::OMPC_depobj:
11687 C = OMPDepobjClause::CreateEmpty(C: Context);
11688 break;
11689 case llvm::omp::OMPC_depend: {
11690 unsigned NumVars = Record.readInt();
11691 unsigned NumLoops = Record.readInt();
11692 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11693 break;
11694 }
11695 case llvm::omp::OMPC_device:
11696 C = new (Context) OMPDeviceClause();
11697 break;
11698 case llvm::omp::OMPC_map: {
11699 OMPMappableExprListSizeTy Sizes;
11700 Sizes.NumVars = Record.readInt();
11701 Sizes.NumUniqueDeclarations = Record.readInt();
11702 Sizes.NumComponentLists = Record.readInt();
11703 Sizes.NumComponents = Record.readInt();
11704 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11705 break;
11706 }
11707 case llvm::omp::OMPC_num_teams:
11708 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11709 break;
11710 case llvm::omp::OMPC_thread_limit:
11711 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11712 break;
11713 case llvm::omp::OMPC_priority:
11714 C = new (Context) OMPPriorityClause();
11715 break;
11716 case llvm::omp::OMPC_grainsize:
11717 C = new (Context) OMPGrainsizeClause();
11718 break;
11719 case llvm::omp::OMPC_num_tasks:
11720 C = new (Context) OMPNumTasksClause();
11721 break;
11722 case llvm::omp::OMPC_hint:
11723 C = new (Context) OMPHintClause();
11724 break;
11725 case llvm::omp::OMPC_dist_schedule:
11726 C = new (Context) OMPDistScheduleClause();
11727 break;
11728 case llvm::omp::OMPC_defaultmap:
11729 C = new (Context) OMPDefaultmapClause();
11730 break;
11731 case llvm::omp::OMPC_to: {
11732 OMPMappableExprListSizeTy Sizes;
11733 Sizes.NumVars = Record.readInt();
11734 Sizes.NumUniqueDeclarations = Record.readInt();
11735 Sizes.NumComponentLists = Record.readInt();
11736 Sizes.NumComponents = Record.readInt();
11737 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11738 break;
11739 }
11740 case llvm::omp::OMPC_from: {
11741 OMPMappableExprListSizeTy Sizes;
11742 Sizes.NumVars = Record.readInt();
11743 Sizes.NumUniqueDeclarations = Record.readInt();
11744 Sizes.NumComponentLists = Record.readInt();
11745 Sizes.NumComponents = Record.readInt();
11746 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11747 break;
11748 }
11749 case llvm::omp::OMPC_use_device_ptr: {
11750 OMPMappableExprListSizeTy Sizes;
11751 Sizes.NumVars = Record.readInt();
11752 Sizes.NumUniqueDeclarations = Record.readInt();
11753 Sizes.NumComponentLists = Record.readInt();
11754 Sizes.NumComponents = Record.readInt();
11755 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11756 break;
11757 }
11758 case llvm::omp::OMPC_use_device_addr: {
11759 OMPMappableExprListSizeTy Sizes;
11760 Sizes.NumVars = Record.readInt();
11761 Sizes.NumUniqueDeclarations = Record.readInt();
11762 Sizes.NumComponentLists = Record.readInt();
11763 Sizes.NumComponents = Record.readInt();
11764 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11765 break;
11766 }
11767 case llvm::omp::OMPC_is_device_ptr: {
11768 OMPMappableExprListSizeTy Sizes;
11769 Sizes.NumVars = Record.readInt();
11770 Sizes.NumUniqueDeclarations = Record.readInt();
11771 Sizes.NumComponentLists = Record.readInt();
11772 Sizes.NumComponents = Record.readInt();
11773 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11774 break;
11775 }
11776 case llvm::omp::OMPC_has_device_addr: {
11777 OMPMappableExprListSizeTy Sizes;
11778 Sizes.NumVars = Record.readInt();
11779 Sizes.NumUniqueDeclarations = Record.readInt();
11780 Sizes.NumComponentLists = Record.readInt();
11781 Sizes.NumComponents = Record.readInt();
11782 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11783 break;
11784 }
11785 case llvm::omp::OMPC_allocate:
11786 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11787 break;
11788 case llvm::omp::OMPC_nontemporal:
11789 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11790 break;
11791 case llvm::omp::OMPC_inclusive:
11792 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11793 break;
11794 case llvm::omp::OMPC_exclusive:
11795 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11796 break;
11797 case llvm::omp::OMPC_order:
11798 C = new (Context) OMPOrderClause();
11799 break;
11800 case llvm::omp::OMPC_init: {
11801 unsigned VarListSize = Record.readInt();
11802 unsigned NumAttrs = Record.readInt();
11803 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11804 NumAttrs);
11805 break;
11806 }
11807 case llvm::omp::OMPC_use:
11808 C = new (Context) OMPUseClause();
11809 break;
11810 case llvm::omp::OMPC_destroy:
11811 C = new (Context) OMPDestroyClause();
11812 break;
11813 case llvm::omp::OMPC_novariants:
11814 C = new (Context) OMPNovariantsClause();
11815 break;
11816 case llvm::omp::OMPC_nocontext:
11817 C = new (Context) OMPNocontextClause();
11818 break;
11819 case llvm::omp::OMPC_detach:
11820 C = new (Context) OMPDetachClause();
11821 break;
11822 case llvm::omp::OMPC_uses_allocators:
11823 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11824 break;
11825 case llvm::omp::OMPC_affinity:
11826 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11827 break;
11828 case llvm::omp::OMPC_filter:
11829 C = new (Context) OMPFilterClause();
11830 break;
11831 case llvm::omp::OMPC_bind:
11832 C = OMPBindClause::CreateEmpty(C: Context);
11833 break;
11834 case llvm::omp::OMPC_align:
11835 C = new (Context) OMPAlignClause();
11836 break;
11837 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11838 C = new (Context) OMPXDynCGroupMemClause();
11839 break;
11840 case llvm::omp::OMPC_dyn_groupprivate:
11841 C = new (Context) OMPDynGroupprivateClause();
11842 break;
11843 case llvm::omp::OMPC_doacross: {
11844 unsigned NumVars = Record.readInt();
11845 unsigned NumLoops = Record.readInt();
11846 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11847 break;
11848 }
11849 case llvm::omp::OMPC_ompx_attribute:
11850 C = new (Context) OMPXAttributeClause();
11851 break;
11852 case llvm::omp::OMPC_ompx_bare:
11853 C = new (Context) OMPXBareClause();
11854 break;
11855#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11856 case llvm::omp::Enum: \
11857 break;
11858#include "llvm/Frontend/OpenMP/OMPKinds.def"
11859 default:
11860 break;
11861 }
11862 assert(C && "Unknown OMPClause type");
11863
11864 Visit(S: C);
11865 C->setLocStart(Record.readSourceLocation());
11866 C->setLocEnd(Record.readSourceLocation());
11867
11868 return C;
11869}
11870
11871void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11872 C->setPreInitStmt(S: Record.readSubStmt(),
11873 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11874}
11875
11876void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11877 VisitOMPClauseWithPreInit(C);
11878 C->setPostUpdateExpr(Record.readSubExpr());
11879}
11880
11881void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11882 VisitOMPClauseWithPreInit(C);
11883 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11884 C->setNameModifierLoc(Record.readSourceLocation());
11885 C->setColonLoc(Record.readSourceLocation());
11886 C->setCondition(Record.readSubExpr());
11887 C->setLParenLoc(Record.readSourceLocation());
11888}
11889
11890void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11891 VisitOMPClauseWithPreInit(C);
11892 C->setCondition(Record.readSubExpr());
11893 C->setLParenLoc(Record.readSourceLocation());
11894}
11895
11896void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11897 C->setPrescriptivenessModifier(
11898 Record.readEnum<OpenMPNumThreadsClauseModifier>());
11899 C->setPrescriptivenessModifierLoc(Record.readSourceLocation());
11900 C->setDimsModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11901 C->setDimsModifierLoc(Record.readSourceLocation());
11902 C->setDimsModifierExpr(Record.readSubExpr());
11903 VisitOMPClauseWithPreInit(C);
11904 C->setLParenLoc(Record.readSourceLocation());
11905 unsigned NumVars = C->varlist_size();
11906 SmallVector<Expr *, 16> Vars;
11907 Vars.reserve(N: NumVars);
11908 for (unsigned I = 0; I != NumVars; ++I)
11909 Vars.push_back(Elt: Record.readSubExpr());
11910 C->setVarRefs(Vars);
11911}
11912
11913void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11914 C->setSafelen(Record.readSubExpr());
11915 C->setLParenLoc(Record.readSourceLocation());
11916}
11917
11918void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11919 C->setSimdlen(Record.readSubExpr());
11920 C->setLParenLoc(Record.readSourceLocation());
11921}
11922
11923void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11924 for (Expr *&E : C->getSizesRefs())
11925 E = Record.readSubExpr();
11926 C->setLParenLoc(Record.readSourceLocation());
11927}
11928
11929void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
11930 bool HasFill = Record.readBool();
11931 if (HasFill)
11932 C->setOmpFillIndex(Record.readInt());
11933 C->setOmpFillLoc(Record.readSourceLocation());
11934 for (Expr *&E : C->getCountsRefs())
11935 E = Record.readSubExpr();
11936 C->setLParenLoc(Record.readSourceLocation());
11937}
11938
11939void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11940 for (Expr *&E : C->getArgsRefs())
11941 E = Record.readSubExpr();
11942 C->setLParenLoc(Record.readSourceLocation());
11943}
11944
11945void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11946
11947void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11948 C->setFactor(Record.readSubExpr());
11949 C->setLParenLoc(Record.readSourceLocation());
11950}
11951
11952void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11953 C->setFirst(Record.readSubExpr());
11954 C->setCount(Record.readSubExpr());
11955 C->setLParenLoc(Record.readSourceLocation());
11956 C->setFirstLoc(Record.readSourceLocation());
11957 C->setCountLoc(Record.readSourceLocation());
11958}
11959
11960void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11961 C->setAllocator(Record.readExpr());
11962 C->setLParenLoc(Record.readSourceLocation());
11963}
11964
11965void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11966 C->setNumForLoops(Record.readSubExpr());
11967 C->setLParenLoc(Record.readSourceLocation());
11968}
11969
11970void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11971 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11972 C->setLParenLoc(Record.readSourceLocation());
11973 C->setDefaultKindKwLoc(Record.readSourceLocation());
11974 C->setDefaultVariableCategory(
11975 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11976 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11977}
11978
11979// Read the parameter of threadset clause. This will have been saved when
11980// OMPClauseWriter is called.
11981void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11982 C->setLParenLoc(Record.readSourceLocation());
11983 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11984 C->setThreadsetKindLoc(ThreadsetKindLoc);
11985 OpenMPThreadsetKind TKind =
11986 static_cast<OpenMPThreadsetKind>(Record.readInt());
11987 C->setThreadsetKind(TKind);
11988}
11989
11990void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11991 C->setLParenLoc(Record.readSourceLocation());
11992 C->setImpexTypeKind(Record.readSubExpr());
11993}
11994
11995void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11996 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11997 C->setLParenLoc(Record.readSourceLocation());
11998 C->setProcBindKindKwLoc(Record.readSourceLocation());
11999}
12000
12001void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12002 VisitOMPClauseWithPreInit(C);
12003 C->setScheduleKind(
12004 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12005 C->setFirstScheduleModifier(
12006 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12007 C->setSecondScheduleModifier(
12008 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12009 C->setChunkSize(Record.readSubExpr());
12010 C->setLParenLoc(Record.readSourceLocation());
12011 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12012 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12013 C->setScheduleKindLoc(Record.readSourceLocation());
12014 C->setCommaLoc(Record.readSourceLocation());
12015}
12016
12017void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12018 C->setNumForLoops(Record.readSubExpr());
12019 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12020 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
12021 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12022 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
12023 C->setLParenLoc(Record.readSourceLocation());
12024}
12025
12026void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12027 C->setEventHandler(Record.readSubExpr());
12028 C->setLParenLoc(Record.readSourceLocation());
12029}
12030
12031void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12032 C->setCondition(Record.readSubExpr());
12033 C->setLParenLoc(Record.readSourceLocation());
12034}
12035
12036void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12037
12038void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12039
12040void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12041
12042void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12043
12044void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {}
12045
12046void OMPClauseReader::VisitOMPUpdateDependObjectsClause(
12047 OMPUpdateDependObjectsClause *C) {
12048 C->setLParenLoc(Record.readSourceLocation());
12049 C->setArgumentLoc(Record.readSourceLocation());
12050 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12051}
12052
12053void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12054
12055void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12056
12057// Read the parameter of fail clause. This will have been saved when
12058// OMPClauseWriter is called.
12059void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12060 C->setLParenLoc(Record.readSourceLocation());
12061 SourceLocation FailParameterLoc = Record.readSourceLocation();
12062 C->setFailParameterLoc(FailParameterLoc);
12063 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12064 C->setFailParameter(CKind);
12065}
12066
12067void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12068 unsigned Count = C->getDirectiveKinds().size();
12069 C->setLParenLoc(Record.readSourceLocation());
12070 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12071 DKVec.reserve(N: Count);
12072 for (unsigned I = 0; I < Count; I++) {
12073 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12074 }
12075 C->setDirectiveKinds(DKVec);
12076}
12077
12078void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12079 C->setExpr(Record.readExpr());
12080 C->setLParenLoc(Record.readSourceLocation());
12081}
12082
12083void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12084 unsigned Count = C->getDirectiveKinds().size();
12085 C->setLParenLoc(Record.readSourceLocation());
12086 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12087 DKVec.reserve(N: Count);
12088 for (unsigned I = 0; I < Count; I++) {
12089 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12090 }
12091 C->setDirectiveKinds(DKVec);
12092}
12093
12094void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12095
12096void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12097 OMPNoOpenMPRoutinesClause *) {}
12098
12099void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12100 OMPNoOpenMPConstructsClause *) {}
12101
12102void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12103
12104void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12105
12106void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12107
12108void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12109
12110void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12111
12112void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12113
12114void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12115
12116void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12117
12118void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12119
12120void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12121
12122void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12123 unsigned NumVars = C->varlist_size();
12124 SmallVector<Expr *, 16> Vars;
12125 Vars.reserve(N: NumVars);
12126 for (unsigned I = 0; I != NumVars; ++I)
12127 Vars.push_back(Elt: Record.readSubExpr());
12128 C->setVarRefs(Vars);
12129 C->setIsTarget(Record.readBool());
12130 C->setIsTargetSync(Record.readBool());
12131 C->setHasPreferAttrs(Record.readBool());
12132
12133 unsigned NumPrefs = C->varlist_size() - 1;
12134 SmallVector<unsigned, 4> Counts;
12135 SmallVector<Expr *, 8> Attrs;
12136 Counts.reserve(N: NumPrefs);
12137 for (unsigned I = 0; I < NumPrefs; ++I) {
12138 unsigned NA = Record.readInt();
12139 Counts.push_back(Elt: NA);
12140 for (unsigned J = 0; J < NA; ++J)
12141 Attrs.push_back(Elt: Record.readSubExpr());
12142 }
12143 C->setAttrs(Counts, Attrs);
12144
12145 C->setLParenLoc(Record.readSourceLocation());
12146 C->setVarLoc(Record.readSourceLocation());
12147}
12148
12149void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12150 C->setInteropVar(Record.readSubExpr());
12151 C->setLParenLoc(Record.readSourceLocation());
12152 C->setVarLoc(Record.readSourceLocation());
12153}
12154
12155void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12156 C->setInteropVar(Record.readSubExpr());
12157 C->setLParenLoc(Record.readSourceLocation());
12158 C->setVarLoc(Record.readSourceLocation());
12159}
12160
12161void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12162 VisitOMPClauseWithPreInit(C);
12163 C->setCondition(Record.readSubExpr());
12164 C->setLParenLoc(Record.readSourceLocation());
12165}
12166
12167void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12168 VisitOMPClauseWithPreInit(C);
12169 C->setCondition(Record.readSubExpr());
12170 C->setLParenLoc(Record.readSourceLocation());
12171}
12172
12173void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12174
12175void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12176 OMPUnifiedSharedMemoryClause *) {}
12177
12178void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12179
12180void
12181OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12182}
12183
12184void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12185 OMPAtomicDefaultMemOrderClause *C) {
12186 C->setAtomicDefaultMemOrderKind(
12187 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12188 C->setLParenLoc(Record.readSourceLocation());
12189 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12190}
12191
12192void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12193
12194void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12195 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12196 C->setLParenLoc(Record.readSourceLocation());
12197 C->setAtKindKwLoc(Record.readSourceLocation());
12198}
12199
12200void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12201 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12202 C->setLParenLoc(Record.readSourceLocation());
12203 C->setSeverityKindKwLoc(Record.readSourceLocation());
12204}
12205
12206void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12207 VisitOMPClauseWithPreInit(C);
12208 C->setMessageString(Record.readSubExpr());
12209 C->setLParenLoc(Record.readSourceLocation());
12210}
12211
12212void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12213 C->setLParenLoc(Record.readSourceLocation());
12214 unsigned NumVars = C->varlist_size();
12215 SmallVector<Expr *, 16> Vars;
12216 Vars.reserve(N: NumVars);
12217 for (unsigned i = 0; i != NumVars; ++i)
12218 Vars.push_back(Elt: Record.readSubExpr());
12219 C->setVarRefs(Vars);
12220 Vars.clear();
12221 for (unsigned i = 0; i != NumVars; ++i)
12222 Vars.push_back(Elt: Record.readSubExpr());
12223 C->setPrivateCopies(Vars);
12224}
12225
12226void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12227 VisitOMPClauseWithPreInit(C);
12228 C->setLParenLoc(Record.readSourceLocation());
12229 unsigned NumVars = C->varlist_size();
12230 SmallVector<Expr *, 16> Vars;
12231 Vars.reserve(N: NumVars);
12232 for (unsigned i = 0; i != NumVars; ++i)
12233 Vars.push_back(Elt: Record.readSubExpr());
12234 C->setVarRefs(Vars);
12235 Vars.clear();
12236 for (unsigned i = 0; i != NumVars; ++i)
12237 Vars.push_back(Elt: Record.readSubExpr());
12238 C->setPrivateCopies(Vars);
12239 Vars.clear();
12240 for (unsigned i = 0; i != NumVars; ++i)
12241 Vars.push_back(Elt: Record.readSubExpr());
12242 C->setInits(Vars);
12243}
12244
12245void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12246 VisitOMPClauseWithPostUpdate(C);
12247 C->setLParenLoc(Record.readSourceLocation());
12248 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12249 C->setKindLoc(Record.readSourceLocation());
12250 C->setColonLoc(Record.readSourceLocation());
12251 unsigned NumVars = C->varlist_size();
12252 SmallVector<Expr *, 16> Vars;
12253 Vars.reserve(N: NumVars);
12254 for (unsigned i = 0; i != NumVars; ++i)
12255 Vars.push_back(Elt: Record.readSubExpr());
12256 C->setVarRefs(Vars);
12257 Vars.clear();
12258 for (unsigned i = 0; i != NumVars; ++i)
12259 Vars.push_back(Elt: Record.readSubExpr());
12260 C->setPrivateCopies(Vars);
12261 Vars.clear();
12262 for (unsigned i = 0; i != NumVars; ++i)
12263 Vars.push_back(Elt: Record.readSubExpr());
12264 C->setSourceExprs(Vars);
12265 Vars.clear();
12266 for (unsigned i = 0; i != NumVars; ++i)
12267 Vars.push_back(Elt: Record.readSubExpr());
12268 C->setDestinationExprs(Vars);
12269 Vars.clear();
12270 for (unsigned i = 0; i != NumVars; ++i)
12271 Vars.push_back(Elt: Record.readSubExpr());
12272 C->setAssignmentOps(Vars);
12273}
12274
12275void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12276 C->setLParenLoc(Record.readSourceLocation());
12277 unsigned NumVars = C->varlist_size();
12278 SmallVector<Expr *, 16> Vars;
12279 Vars.reserve(N: NumVars);
12280 for (unsigned i = 0; i != NumVars; ++i)
12281 Vars.push_back(Elt: Record.readSubExpr());
12282 C->setVarRefs(Vars);
12283}
12284
12285void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12286 VisitOMPClauseWithPostUpdate(C);
12287 C->setLParenLoc(Record.readSourceLocation());
12288 C->setModifierLoc(Record.readSourceLocation());
12289 C->setColonLoc(Record.readSourceLocation());
12290 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12291 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12292 C->setQualifierLoc(NNSL);
12293 C->setNameInfo(DNI);
12294
12295 unsigned NumVars = C->varlist_size();
12296 SmallVector<Expr *, 16> Vars;
12297 Vars.reserve(N: NumVars);
12298 for (unsigned i = 0; i != NumVars; ++i)
12299 Vars.push_back(Elt: Record.readSubExpr());
12300 C->setVarRefs(Vars);
12301 Vars.clear();
12302 for (unsigned i = 0; i != NumVars; ++i)
12303 Vars.push_back(Elt: Record.readSubExpr());
12304 C->setPrivates(Vars);
12305 Vars.clear();
12306 for (unsigned i = 0; i != NumVars; ++i)
12307 Vars.push_back(Elt: Record.readSubExpr());
12308 C->setLHSExprs(Vars);
12309 Vars.clear();
12310 for (unsigned i = 0; i != NumVars; ++i)
12311 Vars.push_back(Elt: Record.readSubExpr());
12312 C->setRHSExprs(Vars);
12313 Vars.clear();
12314 for (unsigned i = 0; i != NumVars; ++i)
12315 Vars.push_back(Elt: Record.readSubExpr());
12316 C->setReductionOps(Vars);
12317 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12318 Vars.clear();
12319 for (unsigned i = 0; i != NumVars; ++i)
12320 Vars.push_back(Elt: Record.readSubExpr());
12321 C->setInscanCopyOps(Vars);
12322 Vars.clear();
12323 for (unsigned i = 0; i != NumVars; ++i)
12324 Vars.push_back(Elt: Record.readSubExpr());
12325 C->setInscanCopyArrayTemps(Vars);
12326 Vars.clear();
12327 for (unsigned i = 0; i != NumVars; ++i)
12328 Vars.push_back(Elt: Record.readSubExpr());
12329 C->setInscanCopyArrayElems(Vars);
12330 }
12331 unsigned NumFlags = Record.readInt();
12332 SmallVector<bool, 16> Flags;
12333 Flags.reserve(N: NumFlags);
12334 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12335 Flags.push_back(Elt: Record.readInt());
12336 C->setPrivateVariableReductionFlags(Flags);
12337}
12338
12339void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12340 VisitOMPClauseWithPostUpdate(C);
12341 C->setLParenLoc(Record.readSourceLocation());
12342 C->setColonLoc(Record.readSourceLocation());
12343 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12344 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12345 C->setQualifierLoc(NNSL);
12346 C->setNameInfo(DNI);
12347
12348 unsigned NumVars = C->varlist_size();
12349 SmallVector<Expr *, 16> Vars;
12350 Vars.reserve(N: NumVars);
12351 for (unsigned I = 0; I != NumVars; ++I)
12352 Vars.push_back(Elt: Record.readSubExpr());
12353 C->setVarRefs(Vars);
12354 Vars.clear();
12355 for (unsigned I = 0; I != NumVars; ++I)
12356 Vars.push_back(Elt: Record.readSubExpr());
12357 C->setPrivates(Vars);
12358 Vars.clear();
12359 for (unsigned I = 0; I != NumVars; ++I)
12360 Vars.push_back(Elt: Record.readSubExpr());
12361 C->setLHSExprs(Vars);
12362 Vars.clear();
12363 for (unsigned I = 0; I != NumVars; ++I)
12364 Vars.push_back(Elt: Record.readSubExpr());
12365 C->setRHSExprs(Vars);
12366 Vars.clear();
12367 for (unsigned I = 0; I != NumVars; ++I)
12368 Vars.push_back(Elt: Record.readSubExpr());
12369 C->setReductionOps(Vars);
12370}
12371
12372void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12373 VisitOMPClauseWithPostUpdate(C);
12374 C->setLParenLoc(Record.readSourceLocation());
12375 C->setColonLoc(Record.readSourceLocation());
12376 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12377 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12378 C->setQualifierLoc(NNSL);
12379 C->setNameInfo(DNI);
12380
12381 unsigned NumVars = C->varlist_size();
12382 SmallVector<Expr *, 16> Vars;
12383 Vars.reserve(N: NumVars);
12384 for (unsigned I = 0; I != NumVars; ++I)
12385 Vars.push_back(Elt: Record.readSubExpr());
12386 C->setVarRefs(Vars);
12387 Vars.clear();
12388 for (unsigned I = 0; I != NumVars; ++I)
12389 Vars.push_back(Elt: Record.readSubExpr());
12390 C->setPrivates(Vars);
12391 Vars.clear();
12392 for (unsigned I = 0; I != NumVars; ++I)
12393 Vars.push_back(Elt: Record.readSubExpr());
12394 C->setLHSExprs(Vars);
12395 Vars.clear();
12396 for (unsigned I = 0; I != NumVars; ++I)
12397 Vars.push_back(Elt: Record.readSubExpr());
12398 C->setRHSExprs(Vars);
12399 Vars.clear();
12400 for (unsigned I = 0; I != NumVars; ++I)
12401 Vars.push_back(Elt: Record.readSubExpr());
12402 C->setReductionOps(Vars);
12403 Vars.clear();
12404 for (unsigned I = 0; I != NumVars; ++I)
12405 Vars.push_back(Elt: Record.readSubExpr());
12406 C->setTaskgroupDescriptors(Vars);
12407}
12408
12409void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12410 VisitOMPClauseWithPostUpdate(C);
12411 C->setLParenLoc(Record.readSourceLocation());
12412 C->setColonLoc(Record.readSourceLocation());
12413 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12414 C->setModifierLoc(Record.readSourceLocation());
12415 unsigned NumVars = C->varlist_size();
12416 SmallVector<Expr *, 16> Vars;
12417 Vars.reserve(N: NumVars);
12418 for (unsigned i = 0; i != NumVars; ++i)
12419 Vars.push_back(Elt: Record.readSubExpr());
12420 C->setVarRefs(Vars);
12421 Vars.clear();
12422 for (unsigned i = 0; i != NumVars; ++i)
12423 Vars.push_back(Elt: Record.readSubExpr());
12424 C->setPrivates(Vars);
12425 Vars.clear();
12426 for (unsigned i = 0; i != NumVars; ++i)
12427 Vars.push_back(Elt: Record.readSubExpr());
12428 C->setInits(Vars);
12429 Vars.clear();
12430 for (unsigned i = 0; i != NumVars; ++i)
12431 Vars.push_back(Elt: Record.readSubExpr());
12432 C->setUpdates(Vars);
12433 Vars.clear();
12434 for (unsigned i = 0; i != NumVars; ++i)
12435 Vars.push_back(Elt: Record.readSubExpr());
12436 C->setFinals(Vars);
12437 C->setStep(Record.readSubExpr());
12438 C->setCalcStep(Record.readSubExpr());
12439 Vars.clear();
12440 for (unsigned I = 0; I != NumVars + 1; ++I)
12441 Vars.push_back(Elt: Record.readSubExpr());
12442 C->setUsedExprs(Vars);
12443}
12444
12445void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12446 C->setLParenLoc(Record.readSourceLocation());
12447 C->setColonLoc(Record.readSourceLocation());
12448 unsigned NumVars = C->varlist_size();
12449 SmallVector<Expr *, 16> Vars;
12450 Vars.reserve(N: NumVars);
12451 for (unsigned i = 0; i != NumVars; ++i)
12452 Vars.push_back(Elt: Record.readSubExpr());
12453 C->setVarRefs(Vars);
12454 C->setAlignment(Record.readSubExpr());
12455}
12456
12457void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12458 C->setLParenLoc(Record.readSourceLocation());
12459 unsigned NumVars = C->varlist_size();
12460 SmallVector<Expr *, 16> Exprs;
12461 Exprs.reserve(N: NumVars);
12462 for (unsigned i = 0; i != NumVars; ++i)
12463 Exprs.push_back(Elt: Record.readSubExpr());
12464 C->setVarRefs(Exprs);
12465 Exprs.clear();
12466 for (unsigned i = 0; i != NumVars; ++i)
12467 Exprs.push_back(Elt: Record.readSubExpr());
12468 C->setSourceExprs(Exprs);
12469 Exprs.clear();
12470 for (unsigned i = 0; i != NumVars; ++i)
12471 Exprs.push_back(Elt: Record.readSubExpr());
12472 C->setDestinationExprs(Exprs);
12473 Exprs.clear();
12474 for (unsigned i = 0; i != NumVars; ++i)
12475 Exprs.push_back(Elt: Record.readSubExpr());
12476 C->setAssignmentOps(Exprs);
12477}
12478
12479void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12480 C->setLParenLoc(Record.readSourceLocation());
12481 unsigned NumVars = C->varlist_size();
12482 SmallVector<Expr *, 16> Exprs;
12483 Exprs.reserve(N: NumVars);
12484 for (unsigned i = 0; i != NumVars; ++i)
12485 Exprs.push_back(Elt: Record.readSubExpr());
12486 C->setVarRefs(Exprs);
12487 Exprs.clear();
12488 for (unsigned i = 0; i != NumVars; ++i)
12489 Exprs.push_back(Elt: Record.readSubExpr());
12490 C->setSourceExprs(Exprs);
12491 Exprs.clear();
12492 for (unsigned i = 0; i != NumVars; ++i)
12493 Exprs.push_back(Elt: Record.readSubExpr());
12494 C->setDestinationExprs(Exprs);
12495 Exprs.clear();
12496 for (unsigned i = 0; i != NumVars; ++i)
12497 Exprs.push_back(Elt: Record.readSubExpr());
12498 C->setAssignmentOps(Exprs);
12499}
12500
12501void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12502 C->setLParenLoc(Record.readSourceLocation());
12503 unsigned NumVars = C->varlist_size();
12504 SmallVector<Expr *, 16> Vars;
12505 Vars.reserve(N: NumVars);
12506 for (unsigned i = 0; i != NumVars; ++i)
12507 Vars.push_back(Elt: Record.readSubExpr());
12508 C->setVarRefs(Vars);
12509}
12510
12511void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12512 C->setDepobj(Record.readSubExpr());
12513 C->setLParenLoc(Record.readSourceLocation());
12514}
12515
12516void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12517 C->setLParenLoc(Record.readSourceLocation());
12518 C->setModifier(Record.readSubExpr());
12519 C->setDependencyKind(
12520 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12521 C->setDependencyLoc(Record.readSourceLocation());
12522 C->setColonLoc(Record.readSourceLocation());
12523 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12524 unsigned NumVars = C->varlist_size();
12525 SmallVector<Expr *, 16> Vars;
12526 Vars.reserve(N: NumVars);
12527 for (unsigned I = 0; I != NumVars; ++I)
12528 Vars.push_back(Elt: Record.readSubExpr());
12529 C->setVarRefs(Vars);
12530 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12531 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12532}
12533
12534void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12535 VisitOMPClauseWithPreInit(C);
12536 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12537 C->setDevice(Record.readSubExpr());
12538 C->setModifierLoc(Record.readSourceLocation());
12539 C->setLParenLoc(Record.readSourceLocation());
12540}
12541
12542void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12543 C->setLParenLoc(Record.readSourceLocation());
12544 bool HasIteratorModifier = false;
12545 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12546 C->setMapTypeModifier(
12547 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12548 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12549 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12550 HasIteratorModifier = true;
12551 }
12552 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12553 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12554 C->setMapType(
12555 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12556 C->setMapLoc(Record.readSourceLocation());
12557 C->setColonLoc(Record.readSourceLocation());
12558 auto NumVars = C->varlist_size();
12559 auto UniqueDecls = C->getUniqueDeclarationsNum();
12560 auto TotalLists = C->getTotalComponentListNum();
12561 auto TotalComponents = C->getTotalComponentsNum();
12562
12563 SmallVector<Expr *, 16> Vars;
12564 Vars.reserve(N: NumVars);
12565 for (unsigned i = 0; i != NumVars; ++i)
12566 Vars.push_back(Elt: Record.readExpr());
12567 C->setVarRefs(Vars);
12568
12569 SmallVector<Expr *, 16> UDMappers;
12570 UDMappers.reserve(N: NumVars);
12571 for (unsigned I = 0; I < NumVars; ++I)
12572 UDMappers.push_back(Elt: Record.readExpr());
12573 C->setUDMapperRefs(UDMappers);
12574
12575 if (HasIteratorModifier)
12576 C->setIteratorModifier(Record.readExpr());
12577
12578 SmallVector<ValueDecl *, 16> Decls;
12579 Decls.reserve(N: UniqueDecls);
12580 for (unsigned i = 0; i < UniqueDecls; ++i)
12581 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12582 C->setUniqueDecls(Decls);
12583
12584 SmallVector<unsigned, 16> ListsPerDecl;
12585 ListsPerDecl.reserve(N: UniqueDecls);
12586 for (unsigned i = 0; i < UniqueDecls; ++i)
12587 ListsPerDecl.push_back(Elt: Record.readInt());
12588 C->setDeclNumLists(ListsPerDecl);
12589
12590 SmallVector<unsigned, 32> ListSizes;
12591 ListSizes.reserve(N: TotalLists);
12592 for (unsigned i = 0; i < TotalLists; ++i)
12593 ListSizes.push_back(Elt: Record.readInt());
12594 C->setComponentListSizes(ListSizes);
12595
12596 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12597 Components.reserve(N: TotalComponents);
12598 for (unsigned i = 0; i < TotalComponents; ++i) {
12599 Expr *AssociatedExprPr = Record.readExpr();
12600 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12601 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12602 /*IsNonContiguous=*/Args: false);
12603 }
12604 C->setComponents(Components, CLSs: ListSizes);
12605}
12606
12607void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12608 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12609 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12610 C->setLParenLoc(Record.readSourceLocation());
12611 C->setColonLoc(Record.readSourceLocation());
12612 C->setAllocator(Record.readSubExpr());
12613 C->setAlignment(Record.readSubExpr());
12614 unsigned NumVars = C->varlist_size();
12615 SmallVector<Expr *, 16> Vars;
12616 Vars.reserve(N: NumVars);
12617 for (unsigned i = 0; i != NumVars; ++i)
12618 Vars.push_back(Elt: Record.readSubExpr());
12619 C->setVarRefs(Vars);
12620}
12621
12622void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12623 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12624 C->setModifierLoc(Record.readSourceLocation());
12625 C->setModifierExpr(Record.readSubExpr());
12626 VisitOMPClauseWithPreInit(C);
12627 C->setLParenLoc(Record.readSourceLocation());
12628 unsigned NumVars = C->varlist_size();
12629 SmallVector<Expr *, 16> Vars;
12630 Vars.reserve(N: NumVars);
12631 for (unsigned I = 0; I != NumVars; ++I)
12632 Vars.push_back(Elt: Record.readSubExpr());
12633 C->setVarRefs(Vars);
12634}
12635
12636void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12637 C->setModifier(Record.readEnum<OpenMPThreadLimitClauseModifier>());
12638 C->setModifierLoc(Record.readSourceLocation());
12639 C->setModifierExpr(Record.readSubExpr());
12640 VisitOMPClauseWithPreInit(C);
12641 C->setLParenLoc(Record.readSourceLocation());
12642 unsigned NumVars = C->varlist_size();
12643 SmallVector<Expr *, 16> Vars;
12644 Vars.reserve(N: NumVars);
12645 for (unsigned I = 0; I != NumVars; ++I)
12646 Vars.push_back(Elt: Record.readSubExpr());
12647 C->setVarRefs(Vars);
12648}
12649
12650void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12651 VisitOMPClauseWithPreInit(C);
12652 C->setPriority(Record.readSubExpr());
12653 C->setLParenLoc(Record.readSourceLocation());
12654}
12655
12656void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12657 VisitOMPClauseWithPreInit(C);
12658 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12659 C->setGrainsize(Record.readSubExpr());
12660 C->setModifierLoc(Record.readSourceLocation());
12661 C->setLParenLoc(Record.readSourceLocation());
12662}
12663
12664void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12665 VisitOMPClauseWithPreInit(C);
12666 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12667 C->setNumTasks(Record.readSubExpr());
12668 C->setModifierLoc(Record.readSourceLocation());
12669 C->setLParenLoc(Record.readSourceLocation());
12670}
12671
12672void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12673 C->setHint(Record.readSubExpr());
12674 C->setLParenLoc(Record.readSourceLocation());
12675}
12676
12677void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12678 VisitOMPClauseWithPreInit(C);
12679 C->setDistScheduleKind(
12680 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12681 C->setChunkSize(Record.readSubExpr());
12682 C->setLParenLoc(Record.readSourceLocation());
12683 C->setDistScheduleKindLoc(Record.readSourceLocation());
12684 C->setCommaLoc(Record.readSourceLocation());
12685}
12686
12687void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12688 C->setDefaultmapKind(
12689 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12690 C->setDefaultmapModifier(
12691 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12692 C->setLParenLoc(Record.readSourceLocation());
12693 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12694 C->setDefaultmapKindLoc(Record.readSourceLocation());
12695}
12696
12697void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12698 C->setLParenLoc(Record.readSourceLocation());
12699 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12700 C->setMotionModifier(
12701 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12702 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12703 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12704 C->setIteratorModifier(Record.readExpr());
12705 }
12706 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12707 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12708 C->setColonLoc(Record.readSourceLocation());
12709 auto NumVars = C->varlist_size();
12710 auto UniqueDecls = C->getUniqueDeclarationsNum();
12711 auto TotalLists = C->getTotalComponentListNum();
12712 auto TotalComponents = C->getTotalComponentsNum();
12713
12714 SmallVector<Expr *, 16> Vars;
12715 Vars.reserve(N: NumVars);
12716 for (unsigned i = 0; i != NumVars; ++i)
12717 Vars.push_back(Elt: Record.readSubExpr());
12718 C->setVarRefs(Vars);
12719
12720 SmallVector<Expr *, 16> UDMappers;
12721 UDMappers.reserve(N: NumVars);
12722 for (unsigned I = 0; I < NumVars; ++I)
12723 UDMappers.push_back(Elt: Record.readSubExpr());
12724 C->setUDMapperRefs(UDMappers);
12725
12726 SmallVector<ValueDecl *, 16> Decls;
12727 Decls.reserve(N: UniqueDecls);
12728 for (unsigned i = 0; i < UniqueDecls; ++i)
12729 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12730 C->setUniqueDecls(Decls);
12731
12732 SmallVector<unsigned, 16> ListsPerDecl;
12733 ListsPerDecl.reserve(N: UniqueDecls);
12734 for (unsigned i = 0; i < UniqueDecls; ++i)
12735 ListsPerDecl.push_back(Elt: Record.readInt());
12736 C->setDeclNumLists(ListsPerDecl);
12737
12738 SmallVector<unsigned, 32> ListSizes;
12739 ListSizes.reserve(N: TotalLists);
12740 for (unsigned i = 0; i < TotalLists; ++i)
12741 ListSizes.push_back(Elt: Record.readInt());
12742 C->setComponentListSizes(ListSizes);
12743
12744 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12745 Components.reserve(N: TotalComponents);
12746 for (unsigned i = 0; i < TotalComponents; ++i) {
12747 Expr *AssociatedExprPr = Record.readSubExpr();
12748 bool IsNonContiguous = Record.readBool();
12749 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12750 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12751 }
12752 C->setComponents(Components, CLSs: ListSizes);
12753}
12754
12755void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12756 C->setLParenLoc(Record.readSourceLocation());
12757 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12758 C->setMotionModifier(
12759 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12760 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12761 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12762 C->setIteratorModifier(Record.readExpr());
12763 }
12764 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12765 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12766 C->setColonLoc(Record.readSourceLocation());
12767 auto NumVars = C->varlist_size();
12768 auto UniqueDecls = C->getUniqueDeclarationsNum();
12769 auto TotalLists = C->getTotalComponentListNum();
12770 auto TotalComponents = C->getTotalComponentsNum();
12771
12772 SmallVector<Expr *, 16> Vars;
12773 Vars.reserve(N: NumVars);
12774 for (unsigned i = 0; i != NumVars; ++i)
12775 Vars.push_back(Elt: Record.readSubExpr());
12776 C->setVarRefs(Vars);
12777
12778 SmallVector<Expr *, 16> UDMappers;
12779 UDMappers.reserve(N: NumVars);
12780 for (unsigned I = 0; I < NumVars; ++I)
12781 UDMappers.push_back(Elt: Record.readSubExpr());
12782 C->setUDMapperRefs(UDMappers);
12783
12784 SmallVector<ValueDecl *, 16> Decls;
12785 Decls.reserve(N: UniqueDecls);
12786 for (unsigned i = 0; i < UniqueDecls; ++i)
12787 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12788 C->setUniqueDecls(Decls);
12789
12790 SmallVector<unsigned, 16> ListsPerDecl;
12791 ListsPerDecl.reserve(N: UniqueDecls);
12792 for (unsigned i = 0; i < UniqueDecls; ++i)
12793 ListsPerDecl.push_back(Elt: Record.readInt());
12794 C->setDeclNumLists(ListsPerDecl);
12795
12796 SmallVector<unsigned, 32> ListSizes;
12797 ListSizes.reserve(N: TotalLists);
12798 for (unsigned i = 0; i < TotalLists; ++i)
12799 ListSizes.push_back(Elt: Record.readInt());
12800 C->setComponentListSizes(ListSizes);
12801
12802 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12803 Components.reserve(N: TotalComponents);
12804 for (unsigned i = 0; i < TotalComponents; ++i) {
12805 Expr *AssociatedExprPr = Record.readSubExpr();
12806 bool IsNonContiguous = Record.readBool();
12807 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12808 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12809 }
12810 C->setComponents(Components, CLSs: ListSizes);
12811}
12812
12813void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12814 C->setLParenLoc(Record.readSourceLocation());
12815 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12816 C->setFallbackModifierLoc(Record.readSourceLocation());
12817 auto NumVars = C->varlist_size();
12818 auto UniqueDecls = C->getUniqueDeclarationsNum();
12819 auto TotalLists = C->getTotalComponentListNum();
12820 auto TotalComponents = C->getTotalComponentsNum();
12821
12822 SmallVector<Expr *, 16> Vars;
12823 Vars.reserve(N: NumVars);
12824 for (unsigned i = 0; i != NumVars; ++i)
12825 Vars.push_back(Elt: Record.readSubExpr());
12826 C->setVarRefs(Vars);
12827 Vars.clear();
12828 for (unsigned i = 0; i != NumVars; ++i)
12829 Vars.push_back(Elt: Record.readSubExpr());
12830 C->setPrivateCopies(Vars);
12831 Vars.clear();
12832 for (unsigned i = 0; i != NumVars; ++i)
12833 Vars.push_back(Elt: Record.readSubExpr());
12834 C->setInits(Vars);
12835
12836 SmallVector<ValueDecl *, 16> Decls;
12837 Decls.reserve(N: UniqueDecls);
12838 for (unsigned i = 0; i < UniqueDecls; ++i)
12839 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12840 C->setUniqueDecls(Decls);
12841
12842 SmallVector<unsigned, 16> ListsPerDecl;
12843 ListsPerDecl.reserve(N: UniqueDecls);
12844 for (unsigned i = 0; i < UniqueDecls; ++i)
12845 ListsPerDecl.push_back(Elt: Record.readInt());
12846 C->setDeclNumLists(ListsPerDecl);
12847
12848 SmallVector<unsigned, 32> ListSizes;
12849 ListSizes.reserve(N: TotalLists);
12850 for (unsigned i = 0; i < TotalLists; ++i)
12851 ListSizes.push_back(Elt: Record.readInt());
12852 C->setComponentListSizes(ListSizes);
12853
12854 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12855 Components.reserve(N: TotalComponents);
12856 for (unsigned i = 0; i < TotalComponents; ++i) {
12857 auto *AssociatedExprPr = Record.readSubExpr();
12858 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12859 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12860 /*IsNonContiguous=*/Args: false);
12861 }
12862 C->setComponents(Components, CLSs: ListSizes);
12863}
12864
12865void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12866 C->setLParenLoc(Record.readSourceLocation());
12867 auto NumVars = C->varlist_size();
12868 auto UniqueDecls = C->getUniqueDeclarationsNum();
12869 auto TotalLists = C->getTotalComponentListNum();
12870 auto TotalComponents = C->getTotalComponentsNum();
12871
12872 SmallVector<Expr *, 16> Vars;
12873 Vars.reserve(N: NumVars);
12874 for (unsigned i = 0; i != NumVars; ++i)
12875 Vars.push_back(Elt: Record.readSubExpr());
12876 C->setVarRefs(Vars);
12877
12878 SmallVector<ValueDecl *, 16> Decls;
12879 Decls.reserve(N: UniqueDecls);
12880 for (unsigned i = 0; i < UniqueDecls; ++i)
12881 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12882 C->setUniqueDecls(Decls);
12883
12884 SmallVector<unsigned, 16> ListsPerDecl;
12885 ListsPerDecl.reserve(N: UniqueDecls);
12886 for (unsigned i = 0; i < UniqueDecls; ++i)
12887 ListsPerDecl.push_back(Elt: Record.readInt());
12888 C->setDeclNumLists(ListsPerDecl);
12889
12890 SmallVector<unsigned, 32> ListSizes;
12891 ListSizes.reserve(N: TotalLists);
12892 for (unsigned i = 0; i < TotalLists; ++i)
12893 ListSizes.push_back(Elt: Record.readInt());
12894 C->setComponentListSizes(ListSizes);
12895
12896 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12897 Components.reserve(N: TotalComponents);
12898 for (unsigned i = 0; i < TotalComponents; ++i) {
12899 Expr *AssociatedExpr = Record.readSubExpr();
12900 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12901 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12902 /*IsNonContiguous*/ Args: false);
12903 }
12904 C->setComponents(Components, CLSs: ListSizes);
12905}
12906
12907void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12908 C->setLParenLoc(Record.readSourceLocation());
12909 auto NumVars = C->varlist_size();
12910 auto UniqueDecls = C->getUniqueDeclarationsNum();
12911 auto TotalLists = C->getTotalComponentListNum();
12912 auto TotalComponents = C->getTotalComponentsNum();
12913
12914 SmallVector<Expr *, 16> Vars;
12915 Vars.reserve(N: NumVars);
12916 for (unsigned i = 0; i != NumVars; ++i)
12917 Vars.push_back(Elt: Record.readSubExpr());
12918 C->setVarRefs(Vars);
12919 Vars.clear();
12920
12921 SmallVector<ValueDecl *, 16> Decls;
12922 Decls.reserve(N: UniqueDecls);
12923 for (unsigned i = 0; i < UniqueDecls; ++i)
12924 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12925 C->setUniqueDecls(Decls);
12926
12927 SmallVector<unsigned, 16> ListsPerDecl;
12928 ListsPerDecl.reserve(N: UniqueDecls);
12929 for (unsigned i = 0; i < UniqueDecls; ++i)
12930 ListsPerDecl.push_back(Elt: Record.readInt());
12931 C->setDeclNumLists(ListsPerDecl);
12932
12933 SmallVector<unsigned, 32> ListSizes;
12934 ListSizes.reserve(N: TotalLists);
12935 for (unsigned i = 0; i < TotalLists; ++i)
12936 ListSizes.push_back(Elt: Record.readInt());
12937 C->setComponentListSizes(ListSizes);
12938
12939 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12940 Components.reserve(N: TotalComponents);
12941 for (unsigned i = 0; i < TotalComponents; ++i) {
12942 Expr *AssociatedExpr = Record.readSubExpr();
12943 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12944 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12945 /*IsNonContiguous=*/Args: false);
12946 }
12947 C->setComponents(Components, CLSs: ListSizes);
12948}
12949
12950void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12951 C->setLParenLoc(Record.readSourceLocation());
12952 auto NumVars = C->varlist_size();
12953 auto UniqueDecls = C->getUniqueDeclarationsNum();
12954 auto TotalLists = C->getTotalComponentListNum();
12955 auto TotalComponents = C->getTotalComponentsNum();
12956
12957 SmallVector<Expr *, 16> Vars;
12958 Vars.reserve(N: NumVars);
12959 for (unsigned I = 0; I != NumVars; ++I)
12960 Vars.push_back(Elt: Record.readSubExpr());
12961 C->setVarRefs(Vars);
12962 Vars.clear();
12963
12964 SmallVector<ValueDecl *, 16> Decls;
12965 Decls.reserve(N: UniqueDecls);
12966 for (unsigned I = 0; I < UniqueDecls; ++I)
12967 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12968 C->setUniqueDecls(Decls);
12969
12970 SmallVector<unsigned, 16> ListsPerDecl;
12971 ListsPerDecl.reserve(N: UniqueDecls);
12972 for (unsigned I = 0; I < UniqueDecls; ++I)
12973 ListsPerDecl.push_back(Elt: Record.readInt());
12974 C->setDeclNumLists(ListsPerDecl);
12975
12976 SmallVector<unsigned, 32> ListSizes;
12977 ListSizes.reserve(N: TotalLists);
12978 for (unsigned i = 0; i < TotalLists; ++i)
12979 ListSizes.push_back(Elt: Record.readInt());
12980 C->setComponentListSizes(ListSizes);
12981
12982 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12983 Components.reserve(N: TotalComponents);
12984 for (unsigned I = 0; I < TotalComponents; ++I) {
12985 Expr *AssociatedExpr = Record.readSubExpr();
12986 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12987 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12988 /*IsNonContiguous=*/Args: false);
12989 }
12990 C->setComponents(Components, CLSs: ListSizes);
12991}
12992
12993void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12994 C->setLParenLoc(Record.readSourceLocation());
12995 unsigned NumVars = C->varlist_size();
12996 SmallVector<Expr *, 16> Vars;
12997 Vars.reserve(N: NumVars);
12998 for (unsigned i = 0; i != NumVars; ++i)
12999 Vars.push_back(Elt: Record.readSubExpr());
13000 C->setVarRefs(Vars);
13001 Vars.clear();
13002 Vars.reserve(N: NumVars);
13003 for (unsigned i = 0; i != NumVars; ++i)
13004 Vars.push_back(Elt: Record.readSubExpr());
13005 C->setPrivateRefs(Vars);
13006}
13007
13008void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
13009 C->setLParenLoc(Record.readSourceLocation());
13010 unsigned NumVars = C->varlist_size();
13011 SmallVector<Expr *, 16> Vars;
13012 Vars.reserve(N: NumVars);
13013 for (unsigned i = 0; i != NumVars; ++i)
13014 Vars.push_back(Elt: Record.readSubExpr());
13015 C->setVarRefs(Vars);
13016}
13017
13018void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
13019 C->setLParenLoc(Record.readSourceLocation());
13020 unsigned NumVars = C->varlist_size();
13021 SmallVector<Expr *, 16> Vars;
13022 Vars.reserve(N: NumVars);
13023 for (unsigned i = 0; i != NumVars; ++i)
13024 Vars.push_back(Elt: Record.readSubExpr());
13025 C->setVarRefs(Vars);
13026}
13027
13028void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13029 C->setLParenLoc(Record.readSourceLocation());
13030 unsigned NumOfAllocators = C->getNumberOfAllocators();
13031 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13032 Data.reserve(N: NumOfAllocators);
13033 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13034 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13035 D.Allocator = Record.readSubExpr();
13036 D.AllocatorTraits = Record.readSubExpr();
13037 D.LParenLoc = Record.readSourceLocation();
13038 D.RParenLoc = Record.readSourceLocation();
13039 }
13040 C->setAllocatorsData(Data);
13041}
13042
13043void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13044 C->setLParenLoc(Record.readSourceLocation());
13045 C->setModifier(Record.readSubExpr());
13046 C->setColonLoc(Record.readSourceLocation());
13047 unsigned NumOfLocators = C->varlist_size();
13048 SmallVector<Expr *, 4> Locators;
13049 Locators.reserve(N: NumOfLocators);
13050 for (unsigned I = 0; I != NumOfLocators; ++I)
13051 Locators.push_back(Elt: Record.readSubExpr());
13052 C->setVarRefs(Locators);
13053}
13054
13055void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13056 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13057 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13058 C->setLParenLoc(Record.readSourceLocation());
13059 C->setKindKwLoc(Record.readSourceLocation());
13060 C->setModifierKwLoc(Record.readSourceLocation());
13061}
13062
13063void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13064 VisitOMPClauseWithPreInit(C);
13065 C->setThreadID(Record.readSubExpr());
13066 C->setLParenLoc(Record.readSourceLocation());
13067}
13068
13069void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13070 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13071 C->setLParenLoc(Record.readSourceLocation());
13072 C->setBindKindLoc(Record.readSourceLocation());
13073}
13074
13075void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13076 C->setAlignment(Record.readExpr());
13077 C->setLParenLoc(Record.readSourceLocation());
13078}
13079
13080void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13081 VisitOMPClauseWithPreInit(C);
13082 C->setSize(Record.readSubExpr());
13083 C->setLParenLoc(Record.readSourceLocation());
13084}
13085
13086void OMPClauseReader::VisitOMPDynGroupprivateClause(
13087 OMPDynGroupprivateClause *C) {
13088 VisitOMPClauseWithPreInit(C);
13089 C->setDynGroupprivateModifier(
13090 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13091 C->setDynGroupprivateFallbackModifier(
13092 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13093 C->setSize(Record.readSubExpr());
13094 C->setLParenLoc(Record.readSourceLocation());
13095 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13096 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13097}
13098
13099void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13100 C->setLParenLoc(Record.readSourceLocation());
13101 C->setDependenceType(
13102 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13103 C->setDependenceLoc(Record.readSourceLocation());
13104 C->setColonLoc(Record.readSourceLocation());
13105 unsigned NumVars = C->varlist_size();
13106 SmallVector<Expr *, 16> Vars;
13107 Vars.reserve(N: NumVars);
13108 for (unsigned I = 0; I != NumVars; ++I)
13109 Vars.push_back(Elt: Record.readSubExpr());
13110 C->setVarRefs(Vars);
13111 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13112 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13113}
13114
13115void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13116 AttrVec Attrs;
13117 Record.readAttributes(Attrs);
13118 C->setAttrs(Attrs);
13119 C->setLocStart(Record.readSourceLocation());
13120 C->setLParenLoc(Record.readSourceLocation());
13121 C->setLocEnd(Record.readSourceLocation());
13122}
13123
13124void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13125
13126OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13127 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13128 TI.Sets.resize(N: readUInt32());
13129 for (auto &Set : TI.Sets) {
13130 Set.Kind = readEnum<llvm::omp::TraitSet>();
13131 Set.Selectors.resize(N: readUInt32());
13132 for (auto &Selector : Set.Selectors) {
13133 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13134 Selector.ScoreOrCondition = nullptr;
13135 if (readBool())
13136 Selector.ScoreOrCondition = readExprRef();
13137 Selector.Properties.resize(N: readUInt32());
13138 for (auto &Property : Selector.Properties)
13139 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13140 }
13141 }
13142 return &TI;
13143}
13144
13145void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13146 if (!Data)
13147 return;
13148 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13149 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13150 skipInts(N: 3);
13151 }
13152 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13153 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13154 Clauses[I] = readOMPClause();
13155 Data->setClauses(Clauses);
13156 if (Data->hasAssociatedStmt())
13157 Data->setAssociatedStmt(readStmt());
13158 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13159 Data->getChildren()[I] = readStmt();
13160}
13161
13162SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13163 unsigned NumVars = readInt();
13164 llvm::SmallVector<Expr *> VarList;
13165 for (unsigned I = 0; I < NumVars; ++I)
13166 VarList.push_back(Elt: readExpr());
13167 return VarList;
13168}
13169
13170SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13171 unsigned NumExprs = readInt();
13172 llvm::SmallVector<Expr *> ExprList;
13173 for (unsigned I = 0; I < NumExprs; ++I)
13174 ExprList.push_back(Elt: readSubExpr());
13175 return ExprList;
13176}
13177
13178OpenACCClause *ASTRecordReader::readOpenACCClause() {
13179 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13180 SourceLocation BeginLoc = readSourceLocation();
13181 SourceLocation EndLoc = readSourceLocation();
13182
13183 switch (ClauseKind) {
13184 case OpenACCClauseKind::Default: {
13185 SourceLocation LParenLoc = readSourceLocation();
13186 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13187 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13188 EndLoc);
13189 }
13190 case OpenACCClauseKind::If: {
13191 SourceLocation LParenLoc = readSourceLocation();
13192 Expr *CondExpr = readSubExpr();
13193 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13194 EndLoc);
13195 }
13196 case OpenACCClauseKind::Self: {
13197 SourceLocation LParenLoc = readSourceLocation();
13198 bool isConditionExprClause = readBool();
13199 if (isConditionExprClause) {
13200 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13201 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13202 ConditionExpr: CondExpr, EndLoc);
13203 }
13204 unsigned NumVars = readInt();
13205 llvm::SmallVector<Expr *> VarList;
13206 for (unsigned I = 0; I < NumVars; ++I)
13207 VarList.push_back(Elt: readSubExpr());
13208 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13209 EndLoc);
13210 }
13211 case OpenACCClauseKind::NumGangs: {
13212 SourceLocation LParenLoc = readSourceLocation();
13213 unsigned NumClauses = readInt();
13214 llvm::SmallVector<Expr *> IntExprs;
13215 for (unsigned I = 0; I < NumClauses; ++I)
13216 IntExprs.push_back(Elt: readSubExpr());
13217 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13218 IntExprs, EndLoc);
13219 }
13220 case OpenACCClauseKind::NumWorkers: {
13221 SourceLocation LParenLoc = readSourceLocation();
13222 Expr *IntExpr = readSubExpr();
13223 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13224 IntExpr, EndLoc);
13225 }
13226 case OpenACCClauseKind::DeviceNum: {
13227 SourceLocation LParenLoc = readSourceLocation();
13228 Expr *IntExpr = readSubExpr();
13229 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13230 IntExpr, EndLoc);
13231 }
13232 case OpenACCClauseKind::DefaultAsync: {
13233 SourceLocation LParenLoc = readSourceLocation();
13234 Expr *IntExpr = readSubExpr();
13235 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13236 IntExpr, EndLoc);
13237 }
13238 case OpenACCClauseKind::VectorLength: {
13239 SourceLocation LParenLoc = readSourceLocation();
13240 Expr *IntExpr = readSubExpr();
13241 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13242 IntExpr, EndLoc);
13243 }
13244 case OpenACCClauseKind::Private: {
13245 SourceLocation LParenLoc = readSourceLocation();
13246 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13247
13248 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13249 for (unsigned I = 0; I < VarList.size(); ++I) {
13250 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13251 VarDecl *Alloca = readDeclAs<VarDecl>();
13252 RecipeList.push_back(Elt: {Alloca});
13253 }
13254
13255 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13256 VarList, InitRecipes: RecipeList, EndLoc);
13257 }
13258 case OpenACCClauseKind::Host: {
13259 SourceLocation LParenLoc = readSourceLocation();
13260 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13261 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13262 EndLoc);
13263 }
13264 case OpenACCClauseKind::Device: {
13265 SourceLocation LParenLoc = readSourceLocation();
13266 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13267 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13268 VarList, EndLoc);
13269 }
13270 case OpenACCClauseKind::FirstPrivate: {
13271 SourceLocation LParenLoc = readSourceLocation();
13272 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13273 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13274 for (unsigned I = 0; I < VarList.size(); ++I) {
13275 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13276 VarDecl *Recipe = readDeclAs<VarDecl>();
13277 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13278 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13279 }
13280
13281 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13282 VarList, InitRecipes: RecipeList, EndLoc);
13283 }
13284 case OpenACCClauseKind::Attach: {
13285 SourceLocation LParenLoc = readSourceLocation();
13286 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13287 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13288 VarList, EndLoc);
13289 }
13290 case OpenACCClauseKind::Detach: {
13291 SourceLocation LParenLoc = readSourceLocation();
13292 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13293 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13294 VarList, EndLoc);
13295 }
13296 case OpenACCClauseKind::Delete: {
13297 SourceLocation LParenLoc = readSourceLocation();
13298 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13299 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13300 VarList, EndLoc);
13301 }
13302 case OpenACCClauseKind::UseDevice: {
13303 SourceLocation LParenLoc = readSourceLocation();
13304 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13305 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13306 VarList, EndLoc);
13307 }
13308 case OpenACCClauseKind::DevicePtr: {
13309 SourceLocation LParenLoc = readSourceLocation();
13310 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13311 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13312 VarList, EndLoc);
13313 }
13314 case OpenACCClauseKind::NoCreate: {
13315 SourceLocation LParenLoc = readSourceLocation();
13316 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13317 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13318 VarList, EndLoc);
13319 }
13320 case OpenACCClauseKind::Present: {
13321 SourceLocation LParenLoc = readSourceLocation();
13322 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13323 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13324 VarList, EndLoc);
13325 }
13326 case OpenACCClauseKind::PCopy:
13327 case OpenACCClauseKind::PresentOrCopy:
13328 case OpenACCClauseKind::Copy: {
13329 SourceLocation LParenLoc = readSourceLocation();
13330 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13331 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13332 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13333 LParenLoc, Mods: ModList, VarList, EndLoc);
13334 }
13335 case OpenACCClauseKind::CopyIn:
13336 case OpenACCClauseKind::PCopyIn:
13337 case OpenACCClauseKind::PresentOrCopyIn: {
13338 SourceLocation LParenLoc = readSourceLocation();
13339 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13340 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13341 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13342 LParenLoc, Mods: ModList, VarList, EndLoc);
13343 }
13344 case OpenACCClauseKind::CopyOut:
13345 case OpenACCClauseKind::PCopyOut:
13346 case OpenACCClauseKind::PresentOrCopyOut: {
13347 SourceLocation LParenLoc = readSourceLocation();
13348 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13349 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13350 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13351 LParenLoc, Mods: ModList, VarList, EndLoc);
13352 }
13353 case OpenACCClauseKind::Create:
13354 case OpenACCClauseKind::PCreate:
13355 case OpenACCClauseKind::PresentOrCreate: {
13356 SourceLocation LParenLoc = readSourceLocation();
13357 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13358 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13359 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13360 LParenLoc, Mods: ModList, VarList, EndLoc);
13361 }
13362 case OpenACCClauseKind::Async: {
13363 SourceLocation LParenLoc = readSourceLocation();
13364 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13365 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13366 IntExpr: AsyncExpr, EndLoc);
13367 }
13368 case OpenACCClauseKind::Wait: {
13369 SourceLocation LParenLoc = readSourceLocation();
13370 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13371 SourceLocation QueuesLoc = readSourceLocation();
13372 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13373 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13374 DevNumExpr, QueuesLoc, QueueIdExprs,
13375 EndLoc);
13376 }
13377 case OpenACCClauseKind::DeviceType:
13378 case OpenACCClauseKind::DType: {
13379 SourceLocation LParenLoc = readSourceLocation();
13380 llvm::SmallVector<DeviceTypeArgument> Archs;
13381 unsigned NumArchs = readInt();
13382
13383 for (unsigned I = 0; I < NumArchs; ++I) {
13384 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13385 SourceLocation Loc = readSourceLocation();
13386 Archs.emplace_back(Args&: Loc, Args&: Ident);
13387 }
13388
13389 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13390 LParenLoc, Archs, EndLoc);
13391 }
13392 case OpenACCClauseKind::Reduction: {
13393 SourceLocation LParenLoc = readSourceLocation();
13394 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13395 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13396 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13397
13398 for (unsigned I = 0; I < VarList.size(); ++I) {
13399 VarDecl *Recipe = readDeclAs<VarDecl>();
13400
13401 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13402 3 * sizeof(int *));
13403
13404 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13405 unsigned NumCombiners = readInt();
13406 for (unsigned I = 0; I < NumCombiners; ++I) {
13407 VarDecl *LHS = readDeclAs<VarDecl>();
13408 VarDecl *RHS = readDeclAs<VarDecl>();
13409 Expr *Op = readExpr();
13410
13411 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13412 }
13413
13414 RecipeList.push_back(Elt: {Recipe, Combiners});
13415 }
13416
13417 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13418 VarList, Recipes: RecipeList, EndLoc);
13419 }
13420 case OpenACCClauseKind::Seq:
13421 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13422 case OpenACCClauseKind::NoHost:
13423 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13424 case OpenACCClauseKind::Finalize:
13425 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13426 case OpenACCClauseKind::IfPresent:
13427 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13428 case OpenACCClauseKind::Independent:
13429 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13430 case OpenACCClauseKind::Auto:
13431 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13432 case OpenACCClauseKind::Collapse: {
13433 SourceLocation LParenLoc = readSourceLocation();
13434 bool HasForce = readBool();
13435 Expr *LoopCount = readSubExpr();
13436 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13437 HasForce, LoopCount, EndLoc);
13438 }
13439 case OpenACCClauseKind::Tile: {
13440 SourceLocation LParenLoc = readSourceLocation();
13441 unsigned NumClauses = readInt();
13442 llvm::SmallVector<Expr *> SizeExprs;
13443 for (unsigned I = 0; I < NumClauses; ++I)
13444 SizeExprs.push_back(Elt: readSubExpr());
13445 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13446 SizeExprs, EndLoc);
13447 }
13448 case OpenACCClauseKind::Gang: {
13449 SourceLocation LParenLoc = readSourceLocation();
13450 unsigned NumExprs = readInt();
13451 llvm::SmallVector<OpenACCGangKind> GangKinds;
13452 llvm::SmallVector<Expr *> Exprs;
13453 for (unsigned I = 0; I < NumExprs; ++I) {
13454 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13455 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13456 Exprs.push_back(Elt: readExpr());
13457 }
13458 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13459 GangKinds, IntExprs: Exprs, EndLoc);
13460 }
13461 case OpenACCClauseKind::Worker: {
13462 SourceLocation LParenLoc = readSourceLocation();
13463 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13464 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13465 IntExpr: WorkerExpr, EndLoc);
13466 }
13467 case OpenACCClauseKind::Vector: {
13468 SourceLocation LParenLoc = readSourceLocation();
13469 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13470 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13471 IntExpr: VectorExpr, EndLoc);
13472 }
13473 case OpenACCClauseKind::Link: {
13474 SourceLocation LParenLoc = readSourceLocation();
13475 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13476 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13477 EndLoc);
13478 }
13479 case OpenACCClauseKind::DeviceResident: {
13480 SourceLocation LParenLoc = readSourceLocation();
13481 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13482 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13483 LParenLoc, VarList, EndLoc);
13484 }
13485
13486 case OpenACCClauseKind::Bind: {
13487 SourceLocation LParenLoc = readSourceLocation();
13488 bool IsString = readBool();
13489 if (IsString)
13490 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13491 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13492 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13493 ID: readIdentifier(), EndLoc);
13494 }
13495 case OpenACCClauseKind::Shortloop:
13496 case OpenACCClauseKind::Invalid:
13497 llvm_unreachable("Clause serialization not yet implemented");
13498 }
13499 llvm_unreachable("Invalid Clause Kind");
13500}
13501
13502void ASTRecordReader::readOpenACCClauseList(
13503 MutableArrayRef<const OpenACCClause *> Clauses) {
13504 for (unsigned I = 0; I < Clauses.size(); ++I)
13505 Clauses[I] = readOpenACCClause();
13506}
13507
13508void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13509 unsigned NumVars = readInt();
13510 A->Clauses.resize(N: NumVars);
13511 readOpenACCClauseList(Clauses: A->Clauses);
13512}
13513
13514static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13515 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13516 // module name?
13517 llvm::FoldingSetNodeID ID;
13518 ID.AddString(String: PrimaryModuleName);
13519 return ID.computeStableHash();
13520}
13521
13522UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13523 if (!M)
13524 return std::nullopt;
13525
13526 if (M->isHeaderLikeModule())
13527 return std::nullopt;
13528
13529 if (M->isGlobalModule())
13530 return std::nullopt;
13531
13532 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13533 return getStableHashForModuleName(PrimaryModuleName);
13534}
13535