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