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/Decl.h"
23#include "clang/AST/DeclBase.h"
24#include "clang/AST/DeclCXX.h"
25#include "clang/AST/DeclFriend.h"
26#include "clang/AST/DeclGroup.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/DeclTemplate.h"
29#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExternalASTSource.h"
33#include "clang/AST/NestedNameSpecifier.h"
34#include "clang/AST/ODRDiagsEmitter.h"
35#include "clang/AST/OpenACCClause.h"
36#include "clang/AST/OpenMPClause.h"
37#include "clang/AST/RawCommentList.h"
38#include "clang/AST/TemplateBase.h"
39#include "clang/AST/TemplateName.h"
40#include "clang/AST/Type.h"
41#include "clang/AST/TypeLoc.h"
42#include "clang/AST/TypeLocVisitor.h"
43#include "clang/AST/UnresolvedSet.h"
44#include "clang/Basic/ASTSourceDescriptor.h"
45#include "clang/Basic/CommentOptions.h"
46#include "clang/Basic/Diagnostic.h"
47#include "clang/Basic/DiagnosticIDs.h"
48#include "clang/Basic/DiagnosticOptions.h"
49#include "clang/Basic/DiagnosticSema.h"
50#include "clang/Basic/FileManager.h"
51#include "clang/Basic/FileSystemOptions.h"
52#include "clang/Basic/IdentifierTable.h"
53#include "clang/Basic/LLVM.h"
54#include "clang/Basic/LangOptions.h"
55#include "clang/Basic/Module.h"
56#include "clang/Basic/ObjCRuntime.h"
57#include "clang/Basic/OpenACCKinds.h"
58#include "clang/Basic/OpenMPKinds.h"
59#include "clang/Basic/OperatorKinds.h"
60#include "clang/Basic/PragmaKinds.h"
61#include "clang/Basic/Sanitizers.h"
62#include "clang/Basic/SourceLocation.h"
63#include "clang/Basic/SourceManager.h"
64#include "clang/Basic/SourceManagerInternals.h"
65#include "clang/Basic/Specifiers.h"
66#include "clang/Basic/TargetInfo.h"
67#include "clang/Basic/TargetOptions.h"
68#include "clang/Basic/TokenKinds.h"
69#include "clang/Basic/Version.h"
70#include "clang/Lex/HeaderSearch.h"
71#include "clang/Lex/HeaderSearchOptions.h"
72#include "clang/Lex/MacroInfo.h"
73#include "clang/Lex/ModuleMap.h"
74#include "clang/Lex/PreprocessingRecord.h"
75#include "clang/Lex/Preprocessor.h"
76#include "clang/Lex/PreprocessorOptions.h"
77#include "clang/Lex/Token.h"
78#include "clang/Sema/ObjCMethodList.h"
79#include "clang/Sema/Scope.h"
80#include "clang/Sema/Sema.h"
81#include "clang/Sema/SemaCUDA.h"
82#include "clang/Sema/SemaObjC.h"
83#include "clang/Sema/Weak.h"
84#include "clang/Serialization/ASTBitCodes.h"
85#include "clang/Serialization/ASTDeserializationListener.h"
86#include "clang/Serialization/ASTRecordReader.h"
87#include "clang/Serialization/ContinuousRangeMap.h"
88#include "clang/Serialization/GlobalModuleIndex.h"
89#include "clang/Serialization/InMemoryModuleCache.h"
90#include "clang/Serialization/ModuleCache.h"
91#include "clang/Serialization/ModuleFile.h"
92#include "clang/Serialization/ModuleFileExtension.h"
93#include "clang/Serialization/ModuleManager.h"
94#include "clang/Serialization/PCHContainerOperations.h"
95#include "clang/Serialization/SerializationDiagnostic.h"
96#include "llvm/ADT/APFloat.h"
97#include "llvm/ADT/APInt.h"
98#include "llvm/ADT/ArrayRef.h"
99#include "llvm/ADT/DenseMap.h"
100#include "llvm/ADT/FoldingSet.h"
101#include "llvm/ADT/IntrusiveRefCntPtr.h"
102#include "llvm/ADT/STLExtras.h"
103#include "llvm/ADT/ScopeExit.h"
104#include "llvm/ADT/Sequence.h"
105#include "llvm/ADT/SmallPtrSet.h"
106#include "llvm/ADT/SmallVector.h"
107#include "llvm/ADT/StringExtras.h"
108#include "llvm/ADT/StringMap.h"
109#include "llvm/ADT/StringRef.h"
110#include "llvm/ADT/iterator_range.h"
111#include "llvm/Bitstream/BitstreamReader.h"
112#include "llvm/Support/Compiler.h"
113#include "llvm/Support/Compression.h"
114#include "llvm/Support/DJB.h"
115#include "llvm/Support/Endian.h"
116#include "llvm/Support/Error.h"
117#include "llvm/Support/ErrorHandling.h"
118#include "llvm/Support/LEB128.h"
119#include "llvm/Support/MemoryBuffer.h"
120#include "llvm/Support/Path.h"
121#include "llvm/Support/SaveAndRestore.h"
122#include "llvm/Support/TimeProfiler.h"
123#include "llvm/Support/Timer.h"
124#include "llvm/Support/VersionTuple.h"
125#include "llvm/Support/raw_ostream.h"
126#include "llvm/TargetParser/Triple.h"
127#include <algorithm>
128#include <cassert>
129#include <cstddef>
130#include <cstdint>
131#include <cstdio>
132#include <ctime>
133#include <iterator>
134#include <limits>
135#include <map>
136#include <memory>
137#include <optional>
138#include <string>
139#include <system_error>
140#include <tuple>
141#include <utility>
142#include <vector>
143
144using namespace clang;
145using namespace clang::serialization;
146using namespace clang::serialization::reader;
147using llvm::BitstreamCursor;
148
149//===----------------------------------------------------------------------===//
150// ChainedASTReaderListener implementation
151//===----------------------------------------------------------------------===//
152
153bool
154ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155 return First->ReadFullVersionInformation(FullVersion) ||
156 Second->ReadFullVersionInformation(FullVersion);
157}
158
159void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160 First->ReadModuleName(ModuleName);
161 Second->ReadModuleName(ModuleName);
162}
163
164void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165 First->ReadModuleMapFile(ModuleMapPath);
166 Second->ReadModuleMapFile(ModuleMapPath);
167}
168
169bool ChainedASTReaderListener::ReadLanguageOptions(
170 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
171 bool AllowCompatibleDifferences) {
172 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
173 AllowCompatibleDifferences) ||
174 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
175 AllowCompatibleDifferences);
176}
177
178bool ChainedASTReaderListener::ReadCodeGenOptions(
179 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
180 bool AllowCompatibleDifferences) {
181 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
182 AllowCompatibleDifferences) ||
183 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
184 AllowCompatibleDifferences);
185}
186
187bool ChainedASTReaderListener::ReadTargetOptions(
188 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
189 bool AllowCompatibleDifferences) {
190 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
191 AllowCompatibleDifferences) ||
192 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
193 AllowCompatibleDifferences);
194}
195
196bool ChainedASTReaderListener::ReadDiagnosticOptions(
197 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
198 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
199 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
200}
201
202bool
203ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
204 bool Complain) {
205 return First->ReadFileSystemOptions(FSOpts, Complain) ||
206 Second->ReadFileSystemOptions(FSOpts, Complain);
207}
208
209bool ChainedASTReaderListener::ReadHeaderSearchOptions(
210 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
211 StringRef SpecificModuleCachePath, bool Complain) {
212 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
213 SpecificModuleCachePath, Complain) ||
214 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
215 SpecificModuleCachePath, Complain);
216}
217
218bool ChainedASTReaderListener::ReadPreprocessorOptions(
219 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
220 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
221 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
222 Complain, SuggestedPredefines) ||
223 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
224 Complain, SuggestedPredefines);
225}
226
227void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
228 uint32_t Value) {
229 First->ReadCounter(M, Value);
230 Second->ReadCounter(M, Value);
231}
232
233bool ChainedASTReaderListener::needsInputFileVisitation() {
234 return First->needsInputFileVisitation() ||
235 Second->needsInputFileVisitation();
236}
237
238bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
239 return First->needsSystemInputFileVisitation() ||
240 Second->needsSystemInputFileVisitation();
241}
242
243void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
244 ModuleKind Kind) {
245 First->visitModuleFile(Filename, Kind);
246 Second->visitModuleFile(Filename, Kind);
247}
248
249bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
250 bool isSystem,
251 bool isOverridden,
252 bool isExplicitModule) {
253 bool Continue = false;
254 if (First->needsInputFileVisitation() &&
255 (!isSystem || First->needsSystemInputFileVisitation()))
256 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
257 isExplicitModule);
258 if (Second->needsInputFileVisitation() &&
259 (!isSystem || Second->needsSystemInputFileVisitation()))
260 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
261 isExplicitModule);
262 return Continue;
263}
264
265void ChainedASTReaderListener::readModuleFileExtension(
266 const ModuleFileExtensionMetadata &Metadata) {
267 First->readModuleFileExtension(Metadata);
268 Second->readModuleFileExtension(Metadata);
269}
270
271//===----------------------------------------------------------------------===//
272// PCH validator implementation
273//===----------------------------------------------------------------------===//
274
275ASTReaderListener::~ASTReaderListener() = default;
276
277/// Compare the given set of language options against an existing set of
278/// language options.
279///
280/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
281/// \param AllowCompatibleDifferences If true, differences between compatible
282/// language options will be permitted.
283///
284/// \returns true if the languagae options mis-match, false otherwise.
285static bool checkLanguageOptions(const LangOptions &LangOpts,
286 const LangOptions &ExistingLangOpts,
287 StringRef ModuleFilename,
288 DiagnosticsEngine *Diags,
289 bool AllowCompatibleDifferences = true) {
290 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
291 using CK = LangOptions::CompatibilityKind;
292
293#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
294 if constexpr (CK::Compatibility != CK::Benign) { \
295 if ((CK::Compatibility == CK::NotCompatible) || \
296 (CK::Compatibility == CK::Compatible && \
297 !AllowCompatibleDifferences)) { \
298 if (ExistingLangOpts.Name != LangOpts.Name) { \
299 if (Diags) { \
300 if (Bits == 1) \
301 Diags->Report(diag::err_ast_file_langopt_mismatch) \
302 << Description << LangOpts.Name << ExistingLangOpts.Name \
303 << ModuleFilename; \
304 else \
305 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
306 << Description << ModuleFilename; \
307 } \
308 return true; \
309 } \
310 } \
311 }
312
313#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
314 if constexpr (CK::Compatibility != CK::Benign) { \
315 if ((CK::Compatibility == CK::NotCompatible) || \
316 (CK::Compatibility == CK::Compatible && \
317 !AllowCompatibleDifferences)) { \
318 if (ExistingLangOpts.Name != LangOpts.Name) { \
319 if (Diags) \
320 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
321 << Description << ModuleFilename; \
322 return true; \
323 } \
324 } \
325 }
326
327#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
328 if constexpr (CK::Compatibility != CK::Benign) { \
329 if ((CK::Compatibility == CK::NotCompatible) || \
330 (CK::Compatibility == CK::Compatible && \
331 !AllowCompatibleDifferences)) { \
332 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
333 if (Diags) \
334 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
335 << Description << ModuleFilename; \
336 return true; \
337 } \
338 } \
339 }
340
341#include "clang/Basic/LangOptions.def"
342
343 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
344 if (Diags)
345 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
346 << "module features" << ModuleFilename;
347 return true;
348 }
349
350 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
351 if (Diags)
352 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
353 << "target Objective-C runtime" << ModuleFilename;
354 return true;
355 }
356
357 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
358 LangOpts.CommentOpts.BlockCommandNames) {
359 if (Diags)
360 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
361 << "block command names" << ModuleFilename;
362 return true;
363 }
364
365 // Sanitizer feature mismatches are treated as compatible differences. If
366 // compatible differences aren't allowed, we still only want to check for
367 // mismatches of non-modular sanitizers (the only ones which can affect AST
368 // generation).
369 if (!AllowCompatibleDifferences) {
370 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
371 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
372 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
373 ExistingSanitizers.clear(K: ModularSanitizers);
374 ImportedSanitizers.clear(K: ModularSanitizers);
375 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
376 const std::string Flag = "-fsanitize=";
377 if (Diags) {
378#define SANITIZER(NAME, ID) \
379 { \
380 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
381 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
382 if (InExistingModule != InImportedModule) \
383 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
384 << InExistingModule << ModuleFilename << (Flag + NAME); \
385 }
386#include "clang/Basic/Sanitizers.def"
387 }
388 return true;
389 }
390 }
391
392 return false;
393}
394
395static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
396 const CodeGenOptions &ExistingCGOpts,
397 StringRef ModuleFilename,
398 DiagnosticsEngine *Diags,
399 bool AllowCompatibleDifferences = true) {
400 // FIXME: Specify and print a description for each option instead of the name.
401 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
402 using CK = CodeGenOptions::CompatibilityKind;
403#define CODEGENOPT(Name, Bits, Default, Compatibility) \
404 if constexpr (CK::Compatibility != CK::Benign) { \
405 if ((CK::Compatibility == CK::NotCompatible) || \
406 (CK::Compatibility == CK::Compatible && \
407 !AllowCompatibleDifferences)) { \
408 if (ExistingCGOpts.Name != CGOpts.Name) { \
409 if (Diags) { \
410 if (Bits == 1) \
411 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
412 << #Name << CGOpts.Name << ExistingCGOpts.Name \
413 << ModuleFilename; \
414 else \
415 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
416 << #Name << ModuleFilename; \
417 } \
418 return true; \
419 } \
420 } \
421 }
422
423#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
424 if constexpr (CK::Compatibility != CK::Benign) { \
425 if ((CK::Compatibility == CK::NotCompatible) || \
426 (CK::Compatibility == CK::Compatible && \
427 !AllowCompatibleDifferences)) { \
428 if (ExistingCGOpts.Name != CGOpts.Name) { \
429 if (Diags) \
430 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
431 << #Name << ModuleFilename; \
432 return true; \
433 } \
434 } \
435 }
436#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
437 if constexpr (CK::Compatibility != CK::Benign) { \
438 if ((CK::Compatibility == CK::NotCompatible) || \
439 (CK::Compatibility == CK::Compatible && \
440 !AllowCompatibleDifferences)) { \
441 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
442 if (Diags) \
443 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
444 << #Name << ModuleFilename; \
445 return true; \
446 } \
447 } \
448 }
449#define DEBUGOPT(Name, Bits, Default, Compatibility)
450#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
451#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
452#include "clang/Basic/CodeGenOptions.def"
453
454 return false;
455}
456
457/// Compare the given set of target options against an existing set of
458/// target options.
459///
460/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
461///
462/// \returns true if the target options mis-match, false otherwise.
463static bool checkTargetOptions(const TargetOptions &TargetOpts,
464 const TargetOptions &ExistingTargetOpts,
465 StringRef ModuleFilename,
466 DiagnosticsEngine *Diags,
467 bool AllowCompatibleDifferences = true) {
468#define CHECK_TARGET_OPT(Field, Name) \
469 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
470 if (Diags) \
471 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
472 << ModuleFilename << Name << TargetOpts.Field \
473 << ExistingTargetOpts.Field; \
474 return true; \
475 }
476
477 // The triple and ABI must match exactly.
478 CHECK_TARGET_OPT(Triple, "target");
479 CHECK_TARGET_OPT(ABI, "target ABI");
480
481 // We can tolerate different CPUs in many cases, notably when one CPU
482 // supports a strict superset of another. When allowing compatible
483 // differences skip this check.
484 if (!AllowCompatibleDifferences) {
485 CHECK_TARGET_OPT(CPU, "target CPU");
486 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
487 }
488
489#undef CHECK_TARGET_OPT
490
491 // Compare feature sets.
492 SmallVector<StringRef, 4> ExistingFeatures(
493 ExistingTargetOpts.FeaturesAsWritten.begin(),
494 ExistingTargetOpts.FeaturesAsWritten.end());
495 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
496 TargetOpts.FeaturesAsWritten.end());
497 llvm::sort(C&: ExistingFeatures);
498 llvm::sort(C&: ReadFeatures);
499
500 // We compute the set difference in both directions explicitly so that we can
501 // diagnose the differences differently.
502 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
503 std::set_difference(
504 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
505 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
506 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
507 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
508 result: std::back_inserter(x&: UnmatchedReadFeatures));
509
510 // If we are allowing compatible differences and the read feature set is
511 // a strict subset of the existing feature set, there is nothing to diagnose.
512 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
513 return false;
514
515 if (Diags) {
516 for (StringRef Feature : UnmatchedReadFeatures)
517 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
518 << /* is-existing-feature */ false << ModuleFilename << Feature;
519 for (StringRef Feature : UnmatchedExistingFeatures)
520 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
521 << /* is-existing-feature */ true << ModuleFilename << Feature;
522 }
523
524 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
525}
526
527bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
528 StringRef ModuleFilename, bool Complain,
529 bool AllowCompatibleDifferences) {
530 const LangOptions &ExistingLangOpts = PP.getLangOpts();
531 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
532 Diags: Complain ? &Reader.Diags : nullptr,
533 AllowCompatibleDifferences);
534}
535
536bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
537 StringRef ModuleFilename, bool Complain,
538 bool AllowCompatibleDifferences) {
539 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
540 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
541 Diags: Complain ? &Reader.Diags : nullptr,
542 AllowCompatibleDifferences);
543}
544
545bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
546 StringRef ModuleFilename, bool Complain,
547 bool AllowCompatibleDifferences) {
548 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
549 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
550 Diags: Complain ? &Reader.Diags : nullptr,
551 AllowCompatibleDifferences);
552}
553
554namespace {
555
556using MacroDefinitionsMap =
557 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
558
559class DeclsSet {
560 SmallVector<NamedDecl *, 64> Decls;
561 llvm::SmallPtrSet<NamedDecl *, 8> Found;
562
563public:
564 operator ArrayRef<NamedDecl *>() const { return Decls; }
565
566 bool empty() const { return Decls.empty(); }
567
568 bool insert(NamedDecl *ND) {
569 auto [_, Inserted] = Found.insert(Ptr: ND);
570 if (Inserted)
571 Decls.push_back(Elt: ND);
572 return Inserted;
573 }
574};
575
576using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
577
578} // namespace
579
580static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
581 DiagnosticsEngine &Diags,
582 StringRef ModuleFilename,
583 bool Complain) {
584 using Level = DiagnosticsEngine::Level;
585
586 // Check current mappings for new -Werror mappings, and the stored mappings
587 // for cases that were explicitly mapped to *not* be errors that are now
588 // errors because of options like -Werror.
589 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
590
591 for (DiagnosticsEngine *MappingSource : MappingSources) {
592 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
593 diag::kind DiagID = DiagIDMappingPair.first;
594 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
595 if (CurLevel < DiagnosticsEngine::Error)
596 continue; // not significant
597 Level StoredLevel =
598 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
599 if (StoredLevel < DiagnosticsEngine::Error) {
600 if (Complain)
601 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
602 << "-Werror=" + Diags.getDiagnosticIDs()
603 ->getWarningOptionForDiag(DiagID)
604 .str()
605 << ModuleFilename;
606 return true;
607 }
608 }
609 }
610
611 return false;
612}
613
614static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
615 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
616 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
617 return true;
618 return Ext >= diag::Severity::Error;
619}
620
621static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
622 DiagnosticsEngine &Diags,
623 StringRef ModuleFilename, bool IsSystem,
624 bool SystemHeaderWarningsInModule,
625 bool Complain) {
626 // Top-level options
627 if (IsSystem) {
628 if (Diags.getSuppressSystemWarnings())
629 return false;
630 // If -Wsystem-headers was not enabled before, and it was not explicit,
631 // be conservative
632 if (StoredDiags.getSuppressSystemWarnings() &&
633 !SystemHeaderWarningsInModule) {
634 if (Complain)
635 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
636 << "-Wsystem-headers" << ModuleFilename;
637 return true;
638 }
639 }
640
641 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
642 if (Complain)
643 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
644 << "-Werror" << ModuleFilename;
645 return true;
646 }
647
648 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
649 !StoredDiags.getEnableAllWarnings()) {
650 if (Complain)
651 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
652 << "-Weverything -Werror" << ModuleFilename;
653 return true;
654 }
655
656 if (isExtHandlingFromDiagsError(Diags) &&
657 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
658 if (Complain)
659 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
660 << "-pedantic-errors" << ModuleFilename;
661 return true;
662 }
663
664 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
665 Complain);
666}
667
668/// Return the top import module if it is implicit, nullptr otherwise.
669static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
670 Preprocessor &PP) {
671 // If the original import came from a file explicitly generated by the user,
672 // don't check the diagnostic mappings.
673 // FIXME: currently this is approximated by checking whether this is not a
674 // module import of an implicitly-loaded module file.
675 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
676 // the transitive closure of its imports, since unrelated modules cannot be
677 // imported until after this module finishes validation.
678 ModuleFile *TopImport = &*ModuleMgr.rbegin();
679 while (!TopImport->ImportedBy.empty())
680 TopImport = TopImport->ImportedBy[0];
681 if (TopImport->Kind != MK_ImplicitModule)
682 return nullptr;
683
684 StringRef ModuleName = TopImport->ModuleName;
685 assert(!ModuleName.empty() && "diagnostic options read before module name");
686
687 Module *M =
688 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
689 assert(M && "missing module");
690 return M;
691}
692
693bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
694 StringRef ModuleFilename,
695 bool Complain) {
696 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
697 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
698 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
699 // This should never fail, because we would have processed these options
700 // before writing them to an ASTFile.
701 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
702 VFS&: PP.getFileManager().getVirtualFileSystem(),
703 /*Report*/ ReportDiags: false);
704
705 ModuleManager &ModuleMgr = Reader.getModuleManager();
706 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
707
708 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
709 if (!TopM)
710 return false;
711
712 Module *Importer = PP.getCurrentModule();
713
714 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
715 bool SystemHeaderWarningsInModule =
716 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
717 Element: Importer->Name);
718
719 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
720 // contains the union of their flags.
721 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
722 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
723 Complain);
724}
725
726/// Collect the macro definitions provided by the given preprocessor
727/// options.
728static void
729collectMacroDefinitions(const PreprocessorOptions &PPOpts,
730 MacroDefinitionsMap &Macros,
731 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
732 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
733 StringRef Macro = PPOpts.Macros[I].first;
734 bool IsUndef = PPOpts.Macros[I].second;
735
736 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
737 StringRef MacroName = MacroPair.first;
738 StringRef MacroBody = MacroPair.second;
739
740 // For an #undef'd macro, we only care about the name.
741 if (IsUndef) {
742 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
743 if (MacroNames && Inserted)
744 MacroNames->push_back(Elt: MacroName);
745
746 It->second = std::make_pair(x: "", y: true);
747 continue;
748 }
749
750 // For a #define'd macro, figure out the actual definition.
751 if (MacroName.size() == Macro.size())
752 MacroBody = "1";
753 else {
754 // Note: GCC drops anything following an end-of-line character.
755 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
756 MacroBody = MacroBody.substr(Start: 0, N: End);
757 }
758
759 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
760 if (MacroNames && Inserted)
761 MacroNames->push_back(Elt: MacroName);
762 It->second = std::make_pair(x&: MacroBody, y: false);
763 }
764}
765
766enum OptionValidation {
767 OptionValidateNone,
768 OptionValidateContradictions,
769 OptionValidateStrictMatches,
770};
771
772/// Check the preprocessor options deserialized from the control block
773/// against the preprocessor options in an existing preprocessor.
774///
775/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
776/// \param Validation If set to OptionValidateNone, ignore differences in
777/// preprocessor options. If set to OptionValidateContradictions,
778/// require that options passed both in the AST file and on the command
779/// line (-D or -U) match, but tolerate options missing in one or the
780/// other. If set to OptionValidateContradictions, require that there
781/// are no differences in the options between the two.
782static bool checkPreprocessorOptions(
783 const PreprocessorOptions &PPOpts,
784 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
785 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
786 std::string &SuggestedPredefines, const LangOptions &LangOpts,
787 OptionValidation Validation = OptionValidateContradictions) {
788 if (ReadMacros) {
789 // Check macro definitions.
790 MacroDefinitionsMap ASTFileMacros;
791 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
792 MacroDefinitionsMap ExistingMacros;
793 SmallVector<StringRef, 4> ExistingMacroNames;
794 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
795 MacroNames: &ExistingMacroNames);
796
797 // Use a line marker to enter the <command line> file, as the defines and
798 // undefines here will have come from the command line.
799 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
800
801 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
802 // Dig out the macro definition in the existing preprocessor options.
803 StringRef MacroName = ExistingMacroNames[I];
804 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
805
806 // Check whether we know anything about this macro name or not.
807 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
808 ASTFileMacros.find(Key: MacroName);
809 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
810 if (Validation == OptionValidateStrictMatches) {
811 // If strict matches are requested, don't tolerate any extra defines
812 // on the command line that are missing in the AST file.
813 if (Diags) {
814 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
815 << MacroName << true << ModuleFilename;
816 }
817 return true;
818 }
819 // FIXME: Check whether this identifier was referenced anywhere in the
820 // AST file. If so, we should reject the AST file. Unfortunately, this
821 // information isn't in the control block. What shall we do about it?
822
823 if (Existing.second) {
824 SuggestedPredefines += "#undef ";
825 SuggestedPredefines += MacroName.str();
826 SuggestedPredefines += '\n';
827 } else {
828 SuggestedPredefines += "#define ";
829 SuggestedPredefines += MacroName.str();
830 SuggestedPredefines += ' ';
831 SuggestedPredefines += Existing.first.str();
832 SuggestedPredefines += '\n';
833 }
834 continue;
835 }
836
837 // If the macro was defined in one but undef'd in the other, we have a
838 // conflict.
839 if (Existing.second != Known->second.second) {
840 if (Diags) {
841 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
842 << MacroName << Known->second.second << ModuleFilename;
843 }
844 return true;
845 }
846
847 // If the macro was #undef'd in both, or if the macro bodies are
848 // identical, it's fine.
849 if (Existing.second || Existing.first == Known->second.first) {
850 ASTFileMacros.erase(I: Known);
851 continue;
852 }
853
854 // The macro bodies differ; complain.
855 if (Diags) {
856 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
857 << MacroName << Known->second.first << Existing.first
858 << ModuleFilename;
859 }
860 return true;
861 }
862
863 // Leave the <command line> file and return to <built-in>.
864 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
865
866 if (Validation == OptionValidateStrictMatches) {
867 // If strict matches are requested, don't tolerate any extra defines in
868 // the AST file that are missing on the command line.
869 for (const auto &MacroName : ASTFileMacros.keys()) {
870 if (Diags) {
871 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
872 << MacroName << false << ModuleFilename;
873 }
874 return true;
875 }
876 }
877 }
878
879 // Check whether we're using predefines.
880 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
881 Validation != OptionValidateNone) {
882 if (Diags) {
883 Diags->Report(DiagID: diag::err_ast_file_undef)
884 << ExistingPPOpts.UsePredefines << ModuleFilename;
885 }
886 return true;
887 }
888
889 // Detailed record is important since it is used for the module cache hash.
890 if (LangOpts.Modules &&
891 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
892 Validation != OptionValidateNone) {
893 if (Diags) {
894 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
895 << PPOpts.DetailedRecord << ModuleFilename;
896 }
897 return true;
898 }
899
900 // Compute the #include and #include_macros lines we need.
901 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
902 StringRef File = ExistingPPOpts.Includes[I];
903
904 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
905 !ExistingPPOpts.PCHThroughHeader.empty()) {
906 // In case the through header is an include, we must add all the includes
907 // to the predefines so the start point can be determined.
908 SuggestedPredefines += "#include \"";
909 SuggestedPredefines += File;
910 SuggestedPredefines += "\"\n";
911 continue;
912 }
913
914 if (File == ExistingPPOpts.ImplicitPCHInclude)
915 continue;
916
917 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
918 continue;
919
920 SuggestedPredefines += "#include \"";
921 SuggestedPredefines += File;
922 SuggestedPredefines += "\"\n";
923 }
924
925 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
926 StringRef File = ExistingPPOpts.MacroIncludes[I];
927 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
928 continue;
929
930 SuggestedPredefines += "#__include_macros \"";
931 SuggestedPredefines += File;
932 SuggestedPredefines += "\"\n##\n";
933 }
934
935 return false;
936}
937
938bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
939 StringRef ModuleFilename,
940 bool ReadMacros, bool Complain,
941 std::string &SuggestedPredefines) {
942 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
943
944 return checkPreprocessorOptions(
945 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
946 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
947 SuggestedPredefines, LangOpts: PP.getLangOpts());
948}
949
950bool SimpleASTReaderListener::ReadPreprocessorOptions(
951 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
952 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
953 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
954 ModuleFilename, ReadMacros, Diags: nullptr,
955 FileMgr&: PP.getFileManager(), SuggestedPredefines,
956 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
957}
958
959/// Check that the specified and the existing module cache paths are equivalent.
960///
961/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
962/// \returns true when the module cache paths differ.
963static bool checkModuleCachePath(
964 llvm::vfs::FileSystem &VFS, StringRef SpecificModuleCachePath,
965 StringRef ExistingSpecificModuleCachePath, StringRef ASTFilename,
966 DiagnosticsEngine *Diags, const LangOptions &LangOpts,
967 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
968 const HeaderSearchOptions &ASTFileHSOpts) {
969 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
970 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
971 return false;
972 auto EqualOrErr =
973 VFS.equivalent(A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
974 if (EqualOrErr && *EqualOrErr)
975 return false;
976 if (Diags) {
977 // If the module cache arguments provided from the command line are the
978 // same, the mismatch must come from other arguments of the configuration
979 // and not directly the cache path.
980 EqualOrErr =
981 VFS.equivalent(A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
982 if (EqualOrErr && *EqualOrErr)
983 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
984 else
985 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
986 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
987 << ASTFilename;
988 }
989 return true;
990}
991
992bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
993 StringRef ASTFilename,
994 StringRef SpecificModuleCachePath,
995 bool Complain) {
996 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
997 return checkModuleCachePath(
998 VFS&: Reader.getFileManager().getVirtualFileSystem(), SpecificModuleCachePath,
999 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(), ASTFilename,
1000 Diags: Complain ? &Reader.Diags : nullptr, LangOpts: PP.getLangOpts(),
1001 PPOpts: PP.getPreprocessorOpts(), HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1002}
1003
1004void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1005 PP.setCounterValue(Value);
1006}
1007
1008//===----------------------------------------------------------------------===//
1009// AST reader implementation
1010//===----------------------------------------------------------------------===//
1011
1012static uint64_t readULEB(const unsigned char *&P) {
1013 unsigned Length = 0;
1014 const char *Error = nullptr;
1015
1016 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1017 if (Error)
1018 llvm::report_fatal_error(reason: Error);
1019 P += Length;
1020 return Val;
1021}
1022
1023/// Read ULEB-encoded key length and data length.
1024static std::pair<unsigned, unsigned>
1025readULEBKeyDataLength(const unsigned char *&P) {
1026 unsigned KeyLen = readULEB(P);
1027 if ((unsigned)KeyLen != KeyLen)
1028 llvm::report_fatal_error(reason: "key too large");
1029
1030 unsigned DataLen = readULEB(P);
1031 if ((unsigned)DataLen != DataLen)
1032 llvm::report_fatal_error(reason: "data too large");
1033
1034 return std::make_pair(x&: KeyLen, y&: DataLen);
1035}
1036
1037void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1038 bool TakeOwnership) {
1039 DeserializationListener = Listener;
1040 OwnsDeserializationListener = TakeOwnership;
1041}
1042
1043unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1044 return serialization::ComputeHash(Sel);
1045}
1046
1047LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1048 LocalDeclID ID(Value);
1049#ifndef NDEBUG
1050 if (!MF.ModuleOffsetMap.empty())
1051 Reader.ReadModuleOffsetMap(MF);
1052
1053 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1054 unsigned LocalDeclID = ID.getLocalDeclIndex();
1055
1056 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1057
1058 ModuleFile *OwningModuleFile =
1059 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1060 assert(OwningModuleFile);
1061
1062 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1063
1064 if (!ModuleFileIndex)
1065 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1066
1067 assert(LocalDeclID < LocalNumDecls);
1068#endif
1069 (void)Reader;
1070 (void)MF;
1071 return ID;
1072}
1073
1074LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1075 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1076 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1077 return LocalDeclID::get(Reader, MF, Value);
1078}
1079
1080std::pair<unsigned, unsigned>
1081ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1082 return readULEBKeyDataLength(P&: d);
1083}
1084
1085ASTSelectorLookupTrait::internal_key_type
1086ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1087 using namespace llvm::support;
1088
1089 SelectorTable &SelTable = Reader.getContext().Selectors;
1090 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1091 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1092 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1093 if (N == 0)
1094 return SelTable.getNullarySelector(ID: FirstII);
1095 else if (N == 1)
1096 return SelTable.getUnarySelector(ID: FirstII);
1097
1098 SmallVector<const IdentifierInfo *, 16> Args;
1099 Args.push_back(Elt: FirstII);
1100 for (unsigned I = 1; I != N; ++I)
1101 Args.push_back(Elt: Reader.getLocalIdentifier(
1102 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1103
1104 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1105}
1106
1107ASTSelectorLookupTrait::data_type
1108ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1109 unsigned DataLen) {
1110 using namespace llvm::support;
1111
1112 data_type Result;
1113
1114 Result.ID = Reader.getGlobalSelectorID(
1115 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1116 unsigned FullInstanceBits =
1117 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1118 unsigned FullFactoryBits =
1119 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1120 Result.InstanceBits = FullInstanceBits & 0x3;
1121 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1122 Result.FactoryBits = FullFactoryBits & 0x3;
1123 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1124 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1125 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1126
1127 // Load instance methods
1128 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1129 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1130 F, LocalID: LocalDeclID::get(
1131 Reader, MF&: F,
1132 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1133 Result.Instance.push_back(Elt: Method);
1134 }
1135
1136 // Load factory methods
1137 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1138 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1139 F, LocalID: LocalDeclID::get(
1140 Reader, MF&: F,
1141 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1142 Result.Factory.push_back(Elt: Method);
1143 }
1144
1145 return Result;
1146}
1147
1148unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1149 return llvm::djbHash(Buffer: a);
1150}
1151
1152std::pair<unsigned, unsigned>
1153ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1154 return readULEBKeyDataLength(P&: d);
1155}
1156
1157ASTIdentifierLookupTraitBase::internal_key_type
1158ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1159 assert(n >= 2 && d[n-1] == '\0');
1160 return StringRef((const char*) d, n-1);
1161}
1162
1163/// Whether the given identifier is "interesting".
1164static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1165 bool IsModule) {
1166 bool IsInteresting =
1167 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1168 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1169 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1170 return II.hadMacroDefinition() || II.isPoisoned() ||
1171 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1172 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1173 II.getFETokenInfo());
1174}
1175
1176static bool readBit(unsigned &Bits) {
1177 bool Value = Bits & 0x1;
1178 Bits >>= 1;
1179 return Value;
1180}
1181
1182IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1183 using namespace llvm::support;
1184
1185 IdentifierID RawID =
1186 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1187 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1188}
1189
1190static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1191 bool IsModule) {
1192 if (!II.isFromAST()) {
1193 II.setIsFromAST();
1194 if (isInterestingIdentifier(Reader, II, IsModule))
1195 II.setChangedSinceDeserialization();
1196 }
1197}
1198
1199IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1200 const unsigned char* d,
1201 unsigned DataLen) {
1202 using namespace llvm::support;
1203
1204 IdentifierID RawID =
1205 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1206 bool IsInteresting = RawID & 0x01;
1207
1208 DataLen -= sizeof(IdentifierID);
1209
1210 // Wipe out the "is interesting" bit.
1211 RawID = RawID >> 1;
1212
1213 // Build the IdentifierInfo and link the identifier ID with it.
1214 IdentifierInfo *II = KnownII;
1215 if (!II) {
1216 II = &Reader.getIdentifierTable().getOwn(Name: k);
1217 KnownII = II;
1218 }
1219 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1220 markIdentifierFromAST(Reader, II&: *II, IsModule);
1221 Reader.markIdentifierUpToDate(II);
1222
1223 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1224 if (!IsInteresting) {
1225 // For uninteresting identifiers, there's nothing else to do. Just notify
1226 // the reader that we've finished loading this identifier.
1227 Reader.SetIdentifierInfo(ID, II);
1228 return II;
1229 }
1230
1231 unsigned ObjCOrBuiltinID =
1232 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1233 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1234 bool CPlusPlusOperatorKeyword = readBit(Bits);
1235 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1236 bool Poisoned = readBit(Bits);
1237 bool ExtensionToken = readBit(Bits);
1238 bool HasMacroDefinition = readBit(Bits);
1239
1240 assert(Bits == 0 && "Extra bits in the identifier?");
1241 DataLen -= sizeof(uint16_t) * 2;
1242
1243 // Set or check the various bits in the IdentifierInfo structure.
1244 // Token IDs are read-only.
1245 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1246 II->revertTokenIDToIdentifier();
1247 if (!F.isModule())
1248 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1249 assert(II->isExtensionToken() == ExtensionToken &&
1250 "Incorrect extension token flag");
1251 (void)ExtensionToken;
1252 if (Poisoned)
1253 II->setIsPoisoned(true);
1254 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1255 "Incorrect C++ operator keyword flag");
1256 (void)CPlusPlusOperatorKeyword;
1257
1258 // If this identifier has a macro definition, deserialize it or notify the
1259 // visitor the actual definition is in a different module.
1260 if (HasMacroDefinition) {
1261 uint32_t MacroDirectivesOffset =
1262 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1263 DataLen -= 4;
1264
1265 if (MacroDirectivesOffset)
1266 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1267 else
1268 hasMacroDefinitionInDependencies = true;
1269 }
1270
1271 Reader.SetIdentifierInfo(ID, II);
1272
1273 // Read all of the declarations visible at global scope with this
1274 // name.
1275 if (DataLen > 0) {
1276 SmallVector<GlobalDeclID, 4> DeclIDs;
1277 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1278 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1279 F, LocalID: LocalDeclID::get(
1280 Reader, MF&: F,
1281 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1282 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1283 }
1284
1285 return II;
1286}
1287
1288DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1289 : Kind(Name.getNameKind()) {
1290 switch (Kind) {
1291 case DeclarationName::Identifier:
1292 Data = (uint64_t)Name.getAsIdentifierInfo();
1293 break;
1294 case DeclarationName::ObjCZeroArgSelector:
1295 case DeclarationName::ObjCOneArgSelector:
1296 case DeclarationName::ObjCMultiArgSelector:
1297 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1298 break;
1299 case DeclarationName::CXXOperatorName:
1300 Data = Name.getCXXOverloadedOperator();
1301 break;
1302 case DeclarationName::CXXLiteralOperatorName:
1303 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1304 break;
1305 case DeclarationName::CXXDeductionGuideName:
1306 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1307 ->getDeclName().getAsIdentifierInfo();
1308 break;
1309 case DeclarationName::CXXConstructorName:
1310 case DeclarationName::CXXDestructorName:
1311 case DeclarationName::CXXConversionFunctionName:
1312 case DeclarationName::CXXUsingDirective:
1313 Data = 0;
1314 break;
1315 }
1316}
1317
1318unsigned DeclarationNameKey::getHash() const {
1319 llvm::FoldingSetNodeID ID;
1320 ID.AddInteger(I: Kind);
1321
1322 switch (Kind) {
1323 case DeclarationName::Identifier:
1324 case DeclarationName::CXXLiteralOperatorName:
1325 case DeclarationName::CXXDeductionGuideName:
1326 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1327 break;
1328 case DeclarationName::ObjCZeroArgSelector:
1329 case DeclarationName::ObjCOneArgSelector:
1330 case DeclarationName::ObjCMultiArgSelector:
1331 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1332 break;
1333 case DeclarationName::CXXOperatorName:
1334 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1335 break;
1336 case DeclarationName::CXXConstructorName:
1337 case DeclarationName::CXXDestructorName:
1338 case DeclarationName::CXXConversionFunctionName:
1339 case DeclarationName::CXXUsingDirective:
1340 break;
1341 }
1342
1343 return ID.computeStableHash();
1344}
1345
1346ModuleFile *
1347ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1348 using namespace llvm::support;
1349
1350 uint32_t ModuleFileID =
1351 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1352 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1353}
1354
1355std::pair<unsigned, unsigned>
1356ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1357 return readULEBKeyDataLength(P&: d);
1358}
1359
1360DeclarationNameKey
1361ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1362 using namespace llvm::support;
1363
1364 auto Kind = (DeclarationName::NameKind)*d++;
1365 uint64_t Data;
1366 switch (Kind) {
1367 case DeclarationName::Identifier:
1368 case DeclarationName::CXXLiteralOperatorName:
1369 case DeclarationName::CXXDeductionGuideName:
1370 Data = (uint64_t)Reader.getLocalIdentifier(
1371 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1372 break;
1373 case DeclarationName::ObjCZeroArgSelector:
1374 case DeclarationName::ObjCOneArgSelector:
1375 case DeclarationName::ObjCMultiArgSelector:
1376 Data = (uint64_t)Reader
1377 .getLocalSelector(
1378 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1379 .getAsOpaquePtr();
1380 break;
1381 case DeclarationName::CXXOperatorName:
1382 Data = *d++; // OverloadedOperatorKind
1383 break;
1384 case DeclarationName::CXXConstructorName:
1385 case DeclarationName::CXXDestructorName:
1386 case DeclarationName::CXXConversionFunctionName:
1387 case DeclarationName::CXXUsingDirective:
1388 Data = 0;
1389 break;
1390 }
1391
1392 return DeclarationNameKey(Kind, Data);
1393}
1394
1395ASTDeclContextNameLookupTrait::internal_key_type
1396ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1397 return ReadKeyBase(d);
1398}
1399
1400void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1401 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1402 using namespace llvm::support;
1403
1404 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1405 LocalDeclID ID = LocalDeclID::get(
1406 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1407 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1408 }
1409}
1410
1411void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1412 const unsigned char *d,
1413 unsigned DataLen,
1414 data_type_builder &Val) {
1415 ReadDataIntoImpl(d, DataLen, Val);
1416}
1417
1418ModuleLocalNameLookupTrait::hash_value_type
1419ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1420 llvm::FoldingSetNodeID ID;
1421 ID.AddInteger(I: Key.first.getHash());
1422 ID.AddInteger(I: Key.second);
1423 return ID.computeStableHash();
1424}
1425
1426ModuleLocalNameLookupTrait::internal_key_type
1427ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1428 DeclarationNameKey Name(Key.first);
1429
1430 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1431 if (!ModuleHash)
1432 return {Name, 0};
1433
1434 return {Name, *ModuleHash};
1435}
1436
1437ModuleLocalNameLookupTrait::internal_key_type
1438ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1439 DeclarationNameKey Name = ReadKeyBase(d);
1440 unsigned PrimaryModuleHash =
1441 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1442 return {Name, PrimaryModuleHash};
1443}
1444
1445void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1446 const unsigned char *d,
1447 unsigned DataLen,
1448 data_type_builder &Val) {
1449 ReadDataIntoImpl(d, DataLen, Val);
1450}
1451
1452ModuleFile *
1453LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1454 using namespace llvm::support;
1455
1456 uint32_t ModuleFileID =
1457 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1458 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1459}
1460
1461LazySpecializationInfoLookupTrait::internal_key_type
1462LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1463 using namespace llvm::support;
1464 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1465}
1466
1467std::pair<unsigned, unsigned>
1468LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1469 return readULEBKeyDataLength(P&: d);
1470}
1471
1472void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1473 const unsigned char *d,
1474 unsigned DataLen,
1475 data_type_builder &Val) {
1476 using namespace llvm::support;
1477
1478 for (unsigned NumDecls =
1479 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1480 NumDecls; --NumDecls) {
1481 LocalDeclID LocalID = LocalDeclID::get(
1482 Reader, MF&: F,
1483 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1484 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1485 }
1486}
1487
1488bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1489 BitstreamCursor &Cursor,
1490 uint64_t Offset,
1491 DeclContext *DC) {
1492 assert(Offset != 0);
1493
1494 SavedStreamPosition SavedPosition(Cursor);
1495 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1496 Error(Err: std::move(Err));
1497 return true;
1498 }
1499
1500 RecordData Record;
1501 StringRef Blob;
1502 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1503 if (!MaybeCode) {
1504 Error(Err: MaybeCode.takeError());
1505 return true;
1506 }
1507 unsigned Code = MaybeCode.get();
1508
1509 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1510 if (!MaybeRecCode) {
1511 Error(Err: MaybeRecCode.takeError());
1512 return true;
1513 }
1514 unsigned RecCode = MaybeRecCode.get();
1515 if (RecCode != DECL_CONTEXT_LEXICAL) {
1516 Error(Msg: "Expected lexical block");
1517 return true;
1518 }
1519
1520 assert(!isa<TranslationUnitDecl>(DC) &&
1521 "expected a TU_UPDATE_LEXICAL record for TU");
1522 // If we are handling a C++ class template instantiation, we can see multiple
1523 // lexical updates for the same record. It's important that we select only one
1524 // of them, so that field numbering works properly. Just pick the first one we
1525 // see.
1526 auto &Lex = LexicalDecls[DC];
1527 if (!Lex.first) {
1528 Lex = std::make_pair(
1529 x: &M, y: llvm::ArrayRef(
1530 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1531 Blob.size() / sizeof(DeclID)));
1532 }
1533 DC->setHasExternalLexicalStorage(true);
1534 return false;
1535}
1536
1537bool ASTReader::ReadVisibleDeclContextStorage(
1538 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1539 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1540 assert(Offset != 0);
1541
1542 SavedStreamPosition SavedPosition(Cursor);
1543 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1544 Error(Err: std::move(Err));
1545 return true;
1546 }
1547
1548 RecordData Record;
1549 StringRef Blob;
1550 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1551 if (!MaybeCode) {
1552 Error(Err: MaybeCode.takeError());
1553 return true;
1554 }
1555 unsigned Code = MaybeCode.get();
1556
1557 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1558 if (!MaybeRecCode) {
1559 Error(Err: MaybeRecCode.takeError());
1560 return true;
1561 }
1562 unsigned RecCode = MaybeRecCode.get();
1563 switch (VisibleKind) {
1564 case VisibleDeclContextStorageKind::GenerallyVisible:
1565 if (RecCode != DECL_CONTEXT_VISIBLE) {
1566 Error(Msg: "Expected visible lookup table block");
1567 return true;
1568 }
1569 break;
1570 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1571 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1572 Error(Msg: "Expected module local visible lookup table block");
1573 return true;
1574 }
1575 break;
1576 case VisibleDeclContextStorageKind::TULocalVisible:
1577 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1578 Error(Msg: "Expected TU local lookup table block");
1579 return true;
1580 }
1581 break;
1582 }
1583
1584 // We can't safely determine the primary context yet, so delay attaching the
1585 // lookup table until we're done with recursive deserialization.
1586 auto *Data = (const unsigned char*)Blob.data();
1587 switch (VisibleKind) {
1588 case VisibleDeclContextStorageKind::GenerallyVisible:
1589 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1590 break;
1591 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1592 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1593 break;
1594 case VisibleDeclContextStorageKind::TULocalVisible:
1595 if (M.Kind == MK_MainFile)
1596 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1597 break;
1598 }
1599 return false;
1600}
1601
1602void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1603 ModuleFile &M, bool IsPartial) {
1604 D = D->getCanonicalDecl();
1605 auto &SpecLookups =
1606 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1607 SpecLookups[D].Table.add(File: &M, Data,
1608 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1609}
1610
1611bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1612 uint64_t Offset, Decl *D, bool IsPartial) {
1613 assert(Offset != 0);
1614
1615 SavedStreamPosition SavedPosition(Cursor);
1616 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1617 Error(Err: std::move(Err));
1618 return true;
1619 }
1620
1621 RecordData Record;
1622 StringRef Blob;
1623 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1624 if (!MaybeCode) {
1625 Error(Err: MaybeCode.takeError());
1626 return true;
1627 }
1628 unsigned Code = MaybeCode.get();
1629
1630 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1631 if (!MaybeRecCode) {
1632 Error(Err: MaybeRecCode.takeError());
1633 return true;
1634 }
1635 unsigned RecCode = MaybeRecCode.get();
1636 if (RecCode != DECL_SPECIALIZATIONS &&
1637 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1638 Error(Msg: "Expected decl specs block");
1639 return true;
1640 }
1641
1642 auto *Data = (const unsigned char *)Blob.data();
1643 AddSpecializations(D, Data, M, IsPartial);
1644 return false;
1645}
1646
1647void ASTReader::Error(StringRef Msg) const {
1648 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1649 if (PP.getLangOpts().Modules &&
1650 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1651 Diag(DiagID: diag::note_module_cache_path)
1652 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1653 }
1654}
1655
1656void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1657 StringRef Arg3) const {
1658 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1659}
1660
1661namespace {
1662struct AlreadyReportedDiagnosticError
1663 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1664 static char ID;
1665
1666 void log(raw_ostream &OS) const override {
1667 llvm_unreachable("reporting an already-reported diagnostic error");
1668 }
1669
1670 std::error_code convertToErrorCode() const override {
1671 return llvm::inconvertibleErrorCode();
1672 }
1673};
1674
1675char AlreadyReportedDiagnosticError::ID = 0;
1676} // namespace
1677
1678void ASTReader::Error(llvm::Error &&Err) const {
1679 handleAllErrors(
1680 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1681 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1682}
1683
1684//===----------------------------------------------------------------------===//
1685// Source Manager Deserialization
1686//===----------------------------------------------------------------------===//
1687
1688/// Read the line table in the source manager block.
1689void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1690 unsigned Idx = 0;
1691 LineTableInfo &LineTable = SourceMgr.getLineTable();
1692
1693 // Parse the file names
1694 std::map<int, int> FileIDs;
1695 FileIDs[-1] = -1; // For unspecified filenames.
1696 for (unsigned I = 0; Record[Idx]; ++I) {
1697 // Extract the file name
1698 auto Filename = ReadPath(F, Record, Idx);
1699 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1700 }
1701 ++Idx;
1702
1703 // Parse the line entries
1704 std::vector<LineEntry> Entries;
1705 while (Idx < Record.size()) {
1706 FileID FID = ReadFileID(F, Record, Idx);
1707
1708 // Extract the line entries
1709 unsigned NumEntries = Record[Idx++];
1710 assert(NumEntries && "no line entries for file ID");
1711 Entries.clear();
1712 Entries.reserve(n: NumEntries);
1713 for (unsigned I = 0; I != NumEntries; ++I) {
1714 unsigned FileOffset = Record[Idx++];
1715 unsigned LineNo = Record[Idx++];
1716 int FilenameID = FileIDs[Record[Idx++]];
1717 SrcMgr::CharacteristicKind FileKind
1718 = (SrcMgr::CharacteristicKind)Record[Idx++];
1719 unsigned IncludeOffset = Record[Idx++];
1720 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1721 FileKind, IncludeOffset));
1722 }
1723 LineTable.AddEntry(FID, Entries);
1724 }
1725}
1726
1727/// Read a source manager block
1728llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1729 using namespace SrcMgr;
1730
1731 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1732
1733 // Set the source-location entry cursor to the current position in
1734 // the stream. This cursor will be used to read the contents of the
1735 // source manager block initially, and then lazily read
1736 // source-location entries as needed.
1737 SLocEntryCursor = F.Stream;
1738
1739 // The stream itself is going to skip over the source manager block.
1740 if (llvm::Error Err = F.Stream.SkipBlock())
1741 return Err;
1742
1743 // Enter the source manager block.
1744 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1745 return Err;
1746 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1747
1748 RecordData Record;
1749 while (true) {
1750 Expected<llvm::BitstreamEntry> MaybeE =
1751 SLocEntryCursor.advanceSkippingSubblocks();
1752 if (!MaybeE)
1753 return MaybeE.takeError();
1754 llvm::BitstreamEntry E = MaybeE.get();
1755
1756 switch (E.Kind) {
1757 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1758 case llvm::BitstreamEntry::Error:
1759 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1760 Fmt: "malformed block record in AST file");
1761 case llvm::BitstreamEntry::EndBlock:
1762 return llvm::Error::success();
1763 case llvm::BitstreamEntry::Record:
1764 // The interesting case.
1765 break;
1766 }
1767
1768 // Read a record.
1769 Record.clear();
1770 StringRef Blob;
1771 Expected<unsigned> MaybeRecord =
1772 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1773 if (!MaybeRecord)
1774 return MaybeRecord.takeError();
1775 switch (MaybeRecord.get()) {
1776 default: // Default behavior: ignore.
1777 break;
1778
1779 case SM_SLOC_FILE_ENTRY:
1780 case SM_SLOC_BUFFER_ENTRY:
1781 case SM_SLOC_EXPANSION_ENTRY:
1782 // Once we hit one of the source location entries, we're done.
1783 return llvm::Error::success();
1784 }
1785 }
1786}
1787
1788llvm::Expected<SourceLocation::UIntTy>
1789ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1790 BitstreamCursor &Cursor = F->SLocEntryCursor;
1791 SavedStreamPosition SavedPosition(Cursor);
1792 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1793 F->SLocEntryOffsets[Index]))
1794 return std::move(Err);
1795
1796 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1797 if (!MaybeEntry)
1798 return MaybeEntry.takeError();
1799
1800 llvm::BitstreamEntry Entry = MaybeEntry.get();
1801 if (Entry.Kind != llvm::BitstreamEntry::Record)
1802 return llvm::createStringError(
1803 EC: std::errc::illegal_byte_sequence,
1804 Fmt: "incorrectly-formatted source location entry in AST file");
1805
1806 RecordData Record;
1807 StringRef Blob;
1808 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1809 if (!MaybeSLOC)
1810 return MaybeSLOC.takeError();
1811
1812 switch (MaybeSLOC.get()) {
1813 default:
1814 return llvm::createStringError(
1815 EC: std::errc::illegal_byte_sequence,
1816 Fmt: "incorrectly-formatted source location entry in AST file");
1817 case SM_SLOC_FILE_ENTRY:
1818 case SM_SLOC_BUFFER_ENTRY:
1819 case SM_SLOC_EXPANSION_ENTRY:
1820 return F->SLocEntryBaseOffset + Record[0];
1821 }
1822}
1823
1824int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1825 auto SLocMapI =
1826 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1827 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1828 "Corrupted global sloc offset map");
1829 ModuleFile *F = SLocMapI->second;
1830
1831 bool Invalid = false;
1832
1833 auto It = llvm::upper_bound(
1834 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1835 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1836 int ID = F->SLocEntryBaseID + LocalIndex;
1837 std::size_t Index = -ID - 2;
1838 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1839 assert(!SourceMgr.SLocEntryLoaded[Index]);
1840 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1841 if (!MaybeEntryOffset) {
1842 Error(Err: MaybeEntryOffset.takeError());
1843 Invalid = true;
1844 return true;
1845 }
1846 SourceMgr.LoadedSLocEntryTable[Index] =
1847 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1848 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1849 }
1850 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1851 });
1852
1853 if (Invalid)
1854 return 0;
1855
1856 // The iterator points to the first entry with start offset greater than the
1857 // offset of interest. The previous entry must contain the offset of interest.
1858 return F->SLocEntryBaseID + *std::prev(x: It);
1859}
1860
1861bool ASTReader::ReadSLocEntry(int ID) {
1862 if (ID == 0)
1863 return false;
1864
1865 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1866 Error(Msg: "source location entry ID out-of-range for AST file");
1867 return true;
1868 }
1869
1870 // Local helper to read the (possibly-compressed) buffer data following the
1871 // entry record.
1872 auto ReadBuffer = [this](
1873 BitstreamCursor &SLocEntryCursor,
1874 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1875 RecordData Record;
1876 StringRef Blob;
1877 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1878 if (!MaybeCode) {
1879 Error(Err: MaybeCode.takeError());
1880 return nullptr;
1881 }
1882 unsigned Code = MaybeCode.get();
1883
1884 Expected<unsigned> MaybeRecCode =
1885 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1886 if (!MaybeRecCode) {
1887 Error(Err: MaybeRecCode.takeError());
1888 return nullptr;
1889 }
1890 unsigned RecCode = MaybeRecCode.get();
1891
1892 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1893 // Inspect the first byte to differentiate zlib (\x78) and zstd
1894 // (little-endian 0xFD2FB528).
1895 const llvm::compression::Format F =
1896 Blob.size() > 0 && Blob.data()[0] == 0x78
1897 ? llvm::compression::Format::Zlib
1898 : llvm::compression::Format::Zstd;
1899 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1900 Error(Msg: Reason);
1901 return nullptr;
1902 }
1903 SmallVector<uint8_t, 0> Decompressed;
1904 if (llvm::Error E = llvm::compression::decompress(
1905 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1906 Error(Msg: "could not decompress embedded file contents: " +
1907 llvm::toString(E: std::move(E)));
1908 return nullptr;
1909 }
1910 return llvm::MemoryBuffer::getMemBufferCopy(
1911 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1912 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1913 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1914 } else {
1915 Error(Msg: "AST record has invalid code");
1916 return nullptr;
1917 }
1918 };
1919
1920 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1921 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1922 BitNo: F->SLocEntryOffsetsBase +
1923 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1924 Error(Err: std::move(Err));
1925 return true;
1926 }
1927
1928 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1929 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1930
1931 ++NumSLocEntriesRead;
1932 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1933 if (!MaybeEntry) {
1934 Error(Err: MaybeEntry.takeError());
1935 return true;
1936 }
1937 llvm::BitstreamEntry Entry = MaybeEntry.get();
1938
1939 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1940 Error(Msg: "incorrectly-formatted source location entry in AST file");
1941 return true;
1942 }
1943
1944 RecordData Record;
1945 StringRef Blob;
1946 Expected<unsigned> MaybeSLOC =
1947 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1948 if (!MaybeSLOC) {
1949 Error(Err: MaybeSLOC.takeError());
1950 return true;
1951 }
1952 switch (MaybeSLOC.get()) {
1953 default:
1954 Error(Msg: "incorrectly-formatted source location entry in AST file");
1955 return true;
1956
1957 case SM_SLOC_FILE_ENTRY: {
1958 // We will detect whether a file changed and return 'Failure' for it, but
1959 // we will also try to fail gracefully by setting up the SLocEntry.
1960 unsigned InputID = Record[4];
1961 InputFile IF = getInputFile(F&: *F, ID: InputID);
1962 OptionalFileEntryRef File = IF.getFile();
1963 bool OverriddenBuffer = IF.isOverridden();
1964
1965 // Note that we only check if a File was returned. If it was out-of-date
1966 // we have complained but we will continue creating a FileID to recover
1967 // gracefully.
1968 if (!File)
1969 return true;
1970
1971 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1972 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1973 // This is the module's main file.
1974 IncludeLoc = getImportLocation(F);
1975 }
1976 SrcMgr::CharacteristicKind
1977 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1978 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1979 LoadedOffset: BaseOffset + Record[0]);
1980 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1981 FileInfo.NumCreatedFIDs = Record[5];
1982 if (Record[3])
1983 FileInfo.setHasLineDirectives();
1984
1985 unsigned NumFileDecls = Record[7];
1986 if (NumFileDecls && ContextObj) {
1987 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1988 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1989 FileDeclIDs[FID] =
1990 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1991 }
1992
1993 const SrcMgr::ContentCache &ContentCache =
1994 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
1995 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1996 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1997 !ContentCache.getBufferIfLoaded()) {
1998 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1999 if (!Buffer)
2000 return true;
2001 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2002 }
2003
2004 break;
2005 }
2006
2007 case SM_SLOC_BUFFER_ENTRY: {
2008 const char *Name = Blob.data();
2009 unsigned Offset = Record[0];
2010 SrcMgr::CharacteristicKind
2011 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2012 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2013 if (IncludeLoc.isInvalid() && F->isModule()) {
2014 IncludeLoc = getImportLocation(F);
2015 }
2016
2017 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2018 if (!Buffer)
2019 return true;
2020 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2021 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2022 if (Record[3]) {
2023 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2024 FileInfo.setHasLineDirectives();
2025 }
2026 break;
2027 }
2028
2029 case SM_SLOC_EXPANSION_ENTRY: {
2030 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2031 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2032 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2033 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2034 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2035 LoadedOffset: BaseOffset + Record[0]);
2036 break;
2037 }
2038 }
2039
2040 return false;
2041}
2042
2043std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2044 if (ID == 0)
2045 return std::make_pair(x: SourceLocation(), y: "");
2046
2047 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2048 Error(Msg: "source location entry ID out-of-range for AST file");
2049 return std::make_pair(x: SourceLocation(), y: "");
2050 }
2051
2052 // Find which module file this entry lands in.
2053 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2054 if (!M->isModule())
2055 return std::make_pair(x: SourceLocation(), y: "");
2056
2057 // FIXME: Can we map this down to a particular submodule? That would be
2058 // ideal.
2059 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2060}
2061
2062/// Find the location where the module F is imported.
2063SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2064 if (F->ImportLoc.isValid())
2065 return F->ImportLoc;
2066
2067 // Otherwise we have a PCH. It's considered to be "imported" at the first
2068 // location of its includer.
2069 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2070 // Main file is the importer.
2071 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2072 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2073 }
2074 return F->ImportedBy[0]->FirstLoc;
2075}
2076
2077/// Enter a subblock of the specified BlockID with the specified cursor. Read
2078/// the abbreviations that are at the top of the block and then leave the cursor
2079/// pointing into the block.
2080llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2081 unsigned BlockID,
2082 uint64_t *StartOfBlockOffset) {
2083 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2084 return Err;
2085
2086 if (StartOfBlockOffset)
2087 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2088
2089 while (true) {
2090 uint64_t Offset = Cursor.GetCurrentBitNo();
2091 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2092 if (!MaybeCode)
2093 return MaybeCode.takeError();
2094 unsigned Code = MaybeCode.get();
2095
2096 // We expect all abbrevs to be at the start of the block.
2097 if (Code != llvm::bitc::DEFINE_ABBREV) {
2098 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2099 return Err;
2100 return llvm::Error::success();
2101 }
2102 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2103 return Err;
2104 }
2105}
2106
2107Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2108 unsigned &Idx) {
2109 Token Tok;
2110 Tok.startToken();
2111 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2112 Tok.setKind((tok::TokenKind)Record[Idx++]);
2113 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2114
2115 if (Tok.isAnnotation()) {
2116 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2117 switch (Tok.getKind()) {
2118 case tok::annot_pragma_loop_hint: {
2119 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2120 Info->PragmaName = ReadToken(M, Record, Idx);
2121 Info->Option = ReadToken(M, Record, Idx);
2122 unsigned NumTokens = Record[Idx++];
2123 SmallVector<Token, 4> Toks;
2124 Toks.reserve(N: NumTokens);
2125 for (unsigned I = 0; I < NumTokens; ++I)
2126 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2127 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2128 Tok.setAnnotationValue(static_cast<void *>(Info));
2129 break;
2130 }
2131 case tok::annot_pragma_pack: {
2132 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2133 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2134 auto SlotLabel = ReadString(Record, Idx);
2135 Info->SlotLabel =
2136 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2137 Info->Alignment = ReadToken(M, Record, Idx);
2138 Tok.setAnnotationValue(static_cast<void *>(Info));
2139 break;
2140 }
2141 // Some annotation tokens do not use the PtrData field.
2142 case tok::annot_pragma_openmp:
2143 case tok::annot_pragma_openmp_end:
2144 case tok::annot_pragma_unused:
2145 case tok::annot_pragma_openacc:
2146 case tok::annot_pragma_openacc_end:
2147 case tok::annot_repl_input_end:
2148 break;
2149 default:
2150 llvm_unreachable("missing deserialization code for annotation token");
2151 }
2152 } else {
2153 Tok.setLength(Record[Idx++]);
2154 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2155 Tok.setIdentifierInfo(II);
2156 }
2157 return Tok;
2158}
2159
2160MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2161 BitstreamCursor &Stream = F.MacroCursor;
2162
2163 // Keep track of where we are in the stream, then jump back there
2164 // after reading this macro.
2165 SavedStreamPosition SavedPosition(Stream);
2166
2167 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2168 // FIXME this drops errors on the floor.
2169 consumeError(Err: std::move(Err));
2170 return nullptr;
2171 }
2172 RecordData Record;
2173 SmallVector<IdentifierInfo*, 16> MacroParams;
2174 MacroInfo *Macro = nullptr;
2175 llvm::MutableArrayRef<Token> MacroTokens;
2176
2177 while (true) {
2178 // Advance to the next record, but if we get to the end of the block, don't
2179 // pop it (removing all the abbreviations from the cursor) since we want to
2180 // be able to reseek within the block and read entries.
2181 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2182 Expected<llvm::BitstreamEntry> MaybeEntry =
2183 Stream.advanceSkippingSubblocks(Flags);
2184 if (!MaybeEntry) {
2185 Error(Err: MaybeEntry.takeError());
2186 return Macro;
2187 }
2188 llvm::BitstreamEntry Entry = MaybeEntry.get();
2189
2190 switch (Entry.Kind) {
2191 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2192 case llvm::BitstreamEntry::Error:
2193 Error(Msg: "malformed block record in AST file");
2194 return Macro;
2195 case llvm::BitstreamEntry::EndBlock:
2196 return Macro;
2197 case llvm::BitstreamEntry::Record:
2198 // The interesting case.
2199 break;
2200 }
2201
2202 // Read a record.
2203 Record.clear();
2204 PreprocessorRecordTypes RecType;
2205 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2206 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2207 else {
2208 Error(Err: MaybeRecType.takeError());
2209 return Macro;
2210 }
2211 switch (RecType) {
2212 case PP_MODULE_MACRO:
2213 case PP_MACRO_DIRECTIVE_HISTORY:
2214 return Macro;
2215
2216 case PP_MACRO_OBJECT_LIKE:
2217 case PP_MACRO_FUNCTION_LIKE: {
2218 // If we already have a macro, that means that we've hit the end
2219 // of the definition of the macro we were looking for. We're
2220 // done.
2221 if (Macro)
2222 return Macro;
2223
2224 unsigned NextIndex = 1; // Skip identifier ID.
2225 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2226 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2227 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2228 MI->setIsUsed(Record[NextIndex++]);
2229 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2230 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2231 PPAllocator&: PP.getPreprocessorAllocator());
2232 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2233 // Decode function-like macro info.
2234 bool isC99VarArgs = Record[NextIndex++];
2235 bool isGNUVarArgs = Record[NextIndex++];
2236 bool hasCommaPasting = Record[NextIndex++];
2237 MacroParams.clear();
2238 unsigned NumArgs = Record[NextIndex++];
2239 for (unsigned i = 0; i != NumArgs; ++i)
2240 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2241
2242 // Install function-like macro info.
2243 MI->setIsFunctionLike();
2244 if (isC99VarArgs) MI->setIsC99Varargs();
2245 if (isGNUVarArgs) MI->setIsGNUVarargs();
2246 if (hasCommaPasting) MI->setHasCommaPasting();
2247 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2248 }
2249
2250 // Remember that we saw this macro last so that we add the tokens that
2251 // form its body to it.
2252 Macro = MI;
2253
2254 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2255 Record[NextIndex]) {
2256 // We have a macro definition. Register the association
2257 PreprocessedEntityID
2258 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2259 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2260 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2261 PreprocessingRecord::PPEntityID PPID =
2262 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2263 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2264 Val: PPRec.getPreprocessedEntity(PPID));
2265 if (PPDef)
2266 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2267 }
2268
2269 ++NumMacrosRead;
2270 break;
2271 }
2272
2273 case PP_TOKEN: {
2274 // If we see a TOKEN before a PP_MACRO_*, then the file is
2275 // erroneous, just pretend we didn't see this.
2276 if (!Macro) break;
2277 if (MacroTokens.empty()) {
2278 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2279 return Macro;
2280 }
2281
2282 unsigned Idx = 0;
2283 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2284 MacroTokens = MacroTokens.drop_front();
2285 break;
2286 }
2287 }
2288 }
2289}
2290
2291PreprocessedEntityID
2292ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2293 PreprocessedEntityID LocalID) const {
2294 if (!M.ModuleOffsetMap.empty())
2295 ReadModuleOffsetMap(F&: M);
2296
2297 unsigned ModuleFileIndex = LocalID >> 32;
2298 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2299 ModuleFile *MF =
2300 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2301 assert(MF && "malformed identifier ID encoding?");
2302
2303 if (!ModuleFileIndex) {
2304 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2305 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2306 }
2307
2308 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2309}
2310
2311OptionalFileEntryRef
2312HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2313 FileManager &FileMgr = Reader.getFileManager();
2314 if (!Key.Imported)
2315 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2316
2317 auto Resolved =
2318 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2319 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2320}
2321
2322unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2323 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2324 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2325 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2326 return llvm::xxh3_64bits(data: buf);
2327}
2328
2329HeaderFileInfoTrait::internal_key_type
2330HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2331 internal_key_type ikey = {.Size: ekey.getSize(),
2332 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2333 .Filename: ekey.getName(), /*Imported*/ false};
2334 return ikey;
2335}
2336
2337bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2338 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2339 return false;
2340
2341 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2342 return true;
2343
2344 // Determine whether the actual files are equivalent.
2345 OptionalFileEntryRef FEA = getFile(Key: a);
2346 OptionalFileEntryRef FEB = getFile(Key: b);
2347 return FEA && FEA == FEB;
2348}
2349
2350std::pair<unsigned, unsigned>
2351HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2352 return readULEBKeyDataLength(P&: d);
2353}
2354
2355HeaderFileInfoTrait::internal_key_type
2356HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2357 using namespace llvm::support;
2358
2359 internal_key_type ikey;
2360 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2361 ikey.ModTime =
2362 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2363 ikey.Filename = (const char *)d;
2364 ikey.Imported = true;
2365 return ikey;
2366}
2367
2368HeaderFileInfoTrait::data_type
2369HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2370 unsigned DataLen) {
2371 using namespace llvm::support;
2372
2373 const unsigned char *End = d + DataLen;
2374 HeaderFileInfo HFI;
2375 unsigned Flags = *d++;
2376
2377 OptionalFileEntryRef FE;
2378 bool Included = (Flags >> 6) & 0x01;
2379 if (Included)
2380 if ((FE = getFile(Key: key)))
2381 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2382 // deserialize this header file info again.
2383 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2384
2385 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2386 HFI.isImport |= (Flags >> 5) & 0x01;
2387 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2388 HFI.DirInfo = (Flags >> 1) & 0x07;
2389 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2390 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2391
2392 assert((End - d) % 4 == 0 &&
2393 "Wrong data length in HeaderFileInfo deserialization");
2394 while (d != End) {
2395 uint32_t LocalSMID =
2396 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2397 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2398 LocalSMID >>= 3;
2399
2400 // This header is part of a module. Associate it with the module to enable
2401 // implicit module import.
2402 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2403 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2404 ModuleMap &ModMap =
2405 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2406
2407 if (FE || (FE = getFile(Key: key))) {
2408 // FIXME: NameAsWritten
2409 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2410 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2411 }
2412 HFI.mergeModuleMembership(Role: HeaderRole);
2413 }
2414
2415 // This HeaderFileInfo was externally loaded.
2416 HFI.External = true;
2417 HFI.IsValid = true;
2418 return HFI;
2419}
2420
2421void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2422 uint32_t MacroDirectivesOffset) {
2423 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2424 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2425}
2426
2427void ASTReader::ReadDefinedMacros() {
2428 // Note that we are loading defined macros.
2429 Deserializing Macros(this);
2430
2431 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2432 BitstreamCursor &MacroCursor = I.MacroCursor;
2433
2434 // If there was no preprocessor block, skip this file.
2435 if (MacroCursor.getBitcodeBytes().empty())
2436 continue;
2437
2438 BitstreamCursor Cursor = MacroCursor;
2439 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2440 Error(Err: std::move(Err));
2441 return;
2442 }
2443
2444 RecordData Record;
2445 while (true) {
2446 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2447 if (!MaybeE) {
2448 Error(Err: MaybeE.takeError());
2449 return;
2450 }
2451 llvm::BitstreamEntry E = MaybeE.get();
2452
2453 switch (E.Kind) {
2454 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2455 case llvm::BitstreamEntry::Error:
2456 Error(Msg: "malformed block record in AST file");
2457 return;
2458 case llvm::BitstreamEntry::EndBlock:
2459 goto NextCursor;
2460
2461 case llvm::BitstreamEntry::Record: {
2462 Record.clear();
2463 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2464 if (!MaybeRecord) {
2465 Error(Err: MaybeRecord.takeError());
2466 return;
2467 }
2468 switch (MaybeRecord.get()) {
2469 default: // Default behavior: ignore.
2470 break;
2471
2472 case PP_MACRO_OBJECT_LIKE:
2473 case PP_MACRO_FUNCTION_LIKE: {
2474 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2475 if (II->isOutOfDate())
2476 updateOutOfDateIdentifier(II: *II);
2477 break;
2478 }
2479
2480 case PP_TOKEN:
2481 // Ignore tokens.
2482 break;
2483 }
2484 break;
2485 }
2486 }
2487 }
2488 NextCursor: ;
2489 }
2490}
2491
2492namespace {
2493
2494 /// Visitor class used to look up identifirs in an AST file.
2495 class IdentifierLookupVisitor {
2496 StringRef Name;
2497 unsigned NameHash;
2498 unsigned PriorGeneration;
2499 unsigned &NumIdentifierLookups;
2500 unsigned &NumIdentifierLookupHits;
2501 IdentifierInfo *Found = nullptr;
2502
2503 public:
2504 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2505 unsigned &NumIdentifierLookups,
2506 unsigned &NumIdentifierLookupHits)
2507 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2508 PriorGeneration(PriorGeneration),
2509 NumIdentifierLookups(NumIdentifierLookups),
2510 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2511
2512 bool operator()(ModuleFile &M) {
2513 // If we've already searched this module file, skip it now.
2514 if (M.Generation <= PriorGeneration)
2515 return true;
2516
2517 ASTIdentifierLookupTable *IdTable
2518 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2519 if (!IdTable)
2520 return false;
2521
2522 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2523 Found);
2524 ++NumIdentifierLookups;
2525 ASTIdentifierLookupTable::iterator Pos =
2526 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2527 if (Pos == IdTable->end())
2528 return false;
2529
2530 // Dereferencing the iterator has the effect of building the
2531 // IdentifierInfo node and populating it with the various
2532 // declarations it needs.
2533 ++NumIdentifierLookupHits;
2534 Found = *Pos;
2535 if (Trait.hasMoreInformationInDependencies()) {
2536 // Look for the identifier in extra modules as they contain more info.
2537 return false;
2538 }
2539 return true;
2540 }
2541
2542 // Retrieve the identifier info found within the module
2543 // files.
2544 IdentifierInfo *getIdentifierInfo() const { return Found; }
2545 };
2546
2547} // namespace
2548
2549void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2550 // Note that we are loading an identifier.
2551 Deserializing AnIdentifier(this);
2552
2553 unsigned PriorGeneration = 0;
2554 if (getContext().getLangOpts().Modules)
2555 PriorGeneration = IdentifierGeneration[&II];
2556
2557 // If there is a global index, look there first to determine which modules
2558 // provably do not have any results for this identifier.
2559 GlobalModuleIndex::HitSet Hits;
2560 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2561 if (!loadGlobalIndex()) {
2562 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2563 HitsPtr = &Hits;
2564 }
2565 }
2566
2567 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2568 NumIdentifierLookups,
2569 NumIdentifierLookupHits);
2570 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2571 markIdentifierUpToDate(II: &II);
2572}
2573
2574void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2575 if (!II)
2576 return;
2577
2578 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2579
2580 // Update the generation for this identifier.
2581 if (getContext().getLangOpts().Modules)
2582 IdentifierGeneration[II] = getGeneration();
2583}
2584
2585MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2586 unsigned &Idx) {
2587 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2588 uint64_t LocalIndex = Record[Idx++];
2589 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2590}
2591
2592void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2593 const PendingMacroInfo &PMInfo) {
2594 ModuleFile &M = *PMInfo.M;
2595
2596 BitstreamCursor &Cursor = M.MacroCursor;
2597 SavedStreamPosition SavedPosition(Cursor);
2598 if (llvm::Error Err =
2599 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2600 Error(Err: std::move(Err));
2601 return;
2602 }
2603
2604 struct ModuleMacroRecord {
2605 SubmoduleID SubModID;
2606 MacroInfo *MI;
2607 SmallVector<SubmoduleID, 8> Overrides;
2608 };
2609 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2610
2611 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2612 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2613 // macro histroy.
2614 RecordData Record;
2615 while (true) {
2616 Expected<llvm::BitstreamEntry> MaybeEntry =
2617 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2618 if (!MaybeEntry) {
2619 Error(Err: MaybeEntry.takeError());
2620 return;
2621 }
2622 llvm::BitstreamEntry Entry = MaybeEntry.get();
2623
2624 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2625 Error(Msg: "malformed block record in AST file");
2626 return;
2627 }
2628
2629 Record.clear();
2630 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2631 if (!MaybePP) {
2632 Error(Err: MaybePP.takeError());
2633 return;
2634 }
2635 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2636 case PP_MACRO_DIRECTIVE_HISTORY:
2637 break;
2638
2639 case PP_MODULE_MACRO: {
2640 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2641 auto &Info = ModuleMacros.back();
2642 unsigned Idx = 0;
2643 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2644 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2645 for (int I = Idx, N = Record.size(); I != N; ++I)
2646 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2647 continue;
2648 }
2649
2650 default:
2651 Error(Msg: "malformed block record in AST file");
2652 return;
2653 }
2654
2655 // We found the macro directive history; that's the last record
2656 // for this macro.
2657 break;
2658 }
2659
2660 // Module macros are listed in reverse dependency order.
2661 {
2662 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2663 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2664 for (auto &MMR : ModuleMacros) {
2665 Overrides.clear();
2666 for (unsigned ModID : MMR.Overrides) {
2667 Module *Mod = getSubmodule(GlobalID: ModID);
2668 auto *Macro = PP.getModuleMacro(Mod, II);
2669 assert(Macro && "missing definition for overridden macro");
2670 Overrides.push_back(Elt: Macro);
2671 }
2672
2673 bool Inserted = false;
2674 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2675 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2676 }
2677 }
2678
2679 // Don't read the directive history for a module; we don't have anywhere
2680 // to put it.
2681 if (M.isModule())
2682 return;
2683
2684 // Deserialize the macro directives history in reverse source-order.
2685 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2686 unsigned Idx = 0, N = Record.size();
2687 while (Idx < N) {
2688 MacroDirective *MD = nullptr;
2689 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2690 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2691 switch (K) {
2692 case MacroDirective::MD_Define: {
2693 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2694 MD = PP.AllocateDefMacroDirective(MI, Loc);
2695 break;
2696 }
2697 case MacroDirective::MD_Undefine:
2698 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2699 break;
2700 case MacroDirective::MD_Visibility:
2701 bool isPublic = Record[Idx++];
2702 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2703 break;
2704 }
2705
2706 if (!Latest)
2707 Latest = MD;
2708 if (Earliest)
2709 Earliest->setPrevious(MD);
2710 Earliest = MD;
2711 }
2712
2713 if (Latest)
2714 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2715}
2716
2717bool ASTReader::shouldDisableValidationForFile(
2718 const serialization::ModuleFile &M) const {
2719 if (DisableValidationKind == DisableValidationForModuleKind::None)
2720 return false;
2721
2722 // If a PCH is loaded and validation is disabled for PCH then disable
2723 // validation for the PCH and the modules it loads.
2724 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2725
2726 switch (K) {
2727 case MK_MainFile:
2728 case MK_Preamble:
2729 case MK_PCH:
2730 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2731 case MK_ImplicitModule:
2732 case MK_ExplicitModule:
2733 case MK_PrebuiltModule:
2734 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2735 }
2736
2737 return false;
2738}
2739
2740static std::pair<StringRef, StringRef>
2741getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2742 const StringRef InputBlob) {
2743 uint16_t AsRequestedLength = Record[7];
2744 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2745 InputBlob.substr(Start: AsRequestedLength)};
2746}
2747
2748InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2749 // If this ID is bogus, just return an empty input file.
2750 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2751 return InputFileInfo();
2752
2753 // If we've already loaded this input file, return it.
2754 if (F.InputFileInfosLoaded[ID - 1].isValid())
2755 return F.InputFileInfosLoaded[ID - 1];
2756
2757 // Go find this input file.
2758 BitstreamCursor &Cursor = F.InputFilesCursor;
2759 SavedStreamPosition SavedPosition(Cursor);
2760 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2761 F.InputFileOffsets[ID - 1])) {
2762 // FIXME this drops errors on the floor.
2763 consumeError(Err: std::move(Err));
2764 }
2765
2766 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2767 if (!MaybeCode) {
2768 // FIXME this drops errors on the floor.
2769 consumeError(Err: MaybeCode.takeError());
2770 }
2771 unsigned Code = MaybeCode.get();
2772 RecordData Record;
2773 StringRef Blob;
2774
2775 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2776 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2777 "invalid record type for input file");
2778 else {
2779 // FIXME this drops errors on the floor.
2780 consumeError(Err: Maybe.takeError());
2781 }
2782
2783 assert(Record[0] == ID && "Bogus stored ID or offset");
2784 InputFileInfo R;
2785 R.StoredSize = static_cast<off_t>(Record[1]);
2786 R.StoredTime = static_cast<time_t>(Record[2]);
2787 R.Overridden = static_cast<bool>(Record[3]);
2788 R.Transient = static_cast<bool>(Record[4]);
2789 R.TopLevel = static_cast<bool>(Record[5]);
2790 R.ModuleMap = static_cast<bool>(Record[6]);
2791 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2792 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2793 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2794 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2795 ? UnresolvedFilenameAsRequested
2796 : UnresolvedFilename;
2797
2798 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2799 if (!MaybeEntry) // FIXME this drops errors on the floor.
2800 consumeError(Err: MaybeEntry.takeError());
2801 llvm::BitstreamEntry Entry = MaybeEntry.get();
2802 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2803 "expected record type for input file hash");
2804
2805 Record.clear();
2806 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2807 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2808 "invalid record type for input file hash");
2809 else {
2810 // FIXME this drops errors on the floor.
2811 consumeError(Err: Maybe.takeError());
2812 }
2813 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2814 static_cast<uint64_t>(Record[0]);
2815
2816 // Note that we've loaded this input file info.
2817 F.InputFileInfosLoaded[ID - 1] = R;
2818 return R;
2819}
2820
2821static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2822InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2823 // If this ID is bogus, just return an empty input file.
2824 if (ID == 0 || ID > F.InputFilesLoaded.size())
2825 return InputFile();
2826
2827 // If we've already loaded this input file, return it.
2828 if (F.InputFilesLoaded[ID-1].getFile())
2829 return F.InputFilesLoaded[ID-1];
2830
2831 if (F.InputFilesLoaded[ID-1].isNotFound())
2832 return InputFile();
2833
2834 // Go find this input file.
2835 BitstreamCursor &Cursor = F.InputFilesCursor;
2836 SavedStreamPosition SavedPosition(Cursor);
2837 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2838 F.InputFileOffsets[ID - 1])) {
2839 // FIXME this drops errors on the floor.
2840 consumeError(Err: std::move(Err));
2841 }
2842
2843 InputFileInfo FI = getInputFileInfo(F, ID);
2844 off_t StoredSize = FI.StoredSize;
2845 time_t StoredTime = FI.StoredTime;
2846 bool Overridden = FI.Overridden;
2847 bool Transient = FI.Transient;
2848 auto Filename =
2849 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2850 uint64_t StoredContentHash = FI.ContentHash;
2851
2852 // For standard C++ modules, we don't need to check the inputs.
2853 bool SkipChecks = F.StandardCXXModule;
2854
2855 const HeaderSearchOptions &HSOpts =
2856 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2857
2858 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2859 // modules.
2860 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2861 SkipChecks = false;
2862 Overridden = false;
2863 }
2864
2865 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2866
2867 // For an overridden file, create a virtual file with the stored
2868 // size/timestamp.
2869 if ((Overridden || Transient || SkipChecks) && !File)
2870 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2871
2872 if (!File) {
2873 if (Complain) {
2874 std::string ErrorStr = "could not find file '";
2875 ErrorStr += *Filename;
2876 ErrorStr += "' referenced by AST file '";
2877 ErrorStr += F.FileName;
2878 ErrorStr += "'";
2879 Error(Msg: ErrorStr);
2880 }
2881 // Record that we didn't find the file.
2882 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2883 return InputFile();
2884 }
2885
2886 // Check if there was a request to override the contents of the file
2887 // that was part of the precompiled header. Overriding such a file
2888 // can lead to problems when lexing using the source locations from the
2889 // PCH.
2890 SourceManager &SM = getSourceManager();
2891 // FIXME: Reject if the overrides are different.
2892 if ((!Overridden && !Transient) && !SkipChecks &&
2893 SM.isFileOverridden(File: *File)) {
2894 if (Complain)
2895 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2896
2897 // After emitting the diagnostic, bypass the overriding file to recover
2898 // (this creates a separate FileEntry).
2899 File = SM.bypassFileContentsOverride(File: *File);
2900 if (!File) {
2901 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2902 return InputFile();
2903 }
2904 }
2905
2906 struct Change {
2907 enum ModificationKind {
2908 Size,
2909 ModTime,
2910 Content,
2911 None,
2912 } Kind;
2913 std::optional<int64_t> Old = std::nullopt;
2914 std::optional<int64_t> New = std::nullopt;
2915 };
2916 auto HasInputContentChanged = [&](Change OriginalChange) {
2917 assert(ValidateASTInputFilesContent &&
2918 "We should only check the content of the inputs with "
2919 "ValidateASTInputFilesContent enabled.");
2920
2921 if (StoredContentHash == 0)
2922 return OriginalChange;
2923
2924 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2925 if (!MemBuffOrError) {
2926 if (!Complain)
2927 return OriginalChange;
2928 std::string ErrorStr = "could not get buffer for file '";
2929 ErrorStr += File->getName();
2930 ErrorStr += "'";
2931 Error(Msg: ErrorStr);
2932 return OriginalChange;
2933 }
2934
2935 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2936 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2937 return Change{.Kind: Change::None};
2938
2939 return Change{.Kind: Change::Content};
2940 };
2941 auto HasInputFileChanged = [&]() {
2942 if (StoredSize != File->getSize())
2943 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2944 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2945 StoredTime != File->getModificationTime()) {
2946 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2947 .New: File->getModificationTime()};
2948
2949 // In case the modification time changes but not the content,
2950 // accept the cached file as legit.
2951 if (ValidateASTInputFilesContent)
2952 return HasInputContentChanged(MTimeChange);
2953
2954 return MTimeChange;
2955 }
2956 return Change{.Kind: Change::None};
2957 };
2958
2959 bool IsOutOfDate = false;
2960 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2961 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2962 // enabled, it is better to check the contents of the inputs. Since we can't
2963 // get correct modified time information for inputs from overriden inputs.
2964 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2965 F.StandardCXXModule && FileChange.Kind == Change::None)
2966 FileChange = HasInputContentChanged(FileChange);
2967
2968 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2969 // it is better to check the content of the input files because we cannot rely
2970 // on the file modification time, which will be the same (zero) for these
2971 // files.
2972 if (!StoredTime && ValidateASTInputFilesContent &&
2973 FileChange.Kind == Change::None)
2974 FileChange = HasInputContentChanged(FileChange);
2975
2976 // For an overridden file, there is nothing to validate.
2977 if (!Overridden && FileChange.Kind != Change::None) {
2978 if (Complain) {
2979 // Build a list of the PCH imports that got us here (in reverse).
2980 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2981 while (!ImportStack.back()->ImportedBy.empty())
2982 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2983
2984 // The top-level AST file is stale.
2985 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
2986 Diag(DiagID: diag::err_fe_ast_file_modified)
2987 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
2988 << TopLevelASTFileName << FileChange.Kind
2989 << (FileChange.Old && FileChange.New)
2990 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
2991 << llvm::itostr(X: FileChange.New.value_or(u: 0));
2992
2993 // Print the import stack.
2994 if (ImportStack.size() > 1) {
2995 Diag(DiagID: diag::note_ast_file_required_by)
2996 << *Filename << ImportStack[0]->FileName;
2997 for (unsigned I = 1; I < ImportStack.size(); ++I)
2998 Diag(DiagID: diag::note_ast_file_required_by)
2999 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3000 }
3001
3002 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3003 }
3004
3005 IsOutOfDate = true;
3006 }
3007 // FIXME: If the file is overridden and we've already opened it,
3008 // issue an error (or split it into a separate FileEntry).
3009
3010 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3011
3012 // Note that we've loaded this input file.
3013 F.InputFilesLoaded[ID-1] = IF;
3014 return IF;
3015}
3016
3017ASTReader::TemporarilyOwnedStringRef
3018ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3019 ModuleFile &ModF) {
3020 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3021}
3022
3023ASTReader::TemporarilyOwnedStringRef
3024ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3025 StringRef Prefix) {
3026 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3027
3028 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3029 Path == "<built-in>" || Path == "<command line>")
3030 return {Path, Buf};
3031
3032 Buf.clear();
3033 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3034 StringRef ResolvedPath{Buf.data(), Buf.size()};
3035 return {ResolvedPath, Buf};
3036}
3037
3038std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3039 StringRef P,
3040 ModuleFile &ModF) {
3041 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3042}
3043
3044std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3045 StringRef P,
3046 StringRef Prefix) {
3047 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3048 return ResolvedPath->str();
3049}
3050
3051static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3052 switch (ARR) {
3053 case ASTReader::Failure: return true;
3054 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3055 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3056 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3057 case ASTReader::ConfigurationMismatch:
3058 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3059 case ASTReader::HadErrors: return true;
3060 case ASTReader::Success: return false;
3061 }
3062
3063 llvm_unreachable("unknown ASTReadResult");
3064}
3065
3066ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3067 BitstreamCursor &Stream, StringRef Filename,
3068 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3069 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3070 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3071 // FIXME this drops errors on the floor.
3072 consumeError(Err: std::move(Err));
3073 return Failure;
3074 }
3075
3076 // Read all of the records in the options block.
3077 RecordData Record;
3078 ASTReadResult Result = Success;
3079 while (true) {
3080 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3081 if (!MaybeEntry) {
3082 // FIXME this drops errors on the floor.
3083 consumeError(Err: MaybeEntry.takeError());
3084 return Failure;
3085 }
3086 llvm::BitstreamEntry Entry = MaybeEntry.get();
3087
3088 switch (Entry.Kind) {
3089 case llvm::BitstreamEntry::Error:
3090 case llvm::BitstreamEntry::SubBlock:
3091 return Failure;
3092
3093 case llvm::BitstreamEntry::EndBlock:
3094 return Result;
3095
3096 case llvm::BitstreamEntry::Record:
3097 // The interesting case.
3098 break;
3099 }
3100
3101 // Read and process a record.
3102 Record.clear();
3103 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3104 if (!MaybeRecordType) {
3105 // FIXME this drops errors on the floor.
3106 consumeError(Err: MaybeRecordType.takeError());
3107 return Failure;
3108 }
3109 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3110 case LANGUAGE_OPTIONS: {
3111 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3112 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3113 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3114 Result = ConfigurationMismatch;
3115 break;
3116 }
3117
3118 case CODEGEN_OPTIONS: {
3119 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3120 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3121 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3122 Result = ConfigurationMismatch;
3123 break;
3124 }
3125
3126 case TARGET_OPTIONS: {
3127 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3128 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3129 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3130 Result = ConfigurationMismatch;
3131 break;
3132 }
3133
3134 case FILE_SYSTEM_OPTIONS: {
3135 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3136 if (!AllowCompatibleConfigurationMismatch &&
3137 ParseFileSystemOptions(Record, Complain, Listener))
3138 Result = ConfigurationMismatch;
3139 break;
3140 }
3141
3142 case HEADER_SEARCH_OPTIONS: {
3143 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3144 if (!AllowCompatibleConfigurationMismatch &&
3145 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3146 Result = ConfigurationMismatch;
3147 break;
3148 }
3149
3150 case PREPROCESSOR_OPTIONS:
3151 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3152 if (!AllowCompatibleConfigurationMismatch &&
3153 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3154 SuggestedPredefines))
3155 Result = ConfigurationMismatch;
3156 break;
3157 }
3158 }
3159}
3160
3161ASTReader::ASTReadResult
3162ASTReader::ReadControlBlock(ModuleFile &F,
3163 SmallVectorImpl<ImportedModule> &Loaded,
3164 const ModuleFile *ImportedBy,
3165 unsigned ClientLoadCapabilities) {
3166 BitstreamCursor &Stream = F.Stream;
3167
3168 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3169 Error(Err: std::move(Err));
3170 return Failure;
3171 }
3172
3173 // Lambda to read the unhashed control block the first time it's called.
3174 //
3175 // For PCM files, the unhashed control block cannot be read until after the
3176 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3177 // need to look ahead before reading the IMPORTS record. For consistency,
3178 // this block is always read somehow (see BitstreamEntry::EndBlock).
3179 bool HasReadUnhashedControlBlock = false;
3180 auto readUnhashedControlBlockOnce = [&]() {
3181 if (!HasReadUnhashedControlBlock) {
3182 HasReadUnhashedControlBlock = true;
3183 if (ASTReadResult Result =
3184 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3185 return Result;
3186 }
3187 return Success;
3188 };
3189
3190 bool DisableValidation = shouldDisableValidationForFile(M: F);
3191
3192 // Read all of the records and blocks in the control block.
3193 RecordData Record;
3194 unsigned NumInputs = 0;
3195 unsigned NumUserInputs = 0;
3196 StringRef BaseDirectoryAsWritten;
3197 while (true) {
3198 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3199 if (!MaybeEntry) {
3200 Error(Err: MaybeEntry.takeError());
3201 return Failure;
3202 }
3203 llvm::BitstreamEntry Entry = MaybeEntry.get();
3204
3205 switch (Entry.Kind) {
3206 case llvm::BitstreamEntry::Error:
3207 Error(Msg: "malformed block record in AST file");
3208 return Failure;
3209 case llvm::BitstreamEntry::EndBlock: {
3210 // Validate the module before returning. This call catches an AST with
3211 // no module name and no imports.
3212 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3213 return Result;
3214
3215 // Validate input files.
3216 const HeaderSearchOptions &HSOpts =
3217 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3218
3219 // All user input files reside at the index range [0, NumUserInputs), and
3220 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3221 // loaded module files, ignore missing inputs.
3222 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3223 F.Kind != MK_PrebuiltModule) {
3224 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3225
3226 // If we are reading a module, we will create a verification timestamp,
3227 // so we verify all input files. Otherwise, verify only user input
3228 // files.
3229
3230 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3231 if (HSOpts.ModulesValidateOncePerBuildSession &&
3232 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3233 F.Kind == MK_ImplicitModule)
3234 N = ForceValidateUserInputs ? NumUserInputs : 0;
3235
3236 if (N != 0)
3237 Diag(DiagID: diag::remark_module_validation)
3238 << N << F.ModuleName << F.FileName;
3239
3240 for (unsigned I = 0; I < N; ++I) {
3241 InputFile IF = getInputFile(F, ID: I+1, Complain);
3242 if (!IF.getFile() || IF.isOutOfDate())
3243 return OutOfDate;
3244 }
3245 }
3246
3247 if (Listener)
3248 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
3249
3250 if (Listener && Listener->needsInputFileVisitation()) {
3251 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3252 : NumUserInputs;
3253 for (unsigned I = 0; I < N; ++I) {
3254 bool IsSystem = I >= NumUserInputs;
3255 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3256 auto FilenameAsRequested = ResolveImportedPath(
3257 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3258 Listener->visitInputFile(
3259 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3260 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3261 }
3262 }
3263
3264 return Success;
3265 }
3266
3267 case llvm::BitstreamEntry::SubBlock:
3268 switch (Entry.ID) {
3269 case INPUT_FILES_BLOCK_ID:
3270 F.InputFilesCursor = Stream;
3271 if (llvm::Error Err = Stream.SkipBlock()) {
3272 Error(Err: std::move(Err));
3273 return Failure;
3274 }
3275 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3276 Error(Msg: "malformed block record in AST file");
3277 return Failure;
3278 }
3279 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3280 continue;
3281
3282 case OPTIONS_BLOCK_ID:
3283 // If we're reading the first module for this group, check its options
3284 // are compatible with ours. For modules it imports, no further checking
3285 // is required, because we checked them when we built it.
3286 if (Listener && !ImportedBy) {
3287 // Should we allow the configuration of the module file to differ from
3288 // the configuration of the current translation unit in a compatible
3289 // way?
3290 //
3291 // FIXME: Allow this for files explicitly specified with -include-pch.
3292 bool AllowCompatibleConfigurationMismatch =
3293 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3294
3295 ASTReadResult Result =
3296 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3297 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3298 SuggestedPredefines);
3299 if (Result == Failure) {
3300 Error(Msg: "malformed block record in AST file");
3301 return Result;
3302 }
3303
3304 if (DisableValidation ||
3305 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3306 Result = Success;
3307
3308 // If we can't load the module, exit early since we likely
3309 // will rebuild the module anyway. The stream may be in the
3310 // middle of a block.
3311 if (Result != Success)
3312 return Result;
3313 } else if (llvm::Error Err = Stream.SkipBlock()) {
3314 Error(Err: std::move(Err));
3315 return Failure;
3316 }
3317 continue;
3318
3319 default:
3320 if (llvm::Error Err = Stream.SkipBlock()) {
3321 Error(Err: std::move(Err));
3322 return Failure;
3323 }
3324 continue;
3325 }
3326
3327 case llvm::BitstreamEntry::Record:
3328 // The interesting case.
3329 break;
3330 }
3331
3332 // Read and process a record.
3333 Record.clear();
3334 StringRef Blob;
3335 Expected<unsigned> MaybeRecordType =
3336 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3337 if (!MaybeRecordType) {
3338 Error(Err: MaybeRecordType.takeError());
3339 return Failure;
3340 }
3341 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3342 case METADATA: {
3343 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3344 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3345 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3346 : diag::err_ast_file_version_too_new)
3347 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3348 return VersionMismatch;
3349 }
3350
3351 bool hasErrors = Record[7];
3352 if (hasErrors && !DisableValidation) {
3353 // If requested by the caller and the module hasn't already been read
3354 // or compiled, mark modules on error as out-of-date.
3355 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3356 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3357 return OutOfDate;
3358
3359 if (!AllowASTWithCompilerErrors) {
3360 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3361 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3362 return HadErrors;
3363 }
3364 }
3365 if (hasErrors) {
3366 Diags.ErrorOccurred = true;
3367 Diags.UncompilableErrorOccurred = true;
3368 Diags.UnrecoverableErrorOccurred = true;
3369 }
3370
3371 F.RelocatablePCH = Record[4];
3372 // Relative paths in a relocatable PCH are relative to our sysroot.
3373 if (F.RelocatablePCH)
3374 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3375
3376 F.StandardCXXModule = Record[5];
3377
3378 F.HasTimestamps = Record[6];
3379
3380 const std::string &CurBranch = getClangFullRepositoryVersion();
3381 StringRef ASTBranch = Blob;
3382 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3383 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3384 Diag(DiagID: diag::err_ast_file_different_branch)
3385 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3386 << CurBranch;
3387 return VersionMismatch;
3388 }
3389 break;
3390 }
3391
3392 case IMPORT: {
3393 // Validate the AST before processing any imports (otherwise, untangling
3394 // them can be error-prone and expensive). A module will have a name and
3395 // will already have been validated, but this catches the PCH case.
3396 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3397 return Result;
3398
3399 unsigned Idx = 0;
3400 // Read information about the AST file.
3401 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3402
3403 // The import location will be the local one for now; we will adjust
3404 // all import locations of module imports after the global source
3405 // location info are setup, in ReadAST.
3406 auto [ImportLoc, ImportModuleFileIndex] =
3407 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3408 // The import location must belong to the current module file itself.
3409 assert(ImportModuleFileIndex == 0);
3410
3411 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3412
3413 bool IsImportingStdCXXModule = Record[Idx++];
3414
3415 off_t StoredSize = 0;
3416 time_t StoredModTime = 0;
3417 ASTFileSignature StoredSignature;
3418 std::string ImportedFile;
3419 std::string StoredFile;
3420 bool IgnoreImportedByNote = false;
3421
3422 // For prebuilt and explicit modules first consult the file map for
3423 // an override. Note that here we don't search prebuilt module
3424 // directories if we're not importing standard c++ module, only the
3425 // explicit name to file mappings. Also, we will still verify the
3426 // size/signature making sure it is essentially the same file but
3427 // perhaps in a different location.
3428 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3429 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3430 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3431
3432 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3433 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3434 return Missing;
3435 }
3436
3437 if (!IsImportingStdCXXModule) {
3438 StoredSize = (off_t)Record[Idx++];
3439 StoredModTime = (time_t)Record[Idx++];
3440
3441 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3442 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3443 Last: SignatureBytes.end());
3444 Blob = Blob.substr(Start: ASTFileSignature::size);
3445
3446 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3447 // ModuleCache as when writing.
3448 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3449 if (ImportedFile.empty()) {
3450 ImportedFile = StoredFile;
3451 } else if (!getDiags().isIgnored(
3452 DiagID: diag::warn_module_file_mapping_mismatch,
3453 Loc: CurrentImportLoc)) {
3454 auto ImportedFileRef =
3455 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3456 auto StoredFileRef =
3457 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3458 if ((ImportedFileRef && StoredFileRef) &&
3459 (*ImportedFileRef != *StoredFileRef)) {
3460 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3461 << ImportedFile << StoredFile;
3462 Diag(DiagID: diag::note_module_file_imported_by)
3463 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3464 IgnoreImportedByNote = true;
3465 }
3466 }
3467 }
3468
3469 // If our client can't cope with us being out of date, we can't cope with
3470 // our dependency being missing.
3471 unsigned Capabilities = ClientLoadCapabilities;
3472 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3473 Capabilities &= ~ARR_Missing;
3474
3475 // Load the AST file.
3476 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3477 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3478 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3479
3480 // Check the AST we just read from ImportedFile contains a different
3481 // module than we expected (ImportedName). This can occur for C++20
3482 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3483 if (IsImportingStdCXXModule) {
3484 if (const auto *Imported =
3485 getModuleManager().lookupByFileName(FileName: ImportedFile);
3486 Imported != nullptr && Imported->ModuleName != ImportedName) {
3487 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3488 Result = Missing;
3489 }
3490 }
3491
3492 // If we diagnosed a problem, produce a backtrace.
3493 bool recompilingFinalized = Result == OutOfDate &&
3494 (Capabilities & ARR_OutOfDate) &&
3495 getModuleManager()
3496 .getModuleCache()
3497 .getInMemoryModuleCache()
3498 .isPCMFinal(Filename: F.FileName);
3499 if (!IgnoreImportedByNote &&
3500 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3501 Diag(DiagID: diag::note_module_file_imported_by)
3502 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3503
3504 switch (Result) {
3505 case Failure: return Failure;
3506 // If we have to ignore the dependency, we'll have to ignore this too.
3507 case Missing:
3508 case OutOfDate: return OutOfDate;
3509 case VersionMismatch: return VersionMismatch;
3510 case ConfigurationMismatch: return ConfigurationMismatch;
3511 case HadErrors: return HadErrors;
3512 case Success: break;
3513 }
3514 break;
3515 }
3516
3517 case ORIGINAL_FILE:
3518 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3519 F.ActualOriginalSourceFileName = std::string(Blob);
3520 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3521 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3522 break;
3523
3524 case ORIGINAL_FILE_ID:
3525 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3526 break;
3527
3528 case MODULE_NAME:
3529 F.ModuleName = std::string(Blob);
3530 Diag(DiagID: diag::remark_module_import)
3531 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3532 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3533 if (Listener)
3534 Listener->ReadModuleName(ModuleName: F.ModuleName);
3535
3536 // Validate the AST as soon as we have a name so we can exit early on
3537 // failure.
3538 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3539 return Result;
3540
3541 break;
3542
3543 case MODULE_DIRECTORY: {
3544 // Save the BaseDirectory as written in the PCM for computing the module
3545 // filename for the ModuleCache.
3546 BaseDirectoryAsWritten = Blob;
3547 assert(!F.ModuleName.empty() &&
3548 "MODULE_DIRECTORY found before MODULE_NAME");
3549 F.BaseDirectory = std::string(Blob);
3550 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3551 break;
3552 // If we've already loaded a module map file covering this module, we may
3553 // have a better path for it (relative to the current build).
3554 Module *M = PP.getHeaderSearchInfo().lookupModule(
3555 ModuleName: F.ModuleName, ImportLoc: SourceLocation(), /*AllowSearch*/ true,
3556 /*AllowExtraModuleMapSearch*/ true);
3557 if (M && M->Directory) {
3558 // If we're implicitly loading a module, the base directory can't
3559 // change between the build and use.
3560 // Don't emit module relocation error if we have -fno-validate-pch
3561 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3562 DisableValidationForModuleKind::Module) &&
3563 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3564 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3565 if (!BuildDir || *BuildDir != M->Directory) {
3566 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3567 Diag(DiagID: diag::err_imported_module_relocated)
3568 << F.ModuleName << Blob << M->Directory->getName();
3569 return OutOfDate;
3570 }
3571 }
3572 F.BaseDirectory = std::string(M->Directory->getName());
3573 }
3574 break;
3575 }
3576
3577 case MODULE_MAP_FILE:
3578 if (ASTReadResult Result =
3579 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3580 return Result;
3581 break;
3582
3583 case INPUT_FILE_OFFSETS:
3584 NumInputs = Record[0];
3585 NumUserInputs = Record[1];
3586 F.InputFileOffsets =
3587 (const llvm::support::unaligned_uint64_t *)Blob.data();
3588 F.InputFilesLoaded.resize(new_size: NumInputs);
3589 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3590 F.NumUserInputFiles = NumUserInputs;
3591 break;
3592 }
3593 }
3594}
3595
3596llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3597 unsigned ClientLoadCapabilities) {
3598 BitstreamCursor &Stream = F.Stream;
3599
3600 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3601 return Err;
3602 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3603
3604 // Read all of the records and blocks for the AST file.
3605 RecordData Record;
3606 while (true) {
3607 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3608 if (!MaybeEntry)
3609 return MaybeEntry.takeError();
3610 llvm::BitstreamEntry Entry = MaybeEntry.get();
3611
3612 switch (Entry.Kind) {
3613 case llvm::BitstreamEntry::Error:
3614 return llvm::createStringError(
3615 EC: std::errc::illegal_byte_sequence,
3616 Fmt: "error at end of module block in AST file");
3617 case llvm::BitstreamEntry::EndBlock:
3618 // Outside of C++, we do not store a lookup map for the translation unit.
3619 // Instead, mark it as needing a lookup map to be built if this module
3620 // contains any declarations lexically within it (which it always does!).
3621 // This usually has no cost, since we very rarely need the lookup map for
3622 // the translation unit outside C++.
3623 if (ASTContext *Ctx = ContextObj) {
3624 DeclContext *DC = Ctx->getTranslationUnitDecl();
3625 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3626 DC->setMustBuildLookupTable();
3627 }
3628
3629 return llvm::Error::success();
3630 case llvm::BitstreamEntry::SubBlock:
3631 switch (Entry.ID) {
3632 case DECLTYPES_BLOCK_ID:
3633 // We lazily load the decls block, but we want to set up the
3634 // DeclsCursor cursor to point into it. Clone our current bitcode
3635 // cursor to it, enter the block and read the abbrevs in that block.
3636 // With the main cursor, we just skip over it.
3637 F.DeclsCursor = Stream;
3638 if (llvm::Error Err = Stream.SkipBlock())
3639 return Err;
3640 if (llvm::Error Err = ReadBlockAbbrevs(
3641 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3642 return Err;
3643 break;
3644
3645 case PREPROCESSOR_BLOCK_ID:
3646 F.MacroCursor = Stream;
3647 if (!PP.getExternalSource())
3648 PP.setExternalSource(this);
3649
3650 if (llvm::Error Err = Stream.SkipBlock())
3651 return Err;
3652 if (llvm::Error Err =
3653 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3654 return Err;
3655 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3656 break;
3657
3658 case PREPROCESSOR_DETAIL_BLOCK_ID:
3659 F.PreprocessorDetailCursor = Stream;
3660
3661 if (llvm::Error Err = Stream.SkipBlock()) {
3662 return Err;
3663 }
3664 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3665 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3666 return Err;
3667 F.PreprocessorDetailStartOffset
3668 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3669
3670 if (!PP.getPreprocessingRecord())
3671 PP.createPreprocessingRecord();
3672 if (!PP.getPreprocessingRecord()->getExternalSource())
3673 PP.getPreprocessingRecord()->SetExternalSource(*this);
3674 break;
3675
3676 case SOURCE_MANAGER_BLOCK_ID:
3677 if (llvm::Error Err = ReadSourceManagerBlock(F))
3678 return Err;
3679 break;
3680
3681 case SUBMODULE_BLOCK_ID:
3682 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3683 return Err;
3684 break;
3685
3686 case COMMENTS_BLOCK_ID: {
3687 BitstreamCursor C = Stream;
3688
3689 if (llvm::Error Err = Stream.SkipBlock())
3690 return Err;
3691 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3692 return Err;
3693 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3694 break;
3695 }
3696
3697 default:
3698 if (llvm::Error Err = Stream.SkipBlock())
3699 return Err;
3700 break;
3701 }
3702 continue;
3703
3704 case llvm::BitstreamEntry::Record:
3705 // The interesting case.
3706 break;
3707 }
3708
3709 // Read and process a record.
3710 Record.clear();
3711 StringRef Blob;
3712 Expected<unsigned> MaybeRecordType =
3713 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3714 if (!MaybeRecordType)
3715 return MaybeRecordType.takeError();
3716 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3717
3718 // If we're not loading an AST context, we don't care about most records.
3719 if (!ContextObj) {
3720 switch (RecordType) {
3721 case IDENTIFIER_TABLE:
3722 case IDENTIFIER_OFFSET:
3723 case INTERESTING_IDENTIFIERS:
3724 case STATISTICS:
3725 case PP_ASSUME_NONNULL_LOC:
3726 case PP_CONDITIONAL_STACK:
3727 case PP_COUNTER_VALUE:
3728 case SOURCE_LOCATION_OFFSETS:
3729 case MODULE_OFFSET_MAP:
3730 case SOURCE_MANAGER_LINE_TABLE:
3731 case PPD_ENTITIES_OFFSETS:
3732 case HEADER_SEARCH_TABLE:
3733 case IMPORTED_MODULES:
3734 case MACRO_OFFSET:
3735 break;
3736 default:
3737 continue;
3738 }
3739 }
3740
3741 switch (RecordType) {
3742 default: // Default behavior: ignore.
3743 break;
3744
3745 case TYPE_OFFSET: {
3746 if (F.LocalNumTypes != 0)
3747 return llvm::createStringError(
3748 EC: std::errc::illegal_byte_sequence,
3749 Fmt: "duplicate TYPE_OFFSET record in AST file");
3750 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3751 F.LocalNumTypes = Record[0];
3752 F.BaseTypeIndex = getTotalNumTypes();
3753
3754 if (F.LocalNumTypes > 0)
3755 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3756
3757 break;
3758 }
3759
3760 case DECL_OFFSET: {
3761 if (F.LocalNumDecls != 0)
3762 return llvm::createStringError(
3763 EC: std::errc::illegal_byte_sequence,
3764 Fmt: "duplicate DECL_OFFSET record in AST file");
3765 F.DeclOffsets = (const DeclOffset *)Blob.data();
3766 F.LocalNumDecls = Record[0];
3767 F.BaseDeclIndex = getTotalNumDecls();
3768
3769 if (F.LocalNumDecls > 0)
3770 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3771
3772 break;
3773 }
3774
3775 case TU_UPDATE_LEXICAL: {
3776 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3777 LexicalContents Contents(
3778 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3779 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3780 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3781 TU->setHasExternalLexicalStorage(true);
3782 break;
3783 }
3784
3785 case UPDATE_VISIBLE: {
3786 unsigned Idx = 0;
3787 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3788 auto *Data = (const unsigned char*)Blob.data();
3789 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3790 // If we've already loaded the decl, perform the updates when we finish
3791 // loading this block.
3792 if (Decl *D = GetExistingDecl(ID))
3793 PendingUpdateRecords.push_back(
3794 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3795 break;
3796 }
3797
3798 case UPDATE_MODULE_LOCAL_VISIBLE: {
3799 unsigned Idx = 0;
3800 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3801 auto *Data = (const unsigned char *)Blob.data();
3802 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3803 // If we've already loaded the decl, perform the updates when we finish
3804 // loading this block.
3805 if (Decl *D = GetExistingDecl(ID))
3806 PendingUpdateRecords.push_back(
3807 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3808 break;
3809 }
3810
3811 case UPDATE_TU_LOCAL_VISIBLE: {
3812 if (F.Kind != MK_MainFile)
3813 break;
3814 unsigned Idx = 0;
3815 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3816 auto *Data = (const unsigned char *)Blob.data();
3817 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3818 // If we've already loaded the decl, perform the updates when we finish
3819 // loading this block.
3820 if (Decl *D = GetExistingDecl(ID))
3821 PendingUpdateRecords.push_back(
3822 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3823 break;
3824 }
3825
3826 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3827 unsigned Idx = 0;
3828 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3829 auto *Data = (const unsigned char *)Blob.data();
3830 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3831 // If we've already loaded the decl, perform the updates when we finish
3832 // loading this block.
3833 if (Decl *D = GetExistingDecl(ID))
3834 PendingUpdateRecords.push_back(
3835 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3836 break;
3837 }
3838
3839 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3840 unsigned Idx = 0;
3841 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3842 auto *Data = (const unsigned char *)Blob.data();
3843 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3844 // If we've already loaded the decl, perform the updates when we finish
3845 // loading this block.
3846 if (Decl *D = GetExistingDecl(ID))
3847 PendingUpdateRecords.push_back(
3848 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3849 break;
3850 }
3851
3852 case IDENTIFIER_TABLE:
3853 F.IdentifierTableData =
3854 reinterpret_cast<const unsigned char *>(Blob.data());
3855 if (Record[0]) {
3856 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3857 Buckets: F.IdentifierTableData + Record[0],
3858 Payload: F.IdentifierTableData + sizeof(uint32_t),
3859 Base: F.IdentifierTableData,
3860 InfoObj: ASTIdentifierLookupTrait(*this, F));
3861
3862 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3863 }
3864 break;
3865
3866 case IDENTIFIER_OFFSET: {
3867 if (F.LocalNumIdentifiers != 0)
3868 return llvm::createStringError(
3869 EC: std::errc::illegal_byte_sequence,
3870 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3871 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3872 F.LocalNumIdentifiers = Record[0];
3873 F.BaseIdentifierID = getTotalNumIdentifiers();
3874
3875 if (F.LocalNumIdentifiers > 0)
3876 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3877 + F.LocalNumIdentifiers);
3878 break;
3879 }
3880
3881 case INTERESTING_IDENTIFIERS:
3882 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3883 break;
3884
3885 case EAGERLY_DESERIALIZED_DECLS:
3886 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3887 // about "interesting" decls (for instance, if we're building a module).
3888 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3889 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3890 break;
3891
3892 case MODULAR_CODEGEN_DECLS:
3893 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3894 // them (ie: if we're not codegenerating this module).
3895 if (F.Kind == MK_MainFile ||
3896 getContext().getLangOpts().BuildingPCHWithObjectFile)
3897 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3898 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3899 break;
3900
3901 case SPECIAL_TYPES:
3902 if (SpecialTypes.empty()) {
3903 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3904 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3905 break;
3906 }
3907
3908 if (Record.empty())
3909 break;
3910
3911 if (SpecialTypes.size() != Record.size())
3912 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3913 Fmt: "invalid special-types record");
3914
3915 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3916 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3917 if (!SpecialTypes[I])
3918 SpecialTypes[I] = ID;
3919 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3920 // merge step?
3921 }
3922 break;
3923
3924 case STATISTICS:
3925 TotalNumStatements += Record[0];
3926 TotalNumMacros += Record[1];
3927 TotalLexicalDeclContexts += Record[2];
3928 TotalVisibleDeclContexts += Record[3];
3929 TotalModuleLocalVisibleDeclContexts += Record[4];
3930 TotalTULocalVisibleDeclContexts += Record[5];
3931 break;
3932
3933 case UNUSED_FILESCOPED_DECLS:
3934 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3935 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3936 break;
3937
3938 case DELEGATING_CTORS:
3939 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3940 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3941 break;
3942
3943 case WEAK_UNDECLARED_IDENTIFIERS:
3944 if (Record.size() % 3 != 0)
3945 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3946 Fmt: "invalid weak identifiers record");
3947
3948 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3949 // files. This isn't the way to do it :)
3950 WeakUndeclaredIdentifiers.clear();
3951
3952 // Translate the weak, undeclared identifiers into global IDs.
3953 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3954 WeakUndeclaredIdentifiers.push_back(
3955 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3956 WeakUndeclaredIdentifiers.push_back(
3957 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3958 WeakUndeclaredIdentifiers.push_back(
3959 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3960 }
3961 break;
3962
3963 case SELECTOR_OFFSETS: {
3964 F.SelectorOffsets = (const uint32_t *)Blob.data();
3965 F.LocalNumSelectors = Record[0];
3966 unsigned LocalBaseSelectorID = Record[1];
3967 F.BaseSelectorID = getTotalNumSelectors();
3968
3969 if (F.LocalNumSelectors > 0) {
3970 // Introduce the global -> local mapping for selectors within this
3971 // module.
3972 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
3973
3974 // Introduce the local -> global mapping for selectors within this
3975 // module.
3976 F.SelectorRemap.insertOrReplace(
3977 Val: std::make_pair(x&: LocalBaseSelectorID,
3978 y: F.BaseSelectorID - LocalBaseSelectorID));
3979
3980 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
3981 }
3982 break;
3983 }
3984
3985 case METHOD_POOL:
3986 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3987 if (Record[0])
3988 F.SelectorLookupTable
3989 = ASTSelectorLookupTable::Create(
3990 Buckets: F.SelectorLookupTableData + Record[0],
3991 Base: F.SelectorLookupTableData,
3992 InfoObj: ASTSelectorLookupTrait(*this, F));
3993 TotalNumMethodPoolEntries += Record[1];
3994 break;
3995
3996 case REFERENCED_SELECTOR_POOL:
3997 if (!Record.empty()) {
3998 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3999 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4000 LocalID: Record[Idx++]));
4001 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4002 getRawEncoding());
4003 }
4004 }
4005 break;
4006
4007 case PP_ASSUME_NONNULL_LOC: {
4008 unsigned Idx = 0;
4009 if (!Record.empty())
4010 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4011 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4012 break;
4013 }
4014
4015 case PP_UNSAFE_BUFFER_USAGE: {
4016 if (!Record.empty()) {
4017 SmallVector<SourceLocation, 64> SrcLocs;
4018 unsigned Idx = 0;
4019 while (Idx < Record.size())
4020 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4021 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4022 }
4023 break;
4024 }
4025
4026 case PP_CONDITIONAL_STACK:
4027 if (!Record.empty()) {
4028 unsigned Idx = 0, End = Record.size() - 1;
4029 bool ReachedEOFWhileSkipping = Record[Idx++];
4030 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4031 if (ReachedEOFWhileSkipping) {
4032 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4033 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4034 bool FoundNonSkipPortion = Record[Idx++];
4035 bool FoundElse = Record[Idx++];
4036 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4037 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4038 args&: FoundElse, args&: ElseLoc);
4039 }
4040 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4041 while (Idx < End) {
4042 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4043 bool WasSkipping = Record[Idx++];
4044 bool FoundNonSkip = Record[Idx++];
4045 bool FoundElse = Record[Idx++];
4046 ConditionalStack.push_back(
4047 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4048 }
4049 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4050 }
4051 break;
4052
4053 case PP_COUNTER_VALUE:
4054 if (!Record.empty() && Listener)
4055 Listener->ReadCounter(M: F, Value: Record[0]);
4056 break;
4057
4058 case FILE_SORTED_DECLS:
4059 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4060 F.NumFileSortedDecls = Record[0];
4061 break;
4062
4063 case SOURCE_LOCATION_OFFSETS: {
4064 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4065 F.LocalNumSLocEntries = Record[0];
4066 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4067 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4068 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4069 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4070 TotalSize: SLocSpaceSize);
4071 if (!F.SLocEntryBaseID) {
4072 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4073 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4074 return llvm::createStringError(EC: std::errc::invalid_argument,
4075 Fmt: "ran out of source locations");
4076 }
4077 // Make our entry in the range map. BaseID is negative and growing, so
4078 // we invert it. Because we invert it, though, we need the other end of
4079 // the range.
4080 unsigned RangeStart =
4081 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4082 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4083 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4084
4085 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4086 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4087 GlobalSLocOffsetMap.insert(
4088 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4089 - SLocSpaceSize,y: &F));
4090
4091 TotalNumSLocEntries += F.LocalNumSLocEntries;
4092 break;
4093 }
4094
4095 case MODULE_OFFSET_MAP:
4096 F.ModuleOffsetMap = Blob;
4097 break;
4098
4099 case SOURCE_MANAGER_LINE_TABLE:
4100 ParseLineTable(F, Record);
4101 break;
4102
4103 case EXT_VECTOR_DECLS:
4104 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4105 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4106 break;
4107
4108 case VTABLE_USES:
4109 if (Record.size() % 3 != 0)
4110 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4111 Fmt: "Invalid VTABLE_USES record");
4112
4113 // Later tables overwrite earlier ones.
4114 // FIXME: Modules will have some trouble with this. This is clearly not
4115 // the right way to do this.
4116 VTableUses.clear();
4117
4118 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4119 VTableUses.push_back(
4120 Elt: {.ID: ReadDeclID(F, Record, Idx),
4121 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4122 .Used: (bool)Record[Idx++]});
4123 }
4124 break;
4125
4126 case PENDING_IMPLICIT_INSTANTIATIONS:
4127
4128 if (Record.size() % 2 != 0)
4129 return llvm::createStringError(
4130 EC: std::errc::illegal_byte_sequence,
4131 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4132
4133 // For standard C++20 module, we will only reads the instantiations
4134 // if it is the main file.
4135 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4136 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4137 PendingInstantiations.push_back(
4138 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4139 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4140 }
4141 }
4142 break;
4143
4144 case SEMA_DECL_REFS:
4145 if (Record.size() != 3)
4146 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4147 Fmt: "Invalid SEMA_DECL_REFS block");
4148 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4149 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4150 break;
4151
4152 case PPD_ENTITIES_OFFSETS: {
4153 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4154 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4155 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4156
4157 unsigned StartingID;
4158 if (!PP.getPreprocessingRecord())
4159 PP.createPreprocessingRecord();
4160 if (!PP.getPreprocessingRecord()->getExternalSource())
4161 PP.getPreprocessingRecord()->SetExternalSource(*this);
4162 StartingID
4163 = PP.getPreprocessingRecord()
4164 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4165 F.BasePreprocessedEntityID = StartingID;
4166
4167 if (F.NumPreprocessedEntities > 0) {
4168 // Introduce the global -> local mapping for preprocessed entities in
4169 // this module.
4170 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4171 }
4172
4173 break;
4174 }
4175
4176 case PPD_SKIPPED_RANGES: {
4177 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4178 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4179 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4180
4181 if (!PP.getPreprocessingRecord())
4182 PP.createPreprocessingRecord();
4183 if (!PP.getPreprocessingRecord()->getExternalSource())
4184 PP.getPreprocessingRecord()->SetExternalSource(*this);
4185 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4186 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4187
4188 if (F.NumPreprocessedSkippedRanges > 0)
4189 GlobalSkippedRangeMap.insert(
4190 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4191 break;
4192 }
4193
4194 case DECL_UPDATE_OFFSETS:
4195 if (Record.size() % 2 != 0)
4196 return llvm::createStringError(
4197 EC: std::errc::illegal_byte_sequence,
4198 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4199 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4200 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4201 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4202
4203 // If we've already loaded the decl, perform the updates when we finish
4204 // loading this block.
4205 if (Decl *D = GetExistingDecl(ID))
4206 PendingUpdateRecords.push_back(
4207 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4208 }
4209 break;
4210
4211 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4212 if (Record.size() % 5 != 0)
4213 return llvm::createStringError(
4214 EC: std::errc::illegal_byte_sequence,
4215 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4216 "file");
4217 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4218 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4219
4220 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4221 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4222 uint64_t LocalLexicalOffset = Record[I++];
4223 uint64_t LexicalOffset =
4224 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4225 uint64_t LocalVisibleOffset = Record[I++];
4226 uint64_t VisibleOffset =
4227 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4228 uint64_t LocalModuleLocalOffset = Record[I++];
4229 uint64_t ModuleLocalOffset =
4230 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4231 uint64_t TULocalLocalOffset = Record[I++];
4232 uint64_t TULocalOffset =
4233 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4234
4235 DelayedNamespaceOffsetMap[ID] = {
4236 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4237
4238 assert(!GetExistingDecl(ID) &&
4239 "We shouldn't load the namespace in the front of delayed "
4240 "namespace lexical and visible block");
4241 }
4242 break;
4243 }
4244
4245 case RELATED_DECLS_MAP:
4246 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4247 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4248 auto &RelatedDecls = RelatedDeclsMap[ID];
4249 unsigned NN = Record[I++];
4250 RelatedDecls.reserve(N: NN);
4251 for (unsigned II = 0; II < NN; II++)
4252 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4253 }
4254 break;
4255
4256 case OBJC_CATEGORIES_MAP:
4257 if (F.LocalNumObjCCategoriesInMap != 0)
4258 return llvm::createStringError(
4259 EC: std::errc::illegal_byte_sequence,
4260 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4261
4262 F.LocalNumObjCCategoriesInMap = Record[0];
4263 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4264 break;
4265
4266 case OBJC_CATEGORIES:
4267 F.ObjCCategories.swap(RHS&: Record);
4268 break;
4269
4270 case CUDA_SPECIAL_DECL_REFS:
4271 // Later tables overwrite earlier ones.
4272 // FIXME: Modules will have trouble with this.
4273 CUDASpecialDeclRefs.clear();
4274 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4275 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4276 break;
4277
4278 case HEADER_SEARCH_TABLE:
4279 F.HeaderFileInfoTableData = Blob.data();
4280 F.LocalNumHeaderFileInfos = Record[1];
4281 if (Record[0]) {
4282 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4283 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4284 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4285 InfoObj: HeaderFileInfoTrait(*this, F));
4286
4287 PP.getHeaderSearchInfo().SetExternalSource(this);
4288 if (!PP.getHeaderSearchInfo().getExternalLookup())
4289 PP.getHeaderSearchInfo().SetExternalLookup(this);
4290 }
4291 break;
4292
4293 case FP_PRAGMA_OPTIONS:
4294 // Later tables overwrite earlier ones.
4295 FPPragmaOptions.swap(RHS&: Record);
4296 break;
4297
4298 case DECLS_WITH_EFFECTS_TO_VERIFY:
4299 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4300 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4301 break;
4302
4303 case OPENCL_EXTENSIONS:
4304 for (unsigned I = 0, E = Record.size(); I != E; ) {
4305 auto Name = ReadString(Record, Idx&: I);
4306 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4307 OptInfo.Supported = Record[I++] != 0;
4308 OptInfo.Enabled = Record[I++] != 0;
4309 OptInfo.WithPragma = Record[I++] != 0;
4310 OptInfo.Avail = Record[I++];
4311 OptInfo.Core = Record[I++];
4312 OptInfo.Opt = Record[I++];
4313 }
4314 break;
4315
4316 case TENTATIVE_DEFINITIONS:
4317 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4318 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4319 break;
4320
4321 case KNOWN_NAMESPACES:
4322 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4323 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4324 break;
4325
4326 case UNDEFINED_BUT_USED:
4327 if (Record.size() % 2 != 0)
4328 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4329 Fmt: "invalid undefined-but-used record");
4330 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4331 UndefinedButUsed.push_back(
4332 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4333 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4334 }
4335 break;
4336
4337 case DELETE_EXPRS_TO_ANALYZE:
4338 for (unsigned I = 0, N = Record.size(); I != N;) {
4339 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4340 const uint64_t Count = Record[I++];
4341 DelayedDeleteExprs.push_back(Elt: Count);
4342 for (uint64_t C = 0; C < Count; ++C) {
4343 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4344 bool IsArrayForm = Record[I++] == 1;
4345 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4346 }
4347 }
4348 break;
4349
4350 case VTABLES_TO_EMIT:
4351 if (F.Kind == MK_MainFile ||
4352 getContext().getLangOpts().BuildingPCHWithObjectFile)
4353 for (unsigned I = 0, N = Record.size(); I != N;)
4354 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4355 break;
4356
4357 case IMPORTED_MODULES:
4358 if (!F.isModule()) {
4359 // If we aren't loading a module (which has its own exports), make
4360 // all of the imported modules visible.
4361 // FIXME: Deal with macros-only imports.
4362 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4363 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4364 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4365 if (GlobalID) {
4366 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4367 if (DeserializationListener)
4368 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4369 }
4370 }
4371 }
4372 break;
4373
4374 case MACRO_OFFSET: {
4375 if (F.LocalNumMacros != 0)
4376 return llvm::createStringError(
4377 EC: std::errc::illegal_byte_sequence,
4378 Fmt: "duplicate MACRO_OFFSET record in AST file");
4379 F.MacroOffsets = (const uint32_t *)Blob.data();
4380 F.LocalNumMacros = Record[0];
4381 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4382 F.BaseMacroID = getTotalNumMacros();
4383
4384 if (F.LocalNumMacros > 0)
4385 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4386 break;
4387 }
4388
4389 case LATE_PARSED_TEMPLATE:
4390 LateParsedTemplates.emplace_back(
4391 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4392 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4393 break;
4394
4395 case OPTIMIZE_PRAGMA_OPTIONS:
4396 if (Record.size() != 1)
4397 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4398 Fmt: "invalid pragma optimize record");
4399 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4400 break;
4401
4402 case MSSTRUCT_PRAGMA_OPTIONS:
4403 if (Record.size() != 1)
4404 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4405 Fmt: "invalid pragma ms_struct record");
4406 PragmaMSStructState = Record[0];
4407 break;
4408
4409 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4410 if (Record.size() != 2)
4411 return llvm::createStringError(
4412 EC: std::errc::illegal_byte_sequence,
4413 Fmt: "invalid pragma pointers to members record");
4414 PragmaMSPointersToMembersState = Record[0];
4415 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4416 break;
4417
4418 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4419 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4420 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4421 break;
4422
4423 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4424 if (Record.size() != 1)
4425 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4426 Fmt: "invalid cuda pragma options record");
4427 ForceHostDeviceDepth = Record[0];
4428 break;
4429
4430 case ALIGN_PACK_PRAGMA_OPTIONS: {
4431 if (Record.size() < 3)
4432 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4433 Fmt: "invalid pragma pack record");
4434 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4435 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4436 unsigned NumStackEntries = Record[2];
4437 unsigned Idx = 3;
4438 // Reset the stack when importing a new module.
4439 PragmaAlignPackStack.clear();
4440 for (unsigned I = 0; I < NumStackEntries; ++I) {
4441 PragmaAlignPackStackEntry Entry;
4442 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4443 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4444 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4445 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4446 Entry.SlotLabel = PragmaAlignPackStrings.back();
4447 PragmaAlignPackStack.push_back(Elt: Entry);
4448 }
4449 break;
4450 }
4451
4452 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4453 if (Record.size() < 3)
4454 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4455 Fmt: "invalid pragma float control record");
4456 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4457 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4458 unsigned NumStackEntries = Record[2];
4459 unsigned Idx = 3;
4460 // Reset the stack when importing a new module.
4461 FpPragmaStack.clear();
4462 for (unsigned I = 0; I < NumStackEntries; ++I) {
4463 FpPragmaStackEntry Entry;
4464 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4465 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4466 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4467 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4468 Entry.SlotLabel = FpPragmaStrings.back();
4469 FpPragmaStack.push_back(Elt: Entry);
4470 }
4471 break;
4472 }
4473
4474 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4475 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4476 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4477 break;
4478
4479 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4480 unsigned NumRecords = Record.front();
4481 // Last record which is used to keep number of valid records.
4482 if (Record.size() - 1 != NumRecords)
4483 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4484 Fmt: "invalid rvv intrinsic pragma record");
4485
4486 if (RISCVVecIntrinsicPragma.empty())
4487 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4488 // There might be multiple precompiled modules imported, we need to union
4489 // them all.
4490 for (unsigned i = 0; i < NumRecords; ++i)
4491 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4492 break;
4493 }
4494 }
4495 }
4496}
4497
4498void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4499 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4500
4501 // Additional remapping information.
4502 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4503 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4504 F.ModuleOffsetMap = StringRef();
4505
4506 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4507 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4508 RemapBuilder SelectorRemap(F.SelectorRemap);
4509
4510 auto &ImportedModuleVector = F.TransitiveImports;
4511 assert(ImportedModuleVector.empty());
4512
4513 while (Data < DataEnd) {
4514 // FIXME: Looking up dependency modules by filename is horrible. Let's
4515 // start fixing this with prebuilt, explicit and implicit modules and see
4516 // how it goes...
4517 using namespace llvm::support;
4518 ModuleKind Kind = static_cast<ModuleKind>(
4519 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4520 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4521 StringRef Name = StringRef((const char*)Data, Len);
4522 Data += Len;
4523 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4524 Kind == MK_ImplicitModule
4525 ? ModuleMgr.lookupByModuleName(ModName: Name)
4526 : ModuleMgr.lookupByFileName(FileName: Name));
4527 if (!OM) {
4528 std::string Msg = "refers to unknown module, cannot find ";
4529 Msg.append(str: std::string(Name));
4530 Error(Msg);
4531 return;
4532 }
4533
4534 ImportedModuleVector.push_back(Elt: OM);
4535
4536 uint32_t SubmoduleIDOffset =
4537 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4538 uint32_t SelectorIDOffset =
4539 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4540
4541 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4542 RemapBuilder &Remap) {
4543 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4544 if (Offset != None)
4545 Remap.insert(Val: std::make_pair(x&: Offset,
4546 y: static_cast<int>(BaseOffset - Offset)));
4547 };
4548
4549 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4550 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4551 }
4552}
4553
4554ASTReader::ASTReadResult
4555ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4556 const ModuleFile *ImportedBy,
4557 unsigned ClientLoadCapabilities) {
4558 unsigned Idx = 0;
4559 F.ModuleMapPath = ReadPath(F, Record, Idx);
4560
4561 // Try to resolve ModuleName in the current header search context and
4562 // verify that it is found in the same module map file as we saved. If the
4563 // top-level AST file is a main file, skip this check because there is no
4564 // usable header search context.
4565 assert(!F.ModuleName.empty() &&
4566 "MODULE_NAME should come before MODULE_MAP_FILE");
4567 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4568 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4569 // An implicitly-loaded module file should have its module listed in some
4570 // module map file that we've already loaded.
4571 Module *M =
4572 PP.getHeaderSearchInfo().lookupModule(ModuleName: F.ModuleName, ImportLoc: F.ImportLoc);
4573 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4574 OptionalFileEntryRef ModMap =
4575 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4576 // Don't emit module relocation error if we have -fno-validate-pch
4577 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4578 DisableValidationForModuleKind::Module) &&
4579 !ModMap) {
4580 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4581 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4582 // This module was defined by an imported (explicit) module.
4583 Diag(DiagID: diag::err_module_file_conflict) << F.ModuleName << F.FileName
4584 << ASTFE->getName();
4585 // TODO: Add a note with the module map paths if they differ.
4586 } else {
4587 // This module was built with a different module map.
4588 Diag(DiagID: diag::err_imported_module_not_found)
4589 << F.ModuleName << F.FileName
4590 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4591 << !ImportedBy;
4592 // In case it was imported by a PCH, there's a chance the user is
4593 // just missing to include the search path to the directory containing
4594 // the modulemap.
4595 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4596 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4597 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4598 }
4599 }
4600 return OutOfDate;
4601 }
4602
4603 assert(M && M->Name == F.ModuleName && "found module with different name");
4604
4605 // Check the primary module map file.
4606 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4607 if (!StoredModMap || *StoredModMap != ModMap) {
4608 assert(ModMap && "found module is missing module map file");
4609 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4610 "top-level import should be verified");
4611 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4612 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4613 Diag(DiagID: diag::err_imported_module_modmap_changed)
4614 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4615 << ModMap->getName() << F.ModuleMapPath << NotImported;
4616 return OutOfDate;
4617 }
4618
4619 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4620 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4621 // FIXME: we should use input files rather than storing names.
4622 std::string Filename = ReadPath(F, Record, Idx);
4623 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4624 if (!SF) {
4625 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4626 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4627 return OutOfDate;
4628 }
4629 AdditionalStoredMaps.insert(V: *SF);
4630 }
4631
4632 // Check any additional module map files (e.g. module.private.modulemap)
4633 // that are not in the pcm.
4634 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4635 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4636 // Remove files that match
4637 // Note: SmallPtrSet::erase is really remove
4638 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4639 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4640 Diag(DiagID: diag::err_module_different_modmap)
4641 << F.ModuleName << /*new*/0 << ModMap.getName();
4642 return OutOfDate;
4643 }
4644 }
4645 }
4646
4647 // Check any additional module map files that are in the pcm, but not
4648 // found in header search. Cases that match are already removed.
4649 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4650 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4651 Diag(DiagID: diag::err_module_different_modmap)
4652 << F.ModuleName << /*not new*/1 << ModMap.getName();
4653 return OutOfDate;
4654 }
4655 }
4656
4657 if (Listener)
4658 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4659 return Success;
4660}
4661
4662/// Move the given method to the back of the global list of methods.
4663static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4664 // Find the entry for this selector in the method pool.
4665 SemaObjC::GlobalMethodPool::iterator Known =
4666 S.ObjC().MethodPool.find(Val: Method->getSelector());
4667 if (Known == S.ObjC().MethodPool.end())
4668 return;
4669
4670 // Retrieve the appropriate method list.
4671 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4672 : Known->second.second;
4673 bool Found = false;
4674 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4675 if (!Found) {
4676 if (List->getMethod() == Method) {
4677 Found = true;
4678 } else {
4679 // Keep searching.
4680 continue;
4681 }
4682 }
4683
4684 if (List->getNext())
4685 List->setMethod(List->getNext()->getMethod());
4686 else
4687 List->setMethod(Method);
4688 }
4689}
4690
4691void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4692 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4693 for (Decl *D : Names) {
4694 bool wasHidden = !D->isUnconditionallyVisible();
4695 D->setVisibleDespiteOwningModule();
4696
4697 if (wasHidden && SemaObj) {
4698 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4699 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4700 }
4701 }
4702 }
4703}
4704
4705void ASTReader::makeModuleVisible(Module *Mod,
4706 Module::NameVisibilityKind NameVisibility,
4707 SourceLocation ImportLoc) {
4708 llvm::SmallPtrSet<Module *, 4> Visited;
4709 SmallVector<Module *, 4> Stack;
4710 Stack.push_back(Elt: Mod);
4711 while (!Stack.empty()) {
4712 Mod = Stack.pop_back_val();
4713
4714 if (NameVisibility <= Mod->NameVisibility) {
4715 // This module already has this level of visibility (or greater), so
4716 // there is nothing more to do.
4717 continue;
4718 }
4719
4720 if (Mod->isUnimportable()) {
4721 // Modules that aren't importable cannot be made visible.
4722 continue;
4723 }
4724
4725 // Update the module's name visibility.
4726 Mod->NameVisibility = NameVisibility;
4727
4728 // If we've already deserialized any names from this module,
4729 // mark them as visible.
4730 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4731 if (Hidden != HiddenNamesMap.end()) {
4732 auto HiddenNames = std::move(*Hidden);
4733 HiddenNamesMap.erase(I: Hidden);
4734 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4735 assert(!HiddenNamesMap.contains(Mod) &&
4736 "making names visible added hidden names");
4737 }
4738
4739 // Push any exported modules onto the stack to be marked as visible.
4740 SmallVector<Module *, 16> Exports;
4741 Mod->getExportedModules(Exported&: Exports);
4742 for (SmallVectorImpl<Module *>::iterator
4743 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4744 Module *Exported = *I;
4745 if (Visited.insert(Ptr: Exported).second)
4746 Stack.push_back(Elt: Exported);
4747 }
4748 }
4749}
4750
4751/// We've merged the definition \p MergedDef into the existing definition
4752/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4753/// visible.
4754void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4755 NamedDecl *MergedDef) {
4756 if (!Def->isUnconditionallyVisible()) {
4757 // If MergedDef is visible or becomes visible, make the definition visible.
4758 if (MergedDef->isUnconditionallyVisible())
4759 Def->setVisibleDespiteOwningModule();
4760 else {
4761 getContext().mergeDefinitionIntoModule(
4762 ND: Def, M: MergedDef->getImportedOwningModule(),
4763 /*NotifyListeners*/ false);
4764 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4765 }
4766 }
4767}
4768
4769bool ASTReader::loadGlobalIndex() {
4770 if (GlobalIndex)
4771 return false;
4772
4773 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4774 !PP.getLangOpts().Modules)
4775 return true;
4776
4777 // Try to load the global index.
4778 TriedLoadingGlobalIndex = true;
4779 StringRef SpecificModuleCachePath =
4780 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4781 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4782 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4783 if (llvm::Error Err = std::move(Result.second)) {
4784 assert(!Result.first);
4785 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4786 return true;
4787 }
4788
4789 GlobalIndex.reset(p: Result.first);
4790 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4791 return false;
4792}
4793
4794bool ASTReader::isGlobalIndexUnavailable() const {
4795 return PP.getLangOpts().Modules && UseGlobalIndex &&
4796 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4797}
4798
4799/// Given a cursor at the start of an AST file, scan ahead and drop the
4800/// cursor into the start of the given block ID, returning false on success and
4801/// true on failure.
4802static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4803 while (true) {
4804 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4805 if (!MaybeEntry) {
4806 // FIXME this drops errors on the floor.
4807 consumeError(Err: MaybeEntry.takeError());
4808 return true;
4809 }
4810 llvm::BitstreamEntry Entry = MaybeEntry.get();
4811
4812 switch (Entry.Kind) {
4813 case llvm::BitstreamEntry::Error:
4814 case llvm::BitstreamEntry::EndBlock:
4815 return true;
4816
4817 case llvm::BitstreamEntry::Record:
4818 // Ignore top-level records.
4819 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4820 break;
4821 else {
4822 // FIXME this drops errors on the floor.
4823 consumeError(Err: Skipped.takeError());
4824 return true;
4825 }
4826
4827 case llvm::BitstreamEntry::SubBlock:
4828 if (Entry.ID == BlockID) {
4829 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4830 // FIXME this drops the error on the floor.
4831 consumeError(Err: std::move(Err));
4832 return true;
4833 }
4834 // Found it!
4835 return false;
4836 }
4837
4838 if (llvm::Error Err = Cursor.SkipBlock()) {
4839 // FIXME this drops the error on the floor.
4840 consumeError(Err: std::move(Err));
4841 return true;
4842 }
4843 }
4844 }
4845}
4846
4847ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4848 SourceLocation ImportLoc,
4849 unsigned ClientLoadCapabilities,
4850 ModuleFile **NewLoadedModuleFile) {
4851 llvm::TimeTraceScope scope("ReadAST", FileName);
4852
4853 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4854 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4855 CurrentDeserializingModuleKind, Type);
4856
4857 // Defer any pending actions until we get to the end of reading the AST file.
4858 Deserializing AnASTFile(this);
4859
4860 // Bump the generation number.
4861 unsigned PreviousGeneration = 0;
4862 if (ContextObj)
4863 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4864
4865 unsigned NumModules = ModuleMgr.size();
4866 SmallVector<ImportedModule, 4> Loaded;
4867 if (ASTReadResult ReadResult =
4868 ReadASTCore(FileName, Type, ImportLoc,
4869 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4870 ClientLoadCapabilities)) {
4871 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4872
4873 // If we find that any modules are unusable, the global index is going
4874 // to be out-of-date. Just remove it.
4875 GlobalIndex.reset();
4876 ModuleMgr.setGlobalIndex(nullptr);
4877 return ReadResult;
4878 }
4879
4880 if (NewLoadedModuleFile && !Loaded.empty())
4881 *NewLoadedModuleFile = Loaded.back().Mod;
4882
4883 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4884 // remove modules from this point. Various fields are updated during reading
4885 // the AST block and removing the modules would result in dangling pointers.
4886 // They are generally only incidentally dereferenced, ie. a binary search
4887 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4888 // be dereferenced but it wouldn't actually be used.
4889
4890 // Load the AST blocks of all of the modules that we loaded. We can still
4891 // hit errors parsing the ASTs at this point.
4892 for (ImportedModule &M : Loaded) {
4893 ModuleFile &F = *M.Mod;
4894 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4895
4896 // Read the AST block.
4897 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4898 Error(Err: std::move(Err));
4899 return Failure;
4900 }
4901
4902 // The AST block should always have a definition for the main module.
4903 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4904 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
4905 return Failure;
4906 }
4907
4908 // Read the extension blocks.
4909 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
4910 if (llvm::Error Err = ReadExtensionBlock(F)) {
4911 Error(Err: std::move(Err));
4912 return Failure;
4913 }
4914 }
4915
4916 // Once read, set the ModuleFile bit base offset and update the size in
4917 // bits of all files we've seen.
4918 F.GlobalBitOffset = TotalModulesSizeInBits;
4919 TotalModulesSizeInBits += F.SizeInBits;
4920 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
4921 }
4922
4923 // Preload source locations and interesting indentifiers.
4924 for (ImportedModule &M : Loaded) {
4925 ModuleFile &F = *M.Mod;
4926
4927 // Map the original source file ID into the ID space of the current
4928 // compilation.
4929 if (F.OriginalSourceFileID.isValid())
4930 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
4931
4932 for (auto Offset : F.PreloadIdentifierOffsets) {
4933 const unsigned char *Data = F.IdentifierTableData + Offset;
4934
4935 ASTIdentifierLookupTrait Trait(*this, F);
4936 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
4937 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
4938
4939 IdentifierInfo *II;
4940 if (!PP.getLangOpts().CPlusPlus) {
4941 // Identifiers present in both the module file and the importing
4942 // instance are marked out-of-date so that they can be deserialized
4943 // on next use via ASTReader::updateOutOfDateIdentifier().
4944 // Identifiers present in the module file but not in the importing
4945 // instance are ignored for now, preventing growth of the identifier
4946 // table. They will be deserialized on first use via ASTReader::get().
4947 auto It = PP.getIdentifierTable().find(Name: Key);
4948 if (It == PP.getIdentifierTable().end())
4949 continue;
4950 II = It->second;
4951 } else {
4952 // With C++ modules, not many identifiers are considered interesting.
4953 // All identifiers in the module file can be placed into the identifier
4954 // table of the importing instance and marked as out-of-date. This makes
4955 // ASTReader::get() a no-op, and deserialization will take place on
4956 // first/next use via ASTReader::updateOutOfDateIdentifier().
4957 II = &PP.getIdentifierTable().getOwn(Name: Key);
4958 }
4959
4960 II->setOutOfDate(true);
4961
4962 // Mark this identifier as being from an AST file so that we can track
4963 // whether we need to serialize it.
4964 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
4965
4966 // Associate the ID with the identifier so that the writer can reuse it.
4967 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
4968 SetIdentifierInfo(ID, II);
4969 }
4970 }
4971
4972 // Builtins and library builtins have already been initialized. Mark all
4973 // identifiers as out-of-date, so that they are deserialized on first use.
4974 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4975 for (auto &Id : PP.getIdentifierTable())
4976 Id.second->setOutOfDate(true);
4977
4978 // Mark selectors as out of date.
4979 for (const auto &Sel : SelectorGeneration)
4980 SelectorOutOfDate[Sel.first] = true;
4981
4982 // Setup the import locations and notify the module manager that we've
4983 // committed to these module files.
4984 for (ImportedModule &M : Loaded) {
4985 ModuleFile &F = *M.Mod;
4986
4987 ModuleMgr.moduleFileAccepted(MF: &F);
4988
4989 // Set the import location.
4990 F.DirectImportLoc = ImportLoc;
4991 // FIXME: We assume that locations from PCH / preamble do not need
4992 // any translation.
4993 if (!M.ImportedBy)
4994 F.ImportLoc = M.ImportLoc;
4995 else
4996 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
4997 }
4998
4999 // Resolve any unresolved module exports.
5000 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
5001 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
5002 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
5003 Module *ResolvedMod = getSubmodule(GlobalID);
5004
5005 switch (Unresolved.Kind) {
5006 case UnresolvedModuleRef::Conflict:
5007 if (ResolvedMod) {
5008 Module::Conflict Conflict;
5009 Conflict.Other = ResolvedMod;
5010 Conflict.Message = Unresolved.String.str();
5011 Unresolved.Mod->Conflicts.push_back(x: Conflict);
5012 }
5013 continue;
5014
5015 case UnresolvedModuleRef::Import:
5016 if (ResolvedMod)
5017 Unresolved.Mod->Imports.insert(X: ResolvedMod);
5018 continue;
5019
5020 case UnresolvedModuleRef::Affecting:
5021 if (ResolvedMod)
5022 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
5023 continue;
5024
5025 case UnresolvedModuleRef::Export:
5026 if (ResolvedMod || Unresolved.IsWildcard)
5027 Unresolved.Mod->Exports.push_back(
5028 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
5029 continue;
5030 }
5031 }
5032 UnresolvedModuleRefs.clear();
5033
5034 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5035 // Might be unnecessary as use declarations are only used to build the
5036 // module itself.
5037
5038 if (ContextObj)
5039 InitializeContext();
5040
5041 if (SemaObj)
5042 UpdateSema();
5043
5044 if (DeserializationListener)
5045 DeserializationListener->ReaderInitialized(Reader: this);
5046
5047 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5048 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5049 // If this AST file is a precompiled preamble, then set the
5050 // preamble file ID of the source manager to the file source file
5051 // from which the preamble was built.
5052 if (Type == MK_Preamble) {
5053 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5054 } else if (Type == MK_MainFile) {
5055 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5056 }
5057 }
5058
5059 // For any Objective-C class definitions we have already loaded, make sure
5060 // that we load any additional categories.
5061 if (ContextObj) {
5062 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5063 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5064 D: ObjCClassesLoaded[I], PreviousGeneration);
5065 }
5066 }
5067
5068 const HeaderSearchOptions &HSOpts =
5069 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5070 if (HSOpts.ModulesValidateOncePerBuildSession) {
5071 // Now we are certain that the module and all modules it depends on are
5072 // up-to-date. For implicitly-built module files, ensure the corresponding
5073 // timestamp files are up-to-date in this build session.
5074 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5075 ImportedModule &M = Loaded[I];
5076 if (M.Mod->Kind == MK_ImplicitModule &&
5077 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5078 getModuleManager().getModuleCache().updateModuleTimestamp(
5079 ModuleFilename: M.Mod->FileName);
5080 }
5081 }
5082
5083 return Success;
5084}
5085
5086static ASTFileSignature readASTFileSignature(StringRef PCH);
5087
5088/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5089static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5090 // FIXME checking magic headers is done in other places such as
5091 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5092 // always done the same. Unify it all with a helper.
5093 if (!Stream.canSkipToPos(pos: 4))
5094 return llvm::createStringError(
5095 EC: std::errc::illegal_byte_sequence,
5096 Fmt: "file too small to contain precompiled file magic");
5097 for (unsigned C : {'C', 'P', 'C', 'H'})
5098 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5099 if (Res.get() != C)
5100 return llvm::createStringError(
5101 EC: std::errc::illegal_byte_sequence,
5102 Fmt: "file doesn't start with precompiled file magic");
5103 } else
5104 return Res.takeError();
5105 return llvm::Error::success();
5106}
5107
5108static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5109 switch (Kind) {
5110 case MK_PCH:
5111 return 0; // PCH
5112 case MK_ImplicitModule:
5113 case MK_ExplicitModule:
5114 case MK_PrebuiltModule:
5115 return 1; // module
5116 case MK_MainFile:
5117 case MK_Preamble:
5118 return 2; // main source file
5119 }
5120 llvm_unreachable("unknown module kind");
5121}
5122
5123ASTReader::ASTReadResult
5124ASTReader::ReadASTCore(StringRef FileName,
5125 ModuleKind Type,
5126 SourceLocation ImportLoc,
5127 ModuleFile *ImportedBy,
5128 SmallVectorImpl<ImportedModule> &Loaded,
5129 off_t ExpectedSize, time_t ExpectedModTime,
5130 ASTFileSignature ExpectedSignature,
5131 unsigned ClientLoadCapabilities) {
5132 ModuleFile *M;
5133 std::string ErrorStr;
5134 ModuleManager::AddModuleResult AddResult
5135 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
5136 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
5137 ExpectedSignature, ReadSignature: readASTFileSignature,
5138 Module&: M, ErrorStr);
5139
5140 switch (AddResult) {
5141 case ModuleManager::AlreadyLoaded:
5142 Diag(DiagID: diag::remark_module_import)
5143 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5144 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5145 return Success;
5146
5147 case ModuleManager::NewlyLoaded:
5148 // Load module file below.
5149 break;
5150
5151 case ModuleManager::Missing:
5152 // The module file was missing; if the client can handle that, return
5153 // it.
5154 if (ClientLoadCapabilities & ARR_Missing)
5155 return Missing;
5156
5157 // Otherwise, return an error.
5158 Diag(DiagID: diag::err_ast_file_not_found)
5159 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5160 << ErrorStr;
5161 return Failure;
5162
5163 case ModuleManager::OutOfDate:
5164 // We couldn't load the module file because it is out-of-date. If the
5165 // client can handle out-of-date, return it.
5166 if (ClientLoadCapabilities & ARR_OutOfDate)
5167 return OutOfDate;
5168
5169 // Otherwise, return an error.
5170 Diag(DiagID: diag::err_ast_file_out_of_date)
5171 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5172 << ErrorStr;
5173 return Failure;
5174 }
5175
5176 assert(M && "Missing module file");
5177
5178 bool ShouldFinalizePCM = false;
5179 llvm::scope_exit FinalizeOrDropPCM([&]() {
5180 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5181 if (ShouldFinalizePCM)
5182 MC.finalizePCM(Filename: FileName);
5183 else
5184 MC.tryToDropPCM(Filename: FileName);
5185 });
5186 ModuleFile &F = *M;
5187 BitstreamCursor &Stream = F.Stream;
5188 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5189 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5190
5191 // Sniff for the signature.
5192 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5193 Diag(DiagID: diag::err_ast_file_invalid)
5194 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5195 return Failure;
5196 }
5197
5198 // This is used for compatibility with older PCH formats.
5199 bool HaveReadControlBlock = false;
5200 while (true) {
5201 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5202 if (!MaybeEntry) {
5203 Error(Err: MaybeEntry.takeError());
5204 return Failure;
5205 }
5206 llvm::BitstreamEntry Entry = MaybeEntry.get();
5207
5208 switch (Entry.Kind) {
5209 case llvm::BitstreamEntry::Error:
5210 case llvm::BitstreamEntry::Record:
5211 case llvm::BitstreamEntry::EndBlock:
5212 Error(Msg: "invalid record at top-level of AST file");
5213 return Failure;
5214
5215 case llvm::BitstreamEntry::SubBlock:
5216 break;
5217 }
5218
5219 switch (Entry.ID) {
5220 case CONTROL_BLOCK_ID:
5221 HaveReadControlBlock = true;
5222 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5223 case Success:
5224 // Check that we didn't try to load a non-module AST file as a module.
5225 //
5226 // FIXME: Should we also perform the converse check? Loading a module as
5227 // a PCH file sort of works, but it's a bit wonky.
5228 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5229 Type == MK_PrebuiltModule) &&
5230 F.ModuleName.empty()) {
5231 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5232 if (Result != OutOfDate ||
5233 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5234 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5235 return Result;
5236 }
5237 break;
5238
5239 case Failure: return Failure;
5240 case Missing: return Missing;
5241 case OutOfDate: return OutOfDate;
5242 case VersionMismatch: return VersionMismatch;
5243 case ConfigurationMismatch: return ConfigurationMismatch;
5244 case HadErrors: return HadErrors;
5245 }
5246 break;
5247
5248 case AST_BLOCK_ID:
5249 if (!HaveReadControlBlock) {
5250 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5251 Diag(DiagID: diag::err_ast_file_version_too_old)
5252 << moduleKindForDiagnostic(Kind: Type) << FileName;
5253 return VersionMismatch;
5254 }
5255
5256 // Record that we've loaded this module.
5257 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5258 ShouldFinalizePCM = true;
5259 return Success;
5260
5261 default:
5262 if (llvm::Error Err = Stream.SkipBlock()) {
5263 Error(Err: std::move(Err));
5264 return Failure;
5265 }
5266 break;
5267 }
5268 }
5269
5270 llvm_unreachable("unexpected break; expected return");
5271}
5272
5273ASTReader::ASTReadResult
5274ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5275 unsigned ClientLoadCapabilities) {
5276 const HeaderSearchOptions &HSOpts =
5277 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5278 bool AllowCompatibleConfigurationMismatch =
5279 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5280 bool DisableValidation = shouldDisableValidationForFile(M: F);
5281
5282 ASTReadResult Result = readUnhashedControlBlockImpl(
5283 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5284 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5285 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5286
5287 // If F was directly imported by another module, it's implicitly validated by
5288 // the importing module.
5289 if (DisableValidation || WasImportedBy ||
5290 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5291 return Success;
5292
5293 if (Result == Failure) {
5294 Error(Msg: "malformed block record in AST file");
5295 return Failure;
5296 }
5297
5298 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5299 // If this module has already been finalized in the ModuleCache, we're stuck
5300 // with it; we can only load a single version of each module.
5301 //
5302 // This can happen when a module is imported in two contexts: in one, as a
5303 // user module; in another, as a system module (due to an import from
5304 // another module marked with the [system] flag). It usually indicates a
5305 // bug in the module map: this module should also be marked with [system].
5306 //
5307 // If -Wno-system-headers (the default), and the first import is as a
5308 // system module, then validation will fail during the as-user import,
5309 // since -Werror flags won't have been validated. However, it's reasonable
5310 // to treat this consistently as a system module.
5311 //
5312 // If -Wsystem-headers, the PCM on disk was built with
5313 // -Wno-system-headers, and the first import is as a user module, then
5314 // validation will fail during the as-system import since the PCM on disk
5315 // doesn't guarantee that -Werror was respected. However, the -Werror
5316 // flags were checked during the initial as-user import.
5317 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5318 Filename: F.FileName)) {
5319 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5320 return Success;
5321 }
5322 }
5323
5324 return Result;
5325}
5326
5327ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5328 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5329 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5330 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5331 // Initialize a stream.
5332 BitstreamCursor Stream(StreamData);
5333
5334 // Sniff for the signature.
5335 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5336 // FIXME this drops the error on the floor.
5337 consumeError(Err: std::move(Err));
5338 return Failure;
5339 }
5340
5341 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5342 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5343 return Failure;
5344
5345 // Read all of the records in the options block.
5346 RecordData Record;
5347 ASTReadResult Result = Success;
5348 while (true) {
5349 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5350 if (!MaybeEntry) {
5351 // FIXME this drops the error on the floor.
5352 consumeError(Err: MaybeEntry.takeError());
5353 return Failure;
5354 }
5355 llvm::BitstreamEntry Entry = MaybeEntry.get();
5356
5357 switch (Entry.Kind) {
5358 case llvm::BitstreamEntry::Error:
5359 case llvm::BitstreamEntry::SubBlock:
5360 return Failure;
5361
5362 case llvm::BitstreamEntry::EndBlock:
5363 return Result;
5364
5365 case llvm::BitstreamEntry::Record:
5366 // The interesting case.
5367 break;
5368 }
5369
5370 // Read and process a record.
5371 Record.clear();
5372 StringRef Blob;
5373 Expected<unsigned> MaybeRecordType =
5374 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5375 if (!MaybeRecordType) {
5376 // FIXME this drops the error.
5377 return Failure;
5378 }
5379 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5380 case SIGNATURE:
5381 if (F) {
5382 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5383 assert(F->Signature != ASTFileSignature::createDummy() &&
5384 "Dummy AST file signature not backpatched in ASTWriter.");
5385 }
5386 break;
5387 case AST_BLOCK_HASH:
5388 if (F) {
5389 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5390 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5391 "Dummy AST block hash not backpatched in ASTWriter.");
5392 }
5393 break;
5394 case DIAGNOSTIC_OPTIONS: {
5395 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5396 if (Listener && ValidateDiagnosticOptions &&
5397 !AllowCompatibleConfigurationMismatch &&
5398 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5399 Result = OutOfDate; // Don't return early. Read the signature.
5400 break;
5401 }
5402 case HEADER_SEARCH_PATHS: {
5403 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5404 if (Listener && !AllowCompatibleConfigurationMismatch &&
5405 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5406 Result = ConfigurationMismatch;
5407 break;
5408 }
5409 case DIAG_PRAGMA_MAPPINGS:
5410 if (!F)
5411 break;
5412 if (F->PragmaDiagMappings.empty())
5413 F->PragmaDiagMappings.swap(RHS&: Record);
5414 else
5415 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5416 From: Record.begin(), To: Record.end());
5417 break;
5418 case HEADER_SEARCH_ENTRY_USAGE:
5419 if (F)
5420 F->SearchPathUsage = ReadBitVector(Record, Blob);
5421 break;
5422 case VFS_USAGE:
5423 if (F)
5424 F->VFSUsage = ReadBitVector(Record, Blob);
5425 break;
5426 }
5427 }
5428}
5429
5430/// Parse a record and blob containing module file extension metadata.
5431static bool parseModuleFileExtensionMetadata(
5432 const SmallVectorImpl<uint64_t> &Record,
5433 StringRef Blob,
5434 ModuleFileExtensionMetadata &Metadata) {
5435 if (Record.size() < 4) return true;
5436
5437 Metadata.MajorVersion = Record[0];
5438 Metadata.MinorVersion = Record[1];
5439
5440 unsigned BlockNameLen = Record[2];
5441 unsigned UserInfoLen = Record[3];
5442
5443 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5444
5445 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5446 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5447 Blob.data() + BlockNameLen + UserInfoLen);
5448 return false;
5449}
5450
5451llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5452 BitstreamCursor &Stream = F.Stream;
5453
5454 RecordData Record;
5455 while (true) {
5456 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5457 if (!MaybeEntry)
5458 return MaybeEntry.takeError();
5459 llvm::BitstreamEntry Entry = MaybeEntry.get();
5460
5461 switch (Entry.Kind) {
5462 case llvm::BitstreamEntry::SubBlock:
5463 if (llvm::Error Err = Stream.SkipBlock())
5464 return Err;
5465 continue;
5466 case llvm::BitstreamEntry::EndBlock:
5467 return llvm::Error::success();
5468 case llvm::BitstreamEntry::Error:
5469 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5470 Fmt: "malformed block record in AST file");
5471 case llvm::BitstreamEntry::Record:
5472 break;
5473 }
5474
5475 Record.clear();
5476 StringRef Blob;
5477 Expected<unsigned> MaybeRecCode =
5478 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5479 if (!MaybeRecCode)
5480 return MaybeRecCode.takeError();
5481 switch (MaybeRecCode.get()) {
5482 case EXTENSION_METADATA: {
5483 ModuleFileExtensionMetadata Metadata;
5484 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5485 return llvm::createStringError(
5486 EC: std::errc::illegal_byte_sequence,
5487 Fmt: "malformed EXTENSION_METADATA in AST file");
5488
5489 // Find a module file extension with this block name.
5490 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5491 if (Known == ModuleFileExtensions.end()) break;
5492
5493 // Form a reader.
5494 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5495 Mod&: F, Stream)) {
5496 F.ExtensionReaders.push_back(x: std::move(Reader));
5497 }
5498
5499 break;
5500 }
5501 }
5502 }
5503
5504 llvm_unreachable("ReadExtensionBlock should return from while loop");
5505}
5506
5507void ASTReader::InitializeContext() {
5508 assert(ContextObj && "no context to initialize");
5509 ASTContext &Context = *ContextObj;
5510
5511 // If there's a listener, notify them that we "read" the translation unit.
5512 if (DeserializationListener)
5513 DeserializationListener->DeclRead(
5514 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5515 D: Context.getTranslationUnitDecl());
5516
5517 // FIXME: Find a better way to deal with collisions between these
5518 // built-in types. Right now, we just ignore the problem.
5519
5520 // Load the special types.
5521 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5522 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5523 if (!Context.CFConstantStringTypeDecl)
5524 Context.setCFConstantStringType(GetType(ID: String));
5525 }
5526
5527 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5528 QualType FileType = GetType(ID: File);
5529 if (FileType.isNull()) {
5530 Error(Msg: "FILE type is NULL");
5531 return;
5532 }
5533
5534 if (!Context.FILEDecl) {
5535 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5536 Context.setFILEDecl(Typedef->getDecl());
5537 else {
5538 const TagType *Tag = FileType->getAs<TagType>();
5539 if (!Tag) {
5540 Error(Msg: "Invalid FILE type in AST file");
5541 return;
5542 }
5543 Context.setFILEDecl(Tag->getDecl());
5544 }
5545 }
5546 }
5547
5548 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5549 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5550 if (Jmp_bufType.isNull()) {
5551 Error(Msg: "jmp_buf type is NULL");
5552 return;
5553 }
5554
5555 if (!Context.jmp_bufDecl) {
5556 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5557 Context.setjmp_bufDecl(Typedef->getDecl());
5558 else {
5559 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5560 if (!Tag) {
5561 Error(Msg: "Invalid jmp_buf type in AST file");
5562 return;
5563 }
5564 Context.setjmp_bufDecl(Tag->getDecl());
5565 }
5566 }
5567 }
5568
5569 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5570 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5571 if (Sigjmp_bufType.isNull()) {
5572 Error(Msg: "sigjmp_buf type is NULL");
5573 return;
5574 }
5575
5576 if (!Context.sigjmp_bufDecl) {
5577 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5578 Context.setsigjmp_bufDecl(Typedef->getDecl());
5579 else {
5580 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5581 assert(Tag && "Invalid sigjmp_buf type in AST file");
5582 Context.setsigjmp_bufDecl(Tag->getDecl());
5583 }
5584 }
5585 }
5586
5587 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5588 if (Context.ObjCIdRedefinitionType.isNull())
5589 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5590 }
5591
5592 if (TypeID ObjCClassRedef =
5593 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5594 if (Context.ObjCClassRedefinitionType.isNull())
5595 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5596 }
5597
5598 if (TypeID ObjCSelRedef =
5599 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5600 if (Context.ObjCSelRedefinitionType.isNull())
5601 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5602 }
5603
5604 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5605 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5606 if (Ucontext_tType.isNull()) {
5607 Error(Msg: "ucontext_t type is NULL");
5608 return;
5609 }
5610
5611 if (!Context.ucontext_tDecl) {
5612 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5613 Context.setucontext_tDecl(Typedef->getDecl());
5614 else {
5615 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5616 assert(Tag && "Invalid ucontext_t type in AST file");
5617 Context.setucontext_tDecl(Tag->getDecl());
5618 }
5619 }
5620 }
5621 }
5622
5623 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5624
5625 // If there were any CUDA special declarations, deserialize them.
5626 if (!CUDASpecialDeclRefs.empty()) {
5627 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5628 Context.setcudaConfigureCallDecl(
5629 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5630 Context.setcudaGetParameterBufferDecl(
5631 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5632 Context.setcudaLaunchDeviceDecl(
5633 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5634 }
5635
5636 // Re-export any modules that were imported by a non-module AST file.
5637 // FIXME: This does not make macro-only imports visible again.
5638 for (auto &Import : PendingImportedModules) {
5639 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5640 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5641 /*ImportLoc=*/Import.ImportLoc);
5642 if (Import.ImportLoc.isValid())
5643 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5644 // This updates visibility for Preprocessor only. For Sema, which can be
5645 // nullptr here, we do the same later, in UpdateSema().
5646 }
5647 }
5648
5649 // Hand off these modules to Sema.
5650 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5651 PendingImportedModules.clear();
5652}
5653
5654void ASTReader::finalizeForWriting() {
5655 // Nothing to do for now.
5656}
5657
5658/// Reads and return the signature record from \p PCH's control block, or
5659/// else returns 0.
5660static ASTFileSignature readASTFileSignature(StringRef PCH) {
5661 BitstreamCursor Stream(PCH);
5662 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5663 // FIXME this drops the error on the floor.
5664 consumeError(Err: std::move(Err));
5665 return ASTFileSignature();
5666 }
5667
5668 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5669 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5670 return ASTFileSignature();
5671
5672 // Scan for SIGNATURE inside the diagnostic options block.
5673 ASTReader::RecordData Record;
5674 while (true) {
5675 Expected<llvm::BitstreamEntry> MaybeEntry =
5676 Stream.advanceSkippingSubblocks();
5677 if (!MaybeEntry) {
5678 // FIXME this drops the error on the floor.
5679 consumeError(Err: MaybeEntry.takeError());
5680 return ASTFileSignature();
5681 }
5682 llvm::BitstreamEntry Entry = MaybeEntry.get();
5683
5684 if (Entry.Kind != llvm::BitstreamEntry::Record)
5685 return ASTFileSignature();
5686
5687 Record.clear();
5688 StringRef Blob;
5689 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5690 if (!MaybeRecord) {
5691 // FIXME this drops the error on the floor.
5692 consumeError(Err: MaybeRecord.takeError());
5693 return ASTFileSignature();
5694 }
5695 if (SIGNATURE == MaybeRecord.get()) {
5696 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5697 assert(Signature != ASTFileSignature::createDummy() &&
5698 "Dummy AST file signature not backpatched in ASTWriter.");
5699 return Signature;
5700 }
5701 }
5702}
5703
5704/// Retrieve the name of the original source file name
5705/// directly from the AST file, without actually loading the AST
5706/// file.
5707std::string ASTReader::getOriginalSourceFile(
5708 const std::string &ASTFileName, FileManager &FileMgr,
5709 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5710 // Open the AST file.
5711 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5712 /*RequiresNullTerminator=*/false,
5713 /*MaybeLimit=*/std::nullopt,
5714 /*IsText=*/false);
5715 if (!Buffer) {
5716 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5717 << ASTFileName << Buffer.getError().message();
5718 return std::string();
5719 }
5720
5721 // Initialize the stream
5722 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5723
5724 // Sniff for the signature.
5725 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5726 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5727 return std::string();
5728 }
5729
5730 // Scan for the CONTROL_BLOCK_ID block.
5731 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5732 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5733 return std::string();
5734 }
5735
5736 // Scan for ORIGINAL_FILE inside the control block.
5737 RecordData Record;
5738 while (true) {
5739 Expected<llvm::BitstreamEntry> MaybeEntry =
5740 Stream.advanceSkippingSubblocks();
5741 if (!MaybeEntry) {
5742 // FIXME this drops errors on the floor.
5743 consumeError(Err: MaybeEntry.takeError());
5744 return std::string();
5745 }
5746 llvm::BitstreamEntry Entry = MaybeEntry.get();
5747
5748 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5749 return std::string();
5750
5751 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5752 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5753 return std::string();
5754 }
5755
5756 Record.clear();
5757 StringRef Blob;
5758 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5759 if (!MaybeRecord) {
5760 // FIXME this drops the errors on the floor.
5761 consumeError(Err: MaybeRecord.takeError());
5762 return std::string();
5763 }
5764 if (ORIGINAL_FILE == MaybeRecord.get())
5765 return Blob.str();
5766 }
5767}
5768
5769namespace {
5770
5771 class SimplePCHValidator : public ASTReaderListener {
5772 const LangOptions &ExistingLangOpts;
5773 const CodeGenOptions &ExistingCGOpts;
5774 const TargetOptions &ExistingTargetOpts;
5775 const PreprocessorOptions &ExistingPPOpts;
5776 const HeaderSearchOptions &ExistingHSOpts;
5777 std::string ExistingSpecificModuleCachePath;
5778 FileManager &FileMgr;
5779 bool StrictOptionMatches;
5780
5781 public:
5782 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5783 const CodeGenOptions &ExistingCGOpts,
5784 const TargetOptions &ExistingTargetOpts,
5785 const PreprocessorOptions &ExistingPPOpts,
5786 const HeaderSearchOptions &ExistingHSOpts,
5787 StringRef ExistingSpecificModuleCachePath,
5788 FileManager &FileMgr, bool StrictOptionMatches)
5789 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5790 ExistingTargetOpts(ExistingTargetOpts),
5791 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5792 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5793 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5794
5795 bool ReadLanguageOptions(const LangOptions &LangOpts,
5796 StringRef ModuleFilename, bool Complain,
5797 bool AllowCompatibleDifferences) override {
5798 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5799 Diags: nullptr, AllowCompatibleDifferences);
5800 }
5801
5802 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5803 StringRef ModuleFilename, bool Complain,
5804 bool AllowCompatibleDifferences) override {
5805 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5806 Diags: nullptr, AllowCompatibleDifferences);
5807 }
5808
5809 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5810 StringRef ModuleFilename, bool Complain,
5811 bool AllowCompatibleDifferences) override {
5812 return checkTargetOptions(TargetOpts: ExistingTargetOpts, ExistingTargetOpts: TargetOpts, ModuleFilename,
5813 Diags: nullptr, AllowCompatibleDifferences);
5814 }
5815
5816 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5817 StringRef ASTFilename,
5818 StringRef SpecificModuleCachePath,
5819 bool Complain) override {
5820 return checkModuleCachePath(
5821 VFS&: FileMgr.getVirtualFileSystem(), SpecificModuleCachePath,
5822 ExistingSpecificModuleCachePath, ASTFilename, Diags: nullptr,
5823 LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5824 }
5825
5826 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5827 StringRef ModuleFilename, bool ReadMacros,
5828 bool Complain,
5829 std::string &SuggestedPredefines) override {
5830 return checkPreprocessorOptions(
5831 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5832 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5833 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5834 : OptionValidateContradictions);
5835 }
5836 };
5837
5838} // namespace
5839
5840bool ASTReader::readASTFileControlBlock(
5841 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5842 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5843 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5844 unsigned ClientLoadCapabilities) {
5845 // Open the AST file.
5846 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5847 llvm::MemoryBuffer *Buffer =
5848 ModCache.getInMemoryModuleCache().lookupPCM(Filename);
5849 if (!Buffer) {
5850 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5851 // read again later, but we do not have the context here to determine if it
5852 // is safe to change the result of InMemoryModuleCache::getPCMState().
5853
5854 // FIXME: This allows use of the VFS; we do not allow use of the
5855 // VFS when actually loading a module.
5856 auto Entry =
5857 Filename == "-" ? FileMgr.getSTDIN() : FileMgr.getFileRef(Filename);
5858 if (!Entry) {
5859 llvm::consumeError(Err: Entry.takeError());
5860 return true;
5861 }
5862 auto BufferOrErr = FileMgr.getBufferForFile(Entry: *Entry);
5863 if (!BufferOrErr)
5864 return true;
5865 OwnedBuffer = std::move(*BufferOrErr);
5866 Buffer = OwnedBuffer.get();
5867 }
5868
5869 // Initialize the stream
5870 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5871 BitstreamCursor Stream(Bytes);
5872
5873 // Sniff for the signature.
5874 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5875 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5876 return true;
5877 }
5878
5879 // Scan for the CONTROL_BLOCK_ID block.
5880 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5881 return true;
5882
5883 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5884 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5885 bool NeedsImports = Listener.needsImportVisitation();
5886 BitstreamCursor InputFilesCursor;
5887 uint64_t InputFilesOffsetBase = 0;
5888
5889 RecordData Record;
5890 std::string ModuleDir;
5891 bool DoneWithControlBlock = false;
5892 SmallString<0> PathBuf;
5893 PathBuf.reserve(N: 256);
5894 // Additional path buffer to use when multiple paths need to be resolved.
5895 // For example, when deserializing input files that contains a path that was
5896 // resolved from a vfs overlay and an external location.
5897 SmallString<0> AdditionalPathBuf;
5898 AdditionalPathBuf.reserve(N: 256);
5899 while (!DoneWithControlBlock) {
5900 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5901 if (!MaybeEntry) {
5902 // FIXME this drops the error on the floor.
5903 consumeError(Err: MaybeEntry.takeError());
5904 return true;
5905 }
5906 llvm::BitstreamEntry Entry = MaybeEntry.get();
5907
5908 switch (Entry.Kind) {
5909 case llvm::BitstreamEntry::SubBlock: {
5910 switch (Entry.ID) {
5911 case OPTIONS_BLOCK_ID: {
5912 std::string IgnoredSuggestedPredefines;
5913 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
5914 /*AllowCompatibleConfigurationMismatch*/ false,
5915 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
5916 return true;
5917 break;
5918 }
5919
5920 case INPUT_FILES_BLOCK_ID:
5921 InputFilesCursor = Stream;
5922 if (llvm::Error Err = Stream.SkipBlock()) {
5923 // FIXME this drops the error on the floor.
5924 consumeError(Err: std::move(Err));
5925 return true;
5926 }
5927 if (NeedsInputFiles &&
5928 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
5929 return true;
5930 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5931 break;
5932
5933 default:
5934 if (llvm::Error Err = Stream.SkipBlock()) {
5935 // FIXME this drops the error on the floor.
5936 consumeError(Err: std::move(Err));
5937 return true;
5938 }
5939 break;
5940 }
5941
5942 continue;
5943 }
5944
5945 case llvm::BitstreamEntry::EndBlock:
5946 DoneWithControlBlock = true;
5947 break;
5948
5949 case llvm::BitstreamEntry::Error:
5950 return true;
5951
5952 case llvm::BitstreamEntry::Record:
5953 break;
5954 }
5955
5956 if (DoneWithControlBlock) break;
5957
5958 Record.clear();
5959 StringRef Blob;
5960 Expected<unsigned> MaybeRecCode =
5961 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5962 if (!MaybeRecCode) {
5963 // FIXME this drops the error.
5964 return Failure;
5965 }
5966 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5967 case METADATA:
5968 if (Record[0] != VERSION_MAJOR)
5969 return true;
5970 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
5971 return true;
5972 break;
5973 case MODULE_NAME:
5974 Listener.ReadModuleName(ModuleName: Blob);
5975 break;
5976 case MODULE_DIRECTORY:
5977 ModuleDir = std::string(Blob);
5978 break;
5979 case MODULE_MAP_FILE: {
5980 unsigned Idx = 0;
5981 std::string PathStr = ReadString(Record, Idx);
5982 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
5983 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
5984 break;
5985 }
5986 case INPUT_FILE_OFFSETS: {
5987 if (!NeedsInputFiles)
5988 break;
5989
5990 unsigned NumInputFiles = Record[0];
5991 unsigned NumUserFiles = Record[1];
5992 const llvm::support::unaligned_uint64_t *InputFileOffs =
5993 (const llvm::support::unaligned_uint64_t *)Blob.data();
5994 for (unsigned I = 0; I != NumInputFiles; ++I) {
5995 // Go find this input file.
5996 bool isSystemFile = I >= NumUserFiles;
5997
5998 if (isSystemFile && !NeedsSystemInputFiles)
5999 break; // the rest are system input files
6000
6001 BitstreamCursor &Cursor = InputFilesCursor;
6002 SavedStreamPosition SavedPosition(Cursor);
6003 if (llvm::Error Err =
6004 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6005 // FIXME this drops errors on the floor.
6006 consumeError(Err: std::move(Err));
6007 }
6008
6009 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6010 if (!MaybeCode) {
6011 // FIXME this drops errors on the floor.
6012 consumeError(Err: MaybeCode.takeError());
6013 }
6014 unsigned Code = MaybeCode.get();
6015
6016 RecordData Record;
6017 StringRef Blob;
6018 bool shouldContinue = false;
6019 Expected<unsigned> MaybeRecordType =
6020 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6021 if (!MaybeRecordType) {
6022 // FIXME this drops errors on the floor.
6023 consumeError(Err: MaybeRecordType.takeError());
6024 }
6025 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6026 case INPUT_FILE_HASH:
6027 break;
6028 case INPUT_FILE:
6029 time_t StoredTime = static_cast<time_t>(Record[2]);
6030 bool Overridden = static_cast<bool>(Record[3]);
6031 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6032 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6033 auto FilenameAsRequestedBuf = ResolveImportedPath(
6034 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6035 StringRef Filename;
6036 if (UnresolvedFilename.empty())
6037 Filename = *FilenameAsRequestedBuf;
6038 else {
6039 auto FilenameBuf = ResolveImportedPath(
6040 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6041 Filename = *FilenameBuf;
6042 }
6043 shouldContinue = Listener.visitInputFileAsRequested(
6044 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6045 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6046 break;
6047 }
6048 if (!shouldContinue)
6049 break;
6050 }
6051 break;
6052 }
6053
6054 case IMPORT: {
6055 if (!NeedsImports)
6056 break;
6057
6058 unsigned Idx = 0;
6059 // Read information about the AST file.
6060
6061 // Skip Kind
6062 Idx++;
6063
6064 // Skip ImportLoc
6065 Idx++;
6066
6067 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6068
6069 bool IsStandardCXXModule = Record[Idx++];
6070
6071 // In C++20 Modules, we don't record the path to imported
6072 // modules in the BMI files.
6073 if (IsStandardCXXModule) {
6074 Listener.visitImport(ModuleName, /*Filename=*/"");
6075 continue;
6076 }
6077
6078 // Skip Size and ModTime.
6079 Idx += 1 + 1;
6080 // Skip signature.
6081 Blob = Blob.substr(Start: ASTFileSignature::size);
6082
6083 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6084 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6085 Listener.visitImport(ModuleName, Filename: *Filename);
6086 break;
6087 }
6088
6089 default:
6090 // No other validation to perform.
6091 break;
6092 }
6093 }
6094
6095 // Look for module file extension blocks, if requested.
6096 if (FindModuleFileExtensions) {
6097 BitstreamCursor SavedStream = Stream;
6098 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6099 bool DoneWithExtensionBlock = false;
6100 while (!DoneWithExtensionBlock) {
6101 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6102 if (!MaybeEntry) {
6103 // FIXME this drops the error.
6104 return true;
6105 }
6106 llvm::BitstreamEntry Entry = MaybeEntry.get();
6107
6108 switch (Entry.Kind) {
6109 case llvm::BitstreamEntry::SubBlock:
6110 if (llvm::Error Err = Stream.SkipBlock()) {
6111 // FIXME this drops the error on the floor.
6112 consumeError(Err: std::move(Err));
6113 return true;
6114 }
6115 continue;
6116
6117 case llvm::BitstreamEntry::EndBlock:
6118 DoneWithExtensionBlock = true;
6119 continue;
6120
6121 case llvm::BitstreamEntry::Error:
6122 return true;
6123
6124 case llvm::BitstreamEntry::Record:
6125 break;
6126 }
6127
6128 Record.clear();
6129 StringRef Blob;
6130 Expected<unsigned> MaybeRecCode =
6131 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6132 if (!MaybeRecCode) {
6133 // FIXME this drops the error.
6134 return true;
6135 }
6136 switch (MaybeRecCode.get()) {
6137 case EXTENSION_METADATA: {
6138 ModuleFileExtensionMetadata Metadata;
6139 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6140 return true;
6141
6142 Listener.readModuleFileExtension(Metadata);
6143 break;
6144 }
6145 }
6146 }
6147 }
6148 Stream = std::move(SavedStream);
6149 }
6150
6151 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6152 if (readUnhashedControlBlockImpl(
6153 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6154 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6155 ValidateDiagnosticOptions) != Success)
6156 return true;
6157
6158 return false;
6159}
6160
6161bool ASTReader::isAcceptableASTFile(
6162 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6163 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6164 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6165 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6166 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6167 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6168 SpecificModuleCachePath, FileMgr,
6169 RequireStrictOptionMatches);
6170 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6171 /*FindModuleFileExtensions=*/false, Listener&: validator,
6172 /*ValidateDiagnosticOptions=*/true);
6173}
6174
6175llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
6176 unsigned ClientLoadCapabilities) {
6177 // Enter the submodule block.
6178 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
6179 return Err;
6180
6181 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6182 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6183 // If we don't know the top-level module, there's no point in doing qualified
6184 // lookup of its submodules; it won't find anything anywhere within this tree.
6185 // Let's skip that and avoid some string lookups.
6186 auto CreateModule = !KnowsTopLevelModule
6187 ? &ModuleMap::createModule
6188 : &ModuleMap::findOrCreateModuleFirst;
6189
6190 bool First = true;
6191 Module *CurrentModule = nullptr;
6192 RecordData Record;
6193 while (true) {
6194 Expected<llvm::BitstreamEntry> MaybeEntry =
6195 F.Stream.advanceSkippingSubblocks();
6196 if (!MaybeEntry)
6197 return MaybeEntry.takeError();
6198 llvm::BitstreamEntry Entry = MaybeEntry.get();
6199
6200 switch (Entry.Kind) {
6201 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6202 case llvm::BitstreamEntry::Error:
6203 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6204 Fmt: "malformed block record in AST file");
6205 case llvm::BitstreamEntry::EndBlock:
6206 return llvm::Error::success();
6207 case llvm::BitstreamEntry::Record:
6208 // The interesting case.
6209 break;
6210 }
6211
6212 // Read a record.
6213 StringRef Blob;
6214 Record.clear();
6215 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6216 if (!MaybeKind)
6217 return MaybeKind.takeError();
6218 unsigned Kind = MaybeKind.get();
6219
6220 if ((Kind == SUBMODULE_METADATA) != First)
6221 return llvm::createStringError(
6222 EC: std::errc::illegal_byte_sequence,
6223 Fmt: "submodule metadata record should be at beginning of block");
6224 First = false;
6225
6226 // Submodule information is only valid if we have a current module.
6227 // FIXME: Should we error on these cases?
6228 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
6229 Kind != SUBMODULE_DEFINITION)
6230 continue;
6231
6232 switch (Kind) {
6233 default: // Default behavior: ignore.
6234 break;
6235
6236 case SUBMODULE_DEFINITION: {
6237 if (Record.size() < 13)
6238 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6239 Fmt: "malformed module definition");
6240
6241 StringRef Name = Blob;
6242 unsigned Idx = 0;
6243 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6244 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6245 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6246 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6247 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6248 bool IsFramework = Record[Idx++];
6249 bool IsExplicit = Record[Idx++];
6250 bool IsSystem = Record[Idx++];
6251 bool IsExternC = Record[Idx++];
6252 bool InferSubmodules = Record[Idx++];
6253 bool InferExplicitSubmodules = Record[Idx++];
6254 bool InferExportWildcard = Record[Idx++];
6255 bool ConfigMacrosExhaustive = Record[Idx++];
6256 bool ModuleMapIsPrivate = Record[Idx++];
6257 bool NamedModuleHasInit = Record[Idx++];
6258
6259 Module *ParentModule = nullptr;
6260 if (Parent)
6261 ParentModule = getSubmodule(GlobalID: Parent);
6262
6263 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6264 args&: IsFramework, args&: IsExplicit);
6265
6266 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6267 if (GlobalIndex >= SubmodulesLoaded.size() ||
6268 SubmodulesLoaded[GlobalIndex])
6269 return llvm::createStringError(EC: std::errc::invalid_argument,
6270 Fmt: "too many submodules");
6271
6272 if (!ParentModule) {
6273 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
6274 // Don't emit module relocation error if we have -fno-validate-pch
6275 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6276 DisableValidationForModuleKind::Module)) {
6277 assert(CurFile != F.File && "ModuleManager did not de-duplicate");
6278
6279 Diag(DiagID: diag::err_module_file_conflict)
6280 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
6281 << F.File.getName();
6282
6283 auto CurModMapFile =
6284 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6285 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6286 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6287 Diag(DiagID: diag::note_module_file_conflict)
6288 << CurModMapFile->getName() << ModMapFile->getName();
6289
6290 return llvm::make_error<AlreadyReportedDiagnosticError>();
6291 }
6292 }
6293
6294 F.DidReadTopLevelSubmodule = true;
6295 CurrentModule->setASTFile(F.File);
6296 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6297 }
6298
6299 CurrentModule->Kind = Kind;
6300 // Note that we may be rewriting an existing location and it is important
6301 // to keep doing that. In particular, we would like to prefer a
6302 // `DefinitionLoc` loaded from the module file instead of the location
6303 // created in the current source manager, because it allows the new
6304 // location to be marked as "unaffecting" when writing and avoid creating
6305 // duplicate locations for the same module map file.
6306 CurrentModule->DefinitionLoc = DefinitionLoc;
6307 CurrentModule->Signature = F.Signature;
6308 CurrentModule->IsFromModuleFile = true;
6309 if (InferredAllowedBy.isValid())
6310 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6311 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6312 CurrentModule->IsExternC = IsExternC;
6313 CurrentModule->InferSubmodules = InferSubmodules;
6314 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6315 CurrentModule->InferExportWildcard = InferExportWildcard;
6316 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6317 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6318 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6319 if (DeserializationListener)
6320 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6321
6322 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6323
6324 // Clear out data that will be replaced by what is in the module file.
6325 CurrentModule->LinkLibraries.clear();
6326 CurrentModule->ConfigMacros.clear();
6327 CurrentModule->UnresolvedConflicts.clear();
6328 CurrentModule->Conflicts.clear();
6329
6330 // The module is available unless it's missing a requirement; relevant
6331 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6332 // Missing headers that were present when the module was built do not
6333 // make it unavailable -- if we got this far, this must be an explicitly
6334 // imported module file.
6335 CurrentModule->Requirements.clear();
6336 CurrentModule->MissingHeaders.clear();
6337 CurrentModule->IsUnimportable =
6338 ParentModule && ParentModule->IsUnimportable;
6339 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6340 break;
6341 }
6342
6343 case SUBMODULE_UMBRELLA_HEADER: {
6344 // FIXME: This doesn't work for framework modules as `Filename` is the
6345 // name as written in the module file and does not include
6346 // `Headers/`, so this path will never exist.
6347 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6348 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename: *Filename)) {
6349 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6350 // FIXME: NameAsWritten
6351 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6352 }
6353 // Note that it's too late at this point to return out of date if the
6354 // name from the PCM doesn't match up with the one in the module map,
6355 // but also quite unlikely since we will have already checked the
6356 // modification time and size of the module map file itself.
6357 }
6358 break;
6359 }
6360
6361 case SUBMODULE_HEADER:
6362 case SUBMODULE_EXCLUDED_HEADER:
6363 case SUBMODULE_PRIVATE_HEADER:
6364 // We lazily associate headers with their modules via the HeaderInfo table.
6365 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6366 // of complete filenames or remove it entirely.
6367 break;
6368
6369 case SUBMODULE_TEXTUAL_HEADER:
6370 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6371 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6372 // them here.
6373 break;
6374
6375 case SUBMODULE_TOPHEADER: {
6376 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6377 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6378 break;
6379 }
6380
6381 case SUBMODULE_UMBRELLA_DIR: {
6382 // See comments in SUBMODULE_UMBRELLA_HEADER
6383 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6384 if (auto Umbrella =
6385 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6386 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6387 // FIXME: NameAsWritten
6388 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6389 }
6390 }
6391 break;
6392 }
6393
6394 case SUBMODULE_METADATA: {
6395 F.BaseSubmoduleID = getTotalNumSubmodules();
6396 F.LocalNumSubmodules = Record[0];
6397 unsigned LocalBaseSubmoduleID = Record[1];
6398 if (F.LocalNumSubmodules > 0) {
6399 // Introduce the global -> local mapping for submodules within this
6400 // module.
6401 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
6402
6403 // Introduce the local -> global mapping for submodules within this
6404 // module.
6405 F.SubmoduleRemap.insertOrReplace(
6406 Val: std::make_pair(x&: LocalBaseSubmoduleID,
6407 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
6408
6409 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
6410 }
6411 break;
6412 }
6413
6414 case SUBMODULE_IMPORTS:
6415 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6416 UnresolvedModuleRef Unresolved;
6417 Unresolved.File = &F;
6418 Unresolved.Mod = CurrentModule;
6419 Unresolved.ID = Record[Idx];
6420 Unresolved.Kind = UnresolvedModuleRef::Import;
6421 Unresolved.IsWildcard = false;
6422 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6423 }
6424 break;
6425
6426 case SUBMODULE_AFFECTING_MODULES:
6427 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6428 UnresolvedModuleRef Unresolved;
6429 Unresolved.File = &F;
6430 Unresolved.Mod = CurrentModule;
6431 Unresolved.ID = Record[Idx];
6432 Unresolved.Kind = UnresolvedModuleRef::Affecting;
6433 Unresolved.IsWildcard = false;
6434 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6435 }
6436 break;
6437
6438 case SUBMODULE_EXPORTS:
6439 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6440 UnresolvedModuleRef Unresolved;
6441 Unresolved.File = &F;
6442 Unresolved.Mod = CurrentModule;
6443 Unresolved.ID = Record[Idx];
6444 Unresolved.Kind = UnresolvedModuleRef::Export;
6445 Unresolved.IsWildcard = Record[Idx + 1];
6446 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6447 }
6448
6449 // Once we've loaded the set of exports, there's no reason to keep
6450 // the parsed, unresolved exports around.
6451 CurrentModule->UnresolvedExports.clear();
6452 break;
6453
6454 case SUBMODULE_REQUIRES:
6455 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6456 Target: PP.getTargetInfo());
6457 break;
6458
6459 case SUBMODULE_LINK_LIBRARY:
6460 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6461 CurrentModule->LinkLibraries.push_back(
6462 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6463 break;
6464
6465 case SUBMODULE_CONFIG_MACRO:
6466 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6467 break;
6468
6469 case SUBMODULE_CONFLICT: {
6470 UnresolvedModuleRef Unresolved;
6471 Unresolved.File = &F;
6472 Unresolved.Mod = CurrentModule;
6473 Unresolved.ID = Record[0];
6474 Unresolved.Kind = UnresolvedModuleRef::Conflict;
6475 Unresolved.IsWildcard = false;
6476 Unresolved.String = Blob;
6477 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6478 break;
6479 }
6480
6481 case SUBMODULE_INITIALIZERS: {
6482 if (!ContextObj)
6483 break;
6484 // Standard C++ module has its own way to initialize variables.
6485 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6486 SmallVector<GlobalDeclID, 16> Inits;
6487 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6488 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6489 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6490 }
6491 break;
6492 }
6493
6494 case SUBMODULE_EXPORT_AS:
6495 CurrentModule->ExportAsModule = Blob.str();
6496 ModMap.addLinkAsDependency(Mod: CurrentModule);
6497 break;
6498 }
6499 }
6500}
6501
6502/// Parse the record that corresponds to a LangOptions data
6503/// structure.
6504///
6505/// This routine parses the language options from the AST file and then gives
6506/// them to the AST listener if one is set.
6507///
6508/// \returns true if the listener deems the file unacceptable, false otherwise.
6509bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6510 StringRef ModuleFilename, bool Complain,
6511 ASTReaderListener &Listener,
6512 bool AllowCompatibleDifferences) {
6513 LangOptions LangOpts;
6514 unsigned Idx = 0;
6515#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6516 LangOpts.Name = Record[Idx++];
6517#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6518 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6519#include "clang/Basic/LangOptions.def"
6520#define SANITIZER(NAME, ID) \
6521 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6522#include "clang/Basic/Sanitizers.def"
6523
6524 for (unsigned N = Record[Idx++]; N; --N)
6525 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6526
6527 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6528 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6529 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6530
6531 LangOpts.CurrentModule = ReadString(Record, Idx);
6532
6533 // Comment options.
6534 for (unsigned N = Record[Idx++]; N; --N) {
6535 LangOpts.CommentOpts.BlockCommandNames.push_back(
6536 x: ReadString(Record, Idx));
6537 }
6538 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6539
6540 // OpenMP offloading options.
6541 for (unsigned N = Record[Idx++]; N; --N) {
6542 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6543 }
6544
6545 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6546
6547 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6548 AllowCompatibleDifferences);
6549}
6550
6551bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6552 StringRef ModuleFilename, bool Complain,
6553 ASTReaderListener &Listener,
6554 bool AllowCompatibleDifferences) {
6555 unsigned Idx = 0;
6556 CodeGenOptions CGOpts;
6557 using CK = CodeGenOptions::CompatibilityKind;
6558#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6559 if constexpr (CK::Compatibility != CK::Benign) \
6560 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6561#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6562 if constexpr (CK::Compatibility != CK::Benign) \
6563 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6564#define DEBUGOPT(Name, Bits, Default, Compatibility)
6565#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6566#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6567#include "clang/Basic/CodeGenOptions.def"
6568
6569 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6570 AllowCompatibleDifferences);
6571}
6572
6573bool ASTReader::ParseTargetOptions(const RecordData &Record,
6574 StringRef ModuleFilename, bool Complain,
6575 ASTReaderListener &Listener,
6576 bool AllowCompatibleDifferences) {
6577 unsigned Idx = 0;
6578 TargetOptions TargetOpts;
6579 TargetOpts.Triple = ReadString(Record, Idx);
6580 TargetOpts.CPU = ReadString(Record, Idx);
6581 TargetOpts.TuneCPU = ReadString(Record, Idx);
6582 TargetOpts.ABI = ReadString(Record, Idx);
6583 for (unsigned N = Record[Idx++]; N; --N) {
6584 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6585 }
6586 for (unsigned N = Record[Idx++]; N; --N) {
6587 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6588 }
6589
6590 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6591 AllowCompatibleDifferences);
6592}
6593
6594bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6595 StringRef ModuleFilename, bool Complain,
6596 ASTReaderListener &Listener) {
6597 DiagnosticOptions DiagOpts;
6598 unsigned Idx = 0;
6599#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6600#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6601 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6602#include "clang/Basic/DiagnosticOptions.def"
6603
6604 for (unsigned N = Record[Idx++]; N; --N)
6605 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6606 for (unsigned N = Record[Idx++]; N; --N)
6607 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6608
6609 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6610}
6611
6612bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6613 ASTReaderListener &Listener) {
6614 FileSystemOptions FSOpts;
6615 unsigned Idx = 0;
6616 FSOpts.WorkingDir = ReadString(Record, Idx);
6617 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6618}
6619
6620bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6621 StringRef ModuleFilename,
6622 bool Complain,
6623 ASTReaderListener &Listener) {
6624 HeaderSearchOptions HSOpts;
6625 unsigned Idx = 0;
6626 HSOpts.Sysroot = ReadString(Record, Idx);
6627
6628 HSOpts.ResourceDir = ReadString(Record, Idx);
6629 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6630 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6631 HSOpts.DisableModuleHash = Record[Idx++];
6632 HSOpts.ImplicitModuleMaps = Record[Idx++];
6633 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6634 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6635 HSOpts.UseBuiltinIncludes = Record[Idx++];
6636 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6637 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6638 HSOpts.UseLibcxx = Record[Idx++];
6639 std::string SpecificModuleCachePath = ReadString(Record, Idx);
6640
6641 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename,
6642 SpecificModuleCachePath, Complain);
6643}
6644
6645bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6646 ASTReaderListener &Listener) {
6647 HeaderSearchOptions HSOpts;
6648 unsigned Idx = 0;
6649
6650 // Include entries.
6651 for (unsigned N = Record[Idx++]; N; --N) {
6652 std::string Path = ReadString(Record, Idx);
6653 frontend::IncludeDirGroup Group
6654 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6655 bool IsFramework = Record[Idx++];
6656 bool IgnoreSysRoot = Record[Idx++];
6657 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6658 args&: IgnoreSysRoot);
6659 }
6660
6661 // System header prefixes.
6662 for (unsigned N = Record[Idx++]; N; --N) {
6663 std::string Prefix = ReadString(Record, Idx);
6664 bool IsSystemHeader = Record[Idx++];
6665 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6666 }
6667
6668 // VFS overlay files.
6669 for (unsigned N = Record[Idx++]; N; --N) {
6670 std::string VFSOverlayFile = ReadString(Record, Idx);
6671 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6672 }
6673
6674 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6675}
6676
6677bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6678 StringRef ModuleFilename,
6679 bool Complain,
6680 ASTReaderListener &Listener,
6681 std::string &SuggestedPredefines) {
6682 PreprocessorOptions PPOpts;
6683 unsigned Idx = 0;
6684
6685 // Macro definitions/undefs
6686 bool ReadMacros = Record[Idx++];
6687 if (ReadMacros) {
6688 for (unsigned N = Record[Idx++]; N; --N) {
6689 std::string Macro = ReadString(Record, Idx);
6690 bool IsUndef = Record[Idx++];
6691 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6692 }
6693 }
6694
6695 // Includes
6696 for (unsigned N = Record[Idx++]; N; --N) {
6697 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6698 }
6699
6700 // Macro Includes
6701 for (unsigned N = Record[Idx++]; N; --N) {
6702 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6703 }
6704
6705 PPOpts.UsePredefines = Record[Idx++];
6706 PPOpts.DetailedRecord = Record[Idx++];
6707 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6708 PPOpts.ObjCXXARCStandardLibrary =
6709 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6710 SuggestedPredefines.clear();
6711 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6712 Complain, SuggestedPredefines);
6713}
6714
6715std::pair<ModuleFile *, unsigned>
6716ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6717 GlobalPreprocessedEntityMapType::iterator
6718 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6719 assert(I != GlobalPreprocessedEntityMap.end() &&
6720 "Corrupted global preprocessed entity map");
6721 ModuleFile *M = I->second;
6722 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6723 return std::make_pair(x&: M, y&: LocalIndex);
6724}
6725
6726llvm::iterator_range<PreprocessingRecord::iterator>
6727ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6728 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6729 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6730 count: Mod.NumPreprocessedEntities);
6731
6732 return llvm::make_range(x: PreprocessingRecord::iterator(),
6733 y: PreprocessingRecord::iterator());
6734}
6735
6736bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6737 unsigned int ClientLoadCapabilities) {
6738 return ClientLoadCapabilities & ARR_OutOfDate &&
6739 !getModuleManager()
6740 .getModuleCache()
6741 .getInMemoryModuleCache()
6742 .isPCMFinal(Filename: ModuleFileName);
6743}
6744
6745llvm::iterator_range<ASTReader::ModuleDeclIterator>
6746ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6747 return llvm::make_range(
6748 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6749 y: ModuleDeclIterator(this, &Mod,
6750 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6751}
6752
6753SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6754 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6755 assert(I != GlobalSkippedRangeMap.end() &&
6756 "Corrupted global skipped range map");
6757 ModuleFile *M = I->second;
6758 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6759 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6760 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6761 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6762 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6763 assert(Range.isValid());
6764 return Range;
6765}
6766
6767unsigned
6768ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6769 unsigned ModuleFileIndex = ID >> 32;
6770 assert(ModuleFileIndex && "not translating loaded MacroID?");
6771 assert(getModuleManager().size() > ModuleFileIndex - 1);
6772 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6773
6774 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6775 return MF.BasePreprocessedEntityID + ID;
6776}
6777
6778PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6779 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6780 ModuleFile &M = *PPInfo.first;
6781 unsigned LocalIndex = PPInfo.second;
6782 PreprocessedEntityID PPID =
6783 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6784 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6785
6786 if (!PP.getPreprocessingRecord()) {
6787 Error(Msg: "no preprocessing record");
6788 return nullptr;
6789 }
6790
6791 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6792 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6793 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6794 Error(Err: std::move(Err));
6795 return nullptr;
6796 }
6797
6798 Expected<llvm::BitstreamEntry> MaybeEntry =
6799 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6800 if (!MaybeEntry) {
6801 Error(Err: MaybeEntry.takeError());
6802 return nullptr;
6803 }
6804 llvm::BitstreamEntry Entry = MaybeEntry.get();
6805
6806 if (Entry.Kind != llvm::BitstreamEntry::Record)
6807 return nullptr;
6808
6809 // Read the record.
6810 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6811 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6812 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6813 StringRef Blob;
6814 RecordData Record;
6815 Expected<unsigned> MaybeRecType =
6816 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6817 if (!MaybeRecType) {
6818 Error(Err: MaybeRecType.takeError());
6819 return nullptr;
6820 }
6821 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6822 case PPD_MACRO_EXPANSION: {
6823 bool isBuiltin = Record[0];
6824 IdentifierInfo *Name = nullptr;
6825 MacroDefinitionRecord *Def = nullptr;
6826 if (isBuiltin)
6827 Name = getLocalIdentifier(M, LocalID: Record[1]);
6828 else {
6829 PreprocessedEntityID GlobalID =
6830 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6831 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
6832 Def =
6833 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
6834 }
6835
6836 MacroExpansion *ME;
6837 if (isBuiltin)
6838 ME = new (PPRec) MacroExpansion(Name, Range);
6839 else
6840 ME = new (PPRec) MacroExpansion(Def, Range);
6841
6842 return ME;
6843 }
6844
6845 case PPD_MACRO_DEFINITION: {
6846 // Decode the identifier info and then check again; if the macro is
6847 // still defined and associated with the identifier,
6848 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6849 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6850
6851 if (DeserializationListener)
6852 DeserializationListener->MacroDefinitionRead(PPID, MD);
6853
6854 return MD;
6855 }
6856
6857 case PPD_INCLUSION_DIRECTIVE: {
6858 const char *FullFileNameStart = Blob.data() + Record[0];
6859 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6860 OptionalFileEntryRef File;
6861 if (!FullFileName.empty())
6862 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6863
6864 // FIXME: Stable encoding
6865 InclusionDirective::InclusionKind Kind
6866 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6867 InclusionDirective *ID
6868 = new (PPRec) InclusionDirective(PPRec, Kind,
6869 StringRef(Blob.data(), Record[0]),
6870 Record[1], Record[3],
6871 File,
6872 Range);
6873 return ID;
6874 }
6875 }
6876
6877 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6878}
6879
6880/// Find the next module that contains entities and return the ID
6881/// of the first entry.
6882///
6883/// \param SLocMapI points at a chunk of a module that contains no
6884/// preprocessed entities or the entities it contains are not the ones we are
6885/// looking for.
6886unsigned ASTReader::findNextPreprocessedEntity(
6887 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6888 ++SLocMapI;
6889 for (GlobalSLocOffsetMapType::const_iterator
6890 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6891 ModuleFile &M = *SLocMapI->second;
6892 if (M.NumPreprocessedEntities)
6893 return M.BasePreprocessedEntityID;
6894 }
6895
6896 return getTotalNumPreprocessedEntities();
6897}
6898
6899namespace {
6900
6901struct PPEntityComp {
6902 const ASTReader &Reader;
6903 ModuleFile &M;
6904
6905 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6906
6907 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6908 SourceLocation LHS = getLoc(PPE: L);
6909 SourceLocation RHS = getLoc(PPE: R);
6910 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6911 }
6912
6913 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6914 SourceLocation LHS = getLoc(PPE: L);
6915 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6916 }
6917
6918 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6919 SourceLocation RHS = getLoc(PPE: R);
6920 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6921 }
6922
6923 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6924 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
6925 }
6926};
6927
6928} // namespace
6929
6930unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
6931 bool EndsAfter) const {
6932 if (SourceMgr.isLocalSourceLocation(Loc))
6933 return getTotalNumPreprocessedEntities();
6934
6935 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6936 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6937 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6938 "Corrupted global sloc offset map");
6939
6940 if (SLocMapI->second->NumPreprocessedEntities == 0)
6941 return findNextPreprocessedEntity(SLocMapI);
6942
6943 ModuleFile &M = *SLocMapI->second;
6944
6945 using pp_iterator = const PPEntityOffset *;
6946
6947 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6948 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6949
6950 size_t Count = M.NumPreprocessedEntities;
6951 size_t Half;
6952 pp_iterator First = pp_begin;
6953 pp_iterator PPI;
6954
6955 if (EndsAfter) {
6956 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
6957 comp: PPEntityComp(*this, M));
6958 } else {
6959 // Do a binary search manually instead of using std::lower_bound because
6960 // The end locations of entities may be unordered (when a macro expansion
6961 // is inside another macro argument), but for this case it is not important
6962 // whether we get the first macro expansion or its containing macro.
6963 while (Count > 0) {
6964 Half = Count / 2;
6965 PPI = First;
6966 std::advance(i&: PPI, n: Half);
6967 if (SourceMgr.isBeforeInTranslationUnit(
6968 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
6969 First = PPI;
6970 ++First;
6971 Count = Count - Half - 1;
6972 } else
6973 Count = Half;
6974 }
6975 }
6976
6977 if (PPI == pp_end)
6978 return findNextPreprocessedEntity(SLocMapI);
6979
6980 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6981}
6982
6983/// Returns a pair of [Begin, End) indices of preallocated
6984/// preprocessed entities that \arg Range encompasses.
6985std::pair<unsigned, unsigned>
6986 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6987 if (Range.isInvalid())
6988 return std::make_pair(x: 0,y: 0);
6989 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6990
6991 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
6992 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
6993 return std::make_pair(x&: BeginID, y&: EndID);
6994}
6995
6996/// Optionally returns true or false if the preallocated preprocessed
6997/// entity with index \arg Index came from file \arg FID.
6998std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6999 FileID FID) {
7000 if (FID.isInvalid())
7001 return false;
7002
7003 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7004 ModuleFile &M = *PPInfo.first;
7005 unsigned LocalIndex = PPInfo.second;
7006 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7007
7008 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7009 if (Loc.isInvalid())
7010 return false;
7011
7012 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7013 return true;
7014 else
7015 return false;
7016}
7017
7018namespace {
7019
7020 /// Visitor used to search for information about a header file.
7021 class HeaderFileInfoVisitor {
7022 FileEntryRef FE;
7023 std::optional<HeaderFileInfo> HFI;
7024
7025 public:
7026 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7027
7028 bool operator()(ModuleFile &M) {
7029 HeaderFileInfoLookupTable *Table
7030 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7031 if (!Table)
7032 return false;
7033
7034 // Look in the on-disk hash table for an entry for this file name.
7035 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7036 if (Pos == Table->end())
7037 return false;
7038
7039 HFI = *Pos;
7040 return true;
7041 }
7042
7043 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7044 };
7045
7046} // namespace
7047
7048HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7049 HeaderFileInfoVisitor Visitor(FE);
7050 ModuleMgr.visit(Visitor);
7051 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7052 return *HFI;
7053
7054 return HeaderFileInfo();
7055}
7056
7057void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7058 using DiagState = DiagnosticsEngine::DiagState;
7059 SmallVector<DiagState *, 32> DiagStates;
7060
7061 for (ModuleFile &F : ModuleMgr) {
7062 unsigned Idx = 0;
7063 auto &Record = F.PragmaDiagMappings;
7064 if (Record.empty())
7065 continue;
7066
7067 DiagStates.clear();
7068
7069 auto ReadDiagState = [&](const DiagState &BasedOn,
7070 bool IncludeNonPragmaStates) {
7071 unsigned BackrefID = Record[Idx++];
7072 if (BackrefID != 0)
7073 return DiagStates[BackrefID - 1];
7074
7075 // A new DiagState was created here.
7076 Diag.DiagStates.push_back(x: BasedOn);
7077 DiagState *NewState = &Diag.DiagStates.back();
7078 DiagStates.push_back(Elt: NewState);
7079 unsigned Size = Record[Idx++];
7080 assert(Idx + Size * 2 <= Record.size() &&
7081 "Invalid data, not enough diag/map pairs");
7082 while (Size--) {
7083 unsigned DiagID = Record[Idx++];
7084 DiagnosticMapping NewMapping =
7085 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7086 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7087 continue;
7088
7089 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7090
7091 // If this mapping was specified as a warning but the severity was
7092 // upgraded due to diagnostic settings, simulate the current diagnostic
7093 // settings (and use a warning).
7094 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7095 NewMapping.setSeverity(diag::Severity::Warning);
7096 NewMapping.setUpgradedFromWarning(false);
7097 }
7098
7099 Mapping = NewMapping;
7100 }
7101 return NewState;
7102 };
7103
7104 // Read the first state.
7105 DiagState *FirstState;
7106 if (F.Kind == MK_ImplicitModule) {
7107 // Implicitly-built modules are reused with different diagnostic
7108 // settings. Use the initial diagnostic state from Diag to simulate this
7109 // compilation's diagnostic settings.
7110 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7111 DiagStates.push_back(Elt: FirstState);
7112
7113 // Skip the initial diagnostic state from the serialized module.
7114 assert(Record[1] == 0 &&
7115 "Invalid data, unexpected backref in initial state");
7116 Idx = 3 + Record[2] * 2;
7117 assert(Idx < Record.size() &&
7118 "Invalid data, not enough state change pairs in initial state");
7119 } else if (F.isModule()) {
7120 // For an explicit module, preserve the flags from the module build
7121 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7122 // -Wblah flags.
7123 unsigned Flags = Record[Idx++];
7124 DiagState Initial(*Diag.getDiagnosticIDs());
7125 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7126 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7127 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7128 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7129 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7130 Initial.ExtBehavior = (diag::Severity)Flags;
7131 FirstState = ReadDiagState(Initial, true);
7132
7133 assert(F.OriginalSourceFileID.isValid());
7134
7135 // Set up the root buffer of the module to start with the initial
7136 // diagnostic state of the module itself, to cover files that contain no
7137 // explicit transitions (for which we did not serialize anything).
7138 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7139 .StateTransitions.push_back(Elt: {FirstState, 0});
7140 } else {
7141 // For prefix ASTs, start with whatever the user configured on the
7142 // command line.
7143 Idx++; // Skip flags.
7144 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7145 }
7146
7147 // Read the state transitions.
7148 unsigned NumLocations = Record[Idx++];
7149 while (NumLocations--) {
7150 assert(Idx < Record.size() &&
7151 "Invalid data, missing pragma diagnostic states");
7152 FileID FID = ReadFileID(F, Record, Idx);
7153 assert(FID.isValid() && "invalid FileID for transition");
7154 unsigned Transitions = Record[Idx++];
7155
7156 // Note that we don't need to set up Parent/ParentOffset here, because
7157 // we won't be changing the diagnostic state within imported FileIDs
7158 // (other than perhaps appending to the main source file, which has no
7159 // parent).
7160 auto &F = Diag.DiagStatesByLoc.Files[FID];
7161 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7162 for (unsigned I = 0; I != Transitions; ++I) {
7163 unsigned Offset = Record[Idx++];
7164 auto *State = ReadDiagState(*FirstState, false);
7165 F.StateTransitions.push_back(Elt: {State, Offset});
7166 }
7167 }
7168
7169 // Read the final state.
7170 assert(Idx < Record.size() &&
7171 "Invalid data, missing final pragma diagnostic state");
7172 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7173 auto *CurState = ReadDiagState(*FirstState, false);
7174
7175 if (!F.isModule()) {
7176 Diag.DiagStatesByLoc.CurDiagState = CurState;
7177 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7178
7179 // Preserve the property that the imaginary root file describes the
7180 // current state.
7181 FileID NullFile;
7182 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7183 if (T.empty())
7184 T.push_back(Elt: {CurState, 0});
7185 else
7186 T[0].State = CurState;
7187 }
7188
7189 // Don't try to read these mappings again.
7190 Record.clear();
7191 }
7192}
7193
7194/// Get the correct cursor and offset for loading a type.
7195ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7196 auto [M, Index] = translateTypeIDToIndex(ID);
7197 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7198 M->DeclsBlockStartOffset);
7199}
7200
7201static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7202 switch (code) {
7203#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7204 case TYPE_##CODE_ID: return Type::CLASS_ID;
7205#include "clang/Serialization/TypeBitCodes.def"
7206 default:
7207 return std::nullopt;
7208 }
7209}
7210
7211/// Read and return the type with the given index..
7212///
7213/// The index is the type ID, shifted and minus the number of predefs. This
7214/// routine actually reads the record corresponding to the type at the given
7215/// location. It is a helper routine for GetType, which deals with reading type
7216/// IDs.
7217QualType ASTReader::readTypeRecord(TypeID ID) {
7218 assert(ContextObj && "reading type with no AST context");
7219 ASTContext &Context = *ContextObj;
7220 RecordLocation Loc = TypeCursorForIndex(ID);
7221 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7222
7223 // Keep track of where we are in the stream, then jump back there
7224 // after reading this type.
7225 SavedStreamPosition SavedPosition(DeclsCursor);
7226
7227 ReadingKindTracker ReadingKind(Read_Type, *this);
7228
7229 // Note that we are loading a type record.
7230 Deserializing AType(this);
7231
7232 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7233 Error(Err: std::move(Err));
7234 return QualType();
7235 }
7236 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7237 if (!RawCode) {
7238 Error(Err: RawCode.takeError());
7239 return QualType();
7240 }
7241
7242 ASTRecordReader Record(*this, *Loc.F);
7243 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7244 if (!Code) {
7245 Error(Err: Code.takeError());
7246 return QualType();
7247 }
7248 if (Code.get() == TYPE_EXT_QUAL) {
7249 QualType baseType = Record.readQualType();
7250 Qualifiers quals = Record.readQualifiers();
7251 return Context.getQualifiedType(T: baseType, Qs: quals);
7252 }
7253
7254 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7255 if (!maybeClass) {
7256 Error(Msg: "Unexpected code for type");
7257 return QualType();
7258 }
7259
7260 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7261 return TypeReader.read(kind: *maybeClass);
7262}
7263
7264namespace clang {
7265
7266class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7267 ASTRecordReader &Reader;
7268
7269 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7270 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7271
7272 TypeSourceInfo *GetTypeSourceInfo() {
7273 return Reader.readTypeSourceInfo();
7274 }
7275
7276 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7277 return Reader.readNestedNameSpecifierLoc();
7278 }
7279
7280 Attr *ReadAttr() {
7281 return Reader.readAttr();
7282 }
7283
7284public:
7285 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7286
7287 // We want compile-time assurance that we've enumerated all of
7288 // these, so unfortunately we have to declare them first, then
7289 // define them out-of-line.
7290#define ABSTRACT_TYPELOC(CLASS, PARENT)
7291#define TYPELOC(CLASS, PARENT) \
7292 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7293#include "clang/AST/TypeLocNodes.def"
7294
7295 void VisitFunctionTypeLoc(FunctionTypeLoc);
7296 void VisitArrayTypeLoc(ArrayTypeLoc);
7297 void VisitTagTypeLoc(TagTypeLoc TL);
7298};
7299
7300} // namespace clang
7301
7302void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7303 // nothing to do
7304}
7305
7306void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7307 TL.setBuiltinLoc(readSourceLocation());
7308 if (TL.needsExtraLocalData()) {
7309 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7310 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7311 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7312 TL.setModeAttr(Reader.readInt());
7313 }
7314}
7315
7316void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7317 TL.setNameLoc(readSourceLocation());
7318}
7319
7320void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7321 TL.setStarLoc(readSourceLocation());
7322}
7323
7324void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7325 // nothing to do
7326}
7327
7328void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7329 // nothing to do
7330}
7331
7332void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7333 // nothing to do
7334}
7335
7336void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7337 TL.setExpansionLoc(readSourceLocation());
7338}
7339
7340void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7341 TL.setCaretLoc(readSourceLocation());
7342}
7343
7344void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7345 TL.setAmpLoc(readSourceLocation());
7346}
7347
7348void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7349 TL.setAmpAmpLoc(readSourceLocation());
7350}
7351
7352void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7353 TL.setStarLoc(readSourceLocation());
7354 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7355}
7356
7357void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7358 TL.setLBracketLoc(readSourceLocation());
7359 TL.setRBracketLoc(readSourceLocation());
7360 if (Reader.readBool())
7361 TL.setSizeExpr(Reader.readExpr());
7362 else
7363 TL.setSizeExpr(nullptr);
7364}
7365
7366void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7367 VisitArrayTypeLoc(TL);
7368}
7369
7370void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7371 VisitArrayTypeLoc(TL);
7372}
7373
7374void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7375 VisitArrayTypeLoc(TL);
7376}
7377
7378void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7379 DependentSizedArrayTypeLoc TL) {
7380 VisitArrayTypeLoc(TL);
7381}
7382
7383void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7384 DependentAddressSpaceTypeLoc TL) {
7385
7386 TL.setAttrNameLoc(readSourceLocation());
7387 TL.setAttrOperandParensRange(readSourceRange());
7388 TL.setAttrExprOperand(Reader.readExpr());
7389}
7390
7391void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7392 DependentSizedExtVectorTypeLoc TL) {
7393 TL.setNameLoc(readSourceLocation());
7394}
7395
7396void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7397 TL.setNameLoc(readSourceLocation());
7398}
7399
7400void TypeLocReader::VisitDependentVectorTypeLoc(
7401 DependentVectorTypeLoc TL) {
7402 TL.setNameLoc(readSourceLocation());
7403}
7404
7405void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7406 TL.setNameLoc(readSourceLocation());
7407}
7408
7409void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7410 TL.setAttrNameLoc(readSourceLocation());
7411 TL.setAttrOperandParensRange(readSourceRange());
7412 TL.setAttrRowOperand(Reader.readExpr());
7413 TL.setAttrColumnOperand(Reader.readExpr());
7414}
7415
7416void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7417 DependentSizedMatrixTypeLoc TL) {
7418 TL.setAttrNameLoc(readSourceLocation());
7419 TL.setAttrOperandParensRange(readSourceRange());
7420 TL.setAttrRowOperand(Reader.readExpr());
7421 TL.setAttrColumnOperand(Reader.readExpr());
7422}
7423
7424void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7425 TL.setLocalRangeBegin(readSourceLocation());
7426 TL.setLParenLoc(readSourceLocation());
7427 TL.setRParenLoc(readSourceLocation());
7428 TL.setExceptionSpecRange(readSourceRange());
7429 TL.setLocalRangeEnd(readSourceLocation());
7430 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7431 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7432 }
7433}
7434
7435void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7436 VisitFunctionTypeLoc(TL);
7437}
7438
7439void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7440 VisitFunctionTypeLoc(TL);
7441}
7442
7443void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7444 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7445 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7446 SourceLocation NameLoc = readSourceLocation();
7447 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7448}
7449
7450void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7451 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7452 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7453 SourceLocation NameLoc = readSourceLocation();
7454 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7455}
7456
7457void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7458 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7459 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7460 SourceLocation NameLoc = readSourceLocation();
7461 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7462}
7463
7464void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7465 TL.setTypeofLoc(readSourceLocation());
7466 TL.setLParenLoc(readSourceLocation());
7467 TL.setRParenLoc(readSourceLocation());
7468}
7469
7470void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7471 TL.setTypeofLoc(readSourceLocation());
7472 TL.setLParenLoc(readSourceLocation());
7473 TL.setRParenLoc(readSourceLocation());
7474 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7475}
7476
7477void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7478 TL.setDecltypeLoc(readSourceLocation());
7479 TL.setRParenLoc(readSourceLocation());
7480}
7481
7482void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7483 TL.setEllipsisLoc(readSourceLocation());
7484}
7485
7486void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7487 TL.setKWLoc(readSourceLocation());
7488 TL.setLParenLoc(readSourceLocation());
7489 TL.setRParenLoc(readSourceLocation());
7490 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7491}
7492
7493ConceptReference *ASTRecordReader::readConceptReference() {
7494 auto NNS = readNestedNameSpecifierLoc();
7495 auto TemplateKWLoc = readSourceLocation();
7496 auto ConceptNameLoc = readDeclarationNameInfo();
7497 auto FoundDecl = readDeclAs<NamedDecl>();
7498 auto NamedConcept = readDeclAs<ConceptDecl>();
7499 auto *CR = ConceptReference::Create(
7500 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7501 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7502 return CR;
7503}
7504
7505void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7506 TL.setNameLoc(readSourceLocation());
7507 if (Reader.readBool())
7508 TL.setConceptReference(Reader.readConceptReference());
7509 if (Reader.readBool())
7510 TL.setRParenLoc(readSourceLocation());
7511}
7512
7513void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7514 DeducedTemplateSpecializationTypeLoc TL) {
7515 TL.setElaboratedKeywordLoc(readSourceLocation());
7516 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7517 TL.setTemplateNameLoc(readSourceLocation());
7518}
7519
7520void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7521 TL.setElaboratedKeywordLoc(readSourceLocation());
7522 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7523 TL.setNameLoc(readSourceLocation());
7524}
7525
7526void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7527 VisitTagTypeLoc(TL);
7528}
7529
7530void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7531 VisitTagTypeLoc(TL);
7532}
7533
7534void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7535
7536void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7537 TL.setAttr(ReadAttr());
7538}
7539
7540void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7541 // Nothing to do
7542}
7543
7544void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7545 // Nothing to do.
7546}
7547
7548void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7549 HLSLAttributedResourceTypeLoc TL) {
7550 // Nothing to do.
7551}
7552
7553void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7554 // Nothing to do.
7555}
7556
7557void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7558 TL.setNameLoc(readSourceLocation());
7559}
7560
7561void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7562 SubstTemplateTypeParmTypeLoc TL) {
7563 TL.setNameLoc(readSourceLocation());
7564}
7565
7566void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7567 SubstTemplateTypeParmPackTypeLoc TL) {
7568 TL.setNameLoc(readSourceLocation());
7569}
7570
7571void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7572 SubstBuiltinTemplatePackTypeLoc TL) {
7573 TL.setNameLoc(readSourceLocation());
7574}
7575
7576void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7577 TemplateSpecializationTypeLoc TL) {
7578 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7579 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7580 SourceLocation TemplateKeywordLoc = readSourceLocation();
7581 SourceLocation NameLoc = readSourceLocation();
7582 SourceLocation LAngleLoc = readSourceLocation();
7583 SourceLocation RAngleLoc = readSourceLocation();
7584 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7585 LAngleLoc, RAngleLoc);
7586 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7587 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7588 Args[I] = Reader.readTemplateArgumentLocInfo(
7589 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7590}
7591
7592void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7593 TL.setLParenLoc(readSourceLocation());
7594 TL.setRParenLoc(readSourceLocation());
7595}
7596
7597void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7598 TL.setElaboratedKeywordLoc(readSourceLocation());
7599 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7600 TL.setNameLoc(readSourceLocation());
7601}
7602
7603void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7604 TL.setEllipsisLoc(readSourceLocation());
7605}
7606
7607void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7608 TL.setNameLoc(readSourceLocation());
7609 TL.setNameEndLoc(readSourceLocation());
7610}
7611
7612void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7613 if (TL.getNumProtocols()) {
7614 TL.setProtocolLAngleLoc(readSourceLocation());
7615 TL.setProtocolRAngleLoc(readSourceLocation());
7616 }
7617 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7618 TL.setProtocolLoc(i, Loc: readSourceLocation());
7619}
7620
7621void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7622 TL.setHasBaseTypeAsWritten(Reader.readBool());
7623 TL.setTypeArgsLAngleLoc(readSourceLocation());
7624 TL.setTypeArgsRAngleLoc(readSourceLocation());
7625 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7626 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7627 TL.setProtocolLAngleLoc(readSourceLocation());
7628 TL.setProtocolRAngleLoc(readSourceLocation());
7629 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7630 TL.setProtocolLoc(i, Loc: readSourceLocation());
7631}
7632
7633void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7634 TL.setStarLoc(readSourceLocation());
7635}
7636
7637void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7638 TL.setKWLoc(readSourceLocation());
7639 TL.setLParenLoc(readSourceLocation());
7640 TL.setRParenLoc(readSourceLocation());
7641}
7642
7643void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7644 TL.setKWLoc(readSourceLocation());
7645}
7646
7647void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7648 TL.setNameLoc(readSourceLocation());
7649}
7650
7651void TypeLocReader::VisitDependentBitIntTypeLoc(
7652 clang::DependentBitIntTypeLoc TL) {
7653 TL.setNameLoc(readSourceLocation());
7654}
7655
7656void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7657 // Nothing to do.
7658}
7659
7660void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7661 TypeLocReader TLR(*this);
7662 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7663 TLR.Visit(TyLoc: TL);
7664}
7665
7666TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7667 QualType InfoTy = readType();
7668 if (InfoTy.isNull())
7669 return nullptr;
7670
7671 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7672 readTypeLoc(TL: TInfo->getTypeLoc());
7673 return TInfo;
7674}
7675
7676static unsigned getIndexForTypeID(serialization::TypeID ID) {
7677 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7678}
7679
7680static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7681 return ID >> 32;
7682}
7683
7684static bool isPredefinedType(serialization::TypeID ID) {
7685 // We don't need to erase the higher bits since if these bits are not 0,
7686 // it must be larger than NUM_PREDEF_TYPE_IDS.
7687 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7688}
7689
7690std::pair<ModuleFile *, unsigned>
7691ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7692 assert(!isPredefinedType(ID) &&
7693 "Predefined type shouldn't be in TypesLoaded");
7694 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7695 assert(ModuleFileIndex && "Untranslated Local Decl?");
7696
7697 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7698 assert(OwningModuleFile &&
7699 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7700
7701 return {OwningModuleFile,
7702 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7703}
7704
7705QualType ASTReader::GetType(TypeID ID) {
7706 assert(ContextObj && "reading type with no AST context");
7707 ASTContext &Context = *ContextObj;
7708
7709 unsigned FastQuals = ID & Qualifiers::FastMask;
7710
7711 if (isPredefinedType(ID)) {
7712 QualType T;
7713 unsigned Index = getIndexForTypeID(ID);
7714 switch ((PredefinedTypeIDs)Index) {
7715 case PREDEF_TYPE_LAST_ID:
7716 // We should never use this one.
7717 llvm_unreachable("Invalid predefined type");
7718 break;
7719 case PREDEF_TYPE_NULL_ID:
7720 return QualType();
7721 case PREDEF_TYPE_VOID_ID:
7722 T = Context.VoidTy;
7723 break;
7724 case PREDEF_TYPE_BOOL_ID:
7725 T = Context.BoolTy;
7726 break;
7727 case PREDEF_TYPE_CHAR_U_ID:
7728 case PREDEF_TYPE_CHAR_S_ID:
7729 // FIXME: Check that the signedness of CharTy is correct!
7730 T = Context.CharTy;
7731 break;
7732 case PREDEF_TYPE_UCHAR_ID:
7733 T = Context.UnsignedCharTy;
7734 break;
7735 case PREDEF_TYPE_USHORT_ID:
7736 T = Context.UnsignedShortTy;
7737 break;
7738 case PREDEF_TYPE_UINT_ID:
7739 T = Context.UnsignedIntTy;
7740 break;
7741 case PREDEF_TYPE_ULONG_ID:
7742 T = Context.UnsignedLongTy;
7743 break;
7744 case PREDEF_TYPE_ULONGLONG_ID:
7745 T = Context.UnsignedLongLongTy;
7746 break;
7747 case PREDEF_TYPE_UINT128_ID:
7748 T = Context.UnsignedInt128Ty;
7749 break;
7750 case PREDEF_TYPE_SCHAR_ID:
7751 T = Context.SignedCharTy;
7752 break;
7753 case PREDEF_TYPE_WCHAR_ID:
7754 T = Context.WCharTy;
7755 break;
7756 case PREDEF_TYPE_SHORT_ID:
7757 T = Context.ShortTy;
7758 break;
7759 case PREDEF_TYPE_INT_ID:
7760 T = Context.IntTy;
7761 break;
7762 case PREDEF_TYPE_LONG_ID:
7763 T = Context.LongTy;
7764 break;
7765 case PREDEF_TYPE_LONGLONG_ID:
7766 T = Context.LongLongTy;
7767 break;
7768 case PREDEF_TYPE_INT128_ID:
7769 T = Context.Int128Ty;
7770 break;
7771 case PREDEF_TYPE_BFLOAT16_ID:
7772 T = Context.BFloat16Ty;
7773 break;
7774 case PREDEF_TYPE_HALF_ID:
7775 T = Context.HalfTy;
7776 break;
7777 case PREDEF_TYPE_FLOAT_ID:
7778 T = Context.FloatTy;
7779 break;
7780 case PREDEF_TYPE_DOUBLE_ID:
7781 T = Context.DoubleTy;
7782 break;
7783 case PREDEF_TYPE_LONGDOUBLE_ID:
7784 T = Context.LongDoubleTy;
7785 break;
7786 case PREDEF_TYPE_SHORT_ACCUM_ID:
7787 T = Context.ShortAccumTy;
7788 break;
7789 case PREDEF_TYPE_ACCUM_ID:
7790 T = Context.AccumTy;
7791 break;
7792 case PREDEF_TYPE_LONG_ACCUM_ID:
7793 T = Context.LongAccumTy;
7794 break;
7795 case PREDEF_TYPE_USHORT_ACCUM_ID:
7796 T = Context.UnsignedShortAccumTy;
7797 break;
7798 case PREDEF_TYPE_UACCUM_ID:
7799 T = Context.UnsignedAccumTy;
7800 break;
7801 case PREDEF_TYPE_ULONG_ACCUM_ID:
7802 T = Context.UnsignedLongAccumTy;
7803 break;
7804 case PREDEF_TYPE_SHORT_FRACT_ID:
7805 T = Context.ShortFractTy;
7806 break;
7807 case PREDEF_TYPE_FRACT_ID:
7808 T = Context.FractTy;
7809 break;
7810 case PREDEF_TYPE_LONG_FRACT_ID:
7811 T = Context.LongFractTy;
7812 break;
7813 case PREDEF_TYPE_USHORT_FRACT_ID:
7814 T = Context.UnsignedShortFractTy;
7815 break;
7816 case PREDEF_TYPE_UFRACT_ID:
7817 T = Context.UnsignedFractTy;
7818 break;
7819 case PREDEF_TYPE_ULONG_FRACT_ID:
7820 T = Context.UnsignedLongFractTy;
7821 break;
7822 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7823 T = Context.SatShortAccumTy;
7824 break;
7825 case PREDEF_TYPE_SAT_ACCUM_ID:
7826 T = Context.SatAccumTy;
7827 break;
7828 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7829 T = Context.SatLongAccumTy;
7830 break;
7831 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7832 T = Context.SatUnsignedShortAccumTy;
7833 break;
7834 case PREDEF_TYPE_SAT_UACCUM_ID:
7835 T = Context.SatUnsignedAccumTy;
7836 break;
7837 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7838 T = Context.SatUnsignedLongAccumTy;
7839 break;
7840 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7841 T = Context.SatShortFractTy;
7842 break;
7843 case PREDEF_TYPE_SAT_FRACT_ID:
7844 T = Context.SatFractTy;
7845 break;
7846 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7847 T = Context.SatLongFractTy;
7848 break;
7849 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7850 T = Context.SatUnsignedShortFractTy;
7851 break;
7852 case PREDEF_TYPE_SAT_UFRACT_ID:
7853 T = Context.SatUnsignedFractTy;
7854 break;
7855 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7856 T = Context.SatUnsignedLongFractTy;
7857 break;
7858 case PREDEF_TYPE_FLOAT16_ID:
7859 T = Context.Float16Ty;
7860 break;
7861 case PREDEF_TYPE_FLOAT128_ID:
7862 T = Context.Float128Ty;
7863 break;
7864 case PREDEF_TYPE_IBM128_ID:
7865 T = Context.Ibm128Ty;
7866 break;
7867 case PREDEF_TYPE_OVERLOAD_ID:
7868 T = Context.OverloadTy;
7869 break;
7870 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
7871 T = Context.UnresolvedTemplateTy;
7872 break;
7873 case PREDEF_TYPE_BOUND_MEMBER:
7874 T = Context.BoundMemberTy;
7875 break;
7876 case PREDEF_TYPE_PSEUDO_OBJECT:
7877 T = Context.PseudoObjectTy;
7878 break;
7879 case PREDEF_TYPE_DEPENDENT_ID:
7880 T = Context.DependentTy;
7881 break;
7882 case PREDEF_TYPE_UNKNOWN_ANY:
7883 T = Context.UnknownAnyTy;
7884 break;
7885 case PREDEF_TYPE_NULLPTR_ID:
7886 T = Context.NullPtrTy;
7887 break;
7888 case PREDEF_TYPE_CHAR8_ID:
7889 T = Context.Char8Ty;
7890 break;
7891 case PREDEF_TYPE_CHAR16_ID:
7892 T = Context.Char16Ty;
7893 break;
7894 case PREDEF_TYPE_CHAR32_ID:
7895 T = Context.Char32Ty;
7896 break;
7897 case PREDEF_TYPE_OBJC_ID:
7898 T = Context.ObjCBuiltinIdTy;
7899 break;
7900 case PREDEF_TYPE_OBJC_CLASS:
7901 T = Context.ObjCBuiltinClassTy;
7902 break;
7903 case PREDEF_TYPE_OBJC_SEL:
7904 T = Context.ObjCBuiltinSelTy;
7905 break;
7906#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7907 case PREDEF_TYPE_##Id##_ID: \
7908 T = Context.SingletonId; \
7909 break;
7910#include "clang/Basic/OpenCLImageTypes.def"
7911#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7912 case PREDEF_TYPE_##Id##_ID: \
7913 T = Context.Id##Ty; \
7914 break;
7915#include "clang/Basic/OpenCLExtensionTypes.def"
7916 case PREDEF_TYPE_SAMPLER_ID:
7917 T = Context.OCLSamplerTy;
7918 break;
7919 case PREDEF_TYPE_EVENT_ID:
7920 T = Context.OCLEventTy;
7921 break;
7922 case PREDEF_TYPE_CLK_EVENT_ID:
7923 T = Context.OCLClkEventTy;
7924 break;
7925 case PREDEF_TYPE_QUEUE_ID:
7926 T = Context.OCLQueueTy;
7927 break;
7928 case PREDEF_TYPE_RESERVE_ID_ID:
7929 T = Context.OCLReserveIDTy;
7930 break;
7931 case PREDEF_TYPE_AUTO_DEDUCT:
7932 T = Context.getAutoDeductType();
7933 break;
7934 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7935 T = Context.getAutoRRefDeductType();
7936 break;
7937 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7938 T = Context.ARCUnbridgedCastTy;
7939 break;
7940 case PREDEF_TYPE_BUILTIN_FN:
7941 T = Context.BuiltinFnTy;
7942 break;
7943 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7944 T = Context.IncompleteMatrixIdxTy;
7945 break;
7946 case PREDEF_TYPE_ARRAY_SECTION:
7947 T = Context.ArraySectionTy;
7948 break;
7949 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7950 T = Context.OMPArrayShapingTy;
7951 break;
7952 case PREDEF_TYPE_OMP_ITERATOR:
7953 T = Context.OMPIteratorTy;
7954 break;
7955#define SVE_TYPE(Name, Id, SingletonId) \
7956 case PREDEF_TYPE_##Id##_ID: \
7957 T = Context.SingletonId; \
7958 break;
7959#include "clang/Basic/AArch64ACLETypes.def"
7960#define PPC_VECTOR_TYPE(Name, Id, Size) \
7961 case PREDEF_TYPE_##Id##_ID: \
7962 T = Context.Id##Ty; \
7963 break;
7964#include "clang/Basic/PPCTypes.def"
7965#define RVV_TYPE(Name, Id, SingletonId) \
7966 case PREDEF_TYPE_##Id##_ID: \
7967 T = Context.SingletonId; \
7968 break;
7969#include "clang/Basic/RISCVVTypes.def"
7970#define WASM_TYPE(Name, Id, SingletonId) \
7971 case PREDEF_TYPE_##Id##_ID: \
7972 T = Context.SingletonId; \
7973 break;
7974#include "clang/Basic/WebAssemblyReferenceTypes.def"
7975#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
7976 case PREDEF_TYPE_##Id##_ID: \
7977 T = Context.SingletonId; \
7978 break;
7979#include "clang/Basic/AMDGPUTypes.def"
7980#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
7981 case PREDEF_TYPE_##Id##_ID: \
7982 T = Context.SingletonId; \
7983 break;
7984#include "clang/Basic/HLSLIntangibleTypes.def"
7985 }
7986
7987 assert(!T.isNull() && "Unknown predefined type");
7988 return T.withFastQualifiers(TQs: FastQuals);
7989 }
7990
7991 unsigned Index = translateTypeIDToIndex(ID).second;
7992
7993 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7994 if (TypesLoaded[Index].isNull()) {
7995 TypesLoaded[Index] = readTypeRecord(ID);
7996 if (TypesLoaded[Index].isNull())
7997 return QualType();
7998
7999 TypesLoaded[Index]->setFromAST();
8000 if (DeserializationListener)
8001 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8002 T: TypesLoaded[Index]);
8003 }
8004
8005 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8006}
8007
8008QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8009 return GetType(ID: getGlobalTypeID(F, LocalID));
8010}
8011
8012serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8013 LocalTypeID LocalID) const {
8014 if (isPredefinedType(ID: LocalID))
8015 return LocalID;
8016
8017 if (!F.ModuleOffsetMap.empty())
8018 ReadModuleOffsetMap(F);
8019
8020 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8021 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8022
8023 if (ModuleFileIndex == 0)
8024 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8025
8026 ModuleFile &MF =
8027 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8028 ModuleFileIndex = MF.Index + 1;
8029 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8030}
8031
8032TemplateArgumentLocInfo
8033ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8034 switch (Kind) {
8035 case TemplateArgument::Expression:
8036 return readExpr();
8037 case TemplateArgument::Type:
8038 return readTypeSourceInfo();
8039 case TemplateArgument::Template:
8040 case TemplateArgument::TemplateExpansion: {
8041 SourceLocation TemplateKWLoc = readSourceLocation();
8042 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8043 SourceLocation TemplateNameLoc = readSourceLocation();
8044 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8045 ? readSourceLocation()
8046 : SourceLocation();
8047 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8048 TemplateNameLoc, EllipsisLoc);
8049 }
8050 case TemplateArgument::Null:
8051 case TemplateArgument::Integral:
8052 case TemplateArgument::Declaration:
8053 case TemplateArgument::NullPtr:
8054 case TemplateArgument::StructuralValue:
8055 case TemplateArgument::Pack:
8056 // FIXME: Is this right?
8057 return TemplateArgumentLocInfo();
8058 }
8059 llvm_unreachable("unexpected template argument loc");
8060}
8061
8062TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8063 TemplateArgument Arg = readTemplateArgument();
8064
8065 if (Arg.getKind() == TemplateArgument::Expression) {
8066 if (readBool()) // bool InfoHasSameExpr.
8067 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8068 }
8069 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8070}
8071
8072void ASTRecordReader::readTemplateArgumentListInfo(
8073 TemplateArgumentListInfo &Result) {
8074 Result.setLAngleLoc(readSourceLocation());
8075 Result.setRAngleLoc(readSourceLocation());
8076 unsigned NumArgsAsWritten = readInt();
8077 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8078 Result.addArgument(Loc: readTemplateArgumentLoc());
8079}
8080
8081const ASTTemplateArgumentListInfo *
8082ASTRecordReader::readASTTemplateArgumentListInfo() {
8083 TemplateArgumentListInfo Result;
8084 readTemplateArgumentListInfo(Result);
8085 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8086}
8087
8088Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8089
8090void ASTReader::CompleteRedeclChain(const Decl *D) {
8091 if (NumCurrentElementsDeserializing) {
8092 // We arrange to not care about the complete redeclaration chain while we're
8093 // deserializing. Just remember that the AST has marked this one as complete
8094 // but that it's not actually complete yet, so we know we still need to
8095 // complete it later.
8096 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8097 return;
8098 }
8099
8100 if (!D->getDeclContext()) {
8101 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8102 return;
8103 }
8104
8105 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8106
8107 // If this is a named declaration, complete it by looking it up
8108 // within its context.
8109 //
8110 // FIXME: Merging a function definition should merge
8111 // all mergeable entities within it.
8112 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8113 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8114 if (!getContext().getLangOpts().CPlusPlus &&
8115 isa<TranslationUnitDecl>(Val: DC)) {
8116 // Outside of C++, we don't have a lookup table for the TU, so update
8117 // the identifier instead. (For C++ modules, we don't store decls
8118 // in the serialized identifier table, so we do the lookup in the TU.)
8119 auto *II = Name.getAsIdentifierInfo();
8120 assert(II && "non-identifier name in C?");
8121 if (II->isOutOfDate())
8122 updateOutOfDateIdentifier(II: *II);
8123 } else
8124 DC->lookup(Name);
8125 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8126 // Find all declarations of this kind from the relevant context.
8127 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8128 auto *DC = cast<DeclContext>(Val: DCDecl);
8129 SmallVector<Decl*, 8> Decls;
8130 FindExternalLexicalDecls(
8131 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8132 }
8133 }
8134 }
8135
8136 RedeclarableTemplateDecl *Template = nullptr;
8137 ArrayRef<TemplateArgument> Args;
8138 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8139 Template = CTSD->getSpecializedTemplate();
8140 Args = CTSD->getTemplateArgs().asArray();
8141 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8142 Template = VTSD->getSpecializedTemplate();
8143 Args = VTSD->getTemplateArgs().asArray();
8144 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8145 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8146 Template = Tmplt;
8147 Args = FD->getTemplateSpecializationArgs()->asArray();
8148 }
8149 }
8150
8151 if (Template)
8152 Template->loadLazySpecializationsImpl(Args);
8153}
8154
8155CXXCtorInitializer **
8156ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8157 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8158 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8159 SavedStreamPosition SavedPosition(Cursor);
8160 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8161 Error(Err: std::move(Err));
8162 return nullptr;
8163 }
8164 ReadingKindTracker ReadingKind(Read_Decl, *this);
8165 Deserializing D(this);
8166
8167 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8168 if (!MaybeCode) {
8169 Error(Err: MaybeCode.takeError());
8170 return nullptr;
8171 }
8172 unsigned Code = MaybeCode.get();
8173
8174 ASTRecordReader Record(*this, *Loc.F);
8175 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8176 if (!MaybeRecCode) {
8177 Error(Err: MaybeRecCode.takeError());
8178 return nullptr;
8179 }
8180 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8181 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8182 return nullptr;
8183 }
8184
8185 return Record.readCXXCtorInitializers();
8186}
8187
8188CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8189 assert(ContextObj && "reading base specifiers with no AST context");
8190 ASTContext &Context = *ContextObj;
8191
8192 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8193 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8194 SavedStreamPosition SavedPosition(Cursor);
8195 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8196 Error(Err: std::move(Err));
8197 return nullptr;
8198 }
8199 ReadingKindTracker ReadingKind(Read_Decl, *this);
8200 Deserializing D(this);
8201
8202 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8203 if (!MaybeCode) {
8204 Error(Err: MaybeCode.takeError());
8205 return nullptr;
8206 }
8207 unsigned Code = MaybeCode.get();
8208
8209 ASTRecordReader Record(*this, *Loc.F);
8210 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8211 if (!MaybeRecCode) {
8212 Error(Err: MaybeCode.takeError());
8213 return nullptr;
8214 }
8215 unsigned RecCode = MaybeRecCode.get();
8216
8217 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8218 Error(Msg: "malformed AST file: missing C++ base specifiers");
8219 return nullptr;
8220 }
8221
8222 unsigned NumBases = Record.readInt();
8223 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8224 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8225 for (unsigned I = 0; I != NumBases; ++I)
8226 Bases[I] = Record.readCXXBaseSpecifier();
8227 return Bases;
8228}
8229
8230GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8231 LocalDeclID LocalID) const {
8232 if (LocalID < NUM_PREDEF_DECL_IDS)
8233 return GlobalDeclID(LocalID.getRawValue());
8234
8235 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8236 DeclID ID = LocalID.getLocalDeclIndex();
8237
8238 if (!F.ModuleOffsetMap.empty())
8239 ReadModuleOffsetMap(F);
8240
8241 ModuleFile *OwningModuleFile =
8242 OwningModuleFileIndex == 0
8243 ? &F
8244 : F.TransitiveImports[OwningModuleFileIndex - 1];
8245
8246 if (OwningModuleFileIndex == 0)
8247 ID -= NUM_PREDEF_DECL_IDS;
8248
8249 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8250 return GlobalDeclID(NewModuleFileIndex, ID);
8251}
8252
8253bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8254 // Predefined decls aren't from any module.
8255 if (ID < NUM_PREDEF_DECL_IDS)
8256 return false;
8257
8258 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8259 return M.Index == ModuleFileIndex - 1;
8260}
8261
8262ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8263 // Predefined decls aren't from any module.
8264 if (ID < NUM_PREDEF_DECL_IDS)
8265 return nullptr;
8266
8267 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8268 assert(ModuleFileIndex && "Untranslated Local Decl?");
8269
8270 return &getModuleManager()[ModuleFileIndex - 1];
8271}
8272
8273ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8274 if (!D->isFromASTFile())
8275 return nullptr;
8276
8277 return getOwningModuleFile(ID: D->getGlobalID());
8278}
8279
8280SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8281 if (ID < NUM_PREDEF_DECL_IDS)
8282 return SourceLocation();
8283
8284 if (Decl *D = GetExistingDecl(ID))
8285 return D->getLocation();
8286
8287 SourceLocation Loc;
8288 DeclCursorForID(ID, Location&: Loc);
8289 return Loc;
8290}
8291
8292Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8293 assert(ContextObj && "reading predefined decl without AST context");
8294 ASTContext &Context = *ContextObj;
8295 Decl *NewLoaded = nullptr;
8296 switch (ID) {
8297 case PREDEF_DECL_NULL_ID:
8298 return nullptr;
8299
8300 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8301 return Context.getTranslationUnitDecl();
8302
8303 case PREDEF_DECL_OBJC_ID_ID:
8304 if (Context.ObjCIdDecl)
8305 return Context.ObjCIdDecl;
8306 NewLoaded = Context.getObjCIdDecl();
8307 break;
8308
8309 case PREDEF_DECL_OBJC_SEL_ID:
8310 if (Context.ObjCSelDecl)
8311 return Context.ObjCSelDecl;
8312 NewLoaded = Context.getObjCSelDecl();
8313 break;
8314
8315 case PREDEF_DECL_OBJC_CLASS_ID:
8316 if (Context.ObjCClassDecl)
8317 return Context.ObjCClassDecl;
8318 NewLoaded = Context.getObjCClassDecl();
8319 break;
8320
8321 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8322 if (Context.ObjCProtocolClassDecl)
8323 return Context.ObjCProtocolClassDecl;
8324 NewLoaded = Context.getObjCProtocolDecl();
8325 break;
8326
8327 case PREDEF_DECL_INT_128_ID:
8328 if (Context.Int128Decl)
8329 return Context.Int128Decl;
8330 NewLoaded = Context.getInt128Decl();
8331 break;
8332
8333 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8334 if (Context.UInt128Decl)
8335 return Context.UInt128Decl;
8336 NewLoaded = Context.getUInt128Decl();
8337 break;
8338
8339 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8340 if (Context.ObjCInstanceTypeDecl)
8341 return Context.ObjCInstanceTypeDecl;
8342 NewLoaded = Context.getObjCInstanceTypeDecl();
8343 break;
8344
8345 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8346 if (Context.BuiltinVaListDecl)
8347 return Context.BuiltinVaListDecl;
8348 NewLoaded = Context.getBuiltinVaListDecl();
8349 break;
8350
8351 case PREDEF_DECL_VA_LIST_TAG:
8352 if (Context.VaListTagDecl)
8353 return Context.VaListTagDecl;
8354 NewLoaded = Context.getVaListTagDecl();
8355 break;
8356
8357 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8358 if (Context.BuiltinMSVaListDecl)
8359 return Context.BuiltinMSVaListDecl;
8360 NewLoaded = Context.getBuiltinMSVaListDecl();
8361 break;
8362
8363 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8364 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8365 return Context.getMSGuidTagDecl();
8366
8367 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8368 if (Context.ExternCContext)
8369 return Context.ExternCContext;
8370 NewLoaded = Context.getExternCContextDecl();
8371 break;
8372
8373 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8374 if (Context.CFConstantStringTypeDecl)
8375 return Context.CFConstantStringTypeDecl;
8376 NewLoaded = Context.getCFConstantStringDecl();
8377 break;
8378
8379 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8380 if (Context.CFConstantStringTagDecl)
8381 return Context.CFConstantStringTagDecl;
8382 NewLoaded = Context.getCFConstantStringTagDecl();
8383 break;
8384
8385 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8386 return Context.getMSTypeInfoTagDecl();
8387
8388#define BuiltinTemplate(BTName) \
8389 case PREDEF_DECL##BTName##_ID: \
8390 if (Context.Decl##BTName) \
8391 return Context.Decl##BTName; \
8392 NewLoaded = Context.get##BTName##Decl(); \
8393 break;
8394#include "clang/Basic/BuiltinTemplates.inc"
8395
8396 case NUM_PREDEF_DECL_IDS:
8397 llvm_unreachable("Invalid decl ID");
8398 break;
8399 }
8400
8401 assert(NewLoaded && "Failed to load predefined decl?");
8402
8403 if (DeserializationListener)
8404 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8405
8406 return NewLoaded;
8407}
8408
8409unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8410 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8411 if (!OwningModuleFile) {
8412 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8413 return GlobalID.getRawValue();
8414 }
8415
8416 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8417}
8418
8419Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8420 assert(ContextObj && "reading decl with no AST context");
8421
8422 if (ID < NUM_PREDEF_DECL_IDS) {
8423 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8424 if (D) {
8425 // Track that we have merged the declaration with ID \p ID into the
8426 // pre-existing predefined declaration \p D.
8427 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8428 if (Merged.empty())
8429 Merged.push_back(Elt: ID);
8430 }
8431 return D;
8432 }
8433
8434 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8435
8436 if (Index >= DeclsLoaded.size()) {
8437 assert(0 && "declaration ID out-of-range for AST file");
8438 Error(Msg: "declaration ID out-of-range for AST file");
8439 return nullptr;
8440 }
8441
8442 return DeclsLoaded[Index];
8443}
8444
8445Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8446 if (ID < NUM_PREDEF_DECL_IDS)
8447 return GetExistingDecl(ID);
8448
8449 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8450
8451 if (Index >= DeclsLoaded.size()) {
8452 assert(0 && "declaration ID out-of-range for AST file");
8453 Error(Msg: "declaration ID out-of-range for AST file");
8454 return nullptr;
8455 }
8456
8457 if (!DeclsLoaded[Index]) {
8458 ReadDeclRecord(ID);
8459 if (DeserializationListener)
8460 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8461 }
8462
8463 return DeclsLoaded[Index];
8464}
8465
8466LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8467 GlobalDeclID GlobalID) {
8468 if (GlobalID < NUM_PREDEF_DECL_IDS)
8469 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8470
8471 if (!M.ModuleOffsetMap.empty())
8472 ReadModuleOffsetMap(F&: M);
8473
8474 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8475 DeclID ID = GlobalID.getLocalDeclIndex();
8476
8477 if (Owner == &M) {
8478 ID += NUM_PREDEF_DECL_IDS;
8479 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8480 }
8481
8482 uint64_t OrignalModuleFileIndex = 0;
8483 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8484 if (M.TransitiveImports[I] == Owner) {
8485 OrignalModuleFileIndex = I + 1;
8486 break;
8487 }
8488
8489 if (!OrignalModuleFileIndex)
8490 return LocalDeclID();
8491
8492 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8493}
8494
8495GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8496 unsigned &Idx) {
8497 if (Idx >= Record.size()) {
8498 Error(Msg: "Corrupted AST file");
8499 return GlobalDeclID(0);
8500 }
8501
8502 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8503}
8504
8505/// Resolve the offset of a statement into a statement.
8506///
8507/// This operation will read a new statement from the external
8508/// source each time it is called, and is meant to be used via a
8509/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8510Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8511 // Switch case IDs are per Decl.
8512 ClearSwitchCaseIDs();
8513
8514 // Offset here is a global offset across the entire chain.
8515 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8516 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8517 Error(Err: std::move(Err));
8518 return nullptr;
8519 }
8520 assert(NumCurrentElementsDeserializing == 0 &&
8521 "should not be called while already deserializing");
8522 Deserializing D(this);
8523 return ReadStmtFromStream(F&: *Loc.F);
8524}
8525
8526bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8527 const Decl *D) {
8528 assert(D);
8529
8530 auto It = SpecLookups.find(Val: D);
8531 if (It == SpecLookups.end())
8532 return false;
8533
8534 // Get Decl may violate the iterator from SpecializationsLookups so we store
8535 // the DeclIDs in ahead.
8536 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8537 It->second.Table.findAll();
8538
8539 // Since we've loaded all the specializations, we can erase it from
8540 // the lookup table.
8541 SpecLookups.erase(I: It);
8542
8543 bool NewSpecsFound = false;
8544 Deserializing LookupResults(this);
8545 for (auto &Info : Infos) {
8546 if (GetExistingDecl(ID: Info))
8547 continue;
8548 NewSpecsFound = true;
8549 GetDecl(ID: Info);
8550 }
8551
8552 return NewSpecsFound;
8553}
8554
8555bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8556 assert(D);
8557
8558 CompleteRedeclChain(D);
8559 bool NewSpecsFound =
8560 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8561 if (OnlyPartial)
8562 return NewSpecsFound;
8563
8564 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8565 return NewSpecsFound;
8566}
8567
8568bool ASTReader::LoadExternalSpecializationsImpl(
8569 SpecLookupTableTy &SpecLookups, const Decl *D,
8570 ArrayRef<TemplateArgument> TemplateArgs) {
8571 assert(D);
8572
8573 reader::LazySpecializationInfoLookupTable *LookupTable = nullptr;
8574 if (auto It = SpecLookups.find(Val: D); It != SpecLookups.end())
8575 LookupTable = &It->getSecond();
8576 if (!LookupTable)
8577 return false;
8578
8579 // NOTE: The getNameForDiagnostic usage in the lambda may mutate the
8580 // `SpecLookups` object.
8581 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8582 std::string Name;
8583 llvm::raw_string_ostream OS(Name);
8584 auto *ND = cast<NamedDecl>(Val: D);
8585 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8586 /*Qualified=*/true);
8587 return Name;
8588 });
8589
8590 Deserializing LookupResults(this);
8591 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8592
8593 // Get Decl may violate the iterator from SpecLookups
8594 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8595 LookupTable->Table.find(EKey: HashValue);
8596
8597 bool NewSpecsFound = false;
8598 for (auto &Info : Infos) {
8599 if (GetExistingDecl(ID: Info))
8600 continue;
8601 NewSpecsFound = true;
8602 GetDecl(ID: Info);
8603 }
8604
8605 return NewSpecsFound;
8606}
8607
8608bool ASTReader::LoadExternalSpecializations(
8609 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8610 assert(D);
8611
8612 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8613 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8614 NewDeclsFound |=
8615 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8616
8617 return NewDeclsFound;
8618}
8619
8620void ASTReader::FindExternalLexicalDecls(
8621 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8622 SmallVectorImpl<Decl *> &Decls) {
8623 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8624
8625 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8626 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8627 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8628 auto K = (Decl::Kind)+LexicalDecls[I];
8629 if (!IsKindWeWant(K))
8630 continue;
8631
8632 auto ID = (DeclID) + LexicalDecls[I + 1];
8633
8634 // Don't add predefined declarations to the lexical context more
8635 // than once.
8636 if (ID < NUM_PREDEF_DECL_IDS) {
8637 if (PredefsVisited[ID])
8638 continue;
8639
8640 PredefsVisited[ID] = true;
8641 }
8642
8643 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8644 assert(D->getKind() == K && "wrong kind for lexical decl");
8645 if (!DC->isDeclInLexicalTraversal(D))
8646 Decls.push_back(Elt: D);
8647 }
8648 }
8649 };
8650
8651 if (isa<TranslationUnitDecl>(Val: DC)) {
8652 for (const auto &Lexical : TULexicalDecls)
8653 Visit(Lexical.first, Lexical.second);
8654 } else {
8655 auto I = LexicalDecls.find(Val: DC);
8656 if (I != LexicalDecls.end())
8657 Visit(I->second.first, I->second.second);
8658 }
8659
8660 ++NumLexicalDeclContextsRead;
8661}
8662
8663namespace {
8664
8665class UnalignedDeclIDComp {
8666 ASTReader &Reader;
8667 ModuleFile &Mod;
8668
8669public:
8670 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8671 : Reader(Reader), Mod(M) {}
8672
8673 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8674 SourceLocation LHS = getLocation(ID: L);
8675 SourceLocation RHS = getLocation(ID: R);
8676 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8677 }
8678
8679 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8680 SourceLocation RHS = getLocation(ID: R);
8681 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8682 }
8683
8684 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8685 SourceLocation LHS = getLocation(ID: L);
8686 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8687 }
8688
8689 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8690 return Reader.getSourceManager().getFileLoc(
8691 Loc: Reader.getSourceLocationForDeclID(
8692 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8693 }
8694};
8695
8696} // namespace
8697
8698void ASTReader::FindFileRegionDecls(FileID File,
8699 unsigned Offset, unsigned Length,
8700 SmallVectorImpl<Decl *> &Decls) {
8701 SourceManager &SM = getSourceManager();
8702
8703 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8704 if (I == FileDeclIDs.end())
8705 return;
8706
8707 FileDeclsInfo &DInfo = I->second;
8708 if (DInfo.Decls.empty())
8709 return;
8710
8711 SourceLocation
8712 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8713 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8714
8715 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8716 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8717 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8718 if (BeginIt != DInfo.Decls.begin())
8719 --BeginIt;
8720
8721 // If we are pointing at a top-level decl inside an objc container, we need
8722 // to backtrack until we find it otherwise we will fail to report that the
8723 // region overlaps with an objc container.
8724 while (BeginIt != DInfo.Decls.begin() &&
8725 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8726 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8727 ->isTopLevelDeclInObjCContainer())
8728 --BeginIt;
8729
8730 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8731 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8732 if (EndIt != DInfo.Decls.end())
8733 ++EndIt;
8734
8735 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8736 ++DIt)
8737 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8738 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8739}
8740
8741bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8742 DeclarationName Name,
8743 const DeclContext *OriginalDC) {
8744 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8745 "DeclContext has no visible decls in storage");
8746 if (!Name)
8747 return false;
8748
8749 // Load the list of declarations.
8750 DeclsSet DS;
8751
8752 auto Find = [&, this](auto &&Table, auto &&Key) {
8753 for (GlobalDeclID ID : Table.find(Key)) {
8754 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8755 if (ND->getDeclName() != Name)
8756 continue;
8757 // Special case for namespaces: There can be a lot of redeclarations of
8758 // some namespaces, and we import a "key declaration" per imported module.
8759 // Since all declarations of a namespace are essentially interchangeable,
8760 // we can optimize namespace look-up by only storing the key declaration
8761 // of the current TU, rather than storing N key declarations where N is
8762 // the # of imported modules that declare that namespace.
8763 // TODO: Try to generalize this optimization to other redeclarable decls.
8764 if (isa<NamespaceDecl>(Val: ND))
8765 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8766 DS.insert(ND);
8767 }
8768 };
8769
8770 Deserializing LookupResults(this);
8771
8772 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8773 // available.
8774 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8775 ++NumVisibleDeclContextsRead;
8776 Find(It->second.Table, Name);
8777 }
8778
8779 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8780 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8781 ++NumModuleLocalVisibleDeclContexts;
8782 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8783 }
8784 };
8785 if (auto *NamedModule =
8786 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8787 : nullptr)
8788 FindModuleLocalLookup(NamedModule);
8789 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8790 // We're going to find a decl but the decl context of the lookup is
8791 // unspecified. In this case, the OriginalDC may be the decl context in other
8792 // module.
8793 if (ContextObj && ContextObj->getCurrentNamedModule())
8794 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8795
8796 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8797 ++NumTULocalVisibleDeclContexts;
8798 Find(It->second.Table, Name);
8799 }
8800
8801 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8802 return !DS.empty();
8803}
8804
8805void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8806 if (!DC->hasExternalVisibleStorage())
8807 return;
8808
8809 DeclsMap Decls;
8810
8811 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8812 auto It = LookupTables.find(DC);
8813 if (It == LookupTables.end())
8814 return;
8815
8816 NumRead++;
8817
8818 for (GlobalDeclID ID : It->second.Table.findAll()) {
8819 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8820 // Special case for namespaces: There can be a lot of redeclarations of
8821 // some namespaces, and we import a "key declaration" per imported module.
8822 // Since all declarations of a namespace are essentially interchangeable,
8823 // we can optimize namespace look-up by only storing the key declaration
8824 // of the current TU, rather than storing N key declarations where N is
8825 // the # of imported modules that declare that namespace.
8826 // TODO: Try to generalize this optimization to other redeclarable decls.
8827 if (isa<NamespaceDecl>(Val: ND))
8828 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8829 Decls[ND->getDeclName()].insert(ND);
8830 }
8831
8832 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8833 };
8834
8835 findAll(Lookups, NumVisibleDeclContextsRead);
8836 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8837 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8838
8839 for (auto &[Name, DS] : Decls)
8840 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8841
8842 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8843}
8844
8845const serialization::reader::DeclContextLookupTable *
8846ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8847 auto I = Lookups.find(Val: Primary);
8848 return I == Lookups.end() ? nullptr : &I->second;
8849}
8850
8851const serialization::reader::ModuleLocalLookupTable *
8852ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
8853 auto I = ModuleLocalLookups.find(Val: Primary);
8854 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
8855}
8856
8857const serialization::reader::DeclContextLookupTable *
8858ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
8859 auto I = TULocalLookups.find(Val: Primary);
8860 return I == TULocalLookups.end() ? nullptr : &I->second;
8861}
8862
8863serialization::reader::LazySpecializationInfoLookupTable *
8864ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
8865 assert(D->isCanonicalDecl());
8866 auto &LookupTable =
8867 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
8868 auto I = LookupTable.find(Val: D);
8869 return I == LookupTable.end() ? nullptr : &I->second;
8870}
8871
8872bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
8873 assert(D->isCanonicalDecl());
8874 return PartialSpecializationsLookups.contains(Val: D) ||
8875 SpecializationsLookups.contains(Val: D);
8876}
8877
8878/// Under non-PCH compilation the consumer receives the objc methods
8879/// before receiving the implementation, and codegen depends on this.
8880/// We simulate this by deserializing and passing to consumer the methods of the
8881/// implementation before passing the deserialized implementation decl.
8882static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8883 ASTConsumer *Consumer) {
8884 assert(ImplD && Consumer);
8885
8886 for (auto *I : ImplD->methods())
8887 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
8888
8889 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8890}
8891
8892void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8893 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8894 PassObjCImplDeclToConsumer(ImplD, Consumer);
8895 else
8896 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
8897}
8898
8899void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
8900 Consumer->HandleVTable(RD);
8901}
8902
8903void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8904 this->Consumer = Consumer;
8905
8906 if (Consumer)
8907 PassInterestingDeclsToConsumer();
8908
8909 if (DeserializationListener)
8910 DeserializationListener->ReaderInitialized(Reader: this);
8911}
8912
8913void ASTReader::PrintStats() {
8914 std::fprintf(stderr, format: "*** AST File Statistics:\n");
8915
8916 unsigned NumTypesLoaded =
8917 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
8918 unsigned NumDeclsLoaded =
8919 DeclsLoaded.size() -
8920 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
8921 unsigned NumIdentifiersLoaded =
8922 IdentifiersLoaded.size() -
8923 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
8924 unsigned NumMacrosLoaded =
8925 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
8926 unsigned NumSelectorsLoaded =
8927 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
8928
8929 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8930 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
8931 NumSLocEntriesRead, TotalNumSLocEntries,
8932 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8933 if (!TypesLoaded.empty())
8934 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
8935 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8936 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8937 if (!DeclsLoaded.empty())
8938 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
8939 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8940 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8941 if (!IdentifiersLoaded.empty())
8942 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
8943 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8944 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8945 if (!MacrosLoaded.empty())
8946 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8947 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8948 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8949 if (!SelectorsLoaded.empty())
8950 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
8951 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8952 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8953 if (TotalNumStatements)
8954 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
8955 NumStatementsRead, TotalNumStatements,
8956 ((float)NumStatementsRead/TotalNumStatements * 100));
8957 if (TotalNumMacros)
8958 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8959 NumMacrosRead, TotalNumMacros,
8960 ((float)NumMacrosRead/TotalNumMacros * 100));
8961 if (TotalLexicalDeclContexts)
8962 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
8963 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8964 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8965 * 100));
8966 if (TotalVisibleDeclContexts)
8967 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
8968 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8969 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8970 * 100));
8971 if (TotalModuleLocalVisibleDeclContexts)
8972 std::fprintf(
8973 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
8974 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
8975 ((float)NumModuleLocalVisibleDeclContexts /
8976 TotalModuleLocalVisibleDeclContexts * 100));
8977 if (TotalTULocalVisibleDeclContexts)
8978 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
8979 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
8980 ((float)NumTULocalVisibleDeclContexts /
8981 TotalTULocalVisibleDeclContexts * 100));
8982 if (TotalNumMethodPoolEntries)
8983 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
8984 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8985 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8986 * 100));
8987 if (NumMethodPoolLookups)
8988 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
8989 NumMethodPoolHits, NumMethodPoolLookups,
8990 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8991 if (NumMethodPoolTableLookups)
8992 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
8993 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8994 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8995 * 100.0));
8996 if (NumIdentifierLookupHits)
8997 std::fprintf(stderr,
8998 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
8999 NumIdentifierLookupHits, NumIdentifierLookups,
9000 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9001
9002 if (GlobalIndex) {
9003 std::fprintf(stderr, format: "\n");
9004 GlobalIndex->printStats();
9005 }
9006
9007 std::fprintf(stderr, format: "\n");
9008 dump();
9009 std::fprintf(stderr, format: "\n");
9010}
9011
9012template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9013LLVM_DUMP_METHOD static void
9014dumpModuleIDMap(StringRef Name,
9015 const ContinuousRangeMap<Key, ModuleFile *,
9016 InitialCapacity> &Map) {
9017 if (Map.begin() == Map.end())
9018 return;
9019
9020 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9021
9022 llvm::errs() << Name << ":\n";
9023 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9024 I != IEnd; ++I)
9025 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9026 << "\n";
9027}
9028
9029LLVM_DUMP_METHOD void ASTReader::dump() {
9030 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9031 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9032 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9033 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9034 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9035 dumpModuleIDMap(Name: "Global preprocessed entity map",
9036 Map: GlobalPreprocessedEntityMap);
9037
9038 llvm::errs() << "\n*** PCH/Modules Loaded:";
9039 for (ModuleFile &M : ModuleMgr)
9040 M.dump();
9041}
9042
9043/// Return the amount of memory used by memory buffers, breaking down
9044/// by heap-backed versus mmap'ed memory.
9045void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9046 for (ModuleFile &I : ModuleMgr) {
9047 if (llvm::MemoryBuffer *buf = I.Buffer) {
9048 size_t bytes = buf->getBufferSize();
9049 switch (buf->getBufferKind()) {
9050 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9051 sizes.malloc_bytes += bytes;
9052 break;
9053 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9054 sizes.mmap_bytes += bytes;
9055 break;
9056 }
9057 }
9058 }
9059}
9060
9061void ASTReader::InitializeSema(Sema &S) {
9062 SemaObj = &S;
9063 S.addExternalSource(E: this);
9064
9065 // Makes sure any declarations that were deserialized "too early"
9066 // still get added to the identifier's declaration chains.
9067 for (GlobalDeclID ID : PreloadedDeclIDs) {
9068 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9069 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9070 }
9071 PreloadedDeclIDs.clear();
9072
9073 // FIXME: What happens if these are changed by a module import?
9074 if (!FPPragmaOptions.empty()) {
9075 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9076 FPOptionsOverride NewOverrides =
9077 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9078 SemaObj->CurFPFeatures =
9079 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9080 }
9081
9082 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9083 Decl *D = GetDecl(ID);
9084 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9085 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9086 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9087 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9088 else
9089 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9090 }
9091 DeclsWithEffectsToVerify.clear();
9092
9093 SemaObj->OpenCLFeatures = OpenCLExtensions;
9094
9095 UpdateSema();
9096}
9097
9098void ASTReader::UpdateSema() {
9099 assert(SemaObj && "no Sema to update");
9100
9101 // Load the offsets of the declarations that Sema references.
9102 // They will be lazily deserialized when needed.
9103 if (!SemaDeclRefs.empty()) {
9104 assert(SemaDeclRefs.size() % 3 == 0);
9105 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9106 if (!SemaObj->StdNamespace)
9107 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9108 if (!SemaObj->StdBadAlloc)
9109 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9110 if (!SemaObj->StdAlignValT)
9111 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9112 }
9113 SemaDeclRefs.clear();
9114 }
9115
9116 // Update the state of pragmas. Use the same API as if we had encountered the
9117 // pragma in the source.
9118 if(OptimizeOffPragmaLocation.isValid())
9119 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9120 if (PragmaMSStructState != -1)
9121 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9122 if (PointersToMembersPragmaLocation.isValid()) {
9123 SemaObj->ActOnPragmaMSPointersToMembers(
9124 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9125 PragmaMSPointersToMembersState,
9126 PragmaLoc: PointersToMembersPragmaLocation);
9127 }
9128 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9129 if (!RISCVVecIntrinsicPragma.empty()) {
9130 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9131 "Wrong number of RISCVVecIntrinsicPragma");
9132 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9133 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9134 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9135 }
9136
9137 if (PragmaAlignPackCurrentValue) {
9138 // The bottom of the stack might have a default value. It must be adjusted
9139 // to the current value to ensure that the packing state is preserved after
9140 // popping entries that were included/imported from a PCH/module.
9141 bool DropFirst = false;
9142 if (!PragmaAlignPackStack.empty() &&
9143 PragmaAlignPackStack.front().Location.isInvalid()) {
9144 assert(PragmaAlignPackStack.front().Value ==
9145 SemaObj->AlignPackStack.DefaultValue &&
9146 "Expected a default alignment value");
9147 SemaObj->AlignPackStack.Stack.emplace_back(
9148 Args&: PragmaAlignPackStack.front().SlotLabel,
9149 Args&: SemaObj->AlignPackStack.CurrentValue,
9150 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9151 Args&: PragmaAlignPackStack.front().PushLocation);
9152 DropFirst = true;
9153 }
9154 for (const auto &Entry :
9155 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9156 SemaObj->AlignPackStack.Stack.emplace_back(
9157 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9158 }
9159 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9160 assert(*PragmaAlignPackCurrentValue ==
9161 SemaObj->AlignPackStack.DefaultValue &&
9162 "Expected a default align and pack value");
9163 // Keep the current values.
9164 } else {
9165 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9166 SemaObj->AlignPackStack.CurrentPragmaLocation =
9167 PragmaAlignPackCurrentLocation;
9168 }
9169 }
9170 if (FpPragmaCurrentValue) {
9171 // The bottom of the stack might have a default value. It must be adjusted
9172 // to the current value to ensure that fp-pragma state is preserved after
9173 // popping entries that were included/imported from a PCH/module.
9174 bool DropFirst = false;
9175 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9176 assert(FpPragmaStack.front().Value ==
9177 SemaObj->FpPragmaStack.DefaultValue &&
9178 "Expected a default pragma float_control value");
9179 SemaObj->FpPragmaStack.Stack.emplace_back(
9180 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9181 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9182 Args&: FpPragmaStack.front().PushLocation);
9183 DropFirst = true;
9184 }
9185 for (const auto &Entry :
9186 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9187 SemaObj->FpPragmaStack.Stack.emplace_back(
9188 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9189 if (FpPragmaCurrentLocation.isInvalid()) {
9190 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9191 "Expected a default pragma float_control value");
9192 // Keep the current values.
9193 } else {
9194 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9195 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9196 }
9197 }
9198
9199 // For non-modular AST files, restore visiblity of modules.
9200 for (auto &Import : PendingImportedModulesSema) {
9201 if (Import.ImportLoc.isInvalid())
9202 continue;
9203 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9204 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9205 }
9206 }
9207 PendingImportedModulesSema.clear();
9208}
9209
9210IdentifierInfo *ASTReader::get(StringRef Name) {
9211 // Note that we are loading an identifier.
9212 Deserializing AnIdentifier(this);
9213
9214 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9215 NumIdentifierLookups,
9216 NumIdentifierLookupHits);
9217
9218 // We don't need to do identifier table lookups in C++ modules (we preload
9219 // all interesting declarations, and don't need to use the scope for name
9220 // lookups). Perform the lookup in PCH files, though, since we don't build
9221 // a complete initial identifier table if we're carrying on from a PCH.
9222 if (PP.getLangOpts().CPlusPlus) {
9223 for (auto *F : ModuleMgr.pch_modules())
9224 if (Visitor(*F))
9225 break;
9226 } else {
9227 // If there is a global index, look there first to determine which modules
9228 // provably do not have any results for this identifier.
9229 GlobalModuleIndex::HitSet Hits;
9230 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9231 if (!loadGlobalIndex()) {
9232 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9233 HitsPtr = &Hits;
9234 }
9235 }
9236
9237 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9238 }
9239
9240 IdentifierInfo *II = Visitor.getIdentifierInfo();
9241 markIdentifierUpToDate(II);
9242 return II;
9243}
9244
9245namespace clang {
9246
9247 /// An identifier-lookup iterator that enumerates all of the
9248 /// identifiers stored within a set of AST files.
9249 class ASTIdentifierIterator : public IdentifierIterator {
9250 /// The AST reader whose identifiers are being enumerated.
9251 const ASTReader &Reader;
9252
9253 /// The current index into the chain of AST files stored in
9254 /// the AST reader.
9255 unsigned Index;
9256
9257 /// The current position within the identifier lookup table
9258 /// of the current AST file.
9259 ASTIdentifierLookupTable::key_iterator Current;
9260
9261 /// The end position within the identifier lookup table of
9262 /// the current AST file.
9263 ASTIdentifierLookupTable::key_iterator End;
9264
9265 /// Whether to skip any modules in the ASTReader.
9266 bool SkipModules;
9267
9268 public:
9269 explicit ASTIdentifierIterator(const ASTReader &Reader,
9270 bool SkipModules = false);
9271
9272 StringRef Next() override;
9273 };
9274
9275} // namespace clang
9276
9277ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9278 bool SkipModules)
9279 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9280}
9281
9282StringRef ASTIdentifierIterator::Next() {
9283 while (Current == End) {
9284 // If we have exhausted all of our AST files, we're done.
9285 if (Index == 0)
9286 return StringRef();
9287
9288 --Index;
9289 ModuleFile &F = Reader.ModuleMgr[Index];
9290 if (SkipModules && F.isModule())
9291 continue;
9292
9293 ASTIdentifierLookupTable *IdTable =
9294 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9295 Current = IdTable->key_begin();
9296 End = IdTable->key_end();
9297 }
9298
9299 // We have any identifiers remaining in the current AST file; return
9300 // the next one.
9301 StringRef Result = *Current;
9302 ++Current;
9303 return Result;
9304}
9305
9306namespace {
9307
9308/// A utility for appending two IdentifierIterators.
9309class ChainedIdentifierIterator : public IdentifierIterator {
9310 std::unique_ptr<IdentifierIterator> Current;
9311 std::unique_ptr<IdentifierIterator> Queued;
9312
9313public:
9314 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9315 std::unique_ptr<IdentifierIterator> Second)
9316 : Current(std::move(First)), Queued(std::move(Second)) {}
9317
9318 StringRef Next() override {
9319 if (!Current)
9320 return StringRef();
9321
9322 StringRef result = Current->Next();
9323 if (!result.empty())
9324 return result;
9325
9326 // Try the queued iterator, which may itself be empty.
9327 Current.reset();
9328 std::swap(x&: Current, y&: Queued);
9329 return Next();
9330 }
9331};
9332
9333} // namespace
9334
9335IdentifierIterator *ASTReader::getIdentifiers() {
9336 if (!loadGlobalIndex()) {
9337 std::unique_ptr<IdentifierIterator> ReaderIter(
9338 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9339 std::unique_ptr<IdentifierIterator> ModulesIter(
9340 GlobalIndex->createIdentifierIterator());
9341 return new ChainedIdentifierIterator(std::move(ReaderIter),
9342 std::move(ModulesIter));
9343 }
9344
9345 return new ASTIdentifierIterator(*this);
9346}
9347
9348namespace clang {
9349namespace serialization {
9350
9351 class ReadMethodPoolVisitor {
9352 ASTReader &Reader;
9353 Selector Sel;
9354 unsigned PriorGeneration;
9355 unsigned InstanceBits = 0;
9356 unsigned FactoryBits = 0;
9357 bool InstanceHasMoreThanOneDecl = false;
9358 bool FactoryHasMoreThanOneDecl = false;
9359 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9360 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9361
9362 public:
9363 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9364 unsigned PriorGeneration)
9365 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9366
9367 bool operator()(ModuleFile &M) {
9368 if (!M.SelectorLookupTable)
9369 return false;
9370
9371 // If we've already searched this module file, skip it now.
9372 if (M.Generation <= PriorGeneration)
9373 return true;
9374
9375 ++Reader.NumMethodPoolTableLookups;
9376 ASTSelectorLookupTable *PoolTable
9377 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9378 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9379 if (Pos == PoolTable->end())
9380 return false;
9381
9382 ++Reader.NumMethodPoolTableHits;
9383 ++Reader.NumSelectorsRead;
9384 // FIXME: Not quite happy with the statistics here. We probably should
9385 // disable this tracking when called via LoadSelector.
9386 // Also, should entries without methods count as misses?
9387 ++Reader.NumMethodPoolEntriesRead;
9388 ASTSelectorLookupTrait::data_type Data = *Pos;
9389 if (Reader.DeserializationListener)
9390 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9391
9392 // Append methods in the reverse order, so that later we can process them
9393 // in the order they appear in the source code by iterating through
9394 // the vector in the reverse order.
9395 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9396 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9397 InstanceBits = Data.InstanceBits;
9398 FactoryBits = Data.FactoryBits;
9399 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9400 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9401 return false;
9402 }
9403
9404 /// Retrieve the instance methods found by this visitor.
9405 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9406 return InstanceMethods;
9407 }
9408
9409 /// Retrieve the instance methods found by this visitor.
9410 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9411 return FactoryMethods;
9412 }
9413
9414 unsigned getInstanceBits() const { return InstanceBits; }
9415 unsigned getFactoryBits() const { return FactoryBits; }
9416
9417 bool instanceHasMoreThanOneDecl() const {
9418 return InstanceHasMoreThanOneDecl;
9419 }
9420
9421 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9422 };
9423
9424} // namespace serialization
9425} // namespace clang
9426
9427/// Add the given set of methods to the method list.
9428static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9429 ObjCMethodList &List) {
9430 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9431 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9432}
9433
9434void ASTReader::ReadMethodPool(Selector Sel) {
9435 // Get the selector generation and update it to the current generation.
9436 unsigned &Generation = SelectorGeneration[Sel];
9437 unsigned PriorGeneration = Generation;
9438 Generation = getGeneration();
9439 SelectorOutOfDate[Sel] = false;
9440
9441 // Search for methods defined with this selector.
9442 ++NumMethodPoolLookups;
9443 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9444 ModuleMgr.visit(Visitor);
9445
9446 if (Visitor.getInstanceMethods().empty() &&
9447 Visitor.getFactoryMethods().empty())
9448 return;
9449
9450 ++NumMethodPoolHits;
9451
9452 if (!getSema())
9453 return;
9454
9455 Sema &S = *getSema();
9456 auto &Methods = S.ObjC().MethodPool[Sel];
9457
9458 Methods.first.setBits(Visitor.getInstanceBits());
9459 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9460 Methods.second.setBits(Visitor.getFactoryBits());
9461 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9462
9463 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9464 // when building a module we keep every method individually and may need to
9465 // update hasMoreThanOneDecl as we add the methods.
9466 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9467 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9468}
9469
9470void ASTReader::updateOutOfDateSelector(Selector Sel) {
9471 if (SelectorOutOfDate[Sel])
9472 ReadMethodPool(Sel);
9473}
9474
9475void ASTReader::ReadKnownNamespaces(
9476 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9477 Namespaces.clear();
9478
9479 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9480 if (NamespaceDecl *Namespace
9481 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9482 Namespaces.push_back(Elt: Namespace);
9483 }
9484}
9485
9486void ASTReader::ReadUndefinedButUsed(
9487 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9488 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9489 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9490 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9491 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9492 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9493 }
9494 UndefinedButUsed.clear();
9495}
9496
9497void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9498 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9499 Exprs) {
9500 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9501 FieldDecl *FD =
9502 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9503 uint64_t Count = DelayedDeleteExprs[Idx++];
9504 for (uint64_t C = 0; C < Count; ++C) {
9505 SourceLocation DeleteLoc =
9506 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9507 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9508 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9509 }
9510 }
9511}
9512
9513void ASTReader::ReadTentativeDefinitions(
9514 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9515 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9516 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9517 if (Var)
9518 TentativeDefs.push_back(Elt: Var);
9519 }
9520 TentativeDefinitions.clear();
9521}
9522
9523void ASTReader::ReadUnusedFileScopedDecls(
9524 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9525 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9526 DeclaratorDecl *D
9527 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9528 if (D)
9529 Decls.push_back(Elt: D);
9530 }
9531 UnusedFileScopedDecls.clear();
9532}
9533
9534void ASTReader::ReadDelegatingConstructors(
9535 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9536 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9537 CXXConstructorDecl *D
9538 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9539 if (D)
9540 Decls.push_back(Elt: D);
9541 }
9542 DelegatingCtorDecls.clear();
9543}
9544
9545void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9546 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9547 TypedefNameDecl *D
9548 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9549 if (D)
9550 Decls.push_back(Elt: D);
9551 }
9552 ExtVectorDecls.clear();
9553}
9554
9555void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9556 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9557 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9558 ++I) {
9559 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9560 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9561 if (D)
9562 Decls.insert(X: D);
9563 }
9564 UnusedLocalTypedefNameCandidates.clear();
9565}
9566
9567void ASTReader::ReadDeclsToCheckForDeferredDiags(
9568 llvm::SmallSetVector<Decl *, 4> &Decls) {
9569 for (auto I : DeclsToCheckForDeferredDiags) {
9570 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9571 if (D)
9572 Decls.insert(X: D);
9573 }
9574 DeclsToCheckForDeferredDiags.clear();
9575}
9576
9577void ASTReader::ReadReferencedSelectors(
9578 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9579 if (ReferencedSelectorsData.empty())
9580 return;
9581
9582 // If there are @selector references added them to its pool. This is for
9583 // implementation of -Wselector.
9584 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9585 unsigned I = 0;
9586 while (I < DataSize) {
9587 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9588 SourceLocation SelLoc
9589 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9590 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9591 }
9592 ReferencedSelectorsData.clear();
9593}
9594
9595void ASTReader::ReadWeakUndeclaredIdentifiers(
9596 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9597 if (WeakUndeclaredIdentifiers.empty())
9598 return;
9599
9600 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9601 IdentifierInfo *WeakId
9602 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9603 IdentifierInfo *AliasId
9604 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9605 SourceLocation Loc =
9606 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9607 WeakInfo WI(AliasId, Loc);
9608 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9609 }
9610 WeakUndeclaredIdentifiers.clear();
9611}
9612
9613void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9614 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9615 ExternalVTableUse VT;
9616 VTableUse &TableInfo = VTableUses[Idx++];
9617 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9618 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9619 VT.DefinitionRequired = TableInfo.Used;
9620 VTables.push_back(Elt: VT);
9621 }
9622
9623 VTableUses.clear();
9624}
9625
9626void ASTReader::ReadPendingInstantiations(
9627 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9628 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9629 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9630 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9631 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9632
9633 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9634 }
9635 PendingInstantiations.clear();
9636}
9637
9638void ASTReader::ReadLateParsedTemplates(
9639 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9640 &LPTMap) {
9641 for (auto &LPT : LateParsedTemplates) {
9642 ModuleFile *FMod = LPT.first;
9643 RecordDataImpl &LateParsed = LPT.second;
9644 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9645 /* In loop */) {
9646 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9647
9648 auto LT = std::make_unique<LateParsedTemplate>();
9649 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9650 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9651
9652 ModuleFile *F = getOwningModuleFile(D: LT->D);
9653 assert(F && "No module");
9654
9655 unsigned TokN = LateParsed[Idx++];
9656 LT->Toks.reserve(N: TokN);
9657 for (unsigned T = 0; T < TokN; ++T)
9658 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9659
9660 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9661 }
9662 }
9663
9664 LateParsedTemplates.clear();
9665}
9666
9667void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9668 if (!Lambda->getLambdaContextDecl())
9669 return;
9670
9671 auto LambdaInfo =
9672 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9673 y: Lambda->getLambdaIndexInContext());
9674
9675 // Handle the import and then include case for lambdas.
9676 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9677 Iter != LambdaDeclarationsForMerging.end() &&
9678 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9679 CXXRecordDecl *Previous =
9680 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9681 Lambda->setPreviousDecl(Previous);
9682 return;
9683 }
9684
9685 // Keep track of this lambda so it can be merged with another lambda that
9686 // is loaded later.
9687 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9688}
9689
9690void ASTReader::LoadSelector(Selector Sel) {
9691 // It would be complicated to avoid reading the methods anyway. So don't.
9692 ReadMethodPool(Sel);
9693}
9694
9695void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9696 assert(ID && "Non-zero identifier ID required");
9697 unsigned Index = translateIdentifierIDToIndex(ID).second;
9698 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9699 IdentifiersLoaded[Index] = II;
9700 if (DeserializationListener)
9701 DeserializationListener->IdentifierRead(ID, II);
9702}
9703
9704/// Set the globally-visible declarations associated with the given
9705/// identifier.
9706///
9707/// If the AST reader is currently in a state where the given declaration IDs
9708/// cannot safely be resolved, they are queued until it is safe to resolve
9709/// them.
9710///
9711/// \param II an IdentifierInfo that refers to one or more globally-visible
9712/// declarations.
9713///
9714/// \param DeclIDs the set of declaration IDs with the name @p II that are
9715/// visible at global scope.
9716///
9717/// \param Decls if non-null, this vector will be populated with the set of
9718/// deserialized declarations. These declarations will not be pushed into
9719/// scope.
9720void ASTReader::SetGloballyVisibleDecls(
9721 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9722 SmallVectorImpl<Decl *> *Decls) {
9723 if (NumCurrentElementsDeserializing && !Decls) {
9724 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9725 return;
9726 }
9727
9728 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9729 if (!SemaObj) {
9730 // Queue this declaration so that it will be added to the
9731 // translation unit scope and identifier's declaration chain
9732 // once a Sema object is known.
9733 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9734 continue;
9735 }
9736
9737 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9738
9739 // If we're simply supposed to record the declarations, do so now.
9740 if (Decls) {
9741 Decls->push_back(Elt: D);
9742 continue;
9743 }
9744
9745 // Introduce this declaration into the translation-unit scope
9746 // and add it to the declaration chain for this identifier, so
9747 // that (unqualified) name lookup will find it.
9748 pushExternalDeclIntoScope(D, Name: II);
9749 }
9750}
9751
9752std::pair<ModuleFile *, unsigned>
9753ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9754 if (ID == 0)
9755 return {nullptr, 0};
9756
9757 unsigned ModuleFileIndex = ID >> 32;
9758 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9759
9760 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9761 assert(getModuleManager().size() > ModuleFileIndex - 1);
9762
9763 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9764 assert(LocalID < MF.LocalNumIdentifiers);
9765 return {&MF, MF.BaseIdentifierID + LocalID};
9766}
9767
9768IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9769 if (ID == 0)
9770 return nullptr;
9771
9772 if (IdentifiersLoaded.empty()) {
9773 Error(Msg: "no identifier table in AST file");
9774 return nullptr;
9775 }
9776
9777 auto [M, Index] = translateIdentifierIDToIndex(ID);
9778 if (!IdentifiersLoaded[Index]) {
9779 assert(M != nullptr && "Untranslated Identifier ID?");
9780 assert(Index >= M->BaseIdentifierID);
9781 unsigned LocalIndex = Index - M->BaseIdentifierID;
9782 const unsigned char *Data =
9783 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9784
9785 ASTIdentifierLookupTrait Trait(*this, *M);
9786 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9787 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9788 auto &II = PP.getIdentifierTable().get(Name: Key);
9789 IdentifiersLoaded[Index] = &II;
9790 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9791 markIdentifierFromAST(Reader&: *this, II, IsModule);
9792 if (DeserializationListener)
9793 DeserializationListener->IdentifierRead(ID, II: &II);
9794 }
9795
9796 return IdentifiersLoaded[Index];
9797}
9798
9799IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9800 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9801}
9802
9803IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9804 if (LocalID < NUM_PREDEF_IDENT_IDS)
9805 return LocalID;
9806
9807 if (!M.ModuleOffsetMap.empty())
9808 ReadModuleOffsetMap(F&: M);
9809
9810 unsigned ModuleFileIndex = LocalID >> 32;
9811 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9812 ModuleFile *MF =
9813 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9814 assert(MF && "malformed identifier ID encoding?");
9815
9816 if (!ModuleFileIndex)
9817 LocalID -= NUM_PREDEF_IDENT_IDS;
9818
9819 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
9820}
9821
9822std::pair<ModuleFile *, unsigned>
9823ASTReader::translateMacroIDToIndex(MacroID ID) const {
9824 if (ID == 0)
9825 return {nullptr, 0};
9826
9827 unsigned ModuleFileIndex = ID >> 32;
9828 assert(ModuleFileIndex && "not translating loaded MacroID?");
9829 assert(getModuleManager().size() > ModuleFileIndex - 1);
9830 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9831
9832 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
9833 assert(LocalID < MF.LocalNumMacros);
9834 return {&MF, MF.BaseMacroID + LocalID};
9835}
9836
9837MacroInfo *ASTReader::getMacro(MacroID ID) {
9838 if (ID == 0)
9839 return nullptr;
9840
9841 if (MacrosLoaded.empty()) {
9842 Error(Msg: "no macro table in AST file");
9843 return nullptr;
9844 }
9845
9846 auto [M, Index] = translateMacroIDToIndex(ID);
9847 if (!MacrosLoaded[Index]) {
9848 assert(M != nullptr && "Untranslated Macro ID?");
9849 assert(Index >= M->BaseMacroID);
9850 unsigned LocalIndex = Index - M->BaseMacroID;
9851 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
9852 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
9853
9854 if (DeserializationListener)
9855 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
9856 }
9857
9858 return MacrosLoaded[Index];
9859}
9860
9861MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
9862 if (LocalID < NUM_PREDEF_MACRO_IDS)
9863 return LocalID;
9864
9865 if (!M.ModuleOffsetMap.empty())
9866 ReadModuleOffsetMap(F&: M);
9867
9868 unsigned ModuleFileIndex = LocalID >> 32;
9869 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
9870 ModuleFile *MF =
9871 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9872 assert(MF && "malformed identifier ID encoding?");
9873
9874 if (!ModuleFileIndex) {
9875 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
9876 LocalID -= NUM_PREDEF_MACRO_IDS;
9877 }
9878
9879 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
9880}
9881
9882serialization::SubmoduleID
9883ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
9884 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
9885 return LocalID;
9886
9887 if (!M.ModuleOffsetMap.empty())
9888 ReadModuleOffsetMap(F&: M);
9889
9890 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9891 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
9892 assert(I != M.SubmoduleRemap.end()
9893 && "Invalid index into submodule index remap");
9894
9895 return LocalID + I->second;
9896}
9897
9898Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
9899 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
9900 assert(GlobalID == 0 && "Unhandled global submodule ID");
9901 return nullptr;
9902 }
9903
9904 if (GlobalID > SubmodulesLoaded.size()) {
9905 Error(Msg: "submodule ID out of range in AST file");
9906 return nullptr;
9907 }
9908
9909 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
9910}
9911
9912Module *ASTReader::getModule(unsigned ID) {
9913 return getSubmodule(GlobalID: ID);
9914}
9915
9916ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
9917 if (ID & 1) {
9918 // It's a module, look it up by submodule ID.
9919 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
9920 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
9921 } else {
9922 // It's a prefix (preamble, PCH, ...). Look it up by index.
9923 int IndexFromEnd = static_cast<int>(ID >> 1);
9924 assert(IndexFromEnd && "got reference to unknown module file");
9925 return getModuleManager().pch_modules().end()[-IndexFromEnd];
9926 }
9927}
9928
9929unsigned ASTReader::getModuleFileID(ModuleFile *M) {
9930 if (!M)
9931 return 1;
9932
9933 // For a file representing a module, use the submodule ID of the top-level
9934 // module as the file ID. For any other kind of file, the number of such
9935 // files loaded beforehand will be the same on reload.
9936 // FIXME: Is this true even if we have an explicit module file and a PCH?
9937 if (M->isModule())
9938 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
9939
9940 auto PCHModules = getModuleManager().pch_modules();
9941 auto I = llvm::find(Range&: PCHModules, Val: M);
9942 assert(I != PCHModules.end() && "emitting reference to unknown file");
9943 return std::distance(first: I, last: PCHModules.end()) << 1;
9944}
9945
9946std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
9947 if (Module *M = getSubmodule(GlobalID: ID))
9948 return ASTSourceDescriptor(*M);
9949
9950 // If there is only a single PCH, return it instead.
9951 // Chained PCH are not supported.
9952 const auto &PCHChain = ModuleMgr.pch_modules();
9953 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
9954 ModuleFile &MF = ModuleMgr.getPrimaryModule();
9955 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
9956 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
9957 return ASTSourceDescriptor(ModuleName,
9958 llvm::sys::path::parent_path(path: MF.FileName),
9959 FileName, MF.Signature);
9960 }
9961 return std::nullopt;
9962}
9963
9964ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9965 auto I = DefinitionSource.find(Val: FD);
9966 if (I == DefinitionSource.end())
9967 return EK_ReplyHazy;
9968 return I->second ? EK_Never : EK_Always;
9969}
9970
9971bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
9972 return ThisDeclarationWasADefinitionSet.contains(V: FD);
9973}
9974
9975Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9976 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
9977}
9978
9979Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9980 if (ID == 0)
9981 return Selector();
9982
9983 if (ID > SelectorsLoaded.size()) {
9984 Error(Msg: "selector ID out of range in AST file");
9985 return Selector();
9986 }
9987
9988 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9989 // Load this selector from the selector table.
9990 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
9991 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9992 ModuleFile &M = *I->second;
9993 ASTSelectorLookupTrait Trait(*this, M);
9994 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9995 SelectorsLoaded[ID - 1] =
9996 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9997 if (DeserializationListener)
9998 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
9999 }
10000
10001 return SelectorsLoaded[ID - 1];
10002}
10003
10004Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10005 return DecodeSelector(ID);
10006}
10007
10008uint32_t ASTReader::GetNumExternalSelectors() {
10009 // ID 0 (the null selector) is considered an external selector.
10010 return getTotalNumSelectors() + 1;
10011}
10012
10013serialization::SelectorID
10014ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10015 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10016 return LocalID;
10017
10018 if (!M.ModuleOffsetMap.empty())
10019 ReadModuleOffsetMap(F&: M);
10020
10021 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10022 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10023 assert(I != M.SelectorRemap.end()
10024 && "Invalid index into selector index remap");
10025
10026 return LocalID + I->second;
10027}
10028
10029DeclarationNameLoc
10030ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10031 switch (Name.getNameKind()) {
10032 case DeclarationName::CXXConstructorName:
10033 case DeclarationName::CXXDestructorName:
10034 case DeclarationName::CXXConversionFunctionName:
10035 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10036
10037 case DeclarationName::CXXOperatorName:
10038 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10039
10040 case DeclarationName::CXXLiteralOperatorName:
10041 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10042 Loc: readSourceLocation());
10043
10044 case DeclarationName::Identifier:
10045 case DeclarationName::ObjCZeroArgSelector:
10046 case DeclarationName::ObjCOneArgSelector:
10047 case DeclarationName::ObjCMultiArgSelector:
10048 case DeclarationName::CXXUsingDirective:
10049 case DeclarationName::CXXDeductionGuideName:
10050 break;
10051 }
10052 return DeclarationNameLoc();
10053}
10054
10055DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10056 DeclarationNameInfo NameInfo;
10057 NameInfo.setName(readDeclarationName());
10058 NameInfo.setLoc(readSourceLocation());
10059 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10060 return NameInfo;
10061}
10062
10063TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10064 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10065}
10066
10067SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10068 auto Kind = readInt();
10069 auto ResultType = readQualType();
10070 auto Value = readAPInt();
10071 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10072 assert(Op.isValid());
10073 return Op;
10074}
10075
10076void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10077 Info.QualifierLoc = readNestedNameSpecifierLoc();
10078 unsigned NumTPLists = readInt();
10079 Info.NumTemplParamLists = NumTPLists;
10080 if (NumTPLists) {
10081 Info.TemplParamLists =
10082 new (getContext()) TemplateParameterList *[NumTPLists];
10083 for (unsigned i = 0; i != NumTPLists; ++i)
10084 Info.TemplParamLists[i] = readTemplateParameterList();
10085 }
10086}
10087
10088TemplateParameterList *
10089ASTRecordReader::readTemplateParameterList() {
10090 SourceLocation TemplateLoc = readSourceLocation();
10091 SourceLocation LAngleLoc = readSourceLocation();
10092 SourceLocation RAngleLoc = readSourceLocation();
10093
10094 unsigned NumParams = readInt();
10095 SmallVector<NamedDecl *, 16> Params;
10096 Params.reserve(N: NumParams);
10097 while (NumParams--)
10098 Params.push_back(Elt: readDeclAs<NamedDecl>());
10099
10100 bool HasRequiresClause = readBool();
10101 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10102
10103 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10104 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10105 return TemplateParams;
10106}
10107
10108void ASTRecordReader::readTemplateArgumentList(
10109 SmallVectorImpl<TemplateArgument> &TemplArgs,
10110 bool Canonicalize) {
10111 unsigned NumTemplateArgs = readInt();
10112 TemplArgs.reserve(N: NumTemplateArgs);
10113 while (NumTemplateArgs--)
10114 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10115}
10116
10117/// Read a UnresolvedSet structure.
10118void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10119 unsigned NumDecls = readInt();
10120 Set.reserve(C&: getContext(), N: NumDecls);
10121 while (NumDecls--) {
10122 GlobalDeclID ID = readDeclID();
10123 AccessSpecifier AS = (AccessSpecifier) readInt();
10124 Set.addLazyDecl(C&: getContext(), ID, AS);
10125 }
10126}
10127
10128CXXBaseSpecifier
10129ASTRecordReader::readCXXBaseSpecifier() {
10130 bool isVirtual = readBool();
10131 bool isBaseOfClass = readBool();
10132 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10133 bool inheritConstructors = readBool();
10134 TypeSourceInfo *TInfo = readTypeSourceInfo();
10135 SourceRange Range = readSourceRange();
10136 SourceLocation EllipsisLoc = readSourceLocation();
10137 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10138 EllipsisLoc);
10139 Result.setInheritConstructors(inheritConstructors);
10140 return Result;
10141}
10142
10143CXXCtorInitializer **
10144ASTRecordReader::readCXXCtorInitializers() {
10145 ASTContext &Context = getContext();
10146 unsigned NumInitializers = readInt();
10147 assert(NumInitializers && "wrote ctor initializers but have no inits");
10148 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10149 for (unsigned i = 0; i != NumInitializers; ++i) {
10150 TypeSourceInfo *TInfo = nullptr;
10151 bool IsBaseVirtual = false;
10152 FieldDecl *Member = nullptr;
10153 IndirectFieldDecl *IndirectMember = nullptr;
10154
10155 CtorInitializerType Type = (CtorInitializerType) readInt();
10156 switch (Type) {
10157 case CTOR_INITIALIZER_BASE:
10158 TInfo = readTypeSourceInfo();
10159 IsBaseVirtual = readBool();
10160 break;
10161
10162 case CTOR_INITIALIZER_DELEGATING:
10163 TInfo = readTypeSourceInfo();
10164 break;
10165
10166 case CTOR_INITIALIZER_MEMBER:
10167 Member = readDeclAs<FieldDecl>();
10168 break;
10169
10170 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10171 IndirectMember = readDeclAs<IndirectFieldDecl>();
10172 break;
10173 }
10174
10175 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10176 Expr *Init = readExpr();
10177 SourceLocation LParenLoc = readSourceLocation();
10178 SourceLocation RParenLoc = readSourceLocation();
10179
10180 CXXCtorInitializer *BOMInit;
10181 if (Type == CTOR_INITIALIZER_BASE)
10182 BOMInit = new (Context)
10183 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10184 RParenLoc, MemberOrEllipsisLoc);
10185 else if (Type == CTOR_INITIALIZER_DELEGATING)
10186 BOMInit = new (Context)
10187 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10188 else if (Member)
10189 BOMInit = new (Context)
10190 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10191 Init, RParenLoc);
10192 else
10193 BOMInit = new (Context)
10194 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10195 LParenLoc, Init, RParenLoc);
10196
10197 if (/*IsWritten*/readBool()) {
10198 unsigned SourceOrder = readInt();
10199 BOMInit->setSourceOrder(SourceOrder);
10200 }
10201
10202 CtorInitializers[i] = BOMInit;
10203 }
10204
10205 return CtorInitializers;
10206}
10207
10208NestedNameSpecifierLoc
10209ASTRecordReader::readNestedNameSpecifierLoc() {
10210 ASTContext &Context = getContext();
10211 unsigned N = readInt();
10212 NestedNameSpecifierLocBuilder Builder;
10213 for (unsigned I = 0; I != N; ++I) {
10214 auto Kind = readNestedNameSpecifierKind();
10215 switch (Kind) {
10216 case NestedNameSpecifier::Kind::Namespace: {
10217 auto *NS = readDeclAs<NamespaceBaseDecl>();
10218 SourceRange Range = readSourceRange();
10219 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10220 break;
10221 }
10222
10223 case NestedNameSpecifier::Kind::Type: {
10224 TypeSourceInfo *T = readTypeSourceInfo();
10225 if (!T)
10226 return NestedNameSpecifierLoc();
10227 SourceLocation ColonColonLoc = readSourceLocation();
10228 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10229 break;
10230 }
10231
10232 case NestedNameSpecifier::Kind::Global: {
10233 SourceLocation ColonColonLoc = readSourceLocation();
10234 Builder.MakeGlobal(Context, ColonColonLoc);
10235 break;
10236 }
10237
10238 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10239 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10240 SourceRange Range = readSourceRange();
10241 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10242 break;
10243 }
10244
10245 case NestedNameSpecifier::Kind::Null:
10246 llvm_unreachable("unexpected null nested name specifier");
10247 }
10248 }
10249
10250 return Builder.getWithLocInContext(Context);
10251}
10252
10253SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10254 unsigned &Idx) {
10255 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10256 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10257 return SourceRange(beg, end);
10258}
10259
10260llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10261 const StringRef Blob) {
10262 unsigned Count = Record[0];
10263 const char *Byte = Blob.data();
10264 llvm::BitVector Ret = llvm::BitVector(Count, false);
10265 for (unsigned I = 0; I < Count; ++Byte)
10266 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10267 if (*Byte & (1 << Bit))
10268 Ret[I] = true;
10269 return Ret;
10270}
10271
10272/// Read a floating-point value
10273llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10274 return llvm::APFloat(Sem, readAPInt());
10275}
10276
10277// Read a string
10278std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10279 unsigned Len = Record[Idx++];
10280 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10281 Idx += Len;
10282 return Result;
10283}
10284
10285StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10286 StringRef &Blob) {
10287 unsigned Len = Record[Idx++];
10288 StringRef Result = Blob.substr(Start: 0, N: Len);
10289 Blob = Blob.substr(Start: Len);
10290 return Result;
10291}
10292
10293std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10294 unsigned &Idx) {
10295 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10296}
10297
10298std::string ASTReader::ReadPath(StringRef BaseDirectory,
10299 const RecordData &Record, unsigned &Idx) {
10300 std::string Filename = ReadString(Record, Idx);
10301 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10302}
10303
10304std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10305 const RecordData &Record, unsigned &Idx,
10306 StringRef &Blob) {
10307 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10308 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10309}
10310
10311VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10312 unsigned &Idx) {
10313 unsigned Major = Record[Idx++];
10314 unsigned Minor = Record[Idx++];
10315 unsigned Subminor = Record[Idx++];
10316 if (Minor == 0)
10317 return VersionTuple(Major);
10318 if (Subminor == 0)
10319 return VersionTuple(Major, Minor - 1);
10320 return VersionTuple(Major, Minor - 1, Subminor - 1);
10321}
10322
10323CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10324 const RecordData &Record,
10325 unsigned &Idx) {
10326 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10327 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10328}
10329
10330DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10331 return Diag(Loc: CurrentImportLoc, DiagID);
10332}
10333
10334DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10335 return Diags.Report(Loc, DiagID);
10336}
10337
10338void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10339 llvm::function_ref<void()> Fn) {
10340 // When Sema is available, avoid duplicate errors.
10341 if (SemaObj) {
10342 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10343 return;
10344 }
10345
10346 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10347}
10348
10349/// Retrieve the identifier table associated with the
10350/// preprocessor.
10351IdentifierTable &ASTReader::getIdentifierTable() {
10352 return PP.getIdentifierTable();
10353}
10354
10355/// Record that the given ID maps to the given switch-case
10356/// statement.
10357void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10358 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10359 "Already have a SwitchCase with this ID");
10360 (*CurrSwitchCaseStmts)[ID] = SC;
10361}
10362
10363/// Retrieve the switch-case statement with the given ID.
10364SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10365 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10366 return (*CurrSwitchCaseStmts)[ID];
10367}
10368
10369void ASTReader::ClearSwitchCaseIDs() {
10370 CurrSwitchCaseStmts->clear();
10371}
10372
10373void ASTReader::ReadComments() {
10374 ASTContext &Context = getContext();
10375 std::vector<RawComment *> Comments;
10376 for (SmallVectorImpl<std::pair<BitstreamCursor,
10377 serialization::ModuleFile *>>::iterator
10378 I = CommentsCursors.begin(),
10379 E = CommentsCursors.end();
10380 I != E; ++I) {
10381 Comments.clear();
10382 BitstreamCursor &Cursor = I->first;
10383 serialization::ModuleFile &F = *I->second;
10384 SavedStreamPosition SavedPosition(Cursor);
10385
10386 RecordData Record;
10387 while (true) {
10388 Expected<llvm::BitstreamEntry> MaybeEntry =
10389 Cursor.advanceSkippingSubblocks(
10390 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10391 if (!MaybeEntry) {
10392 Error(Err: MaybeEntry.takeError());
10393 return;
10394 }
10395 llvm::BitstreamEntry Entry = MaybeEntry.get();
10396
10397 switch (Entry.Kind) {
10398 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10399 case llvm::BitstreamEntry::Error:
10400 Error(Msg: "malformed block record in AST file");
10401 return;
10402 case llvm::BitstreamEntry::EndBlock:
10403 goto NextCursor;
10404 case llvm::BitstreamEntry::Record:
10405 // The interesting case.
10406 break;
10407 }
10408
10409 // Read a record.
10410 Record.clear();
10411 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10412 if (!MaybeComment) {
10413 Error(Err: MaybeComment.takeError());
10414 return;
10415 }
10416 switch ((CommentRecordTypes)MaybeComment.get()) {
10417 case COMMENTS_RAW_COMMENT: {
10418 unsigned Idx = 0;
10419 SourceRange SR = ReadSourceRange(F, Record, Idx);
10420 RawComment::CommentKind Kind =
10421 (RawComment::CommentKind) Record[Idx++];
10422 bool IsTrailingComment = Record[Idx++];
10423 bool IsAlmostTrailingComment = Record[Idx++];
10424 Comments.push_back(x: new (Context) RawComment(
10425 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10426 break;
10427 }
10428 }
10429 }
10430 NextCursor:
10431 for (RawComment *C : Comments) {
10432 SourceLocation CommentLoc = C->getBeginLoc();
10433 if (CommentLoc.isValid()) {
10434 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10435 if (Loc.first.isValid())
10436 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10437 }
10438 }
10439 }
10440}
10441
10442void ASTReader::visitInputFileInfos(
10443 serialization::ModuleFile &MF, bool IncludeSystem,
10444 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10445 bool IsSystem)>
10446 Visitor) {
10447 unsigned NumUserInputs = MF.NumUserInputFiles;
10448 unsigned NumInputs = MF.InputFilesLoaded.size();
10449 assert(NumUserInputs <= NumInputs);
10450 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10451 for (unsigned I = 0; I < N; ++I) {
10452 bool IsSystem = I >= NumUserInputs;
10453 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10454 Visitor(IFI, IsSystem);
10455 }
10456}
10457
10458void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10459 bool IncludeSystem, bool Complain,
10460 llvm::function_ref<void(const serialization::InputFile &IF,
10461 bool isSystem)> Visitor) {
10462 unsigned NumUserInputs = MF.NumUserInputFiles;
10463 unsigned NumInputs = MF.InputFilesLoaded.size();
10464 assert(NumUserInputs <= NumInputs);
10465 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10466 for (unsigned I = 0; I < N; ++I) {
10467 bool IsSystem = I >= NumUserInputs;
10468 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10469 Visitor(IF, IsSystem);
10470 }
10471}
10472
10473void ASTReader::visitTopLevelModuleMaps(
10474 serialization::ModuleFile &MF,
10475 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10476 unsigned NumInputs = MF.InputFilesLoaded.size();
10477 for (unsigned I = 0; I < NumInputs; ++I) {
10478 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10479 if (IFI.TopLevel && IFI.ModuleMap)
10480 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10481 Visitor(*FE);
10482 }
10483}
10484
10485void ASTReader::finishPendingActions() {
10486 while (!PendingIdentifierInfos.empty() ||
10487 !PendingDeducedFunctionTypes.empty() ||
10488 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10489 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10490 !PendingUpdateRecords.empty() ||
10491 !PendingObjCExtensionIvarRedeclarations.empty()) {
10492 // If any identifiers with corresponding top-level declarations have
10493 // been loaded, load those declarations now.
10494 using TopLevelDeclsMap =
10495 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10496 TopLevelDeclsMap TopLevelDecls;
10497
10498 while (!PendingIdentifierInfos.empty()) {
10499 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10500 SmallVector<GlobalDeclID, 4> DeclIDs =
10501 std::move(PendingIdentifierInfos.back().second);
10502 PendingIdentifierInfos.pop_back();
10503
10504 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10505 }
10506
10507 // Load each function type that we deferred loading because it was a
10508 // deduced type that might refer to a local type declared within itself.
10509 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10510 auto *FD = PendingDeducedFunctionTypes[I].first;
10511 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10512
10513 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10514 // If we gave a function a deduced return type, remember that we need to
10515 // propagate that along the redeclaration chain.
10516 if (DT->isDeduced()) {
10517 PendingDeducedTypeUpdates.insert(
10518 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10519 continue;
10520 }
10521
10522 // The function has undeduced DeduceType return type. We hope we can
10523 // find the deduced type by iterating the redecls in other modules
10524 // later.
10525 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10526 continue;
10527 }
10528 }
10529 PendingDeducedFunctionTypes.clear();
10530
10531 // Load each variable type that we deferred loading because it was a
10532 // deduced type that might refer to a local type declared within itself.
10533 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10534 auto *VD = PendingDeducedVarTypes[I].first;
10535 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10536 }
10537 PendingDeducedVarTypes.clear();
10538
10539 // Load pending declaration chains.
10540 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10541 loadPendingDeclChain(D: PendingDeclChains[I].first,
10542 LocalOffset: PendingDeclChains[I].second);
10543 PendingDeclChains.clear();
10544
10545 // Make the most recent of the top-level declarations visible.
10546 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10547 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10548 IdentifierInfo *II = TLD->first;
10549 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10550 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10551 }
10552 }
10553
10554 // Load any pending macro definitions.
10555 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10556 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10557 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10558 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10559 // Initialize the macro history from chained-PCHs ahead of module imports.
10560 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10561 ++IDIdx) {
10562 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10563 if (!Info.M->isModule())
10564 resolvePendingMacro(II, PMInfo: Info);
10565 }
10566 // Handle module imports.
10567 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10568 ++IDIdx) {
10569 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10570 if (Info.M->isModule())
10571 resolvePendingMacro(II, PMInfo: Info);
10572 }
10573 }
10574 PendingMacroIDs.clear();
10575
10576 // Wire up the DeclContexts for Decls that we delayed setting until
10577 // recursive loading is completed.
10578 while (!PendingDeclContextInfos.empty()) {
10579 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10580 PendingDeclContextInfos.pop_front();
10581 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10582 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10583 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10584 }
10585
10586 // Perform any pending declaration updates.
10587 while (!PendingUpdateRecords.empty()) {
10588 auto Update = PendingUpdateRecords.pop_back_val();
10589 ReadingKindTracker ReadingKind(Read_Decl, *this);
10590 loadDeclUpdateRecords(Record&: Update);
10591 }
10592
10593 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10594 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10595 auto DuplicateIvars =
10596 PendingObjCExtensionIvarRedeclarations.back().second;
10597 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10598 StructuralEquivalenceContext Ctx(
10599 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10600 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10601 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10602 /*Complain =*/false,
10603 /*ErrorOnTagTypeMismatch =*/true);
10604 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10605 // Merge redeclared ivars with their predecessors.
10606 for (auto IvarPair : DuplicateIvars) {
10607 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10608 // Change semantic DeclContext but keep the lexical one.
10609 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10610 LexicalDC: Ivar->getLexicalDeclContext(),
10611 Ctx&: getContext());
10612 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10613 }
10614 // Invalidate duplicate extension and the cached ivar list.
10615 ExtensionsPair.first->setInvalidDecl();
10616 ExtensionsPair.second->getClassInterface()
10617 ->getDefinition()
10618 ->setIvarList(nullptr);
10619 } else {
10620 for (auto IvarPair : DuplicateIvars) {
10621 Diag(Loc: IvarPair.first->getLocation(),
10622 DiagID: diag::err_duplicate_ivar_declaration)
10623 << IvarPair.first->getIdentifier();
10624 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10625 }
10626 }
10627 PendingObjCExtensionIvarRedeclarations.pop_back();
10628 }
10629 }
10630
10631 // At this point, all update records for loaded decls are in place, so any
10632 // fake class definitions should have become real.
10633 assert(PendingFakeDefinitionData.empty() &&
10634 "faked up a class definition but never saw the real one");
10635
10636 // If we deserialized any C++ or Objective-C class definitions, any
10637 // Objective-C protocol definitions, or any redeclarable templates, make sure
10638 // that all redeclarations point to the definitions. Note that this can only
10639 // happen now, after the redeclaration chains have been fully wired.
10640 for (Decl *D : PendingDefinitions) {
10641 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10642 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10643 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10644 R = R->getPreviousDecl()) {
10645 assert((R == D) ==
10646 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10647 "declaration thinks it's the definition but it isn't");
10648 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10649 }
10650 }
10651
10652 continue;
10653 }
10654
10655 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10656 // Make sure that the ObjCInterfaceType points at the definition.
10657 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10658 ->Decl = ID;
10659
10660 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10661 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10662
10663 continue;
10664 }
10665
10666 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10667 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10668 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10669
10670 continue;
10671 }
10672
10673 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10674 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10675 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10676 }
10677 PendingDefinitions.clear();
10678
10679 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10680 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10681 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10682 return VD->isThisDeclarationADefinition() ||
10683 VD->isThisDeclarationADemotedDefinition();
10684
10685 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10686 return TD->isThisDeclarationADefinition() ||
10687 TD->isThisDeclarationADemotedDefinition();
10688
10689 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10690 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10691
10692 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10693 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10694
10695 // Conservatively return false here.
10696 return false;
10697 };
10698
10699 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10700 return hasDefinitionImpl(D, hasDefinitionImpl);
10701 };
10702
10703 // It is not good to prevent multiple declarations since the forward
10704 // declaration is common. Let's try to avoid duplicated definitions
10705 // only.
10706 if (!hasDefinition(D) || !hasDefinition(Previous))
10707 continue;
10708
10709 Module *PM = Previous->getOwningModule();
10710 Module *DM = D->getOwningModule();
10711 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10712 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10713 << (DM ? DM->getTopLevelModuleName() : "global module");
10714 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10715 }
10716 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10717
10718 // Load the bodies of any functions or methods we've encountered. We do
10719 // this now (delayed) so that we can be sure that the declaration chains
10720 // have been fully wired up (hasBody relies on this).
10721 // FIXME: We shouldn't require complete redeclaration chains here.
10722 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10723 PBEnd = PendingBodies.end();
10724 PB != PBEnd; ++PB) {
10725 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10726 // FIXME: Check for =delete/=default?
10727 const FunctionDecl *Defn = nullptr;
10728 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10729 FD->setLazyBody(PB->second);
10730 } else {
10731 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10732 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10733
10734 if (!FD->isLateTemplateParsed() &&
10735 !NonConstDefn->isLateTemplateParsed() &&
10736 // We only perform ODR checks for decls not in the explicit
10737 // global module fragment.
10738 !shouldSkipCheckingODR(D: FD) &&
10739 !shouldSkipCheckingODR(D: NonConstDefn) &&
10740 FD->getODRHash() != NonConstDefn->getODRHash()) {
10741 if (!isa<CXXMethodDecl>(Val: FD)) {
10742 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10743 } else if (FD->getLexicalParent()->isFileContext() &&
10744 NonConstDefn->getLexicalParent()->isFileContext()) {
10745 // Only diagnose out-of-line method definitions. If they are
10746 // in class definitions, then an error will be generated when
10747 // processing the class bodies.
10748 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10749 }
10750 }
10751 }
10752 continue;
10753 }
10754
10755 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10756 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10757 MD->setLazyBody(PB->second);
10758 }
10759 PendingBodies.clear();
10760
10761 // Inform any classes that had members added that they now have more members.
10762 for (auto [RD, MD] : PendingAddedClassMembers) {
10763 RD->addedMember(D: MD);
10764 }
10765 PendingAddedClassMembers.clear();
10766
10767 // Do some cleanup.
10768 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10769 getContext().deduplicateMergedDefinitionsFor(ND);
10770 PendingMergedDefinitionsToDeduplicate.clear();
10771
10772 // For each decl chain that we wanted to complete while deserializing, mark
10773 // it as "still needs to be completed".
10774 for (Decl *D : PendingIncompleteDeclChains)
10775 markIncompleteDeclChain(D);
10776 PendingIncompleteDeclChains.clear();
10777
10778 assert(PendingIdentifierInfos.empty() &&
10779 "Should be empty at the end of finishPendingActions");
10780 assert(PendingDeducedFunctionTypes.empty() &&
10781 "Should be empty at the end of finishPendingActions");
10782 assert(PendingDeducedVarTypes.empty() &&
10783 "Should be empty at the end of finishPendingActions");
10784 assert(PendingDeclChains.empty() &&
10785 "Should be empty at the end of finishPendingActions");
10786 assert(PendingMacroIDs.empty() &&
10787 "Should be empty at the end of finishPendingActions");
10788 assert(PendingDeclContextInfos.empty() &&
10789 "Should be empty at the end of finishPendingActions");
10790 assert(PendingUpdateRecords.empty() &&
10791 "Should be empty at the end of finishPendingActions");
10792 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10793 "Should be empty at the end of finishPendingActions");
10794 assert(PendingFakeDefinitionData.empty() &&
10795 "Should be empty at the end of finishPendingActions");
10796 assert(PendingDefinitions.empty() &&
10797 "Should be empty at the end of finishPendingActions");
10798 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10799 "Should be empty at the end of finishPendingActions");
10800 assert(PendingBodies.empty() &&
10801 "Should be empty at the end of finishPendingActions");
10802 assert(PendingAddedClassMembers.empty() &&
10803 "Should be empty at the end of finishPendingActions");
10804 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10805 "Should be empty at the end of finishPendingActions");
10806 assert(PendingIncompleteDeclChains.empty() &&
10807 "Should be empty at the end of finishPendingActions");
10808}
10809
10810void ASTReader::diagnoseOdrViolations() {
10811 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10812 PendingRecordOdrMergeFailures.empty() &&
10813 PendingFunctionOdrMergeFailures.empty() &&
10814 PendingEnumOdrMergeFailures.empty() &&
10815 PendingObjCInterfaceOdrMergeFailures.empty() &&
10816 PendingObjCProtocolOdrMergeFailures.empty())
10817 return;
10818
10819 // Trigger the import of the full definition of each class that had any
10820 // odr-merging problems, so we can produce better diagnostics for them.
10821 // These updates may in turn find and diagnose some ODR failures, so take
10822 // ownership of the set first.
10823 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10824 PendingOdrMergeFailures.clear();
10825 for (auto &Merge : OdrMergeFailures) {
10826 Merge.first->buildLookup();
10827 Merge.first->decls_begin();
10828 Merge.first->bases_begin();
10829 Merge.first->vbases_begin();
10830 for (auto &RecordPair : Merge.second) {
10831 auto *RD = RecordPair.first;
10832 RD->decls_begin();
10833 RD->bases_begin();
10834 RD->vbases_begin();
10835 }
10836 }
10837
10838 // Trigger the import of the full definition of each record in C/ObjC.
10839 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
10840 PendingRecordOdrMergeFailures.clear();
10841 for (auto &Merge : RecordOdrMergeFailures) {
10842 Merge.first->decls_begin();
10843 for (auto &D : Merge.second)
10844 D->decls_begin();
10845 }
10846
10847 // Trigger the import of the full interface definition.
10848 auto ObjCInterfaceOdrMergeFailures =
10849 std::move(PendingObjCInterfaceOdrMergeFailures);
10850 PendingObjCInterfaceOdrMergeFailures.clear();
10851 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10852 Merge.first->decls_begin();
10853 for (auto &InterfacePair : Merge.second)
10854 InterfacePair.first->decls_begin();
10855 }
10856
10857 // Trigger the import of functions.
10858 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10859 PendingFunctionOdrMergeFailures.clear();
10860 for (auto &Merge : FunctionOdrMergeFailures) {
10861 Merge.first->buildLookup();
10862 Merge.first->decls_begin();
10863 Merge.first->getBody();
10864 for (auto &FD : Merge.second) {
10865 FD->buildLookup();
10866 FD->decls_begin();
10867 FD->getBody();
10868 }
10869 }
10870
10871 // Trigger the import of enums.
10872 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10873 PendingEnumOdrMergeFailures.clear();
10874 for (auto &Merge : EnumOdrMergeFailures) {
10875 Merge.first->decls_begin();
10876 for (auto &Enum : Merge.second) {
10877 Enum->decls_begin();
10878 }
10879 }
10880
10881 // Trigger the import of the full protocol definition.
10882 auto ObjCProtocolOdrMergeFailures =
10883 std::move(PendingObjCProtocolOdrMergeFailures);
10884 PendingObjCProtocolOdrMergeFailures.clear();
10885 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10886 Merge.first->decls_begin();
10887 for (auto &ProtocolPair : Merge.second)
10888 ProtocolPair.first->decls_begin();
10889 }
10890
10891 // For each declaration from a merged context, check that the canonical
10892 // definition of that context also contains a declaration of the same
10893 // entity.
10894 //
10895 // Caution: this loop does things that might invalidate iterators into
10896 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10897 while (!PendingOdrMergeChecks.empty()) {
10898 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10899
10900 // FIXME: Skip over implicit declarations for now. This matters for things
10901 // like implicitly-declared special member functions. This isn't entirely
10902 // correct; we can end up with multiple unmerged declarations of the same
10903 // implicit entity.
10904 if (D->isImplicit())
10905 continue;
10906
10907 DeclContext *CanonDef = D->getDeclContext();
10908
10909 bool Found = false;
10910 const Decl *DCanon = D->getCanonicalDecl();
10911
10912 for (auto *RI : D->redecls()) {
10913 if (RI->getLexicalDeclContext() == CanonDef) {
10914 Found = true;
10915 break;
10916 }
10917 }
10918 if (Found)
10919 continue;
10920
10921 // Quick check failed, time to do the slow thing. Note, we can't just
10922 // look up the name of D in CanonDef here, because the member that is
10923 // in CanonDef might not be found by name lookup (it might have been
10924 // replaced by a more recent declaration in the lookup table), and we
10925 // can't necessarily find it in the redeclaration chain because it might
10926 // be merely mergeable, not redeclarable.
10927 llvm::SmallVector<const NamedDecl*, 4> Candidates;
10928 for (auto *CanonMember : CanonDef->decls()) {
10929 if (CanonMember->getCanonicalDecl() == DCanon) {
10930 // This can happen if the declaration is merely mergeable and not
10931 // actually redeclarable (we looked for redeclarations earlier).
10932 //
10933 // FIXME: We should be able to detect this more efficiently, without
10934 // pulling in all of the members of CanonDef.
10935 Found = true;
10936 break;
10937 }
10938 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
10939 if (ND->getDeclName() == D->getDeclName())
10940 Candidates.push_back(Elt: ND);
10941 }
10942
10943 if (!Found) {
10944 // The AST doesn't like TagDecls becoming invalid after they've been
10945 // completed. We only really need to mark FieldDecls as invalid here.
10946 if (!isa<TagDecl>(Val: D))
10947 D->setInvalidDecl();
10948
10949 // Ensure we don't accidentally recursively enter deserialization while
10950 // we're producing our diagnostic.
10951 Deserializing RecursionGuard(this);
10952
10953 std::string CanonDefModule =
10954 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
10955 D: cast<Decl>(Val: CanonDef));
10956 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
10957 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
10958 << CanonDef << CanonDefModule.empty() << CanonDefModule;
10959
10960 if (Candidates.empty())
10961 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
10962 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
10963 else {
10964 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10965 Diag(Loc: Candidates[I]->getLocation(),
10966 DiagID: diag::note_module_odr_violation_possible_decl)
10967 << Candidates[I];
10968 }
10969
10970 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
10971 }
10972 }
10973
10974 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
10975 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
10976 ObjCInterfaceOdrMergeFailures.empty() &&
10977 ObjCProtocolOdrMergeFailures.empty())
10978 return;
10979
10980 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
10981 getPreprocessor().getLangOpts());
10982
10983 // Issue any pending ODR-failure diagnostics.
10984 for (auto &Merge : OdrMergeFailures) {
10985 // If we've already pointed out a specific problem with this class, don't
10986 // bother issuing a general "something's different" diagnostic.
10987 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10988 continue;
10989
10990 bool Diagnosed = false;
10991 CXXRecordDecl *FirstRecord = Merge.first;
10992 for (auto &RecordPair : Merge.second) {
10993 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
10994 SecondDD: RecordPair.second)) {
10995 Diagnosed = true;
10996 break;
10997 }
10998 }
10999
11000 if (!Diagnosed) {
11001 // All definitions are updates to the same declaration. This happens if a
11002 // module instantiates the declaration of a class template specialization
11003 // and two or more other modules instantiate its definition.
11004 //
11005 // FIXME: Indicate which modules had instantiations of this definition.
11006 // FIXME: How can this even happen?
11007 Diag(Loc: Merge.first->getLocation(),
11008 DiagID: diag::err_module_odr_violation_different_instantiations)
11009 << Merge.first;
11010 }
11011 }
11012
11013 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11014 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11015 for (auto &Merge : RecordOdrMergeFailures) {
11016 // If we've already pointed out a specific problem with this class, don't
11017 // bother issuing a general "something's different" diagnostic.
11018 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11019 continue;
11020
11021 RecordDecl *FirstRecord = Merge.first;
11022 bool Diagnosed = false;
11023 for (auto *SecondRecord : Merge.second) {
11024 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11025 Diagnosed = true;
11026 break;
11027 }
11028 }
11029 (void)Diagnosed;
11030 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11031 }
11032
11033 // Issue ODR failures diagnostics for functions.
11034 for (auto &Merge : FunctionOdrMergeFailures) {
11035 FunctionDecl *FirstFunction = Merge.first;
11036 bool Diagnosed = false;
11037 for (auto &SecondFunction : Merge.second) {
11038 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11039 Diagnosed = true;
11040 break;
11041 }
11042 }
11043 (void)Diagnosed;
11044 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11045 }
11046
11047 // Issue ODR failures diagnostics for enums.
11048 for (auto &Merge : EnumOdrMergeFailures) {
11049 // If we've already pointed out a specific problem with this enum, don't
11050 // bother issuing a general "something's different" diagnostic.
11051 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11052 continue;
11053
11054 EnumDecl *FirstEnum = Merge.first;
11055 bool Diagnosed = false;
11056 for (auto &SecondEnum : Merge.second) {
11057 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11058 Diagnosed = true;
11059 break;
11060 }
11061 }
11062 (void)Diagnosed;
11063 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11064 }
11065
11066 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11067 // If we've already pointed out a specific problem with this interface,
11068 // don't bother issuing a general "something's different" diagnostic.
11069 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11070 continue;
11071
11072 bool Diagnosed = false;
11073 ObjCInterfaceDecl *FirstID = Merge.first;
11074 for (auto &InterfacePair : Merge.second) {
11075 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11076 SecondDD: InterfacePair.second)) {
11077 Diagnosed = true;
11078 break;
11079 }
11080 }
11081 (void)Diagnosed;
11082 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11083 }
11084
11085 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11086 // If we've already pointed out a specific problem with this protocol,
11087 // don't bother issuing a general "something's different" diagnostic.
11088 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11089 continue;
11090
11091 ObjCProtocolDecl *FirstProtocol = Merge.first;
11092 bool Diagnosed = false;
11093 for (auto &ProtocolPair : Merge.second) {
11094 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11095 SecondDD: ProtocolPair.second)) {
11096 Diagnosed = true;
11097 break;
11098 }
11099 }
11100 (void)Diagnosed;
11101 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11102 }
11103}
11104
11105void ASTReader::StartedDeserializing() {
11106 if (llvm::Timer *T = ReadTimer.get();
11107 ++NumCurrentElementsDeserializing == 1 && T)
11108 ReadTimeRegion.emplace(args&: T);
11109}
11110
11111void ASTReader::FinishedDeserializing() {
11112 assert(NumCurrentElementsDeserializing &&
11113 "FinishedDeserializing not paired with StartedDeserializing");
11114 if (NumCurrentElementsDeserializing == 1) {
11115 // We decrease NumCurrentElementsDeserializing only after pending actions
11116 // are finished, to avoid recursively re-calling finishPendingActions().
11117 finishPendingActions();
11118 }
11119 --NumCurrentElementsDeserializing;
11120
11121 if (NumCurrentElementsDeserializing == 0) {
11122 {
11123 // Guard variable to avoid recursively entering the process of passing
11124 // decls to consumer.
11125 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11126 /*NewValue=*/false);
11127
11128 // Propagate exception specification and deduced type updates along
11129 // redeclaration chains.
11130 //
11131 // We do this now rather than in finishPendingActions because we want to
11132 // be able to walk the complete redeclaration chains of the updated decls.
11133 while (!PendingExceptionSpecUpdates.empty() ||
11134 !PendingDeducedTypeUpdates.empty() ||
11135 !PendingUndeducedFunctionDecls.empty()) {
11136 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11137 PendingExceptionSpecUpdates.clear();
11138 for (auto Update : ESUpdates) {
11139 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11140 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11141 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11142 if (auto *Listener = getContext().getASTMutationListener())
11143 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11144 for (auto *Redecl : Update.second->redecls())
11145 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11146 }
11147
11148 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11149 PendingDeducedTypeUpdates.clear();
11150 for (auto Update : DTUpdates) {
11151 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11152 // FIXME: If the return type is already deduced, check that it
11153 // matches.
11154 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11155 ResultType: Update.second);
11156 }
11157
11158 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11159 PendingUndeducedFunctionDecls.clear();
11160 // We hope we can find the deduced type for the functions by iterating
11161 // redeclarations in other modules.
11162 for (FunctionDecl *UndeducedFD : UDTUpdates)
11163 (void)UndeducedFD->getMostRecentDecl();
11164 }
11165
11166 ReadTimeRegion.reset();
11167
11168 diagnoseOdrViolations();
11169 }
11170
11171 // We are not in recursive loading, so it's safe to pass the "interesting"
11172 // decls to the consumer.
11173 if (Consumer)
11174 PassInterestingDeclsToConsumer();
11175 }
11176}
11177
11178void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11179 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11180 // Remove any fake results before adding any real ones.
11181 auto It = PendingFakeLookupResults.find(Key: II);
11182 if (It != PendingFakeLookupResults.end()) {
11183 for (auto *ND : It->second)
11184 SemaObj->IdResolver.RemoveDecl(D: ND);
11185 // FIXME: this works around module+PCH performance issue.
11186 // Rather than erase the result from the map, which is O(n), just clear
11187 // the vector of NamedDecls.
11188 It->second.clear();
11189 }
11190 }
11191
11192 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11193 SemaObj->TUScope->AddDecl(D);
11194 } else if (SemaObj->TUScope) {
11195 // Adding the decl to IdResolver may have failed because it was already in
11196 // (even though it was not added in scope). If it is already in, make sure
11197 // it gets in the scope as well.
11198 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11199 SemaObj->TUScope->AddDecl(D);
11200 }
11201}
11202
11203ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11204 ASTContext *Context,
11205 const PCHContainerReader &PCHContainerRdr,
11206 const CodeGenOptions &CodeGenOpts,
11207 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11208 StringRef isysroot,
11209 DisableValidationForModuleKind DisableValidationKind,
11210 bool AllowASTWithCompilerErrors,
11211 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11212 bool ForceValidateUserInputs,
11213 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11214 std::unique_ptr<llvm::Timer> ReadTimer)
11215 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11216 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11217 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11218 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11219 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11220 StackHandler(Diags), PP(PP), ContextObj(Context),
11221 CodeGenOpts(CodeGenOpts),
11222 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11223 PP.getHeaderSearchInfo()),
11224 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11225 DisableValidationKind(DisableValidationKind),
11226 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11227 AllowConfigurationMismatch(AllowConfigurationMismatch),
11228 ValidateSystemInputs(ValidateSystemInputs),
11229 ForceValidateUserInputs(ForceValidateUserInputs),
11230 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11231 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11232 SourceMgr.setExternalSLocEntrySource(this);
11233
11234 PathBuf.reserve(N: 256);
11235
11236 for (const auto &Ext : Extensions) {
11237 auto BlockName = Ext->getExtensionMetadata().BlockName;
11238 auto Known = ModuleFileExtensions.find(Key: BlockName);
11239 if (Known != ModuleFileExtensions.end()) {
11240 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11241 << BlockName;
11242 continue;
11243 }
11244
11245 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11246 }
11247}
11248
11249ASTReader::~ASTReader() {
11250 if (OwnsDeserializationListener)
11251 delete DeserializationListener;
11252}
11253
11254IdentifierResolver &ASTReader::getIdResolver() {
11255 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11256}
11257
11258Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11259 unsigned AbbrevID) {
11260 Idx = 0;
11261 Record.clear();
11262 return Cursor.readRecord(AbbrevID, Vals&: Record);
11263}
11264//===----------------------------------------------------------------------===//
11265//// OMPClauseReader implementation
11266////===----------------------------------------------------------------------===//
11267
11268// This has to be in namespace clang because it's friended by all
11269// of the OMP clauses.
11270namespace clang {
11271
11272class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11273 ASTRecordReader &Record;
11274 ASTContext &Context;
11275
11276public:
11277 OMPClauseReader(ASTRecordReader &Record)
11278 : Record(Record), Context(Record.getContext()) {}
11279#define GEN_CLANG_CLAUSE_CLASS
11280#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11281#include "llvm/Frontend/OpenMP/OMP.inc"
11282 OMPClause *readClause();
11283 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11284 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11285};
11286
11287} // end namespace clang
11288
11289OMPClause *ASTRecordReader::readOMPClause() {
11290 return OMPClauseReader(*this).readClause();
11291}
11292
11293OMPClause *OMPClauseReader::readClause() {
11294 OMPClause *C = nullptr;
11295 switch (llvm::omp::Clause(Record.readInt())) {
11296 case llvm::omp::OMPC_if:
11297 C = new (Context) OMPIfClause();
11298 break;
11299 case llvm::omp::OMPC_final:
11300 C = new (Context) OMPFinalClause();
11301 break;
11302 case llvm::omp::OMPC_num_threads:
11303 C = new (Context) OMPNumThreadsClause();
11304 break;
11305 case llvm::omp::OMPC_safelen:
11306 C = new (Context) OMPSafelenClause();
11307 break;
11308 case llvm::omp::OMPC_simdlen:
11309 C = new (Context) OMPSimdlenClause();
11310 break;
11311 case llvm::omp::OMPC_sizes: {
11312 unsigned NumSizes = Record.readInt();
11313 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11314 break;
11315 }
11316 case llvm::omp::OMPC_permutation: {
11317 unsigned NumLoops = Record.readInt();
11318 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11319 break;
11320 }
11321 case llvm::omp::OMPC_full:
11322 C = OMPFullClause::CreateEmpty(C: Context);
11323 break;
11324 case llvm::omp::OMPC_partial:
11325 C = OMPPartialClause::CreateEmpty(C: Context);
11326 break;
11327 case llvm::omp::OMPC_looprange:
11328 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11329 break;
11330 case llvm::omp::OMPC_allocator:
11331 C = new (Context) OMPAllocatorClause();
11332 break;
11333 case llvm::omp::OMPC_collapse:
11334 C = new (Context) OMPCollapseClause();
11335 break;
11336 case llvm::omp::OMPC_default:
11337 C = new (Context) OMPDefaultClause();
11338 break;
11339 case llvm::omp::OMPC_proc_bind:
11340 C = new (Context) OMPProcBindClause();
11341 break;
11342 case llvm::omp::OMPC_schedule:
11343 C = new (Context) OMPScheduleClause();
11344 break;
11345 case llvm::omp::OMPC_ordered:
11346 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11347 break;
11348 case llvm::omp::OMPC_nowait:
11349 C = new (Context) OMPNowaitClause();
11350 break;
11351 case llvm::omp::OMPC_untied:
11352 C = new (Context) OMPUntiedClause();
11353 break;
11354 case llvm::omp::OMPC_mergeable:
11355 C = new (Context) OMPMergeableClause();
11356 break;
11357 case llvm::omp::OMPC_threadset:
11358 C = new (Context) OMPThreadsetClause();
11359 break;
11360 case llvm::omp::OMPC_transparent:
11361 C = new (Context) OMPTransparentClause();
11362 break;
11363 case llvm::omp::OMPC_read:
11364 C = new (Context) OMPReadClause();
11365 break;
11366 case llvm::omp::OMPC_write:
11367 C = new (Context) OMPWriteClause();
11368 break;
11369 case llvm::omp::OMPC_update:
11370 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11371 break;
11372 case llvm::omp::OMPC_capture:
11373 C = new (Context) OMPCaptureClause();
11374 break;
11375 case llvm::omp::OMPC_compare:
11376 C = new (Context) OMPCompareClause();
11377 break;
11378 case llvm::omp::OMPC_fail:
11379 C = new (Context) OMPFailClause();
11380 break;
11381 case llvm::omp::OMPC_seq_cst:
11382 C = new (Context) OMPSeqCstClause();
11383 break;
11384 case llvm::omp::OMPC_acq_rel:
11385 C = new (Context) OMPAcqRelClause();
11386 break;
11387 case llvm::omp::OMPC_absent: {
11388 unsigned NumKinds = Record.readInt();
11389 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11390 break;
11391 }
11392 case llvm::omp::OMPC_holds:
11393 C = new (Context) OMPHoldsClause();
11394 break;
11395 case llvm::omp::OMPC_contains: {
11396 unsigned NumKinds = Record.readInt();
11397 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11398 break;
11399 }
11400 case llvm::omp::OMPC_no_openmp:
11401 C = new (Context) OMPNoOpenMPClause();
11402 break;
11403 case llvm::omp::OMPC_no_openmp_routines:
11404 C = new (Context) OMPNoOpenMPRoutinesClause();
11405 break;
11406 case llvm::omp::OMPC_no_openmp_constructs:
11407 C = new (Context) OMPNoOpenMPConstructsClause();
11408 break;
11409 case llvm::omp::OMPC_no_parallelism:
11410 C = new (Context) OMPNoParallelismClause();
11411 break;
11412 case llvm::omp::OMPC_acquire:
11413 C = new (Context) OMPAcquireClause();
11414 break;
11415 case llvm::omp::OMPC_release:
11416 C = new (Context) OMPReleaseClause();
11417 break;
11418 case llvm::omp::OMPC_relaxed:
11419 C = new (Context) OMPRelaxedClause();
11420 break;
11421 case llvm::omp::OMPC_weak:
11422 C = new (Context) OMPWeakClause();
11423 break;
11424 case llvm::omp::OMPC_threads:
11425 C = new (Context) OMPThreadsClause();
11426 break;
11427 case llvm::omp::OMPC_simd:
11428 C = new (Context) OMPSIMDClause();
11429 break;
11430 case llvm::omp::OMPC_nogroup:
11431 C = new (Context) OMPNogroupClause();
11432 break;
11433 case llvm::omp::OMPC_unified_address:
11434 C = new (Context) OMPUnifiedAddressClause();
11435 break;
11436 case llvm::omp::OMPC_unified_shared_memory:
11437 C = new (Context) OMPUnifiedSharedMemoryClause();
11438 break;
11439 case llvm::omp::OMPC_reverse_offload:
11440 C = new (Context) OMPReverseOffloadClause();
11441 break;
11442 case llvm::omp::OMPC_dynamic_allocators:
11443 C = new (Context) OMPDynamicAllocatorsClause();
11444 break;
11445 case llvm::omp::OMPC_atomic_default_mem_order:
11446 C = new (Context) OMPAtomicDefaultMemOrderClause();
11447 break;
11448 case llvm::omp::OMPC_self_maps:
11449 C = new (Context) OMPSelfMapsClause();
11450 break;
11451 case llvm::omp::OMPC_at:
11452 C = new (Context) OMPAtClause();
11453 break;
11454 case llvm::omp::OMPC_severity:
11455 C = new (Context) OMPSeverityClause();
11456 break;
11457 case llvm::omp::OMPC_message:
11458 C = new (Context) OMPMessageClause();
11459 break;
11460 case llvm::omp::OMPC_private:
11461 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11462 break;
11463 case llvm::omp::OMPC_firstprivate:
11464 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11465 break;
11466 case llvm::omp::OMPC_lastprivate:
11467 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11468 break;
11469 case llvm::omp::OMPC_shared:
11470 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11471 break;
11472 case llvm::omp::OMPC_reduction: {
11473 unsigned N = Record.readInt();
11474 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11475 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11476 break;
11477 }
11478 case llvm::omp::OMPC_task_reduction:
11479 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11480 break;
11481 case llvm::omp::OMPC_in_reduction:
11482 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11483 break;
11484 case llvm::omp::OMPC_linear:
11485 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11486 break;
11487 case llvm::omp::OMPC_aligned:
11488 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11489 break;
11490 case llvm::omp::OMPC_copyin:
11491 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11492 break;
11493 case llvm::omp::OMPC_copyprivate:
11494 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11495 break;
11496 case llvm::omp::OMPC_flush:
11497 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11498 break;
11499 case llvm::omp::OMPC_depobj:
11500 C = OMPDepobjClause::CreateEmpty(C: Context);
11501 break;
11502 case llvm::omp::OMPC_depend: {
11503 unsigned NumVars = Record.readInt();
11504 unsigned NumLoops = Record.readInt();
11505 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11506 break;
11507 }
11508 case llvm::omp::OMPC_device:
11509 C = new (Context) OMPDeviceClause();
11510 break;
11511 case llvm::omp::OMPC_map: {
11512 OMPMappableExprListSizeTy Sizes;
11513 Sizes.NumVars = Record.readInt();
11514 Sizes.NumUniqueDeclarations = Record.readInt();
11515 Sizes.NumComponentLists = Record.readInt();
11516 Sizes.NumComponents = Record.readInt();
11517 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11518 break;
11519 }
11520 case llvm::omp::OMPC_num_teams:
11521 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11522 break;
11523 case llvm::omp::OMPC_thread_limit:
11524 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11525 break;
11526 case llvm::omp::OMPC_priority:
11527 C = new (Context) OMPPriorityClause();
11528 break;
11529 case llvm::omp::OMPC_grainsize:
11530 C = new (Context) OMPGrainsizeClause();
11531 break;
11532 case llvm::omp::OMPC_num_tasks:
11533 C = new (Context) OMPNumTasksClause();
11534 break;
11535 case llvm::omp::OMPC_hint:
11536 C = new (Context) OMPHintClause();
11537 break;
11538 case llvm::omp::OMPC_dist_schedule:
11539 C = new (Context) OMPDistScheduleClause();
11540 break;
11541 case llvm::omp::OMPC_defaultmap:
11542 C = new (Context) OMPDefaultmapClause();
11543 break;
11544 case llvm::omp::OMPC_to: {
11545 OMPMappableExprListSizeTy Sizes;
11546 Sizes.NumVars = Record.readInt();
11547 Sizes.NumUniqueDeclarations = Record.readInt();
11548 Sizes.NumComponentLists = Record.readInt();
11549 Sizes.NumComponents = Record.readInt();
11550 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11551 break;
11552 }
11553 case llvm::omp::OMPC_from: {
11554 OMPMappableExprListSizeTy Sizes;
11555 Sizes.NumVars = Record.readInt();
11556 Sizes.NumUniqueDeclarations = Record.readInt();
11557 Sizes.NumComponentLists = Record.readInt();
11558 Sizes.NumComponents = Record.readInt();
11559 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11560 break;
11561 }
11562 case llvm::omp::OMPC_use_device_ptr: {
11563 OMPMappableExprListSizeTy Sizes;
11564 Sizes.NumVars = Record.readInt();
11565 Sizes.NumUniqueDeclarations = Record.readInt();
11566 Sizes.NumComponentLists = Record.readInt();
11567 Sizes.NumComponents = Record.readInt();
11568 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11569 break;
11570 }
11571 case llvm::omp::OMPC_use_device_addr: {
11572 OMPMappableExprListSizeTy Sizes;
11573 Sizes.NumVars = Record.readInt();
11574 Sizes.NumUniqueDeclarations = Record.readInt();
11575 Sizes.NumComponentLists = Record.readInt();
11576 Sizes.NumComponents = Record.readInt();
11577 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11578 break;
11579 }
11580 case llvm::omp::OMPC_is_device_ptr: {
11581 OMPMappableExprListSizeTy Sizes;
11582 Sizes.NumVars = Record.readInt();
11583 Sizes.NumUniqueDeclarations = Record.readInt();
11584 Sizes.NumComponentLists = Record.readInt();
11585 Sizes.NumComponents = Record.readInt();
11586 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11587 break;
11588 }
11589 case llvm::omp::OMPC_has_device_addr: {
11590 OMPMappableExprListSizeTy Sizes;
11591 Sizes.NumVars = Record.readInt();
11592 Sizes.NumUniqueDeclarations = Record.readInt();
11593 Sizes.NumComponentLists = Record.readInt();
11594 Sizes.NumComponents = Record.readInt();
11595 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11596 break;
11597 }
11598 case llvm::omp::OMPC_allocate:
11599 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11600 break;
11601 case llvm::omp::OMPC_nontemporal:
11602 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11603 break;
11604 case llvm::omp::OMPC_inclusive:
11605 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11606 break;
11607 case llvm::omp::OMPC_exclusive:
11608 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11609 break;
11610 case llvm::omp::OMPC_order:
11611 C = new (Context) OMPOrderClause();
11612 break;
11613 case llvm::omp::OMPC_init:
11614 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
11615 break;
11616 case llvm::omp::OMPC_use:
11617 C = new (Context) OMPUseClause();
11618 break;
11619 case llvm::omp::OMPC_destroy:
11620 C = new (Context) OMPDestroyClause();
11621 break;
11622 case llvm::omp::OMPC_novariants:
11623 C = new (Context) OMPNovariantsClause();
11624 break;
11625 case llvm::omp::OMPC_nocontext:
11626 C = new (Context) OMPNocontextClause();
11627 break;
11628 case llvm::omp::OMPC_detach:
11629 C = new (Context) OMPDetachClause();
11630 break;
11631 case llvm::omp::OMPC_uses_allocators:
11632 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11633 break;
11634 case llvm::omp::OMPC_affinity:
11635 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11636 break;
11637 case llvm::omp::OMPC_filter:
11638 C = new (Context) OMPFilterClause();
11639 break;
11640 case llvm::omp::OMPC_bind:
11641 C = OMPBindClause::CreateEmpty(C: Context);
11642 break;
11643 case llvm::omp::OMPC_align:
11644 C = new (Context) OMPAlignClause();
11645 break;
11646 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11647 C = new (Context) OMPXDynCGroupMemClause();
11648 break;
11649 case llvm::omp::OMPC_dyn_groupprivate:
11650 C = new (Context) OMPDynGroupprivateClause();
11651 break;
11652 case llvm::omp::OMPC_doacross: {
11653 unsigned NumVars = Record.readInt();
11654 unsigned NumLoops = Record.readInt();
11655 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11656 break;
11657 }
11658 case llvm::omp::OMPC_ompx_attribute:
11659 C = new (Context) OMPXAttributeClause();
11660 break;
11661 case llvm::omp::OMPC_ompx_bare:
11662 C = new (Context) OMPXBareClause();
11663 break;
11664#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11665 case llvm::omp::Enum: \
11666 break;
11667#include "llvm/Frontend/OpenMP/OMPKinds.def"
11668 default:
11669 break;
11670 }
11671 assert(C && "Unknown OMPClause type");
11672
11673 Visit(S: C);
11674 C->setLocStart(Record.readSourceLocation());
11675 C->setLocEnd(Record.readSourceLocation());
11676
11677 return C;
11678}
11679
11680void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11681 C->setPreInitStmt(S: Record.readSubStmt(),
11682 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11683}
11684
11685void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11686 VisitOMPClauseWithPreInit(C);
11687 C->setPostUpdateExpr(Record.readSubExpr());
11688}
11689
11690void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11691 VisitOMPClauseWithPreInit(C);
11692 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11693 C->setNameModifierLoc(Record.readSourceLocation());
11694 C->setColonLoc(Record.readSourceLocation());
11695 C->setCondition(Record.readSubExpr());
11696 C->setLParenLoc(Record.readSourceLocation());
11697}
11698
11699void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11700 VisitOMPClauseWithPreInit(C);
11701 C->setCondition(Record.readSubExpr());
11702 C->setLParenLoc(Record.readSourceLocation());
11703}
11704
11705void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11706 VisitOMPClauseWithPreInit(C);
11707 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11708 C->setNumThreads(Record.readSubExpr());
11709 C->setModifierLoc(Record.readSourceLocation());
11710 C->setLParenLoc(Record.readSourceLocation());
11711}
11712
11713void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11714 C->setSafelen(Record.readSubExpr());
11715 C->setLParenLoc(Record.readSourceLocation());
11716}
11717
11718void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11719 C->setSimdlen(Record.readSubExpr());
11720 C->setLParenLoc(Record.readSourceLocation());
11721}
11722
11723void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11724 for (Expr *&E : C->getSizesRefs())
11725 E = Record.readSubExpr();
11726 C->setLParenLoc(Record.readSourceLocation());
11727}
11728
11729void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11730 for (Expr *&E : C->getArgsRefs())
11731 E = Record.readSubExpr();
11732 C->setLParenLoc(Record.readSourceLocation());
11733}
11734
11735void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11736
11737void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11738 C->setFactor(Record.readSubExpr());
11739 C->setLParenLoc(Record.readSourceLocation());
11740}
11741
11742void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11743 C->setFirst(Record.readSubExpr());
11744 C->setCount(Record.readSubExpr());
11745 C->setLParenLoc(Record.readSourceLocation());
11746 C->setFirstLoc(Record.readSourceLocation());
11747 C->setCountLoc(Record.readSourceLocation());
11748}
11749
11750void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11751 C->setAllocator(Record.readExpr());
11752 C->setLParenLoc(Record.readSourceLocation());
11753}
11754
11755void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11756 C->setNumForLoops(Record.readSubExpr());
11757 C->setLParenLoc(Record.readSourceLocation());
11758}
11759
11760void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11761 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11762 C->setLParenLoc(Record.readSourceLocation());
11763 C->setDefaultKindKwLoc(Record.readSourceLocation());
11764 C->setDefaultVariableCategory(
11765 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11766 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11767}
11768
11769// Read the parameter of threadset clause. This will have been saved when
11770// OMPClauseWriter is called.
11771void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11772 C->setLParenLoc(Record.readSourceLocation());
11773 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11774 C->setThreadsetKindLoc(ThreadsetKindLoc);
11775 OpenMPThreadsetKind TKind =
11776 static_cast<OpenMPThreadsetKind>(Record.readInt());
11777 C->setThreadsetKind(TKind);
11778}
11779
11780void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11781 C->setLParenLoc(Record.readSourceLocation());
11782 C->setImpexTypeKind(Record.readSubExpr());
11783}
11784
11785void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11786 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11787 C->setLParenLoc(Record.readSourceLocation());
11788 C->setProcBindKindKwLoc(Record.readSourceLocation());
11789}
11790
11791void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11792 VisitOMPClauseWithPreInit(C);
11793 C->setScheduleKind(
11794 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11795 C->setFirstScheduleModifier(
11796 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11797 C->setSecondScheduleModifier(
11798 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11799 C->setChunkSize(Record.readSubExpr());
11800 C->setLParenLoc(Record.readSourceLocation());
11801 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11802 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11803 C->setScheduleKindLoc(Record.readSourceLocation());
11804 C->setCommaLoc(Record.readSourceLocation());
11805}
11806
11807void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11808 C->setNumForLoops(Record.readSubExpr());
11809 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11810 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11811 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11812 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11813 C->setLParenLoc(Record.readSourceLocation());
11814}
11815
11816void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11817 C->setEventHandler(Record.readSubExpr());
11818 C->setLParenLoc(Record.readSourceLocation());
11819}
11820
11821void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
11822 C->setCondition(Record.readSubExpr());
11823 C->setLParenLoc(Record.readSourceLocation());
11824}
11825
11826void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11827
11828void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11829
11830void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11831
11832void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11833
11834void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11835 if (C->isExtended()) {
11836 C->setLParenLoc(Record.readSourceLocation());
11837 C->setArgumentLoc(Record.readSourceLocation());
11838 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11839 }
11840}
11841
11842void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11843
11844void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11845
11846// Read the parameter of fail clause. This will have been saved when
11847// OMPClauseWriter is called.
11848void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
11849 C->setLParenLoc(Record.readSourceLocation());
11850 SourceLocation FailParameterLoc = Record.readSourceLocation();
11851 C->setFailParameterLoc(FailParameterLoc);
11852 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
11853 C->setFailParameter(CKind);
11854}
11855
11856void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
11857 unsigned Count = C->getDirectiveKinds().size();
11858 C->setLParenLoc(Record.readSourceLocation());
11859 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11860 DKVec.reserve(N: Count);
11861 for (unsigned I = 0; I < Count; I++) {
11862 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11863 }
11864 C->setDirectiveKinds(DKVec);
11865}
11866
11867void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
11868 C->setExpr(Record.readExpr());
11869 C->setLParenLoc(Record.readSourceLocation());
11870}
11871
11872void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
11873 unsigned Count = C->getDirectiveKinds().size();
11874 C->setLParenLoc(Record.readSourceLocation());
11875 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11876 DKVec.reserve(N: Count);
11877 for (unsigned I = 0; I < Count; I++) {
11878 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11879 }
11880 C->setDirectiveKinds(DKVec);
11881}
11882
11883void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
11884
11885void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
11886 OMPNoOpenMPRoutinesClause *) {}
11887
11888void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
11889 OMPNoOpenMPConstructsClause *) {}
11890
11891void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
11892
11893void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11894
11895void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11896
11897void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11898
11899void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11900
11901void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11902
11903void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
11904
11905void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11906
11907void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11908
11909void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11910
11911void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
11912 unsigned NumVars = C->varlist_size();
11913 SmallVector<Expr *, 16> Vars;
11914 Vars.reserve(N: NumVars);
11915 for (unsigned I = 0; I != NumVars; ++I)
11916 Vars.push_back(Elt: Record.readSubExpr());
11917 C->setVarRefs(Vars);
11918 C->setIsTarget(Record.readBool());
11919 C->setIsTargetSync(Record.readBool());
11920 C->setLParenLoc(Record.readSourceLocation());
11921 C->setVarLoc(Record.readSourceLocation());
11922}
11923
11924void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
11925 C->setInteropVar(Record.readSubExpr());
11926 C->setLParenLoc(Record.readSourceLocation());
11927 C->setVarLoc(Record.readSourceLocation());
11928}
11929
11930void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
11931 C->setInteropVar(Record.readSubExpr());
11932 C->setLParenLoc(Record.readSourceLocation());
11933 C->setVarLoc(Record.readSourceLocation());
11934}
11935
11936void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
11937 VisitOMPClauseWithPreInit(C);
11938 C->setCondition(Record.readSubExpr());
11939 C->setLParenLoc(Record.readSourceLocation());
11940}
11941
11942void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
11943 VisitOMPClauseWithPreInit(C);
11944 C->setCondition(Record.readSubExpr());
11945 C->setLParenLoc(Record.readSourceLocation());
11946}
11947
11948void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11949
11950void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11951 OMPUnifiedSharedMemoryClause *) {}
11952
11953void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11954
11955void
11956OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11957}
11958
11959void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11960 OMPAtomicDefaultMemOrderClause *C) {
11961 C->setAtomicDefaultMemOrderKind(
11962 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11963 C->setLParenLoc(Record.readSourceLocation());
11964 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11965}
11966
11967void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
11968
11969void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
11970 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
11971 C->setLParenLoc(Record.readSourceLocation());
11972 C->setAtKindKwLoc(Record.readSourceLocation());
11973}
11974
11975void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
11976 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
11977 C->setLParenLoc(Record.readSourceLocation());
11978 C->setSeverityKindKwLoc(Record.readSourceLocation());
11979}
11980
11981void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
11982 VisitOMPClauseWithPreInit(C);
11983 C->setMessageString(Record.readSubExpr());
11984 C->setLParenLoc(Record.readSourceLocation());
11985}
11986
11987void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11988 C->setLParenLoc(Record.readSourceLocation());
11989 unsigned NumVars = C->varlist_size();
11990 SmallVector<Expr *, 16> Vars;
11991 Vars.reserve(N: NumVars);
11992 for (unsigned i = 0; i != NumVars; ++i)
11993 Vars.push_back(Elt: Record.readSubExpr());
11994 C->setVarRefs(Vars);
11995 Vars.clear();
11996 for (unsigned i = 0; i != NumVars; ++i)
11997 Vars.push_back(Elt: Record.readSubExpr());
11998 C->setPrivateCopies(Vars);
11999}
12000
12001void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12002 VisitOMPClauseWithPreInit(C);
12003 C->setLParenLoc(Record.readSourceLocation());
12004 unsigned NumVars = C->varlist_size();
12005 SmallVector<Expr *, 16> Vars;
12006 Vars.reserve(N: NumVars);
12007 for (unsigned i = 0; i != NumVars; ++i)
12008 Vars.push_back(Elt: Record.readSubExpr());
12009 C->setVarRefs(Vars);
12010 Vars.clear();
12011 for (unsigned i = 0; i != NumVars; ++i)
12012 Vars.push_back(Elt: Record.readSubExpr());
12013 C->setPrivateCopies(Vars);
12014 Vars.clear();
12015 for (unsigned i = 0; i != NumVars; ++i)
12016 Vars.push_back(Elt: Record.readSubExpr());
12017 C->setInits(Vars);
12018}
12019
12020void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12021 VisitOMPClauseWithPostUpdate(C);
12022 C->setLParenLoc(Record.readSourceLocation());
12023 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12024 C->setKindLoc(Record.readSourceLocation());
12025 C->setColonLoc(Record.readSourceLocation());
12026 unsigned NumVars = C->varlist_size();
12027 SmallVector<Expr *, 16> Vars;
12028 Vars.reserve(N: NumVars);
12029 for (unsigned i = 0; i != NumVars; ++i)
12030 Vars.push_back(Elt: Record.readSubExpr());
12031 C->setVarRefs(Vars);
12032 Vars.clear();
12033 for (unsigned i = 0; i != NumVars; ++i)
12034 Vars.push_back(Elt: Record.readSubExpr());
12035 C->setPrivateCopies(Vars);
12036 Vars.clear();
12037 for (unsigned i = 0; i != NumVars; ++i)
12038 Vars.push_back(Elt: Record.readSubExpr());
12039 C->setSourceExprs(Vars);
12040 Vars.clear();
12041 for (unsigned i = 0; i != NumVars; ++i)
12042 Vars.push_back(Elt: Record.readSubExpr());
12043 C->setDestinationExprs(Vars);
12044 Vars.clear();
12045 for (unsigned i = 0; i != NumVars; ++i)
12046 Vars.push_back(Elt: Record.readSubExpr());
12047 C->setAssignmentOps(Vars);
12048}
12049
12050void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12051 C->setLParenLoc(Record.readSourceLocation());
12052 unsigned NumVars = C->varlist_size();
12053 SmallVector<Expr *, 16> Vars;
12054 Vars.reserve(N: NumVars);
12055 for (unsigned i = 0; i != NumVars; ++i)
12056 Vars.push_back(Elt: Record.readSubExpr());
12057 C->setVarRefs(Vars);
12058}
12059
12060void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12061 VisitOMPClauseWithPostUpdate(C);
12062 C->setLParenLoc(Record.readSourceLocation());
12063 C->setModifierLoc(Record.readSourceLocation());
12064 C->setColonLoc(Record.readSourceLocation());
12065 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12066 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12067 C->setQualifierLoc(NNSL);
12068 C->setNameInfo(DNI);
12069
12070 unsigned NumVars = C->varlist_size();
12071 SmallVector<Expr *, 16> Vars;
12072 Vars.reserve(N: NumVars);
12073 for (unsigned i = 0; i != NumVars; ++i)
12074 Vars.push_back(Elt: Record.readSubExpr());
12075 C->setVarRefs(Vars);
12076 Vars.clear();
12077 for (unsigned i = 0; i != NumVars; ++i)
12078 Vars.push_back(Elt: Record.readSubExpr());
12079 C->setPrivates(Vars);
12080 Vars.clear();
12081 for (unsigned i = 0; i != NumVars; ++i)
12082 Vars.push_back(Elt: Record.readSubExpr());
12083 C->setLHSExprs(Vars);
12084 Vars.clear();
12085 for (unsigned i = 0; i != NumVars; ++i)
12086 Vars.push_back(Elt: Record.readSubExpr());
12087 C->setRHSExprs(Vars);
12088 Vars.clear();
12089 for (unsigned i = 0; i != NumVars; ++i)
12090 Vars.push_back(Elt: Record.readSubExpr());
12091 C->setReductionOps(Vars);
12092 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12093 Vars.clear();
12094 for (unsigned i = 0; i != NumVars; ++i)
12095 Vars.push_back(Elt: Record.readSubExpr());
12096 C->setInscanCopyOps(Vars);
12097 Vars.clear();
12098 for (unsigned i = 0; i != NumVars; ++i)
12099 Vars.push_back(Elt: Record.readSubExpr());
12100 C->setInscanCopyArrayTemps(Vars);
12101 Vars.clear();
12102 for (unsigned i = 0; i != NumVars; ++i)
12103 Vars.push_back(Elt: Record.readSubExpr());
12104 C->setInscanCopyArrayElems(Vars);
12105 }
12106 unsigned NumFlags = Record.readInt();
12107 SmallVector<bool, 16> Flags;
12108 Flags.reserve(N: NumFlags);
12109 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12110 Flags.push_back(Elt: Record.readInt());
12111 C->setPrivateVariableReductionFlags(Flags);
12112}
12113
12114void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12115 VisitOMPClauseWithPostUpdate(C);
12116 C->setLParenLoc(Record.readSourceLocation());
12117 C->setColonLoc(Record.readSourceLocation());
12118 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12119 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12120 C->setQualifierLoc(NNSL);
12121 C->setNameInfo(DNI);
12122
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 Vars.clear();
12130 for (unsigned I = 0; I != NumVars; ++I)
12131 Vars.push_back(Elt: Record.readSubExpr());
12132 C->setPrivates(Vars);
12133 Vars.clear();
12134 for (unsigned I = 0; I != NumVars; ++I)
12135 Vars.push_back(Elt: Record.readSubExpr());
12136 C->setLHSExprs(Vars);
12137 Vars.clear();
12138 for (unsigned I = 0; I != NumVars; ++I)
12139 Vars.push_back(Elt: Record.readSubExpr());
12140 C->setRHSExprs(Vars);
12141 Vars.clear();
12142 for (unsigned I = 0; I != NumVars; ++I)
12143 Vars.push_back(Elt: Record.readSubExpr());
12144 C->setReductionOps(Vars);
12145}
12146
12147void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12148 VisitOMPClauseWithPostUpdate(C);
12149 C->setLParenLoc(Record.readSourceLocation());
12150 C->setColonLoc(Record.readSourceLocation());
12151 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12152 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12153 C->setQualifierLoc(NNSL);
12154 C->setNameInfo(DNI);
12155
12156 unsigned NumVars = C->varlist_size();
12157 SmallVector<Expr *, 16> Vars;
12158 Vars.reserve(N: NumVars);
12159 for (unsigned I = 0; I != NumVars; ++I)
12160 Vars.push_back(Elt: Record.readSubExpr());
12161 C->setVarRefs(Vars);
12162 Vars.clear();
12163 for (unsigned I = 0; I != NumVars; ++I)
12164 Vars.push_back(Elt: Record.readSubExpr());
12165 C->setPrivates(Vars);
12166 Vars.clear();
12167 for (unsigned I = 0; I != NumVars; ++I)
12168 Vars.push_back(Elt: Record.readSubExpr());
12169 C->setLHSExprs(Vars);
12170 Vars.clear();
12171 for (unsigned I = 0; I != NumVars; ++I)
12172 Vars.push_back(Elt: Record.readSubExpr());
12173 C->setRHSExprs(Vars);
12174 Vars.clear();
12175 for (unsigned I = 0; I != NumVars; ++I)
12176 Vars.push_back(Elt: Record.readSubExpr());
12177 C->setReductionOps(Vars);
12178 Vars.clear();
12179 for (unsigned I = 0; I != NumVars; ++I)
12180 Vars.push_back(Elt: Record.readSubExpr());
12181 C->setTaskgroupDescriptors(Vars);
12182}
12183
12184void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12185 VisitOMPClauseWithPostUpdate(C);
12186 C->setLParenLoc(Record.readSourceLocation());
12187 C->setColonLoc(Record.readSourceLocation());
12188 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12189 C->setModifierLoc(Record.readSourceLocation());
12190 unsigned NumVars = C->varlist_size();
12191 SmallVector<Expr *, 16> Vars;
12192 Vars.reserve(N: NumVars);
12193 for (unsigned i = 0; i != NumVars; ++i)
12194 Vars.push_back(Elt: Record.readSubExpr());
12195 C->setVarRefs(Vars);
12196 Vars.clear();
12197 for (unsigned i = 0; i != NumVars; ++i)
12198 Vars.push_back(Elt: Record.readSubExpr());
12199 C->setPrivates(Vars);
12200 Vars.clear();
12201 for (unsigned i = 0; i != NumVars; ++i)
12202 Vars.push_back(Elt: Record.readSubExpr());
12203 C->setInits(Vars);
12204 Vars.clear();
12205 for (unsigned i = 0; i != NumVars; ++i)
12206 Vars.push_back(Elt: Record.readSubExpr());
12207 C->setUpdates(Vars);
12208 Vars.clear();
12209 for (unsigned i = 0; i != NumVars; ++i)
12210 Vars.push_back(Elt: Record.readSubExpr());
12211 C->setFinals(Vars);
12212 C->setStep(Record.readSubExpr());
12213 C->setCalcStep(Record.readSubExpr());
12214 Vars.clear();
12215 for (unsigned I = 0; I != NumVars + 1; ++I)
12216 Vars.push_back(Elt: Record.readSubExpr());
12217 C->setUsedExprs(Vars);
12218}
12219
12220void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12221 C->setLParenLoc(Record.readSourceLocation());
12222 C->setColonLoc(Record.readSourceLocation());
12223 unsigned NumVars = C->varlist_size();
12224 SmallVector<Expr *, 16> Vars;
12225 Vars.reserve(N: NumVars);
12226 for (unsigned i = 0; i != NumVars; ++i)
12227 Vars.push_back(Elt: Record.readSubExpr());
12228 C->setVarRefs(Vars);
12229 C->setAlignment(Record.readSubExpr());
12230}
12231
12232void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12233 C->setLParenLoc(Record.readSourceLocation());
12234 unsigned NumVars = C->varlist_size();
12235 SmallVector<Expr *, 16> Exprs;
12236 Exprs.reserve(N: NumVars);
12237 for (unsigned i = 0; i != NumVars; ++i)
12238 Exprs.push_back(Elt: Record.readSubExpr());
12239 C->setVarRefs(Exprs);
12240 Exprs.clear();
12241 for (unsigned i = 0; i != NumVars; ++i)
12242 Exprs.push_back(Elt: Record.readSubExpr());
12243 C->setSourceExprs(Exprs);
12244 Exprs.clear();
12245 for (unsigned i = 0; i != NumVars; ++i)
12246 Exprs.push_back(Elt: Record.readSubExpr());
12247 C->setDestinationExprs(Exprs);
12248 Exprs.clear();
12249 for (unsigned i = 0; i != NumVars; ++i)
12250 Exprs.push_back(Elt: Record.readSubExpr());
12251 C->setAssignmentOps(Exprs);
12252}
12253
12254void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12255 C->setLParenLoc(Record.readSourceLocation());
12256 unsigned NumVars = C->varlist_size();
12257 SmallVector<Expr *, 16> Exprs;
12258 Exprs.reserve(N: NumVars);
12259 for (unsigned i = 0; i != NumVars; ++i)
12260 Exprs.push_back(Elt: Record.readSubExpr());
12261 C->setVarRefs(Exprs);
12262 Exprs.clear();
12263 for (unsigned i = 0; i != NumVars; ++i)
12264 Exprs.push_back(Elt: Record.readSubExpr());
12265 C->setSourceExprs(Exprs);
12266 Exprs.clear();
12267 for (unsigned i = 0; i != NumVars; ++i)
12268 Exprs.push_back(Elt: Record.readSubExpr());
12269 C->setDestinationExprs(Exprs);
12270 Exprs.clear();
12271 for (unsigned i = 0; i != NumVars; ++i)
12272 Exprs.push_back(Elt: Record.readSubExpr());
12273 C->setAssignmentOps(Exprs);
12274}
12275
12276void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12277 C->setLParenLoc(Record.readSourceLocation());
12278 unsigned NumVars = C->varlist_size();
12279 SmallVector<Expr *, 16> Vars;
12280 Vars.reserve(N: NumVars);
12281 for (unsigned i = 0; i != NumVars; ++i)
12282 Vars.push_back(Elt: Record.readSubExpr());
12283 C->setVarRefs(Vars);
12284}
12285
12286void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12287 C->setDepobj(Record.readSubExpr());
12288 C->setLParenLoc(Record.readSourceLocation());
12289}
12290
12291void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12292 C->setLParenLoc(Record.readSourceLocation());
12293 C->setModifier(Record.readSubExpr());
12294 C->setDependencyKind(
12295 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12296 C->setDependencyLoc(Record.readSourceLocation());
12297 C->setColonLoc(Record.readSourceLocation());
12298 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12299 unsigned NumVars = C->varlist_size();
12300 SmallVector<Expr *, 16> Vars;
12301 Vars.reserve(N: NumVars);
12302 for (unsigned I = 0; I != NumVars; ++I)
12303 Vars.push_back(Elt: Record.readSubExpr());
12304 C->setVarRefs(Vars);
12305 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12306 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12307}
12308
12309void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12310 VisitOMPClauseWithPreInit(C);
12311 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12312 C->setDevice(Record.readSubExpr());
12313 C->setModifierLoc(Record.readSourceLocation());
12314 C->setLParenLoc(Record.readSourceLocation());
12315}
12316
12317void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12318 C->setLParenLoc(Record.readSourceLocation());
12319 bool HasIteratorModifier = false;
12320 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12321 C->setMapTypeModifier(
12322 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12323 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12324 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12325 HasIteratorModifier = true;
12326 }
12327 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12328 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12329 C->setMapType(
12330 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12331 C->setMapLoc(Record.readSourceLocation());
12332 C->setColonLoc(Record.readSourceLocation());
12333 auto NumVars = C->varlist_size();
12334 auto UniqueDecls = C->getUniqueDeclarationsNum();
12335 auto TotalLists = C->getTotalComponentListNum();
12336 auto TotalComponents = C->getTotalComponentsNum();
12337
12338 SmallVector<Expr *, 16> Vars;
12339 Vars.reserve(N: NumVars);
12340 for (unsigned i = 0; i != NumVars; ++i)
12341 Vars.push_back(Elt: Record.readExpr());
12342 C->setVarRefs(Vars);
12343
12344 SmallVector<Expr *, 16> UDMappers;
12345 UDMappers.reserve(N: NumVars);
12346 for (unsigned I = 0; I < NumVars; ++I)
12347 UDMappers.push_back(Elt: Record.readExpr());
12348 C->setUDMapperRefs(UDMappers);
12349
12350 if (HasIteratorModifier)
12351 C->setIteratorModifier(Record.readExpr());
12352
12353 SmallVector<ValueDecl *, 16> Decls;
12354 Decls.reserve(N: UniqueDecls);
12355 for (unsigned i = 0; i < UniqueDecls; ++i)
12356 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12357 C->setUniqueDecls(Decls);
12358
12359 SmallVector<unsigned, 16> ListsPerDecl;
12360 ListsPerDecl.reserve(N: UniqueDecls);
12361 for (unsigned i = 0; i < UniqueDecls; ++i)
12362 ListsPerDecl.push_back(Elt: Record.readInt());
12363 C->setDeclNumLists(ListsPerDecl);
12364
12365 SmallVector<unsigned, 32> ListSizes;
12366 ListSizes.reserve(N: TotalLists);
12367 for (unsigned i = 0; i < TotalLists; ++i)
12368 ListSizes.push_back(Elt: Record.readInt());
12369 C->setComponentListSizes(ListSizes);
12370
12371 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12372 Components.reserve(N: TotalComponents);
12373 for (unsigned i = 0; i < TotalComponents; ++i) {
12374 Expr *AssociatedExprPr = Record.readExpr();
12375 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12376 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12377 /*IsNonContiguous=*/Args: false);
12378 }
12379 C->setComponents(Components, CLSs: ListSizes);
12380}
12381
12382void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12383 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12384 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12385 C->setLParenLoc(Record.readSourceLocation());
12386 C->setColonLoc(Record.readSourceLocation());
12387 C->setAllocator(Record.readSubExpr());
12388 C->setAlignment(Record.readSubExpr());
12389 unsigned NumVars = C->varlist_size();
12390 SmallVector<Expr *, 16> Vars;
12391 Vars.reserve(N: NumVars);
12392 for (unsigned i = 0; i != NumVars; ++i)
12393 Vars.push_back(Elt: Record.readSubExpr());
12394 C->setVarRefs(Vars);
12395}
12396
12397void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12398 VisitOMPClauseWithPreInit(C);
12399 C->setLParenLoc(Record.readSourceLocation());
12400 unsigned NumVars = C->varlist_size();
12401 SmallVector<Expr *, 16> Vars;
12402 Vars.reserve(N: NumVars);
12403 for (unsigned I = 0; I != NumVars; ++I)
12404 Vars.push_back(Elt: Record.readSubExpr());
12405 C->setVarRefs(Vars);
12406}
12407
12408void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12409 VisitOMPClauseWithPreInit(C);
12410 C->setLParenLoc(Record.readSourceLocation());
12411 unsigned NumVars = C->varlist_size();
12412 SmallVector<Expr *, 16> Vars;
12413 Vars.reserve(N: NumVars);
12414 for (unsigned I = 0; I != NumVars; ++I)
12415 Vars.push_back(Elt: Record.readSubExpr());
12416 C->setVarRefs(Vars);
12417}
12418
12419void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12420 VisitOMPClauseWithPreInit(C);
12421 C->setPriority(Record.readSubExpr());
12422 C->setLParenLoc(Record.readSourceLocation());
12423}
12424
12425void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12426 VisitOMPClauseWithPreInit(C);
12427 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12428 C->setGrainsize(Record.readSubExpr());
12429 C->setModifierLoc(Record.readSourceLocation());
12430 C->setLParenLoc(Record.readSourceLocation());
12431}
12432
12433void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12434 VisitOMPClauseWithPreInit(C);
12435 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12436 C->setNumTasks(Record.readSubExpr());
12437 C->setModifierLoc(Record.readSourceLocation());
12438 C->setLParenLoc(Record.readSourceLocation());
12439}
12440
12441void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12442 C->setHint(Record.readSubExpr());
12443 C->setLParenLoc(Record.readSourceLocation());
12444}
12445
12446void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12447 VisitOMPClauseWithPreInit(C);
12448 C->setDistScheduleKind(
12449 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12450 C->setChunkSize(Record.readSubExpr());
12451 C->setLParenLoc(Record.readSourceLocation());
12452 C->setDistScheduleKindLoc(Record.readSourceLocation());
12453 C->setCommaLoc(Record.readSourceLocation());
12454}
12455
12456void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12457 C->setDefaultmapKind(
12458 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12459 C->setDefaultmapModifier(
12460 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12461 C->setLParenLoc(Record.readSourceLocation());
12462 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12463 C->setDefaultmapKindLoc(Record.readSourceLocation());
12464}
12465
12466void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12467 C->setLParenLoc(Record.readSourceLocation());
12468 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12469 C->setMotionModifier(
12470 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12471 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12472 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12473 C->setIteratorModifier(Record.readExpr());
12474 }
12475 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12476 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12477 C->setColonLoc(Record.readSourceLocation());
12478 auto NumVars = C->varlist_size();
12479 auto UniqueDecls = C->getUniqueDeclarationsNum();
12480 auto TotalLists = C->getTotalComponentListNum();
12481 auto TotalComponents = C->getTotalComponentsNum();
12482
12483 SmallVector<Expr *, 16> Vars;
12484 Vars.reserve(N: NumVars);
12485 for (unsigned i = 0; i != NumVars; ++i)
12486 Vars.push_back(Elt: Record.readSubExpr());
12487 C->setVarRefs(Vars);
12488
12489 SmallVector<Expr *, 16> UDMappers;
12490 UDMappers.reserve(N: NumVars);
12491 for (unsigned I = 0; I < NumVars; ++I)
12492 UDMappers.push_back(Elt: Record.readSubExpr());
12493 C->setUDMapperRefs(UDMappers);
12494
12495 SmallVector<ValueDecl *, 16> Decls;
12496 Decls.reserve(N: UniqueDecls);
12497 for (unsigned i = 0; i < UniqueDecls; ++i)
12498 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12499 C->setUniqueDecls(Decls);
12500
12501 SmallVector<unsigned, 16> ListsPerDecl;
12502 ListsPerDecl.reserve(N: UniqueDecls);
12503 for (unsigned i = 0; i < UniqueDecls; ++i)
12504 ListsPerDecl.push_back(Elt: Record.readInt());
12505 C->setDeclNumLists(ListsPerDecl);
12506
12507 SmallVector<unsigned, 32> ListSizes;
12508 ListSizes.reserve(N: TotalLists);
12509 for (unsigned i = 0; i < TotalLists; ++i)
12510 ListSizes.push_back(Elt: Record.readInt());
12511 C->setComponentListSizes(ListSizes);
12512
12513 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12514 Components.reserve(N: TotalComponents);
12515 for (unsigned i = 0; i < TotalComponents; ++i) {
12516 Expr *AssociatedExprPr = Record.readSubExpr();
12517 bool IsNonContiguous = Record.readBool();
12518 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12519 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12520 }
12521 C->setComponents(Components, CLSs: ListSizes);
12522}
12523
12524void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12525 C->setLParenLoc(Record.readSourceLocation());
12526 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12527 C->setMotionModifier(
12528 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12529 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12530 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12531 C->setIteratorModifier(Record.readExpr());
12532 }
12533 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12534 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12535 C->setColonLoc(Record.readSourceLocation());
12536 auto NumVars = C->varlist_size();
12537 auto UniqueDecls = C->getUniqueDeclarationsNum();
12538 auto TotalLists = C->getTotalComponentListNum();
12539 auto TotalComponents = C->getTotalComponentsNum();
12540
12541 SmallVector<Expr *, 16> Vars;
12542 Vars.reserve(N: NumVars);
12543 for (unsigned i = 0; i != NumVars; ++i)
12544 Vars.push_back(Elt: Record.readSubExpr());
12545 C->setVarRefs(Vars);
12546
12547 SmallVector<Expr *, 16> UDMappers;
12548 UDMappers.reserve(N: NumVars);
12549 for (unsigned I = 0; I < NumVars; ++I)
12550 UDMappers.push_back(Elt: Record.readSubExpr());
12551 C->setUDMapperRefs(UDMappers);
12552
12553 SmallVector<ValueDecl *, 16> Decls;
12554 Decls.reserve(N: UniqueDecls);
12555 for (unsigned i = 0; i < UniqueDecls; ++i)
12556 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12557 C->setUniqueDecls(Decls);
12558
12559 SmallVector<unsigned, 16> ListsPerDecl;
12560 ListsPerDecl.reserve(N: UniqueDecls);
12561 for (unsigned i = 0; i < UniqueDecls; ++i)
12562 ListsPerDecl.push_back(Elt: Record.readInt());
12563 C->setDeclNumLists(ListsPerDecl);
12564
12565 SmallVector<unsigned, 32> ListSizes;
12566 ListSizes.reserve(N: TotalLists);
12567 for (unsigned i = 0; i < TotalLists; ++i)
12568 ListSizes.push_back(Elt: Record.readInt());
12569 C->setComponentListSizes(ListSizes);
12570
12571 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12572 Components.reserve(N: TotalComponents);
12573 for (unsigned i = 0; i < TotalComponents; ++i) {
12574 Expr *AssociatedExprPr = Record.readSubExpr();
12575 bool IsNonContiguous = Record.readBool();
12576 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12577 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12578 }
12579 C->setComponents(Components, CLSs: ListSizes);
12580}
12581
12582void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12583 C->setLParenLoc(Record.readSourceLocation());
12584 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12585 C->setFallbackModifierLoc(Record.readSourceLocation());
12586 auto NumVars = C->varlist_size();
12587 auto UniqueDecls = C->getUniqueDeclarationsNum();
12588 auto TotalLists = C->getTotalComponentListNum();
12589 auto TotalComponents = C->getTotalComponentsNum();
12590
12591 SmallVector<Expr *, 16> Vars;
12592 Vars.reserve(N: NumVars);
12593 for (unsigned i = 0; i != NumVars; ++i)
12594 Vars.push_back(Elt: Record.readSubExpr());
12595 C->setVarRefs(Vars);
12596 Vars.clear();
12597 for (unsigned i = 0; i != NumVars; ++i)
12598 Vars.push_back(Elt: Record.readSubExpr());
12599 C->setPrivateCopies(Vars);
12600 Vars.clear();
12601 for (unsigned i = 0; i != NumVars; ++i)
12602 Vars.push_back(Elt: Record.readSubExpr());
12603 C->setInits(Vars);
12604
12605 SmallVector<ValueDecl *, 16> Decls;
12606 Decls.reserve(N: UniqueDecls);
12607 for (unsigned i = 0; i < UniqueDecls; ++i)
12608 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12609 C->setUniqueDecls(Decls);
12610
12611 SmallVector<unsigned, 16> ListsPerDecl;
12612 ListsPerDecl.reserve(N: UniqueDecls);
12613 for (unsigned i = 0; i < UniqueDecls; ++i)
12614 ListsPerDecl.push_back(Elt: Record.readInt());
12615 C->setDeclNumLists(ListsPerDecl);
12616
12617 SmallVector<unsigned, 32> ListSizes;
12618 ListSizes.reserve(N: TotalLists);
12619 for (unsigned i = 0; i < TotalLists; ++i)
12620 ListSizes.push_back(Elt: Record.readInt());
12621 C->setComponentListSizes(ListSizes);
12622
12623 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12624 Components.reserve(N: TotalComponents);
12625 for (unsigned i = 0; i < TotalComponents; ++i) {
12626 auto *AssociatedExprPr = Record.readSubExpr();
12627 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12628 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12629 /*IsNonContiguous=*/Args: false);
12630 }
12631 C->setComponents(Components, CLSs: ListSizes);
12632}
12633
12634void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12635 C->setLParenLoc(Record.readSourceLocation());
12636 auto NumVars = C->varlist_size();
12637 auto UniqueDecls = C->getUniqueDeclarationsNum();
12638 auto TotalLists = C->getTotalComponentListNum();
12639 auto TotalComponents = C->getTotalComponentsNum();
12640
12641 SmallVector<Expr *, 16> Vars;
12642 Vars.reserve(N: NumVars);
12643 for (unsigned i = 0; i != NumVars; ++i)
12644 Vars.push_back(Elt: Record.readSubExpr());
12645 C->setVarRefs(Vars);
12646
12647 SmallVector<ValueDecl *, 16> Decls;
12648 Decls.reserve(N: UniqueDecls);
12649 for (unsigned i = 0; i < UniqueDecls; ++i)
12650 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12651 C->setUniqueDecls(Decls);
12652
12653 SmallVector<unsigned, 16> ListsPerDecl;
12654 ListsPerDecl.reserve(N: UniqueDecls);
12655 for (unsigned i = 0; i < UniqueDecls; ++i)
12656 ListsPerDecl.push_back(Elt: Record.readInt());
12657 C->setDeclNumLists(ListsPerDecl);
12658
12659 SmallVector<unsigned, 32> ListSizes;
12660 ListSizes.reserve(N: TotalLists);
12661 for (unsigned i = 0; i < TotalLists; ++i)
12662 ListSizes.push_back(Elt: Record.readInt());
12663 C->setComponentListSizes(ListSizes);
12664
12665 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12666 Components.reserve(N: TotalComponents);
12667 for (unsigned i = 0; i < TotalComponents; ++i) {
12668 Expr *AssociatedExpr = Record.readSubExpr();
12669 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12670 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12671 /*IsNonContiguous*/ Args: false);
12672 }
12673 C->setComponents(Components, CLSs: ListSizes);
12674}
12675
12676void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12677 C->setLParenLoc(Record.readSourceLocation());
12678 auto NumVars = C->varlist_size();
12679 auto UniqueDecls = C->getUniqueDeclarationsNum();
12680 auto TotalLists = C->getTotalComponentListNum();
12681 auto TotalComponents = C->getTotalComponentsNum();
12682
12683 SmallVector<Expr *, 16> Vars;
12684 Vars.reserve(N: NumVars);
12685 for (unsigned i = 0; i != NumVars; ++i)
12686 Vars.push_back(Elt: Record.readSubExpr());
12687 C->setVarRefs(Vars);
12688 Vars.clear();
12689
12690 SmallVector<ValueDecl *, 16> Decls;
12691 Decls.reserve(N: UniqueDecls);
12692 for (unsigned i = 0; i < UniqueDecls; ++i)
12693 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12694 C->setUniqueDecls(Decls);
12695
12696 SmallVector<unsigned, 16> ListsPerDecl;
12697 ListsPerDecl.reserve(N: UniqueDecls);
12698 for (unsigned i = 0; i < UniqueDecls; ++i)
12699 ListsPerDecl.push_back(Elt: Record.readInt());
12700 C->setDeclNumLists(ListsPerDecl);
12701
12702 SmallVector<unsigned, 32> ListSizes;
12703 ListSizes.reserve(N: TotalLists);
12704 for (unsigned i = 0; i < TotalLists; ++i)
12705 ListSizes.push_back(Elt: Record.readInt());
12706 C->setComponentListSizes(ListSizes);
12707
12708 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12709 Components.reserve(N: TotalComponents);
12710 for (unsigned i = 0; i < TotalComponents; ++i) {
12711 Expr *AssociatedExpr = Record.readSubExpr();
12712 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12713 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12714 /*IsNonContiguous=*/Args: false);
12715 }
12716 C->setComponents(Components, CLSs: ListSizes);
12717}
12718
12719void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12720 C->setLParenLoc(Record.readSourceLocation());
12721 auto NumVars = C->varlist_size();
12722 auto UniqueDecls = C->getUniqueDeclarationsNum();
12723 auto TotalLists = C->getTotalComponentListNum();
12724 auto TotalComponents = C->getTotalComponentsNum();
12725
12726 SmallVector<Expr *, 16> Vars;
12727 Vars.reserve(N: NumVars);
12728 for (unsigned I = 0; I != NumVars; ++I)
12729 Vars.push_back(Elt: Record.readSubExpr());
12730 C->setVarRefs(Vars);
12731 Vars.clear();
12732
12733 SmallVector<ValueDecl *, 16> Decls;
12734 Decls.reserve(N: UniqueDecls);
12735 for (unsigned I = 0; I < UniqueDecls; ++I)
12736 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12737 C->setUniqueDecls(Decls);
12738
12739 SmallVector<unsigned, 16> ListsPerDecl;
12740 ListsPerDecl.reserve(N: UniqueDecls);
12741 for (unsigned I = 0; I < UniqueDecls; ++I)
12742 ListsPerDecl.push_back(Elt: Record.readInt());
12743 C->setDeclNumLists(ListsPerDecl);
12744
12745 SmallVector<unsigned, 32> ListSizes;
12746 ListSizes.reserve(N: TotalLists);
12747 for (unsigned i = 0; i < TotalLists; ++i)
12748 ListSizes.push_back(Elt: Record.readInt());
12749 C->setComponentListSizes(ListSizes);
12750
12751 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12752 Components.reserve(N: TotalComponents);
12753 for (unsigned I = 0; I < TotalComponents; ++I) {
12754 Expr *AssociatedExpr = Record.readSubExpr();
12755 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12756 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12757 /*IsNonContiguous=*/Args: false);
12758 }
12759 C->setComponents(Components, CLSs: ListSizes);
12760}
12761
12762void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12763 C->setLParenLoc(Record.readSourceLocation());
12764 unsigned NumVars = C->varlist_size();
12765 SmallVector<Expr *, 16> Vars;
12766 Vars.reserve(N: NumVars);
12767 for (unsigned i = 0; i != NumVars; ++i)
12768 Vars.push_back(Elt: Record.readSubExpr());
12769 C->setVarRefs(Vars);
12770 Vars.clear();
12771 Vars.reserve(N: NumVars);
12772 for (unsigned i = 0; i != NumVars; ++i)
12773 Vars.push_back(Elt: Record.readSubExpr());
12774 C->setPrivateRefs(Vars);
12775}
12776
12777void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12778 C->setLParenLoc(Record.readSourceLocation());
12779 unsigned NumVars = C->varlist_size();
12780 SmallVector<Expr *, 16> Vars;
12781 Vars.reserve(N: NumVars);
12782 for (unsigned i = 0; i != NumVars; ++i)
12783 Vars.push_back(Elt: Record.readSubExpr());
12784 C->setVarRefs(Vars);
12785}
12786
12787void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12788 C->setLParenLoc(Record.readSourceLocation());
12789 unsigned NumVars = C->varlist_size();
12790 SmallVector<Expr *, 16> Vars;
12791 Vars.reserve(N: NumVars);
12792 for (unsigned i = 0; i != NumVars; ++i)
12793 Vars.push_back(Elt: Record.readSubExpr());
12794 C->setVarRefs(Vars);
12795}
12796
12797void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12798 C->setLParenLoc(Record.readSourceLocation());
12799 unsigned NumOfAllocators = C->getNumberOfAllocators();
12800 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12801 Data.reserve(N: NumOfAllocators);
12802 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12803 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12804 D.Allocator = Record.readSubExpr();
12805 D.AllocatorTraits = Record.readSubExpr();
12806 D.LParenLoc = Record.readSourceLocation();
12807 D.RParenLoc = Record.readSourceLocation();
12808 }
12809 C->setAllocatorsData(Data);
12810}
12811
12812void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12813 C->setLParenLoc(Record.readSourceLocation());
12814 C->setModifier(Record.readSubExpr());
12815 C->setColonLoc(Record.readSourceLocation());
12816 unsigned NumOfLocators = C->varlist_size();
12817 SmallVector<Expr *, 4> Locators;
12818 Locators.reserve(N: NumOfLocators);
12819 for (unsigned I = 0; I != NumOfLocators; ++I)
12820 Locators.push_back(Elt: Record.readSubExpr());
12821 C->setVarRefs(Locators);
12822}
12823
12824void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12825 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12826 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
12827 C->setLParenLoc(Record.readSourceLocation());
12828 C->setKindKwLoc(Record.readSourceLocation());
12829 C->setModifierKwLoc(Record.readSourceLocation());
12830}
12831
12832void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12833 VisitOMPClauseWithPreInit(C);
12834 C->setThreadID(Record.readSubExpr());
12835 C->setLParenLoc(Record.readSourceLocation());
12836}
12837
12838void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12839 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12840 C->setLParenLoc(Record.readSourceLocation());
12841 C->setBindKindLoc(Record.readSourceLocation());
12842}
12843
12844void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12845 C->setAlignment(Record.readExpr());
12846 C->setLParenLoc(Record.readSourceLocation());
12847}
12848
12849void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
12850 VisitOMPClauseWithPreInit(C);
12851 C->setSize(Record.readSubExpr());
12852 C->setLParenLoc(Record.readSourceLocation());
12853}
12854
12855void OMPClauseReader::VisitOMPDynGroupprivateClause(
12856 OMPDynGroupprivateClause *C) {
12857 VisitOMPClauseWithPreInit(C);
12858 C->setDynGroupprivateModifier(
12859 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
12860 C->setDynGroupprivateFallbackModifier(
12861 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
12862 C->setSize(Record.readSubExpr());
12863 C->setLParenLoc(Record.readSourceLocation());
12864 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
12865 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
12866}
12867
12868void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
12869 C->setLParenLoc(Record.readSourceLocation());
12870 C->setDependenceType(
12871 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
12872 C->setDependenceLoc(Record.readSourceLocation());
12873 C->setColonLoc(Record.readSourceLocation());
12874 unsigned NumVars = C->varlist_size();
12875 SmallVector<Expr *, 16> Vars;
12876 Vars.reserve(N: NumVars);
12877 for (unsigned I = 0; I != NumVars; ++I)
12878 Vars.push_back(Elt: Record.readSubExpr());
12879 C->setVarRefs(Vars);
12880 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12881 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12882}
12883
12884void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
12885 AttrVec Attrs;
12886 Record.readAttributes(Attrs);
12887 C->setAttrs(Attrs);
12888 C->setLocStart(Record.readSourceLocation());
12889 C->setLParenLoc(Record.readSourceLocation());
12890 C->setLocEnd(Record.readSourceLocation());
12891}
12892
12893void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
12894
12895OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12896 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12897 TI.Sets.resize(N: readUInt32());
12898 for (auto &Set : TI.Sets) {
12899 Set.Kind = readEnum<llvm::omp::TraitSet>();
12900 Set.Selectors.resize(N: readUInt32());
12901 for (auto &Selector : Set.Selectors) {
12902 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12903 Selector.ScoreOrCondition = nullptr;
12904 if (readBool())
12905 Selector.ScoreOrCondition = readExprRef();
12906 Selector.Properties.resize(N: readUInt32());
12907 for (auto &Property : Selector.Properties)
12908 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12909 }
12910 }
12911 return &TI;
12912}
12913
12914void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12915 if (!Data)
12916 return;
12917 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12918 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12919 skipInts(N: 3);
12920 }
12921 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12922 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12923 Clauses[I] = readOMPClause();
12924 Data->setClauses(Clauses);
12925 if (Data->hasAssociatedStmt())
12926 Data->setAssociatedStmt(readStmt());
12927 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12928 Data->getChildren()[I] = readStmt();
12929}
12930
12931SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
12932 unsigned NumVars = readInt();
12933 llvm::SmallVector<Expr *> VarList;
12934 for (unsigned I = 0; I < NumVars; ++I)
12935 VarList.push_back(Elt: readExpr());
12936 return VarList;
12937}
12938
12939SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
12940 unsigned NumExprs = readInt();
12941 llvm::SmallVector<Expr *> ExprList;
12942 for (unsigned I = 0; I < NumExprs; ++I)
12943 ExprList.push_back(Elt: readSubExpr());
12944 return ExprList;
12945}
12946
12947OpenACCClause *ASTRecordReader::readOpenACCClause() {
12948 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
12949 SourceLocation BeginLoc = readSourceLocation();
12950 SourceLocation EndLoc = readSourceLocation();
12951
12952 switch (ClauseKind) {
12953 case OpenACCClauseKind::Default: {
12954 SourceLocation LParenLoc = readSourceLocation();
12955 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
12956 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
12957 EndLoc);
12958 }
12959 case OpenACCClauseKind::If: {
12960 SourceLocation LParenLoc = readSourceLocation();
12961 Expr *CondExpr = readSubExpr();
12962 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
12963 EndLoc);
12964 }
12965 case OpenACCClauseKind::Self: {
12966 SourceLocation LParenLoc = readSourceLocation();
12967 bool isConditionExprClause = readBool();
12968 if (isConditionExprClause) {
12969 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
12970 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
12971 ConditionExpr: CondExpr, EndLoc);
12972 }
12973 unsigned NumVars = readInt();
12974 llvm::SmallVector<Expr *> VarList;
12975 for (unsigned I = 0; I < NumVars; ++I)
12976 VarList.push_back(Elt: readSubExpr());
12977 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
12978 EndLoc);
12979 }
12980 case OpenACCClauseKind::NumGangs: {
12981 SourceLocation LParenLoc = readSourceLocation();
12982 unsigned NumClauses = readInt();
12983 llvm::SmallVector<Expr *> IntExprs;
12984 for (unsigned I = 0; I < NumClauses; ++I)
12985 IntExprs.push_back(Elt: readSubExpr());
12986 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
12987 IntExprs, EndLoc);
12988 }
12989 case OpenACCClauseKind::NumWorkers: {
12990 SourceLocation LParenLoc = readSourceLocation();
12991 Expr *IntExpr = readSubExpr();
12992 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
12993 IntExpr, EndLoc);
12994 }
12995 case OpenACCClauseKind::DeviceNum: {
12996 SourceLocation LParenLoc = readSourceLocation();
12997 Expr *IntExpr = readSubExpr();
12998 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
12999 IntExpr, EndLoc);
13000 }
13001 case OpenACCClauseKind::DefaultAsync: {
13002 SourceLocation LParenLoc = readSourceLocation();
13003 Expr *IntExpr = readSubExpr();
13004 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13005 IntExpr, EndLoc);
13006 }
13007 case OpenACCClauseKind::VectorLength: {
13008 SourceLocation LParenLoc = readSourceLocation();
13009 Expr *IntExpr = readSubExpr();
13010 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13011 IntExpr, EndLoc);
13012 }
13013 case OpenACCClauseKind::Private: {
13014 SourceLocation LParenLoc = readSourceLocation();
13015 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13016
13017 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13018 for (unsigned I = 0; I < VarList.size(); ++I) {
13019 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13020 VarDecl *Alloca = readDeclAs<VarDecl>();
13021 RecipeList.push_back(Elt: {Alloca});
13022 }
13023
13024 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13025 VarList, InitRecipes: RecipeList, EndLoc);
13026 }
13027 case OpenACCClauseKind::Host: {
13028 SourceLocation LParenLoc = readSourceLocation();
13029 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13030 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13031 EndLoc);
13032 }
13033 case OpenACCClauseKind::Device: {
13034 SourceLocation LParenLoc = readSourceLocation();
13035 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13036 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13037 VarList, EndLoc);
13038 }
13039 case OpenACCClauseKind::FirstPrivate: {
13040 SourceLocation LParenLoc = readSourceLocation();
13041 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13042 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13043 for (unsigned I = 0; I < VarList.size(); ++I) {
13044 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13045 VarDecl *Recipe = readDeclAs<VarDecl>();
13046 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13047 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13048 }
13049
13050 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13051 VarList, InitRecipes: RecipeList, EndLoc);
13052 }
13053 case OpenACCClauseKind::Attach: {
13054 SourceLocation LParenLoc = readSourceLocation();
13055 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13056 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13057 VarList, EndLoc);
13058 }
13059 case OpenACCClauseKind::Detach: {
13060 SourceLocation LParenLoc = readSourceLocation();
13061 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13062 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13063 VarList, EndLoc);
13064 }
13065 case OpenACCClauseKind::Delete: {
13066 SourceLocation LParenLoc = readSourceLocation();
13067 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13068 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13069 VarList, EndLoc);
13070 }
13071 case OpenACCClauseKind::UseDevice: {
13072 SourceLocation LParenLoc = readSourceLocation();
13073 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13074 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13075 VarList, EndLoc);
13076 }
13077 case OpenACCClauseKind::DevicePtr: {
13078 SourceLocation LParenLoc = readSourceLocation();
13079 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13080 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13081 VarList, EndLoc);
13082 }
13083 case OpenACCClauseKind::NoCreate: {
13084 SourceLocation LParenLoc = readSourceLocation();
13085 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13086 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13087 VarList, EndLoc);
13088 }
13089 case OpenACCClauseKind::Present: {
13090 SourceLocation LParenLoc = readSourceLocation();
13091 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13092 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13093 VarList, EndLoc);
13094 }
13095 case OpenACCClauseKind::PCopy:
13096 case OpenACCClauseKind::PresentOrCopy:
13097 case OpenACCClauseKind::Copy: {
13098 SourceLocation LParenLoc = readSourceLocation();
13099 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13100 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13101 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13102 LParenLoc, Mods: ModList, VarList, EndLoc);
13103 }
13104 case OpenACCClauseKind::CopyIn:
13105 case OpenACCClauseKind::PCopyIn:
13106 case OpenACCClauseKind::PresentOrCopyIn: {
13107 SourceLocation LParenLoc = readSourceLocation();
13108 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13109 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13110 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13111 LParenLoc, Mods: ModList, VarList, EndLoc);
13112 }
13113 case OpenACCClauseKind::CopyOut:
13114 case OpenACCClauseKind::PCopyOut:
13115 case OpenACCClauseKind::PresentOrCopyOut: {
13116 SourceLocation LParenLoc = readSourceLocation();
13117 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13118 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13119 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13120 LParenLoc, Mods: ModList, VarList, EndLoc);
13121 }
13122 case OpenACCClauseKind::Create:
13123 case OpenACCClauseKind::PCreate:
13124 case OpenACCClauseKind::PresentOrCreate: {
13125 SourceLocation LParenLoc = readSourceLocation();
13126 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13127 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13128 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13129 LParenLoc, Mods: ModList, VarList, EndLoc);
13130 }
13131 case OpenACCClauseKind::Async: {
13132 SourceLocation LParenLoc = readSourceLocation();
13133 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13134 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13135 IntExpr: AsyncExpr, EndLoc);
13136 }
13137 case OpenACCClauseKind::Wait: {
13138 SourceLocation LParenLoc = readSourceLocation();
13139 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13140 SourceLocation QueuesLoc = readSourceLocation();
13141 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13142 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13143 DevNumExpr, QueuesLoc, QueueIdExprs,
13144 EndLoc);
13145 }
13146 case OpenACCClauseKind::DeviceType:
13147 case OpenACCClauseKind::DType: {
13148 SourceLocation LParenLoc = readSourceLocation();
13149 llvm::SmallVector<DeviceTypeArgument> Archs;
13150 unsigned NumArchs = readInt();
13151
13152 for (unsigned I = 0; I < NumArchs; ++I) {
13153 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13154 SourceLocation Loc = readSourceLocation();
13155 Archs.emplace_back(Args&: Loc, Args&: Ident);
13156 }
13157
13158 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13159 LParenLoc, Archs, EndLoc);
13160 }
13161 case OpenACCClauseKind::Reduction: {
13162 SourceLocation LParenLoc = readSourceLocation();
13163 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13164 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13165 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13166
13167 for (unsigned I = 0; I < VarList.size(); ++I) {
13168 VarDecl *Recipe = readDeclAs<VarDecl>();
13169
13170 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13171 3 * sizeof(int *));
13172
13173 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13174 unsigned NumCombiners = readInt();
13175 for (unsigned I = 0; I < NumCombiners; ++I) {
13176 VarDecl *LHS = readDeclAs<VarDecl>();
13177 VarDecl *RHS = readDeclAs<VarDecl>();
13178 Expr *Op = readExpr();
13179
13180 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13181 }
13182
13183 RecipeList.push_back(Elt: {Recipe, Combiners});
13184 }
13185
13186 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13187 VarList, Recipes: RecipeList, EndLoc);
13188 }
13189 case OpenACCClauseKind::Seq:
13190 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13191 case OpenACCClauseKind::NoHost:
13192 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13193 case OpenACCClauseKind::Finalize:
13194 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13195 case OpenACCClauseKind::IfPresent:
13196 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13197 case OpenACCClauseKind::Independent:
13198 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13199 case OpenACCClauseKind::Auto:
13200 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13201 case OpenACCClauseKind::Collapse: {
13202 SourceLocation LParenLoc = readSourceLocation();
13203 bool HasForce = readBool();
13204 Expr *LoopCount = readSubExpr();
13205 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13206 HasForce, LoopCount, EndLoc);
13207 }
13208 case OpenACCClauseKind::Tile: {
13209 SourceLocation LParenLoc = readSourceLocation();
13210 unsigned NumClauses = readInt();
13211 llvm::SmallVector<Expr *> SizeExprs;
13212 for (unsigned I = 0; I < NumClauses; ++I)
13213 SizeExprs.push_back(Elt: readSubExpr());
13214 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13215 SizeExprs, EndLoc);
13216 }
13217 case OpenACCClauseKind::Gang: {
13218 SourceLocation LParenLoc = readSourceLocation();
13219 unsigned NumExprs = readInt();
13220 llvm::SmallVector<OpenACCGangKind> GangKinds;
13221 llvm::SmallVector<Expr *> Exprs;
13222 for (unsigned I = 0; I < NumExprs; ++I) {
13223 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13224 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13225 Exprs.push_back(Elt: readExpr());
13226 }
13227 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13228 GangKinds, IntExprs: Exprs, EndLoc);
13229 }
13230 case OpenACCClauseKind::Worker: {
13231 SourceLocation LParenLoc = readSourceLocation();
13232 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13233 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13234 IntExpr: WorkerExpr, EndLoc);
13235 }
13236 case OpenACCClauseKind::Vector: {
13237 SourceLocation LParenLoc = readSourceLocation();
13238 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13239 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13240 IntExpr: VectorExpr, EndLoc);
13241 }
13242 case OpenACCClauseKind::Link: {
13243 SourceLocation LParenLoc = readSourceLocation();
13244 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13245 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13246 EndLoc);
13247 }
13248 case OpenACCClauseKind::DeviceResident: {
13249 SourceLocation LParenLoc = readSourceLocation();
13250 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13251 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13252 LParenLoc, VarList, EndLoc);
13253 }
13254
13255 case OpenACCClauseKind::Bind: {
13256 SourceLocation LParenLoc = readSourceLocation();
13257 bool IsString = readBool();
13258 if (IsString)
13259 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13260 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13261 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13262 ID: readIdentifier(), EndLoc);
13263 }
13264 case OpenACCClauseKind::Shortloop:
13265 case OpenACCClauseKind::Invalid:
13266 llvm_unreachable("Clause serialization not yet implemented");
13267 }
13268 llvm_unreachable("Invalid Clause Kind");
13269}
13270
13271void ASTRecordReader::readOpenACCClauseList(
13272 MutableArrayRef<const OpenACCClause *> Clauses) {
13273 for (unsigned I = 0; I < Clauses.size(); ++I)
13274 Clauses[I] = readOpenACCClause();
13275}
13276
13277void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13278 unsigned NumVars = readInt();
13279 A->Clauses.resize(N: NumVars);
13280 readOpenACCClauseList(Clauses: A->Clauses);
13281}
13282
13283static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13284 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13285 // module name?
13286 llvm::FoldingSetNodeID ID;
13287 ID.AddString(String: PrimaryModuleName);
13288 return ID.computeStableHash();
13289}
13290
13291UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13292 if (!M)
13293 return std::nullopt;
13294
13295 if (M->isHeaderLikeModule())
13296 return std::nullopt;
13297
13298 if (M->isGlobalModule())
13299 return std::nullopt;
13300
13301 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13302 return getStableHashForModuleName(PrimaryModuleName);
13303}
13304