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 ContextHash, bool Complain) {
212 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
213 Complain) ||
214 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
215 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(FileManager &FileMgr, StringRef ContextHash,
964 StringRef ExistingSpecificModuleCachePath,
965 StringRef ASTFilename,
966 DiagnosticsEngine *Diags,
967 const LangOptions &LangOpts,
968 const PreprocessorOptions &PPOpts,
969 const HeaderSearchOptions &HSOpts,
970 const HeaderSearchOptions &ASTFileHSOpts) {
971 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
972 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
973 ContextHash: std::string(ContextHash));
974
975 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
976 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
977 return false;
978 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
979 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
980 if (EqualOrErr && *EqualOrErr)
981 return false;
982 if (Diags) {
983 // If the module cache arguments provided from the command line are the
984 // same, the mismatch must come from other arguments of the configuration
985 // and not directly the cache path.
986 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
987 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
988 if (EqualOrErr && *EqualOrErr)
989 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
990 else
991 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
992 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
993 << ASTFilename;
994 }
995 return true;
996}
997
998bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
999 StringRef ASTFilename,
1000 StringRef ContextHash,
1001 bool Complain) {
1002 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1003 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1004 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1005 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1006 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1007 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1008}
1009
1010void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1011 PP.setCounterValue(Value);
1012}
1013
1014//===----------------------------------------------------------------------===//
1015// AST reader implementation
1016//===----------------------------------------------------------------------===//
1017
1018static uint64_t readULEB(const unsigned char *&P) {
1019 unsigned Length = 0;
1020 const char *Error = nullptr;
1021
1022 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1023 if (Error)
1024 llvm::report_fatal_error(reason: Error);
1025 P += Length;
1026 return Val;
1027}
1028
1029/// Read ULEB-encoded key length and data length.
1030static std::pair<unsigned, unsigned>
1031readULEBKeyDataLength(const unsigned char *&P) {
1032 unsigned KeyLen = readULEB(P);
1033 if ((unsigned)KeyLen != KeyLen)
1034 llvm::report_fatal_error(reason: "key too large");
1035
1036 unsigned DataLen = readULEB(P);
1037 if ((unsigned)DataLen != DataLen)
1038 llvm::report_fatal_error(reason: "data too large");
1039
1040 return std::make_pair(x&: KeyLen, y&: DataLen);
1041}
1042
1043void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1044 bool TakeOwnership) {
1045 DeserializationListener = Listener;
1046 OwnsDeserializationListener = TakeOwnership;
1047}
1048
1049unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1050 return serialization::ComputeHash(Sel);
1051}
1052
1053LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1054 LocalDeclID ID(Value);
1055#ifndef NDEBUG
1056 if (!MF.ModuleOffsetMap.empty())
1057 Reader.ReadModuleOffsetMap(MF);
1058
1059 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1060 unsigned LocalDeclID = ID.getLocalDeclIndex();
1061
1062 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1063
1064 ModuleFile *OwningModuleFile =
1065 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1066 assert(OwningModuleFile);
1067
1068 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1069
1070 if (!ModuleFileIndex)
1071 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1072
1073 assert(LocalDeclID < LocalNumDecls);
1074#endif
1075 (void)Reader;
1076 (void)MF;
1077 return ID;
1078}
1079
1080LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1081 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1082 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1083 return LocalDeclID::get(Reader, MF, Value);
1084}
1085
1086std::pair<unsigned, unsigned>
1087ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1088 return readULEBKeyDataLength(P&: d);
1089}
1090
1091ASTSelectorLookupTrait::internal_key_type
1092ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1093 using namespace llvm::support;
1094
1095 SelectorTable &SelTable = Reader.getContext().Selectors;
1096 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1097 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1098 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1099 if (N == 0)
1100 return SelTable.getNullarySelector(ID: FirstII);
1101 else if (N == 1)
1102 return SelTable.getUnarySelector(ID: FirstII);
1103
1104 SmallVector<const IdentifierInfo *, 16> Args;
1105 Args.push_back(Elt: FirstII);
1106 for (unsigned I = 1; I != N; ++I)
1107 Args.push_back(Elt: Reader.getLocalIdentifier(
1108 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1109
1110 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1111}
1112
1113ASTSelectorLookupTrait::data_type
1114ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1115 unsigned DataLen) {
1116 using namespace llvm::support;
1117
1118 data_type Result;
1119
1120 Result.ID = Reader.getGlobalSelectorID(
1121 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1122 unsigned FullInstanceBits =
1123 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1124 unsigned FullFactoryBits =
1125 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1126 Result.InstanceBits = FullInstanceBits & 0x3;
1127 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1128 Result.FactoryBits = FullFactoryBits & 0x3;
1129 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1130 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1131 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1132
1133 // Load instance methods
1134 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1135 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1136 F, LocalID: LocalDeclID::get(
1137 Reader, MF&: F,
1138 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1139 Result.Instance.push_back(Elt: Method);
1140 }
1141
1142 // Load factory methods
1143 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1144 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1145 F, LocalID: LocalDeclID::get(
1146 Reader, MF&: F,
1147 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1148 Result.Factory.push_back(Elt: Method);
1149 }
1150
1151 return Result;
1152}
1153
1154unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1155 return llvm::djbHash(Buffer: a);
1156}
1157
1158std::pair<unsigned, unsigned>
1159ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1160 return readULEBKeyDataLength(P&: d);
1161}
1162
1163ASTIdentifierLookupTraitBase::internal_key_type
1164ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1165 assert(n >= 2 && d[n-1] == '\0');
1166 return StringRef((const char*) d, n-1);
1167}
1168
1169/// Whether the given identifier is "interesting".
1170static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1171 bool IsModule) {
1172 bool IsInteresting =
1173 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1174 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1175 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1176 return II.hadMacroDefinition() || II.isPoisoned() ||
1177 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1178 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1179 II.getFETokenInfo());
1180}
1181
1182static bool readBit(unsigned &Bits) {
1183 bool Value = Bits & 0x1;
1184 Bits >>= 1;
1185 return Value;
1186}
1187
1188IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1189 using namespace llvm::support;
1190
1191 IdentifierID RawID =
1192 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1193 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1194}
1195
1196static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1197 bool IsModule) {
1198 if (!II.isFromAST()) {
1199 II.setIsFromAST();
1200 if (isInterestingIdentifier(Reader, II, IsModule))
1201 II.setChangedSinceDeserialization();
1202 }
1203}
1204
1205IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1206 const unsigned char* d,
1207 unsigned DataLen) {
1208 using namespace llvm::support;
1209
1210 IdentifierID RawID =
1211 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1212 bool IsInteresting = RawID & 0x01;
1213
1214 DataLen -= sizeof(IdentifierID);
1215
1216 // Wipe out the "is interesting" bit.
1217 RawID = RawID >> 1;
1218
1219 // Build the IdentifierInfo and link the identifier ID with it.
1220 IdentifierInfo *II = KnownII;
1221 if (!II) {
1222 II = &Reader.getIdentifierTable().getOwn(Name: k);
1223 KnownII = II;
1224 }
1225 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1226 markIdentifierFromAST(Reader, II&: *II, IsModule);
1227 Reader.markIdentifierUpToDate(II);
1228
1229 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1230 if (!IsInteresting) {
1231 // For uninteresting identifiers, there's nothing else to do. Just notify
1232 // the reader that we've finished loading this identifier.
1233 Reader.SetIdentifierInfo(ID, II);
1234 return II;
1235 }
1236
1237 unsigned ObjCOrBuiltinID =
1238 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1239 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1240 bool CPlusPlusOperatorKeyword = readBit(Bits);
1241 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1242 bool Poisoned = readBit(Bits);
1243 bool ExtensionToken = readBit(Bits);
1244 bool HasMacroDefinition = readBit(Bits);
1245
1246 assert(Bits == 0 && "Extra bits in the identifier?");
1247 DataLen -= sizeof(uint16_t) * 2;
1248
1249 // Set or check the various bits in the IdentifierInfo structure.
1250 // Token IDs are read-only.
1251 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1252 II->revertTokenIDToIdentifier();
1253 if (!F.isModule())
1254 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1255 assert(II->isExtensionToken() == ExtensionToken &&
1256 "Incorrect extension token flag");
1257 (void)ExtensionToken;
1258 if (Poisoned)
1259 II->setIsPoisoned(true);
1260 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1261 "Incorrect C++ operator keyword flag");
1262 (void)CPlusPlusOperatorKeyword;
1263
1264 // If this identifier has a macro definition, deserialize it or notify the
1265 // visitor the actual definition is in a different module.
1266 if (HasMacroDefinition) {
1267 uint32_t MacroDirectivesOffset =
1268 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1269 DataLen -= 4;
1270
1271 if (MacroDirectivesOffset)
1272 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1273 else
1274 hasMacroDefinitionInDependencies = true;
1275 }
1276
1277 Reader.SetIdentifierInfo(ID, II);
1278
1279 // Read all of the declarations visible at global scope with this
1280 // name.
1281 if (DataLen > 0) {
1282 SmallVector<GlobalDeclID, 4> DeclIDs;
1283 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1284 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1285 F, LocalID: LocalDeclID::get(
1286 Reader, MF&: F,
1287 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1288 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1289 }
1290
1291 return II;
1292}
1293
1294DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1295 : Kind(Name.getNameKind()) {
1296 switch (Kind) {
1297 case DeclarationName::Identifier:
1298 Data = (uint64_t)Name.getAsIdentifierInfo();
1299 break;
1300 case DeclarationName::ObjCZeroArgSelector:
1301 case DeclarationName::ObjCOneArgSelector:
1302 case DeclarationName::ObjCMultiArgSelector:
1303 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1304 break;
1305 case DeclarationName::CXXOperatorName:
1306 Data = Name.getCXXOverloadedOperator();
1307 break;
1308 case DeclarationName::CXXLiteralOperatorName:
1309 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1310 break;
1311 case DeclarationName::CXXDeductionGuideName:
1312 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1313 ->getDeclName().getAsIdentifierInfo();
1314 break;
1315 case DeclarationName::CXXConstructorName:
1316 case DeclarationName::CXXDestructorName:
1317 case DeclarationName::CXXConversionFunctionName:
1318 case DeclarationName::CXXUsingDirective:
1319 Data = 0;
1320 break;
1321 }
1322}
1323
1324unsigned DeclarationNameKey::getHash() const {
1325 llvm::FoldingSetNodeID ID;
1326 ID.AddInteger(I: Kind);
1327
1328 switch (Kind) {
1329 case DeclarationName::Identifier:
1330 case DeclarationName::CXXLiteralOperatorName:
1331 case DeclarationName::CXXDeductionGuideName:
1332 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1333 break;
1334 case DeclarationName::ObjCZeroArgSelector:
1335 case DeclarationName::ObjCOneArgSelector:
1336 case DeclarationName::ObjCMultiArgSelector:
1337 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1338 break;
1339 case DeclarationName::CXXOperatorName:
1340 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1341 break;
1342 case DeclarationName::CXXConstructorName:
1343 case DeclarationName::CXXDestructorName:
1344 case DeclarationName::CXXConversionFunctionName:
1345 case DeclarationName::CXXUsingDirective:
1346 break;
1347 }
1348
1349 return ID.computeStableHash();
1350}
1351
1352ModuleFile *
1353ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1354 using namespace llvm::support;
1355
1356 uint32_t ModuleFileID =
1357 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1358 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1359}
1360
1361std::pair<unsigned, unsigned>
1362ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1363 return readULEBKeyDataLength(P&: d);
1364}
1365
1366DeclarationNameKey
1367ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1368 using namespace llvm::support;
1369
1370 auto Kind = (DeclarationName::NameKind)*d++;
1371 uint64_t Data;
1372 switch (Kind) {
1373 case DeclarationName::Identifier:
1374 case DeclarationName::CXXLiteralOperatorName:
1375 case DeclarationName::CXXDeductionGuideName:
1376 Data = (uint64_t)Reader.getLocalIdentifier(
1377 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1378 break;
1379 case DeclarationName::ObjCZeroArgSelector:
1380 case DeclarationName::ObjCOneArgSelector:
1381 case DeclarationName::ObjCMultiArgSelector:
1382 Data = (uint64_t)Reader
1383 .getLocalSelector(
1384 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1385 .getAsOpaquePtr();
1386 break;
1387 case DeclarationName::CXXOperatorName:
1388 Data = *d++; // OverloadedOperatorKind
1389 break;
1390 case DeclarationName::CXXConstructorName:
1391 case DeclarationName::CXXDestructorName:
1392 case DeclarationName::CXXConversionFunctionName:
1393 case DeclarationName::CXXUsingDirective:
1394 Data = 0;
1395 break;
1396 }
1397
1398 return DeclarationNameKey(Kind, Data);
1399}
1400
1401ASTDeclContextNameLookupTrait::internal_key_type
1402ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1403 return ReadKeyBase(d);
1404}
1405
1406void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1407 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1408 using namespace llvm::support;
1409
1410 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1411 LocalDeclID ID = LocalDeclID::get(
1412 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1413 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1414 }
1415}
1416
1417void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1418 const unsigned char *d,
1419 unsigned DataLen,
1420 data_type_builder &Val) {
1421 ReadDataIntoImpl(d, DataLen, Val);
1422}
1423
1424ModuleLocalNameLookupTrait::hash_value_type
1425ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1426 llvm::FoldingSetNodeID ID;
1427 ID.AddInteger(I: Key.first.getHash());
1428 ID.AddInteger(I: Key.second);
1429 return ID.computeStableHash();
1430}
1431
1432ModuleLocalNameLookupTrait::internal_key_type
1433ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1434 DeclarationNameKey Name(Key.first);
1435
1436 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1437 if (!ModuleHash)
1438 return {Name, 0};
1439
1440 return {Name, *ModuleHash};
1441}
1442
1443ModuleLocalNameLookupTrait::internal_key_type
1444ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1445 DeclarationNameKey Name = ReadKeyBase(d);
1446 unsigned PrimaryModuleHash =
1447 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1448 return {Name, PrimaryModuleHash};
1449}
1450
1451void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1452 const unsigned char *d,
1453 unsigned DataLen,
1454 data_type_builder &Val) {
1455 ReadDataIntoImpl(d, DataLen, Val);
1456}
1457
1458ModuleFile *
1459LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1460 using namespace llvm::support;
1461
1462 uint32_t ModuleFileID =
1463 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1464 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1465}
1466
1467LazySpecializationInfoLookupTrait::internal_key_type
1468LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1469 using namespace llvm::support;
1470 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1471}
1472
1473std::pair<unsigned, unsigned>
1474LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1475 return readULEBKeyDataLength(P&: d);
1476}
1477
1478void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1479 const unsigned char *d,
1480 unsigned DataLen,
1481 data_type_builder &Val) {
1482 using namespace llvm::support;
1483
1484 for (unsigned NumDecls =
1485 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1486 NumDecls; --NumDecls) {
1487 LocalDeclID LocalID = LocalDeclID::get(
1488 Reader, MF&: F,
1489 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1490 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1491 }
1492}
1493
1494bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1495 BitstreamCursor &Cursor,
1496 uint64_t Offset,
1497 DeclContext *DC) {
1498 assert(Offset != 0);
1499
1500 SavedStreamPosition SavedPosition(Cursor);
1501 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1502 Error(Err: std::move(Err));
1503 return true;
1504 }
1505
1506 RecordData Record;
1507 StringRef Blob;
1508 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1509 if (!MaybeCode) {
1510 Error(Err: MaybeCode.takeError());
1511 return true;
1512 }
1513 unsigned Code = MaybeCode.get();
1514
1515 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1516 if (!MaybeRecCode) {
1517 Error(Err: MaybeRecCode.takeError());
1518 return true;
1519 }
1520 unsigned RecCode = MaybeRecCode.get();
1521 if (RecCode != DECL_CONTEXT_LEXICAL) {
1522 Error(Msg: "Expected lexical block");
1523 return true;
1524 }
1525
1526 assert(!isa<TranslationUnitDecl>(DC) &&
1527 "expected a TU_UPDATE_LEXICAL record for TU");
1528 // If we are handling a C++ class template instantiation, we can see multiple
1529 // lexical updates for the same record. It's important that we select only one
1530 // of them, so that field numbering works properly. Just pick the first one we
1531 // see.
1532 auto &Lex = LexicalDecls[DC];
1533 if (!Lex.first) {
1534 Lex = std::make_pair(
1535 x: &M, y: llvm::ArrayRef(
1536 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1537 Blob.size() / sizeof(DeclID)));
1538 }
1539 DC->setHasExternalLexicalStorage(true);
1540 return false;
1541}
1542
1543bool ASTReader::ReadVisibleDeclContextStorage(
1544 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1545 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1546 assert(Offset != 0);
1547
1548 SavedStreamPosition SavedPosition(Cursor);
1549 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1550 Error(Err: std::move(Err));
1551 return true;
1552 }
1553
1554 RecordData Record;
1555 StringRef Blob;
1556 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1557 if (!MaybeCode) {
1558 Error(Err: MaybeCode.takeError());
1559 return true;
1560 }
1561 unsigned Code = MaybeCode.get();
1562
1563 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1564 if (!MaybeRecCode) {
1565 Error(Err: MaybeRecCode.takeError());
1566 return true;
1567 }
1568 unsigned RecCode = MaybeRecCode.get();
1569 switch (VisibleKind) {
1570 case VisibleDeclContextStorageKind::GenerallyVisible:
1571 if (RecCode != DECL_CONTEXT_VISIBLE) {
1572 Error(Msg: "Expected visible lookup table block");
1573 return true;
1574 }
1575 break;
1576 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1577 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1578 Error(Msg: "Expected module local visible lookup table block");
1579 return true;
1580 }
1581 break;
1582 case VisibleDeclContextStorageKind::TULocalVisible:
1583 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1584 Error(Msg: "Expected TU local lookup table block");
1585 return true;
1586 }
1587 break;
1588 }
1589
1590 // We can't safely determine the primary context yet, so delay attaching the
1591 // lookup table until we're done with recursive deserialization.
1592 auto *Data = (const unsigned char*)Blob.data();
1593 switch (VisibleKind) {
1594 case VisibleDeclContextStorageKind::GenerallyVisible:
1595 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1596 break;
1597 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1598 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1599 break;
1600 case VisibleDeclContextStorageKind::TULocalVisible:
1601 if (M.Kind == MK_MainFile)
1602 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1603 break;
1604 }
1605 return false;
1606}
1607
1608void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1609 ModuleFile &M, bool IsPartial) {
1610 D = D->getCanonicalDecl();
1611 auto &SpecLookups =
1612 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1613 SpecLookups[D].Table.add(File: &M, Data,
1614 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1615}
1616
1617bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1618 uint64_t Offset, Decl *D, bool IsPartial) {
1619 assert(Offset != 0);
1620
1621 SavedStreamPosition SavedPosition(Cursor);
1622 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1623 Error(Err: std::move(Err));
1624 return true;
1625 }
1626
1627 RecordData Record;
1628 StringRef Blob;
1629 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1630 if (!MaybeCode) {
1631 Error(Err: MaybeCode.takeError());
1632 return true;
1633 }
1634 unsigned Code = MaybeCode.get();
1635
1636 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1637 if (!MaybeRecCode) {
1638 Error(Err: MaybeRecCode.takeError());
1639 return true;
1640 }
1641 unsigned RecCode = MaybeRecCode.get();
1642 if (RecCode != DECL_SPECIALIZATIONS &&
1643 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1644 Error(Msg: "Expected decl specs block");
1645 return true;
1646 }
1647
1648 auto *Data = (const unsigned char *)Blob.data();
1649 AddSpecializations(D, Data, M, IsPartial);
1650 return false;
1651}
1652
1653void ASTReader::Error(StringRef Msg) const {
1654 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1655 if (PP.getLangOpts().Modules &&
1656 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1657 Diag(DiagID: diag::note_module_cache_path)
1658 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1659 }
1660}
1661
1662void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1663 StringRef Arg3) const {
1664 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1665}
1666
1667namespace {
1668struct AlreadyReportedDiagnosticError
1669 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1670 static char ID;
1671
1672 void log(raw_ostream &OS) const override {
1673 llvm_unreachable("reporting an already-reported diagnostic error");
1674 }
1675
1676 std::error_code convertToErrorCode() const override {
1677 return llvm::inconvertibleErrorCode();
1678 }
1679};
1680
1681char AlreadyReportedDiagnosticError::ID = 0;
1682} // namespace
1683
1684void ASTReader::Error(llvm::Error &&Err) const {
1685 handleAllErrors(
1686 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1687 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1688}
1689
1690//===----------------------------------------------------------------------===//
1691// Source Manager Deserialization
1692//===----------------------------------------------------------------------===//
1693
1694/// Read the line table in the source manager block.
1695void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1696 unsigned Idx = 0;
1697 LineTableInfo &LineTable = SourceMgr.getLineTable();
1698
1699 // Parse the file names
1700 std::map<int, int> FileIDs;
1701 FileIDs[-1] = -1; // For unspecified filenames.
1702 for (unsigned I = 0; Record[Idx]; ++I) {
1703 // Extract the file name
1704 auto Filename = ReadPath(F, Record, Idx);
1705 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1706 }
1707 ++Idx;
1708
1709 // Parse the line entries
1710 std::vector<LineEntry> Entries;
1711 while (Idx < Record.size()) {
1712 FileID FID = ReadFileID(F, Record, Idx);
1713
1714 // Extract the line entries
1715 unsigned NumEntries = Record[Idx++];
1716 assert(NumEntries && "no line entries for file ID");
1717 Entries.clear();
1718 Entries.reserve(n: NumEntries);
1719 for (unsigned I = 0; I != NumEntries; ++I) {
1720 unsigned FileOffset = Record[Idx++];
1721 unsigned LineNo = Record[Idx++];
1722 int FilenameID = FileIDs[Record[Idx++]];
1723 SrcMgr::CharacteristicKind FileKind
1724 = (SrcMgr::CharacteristicKind)Record[Idx++];
1725 unsigned IncludeOffset = Record[Idx++];
1726 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1727 FileKind, IncludeOffset));
1728 }
1729 LineTable.AddEntry(FID, Entries);
1730 }
1731}
1732
1733/// Read a source manager block
1734llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1735 using namespace SrcMgr;
1736
1737 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1738
1739 // Set the source-location entry cursor to the current position in
1740 // the stream. This cursor will be used to read the contents of the
1741 // source manager block initially, and then lazily read
1742 // source-location entries as needed.
1743 SLocEntryCursor = F.Stream;
1744
1745 // The stream itself is going to skip over the source manager block.
1746 if (llvm::Error Err = F.Stream.SkipBlock())
1747 return Err;
1748
1749 // Enter the source manager block.
1750 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1751 return Err;
1752 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1753
1754 RecordData Record;
1755 while (true) {
1756 Expected<llvm::BitstreamEntry> MaybeE =
1757 SLocEntryCursor.advanceSkippingSubblocks();
1758 if (!MaybeE)
1759 return MaybeE.takeError();
1760 llvm::BitstreamEntry E = MaybeE.get();
1761
1762 switch (E.Kind) {
1763 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1764 case llvm::BitstreamEntry::Error:
1765 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1766 Fmt: "malformed block record in AST file");
1767 case llvm::BitstreamEntry::EndBlock:
1768 return llvm::Error::success();
1769 case llvm::BitstreamEntry::Record:
1770 // The interesting case.
1771 break;
1772 }
1773
1774 // Read a record.
1775 Record.clear();
1776 StringRef Blob;
1777 Expected<unsigned> MaybeRecord =
1778 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1779 if (!MaybeRecord)
1780 return MaybeRecord.takeError();
1781 switch (MaybeRecord.get()) {
1782 default: // Default behavior: ignore.
1783 break;
1784
1785 case SM_SLOC_FILE_ENTRY:
1786 case SM_SLOC_BUFFER_ENTRY:
1787 case SM_SLOC_EXPANSION_ENTRY:
1788 // Once we hit one of the source location entries, we're done.
1789 return llvm::Error::success();
1790 }
1791 }
1792}
1793
1794llvm::Expected<SourceLocation::UIntTy>
1795ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1796 BitstreamCursor &Cursor = F->SLocEntryCursor;
1797 SavedStreamPosition SavedPosition(Cursor);
1798 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1799 F->SLocEntryOffsets[Index]))
1800 return std::move(Err);
1801
1802 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1803 if (!MaybeEntry)
1804 return MaybeEntry.takeError();
1805
1806 llvm::BitstreamEntry Entry = MaybeEntry.get();
1807 if (Entry.Kind != llvm::BitstreamEntry::Record)
1808 return llvm::createStringError(
1809 EC: std::errc::illegal_byte_sequence,
1810 Fmt: "incorrectly-formatted source location entry in AST file");
1811
1812 RecordData Record;
1813 StringRef Blob;
1814 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1815 if (!MaybeSLOC)
1816 return MaybeSLOC.takeError();
1817
1818 switch (MaybeSLOC.get()) {
1819 default:
1820 return llvm::createStringError(
1821 EC: std::errc::illegal_byte_sequence,
1822 Fmt: "incorrectly-formatted source location entry in AST file");
1823 case SM_SLOC_FILE_ENTRY:
1824 case SM_SLOC_BUFFER_ENTRY:
1825 case SM_SLOC_EXPANSION_ENTRY:
1826 return F->SLocEntryBaseOffset + Record[0];
1827 }
1828}
1829
1830int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1831 auto SLocMapI =
1832 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1833 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1834 "Corrupted global sloc offset map");
1835 ModuleFile *F = SLocMapI->second;
1836
1837 bool Invalid = false;
1838
1839 auto It = llvm::upper_bound(
1840 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1841 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1842 int ID = F->SLocEntryBaseID + LocalIndex;
1843 std::size_t Index = -ID - 2;
1844 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1845 assert(!SourceMgr.SLocEntryLoaded[Index]);
1846 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1847 if (!MaybeEntryOffset) {
1848 Error(Err: MaybeEntryOffset.takeError());
1849 Invalid = true;
1850 return true;
1851 }
1852 SourceMgr.LoadedSLocEntryTable[Index] =
1853 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1854 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1855 }
1856 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1857 });
1858
1859 if (Invalid)
1860 return 0;
1861
1862 // The iterator points to the first entry with start offset greater than the
1863 // offset of interest. The previous entry must contain the offset of interest.
1864 return F->SLocEntryBaseID + *std::prev(x: It);
1865}
1866
1867bool ASTReader::ReadSLocEntry(int ID) {
1868 if (ID == 0)
1869 return false;
1870
1871 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1872 Error(Msg: "source location entry ID out-of-range for AST file");
1873 return true;
1874 }
1875
1876 // Local helper to read the (possibly-compressed) buffer data following the
1877 // entry record.
1878 auto ReadBuffer = [this](
1879 BitstreamCursor &SLocEntryCursor,
1880 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1881 RecordData Record;
1882 StringRef Blob;
1883 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1884 if (!MaybeCode) {
1885 Error(Err: MaybeCode.takeError());
1886 return nullptr;
1887 }
1888 unsigned Code = MaybeCode.get();
1889
1890 Expected<unsigned> MaybeRecCode =
1891 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1892 if (!MaybeRecCode) {
1893 Error(Err: MaybeRecCode.takeError());
1894 return nullptr;
1895 }
1896 unsigned RecCode = MaybeRecCode.get();
1897
1898 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1899 // Inspect the first byte to differentiate zlib (\x78) and zstd
1900 // (little-endian 0xFD2FB528).
1901 const llvm::compression::Format F =
1902 Blob.size() > 0 && Blob.data()[0] == 0x78
1903 ? llvm::compression::Format::Zlib
1904 : llvm::compression::Format::Zstd;
1905 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1906 Error(Msg: Reason);
1907 return nullptr;
1908 }
1909 SmallVector<uint8_t, 0> Decompressed;
1910 if (llvm::Error E = llvm::compression::decompress(
1911 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1912 Error(Msg: "could not decompress embedded file contents: " +
1913 llvm::toString(E: std::move(E)));
1914 return nullptr;
1915 }
1916 return llvm::MemoryBuffer::getMemBufferCopy(
1917 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1918 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1919 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1920 } else {
1921 Error(Msg: "AST record has invalid code");
1922 return nullptr;
1923 }
1924 };
1925
1926 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1927 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1928 BitNo: F->SLocEntryOffsetsBase +
1929 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1930 Error(Err: std::move(Err));
1931 return true;
1932 }
1933
1934 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1935 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1936
1937 ++NumSLocEntriesRead;
1938 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1939 if (!MaybeEntry) {
1940 Error(Err: MaybeEntry.takeError());
1941 return true;
1942 }
1943 llvm::BitstreamEntry Entry = MaybeEntry.get();
1944
1945 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1946 Error(Msg: "incorrectly-formatted source location entry in AST file");
1947 return true;
1948 }
1949
1950 RecordData Record;
1951 StringRef Blob;
1952 Expected<unsigned> MaybeSLOC =
1953 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1954 if (!MaybeSLOC) {
1955 Error(Err: MaybeSLOC.takeError());
1956 return true;
1957 }
1958 switch (MaybeSLOC.get()) {
1959 default:
1960 Error(Msg: "incorrectly-formatted source location entry in AST file");
1961 return true;
1962
1963 case SM_SLOC_FILE_ENTRY: {
1964 // We will detect whether a file changed and return 'Failure' for it, but
1965 // we will also try to fail gracefully by setting up the SLocEntry.
1966 unsigned InputID = Record[4];
1967 InputFile IF = getInputFile(F&: *F, ID: InputID);
1968 OptionalFileEntryRef File = IF.getFile();
1969 bool OverriddenBuffer = IF.isOverridden();
1970
1971 // Note that we only check if a File was returned. If it was out-of-date
1972 // we have complained but we will continue creating a FileID to recover
1973 // gracefully.
1974 if (!File)
1975 return true;
1976
1977 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1978 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1979 // This is the module's main file.
1980 IncludeLoc = getImportLocation(F);
1981 }
1982 SrcMgr::CharacteristicKind
1983 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1984 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1985 LoadedOffset: BaseOffset + Record[0]);
1986 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1987 FileInfo.NumCreatedFIDs = Record[5];
1988 if (Record[3])
1989 FileInfo.setHasLineDirectives();
1990
1991 unsigned NumFileDecls = Record[7];
1992 if (NumFileDecls && ContextObj) {
1993 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1994 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1995 FileDeclIDs[FID] =
1996 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1997 }
1998
1999 const SrcMgr::ContentCache &ContentCache =
2000 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2001 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2002 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2003 !ContentCache.getBufferIfLoaded()) {
2004 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2005 if (!Buffer)
2006 return true;
2007 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2008 }
2009
2010 break;
2011 }
2012
2013 case SM_SLOC_BUFFER_ENTRY: {
2014 const char *Name = Blob.data();
2015 unsigned Offset = Record[0];
2016 SrcMgr::CharacteristicKind
2017 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2018 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2019 if (IncludeLoc.isInvalid() && F->isModule()) {
2020 IncludeLoc = getImportLocation(F);
2021 }
2022
2023 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2024 if (!Buffer)
2025 return true;
2026 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2027 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2028 if (Record[3]) {
2029 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2030 FileInfo.setHasLineDirectives();
2031 }
2032 break;
2033 }
2034
2035 case SM_SLOC_EXPANSION_ENTRY: {
2036 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2037 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2038 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2039 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2040 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2041 LoadedOffset: BaseOffset + Record[0]);
2042 break;
2043 }
2044 }
2045
2046 return false;
2047}
2048
2049std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2050 if (ID == 0)
2051 return std::make_pair(x: SourceLocation(), y: "");
2052
2053 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2054 Error(Msg: "source location entry ID out-of-range for AST file");
2055 return std::make_pair(x: SourceLocation(), y: "");
2056 }
2057
2058 // Find which module file this entry lands in.
2059 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2060 if (!M->isModule())
2061 return std::make_pair(x: SourceLocation(), y: "");
2062
2063 // FIXME: Can we map this down to a particular submodule? That would be
2064 // ideal.
2065 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2066}
2067
2068/// Find the location where the module F is imported.
2069SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2070 if (F->ImportLoc.isValid())
2071 return F->ImportLoc;
2072
2073 // Otherwise we have a PCH. It's considered to be "imported" at the first
2074 // location of its includer.
2075 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2076 // Main file is the importer.
2077 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2078 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2079 }
2080 return F->ImportedBy[0]->FirstLoc;
2081}
2082
2083/// Enter a subblock of the specified BlockID with the specified cursor. Read
2084/// the abbreviations that are at the top of the block and then leave the cursor
2085/// pointing into the block.
2086llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2087 unsigned BlockID,
2088 uint64_t *StartOfBlockOffset) {
2089 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2090 return Err;
2091
2092 if (StartOfBlockOffset)
2093 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2094
2095 while (true) {
2096 uint64_t Offset = Cursor.GetCurrentBitNo();
2097 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2098 if (!MaybeCode)
2099 return MaybeCode.takeError();
2100 unsigned Code = MaybeCode.get();
2101
2102 // We expect all abbrevs to be at the start of the block.
2103 if (Code != llvm::bitc::DEFINE_ABBREV) {
2104 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2105 return Err;
2106 return llvm::Error::success();
2107 }
2108 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2109 return Err;
2110 }
2111}
2112
2113Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2114 unsigned &Idx) {
2115 Token Tok;
2116 Tok.startToken();
2117 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2118 Tok.setKind((tok::TokenKind)Record[Idx++]);
2119 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2120
2121 if (Tok.isAnnotation()) {
2122 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2123 switch (Tok.getKind()) {
2124 case tok::annot_pragma_loop_hint: {
2125 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2126 Info->PragmaName = ReadToken(M, Record, Idx);
2127 Info->Option = ReadToken(M, Record, Idx);
2128 unsigned NumTokens = Record[Idx++];
2129 SmallVector<Token, 4> Toks;
2130 Toks.reserve(N: NumTokens);
2131 for (unsigned I = 0; I < NumTokens; ++I)
2132 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2133 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2134 Tok.setAnnotationValue(static_cast<void *>(Info));
2135 break;
2136 }
2137 case tok::annot_pragma_pack: {
2138 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2139 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2140 auto SlotLabel = ReadString(Record, Idx);
2141 Info->SlotLabel =
2142 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2143 Info->Alignment = ReadToken(M, Record, Idx);
2144 Tok.setAnnotationValue(static_cast<void *>(Info));
2145 break;
2146 }
2147 // Some annotation tokens do not use the PtrData field.
2148 case tok::annot_pragma_openmp:
2149 case tok::annot_pragma_openmp_end:
2150 case tok::annot_pragma_unused:
2151 case tok::annot_pragma_openacc:
2152 case tok::annot_pragma_openacc_end:
2153 case tok::annot_repl_input_end:
2154 break;
2155 default:
2156 llvm_unreachable("missing deserialization code for annotation token");
2157 }
2158 } else {
2159 Tok.setLength(Record[Idx++]);
2160 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2161 Tok.setIdentifierInfo(II);
2162 }
2163 return Tok;
2164}
2165
2166MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2167 BitstreamCursor &Stream = F.MacroCursor;
2168
2169 // Keep track of where we are in the stream, then jump back there
2170 // after reading this macro.
2171 SavedStreamPosition SavedPosition(Stream);
2172
2173 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2174 // FIXME this drops errors on the floor.
2175 consumeError(Err: std::move(Err));
2176 return nullptr;
2177 }
2178 RecordData Record;
2179 SmallVector<IdentifierInfo*, 16> MacroParams;
2180 MacroInfo *Macro = nullptr;
2181 llvm::MutableArrayRef<Token> MacroTokens;
2182
2183 while (true) {
2184 // Advance to the next record, but if we get to the end of the block, don't
2185 // pop it (removing all the abbreviations from the cursor) since we want to
2186 // be able to reseek within the block and read entries.
2187 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2188 Expected<llvm::BitstreamEntry> MaybeEntry =
2189 Stream.advanceSkippingSubblocks(Flags);
2190 if (!MaybeEntry) {
2191 Error(Err: MaybeEntry.takeError());
2192 return Macro;
2193 }
2194 llvm::BitstreamEntry Entry = MaybeEntry.get();
2195
2196 switch (Entry.Kind) {
2197 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2198 case llvm::BitstreamEntry::Error:
2199 Error(Msg: "malformed block record in AST file");
2200 return Macro;
2201 case llvm::BitstreamEntry::EndBlock:
2202 return Macro;
2203 case llvm::BitstreamEntry::Record:
2204 // The interesting case.
2205 break;
2206 }
2207
2208 // Read a record.
2209 Record.clear();
2210 PreprocessorRecordTypes RecType;
2211 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2212 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2213 else {
2214 Error(Err: MaybeRecType.takeError());
2215 return Macro;
2216 }
2217 switch (RecType) {
2218 case PP_MODULE_MACRO:
2219 case PP_MACRO_DIRECTIVE_HISTORY:
2220 return Macro;
2221
2222 case PP_MACRO_OBJECT_LIKE:
2223 case PP_MACRO_FUNCTION_LIKE: {
2224 // If we already have a macro, that means that we've hit the end
2225 // of the definition of the macro we were looking for. We're
2226 // done.
2227 if (Macro)
2228 return Macro;
2229
2230 unsigned NextIndex = 1; // Skip identifier ID.
2231 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2232 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2233 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2234 MI->setIsUsed(Record[NextIndex++]);
2235 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2236 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2237 PPAllocator&: PP.getPreprocessorAllocator());
2238 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2239 // Decode function-like macro info.
2240 bool isC99VarArgs = Record[NextIndex++];
2241 bool isGNUVarArgs = Record[NextIndex++];
2242 bool hasCommaPasting = Record[NextIndex++];
2243 MacroParams.clear();
2244 unsigned NumArgs = Record[NextIndex++];
2245 for (unsigned i = 0; i != NumArgs; ++i)
2246 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2247
2248 // Install function-like macro info.
2249 MI->setIsFunctionLike();
2250 if (isC99VarArgs) MI->setIsC99Varargs();
2251 if (isGNUVarArgs) MI->setIsGNUVarargs();
2252 if (hasCommaPasting) MI->setHasCommaPasting();
2253 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2254 }
2255
2256 // Remember that we saw this macro last so that we add the tokens that
2257 // form its body to it.
2258 Macro = MI;
2259
2260 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2261 Record[NextIndex]) {
2262 // We have a macro definition. Register the association
2263 PreprocessedEntityID
2264 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2265 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2266 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2267 PreprocessingRecord::PPEntityID PPID =
2268 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2269 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2270 Val: PPRec.getPreprocessedEntity(PPID));
2271 if (PPDef)
2272 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2273 }
2274
2275 ++NumMacrosRead;
2276 break;
2277 }
2278
2279 case PP_TOKEN: {
2280 // If we see a TOKEN before a PP_MACRO_*, then the file is
2281 // erroneous, just pretend we didn't see this.
2282 if (!Macro) break;
2283 if (MacroTokens.empty()) {
2284 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2285 return Macro;
2286 }
2287
2288 unsigned Idx = 0;
2289 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2290 MacroTokens = MacroTokens.drop_front();
2291 break;
2292 }
2293 }
2294 }
2295}
2296
2297PreprocessedEntityID
2298ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2299 PreprocessedEntityID LocalID) const {
2300 if (!M.ModuleOffsetMap.empty())
2301 ReadModuleOffsetMap(F&: M);
2302
2303 unsigned ModuleFileIndex = LocalID >> 32;
2304 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2305 ModuleFile *MF =
2306 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2307 assert(MF && "malformed identifier ID encoding?");
2308
2309 if (!ModuleFileIndex) {
2310 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2311 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2312 }
2313
2314 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2315}
2316
2317OptionalFileEntryRef
2318HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2319 FileManager &FileMgr = Reader.getFileManager();
2320 if (!Key.Imported)
2321 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2322
2323 auto Resolved =
2324 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2325 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2326}
2327
2328unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2329 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2330 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2331 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2332 return llvm::xxh3_64bits(data: buf);
2333}
2334
2335HeaderFileInfoTrait::internal_key_type
2336HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2337 internal_key_type ikey = {.Size: ekey.getSize(),
2338 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2339 .Filename: ekey.getName(), /*Imported*/ false};
2340 return ikey;
2341}
2342
2343bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2344 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2345 return false;
2346
2347 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2348 return true;
2349
2350 // Determine whether the actual files are equivalent.
2351 OptionalFileEntryRef FEA = getFile(Key: a);
2352 OptionalFileEntryRef FEB = getFile(Key: b);
2353 return FEA && FEA == FEB;
2354}
2355
2356std::pair<unsigned, unsigned>
2357HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2358 return readULEBKeyDataLength(P&: d);
2359}
2360
2361HeaderFileInfoTrait::internal_key_type
2362HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2363 using namespace llvm::support;
2364
2365 internal_key_type ikey;
2366 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2367 ikey.ModTime =
2368 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2369 ikey.Filename = (const char *)d;
2370 ikey.Imported = true;
2371 return ikey;
2372}
2373
2374HeaderFileInfoTrait::data_type
2375HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2376 unsigned DataLen) {
2377 using namespace llvm::support;
2378
2379 const unsigned char *End = d + DataLen;
2380 HeaderFileInfo HFI;
2381 unsigned Flags = *d++;
2382
2383 OptionalFileEntryRef FE;
2384 bool Included = (Flags >> 6) & 0x01;
2385 if (Included)
2386 if ((FE = getFile(Key: key)))
2387 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2388 // deserialize this header file info again.
2389 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2390
2391 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2392 HFI.isImport |= (Flags >> 5) & 0x01;
2393 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2394 HFI.DirInfo = (Flags >> 1) & 0x07;
2395 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2396 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2397
2398 assert((End - d) % 4 == 0 &&
2399 "Wrong data length in HeaderFileInfo deserialization");
2400 while (d != End) {
2401 uint32_t LocalSMID =
2402 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2403 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2404 LocalSMID >>= 3;
2405
2406 // This header is part of a module. Associate it with the module to enable
2407 // implicit module import.
2408 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2409 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2410 ModuleMap &ModMap =
2411 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2412
2413 if (FE || (FE = getFile(Key: key))) {
2414 // FIXME: NameAsWritten
2415 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2416 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2417 }
2418 HFI.mergeModuleMembership(Role: HeaderRole);
2419 }
2420
2421 // This HeaderFileInfo was externally loaded.
2422 HFI.External = true;
2423 HFI.IsValid = true;
2424 return HFI;
2425}
2426
2427void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2428 uint32_t MacroDirectivesOffset) {
2429 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2430 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2431}
2432
2433void ASTReader::ReadDefinedMacros() {
2434 // Note that we are loading defined macros.
2435 Deserializing Macros(this);
2436
2437 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2438 BitstreamCursor &MacroCursor = I.MacroCursor;
2439
2440 // If there was no preprocessor block, skip this file.
2441 if (MacroCursor.getBitcodeBytes().empty())
2442 continue;
2443
2444 BitstreamCursor Cursor = MacroCursor;
2445 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2446 Error(Err: std::move(Err));
2447 return;
2448 }
2449
2450 RecordData Record;
2451 while (true) {
2452 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2453 if (!MaybeE) {
2454 Error(Err: MaybeE.takeError());
2455 return;
2456 }
2457 llvm::BitstreamEntry E = MaybeE.get();
2458
2459 switch (E.Kind) {
2460 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2461 case llvm::BitstreamEntry::Error:
2462 Error(Msg: "malformed block record in AST file");
2463 return;
2464 case llvm::BitstreamEntry::EndBlock:
2465 goto NextCursor;
2466
2467 case llvm::BitstreamEntry::Record: {
2468 Record.clear();
2469 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2470 if (!MaybeRecord) {
2471 Error(Err: MaybeRecord.takeError());
2472 return;
2473 }
2474 switch (MaybeRecord.get()) {
2475 default: // Default behavior: ignore.
2476 break;
2477
2478 case PP_MACRO_OBJECT_LIKE:
2479 case PP_MACRO_FUNCTION_LIKE: {
2480 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2481 if (II->isOutOfDate())
2482 updateOutOfDateIdentifier(II: *II);
2483 break;
2484 }
2485
2486 case PP_TOKEN:
2487 // Ignore tokens.
2488 break;
2489 }
2490 break;
2491 }
2492 }
2493 }
2494 NextCursor: ;
2495 }
2496}
2497
2498namespace {
2499
2500 /// Visitor class used to look up identifirs in an AST file.
2501 class IdentifierLookupVisitor {
2502 StringRef Name;
2503 unsigned NameHash;
2504 unsigned PriorGeneration;
2505 unsigned &NumIdentifierLookups;
2506 unsigned &NumIdentifierLookupHits;
2507 IdentifierInfo *Found = nullptr;
2508
2509 public:
2510 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2511 unsigned &NumIdentifierLookups,
2512 unsigned &NumIdentifierLookupHits)
2513 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2514 PriorGeneration(PriorGeneration),
2515 NumIdentifierLookups(NumIdentifierLookups),
2516 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2517
2518 bool operator()(ModuleFile &M) {
2519 // If we've already searched this module file, skip it now.
2520 if (M.Generation <= PriorGeneration)
2521 return true;
2522
2523 ASTIdentifierLookupTable *IdTable
2524 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2525 if (!IdTable)
2526 return false;
2527
2528 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2529 Found);
2530 ++NumIdentifierLookups;
2531 ASTIdentifierLookupTable::iterator Pos =
2532 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2533 if (Pos == IdTable->end())
2534 return false;
2535
2536 // Dereferencing the iterator has the effect of building the
2537 // IdentifierInfo node and populating it with the various
2538 // declarations it needs.
2539 ++NumIdentifierLookupHits;
2540 Found = *Pos;
2541 if (Trait.hasMoreInformationInDependencies()) {
2542 // Look for the identifier in extra modules as they contain more info.
2543 return false;
2544 }
2545 return true;
2546 }
2547
2548 // Retrieve the identifier info found within the module
2549 // files.
2550 IdentifierInfo *getIdentifierInfo() const { return Found; }
2551 };
2552
2553} // namespace
2554
2555void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2556 // Note that we are loading an identifier.
2557 Deserializing AnIdentifier(this);
2558
2559 unsigned PriorGeneration = 0;
2560 if (getContext().getLangOpts().Modules)
2561 PriorGeneration = IdentifierGeneration[&II];
2562
2563 // If there is a global index, look there first to determine which modules
2564 // provably do not have any results for this identifier.
2565 GlobalModuleIndex::HitSet Hits;
2566 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2567 if (!loadGlobalIndex()) {
2568 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2569 HitsPtr = &Hits;
2570 }
2571 }
2572
2573 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2574 NumIdentifierLookups,
2575 NumIdentifierLookupHits);
2576 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2577 markIdentifierUpToDate(II: &II);
2578}
2579
2580void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2581 if (!II)
2582 return;
2583
2584 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2585
2586 // Update the generation for this identifier.
2587 if (getContext().getLangOpts().Modules)
2588 IdentifierGeneration[II] = getGeneration();
2589}
2590
2591MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2592 unsigned &Idx) {
2593 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2594 uint64_t LocalIndex = Record[Idx++];
2595 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2596}
2597
2598void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2599 const PendingMacroInfo &PMInfo) {
2600 ModuleFile &M = *PMInfo.M;
2601
2602 BitstreamCursor &Cursor = M.MacroCursor;
2603 SavedStreamPosition SavedPosition(Cursor);
2604 if (llvm::Error Err =
2605 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2606 Error(Err: std::move(Err));
2607 return;
2608 }
2609
2610 struct ModuleMacroRecord {
2611 SubmoduleID SubModID;
2612 MacroInfo *MI;
2613 SmallVector<SubmoduleID, 8> Overrides;
2614 };
2615 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2616
2617 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2618 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2619 // macro histroy.
2620 RecordData Record;
2621 while (true) {
2622 Expected<llvm::BitstreamEntry> MaybeEntry =
2623 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2624 if (!MaybeEntry) {
2625 Error(Err: MaybeEntry.takeError());
2626 return;
2627 }
2628 llvm::BitstreamEntry Entry = MaybeEntry.get();
2629
2630 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2631 Error(Msg: "malformed block record in AST file");
2632 return;
2633 }
2634
2635 Record.clear();
2636 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2637 if (!MaybePP) {
2638 Error(Err: MaybePP.takeError());
2639 return;
2640 }
2641 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2642 case PP_MACRO_DIRECTIVE_HISTORY:
2643 break;
2644
2645 case PP_MODULE_MACRO: {
2646 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2647 auto &Info = ModuleMacros.back();
2648 unsigned Idx = 0;
2649 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2650 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2651 for (int I = Idx, N = Record.size(); I != N; ++I)
2652 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2653 continue;
2654 }
2655
2656 default:
2657 Error(Msg: "malformed block record in AST file");
2658 return;
2659 }
2660
2661 // We found the macro directive history; that's the last record
2662 // for this macro.
2663 break;
2664 }
2665
2666 // Module macros are listed in reverse dependency order.
2667 {
2668 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2669 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2670 for (auto &MMR : ModuleMacros) {
2671 Overrides.clear();
2672 for (unsigned ModID : MMR.Overrides) {
2673 Module *Mod = getSubmodule(GlobalID: ModID);
2674 auto *Macro = PP.getModuleMacro(Mod, II);
2675 assert(Macro && "missing definition for overridden macro");
2676 Overrides.push_back(Elt: Macro);
2677 }
2678
2679 bool Inserted = false;
2680 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2681 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2682 }
2683 }
2684
2685 // Don't read the directive history for a module; we don't have anywhere
2686 // to put it.
2687 if (M.isModule())
2688 return;
2689
2690 // Deserialize the macro directives history in reverse source-order.
2691 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2692 unsigned Idx = 0, N = Record.size();
2693 while (Idx < N) {
2694 MacroDirective *MD = nullptr;
2695 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2696 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2697 switch (K) {
2698 case MacroDirective::MD_Define: {
2699 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2700 MD = PP.AllocateDefMacroDirective(MI, Loc);
2701 break;
2702 }
2703 case MacroDirective::MD_Undefine:
2704 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2705 break;
2706 case MacroDirective::MD_Visibility:
2707 bool isPublic = Record[Idx++];
2708 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2709 break;
2710 }
2711
2712 if (!Latest)
2713 Latest = MD;
2714 if (Earliest)
2715 Earliest->setPrevious(MD);
2716 Earliest = MD;
2717 }
2718
2719 if (Latest)
2720 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2721}
2722
2723bool ASTReader::shouldDisableValidationForFile(
2724 const serialization::ModuleFile &M) const {
2725 if (DisableValidationKind == DisableValidationForModuleKind::None)
2726 return false;
2727
2728 // If a PCH is loaded and validation is disabled for PCH then disable
2729 // validation for the PCH and the modules it loads.
2730 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2731
2732 switch (K) {
2733 case MK_MainFile:
2734 case MK_Preamble:
2735 case MK_PCH:
2736 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2737 case MK_ImplicitModule:
2738 case MK_ExplicitModule:
2739 case MK_PrebuiltModule:
2740 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2741 }
2742
2743 return false;
2744}
2745
2746static std::pair<StringRef, StringRef>
2747getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2748 const StringRef InputBlob) {
2749 uint16_t AsRequestedLength = Record[7];
2750 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2751 InputBlob.substr(Start: AsRequestedLength)};
2752}
2753
2754InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2755 // If this ID is bogus, just return an empty input file.
2756 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2757 return InputFileInfo();
2758
2759 // If we've already loaded this input file, return it.
2760 if (F.InputFileInfosLoaded[ID - 1].isValid())
2761 return F.InputFileInfosLoaded[ID - 1];
2762
2763 // Go find this input file.
2764 BitstreamCursor &Cursor = F.InputFilesCursor;
2765 SavedStreamPosition SavedPosition(Cursor);
2766 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2767 F.InputFileOffsets[ID - 1])) {
2768 // FIXME this drops errors on the floor.
2769 consumeError(Err: std::move(Err));
2770 }
2771
2772 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2773 if (!MaybeCode) {
2774 // FIXME this drops errors on the floor.
2775 consumeError(Err: MaybeCode.takeError());
2776 }
2777 unsigned Code = MaybeCode.get();
2778 RecordData Record;
2779 StringRef Blob;
2780
2781 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2782 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2783 "invalid record type for input file");
2784 else {
2785 // FIXME this drops errors on the floor.
2786 consumeError(Err: Maybe.takeError());
2787 }
2788
2789 assert(Record[0] == ID && "Bogus stored ID or offset");
2790 InputFileInfo R;
2791 R.StoredSize = static_cast<off_t>(Record[1]);
2792 R.StoredTime = static_cast<time_t>(Record[2]);
2793 R.Overridden = static_cast<bool>(Record[3]);
2794 R.Transient = static_cast<bool>(Record[4]);
2795 R.TopLevel = static_cast<bool>(Record[5]);
2796 R.ModuleMap = static_cast<bool>(Record[6]);
2797 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2798 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2799 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2800 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2801 ? UnresolvedFilenameAsRequested
2802 : UnresolvedFilename;
2803
2804 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2805 if (!MaybeEntry) // FIXME this drops errors on the floor.
2806 consumeError(Err: MaybeEntry.takeError());
2807 llvm::BitstreamEntry Entry = MaybeEntry.get();
2808 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2809 "expected record type for input file hash");
2810
2811 Record.clear();
2812 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2813 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2814 "invalid record type for input file hash");
2815 else {
2816 // FIXME this drops errors on the floor.
2817 consumeError(Err: Maybe.takeError());
2818 }
2819 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2820 static_cast<uint64_t>(Record[0]);
2821
2822 // Note that we've loaded this input file info.
2823 F.InputFileInfosLoaded[ID - 1] = R;
2824 return R;
2825}
2826
2827static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2828InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2829 // If this ID is bogus, just return an empty input file.
2830 if (ID == 0 || ID > F.InputFilesLoaded.size())
2831 return InputFile();
2832
2833 // If we've already loaded this input file, return it.
2834 if (F.InputFilesLoaded[ID-1].getFile())
2835 return F.InputFilesLoaded[ID-1];
2836
2837 if (F.InputFilesLoaded[ID-1].isNotFound())
2838 return InputFile();
2839
2840 // Go find this input file.
2841 BitstreamCursor &Cursor = F.InputFilesCursor;
2842 SavedStreamPosition SavedPosition(Cursor);
2843 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2844 F.InputFileOffsets[ID - 1])) {
2845 // FIXME this drops errors on the floor.
2846 consumeError(Err: std::move(Err));
2847 }
2848
2849 InputFileInfo FI = getInputFileInfo(F, ID);
2850 off_t StoredSize = FI.StoredSize;
2851 time_t StoredTime = FI.StoredTime;
2852 bool Overridden = FI.Overridden;
2853 bool Transient = FI.Transient;
2854 auto Filename =
2855 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2856 uint64_t StoredContentHash = FI.ContentHash;
2857
2858 // For standard C++ modules, we don't need to check the inputs.
2859 bool SkipChecks = F.StandardCXXModule;
2860
2861 const HeaderSearchOptions &HSOpts =
2862 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2863
2864 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2865 // modules.
2866 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2867 SkipChecks = false;
2868 Overridden = false;
2869 }
2870
2871 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2872
2873 // For an overridden file, create a virtual file with the stored
2874 // size/timestamp.
2875 if ((Overridden || Transient || SkipChecks) && !File)
2876 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2877
2878 if (!File) {
2879 if (Complain) {
2880 std::string ErrorStr = "could not find file '";
2881 ErrorStr += *Filename;
2882 ErrorStr += "' referenced by AST file '";
2883 ErrorStr += F.FileName;
2884 ErrorStr += "'";
2885 Error(Msg: ErrorStr);
2886 }
2887 // Record that we didn't find the file.
2888 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2889 return InputFile();
2890 }
2891
2892 // Check if there was a request to override the contents of the file
2893 // that was part of the precompiled header. Overriding such a file
2894 // can lead to problems when lexing using the source locations from the
2895 // PCH.
2896 SourceManager &SM = getSourceManager();
2897 // FIXME: Reject if the overrides are different.
2898 if ((!Overridden && !Transient) && !SkipChecks &&
2899 SM.isFileOverridden(File: *File)) {
2900 if (Complain)
2901 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2902
2903 // After emitting the diagnostic, bypass the overriding file to recover
2904 // (this creates a separate FileEntry).
2905 File = SM.bypassFileContentsOverride(File: *File);
2906 if (!File) {
2907 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2908 return InputFile();
2909 }
2910 }
2911
2912 struct Change {
2913 enum ModificationKind {
2914 Size,
2915 ModTime,
2916 Content,
2917 None,
2918 } Kind;
2919 std::optional<int64_t> Old = std::nullopt;
2920 std::optional<int64_t> New = std::nullopt;
2921 };
2922 auto HasInputContentChanged = [&](Change OriginalChange) {
2923 assert(ValidateASTInputFilesContent &&
2924 "We should only check the content of the inputs with "
2925 "ValidateASTInputFilesContent enabled.");
2926
2927 if (StoredContentHash == 0)
2928 return OriginalChange;
2929
2930 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2931 if (!MemBuffOrError) {
2932 if (!Complain)
2933 return OriginalChange;
2934 std::string ErrorStr = "could not get buffer for file '";
2935 ErrorStr += File->getName();
2936 ErrorStr += "'";
2937 Error(Msg: ErrorStr);
2938 return OriginalChange;
2939 }
2940
2941 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2942 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2943 return Change{.Kind: Change::None};
2944
2945 return Change{.Kind: Change::Content};
2946 };
2947 auto HasInputFileChanged = [&]() {
2948 if (StoredSize != File->getSize())
2949 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2950 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2951 StoredTime != File->getModificationTime()) {
2952 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2953 .New: File->getModificationTime()};
2954
2955 // In case the modification time changes but not the content,
2956 // accept the cached file as legit.
2957 if (ValidateASTInputFilesContent)
2958 return HasInputContentChanged(MTimeChange);
2959
2960 return MTimeChange;
2961 }
2962 return Change{.Kind: Change::None};
2963 };
2964
2965 bool IsOutOfDate = false;
2966 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2967 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2968 // enabled, it is better to check the contents of the inputs. Since we can't
2969 // get correct modified time information for inputs from overriden inputs.
2970 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2971 F.StandardCXXModule && FileChange.Kind == Change::None)
2972 FileChange = HasInputContentChanged(FileChange);
2973
2974 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2975 // it is better to check the content of the input files because we cannot rely
2976 // on the file modification time, which will be the same (zero) for these
2977 // files.
2978 if (!StoredTime && ValidateASTInputFilesContent &&
2979 FileChange.Kind == Change::None)
2980 FileChange = HasInputContentChanged(FileChange);
2981
2982 // For an overridden file, there is nothing to validate.
2983 if (!Overridden && FileChange.Kind != Change::None) {
2984 if (Complain) {
2985 // Build a list of the PCH imports that got us here (in reverse).
2986 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2987 while (!ImportStack.back()->ImportedBy.empty())
2988 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2989
2990 // The top-level AST file is stale.
2991 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
2992 Diag(DiagID: diag::err_fe_ast_file_modified)
2993 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
2994 << TopLevelASTFileName;
2995 Diag(DiagID: diag::note_fe_ast_file_modified)
2996 << FileChange.Kind << (FileChange.Old && FileChange.New)
2997 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
2998 << llvm::itostr(X: FileChange.New.value_or(u: 0));
2999
3000 // Print the import stack.
3001 if (ImportStack.size() > 1) {
3002 Diag(DiagID: diag::note_ast_file_required_by)
3003 << *Filename << ImportStack[0]->FileName;
3004 for (unsigned I = 1; I < ImportStack.size(); ++I)
3005 Diag(DiagID: diag::note_ast_file_required_by)
3006 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3007 }
3008
3009 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3010 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3011 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3012 << F.InputFilesValidationStatus;
3013 }
3014
3015 IsOutOfDate = true;
3016 }
3017 // FIXME: If the file is overridden and we've already opened it,
3018 // issue an error (or split it into a separate FileEntry).
3019
3020 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3021
3022 // Note that we've loaded this input file.
3023 F.InputFilesLoaded[ID-1] = IF;
3024 return IF;
3025}
3026
3027ASTReader::TemporarilyOwnedStringRef
3028ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3029 ModuleFile &ModF) {
3030 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3031}
3032
3033ASTReader::TemporarilyOwnedStringRef
3034ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3035 StringRef Prefix) {
3036 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3037
3038 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3039 Path == "<built-in>" || Path == "<command line>")
3040 return {Path, Buf};
3041
3042 Buf.clear();
3043 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3044 StringRef ResolvedPath{Buf.data(), Buf.size()};
3045 return {ResolvedPath, Buf};
3046}
3047
3048std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3049 StringRef P,
3050 ModuleFile &ModF) {
3051 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3052}
3053
3054std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3055 StringRef P,
3056 StringRef Prefix) {
3057 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3058 return ResolvedPath->str();
3059}
3060
3061static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3062 switch (ARR) {
3063 case ASTReader::Failure: return true;
3064 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3065 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3066 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3067 case ASTReader::ConfigurationMismatch:
3068 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3069 case ASTReader::HadErrors: return true;
3070 case ASTReader::Success: return false;
3071 }
3072
3073 llvm_unreachable("unknown ASTReadResult");
3074}
3075
3076ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3077 BitstreamCursor &Stream, StringRef Filename,
3078 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3079 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3080 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3081 // FIXME this drops errors on the floor.
3082 consumeError(Err: std::move(Err));
3083 return Failure;
3084 }
3085
3086 // Read all of the records in the options block.
3087 RecordData Record;
3088 ASTReadResult Result = Success;
3089 while (true) {
3090 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3091 if (!MaybeEntry) {
3092 // FIXME this drops errors on the floor.
3093 consumeError(Err: MaybeEntry.takeError());
3094 return Failure;
3095 }
3096 llvm::BitstreamEntry Entry = MaybeEntry.get();
3097
3098 switch (Entry.Kind) {
3099 case llvm::BitstreamEntry::Error:
3100 case llvm::BitstreamEntry::SubBlock:
3101 return Failure;
3102
3103 case llvm::BitstreamEntry::EndBlock:
3104 return Result;
3105
3106 case llvm::BitstreamEntry::Record:
3107 // The interesting case.
3108 break;
3109 }
3110
3111 // Read and process a record.
3112 Record.clear();
3113 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3114 if (!MaybeRecordType) {
3115 // FIXME this drops errors on the floor.
3116 consumeError(Err: MaybeRecordType.takeError());
3117 return Failure;
3118 }
3119 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3120 case LANGUAGE_OPTIONS: {
3121 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3122 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3123 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3124 Result = ConfigurationMismatch;
3125 break;
3126 }
3127
3128 case CODEGEN_OPTIONS: {
3129 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3130 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3131 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3132 Result = ConfigurationMismatch;
3133 break;
3134 }
3135
3136 case TARGET_OPTIONS: {
3137 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3138 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3139 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3140 Result = ConfigurationMismatch;
3141 break;
3142 }
3143
3144 case FILE_SYSTEM_OPTIONS: {
3145 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3146 if (!AllowCompatibleConfigurationMismatch &&
3147 ParseFileSystemOptions(Record, Complain, Listener))
3148 Result = ConfigurationMismatch;
3149 break;
3150 }
3151
3152 case HEADER_SEARCH_OPTIONS: {
3153 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3154 if (!AllowCompatibleConfigurationMismatch &&
3155 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3156 Result = ConfigurationMismatch;
3157 break;
3158 }
3159
3160 case PREPROCESSOR_OPTIONS:
3161 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3162 if (!AllowCompatibleConfigurationMismatch &&
3163 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3164 SuggestedPredefines))
3165 Result = ConfigurationMismatch;
3166 break;
3167 }
3168 }
3169}
3170
3171ASTReader::RelocationResult
3172ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3173 // Don't emit module relocation errors if we have -fno-validate-pch.
3174 const bool IgnoreError =
3175 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3176 DisableValidationForModuleKind::Module);
3177
3178 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3179 return {std::nullopt, IgnoreError};
3180
3181 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3182
3183 if (!DirectoryCheck &&
3184 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3185 return {std::nullopt, IgnoreError};
3186
3187 const HeaderSearchOptions &HSOpts =
3188 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3189
3190 // When only validating modules once per build session,
3191 // Skip check if the timestamp is up to date or module was built in same build
3192 // session.
3193 if (HSOpts.ModulesValidateOncePerBuildSession && IsImplicitModule) {
3194 if (F.InputFilesValidationTimestamp >= HSOpts.BuildSessionTimestamp)
3195 return {std::nullopt, IgnoreError};
3196 if (static_cast<uint64_t>(F.File.getModificationTime()) >=
3197 HSOpts.BuildSessionTimestamp)
3198 return {std::nullopt, IgnoreError};
3199 }
3200
3201 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3202
3203 // If we've already loaded a module map file covering this module, we may
3204 // have a better path for it (relative to the current build if doing directory
3205 // check).
3206 Module *M = PP.getHeaderSearchInfo().lookupModule(
3207 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3208 /*AllowSearch=*/DirectoryCheck,
3209 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3210
3211 return {M, IgnoreError};
3212}
3213
3214ASTReader::ASTReadResult
3215ASTReader::ReadControlBlock(ModuleFile &F,
3216 SmallVectorImpl<ImportedModule> &Loaded,
3217 const ModuleFile *ImportedBy,
3218 unsigned ClientLoadCapabilities) {
3219 BitstreamCursor &Stream = F.Stream;
3220
3221 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3222 Error(Err: std::move(Err));
3223 return Failure;
3224 }
3225
3226 // Lambda to read the unhashed control block the first time it's called.
3227 //
3228 // For PCM files, the unhashed control block cannot be read until after the
3229 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3230 // need to look ahead before reading the IMPORTS record. For consistency,
3231 // this block is always read somehow (see BitstreamEntry::EndBlock).
3232 bool HasReadUnhashedControlBlock = false;
3233 auto readUnhashedControlBlockOnce = [&]() {
3234 if (!HasReadUnhashedControlBlock) {
3235 HasReadUnhashedControlBlock = true;
3236 if (ASTReadResult Result =
3237 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3238 return Result;
3239 }
3240 return Success;
3241 };
3242
3243 bool DisableValidation = shouldDisableValidationForFile(M: F);
3244
3245 // Read all of the records and blocks in the control block.
3246 RecordData Record;
3247 unsigned NumInputs = 0;
3248 unsigned NumUserInputs = 0;
3249 StringRef BaseDirectoryAsWritten;
3250 while (true) {
3251 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3252 if (!MaybeEntry) {
3253 Error(Err: MaybeEntry.takeError());
3254 return Failure;
3255 }
3256 llvm::BitstreamEntry Entry = MaybeEntry.get();
3257
3258 switch (Entry.Kind) {
3259 case llvm::BitstreamEntry::Error:
3260 Error(Msg: "malformed block record in AST file");
3261 return Failure;
3262 case llvm::BitstreamEntry::EndBlock: {
3263 // Validate the module before returning. This call catches an AST with
3264 // no module name and no imports.
3265 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3266 return Result;
3267
3268 // Validate input files.
3269 const HeaderSearchOptions &HSOpts =
3270 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3271
3272 // All user input files reside at the index range [0, NumUserInputs), and
3273 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3274 // loaded module files, ignore missing inputs.
3275 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3276 F.Kind != MK_PrebuiltModule) {
3277 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3278
3279 // If we are reading a module, we will create a verification timestamp,
3280 // so we verify all input files. Otherwise, verify only user input
3281 // files.
3282
3283 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3284 F.InputFilesValidationStatus = ValidateSystemInputs
3285 ? InputFilesValidation::AllFiles
3286 : InputFilesValidation::UserFiles;
3287 if (HSOpts.ModulesValidateOncePerBuildSession &&
3288 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3289 F.Kind == MK_ImplicitModule) {
3290 N = ForceValidateUserInputs ? NumUserInputs : 0;
3291 F.InputFilesValidationStatus =
3292 ForceValidateUserInputs
3293 ? InputFilesValidation::UserFiles
3294 : InputFilesValidation::SkippedInBuildSession;
3295 }
3296
3297 if (N != 0)
3298 Diag(DiagID: diag::remark_module_validation)
3299 << N << F.ModuleName << F.FileName;
3300
3301 for (unsigned I = 0; I < N; ++I) {
3302 InputFile IF = getInputFile(F, ID: I+1, Complain);
3303 if (!IF.getFile() || IF.isOutOfDate())
3304 return OutOfDate;
3305 }
3306 } else {
3307 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3308 }
3309
3310 if (Listener)
3311 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
3312
3313 if (Listener && Listener->needsInputFileVisitation()) {
3314 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3315 : NumUserInputs;
3316 for (unsigned I = 0; I < N; ++I) {
3317 bool IsSystem = I >= NumUserInputs;
3318 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3319 auto FilenameAsRequested = ResolveImportedPath(
3320 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3321 Listener->visitInputFile(
3322 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3323 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3324 }
3325 }
3326
3327 return Success;
3328 }
3329
3330 case llvm::BitstreamEntry::SubBlock:
3331 switch (Entry.ID) {
3332 case INPUT_FILES_BLOCK_ID:
3333 F.InputFilesCursor = Stream;
3334 if (llvm::Error Err = Stream.SkipBlock()) {
3335 Error(Err: std::move(Err));
3336 return Failure;
3337 }
3338 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3339 Error(Msg: "malformed block record in AST file");
3340 return Failure;
3341 }
3342 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3343 continue;
3344
3345 case OPTIONS_BLOCK_ID:
3346 // If we're reading the first module for this group, check its options
3347 // are compatible with ours. For modules it imports, no further checking
3348 // is required, because we checked them when we built it.
3349 if (Listener && !ImportedBy) {
3350 // Should we allow the configuration of the module file to differ from
3351 // the configuration of the current translation unit in a compatible
3352 // way?
3353 //
3354 // FIXME: Allow this for files explicitly specified with -include-pch.
3355 bool AllowCompatibleConfigurationMismatch =
3356 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3357
3358 ASTReadResult Result =
3359 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3360 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3361 SuggestedPredefines);
3362 if (Result == Failure) {
3363 Error(Msg: "malformed block record in AST file");
3364 return Result;
3365 }
3366
3367 if (DisableValidation ||
3368 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3369 Result = Success;
3370
3371 // If we can't load the module, exit early since we likely
3372 // will rebuild the module anyway. The stream may be in the
3373 // middle of a block.
3374 if (Result != Success)
3375 return Result;
3376 } else if (llvm::Error Err = Stream.SkipBlock()) {
3377 Error(Err: std::move(Err));
3378 return Failure;
3379 }
3380 continue;
3381
3382 default:
3383 if (llvm::Error Err = Stream.SkipBlock()) {
3384 Error(Err: std::move(Err));
3385 return Failure;
3386 }
3387 continue;
3388 }
3389
3390 case llvm::BitstreamEntry::Record:
3391 // The interesting case.
3392 break;
3393 }
3394
3395 // Read and process a record.
3396 Record.clear();
3397 StringRef Blob;
3398 Expected<unsigned> MaybeRecordType =
3399 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3400 if (!MaybeRecordType) {
3401 Error(Err: MaybeRecordType.takeError());
3402 return Failure;
3403 }
3404 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3405 case METADATA: {
3406 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3407 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3408 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3409 : diag::err_ast_file_version_too_new)
3410 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3411 return VersionMismatch;
3412 }
3413
3414 bool hasErrors = Record[7];
3415 if (hasErrors && !DisableValidation) {
3416 // If requested by the caller and the module hasn't already been read
3417 // or compiled, mark modules on error as out-of-date.
3418 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3419 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3420 return OutOfDate;
3421
3422 if (!AllowASTWithCompilerErrors) {
3423 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3424 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3425 return HadErrors;
3426 }
3427 }
3428 if (hasErrors) {
3429 Diags.ErrorOccurred = true;
3430 Diags.UncompilableErrorOccurred = true;
3431 Diags.UnrecoverableErrorOccurred = true;
3432 }
3433
3434 F.RelocatablePCH = Record[4];
3435 // Relative paths in a relocatable PCH are relative to our sysroot.
3436 if (F.RelocatablePCH)
3437 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3438
3439 F.StandardCXXModule = Record[5];
3440
3441 F.HasTimestamps = Record[6];
3442
3443 const std::string &CurBranch = getClangFullRepositoryVersion();
3444 StringRef ASTBranch = Blob;
3445 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3446 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3447 Diag(DiagID: diag::err_ast_file_different_branch)
3448 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3449 << CurBranch;
3450 return VersionMismatch;
3451 }
3452 break;
3453 }
3454
3455 case IMPORT: {
3456 // Validate the AST before processing any imports (otherwise, untangling
3457 // them can be error-prone and expensive). A module will have a name and
3458 // will already have been validated, but this catches the PCH case.
3459 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3460 return Result;
3461
3462 unsigned Idx = 0;
3463 // Read information about the AST file.
3464 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3465
3466 // The import location will be the local one for now; we will adjust
3467 // all import locations of module imports after the global source
3468 // location info are setup, in ReadAST.
3469 auto [ImportLoc, ImportModuleFileIndex] =
3470 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3471 // The import location must belong to the current module file itself.
3472 assert(ImportModuleFileIndex == 0);
3473
3474 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3475
3476 bool IsImportingStdCXXModule = Record[Idx++];
3477
3478 off_t StoredSize = 0;
3479 time_t StoredModTime = 0;
3480 ASTFileSignature StoredSignature;
3481 std::string ImportedFile;
3482 std::string StoredFile;
3483 bool IgnoreImportedByNote = false;
3484
3485 // For prebuilt and explicit modules first consult the file map for
3486 // an override. Note that here we don't search prebuilt module
3487 // directories if we're not importing standard c++ module, only the
3488 // explicit name to file mappings. Also, we will still verify the
3489 // size/signature making sure it is essentially the same file but
3490 // perhaps in a different location.
3491 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3492 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3493 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3494
3495 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3496 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3497 return Missing;
3498 }
3499
3500 if (!IsImportingStdCXXModule) {
3501 StoredSize = (off_t)Record[Idx++];
3502 StoredModTime = (time_t)Record[Idx++];
3503
3504 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3505 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3506 Last: SignatureBytes.end());
3507 Blob = Blob.substr(Start: ASTFileSignature::size);
3508
3509 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3510 // ModuleCache as when writing.
3511 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3512 if (ImportedFile.empty()) {
3513 ImportedFile = StoredFile;
3514 } else if (!getDiags().isIgnored(
3515 DiagID: diag::warn_module_file_mapping_mismatch,
3516 Loc: CurrentImportLoc)) {
3517 auto ImportedFileRef =
3518 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3519 auto StoredFileRef =
3520 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3521 if ((ImportedFileRef && StoredFileRef) &&
3522 (*ImportedFileRef != *StoredFileRef)) {
3523 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3524 << ImportedFile << StoredFile;
3525 Diag(DiagID: diag::note_module_file_imported_by)
3526 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3527 IgnoreImportedByNote = true;
3528 }
3529 }
3530 }
3531
3532 // If our client can't cope with us being out of date, we can't cope with
3533 // our dependency being missing.
3534 unsigned Capabilities = ClientLoadCapabilities;
3535 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3536 Capabilities &= ~ARR_Missing;
3537
3538 // Load the AST file.
3539 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3540 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3541 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3542
3543 // Check the AST we just read from ImportedFile contains a different
3544 // module than we expected (ImportedName). This can occur for C++20
3545 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3546 if (IsImportingStdCXXModule) {
3547 if (const auto *Imported =
3548 getModuleManager().lookupByFileName(FileName: ImportedFile);
3549 Imported != nullptr && Imported->ModuleName != ImportedName) {
3550 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3551 Result = Missing;
3552 }
3553 }
3554
3555 // If we diagnosed a problem, produce a backtrace.
3556 bool recompilingFinalized = Result == OutOfDate &&
3557 (Capabilities & ARR_OutOfDate) &&
3558 getModuleManager()
3559 .getModuleCache()
3560 .getInMemoryModuleCache()
3561 .isPCMFinal(Filename: F.FileName);
3562 if (!IgnoreImportedByNote &&
3563 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3564 Diag(DiagID: diag::note_module_file_imported_by)
3565 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3566
3567 switch (Result) {
3568 case Failure: return Failure;
3569 // If we have to ignore the dependency, we'll have to ignore this too.
3570 case Missing:
3571 case OutOfDate: return OutOfDate;
3572 case VersionMismatch: return VersionMismatch;
3573 case ConfigurationMismatch: return ConfigurationMismatch;
3574 case HadErrors: return HadErrors;
3575 case Success: break;
3576 }
3577 break;
3578 }
3579
3580 case ORIGINAL_FILE:
3581 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3582 F.ActualOriginalSourceFileName = std::string(Blob);
3583 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3584 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3585 break;
3586
3587 case ORIGINAL_FILE_ID:
3588 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3589 break;
3590
3591 case MODULE_NAME:
3592 F.ModuleName = std::string(Blob);
3593 Diag(DiagID: diag::remark_module_import)
3594 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3595 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3596 if (Listener)
3597 Listener->ReadModuleName(ModuleName: F.ModuleName);
3598
3599 // Validate the AST as soon as we have a name so we can exit early on
3600 // failure.
3601 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3602 return Result;
3603
3604 break;
3605
3606 case MODULE_DIRECTORY: {
3607 // Save the BaseDirectory as written in the PCM for computing the module
3608 // filename for the ModuleCache.
3609 BaseDirectoryAsWritten = Blob;
3610 assert(!F.ModuleName.empty() &&
3611 "MODULE_DIRECTORY found before MODULE_NAME");
3612 F.BaseDirectory = std::string(Blob);
3613
3614 auto [MaybeM, IgnoreError] =
3615 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3616 if (!MaybeM.has_value())
3617 break;
3618
3619 Module *M = MaybeM.value();
3620 if (!M || !M->Directory)
3621 break;
3622 if (IgnoreError) {
3623 F.BaseDirectory = std::string(M->Directory->getName());
3624 break;
3625 }
3626 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3627 break;
3628
3629 // If we're implicitly loading a module, the base directory can't
3630 // change between the build and use.
3631 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3632 if (BuildDir && (*BuildDir == M->Directory)) {
3633 F.BaseDirectory = std::string(M->Directory->getName());
3634 break;
3635 }
3636 Diag(DiagID: diag::remark_module_relocated)
3637 << F.ModuleName << Blob << M->Directory->getName();
3638
3639 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3640 Diag(DiagID: diag::err_imported_module_relocated)
3641 << F.ModuleName << Blob << M->Directory->getName();
3642 return OutOfDate;
3643 }
3644
3645 case MODULE_MAP_FILE:
3646 if (ASTReadResult Result =
3647 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3648 return Result;
3649 break;
3650
3651 case INPUT_FILE_OFFSETS:
3652 NumInputs = Record[0];
3653 NumUserInputs = Record[1];
3654 F.InputFileOffsets =
3655 (const llvm::support::unaligned_uint64_t *)Blob.data();
3656 F.InputFilesLoaded.resize(new_size: NumInputs);
3657 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3658 F.NumUserInputFiles = NumUserInputs;
3659 break;
3660 }
3661 }
3662}
3663
3664llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3665 unsigned ClientLoadCapabilities) {
3666 BitstreamCursor &Stream = F.Stream;
3667
3668 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3669 return Err;
3670 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3671
3672 // Read all of the records and blocks for the AST file.
3673 RecordData Record;
3674 while (true) {
3675 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3676 if (!MaybeEntry)
3677 return MaybeEntry.takeError();
3678 llvm::BitstreamEntry Entry = MaybeEntry.get();
3679
3680 switch (Entry.Kind) {
3681 case llvm::BitstreamEntry::Error:
3682 return llvm::createStringError(
3683 EC: std::errc::illegal_byte_sequence,
3684 Fmt: "error at end of module block in AST file");
3685 case llvm::BitstreamEntry::EndBlock:
3686 // Outside of C++, we do not store a lookup map for the translation unit.
3687 // Instead, mark it as needing a lookup map to be built if this module
3688 // contains any declarations lexically within it (which it always does!).
3689 // This usually has no cost, since we very rarely need the lookup map for
3690 // the translation unit outside C++.
3691 if (ASTContext *Ctx = ContextObj) {
3692 DeclContext *DC = Ctx->getTranslationUnitDecl();
3693 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3694 DC->setMustBuildLookupTable();
3695 }
3696
3697 return llvm::Error::success();
3698 case llvm::BitstreamEntry::SubBlock:
3699 switch (Entry.ID) {
3700 case DECLTYPES_BLOCK_ID:
3701 // We lazily load the decls block, but we want to set up the
3702 // DeclsCursor cursor to point into it. Clone our current bitcode
3703 // cursor to it, enter the block and read the abbrevs in that block.
3704 // With the main cursor, we just skip over it.
3705 F.DeclsCursor = Stream;
3706 if (llvm::Error Err = Stream.SkipBlock())
3707 return Err;
3708 if (llvm::Error Err = ReadBlockAbbrevs(
3709 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3710 return Err;
3711 break;
3712
3713 case PREPROCESSOR_BLOCK_ID:
3714 F.MacroCursor = Stream;
3715 if (!PP.getExternalSource())
3716 PP.setExternalSource(this);
3717
3718 if (llvm::Error Err = Stream.SkipBlock())
3719 return Err;
3720 if (llvm::Error Err =
3721 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3722 return Err;
3723 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3724 break;
3725
3726 case PREPROCESSOR_DETAIL_BLOCK_ID:
3727 F.PreprocessorDetailCursor = Stream;
3728
3729 if (llvm::Error Err = Stream.SkipBlock()) {
3730 return Err;
3731 }
3732 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3733 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3734 return Err;
3735 F.PreprocessorDetailStartOffset
3736 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3737
3738 if (!PP.getPreprocessingRecord())
3739 PP.createPreprocessingRecord();
3740 if (!PP.getPreprocessingRecord()->getExternalSource())
3741 PP.getPreprocessingRecord()->SetExternalSource(*this);
3742 break;
3743
3744 case SOURCE_MANAGER_BLOCK_ID:
3745 if (llvm::Error Err = ReadSourceManagerBlock(F))
3746 return Err;
3747 break;
3748
3749 case SUBMODULE_BLOCK_ID:
3750 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3751 return Err;
3752 break;
3753
3754 case COMMENTS_BLOCK_ID: {
3755 BitstreamCursor C = Stream;
3756
3757 if (llvm::Error Err = Stream.SkipBlock())
3758 return Err;
3759 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3760 return Err;
3761 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3762 break;
3763 }
3764
3765 default:
3766 if (llvm::Error Err = Stream.SkipBlock())
3767 return Err;
3768 break;
3769 }
3770 continue;
3771
3772 case llvm::BitstreamEntry::Record:
3773 // The interesting case.
3774 break;
3775 }
3776
3777 // Read and process a record.
3778 Record.clear();
3779 StringRef Blob;
3780 Expected<unsigned> MaybeRecordType =
3781 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3782 if (!MaybeRecordType)
3783 return MaybeRecordType.takeError();
3784 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3785
3786 // If we're not loading an AST context, we don't care about most records.
3787 if (!ContextObj) {
3788 switch (RecordType) {
3789 case IDENTIFIER_TABLE:
3790 case IDENTIFIER_OFFSET:
3791 case INTERESTING_IDENTIFIERS:
3792 case STATISTICS:
3793 case PP_ASSUME_NONNULL_LOC:
3794 case PP_CONDITIONAL_STACK:
3795 case PP_COUNTER_VALUE:
3796 case SOURCE_LOCATION_OFFSETS:
3797 case MODULE_OFFSET_MAP:
3798 case SOURCE_MANAGER_LINE_TABLE:
3799 case PPD_ENTITIES_OFFSETS:
3800 case HEADER_SEARCH_TABLE:
3801 case IMPORTED_MODULES:
3802 case MACRO_OFFSET:
3803 break;
3804 default:
3805 continue;
3806 }
3807 }
3808
3809 switch (RecordType) {
3810 default: // Default behavior: ignore.
3811 break;
3812
3813 case TYPE_OFFSET: {
3814 if (F.LocalNumTypes != 0)
3815 return llvm::createStringError(
3816 EC: std::errc::illegal_byte_sequence,
3817 Fmt: "duplicate TYPE_OFFSET record in AST file");
3818 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3819 F.LocalNumTypes = Record[0];
3820 F.BaseTypeIndex = getTotalNumTypes();
3821
3822 if (F.LocalNumTypes > 0)
3823 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3824
3825 break;
3826 }
3827
3828 case DECL_OFFSET: {
3829 if (F.LocalNumDecls != 0)
3830 return llvm::createStringError(
3831 EC: std::errc::illegal_byte_sequence,
3832 Fmt: "duplicate DECL_OFFSET record in AST file");
3833 F.DeclOffsets = (const DeclOffset *)Blob.data();
3834 F.LocalNumDecls = Record[0];
3835 F.BaseDeclIndex = getTotalNumDecls();
3836
3837 if (F.LocalNumDecls > 0)
3838 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3839
3840 break;
3841 }
3842
3843 case TU_UPDATE_LEXICAL: {
3844 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3845 LexicalContents Contents(
3846 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3847 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3848 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3849 TU->setHasExternalLexicalStorage(true);
3850 break;
3851 }
3852
3853 case UPDATE_VISIBLE: {
3854 unsigned Idx = 0;
3855 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3856 auto *Data = (const unsigned char*)Blob.data();
3857 PendingVisibleUpdates[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 UPDATE_MODULE_LOCAL_VISIBLE: {
3867 unsigned Idx = 0;
3868 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3869 auto *Data = (const unsigned char *)Blob.data();
3870 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3871 // If we've already loaded the decl, perform the updates when we finish
3872 // loading this block.
3873 if (Decl *D = GetExistingDecl(ID))
3874 PendingUpdateRecords.push_back(
3875 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3876 break;
3877 }
3878
3879 case UPDATE_TU_LOCAL_VISIBLE: {
3880 if (F.Kind != MK_MainFile)
3881 break;
3882 unsigned Idx = 0;
3883 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3884 auto *Data = (const unsigned char *)Blob.data();
3885 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3886 // If we've already loaded the decl, perform the updates when we finish
3887 // loading this block.
3888 if (Decl *D = GetExistingDecl(ID))
3889 PendingUpdateRecords.push_back(
3890 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3891 break;
3892 }
3893
3894 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3895 unsigned Idx = 0;
3896 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3897 auto *Data = (const unsigned char *)Blob.data();
3898 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3899 // If we've already loaded the decl, perform the updates when we finish
3900 // loading this block.
3901 if (Decl *D = GetExistingDecl(ID))
3902 PendingUpdateRecords.push_back(
3903 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3904 break;
3905 }
3906
3907 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3908 unsigned Idx = 0;
3909 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3910 auto *Data = (const unsigned char *)Blob.data();
3911 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3912 // If we've already loaded the decl, perform the updates when we finish
3913 // loading this block.
3914 if (Decl *D = GetExistingDecl(ID))
3915 PendingUpdateRecords.push_back(
3916 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3917 break;
3918 }
3919
3920 case IDENTIFIER_TABLE:
3921 F.IdentifierTableData =
3922 reinterpret_cast<const unsigned char *>(Blob.data());
3923 if (Record[0]) {
3924 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3925 Buckets: F.IdentifierTableData + Record[0],
3926 Payload: F.IdentifierTableData + sizeof(uint32_t),
3927 Base: F.IdentifierTableData,
3928 InfoObj: ASTIdentifierLookupTrait(*this, F));
3929
3930 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3931 }
3932 break;
3933
3934 case IDENTIFIER_OFFSET: {
3935 if (F.LocalNumIdentifiers != 0)
3936 return llvm::createStringError(
3937 EC: std::errc::illegal_byte_sequence,
3938 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3939 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3940 F.LocalNumIdentifiers = Record[0];
3941 F.BaseIdentifierID = getTotalNumIdentifiers();
3942
3943 if (F.LocalNumIdentifiers > 0)
3944 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3945 + F.LocalNumIdentifiers);
3946 break;
3947 }
3948
3949 case INTERESTING_IDENTIFIERS:
3950 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3951 break;
3952
3953 case EAGERLY_DESERIALIZED_DECLS:
3954 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3955 // about "interesting" decls (for instance, if we're building a module).
3956 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3957 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3958 break;
3959
3960 case MODULAR_CODEGEN_DECLS:
3961 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3962 // them (ie: if we're not codegenerating this module).
3963 if (F.Kind == MK_MainFile ||
3964 getContext().getLangOpts().BuildingPCHWithObjectFile)
3965 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3966 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3967 break;
3968
3969 case SPECIAL_TYPES:
3970 if (SpecialTypes.empty()) {
3971 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3972 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3973 break;
3974 }
3975
3976 if (Record.empty())
3977 break;
3978
3979 if (SpecialTypes.size() != Record.size())
3980 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3981 Fmt: "invalid special-types record");
3982
3983 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3984 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3985 if (!SpecialTypes[I])
3986 SpecialTypes[I] = ID;
3987 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3988 // merge step?
3989 }
3990 break;
3991
3992 case STATISTICS:
3993 TotalNumStatements += Record[0];
3994 TotalNumMacros += Record[1];
3995 TotalLexicalDeclContexts += Record[2];
3996 TotalVisibleDeclContexts += Record[3];
3997 TotalModuleLocalVisibleDeclContexts += Record[4];
3998 TotalTULocalVisibleDeclContexts += Record[5];
3999 break;
4000
4001 case UNUSED_FILESCOPED_DECLS:
4002 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4003 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4004 break;
4005
4006 case DELEGATING_CTORS:
4007 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4008 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4009 break;
4010
4011 case WEAK_UNDECLARED_IDENTIFIERS:
4012 if (Record.size() % 3 != 0)
4013 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4014 Fmt: "invalid weak identifiers record");
4015
4016 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4017 // files. This isn't the way to do it :)
4018 WeakUndeclaredIdentifiers.clear();
4019
4020 // Translate the weak, undeclared identifiers into global IDs.
4021 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4022 WeakUndeclaredIdentifiers.push_back(
4023 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4024 WeakUndeclaredIdentifiers.push_back(
4025 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4026 WeakUndeclaredIdentifiers.push_back(
4027 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4028 }
4029 break;
4030
4031 case SELECTOR_OFFSETS: {
4032 F.SelectorOffsets = (const uint32_t *)Blob.data();
4033 F.LocalNumSelectors = Record[0];
4034 unsigned LocalBaseSelectorID = Record[1];
4035 F.BaseSelectorID = getTotalNumSelectors();
4036
4037 if (F.LocalNumSelectors > 0) {
4038 // Introduce the global -> local mapping for selectors within this
4039 // module.
4040 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4041
4042 // Introduce the local -> global mapping for selectors within this
4043 // module.
4044 F.SelectorRemap.insertOrReplace(
4045 Val: std::make_pair(x&: LocalBaseSelectorID,
4046 y: F.BaseSelectorID - LocalBaseSelectorID));
4047
4048 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4049 }
4050 break;
4051 }
4052
4053 case METHOD_POOL:
4054 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4055 if (Record[0])
4056 F.SelectorLookupTable
4057 = ASTSelectorLookupTable::Create(
4058 Buckets: F.SelectorLookupTableData + Record[0],
4059 Base: F.SelectorLookupTableData,
4060 InfoObj: ASTSelectorLookupTrait(*this, F));
4061 TotalNumMethodPoolEntries += Record[1];
4062 break;
4063
4064 case REFERENCED_SELECTOR_POOL:
4065 if (!Record.empty()) {
4066 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4067 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4068 LocalID: Record[Idx++]));
4069 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4070 getRawEncoding());
4071 }
4072 }
4073 break;
4074
4075 case PP_ASSUME_NONNULL_LOC: {
4076 unsigned Idx = 0;
4077 if (!Record.empty())
4078 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4079 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4080 break;
4081 }
4082
4083 case PP_UNSAFE_BUFFER_USAGE: {
4084 if (!Record.empty()) {
4085 SmallVector<SourceLocation, 64> SrcLocs;
4086 unsigned Idx = 0;
4087 while (Idx < Record.size())
4088 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4089 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4090 }
4091 break;
4092 }
4093
4094 case PP_CONDITIONAL_STACK:
4095 if (!Record.empty()) {
4096 unsigned Idx = 0, End = Record.size() - 1;
4097 bool ReachedEOFWhileSkipping = Record[Idx++];
4098 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4099 if (ReachedEOFWhileSkipping) {
4100 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4101 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4102 bool FoundNonSkipPortion = Record[Idx++];
4103 bool FoundElse = Record[Idx++];
4104 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4105 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4106 args&: FoundElse, args&: ElseLoc);
4107 }
4108 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4109 while (Idx < End) {
4110 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4111 bool WasSkipping = Record[Idx++];
4112 bool FoundNonSkip = Record[Idx++];
4113 bool FoundElse = Record[Idx++];
4114 ConditionalStack.push_back(
4115 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4116 }
4117 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4118 }
4119 break;
4120
4121 case PP_COUNTER_VALUE:
4122 if (!Record.empty() && Listener)
4123 Listener->ReadCounter(M: F, Value: Record[0]);
4124 break;
4125
4126 case FILE_SORTED_DECLS:
4127 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4128 F.NumFileSortedDecls = Record[0];
4129 break;
4130
4131 case SOURCE_LOCATION_OFFSETS: {
4132 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4133 F.LocalNumSLocEntries = Record[0];
4134 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4135 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4136 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4137 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4138 TotalSize: SLocSpaceSize);
4139 if (!F.SLocEntryBaseID) {
4140 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4141 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4142 return llvm::createStringError(EC: std::errc::invalid_argument,
4143 Fmt: "ran out of source locations");
4144 }
4145 // Make our entry in the range map. BaseID is negative and growing, so
4146 // we invert it. Because we invert it, though, we need the other end of
4147 // the range.
4148 unsigned RangeStart =
4149 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4150 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4151 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4152
4153 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4154 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4155 GlobalSLocOffsetMap.insert(
4156 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4157 - SLocSpaceSize,y: &F));
4158
4159 TotalNumSLocEntries += F.LocalNumSLocEntries;
4160 break;
4161 }
4162
4163 case MODULE_OFFSET_MAP:
4164 F.ModuleOffsetMap = Blob;
4165 break;
4166
4167 case SOURCE_MANAGER_LINE_TABLE:
4168 ParseLineTable(F, Record);
4169 break;
4170
4171 case EXT_VECTOR_DECLS:
4172 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4173 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4174 break;
4175
4176 case VTABLE_USES:
4177 if (Record.size() % 3 != 0)
4178 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4179 Fmt: "Invalid VTABLE_USES record");
4180
4181 // Later tables overwrite earlier ones.
4182 // FIXME: Modules will have some trouble with this. This is clearly not
4183 // the right way to do this.
4184 VTableUses.clear();
4185
4186 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4187 VTableUses.push_back(
4188 Elt: {.ID: ReadDeclID(F, Record, Idx),
4189 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4190 .Used: (bool)Record[Idx++]});
4191 }
4192 break;
4193
4194 case PENDING_IMPLICIT_INSTANTIATIONS:
4195
4196 if (Record.size() % 2 != 0)
4197 return llvm::createStringError(
4198 EC: std::errc::illegal_byte_sequence,
4199 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4200
4201 // For standard C++20 module, we will only reads the instantiations
4202 // if it is the main file.
4203 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4204 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4205 PendingInstantiations.push_back(
4206 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4207 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4208 }
4209 }
4210 break;
4211
4212 case SEMA_DECL_REFS:
4213 if (Record.size() != 3)
4214 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4215 Fmt: "Invalid SEMA_DECL_REFS block");
4216 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4217 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4218 break;
4219
4220 case PPD_ENTITIES_OFFSETS: {
4221 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4222 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4223 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4224
4225 unsigned StartingID;
4226 if (!PP.getPreprocessingRecord())
4227 PP.createPreprocessingRecord();
4228 if (!PP.getPreprocessingRecord()->getExternalSource())
4229 PP.getPreprocessingRecord()->SetExternalSource(*this);
4230 StartingID
4231 = PP.getPreprocessingRecord()
4232 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4233 F.BasePreprocessedEntityID = StartingID;
4234
4235 if (F.NumPreprocessedEntities > 0) {
4236 // Introduce the global -> local mapping for preprocessed entities in
4237 // this module.
4238 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4239 }
4240
4241 break;
4242 }
4243
4244 case PPD_SKIPPED_RANGES: {
4245 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4246 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4247 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4248
4249 if (!PP.getPreprocessingRecord())
4250 PP.createPreprocessingRecord();
4251 if (!PP.getPreprocessingRecord()->getExternalSource())
4252 PP.getPreprocessingRecord()->SetExternalSource(*this);
4253 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4254 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4255
4256 if (F.NumPreprocessedSkippedRanges > 0)
4257 GlobalSkippedRangeMap.insert(
4258 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4259 break;
4260 }
4261
4262 case DECL_UPDATE_OFFSETS:
4263 if (Record.size() % 2 != 0)
4264 return llvm::createStringError(
4265 EC: std::errc::illegal_byte_sequence,
4266 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4267 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4268 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4269 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4270
4271 // If we've already loaded the decl, perform the updates when we finish
4272 // loading this block.
4273 if (Decl *D = GetExistingDecl(ID))
4274 PendingUpdateRecords.push_back(
4275 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4276 }
4277 break;
4278
4279 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4280 if (Record.size() % 5 != 0)
4281 return llvm::createStringError(
4282 EC: std::errc::illegal_byte_sequence,
4283 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4284 "file");
4285 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4286 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4287
4288 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4289 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4290 uint64_t LocalLexicalOffset = Record[I++];
4291 uint64_t LexicalOffset =
4292 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4293 uint64_t LocalVisibleOffset = Record[I++];
4294 uint64_t VisibleOffset =
4295 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4296 uint64_t LocalModuleLocalOffset = Record[I++];
4297 uint64_t ModuleLocalOffset =
4298 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4299 uint64_t TULocalLocalOffset = Record[I++];
4300 uint64_t TULocalOffset =
4301 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4302
4303 DelayedNamespaceOffsetMap[ID] = {
4304 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4305
4306 assert(!GetExistingDecl(ID) &&
4307 "We shouldn't load the namespace in the front of delayed "
4308 "namespace lexical and visible block");
4309 }
4310 break;
4311 }
4312
4313 case RELATED_DECLS_MAP:
4314 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4315 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4316 auto &RelatedDecls = RelatedDeclsMap[ID];
4317 unsigned NN = Record[I++];
4318 RelatedDecls.reserve(N: NN);
4319 for (unsigned II = 0; II < NN; II++)
4320 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4321 }
4322 break;
4323
4324 case OBJC_CATEGORIES_MAP:
4325 if (F.LocalNumObjCCategoriesInMap != 0)
4326 return llvm::createStringError(
4327 EC: std::errc::illegal_byte_sequence,
4328 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4329
4330 F.LocalNumObjCCategoriesInMap = Record[0];
4331 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4332 break;
4333
4334 case OBJC_CATEGORIES:
4335 F.ObjCCategories.swap(RHS&: Record);
4336 break;
4337
4338 case CUDA_SPECIAL_DECL_REFS:
4339 // Later tables overwrite earlier ones.
4340 // FIXME: Modules will have trouble with this.
4341 CUDASpecialDeclRefs.clear();
4342 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4343 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4344 break;
4345
4346 case HEADER_SEARCH_TABLE:
4347 F.HeaderFileInfoTableData = Blob.data();
4348 F.LocalNumHeaderFileInfos = Record[1];
4349 if (Record[0]) {
4350 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4351 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4352 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4353 InfoObj: HeaderFileInfoTrait(*this, F));
4354
4355 PP.getHeaderSearchInfo().SetExternalSource(this);
4356 if (!PP.getHeaderSearchInfo().getExternalLookup())
4357 PP.getHeaderSearchInfo().SetExternalLookup(this);
4358 }
4359 break;
4360
4361 case FP_PRAGMA_OPTIONS:
4362 // Later tables overwrite earlier ones.
4363 FPPragmaOptions.swap(RHS&: Record);
4364 break;
4365
4366 case DECLS_WITH_EFFECTS_TO_VERIFY:
4367 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4368 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4369 break;
4370
4371 case OPENCL_EXTENSIONS:
4372 for (unsigned I = 0, E = Record.size(); I != E; ) {
4373 auto Name = ReadString(Record, Idx&: I);
4374 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4375 OptInfo.Supported = Record[I++] != 0;
4376 OptInfo.Enabled = Record[I++] != 0;
4377 OptInfo.WithPragma = Record[I++] != 0;
4378 OptInfo.Avail = Record[I++];
4379 OptInfo.Core = Record[I++];
4380 OptInfo.Opt = Record[I++];
4381 }
4382 break;
4383
4384 case TENTATIVE_DEFINITIONS:
4385 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4386 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4387 break;
4388
4389 case KNOWN_NAMESPACES:
4390 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4391 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4392 break;
4393
4394 case UNDEFINED_BUT_USED:
4395 if (Record.size() % 2 != 0)
4396 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4397 Fmt: "invalid undefined-but-used record");
4398 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4399 UndefinedButUsed.push_back(
4400 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4401 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4402 }
4403 break;
4404
4405 case DELETE_EXPRS_TO_ANALYZE:
4406 for (unsigned I = 0, N = Record.size(); I != N;) {
4407 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4408 const uint64_t Count = Record[I++];
4409 DelayedDeleteExprs.push_back(Elt: Count);
4410 for (uint64_t C = 0; C < Count; ++C) {
4411 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4412 bool IsArrayForm = Record[I++] == 1;
4413 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4414 }
4415 }
4416 break;
4417
4418 case VTABLES_TO_EMIT:
4419 if (F.Kind == MK_MainFile ||
4420 getContext().getLangOpts().BuildingPCHWithObjectFile)
4421 for (unsigned I = 0, N = Record.size(); I != N;)
4422 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4423 break;
4424
4425 case IMPORTED_MODULES:
4426 if (!F.isModule()) {
4427 // If we aren't loading a module (which has its own exports), make
4428 // all of the imported modules visible.
4429 // FIXME: Deal with macros-only imports.
4430 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4431 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4432 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4433 if (GlobalID) {
4434 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4435 if (DeserializationListener)
4436 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4437 }
4438 }
4439 }
4440 break;
4441
4442 case MACRO_OFFSET: {
4443 if (F.LocalNumMacros != 0)
4444 return llvm::createStringError(
4445 EC: std::errc::illegal_byte_sequence,
4446 Fmt: "duplicate MACRO_OFFSET record in AST file");
4447 F.MacroOffsets = (const uint32_t *)Blob.data();
4448 F.LocalNumMacros = Record[0];
4449 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4450 F.BaseMacroID = getTotalNumMacros();
4451
4452 if (F.LocalNumMacros > 0)
4453 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4454 break;
4455 }
4456
4457 case LATE_PARSED_TEMPLATE:
4458 LateParsedTemplates.emplace_back(
4459 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4460 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4461 break;
4462
4463 case OPTIMIZE_PRAGMA_OPTIONS:
4464 if (Record.size() != 1)
4465 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4466 Fmt: "invalid pragma optimize record");
4467 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4468 break;
4469
4470 case MSSTRUCT_PRAGMA_OPTIONS:
4471 if (Record.size() != 1)
4472 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4473 Fmt: "invalid pragma ms_struct record");
4474 PragmaMSStructState = Record[0];
4475 break;
4476
4477 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4478 if (Record.size() != 2)
4479 return llvm::createStringError(
4480 EC: std::errc::illegal_byte_sequence,
4481 Fmt: "invalid pragma pointers to members record");
4482 PragmaMSPointersToMembersState = Record[0];
4483 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4484 break;
4485
4486 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4487 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4488 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4489 break;
4490
4491 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4492 if (Record.size() != 1)
4493 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4494 Fmt: "invalid cuda pragma options record");
4495 ForceHostDeviceDepth = Record[0];
4496 break;
4497
4498 case ALIGN_PACK_PRAGMA_OPTIONS: {
4499 if (Record.size() < 3)
4500 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4501 Fmt: "invalid pragma pack record");
4502 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4503 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4504 unsigned NumStackEntries = Record[2];
4505 unsigned Idx = 3;
4506 // Reset the stack when importing a new module.
4507 PragmaAlignPackStack.clear();
4508 for (unsigned I = 0; I < NumStackEntries; ++I) {
4509 PragmaAlignPackStackEntry Entry;
4510 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4511 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4512 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4513 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4514 Entry.SlotLabel = PragmaAlignPackStrings.back();
4515 PragmaAlignPackStack.push_back(Elt: Entry);
4516 }
4517 break;
4518 }
4519
4520 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4521 if (Record.size() < 3)
4522 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4523 Fmt: "invalid pragma float control record");
4524 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4525 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4526 unsigned NumStackEntries = Record[2];
4527 unsigned Idx = 3;
4528 // Reset the stack when importing a new module.
4529 FpPragmaStack.clear();
4530 for (unsigned I = 0; I < NumStackEntries; ++I) {
4531 FpPragmaStackEntry Entry;
4532 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4533 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4534 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4535 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4536 Entry.SlotLabel = FpPragmaStrings.back();
4537 FpPragmaStack.push_back(Elt: Entry);
4538 }
4539 break;
4540 }
4541
4542 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4543 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4544 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4545 break;
4546
4547 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4548 unsigned NumRecords = Record.front();
4549 // Last record which is used to keep number of valid records.
4550 if (Record.size() - 1 != NumRecords)
4551 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4552 Fmt: "invalid rvv intrinsic pragma record");
4553
4554 if (RISCVVecIntrinsicPragma.empty())
4555 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4556 // There might be multiple precompiled modules imported, we need to union
4557 // them all.
4558 for (unsigned i = 0; i < NumRecords; ++i)
4559 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4560 break;
4561 }
4562 }
4563 }
4564}
4565
4566void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4567 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4568
4569 // Additional remapping information.
4570 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4571 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4572 F.ModuleOffsetMap = StringRef();
4573
4574 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4575 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4576 RemapBuilder SelectorRemap(F.SelectorRemap);
4577
4578 auto &ImportedModuleVector = F.TransitiveImports;
4579 assert(ImportedModuleVector.empty());
4580
4581 while (Data < DataEnd) {
4582 // FIXME: Looking up dependency modules by filename is horrible. Let's
4583 // start fixing this with prebuilt, explicit and implicit modules and see
4584 // how it goes...
4585 using namespace llvm::support;
4586 ModuleKind Kind = static_cast<ModuleKind>(
4587 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4588 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4589 StringRef Name = StringRef((const char*)Data, Len);
4590 Data += Len;
4591 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4592 Kind == MK_ImplicitModule
4593 ? ModuleMgr.lookupByModuleName(ModName: Name)
4594 : ModuleMgr.lookupByFileName(FileName: Name));
4595 if (!OM) {
4596 std::string Msg = "refers to unknown module, cannot find ";
4597 Msg.append(str: std::string(Name));
4598 Error(Msg);
4599 return;
4600 }
4601
4602 ImportedModuleVector.push_back(Elt: OM);
4603
4604 uint32_t SubmoduleIDOffset =
4605 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4606 uint32_t SelectorIDOffset =
4607 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4608
4609 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4610 RemapBuilder &Remap) {
4611 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4612 if (Offset != None)
4613 Remap.insert(Val: std::make_pair(x&: Offset,
4614 y: static_cast<int>(BaseOffset - Offset)));
4615 };
4616
4617 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4618 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4619 }
4620}
4621
4622ASTReader::ASTReadResult
4623ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4624 const ModuleFile *ImportedBy,
4625 unsigned ClientLoadCapabilities) {
4626 unsigned Idx = 0;
4627 F.ModuleMapPath = ReadPath(F, Record, Idx);
4628
4629 // Try to resolve ModuleName in the current header search context and
4630 // verify that it is found in the same module map file as we saved. If the
4631 // top-level AST file is a main file, skip this check because there is no
4632 // usable header search context.
4633 assert(!F.ModuleName.empty() &&
4634 "MODULE_NAME should come before MODULE_MAP_FILE");
4635 auto [MaybeM, IgnoreError] =
4636 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4637 if (MaybeM.has_value()) {
4638 // An implicitly-loaded module file should have its module listed in some
4639 // module map file that we've already loaded.
4640 Module *M = MaybeM.value();
4641 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4642 OptionalFileEntryRef ModMap =
4643 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4644 if (!IgnoreError && !ModMap) {
4645 if (M && M->Directory)
4646 Diag(DiagID: diag::remark_module_relocated)
4647 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4648
4649 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4650 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4651 // This module was defined by an imported (explicit) module.
4652 Diag(DiagID: diag::err_module_file_conflict) << F.ModuleName << F.FileName
4653 << ASTFE->getName();
4654 // TODO: Add a note with the module map paths if they differ.
4655 } else {
4656 // This module was built with a different module map.
4657 Diag(DiagID: diag::err_imported_module_not_found)
4658 << F.ModuleName << F.FileName
4659 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4660 << !ImportedBy;
4661 // In case it was imported by a PCH, there's a chance the user is
4662 // just missing to include the search path to the directory containing
4663 // the modulemap.
4664 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4665 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4666 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4667 }
4668 }
4669 return OutOfDate;
4670 }
4671
4672 assert(M && M->Name == F.ModuleName && "found module with different name");
4673
4674 // Check the primary module map file.
4675 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4676 if (!StoredModMap || *StoredModMap != ModMap) {
4677 assert(ModMap && "found module is missing module map file");
4678 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4679 "top-level import should be verified");
4680 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4681 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4682 Diag(DiagID: diag::err_imported_module_modmap_changed)
4683 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4684 << ModMap->getName() << F.ModuleMapPath << NotImported;
4685 return OutOfDate;
4686 }
4687
4688 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4689 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4690 // FIXME: we should use input files rather than storing names.
4691 std::string Filename = ReadPath(F, Record, Idx);
4692 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4693 if (!SF) {
4694 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4695 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4696 return OutOfDate;
4697 }
4698 AdditionalStoredMaps.insert(V: *SF);
4699 }
4700
4701 // Check any additional module map files (e.g. module.private.modulemap)
4702 // that are not in the pcm.
4703 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4704 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4705 // Remove files that match
4706 // Note: SmallPtrSet::erase is really remove
4707 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4708 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4709 Diag(DiagID: diag::err_module_different_modmap)
4710 << F.ModuleName << /*new*/0 << ModMap.getName();
4711 return OutOfDate;
4712 }
4713 }
4714 }
4715
4716 // Check any additional module map files that are in the pcm, but not
4717 // found in header search. Cases that match are already removed.
4718 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4719 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4720 Diag(DiagID: diag::err_module_different_modmap)
4721 << F.ModuleName << /*not new*/1 << ModMap.getName();
4722 return OutOfDate;
4723 }
4724 }
4725
4726 if (Listener)
4727 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4728 return Success;
4729}
4730
4731/// Move the given method to the back of the global list of methods.
4732static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4733 // Find the entry for this selector in the method pool.
4734 SemaObjC::GlobalMethodPool::iterator Known =
4735 S.ObjC().MethodPool.find(Val: Method->getSelector());
4736 if (Known == S.ObjC().MethodPool.end())
4737 return;
4738
4739 // Retrieve the appropriate method list.
4740 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4741 : Known->second.second;
4742 bool Found = false;
4743 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4744 if (!Found) {
4745 if (List->getMethod() == Method) {
4746 Found = true;
4747 } else {
4748 // Keep searching.
4749 continue;
4750 }
4751 }
4752
4753 if (List->getNext())
4754 List->setMethod(List->getNext()->getMethod());
4755 else
4756 List->setMethod(Method);
4757 }
4758}
4759
4760void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4761 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4762 for (Decl *D : Names) {
4763 bool wasHidden = !D->isUnconditionallyVisible();
4764 D->setVisibleDespiteOwningModule();
4765
4766 if (wasHidden && SemaObj) {
4767 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4768 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4769 }
4770 }
4771 }
4772}
4773
4774void ASTReader::makeModuleVisible(Module *Mod,
4775 Module::NameVisibilityKind NameVisibility,
4776 SourceLocation ImportLoc) {
4777 llvm::SmallPtrSet<Module *, 4> Visited;
4778 SmallVector<Module *, 4> Stack;
4779 Stack.push_back(Elt: Mod);
4780 while (!Stack.empty()) {
4781 Mod = Stack.pop_back_val();
4782
4783 if (NameVisibility <= Mod->NameVisibility) {
4784 // This module already has this level of visibility (or greater), so
4785 // there is nothing more to do.
4786 continue;
4787 }
4788
4789 if (Mod->isUnimportable()) {
4790 // Modules that aren't importable cannot be made visible.
4791 continue;
4792 }
4793
4794 // Update the module's name visibility.
4795 Mod->NameVisibility = NameVisibility;
4796
4797 // If we've already deserialized any names from this module,
4798 // mark them as visible.
4799 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4800 if (Hidden != HiddenNamesMap.end()) {
4801 auto HiddenNames = std::move(*Hidden);
4802 HiddenNamesMap.erase(I: Hidden);
4803 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4804 assert(!HiddenNamesMap.contains(Mod) &&
4805 "making names visible added hidden names");
4806 }
4807
4808 // Push any exported modules onto the stack to be marked as visible.
4809 SmallVector<Module *, 16> Exports;
4810 Mod->getExportedModules(Exported&: Exports);
4811 for (SmallVectorImpl<Module *>::iterator
4812 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4813 Module *Exported = *I;
4814 if (Visited.insert(Ptr: Exported).second)
4815 Stack.push_back(Elt: Exported);
4816 }
4817 }
4818}
4819
4820/// We've merged the definition \p MergedDef into the existing definition
4821/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4822/// visible.
4823void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4824 NamedDecl *MergedDef) {
4825 if (!Def->isUnconditionallyVisible()) {
4826 // If MergedDef is visible or becomes visible, make the definition visible.
4827 if (MergedDef->isUnconditionallyVisible())
4828 Def->setVisibleDespiteOwningModule();
4829 else {
4830 getContext().mergeDefinitionIntoModule(
4831 ND: Def, M: MergedDef->getImportedOwningModule(),
4832 /*NotifyListeners*/ false);
4833 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4834 }
4835 }
4836}
4837
4838bool ASTReader::loadGlobalIndex() {
4839 if (GlobalIndex)
4840 return false;
4841
4842 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4843 !PP.getLangOpts().Modules)
4844 return true;
4845
4846 // Try to load the global index.
4847 TriedLoadingGlobalIndex = true;
4848 StringRef SpecificModuleCachePath =
4849 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4850 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4851 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4852 if (llvm::Error Err = std::move(Result.second)) {
4853 assert(!Result.first);
4854 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4855 return true;
4856 }
4857
4858 GlobalIndex.reset(p: Result.first);
4859 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4860 return false;
4861}
4862
4863bool ASTReader::isGlobalIndexUnavailable() const {
4864 return PP.getLangOpts().Modules && UseGlobalIndex &&
4865 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4866}
4867
4868/// Given a cursor at the start of an AST file, scan ahead and drop the
4869/// cursor into the start of the given block ID, returning false on success and
4870/// true on failure.
4871static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4872 while (true) {
4873 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4874 if (!MaybeEntry) {
4875 // FIXME this drops errors on the floor.
4876 consumeError(Err: MaybeEntry.takeError());
4877 return true;
4878 }
4879 llvm::BitstreamEntry Entry = MaybeEntry.get();
4880
4881 switch (Entry.Kind) {
4882 case llvm::BitstreamEntry::Error:
4883 case llvm::BitstreamEntry::EndBlock:
4884 return true;
4885
4886 case llvm::BitstreamEntry::Record:
4887 // Ignore top-level records.
4888 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4889 break;
4890 else {
4891 // FIXME this drops errors on the floor.
4892 consumeError(Err: Skipped.takeError());
4893 return true;
4894 }
4895
4896 case llvm::BitstreamEntry::SubBlock:
4897 if (Entry.ID == BlockID) {
4898 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4899 // FIXME this drops the error on the floor.
4900 consumeError(Err: std::move(Err));
4901 return true;
4902 }
4903 // Found it!
4904 return false;
4905 }
4906
4907 if (llvm::Error Err = Cursor.SkipBlock()) {
4908 // FIXME this drops the error on the floor.
4909 consumeError(Err: std::move(Err));
4910 return true;
4911 }
4912 }
4913 }
4914}
4915
4916ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4917 SourceLocation ImportLoc,
4918 unsigned ClientLoadCapabilities,
4919 ModuleFile **NewLoadedModuleFile) {
4920 llvm::TimeTraceScope scope("ReadAST", FileName);
4921
4922 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4923 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4924 CurrentDeserializingModuleKind, Type);
4925
4926 // Defer any pending actions until we get to the end of reading the AST file.
4927 Deserializing AnASTFile(this);
4928
4929 // Bump the generation number.
4930 unsigned PreviousGeneration = 0;
4931 if (ContextObj)
4932 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4933
4934 unsigned NumModules = ModuleMgr.size();
4935 SmallVector<ImportedModule, 4> Loaded;
4936 if (ASTReadResult ReadResult =
4937 ReadASTCore(FileName, Type, ImportLoc,
4938 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4939 ClientLoadCapabilities)) {
4940 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4941
4942 // If we find that any modules are unusable, the global index is going
4943 // to be out-of-date. Just remove it.
4944 GlobalIndex.reset();
4945 ModuleMgr.setGlobalIndex(nullptr);
4946 return ReadResult;
4947 }
4948
4949 if (NewLoadedModuleFile && !Loaded.empty())
4950 *NewLoadedModuleFile = Loaded.back().Mod;
4951
4952 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4953 // remove modules from this point. Various fields are updated during reading
4954 // the AST block and removing the modules would result in dangling pointers.
4955 // They are generally only incidentally dereferenced, ie. a binary search
4956 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4957 // be dereferenced but it wouldn't actually be used.
4958
4959 // Load the AST blocks of all of the modules that we loaded. We can still
4960 // hit errors parsing the ASTs at this point.
4961 for (ImportedModule &M : Loaded) {
4962 ModuleFile &F = *M.Mod;
4963 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4964
4965 // Read the AST block.
4966 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4967 Error(Err: std::move(Err));
4968 return Failure;
4969 }
4970
4971 // The AST block should always have a definition for the main module.
4972 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4973 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
4974 return Failure;
4975 }
4976
4977 // Read the extension blocks.
4978 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
4979 if (llvm::Error Err = ReadExtensionBlock(F)) {
4980 Error(Err: std::move(Err));
4981 return Failure;
4982 }
4983 }
4984
4985 // Once read, set the ModuleFile bit base offset and update the size in
4986 // bits of all files we've seen.
4987 F.GlobalBitOffset = TotalModulesSizeInBits;
4988 TotalModulesSizeInBits += F.SizeInBits;
4989 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
4990 }
4991
4992 // Preload source locations and interesting indentifiers.
4993 for (ImportedModule &M : Loaded) {
4994 ModuleFile &F = *M.Mod;
4995
4996 // Map the original source file ID into the ID space of the current
4997 // compilation.
4998 if (F.OriginalSourceFileID.isValid())
4999 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5000
5001 for (auto Offset : F.PreloadIdentifierOffsets) {
5002 const unsigned char *Data = F.IdentifierTableData + Offset;
5003
5004 ASTIdentifierLookupTrait Trait(*this, F);
5005 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5006 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5007
5008 IdentifierInfo *II;
5009 if (!PP.getLangOpts().CPlusPlus) {
5010 // Identifiers present in both the module file and the importing
5011 // instance are marked out-of-date so that they can be deserialized
5012 // on next use via ASTReader::updateOutOfDateIdentifier().
5013 // Identifiers present in the module file but not in the importing
5014 // instance are ignored for now, preventing growth of the identifier
5015 // table. They will be deserialized on first use via ASTReader::get().
5016 auto It = PP.getIdentifierTable().find(Name: Key);
5017 if (It == PP.getIdentifierTable().end())
5018 continue;
5019 II = It->second;
5020 } else {
5021 // With C++ modules, not many identifiers are considered interesting.
5022 // All identifiers in the module file can be placed into the identifier
5023 // table of the importing instance and marked as out-of-date. This makes
5024 // ASTReader::get() a no-op, and deserialization will take place on
5025 // first/next use via ASTReader::updateOutOfDateIdentifier().
5026 II = &PP.getIdentifierTable().getOwn(Name: Key);
5027 }
5028
5029 II->setOutOfDate(true);
5030
5031 // Mark this identifier as being from an AST file so that we can track
5032 // whether we need to serialize it.
5033 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5034
5035 // Associate the ID with the identifier so that the writer can reuse it.
5036 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5037 SetIdentifierInfo(ID, II);
5038 }
5039 }
5040
5041 // Builtins and library builtins have already been initialized. Mark all
5042 // identifiers as out-of-date, so that they are deserialized on first use.
5043 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5044 for (auto &Id : PP.getIdentifierTable())
5045 Id.second->setOutOfDate(true);
5046
5047 // Mark selectors as out of date.
5048 for (const auto &Sel : SelectorGeneration)
5049 SelectorOutOfDate[Sel.first] = true;
5050
5051 // Setup the import locations and notify the module manager that we've
5052 // committed to these module files.
5053 for (ImportedModule &M : Loaded) {
5054 ModuleFile &F = *M.Mod;
5055
5056 ModuleMgr.moduleFileAccepted(MF: &F);
5057
5058 // Set the import location.
5059 F.DirectImportLoc = ImportLoc;
5060 // FIXME: We assume that locations from PCH / preamble do not need
5061 // any translation.
5062 if (!M.ImportedBy)
5063 F.ImportLoc = M.ImportLoc;
5064 else
5065 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5066 }
5067
5068 // Resolve any unresolved module exports.
5069 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
5070 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
5071 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
5072 Module *ResolvedMod = getSubmodule(GlobalID);
5073
5074 switch (Unresolved.Kind) {
5075 case UnresolvedModuleRef::Conflict:
5076 if (ResolvedMod) {
5077 Module::Conflict Conflict;
5078 Conflict.Other = ResolvedMod;
5079 Conflict.Message = Unresolved.String.str();
5080 Unresolved.Mod->Conflicts.push_back(x: Conflict);
5081 }
5082 continue;
5083
5084 case UnresolvedModuleRef::Import:
5085 if (ResolvedMod)
5086 Unresolved.Mod->Imports.insert(X: ResolvedMod);
5087 continue;
5088
5089 case UnresolvedModuleRef::Affecting:
5090 if (ResolvedMod)
5091 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
5092 continue;
5093
5094 case UnresolvedModuleRef::Export:
5095 if (ResolvedMod || Unresolved.IsWildcard)
5096 Unresolved.Mod->Exports.push_back(
5097 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
5098 continue;
5099 }
5100 }
5101 UnresolvedModuleRefs.clear();
5102
5103 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5104 // Might be unnecessary as use declarations are only used to build the
5105 // module itself.
5106
5107 if (ContextObj)
5108 InitializeContext();
5109
5110 if (SemaObj)
5111 UpdateSema();
5112
5113 if (DeserializationListener)
5114 DeserializationListener->ReaderInitialized(Reader: this);
5115
5116 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5117 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5118 // If this AST file is a precompiled preamble, then set the
5119 // preamble file ID of the source manager to the file source file
5120 // from which the preamble was built.
5121 if (Type == MK_Preamble) {
5122 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5123 } else if (Type == MK_MainFile) {
5124 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5125 }
5126 }
5127
5128 // For any Objective-C class definitions we have already loaded, make sure
5129 // that we load any additional categories.
5130 if (ContextObj) {
5131 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5132 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5133 D: ObjCClassesLoaded[I], PreviousGeneration);
5134 }
5135 }
5136
5137 const HeaderSearchOptions &HSOpts =
5138 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5139 if (HSOpts.ModulesValidateOncePerBuildSession) {
5140 // Now we are certain that the module and all modules it depends on are
5141 // up-to-date. For implicitly-built module files, ensure the corresponding
5142 // timestamp files are up-to-date in this build session.
5143 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5144 ImportedModule &M = Loaded[I];
5145 if (M.Mod->Kind == MK_ImplicitModule &&
5146 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5147 getModuleManager().getModuleCache().updateModuleTimestamp(
5148 ModuleFilename: M.Mod->FileName);
5149 }
5150 }
5151
5152 return Success;
5153}
5154
5155static ASTFileSignature readASTFileSignature(StringRef PCH);
5156
5157/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5158static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5159 // FIXME checking magic headers is done in other places such as
5160 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5161 // always done the same. Unify it all with a helper.
5162 if (!Stream.canSkipToPos(pos: 4))
5163 return llvm::createStringError(
5164 EC: std::errc::illegal_byte_sequence,
5165 Fmt: "file too small to contain precompiled file magic");
5166 for (unsigned C : {'C', 'P', 'C', 'H'})
5167 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5168 if (Res.get() != C)
5169 return llvm::createStringError(
5170 EC: std::errc::illegal_byte_sequence,
5171 Fmt: "file doesn't start with precompiled file magic");
5172 } else
5173 return Res.takeError();
5174 return llvm::Error::success();
5175}
5176
5177static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5178 switch (Kind) {
5179 case MK_PCH:
5180 return 0; // PCH
5181 case MK_ImplicitModule:
5182 case MK_ExplicitModule:
5183 case MK_PrebuiltModule:
5184 return 1; // module
5185 case MK_MainFile:
5186 case MK_Preamble:
5187 return 2; // main source file
5188 }
5189 llvm_unreachable("unknown module kind");
5190}
5191
5192ASTReader::ASTReadResult
5193ASTReader::ReadASTCore(StringRef FileName,
5194 ModuleKind Type,
5195 SourceLocation ImportLoc,
5196 ModuleFile *ImportedBy,
5197 SmallVectorImpl<ImportedModule> &Loaded,
5198 off_t ExpectedSize, time_t ExpectedModTime,
5199 ASTFileSignature ExpectedSignature,
5200 unsigned ClientLoadCapabilities) {
5201 ModuleFile *M;
5202 std::string ErrorStr;
5203 ModuleManager::AddModuleResult AddResult
5204 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
5205 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
5206 ExpectedSignature, ReadSignature: readASTFileSignature,
5207 Module&: M, ErrorStr);
5208
5209 switch (AddResult) {
5210 case ModuleManager::AlreadyLoaded:
5211 Diag(DiagID: diag::remark_module_import)
5212 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5213 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5214 return Success;
5215
5216 case ModuleManager::NewlyLoaded:
5217 // Load module file below.
5218 break;
5219
5220 case ModuleManager::Missing:
5221 // The module file was missing; if the client can handle that, return
5222 // it.
5223 if (ClientLoadCapabilities & ARR_Missing)
5224 return Missing;
5225
5226 // Otherwise, return an error.
5227 Diag(DiagID: diag::err_ast_file_not_found)
5228 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5229 << ErrorStr;
5230 return Failure;
5231
5232 case ModuleManager::OutOfDate:
5233 // We couldn't load the module file because it is out-of-date. If the
5234 // client can handle out-of-date, return it.
5235 if (ClientLoadCapabilities & ARR_OutOfDate)
5236 return OutOfDate;
5237
5238 // Otherwise, return an error.
5239 Diag(DiagID: diag::err_ast_file_out_of_date)
5240 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5241 << ErrorStr;
5242 return Failure;
5243 }
5244
5245 assert(M && "Missing module file");
5246
5247 bool ShouldFinalizePCM = false;
5248 llvm::scope_exit FinalizeOrDropPCM([&]() {
5249 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5250 if (ShouldFinalizePCM)
5251 MC.finalizePCM(Filename: FileName);
5252 else
5253 MC.tryToDropPCM(Filename: FileName);
5254 });
5255 ModuleFile &F = *M;
5256 BitstreamCursor &Stream = F.Stream;
5257 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5258 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5259
5260 // Sniff for the signature.
5261 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5262 Diag(DiagID: diag::err_ast_file_invalid)
5263 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5264 return Failure;
5265 }
5266
5267 // This is used for compatibility with older PCH formats.
5268 bool HaveReadControlBlock = false;
5269 while (true) {
5270 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5271 if (!MaybeEntry) {
5272 Error(Err: MaybeEntry.takeError());
5273 return Failure;
5274 }
5275 llvm::BitstreamEntry Entry = MaybeEntry.get();
5276
5277 switch (Entry.Kind) {
5278 case llvm::BitstreamEntry::Error:
5279 case llvm::BitstreamEntry::Record:
5280 case llvm::BitstreamEntry::EndBlock:
5281 Error(Msg: "invalid record at top-level of AST file");
5282 return Failure;
5283
5284 case llvm::BitstreamEntry::SubBlock:
5285 break;
5286 }
5287
5288 switch (Entry.ID) {
5289 case CONTROL_BLOCK_ID:
5290 HaveReadControlBlock = true;
5291 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5292 case Success:
5293 // Check that we didn't try to load a non-module AST file as a module.
5294 //
5295 // FIXME: Should we also perform the converse check? Loading a module as
5296 // a PCH file sort of works, but it's a bit wonky.
5297 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5298 Type == MK_PrebuiltModule) &&
5299 F.ModuleName.empty()) {
5300 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5301 if (Result != OutOfDate ||
5302 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5303 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5304 return Result;
5305 }
5306 break;
5307
5308 case Failure: return Failure;
5309 case Missing: return Missing;
5310 case OutOfDate: return OutOfDate;
5311 case VersionMismatch: return VersionMismatch;
5312 case ConfigurationMismatch: return ConfigurationMismatch;
5313 case HadErrors: return HadErrors;
5314 }
5315 break;
5316
5317 case AST_BLOCK_ID:
5318 if (!HaveReadControlBlock) {
5319 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5320 Diag(DiagID: diag::err_ast_file_version_too_old)
5321 << moduleKindForDiagnostic(Kind: Type) << FileName;
5322 return VersionMismatch;
5323 }
5324
5325 // Record that we've loaded this module.
5326 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5327 ShouldFinalizePCM = true;
5328 return Success;
5329
5330 default:
5331 if (llvm::Error Err = Stream.SkipBlock()) {
5332 Error(Err: std::move(Err));
5333 return Failure;
5334 }
5335 break;
5336 }
5337 }
5338
5339 llvm_unreachable("unexpected break; expected return");
5340}
5341
5342ASTReader::ASTReadResult
5343ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5344 unsigned ClientLoadCapabilities) {
5345 const HeaderSearchOptions &HSOpts =
5346 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5347 bool AllowCompatibleConfigurationMismatch =
5348 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5349 bool DisableValidation = shouldDisableValidationForFile(M: F);
5350
5351 ASTReadResult Result = readUnhashedControlBlockImpl(
5352 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5353 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5354 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5355
5356 // If F was directly imported by another module, it's implicitly validated by
5357 // the importing module.
5358 if (DisableValidation || WasImportedBy ||
5359 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5360 return Success;
5361
5362 if (Result == Failure) {
5363 Error(Msg: "malformed block record in AST file");
5364 return Failure;
5365 }
5366
5367 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5368 // If this module has already been finalized in the ModuleCache, we're stuck
5369 // with it; we can only load a single version of each module.
5370 //
5371 // This can happen when a module is imported in two contexts: in one, as a
5372 // user module; in another, as a system module (due to an import from
5373 // another module marked with the [system] flag). It usually indicates a
5374 // bug in the module map: this module should also be marked with [system].
5375 //
5376 // If -Wno-system-headers (the default), and the first import is as a
5377 // system module, then validation will fail during the as-user import,
5378 // since -Werror flags won't have been validated. However, it's reasonable
5379 // to treat this consistently as a system module.
5380 //
5381 // If -Wsystem-headers, the PCM on disk was built with
5382 // -Wno-system-headers, and the first import is as a user module, then
5383 // validation will fail during the as-system import since the PCM on disk
5384 // doesn't guarantee that -Werror was respected. However, the -Werror
5385 // flags were checked during the initial as-user import.
5386 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5387 Filename: F.FileName)) {
5388 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5389 return Success;
5390 }
5391 }
5392
5393 return Result;
5394}
5395
5396ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5397 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5398 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5399 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5400 // Initialize a stream.
5401 BitstreamCursor Stream(StreamData);
5402
5403 // Sniff for the signature.
5404 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5405 // FIXME this drops the error on the floor.
5406 consumeError(Err: std::move(Err));
5407 return Failure;
5408 }
5409
5410 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5411 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5412 return Failure;
5413
5414 // Read all of the records in the options block.
5415 RecordData Record;
5416 ASTReadResult Result = Success;
5417 while (true) {
5418 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5419 if (!MaybeEntry) {
5420 // FIXME this drops the error on the floor.
5421 consumeError(Err: MaybeEntry.takeError());
5422 return Failure;
5423 }
5424 llvm::BitstreamEntry Entry = MaybeEntry.get();
5425
5426 switch (Entry.Kind) {
5427 case llvm::BitstreamEntry::Error:
5428 case llvm::BitstreamEntry::SubBlock:
5429 return Failure;
5430
5431 case llvm::BitstreamEntry::EndBlock:
5432 return Result;
5433
5434 case llvm::BitstreamEntry::Record:
5435 // The interesting case.
5436 break;
5437 }
5438
5439 // Read and process a record.
5440 Record.clear();
5441 StringRef Blob;
5442 Expected<unsigned> MaybeRecordType =
5443 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5444 if (!MaybeRecordType) {
5445 // FIXME this drops the error.
5446 return Failure;
5447 }
5448 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5449 case SIGNATURE:
5450 if (F) {
5451 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5452 assert(F->Signature != ASTFileSignature::createDummy() &&
5453 "Dummy AST file signature not backpatched in ASTWriter.");
5454 }
5455 break;
5456 case AST_BLOCK_HASH:
5457 if (F) {
5458 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5459 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5460 "Dummy AST block hash not backpatched in ASTWriter.");
5461 }
5462 break;
5463 case DIAGNOSTIC_OPTIONS: {
5464 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5465 if (Listener && ValidateDiagnosticOptions &&
5466 !AllowCompatibleConfigurationMismatch &&
5467 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5468 Result = OutOfDate; // Don't return early. Read the signature.
5469 break;
5470 }
5471 case HEADER_SEARCH_PATHS: {
5472 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5473 if (Listener && !AllowCompatibleConfigurationMismatch &&
5474 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5475 Result = ConfigurationMismatch;
5476 break;
5477 }
5478 case DIAG_PRAGMA_MAPPINGS:
5479 if (!F)
5480 break;
5481 if (F->PragmaDiagMappings.empty())
5482 F->PragmaDiagMappings.swap(RHS&: Record);
5483 else
5484 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5485 From: Record.begin(), To: Record.end());
5486 break;
5487 case HEADER_SEARCH_ENTRY_USAGE:
5488 if (F)
5489 F->SearchPathUsage = ReadBitVector(Record, Blob);
5490 break;
5491 case VFS_USAGE:
5492 if (F)
5493 F->VFSUsage = ReadBitVector(Record, Blob);
5494 break;
5495 }
5496 }
5497}
5498
5499/// Parse a record and blob containing module file extension metadata.
5500static bool parseModuleFileExtensionMetadata(
5501 const SmallVectorImpl<uint64_t> &Record,
5502 StringRef Blob,
5503 ModuleFileExtensionMetadata &Metadata) {
5504 if (Record.size() < 4) return true;
5505
5506 Metadata.MajorVersion = Record[0];
5507 Metadata.MinorVersion = Record[1];
5508
5509 unsigned BlockNameLen = Record[2];
5510 unsigned UserInfoLen = Record[3];
5511
5512 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5513
5514 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5515 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5516 Blob.data() + BlockNameLen + UserInfoLen);
5517 return false;
5518}
5519
5520llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5521 BitstreamCursor &Stream = F.Stream;
5522
5523 RecordData Record;
5524 while (true) {
5525 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5526 if (!MaybeEntry)
5527 return MaybeEntry.takeError();
5528 llvm::BitstreamEntry Entry = MaybeEntry.get();
5529
5530 switch (Entry.Kind) {
5531 case llvm::BitstreamEntry::SubBlock:
5532 if (llvm::Error Err = Stream.SkipBlock())
5533 return Err;
5534 continue;
5535 case llvm::BitstreamEntry::EndBlock:
5536 return llvm::Error::success();
5537 case llvm::BitstreamEntry::Error:
5538 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5539 Fmt: "malformed block record in AST file");
5540 case llvm::BitstreamEntry::Record:
5541 break;
5542 }
5543
5544 Record.clear();
5545 StringRef Blob;
5546 Expected<unsigned> MaybeRecCode =
5547 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5548 if (!MaybeRecCode)
5549 return MaybeRecCode.takeError();
5550 switch (MaybeRecCode.get()) {
5551 case EXTENSION_METADATA: {
5552 ModuleFileExtensionMetadata Metadata;
5553 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5554 return llvm::createStringError(
5555 EC: std::errc::illegal_byte_sequence,
5556 Fmt: "malformed EXTENSION_METADATA in AST file");
5557
5558 // Find a module file extension with this block name.
5559 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5560 if (Known == ModuleFileExtensions.end()) break;
5561
5562 // Form a reader.
5563 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5564 Mod&: F, Stream)) {
5565 F.ExtensionReaders.push_back(x: std::move(Reader));
5566 }
5567
5568 break;
5569 }
5570 }
5571 }
5572
5573 llvm_unreachable("ReadExtensionBlock should return from while loop");
5574}
5575
5576void ASTReader::InitializeContext() {
5577 assert(ContextObj && "no context to initialize");
5578 ASTContext &Context = *ContextObj;
5579
5580 // If there's a listener, notify them that we "read" the translation unit.
5581 if (DeserializationListener)
5582 DeserializationListener->DeclRead(
5583 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5584 D: Context.getTranslationUnitDecl());
5585
5586 // FIXME: Find a better way to deal with collisions between these
5587 // built-in types. Right now, we just ignore the problem.
5588
5589 // Load the special types.
5590 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5591 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5592 if (!Context.CFConstantStringTypeDecl)
5593 Context.setCFConstantStringType(GetType(ID: String));
5594 }
5595
5596 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5597 QualType FileType = GetType(ID: File);
5598 if (FileType.isNull()) {
5599 Error(Msg: "FILE type is NULL");
5600 return;
5601 }
5602
5603 if (!Context.FILEDecl) {
5604 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5605 Context.setFILEDecl(Typedef->getDecl());
5606 else {
5607 const TagType *Tag = FileType->getAs<TagType>();
5608 if (!Tag) {
5609 Error(Msg: "Invalid FILE type in AST file");
5610 return;
5611 }
5612 Context.setFILEDecl(Tag->getDecl());
5613 }
5614 }
5615 }
5616
5617 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5618 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5619 if (Jmp_bufType.isNull()) {
5620 Error(Msg: "jmp_buf type is NULL");
5621 return;
5622 }
5623
5624 if (!Context.jmp_bufDecl) {
5625 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5626 Context.setjmp_bufDecl(Typedef->getDecl());
5627 else {
5628 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5629 if (!Tag) {
5630 Error(Msg: "Invalid jmp_buf type in AST file");
5631 return;
5632 }
5633 Context.setjmp_bufDecl(Tag->getDecl());
5634 }
5635 }
5636 }
5637
5638 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5639 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5640 if (Sigjmp_bufType.isNull()) {
5641 Error(Msg: "sigjmp_buf type is NULL");
5642 return;
5643 }
5644
5645 if (!Context.sigjmp_bufDecl) {
5646 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5647 Context.setsigjmp_bufDecl(Typedef->getDecl());
5648 else {
5649 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5650 assert(Tag && "Invalid sigjmp_buf type in AST file");
5651 Context.setsigjmp_bufDecl(Tag->getDecl());
5652 }
5653 }
5654 }
5655
5656 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5657 if (Context.ObjCIdRedefinitionType.isNull())
5658 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5659 }
5660
5661 if (TypeID ObjCClassRedef =
5662 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5663 if (Context.ObjCClassRedefinitionType.isNull())
5664 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5665 }
5666
5667 if (TypeID ObjCSelRedef =
5668 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5669 if (Context.ObjCSelRedefinitionType.isNull())
5670 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5671 }
5672
5673 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5674 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5675 if (Ucontext_tType.isNull()) {
5676 Error(Msg: "ucontext_t type is NULL");
5677 return;
5678 }
5679
5680 if (!Context.ucontext_tDecl) {
5681 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5682 Context.setucontext_tDecl(Typedef->getDecl());
5683 else {
5684 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5685 assert(Tag && "Invalid ucontext_t type in AST file");
5686 Context.setucontext_tDecl(Tag->getDecl());
5687 }
5688 }
5689 }
5690 }
5691
5692 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5693
5694 // If there were any CUDA special declarations, deserialize them.
5695 if (!CUDASpecialDeclRefs.empty()) {
5696 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5697 Context.setcudaConfigureCallDecl(
5698 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5699 Context.setcudaGetParameterBufferDecl(
5700 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5701 Context.setcudaLaunchDeviceDecl(
5702 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5703 }
5704
5705 // Re-export any modules that were imported by a non-module AST file.
5706 // FIXME: This does not make macro-only imports visible again.
5707 for (auto &Import : PendingImportedModules) {
5708 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5709 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5710 /*ImportLoc=*/Import.ImportLoc);
5711 if (Import.ImportLoc.isValid())
5712 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5713 // This updates visibility for Preprocessor only. For Sema, which can be
5714 // nullptr here, we do the same later, in UpdateSema().
5715 }
5716 }
5717
5718 // Hand off these modules to Sema.
5719 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5720 PendingImportedModules.clear();
5721}
5722
5723void ASTReader::finalizeForWriting() {
5724 // Nothing to do for now.
5725}
5726
5727/// Reads and return the signature record from \p PCH's control block, or
5728/// else returns 0.
5729static ASTFileSignature readASTFileSignature(StringRef PCH) {
5730 BitstreamCursor Stream(PCH);
5731 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5732 // FIXME this drops the error on the floor.
5733 consumeError(Err: std::move(Err));
5734 return ASTFileSignature();
5735 }
5736
5737 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5738 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5739 return ASTFileSignature();
5740
5741 // Scan for SIGNATURE inside the diagnostic options block.
5742 ASTReader::RecordData Record;
5743 while (true) {
5744 Expected<llvm::BitstreamEntry> MaybeEntry =
5745 Stream.advanceSkippingSubblocks();
5746 if (!MaybeEntry) {
5747 // FIXME this drops the error on the floor.
5748 consumeError(Err: MaybeEntry.takeError());
5749 return ASTFileSignature();
5750 }
5751 llvm::BitstreamEntry Entry = MaybeEntry.get();
5752
5753 if (Entry.Kind != llvm::BitstreamEntry::Record)
5754 return ASTFileSignature();
5755
5756 Record.clear();
5757 StringRef Blob;
5758 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5759 if (!MaybeRecord) {
5760 // FIXME this drops the error on the floor.
5761 consumeError(Err: MaybeRecord.takeError());
5762 return ASTFileSignature();
5763 }
5764 if (SIGNATURE == MaybeRecord.get()) {
5765 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5766 assert(Signature != ASTFileSignature::createDummy() &&
5767 "Dummy AST file signature not backpatched in ASTWriter.");
5768 return Signature;
5769 }
5770 }
5771}
5772
5773/// Retrieve the name of the original source file name
5774/// directly from the AST file, without actually loading the AST
5775/// file.
5776std::string ASTReader::getOriginalSourceFile(
5777 const std::string &ASTFileName, FileManager &FileMgr,
5778 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5779 // Open the AST file.
5780 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5781 /*RequiresNullTerminator=*/false,
5782 /*MaybeLimit=*/std::nullopt,
5783 /*IsText=*/false);
5784 if (!Buffer) {
5785 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5786 << ASTFileName << Buffer.getError().message();
5787 return std::string();
5788 }
5789
5790 // Initialize the stream
5791 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5792
5793 // Sniff for the signature.
5794 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5795 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5796 return std::string();
5797 }
5798
5799 // Scan for the CONTROL_BLOCK_ID block.
5800 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5801 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5802 return std::string();
5803 }
5804
5805 // Scan for ORIGINAL_FILE inside the control block.
5806 RecordData Record;
5807 while (true) {
5808 Expected<llvm::BitstreamEntry> MaybeEntry =
5809 Stream.advanceSkippingSubblocks();
5810 if (!MaybeEntry) {
5811 // FIXME this drops errors on the floor.
5812 consumeError(Err: MaybeEntry.takeError());
5813 return std::string();
5814 }
5815 llvm::BitstreamEntry Entry = MaybeEntry.get();
5816
5817 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5818 return std::string();
5819
5820 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5821 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5822 return std::string();
5823 }
5824
5825 Record.clear();
5826 StringRef Blob;
5827 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5828 if (!MaybeRecord) {
5829 // FIXME this drops the errors on the floor.
5830 consumeError(Err: MaybeRecord.takeError());
5831 return std::string();
5832 }
5833 if (ORIGINAL_FILE == MaybeRecord.get())
5834 return Blob.str();
5835 }
5836}
5837
5838namespace {
5839
5840 class SimplePCHValidator : public ASTReaderListener {
5841 const LangOptions &ExistingLangOpts;
5842 const CodeGenOptions &ExistingCGOpts;
5843 const TargetOptions &ExistingTargetOpts;
5844 const PreprocessorOptions &ExistingPPOpts;
5845 const HeaderSearchOptions &ExistingHSOpts;
5846 std::string ExistingSpecificModuleCachePath;
5847 FileManager &FileMgr;
5848 bool StrictOptionMatches;
5849
5850 public:
5851 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5852 const CodeGenOptions &ExistingCGOpts,
5853 const TargetOptions &ExistingTargetOpts,
5854 const PreprocessorOptions &ExistingPPOpts,
5855 const HeaderSearchOptions &ExistingHSOpts,
5856 StringRef ExistingSpecificModuleCachePath,
5857 FileManager &FileMgr, bool StrictOptionMatches)
5858 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5859 ExistingTargetOpts(ExistingTargetOpts),
5860 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5861 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5862 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5863
5864 bool ReadLanguageOptions(const LangOptions &LangOpts,
5865 StringRef ModuleFilename, bool Complain,
5866 bool AllowCompatibleDifferences) override {
5867 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5868 Diags: nullptr, AllowCompatibleDifferences);
5869 }
5870
5871 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5872 StringRef ModuleFilename, bool Complain,
5873 bool AllowCompatibleDifferences) override {
5874 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5875 Diags: nullptr, AllowCompatibleDifferences);
5876 }
5877
5878 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5879 StringRef ModuleFilename, bool Complain,
5880 bool AllowCompatibleDifferences) override {
5881 return checkTargetOptions(TargetOpts: ExistingTargetOpts, ExistingTargetOpts: TargetOpts, ModuleFilename,
5882 Diags: nullptr, AllowCompatibleDifferences);
5883 }
5884
5885 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5886 StringRef ASTFilename, StringRef ContextHash,
5887 bool Complain) override {
5888 return checkModuleCachePath(
5889 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
5890 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5891 }
5892
5893 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5894 StringRef ModuleFilename, bool ReadMacros,
5895 bool Complain,
5896 std::string &SuggestedPredefines) override {
5897 return checkPreprocessorOptions(
5898 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5899 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5900 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5901 : OptionValidateContradictions);
5902 }
5903 };
5904
5905} // namespace
5906
5907bool ASTReader::readASTFileControlBlock(
5908 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5909 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5910 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5911 unsigned ClientLoadCapabilities) {
5912 // Open the AST file.
5913 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5914 llvm::MemoryBuffer *Buffer =
5915 ModCache.getInMemoryModuleCache().lookupPCM(Filename);
5916 if (!Buffer) {
5917 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5918 // read again later, but we do not have the context here to determine if it
5919 // is safe to change the result of InMemoryModuleCache::getPCMState().
5920
5921 // FIXME: This allows use of the VFS; we do not allow use of the
5922 // VFS when actually loading a module.
5923 auto Entry =
5924 Filename == "-" ? FileMgr.getSTDIN() : FileMgr.getFileRef(Filename);
5925 if (!Entry) {
5926 llvm::consumeError(Err: Entry.takeError());
5927 return true;
5928 }
5929 auto BufferOrErr = FileMgr.getBufferForFile(Entry: *Entry);
5930 if (!BufferOrErr)
5931 return true;
5932 OwnedBuffer = std::move(*BufferOrErr);
5933 Buffer = OwnedBuffer.get();
5934 }
5935
5936 // Initialize the stream
5937 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5938 BitstreamCursor Stream(Bytes);
5939
5940 // Sniff for the signature.
5941 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5942 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5943 return true;
5944 }
5945
5946 // Scan for the CONTROL_BLOCK_ID block.
5947 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5948 return true;
5949
5950 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5951 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5952 bool NeedsImports = Listener.needsImportVisitation();
5953 BitstreamCursor InputFilesCursor;
5954 uint64_t InputFilesOffsetBase = 0;
5955
5956 RecordData Record;
5957 std::string ModuleDir;
5958 bool DoneWithControlBlock = false;
5959 SmallString<0> PathBuf;
5960 PathBuf.reserve(N: 256);
5961 // Additional path buffer to use when multiple paths need to be resolved.
5962 // For example, when deserializing input files that contains a path that was
5963 // resolved from a vfs overlay and an external location.
5964 SmallString<0> AdditionalPathBuf;
5965 AdditionalPathBuf.reserve(N: 256);
5966 while (!DoneWithControlBlock) {
5967 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5968 if (!MaybeEntry) {
5969 // FIXME this drops the error on the floor.
5970 consumeError(Err: MaybeEntry.takeError());
5971 return true;
5972 }
5973 llvm::BitstreamEntry Entry = MaybeEntry.get();
5974
5975 switch (Entry.Kind) {
5976 case llvm::BitstreamEntry::SubBlock: {
5977 switch (Entry.ID) {
5978 case OPTIONS_BLOCK_ID: {
5979 std::string IgnoredSuggestedPredefines;
5980 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
5981 /*AllowCompatibleConfigurationMismatch*/ false,
5982 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
5983 return true;
5984 break;
5985 }
5986
5987 case INPUT_FILES_BLOCK_ID:
5988 InputFilesCursor = Stream;
5989 if (llvm::Error Err = Stream.SkipBlock()) {
5990 // FIXME this drops the error on the floor.
5991 consumeError(Err: std::move(Err));
5992 return true;
5993 }
5994 if (NeedsInputFiles &&
5995 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
5996 return true;
5997 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5998 break;
5999
6000 default:
6001 if (llvm::Error Err = Stream.SkipBlock()) {
6002 // FIXME this drops the error on the floor.
6003 consumeError(Err: std::move(Err));
6004 return true;
6005 }
6006 break;
6007 }
6008
6009 continue;
6010 }
6011
6012 case llvm::BitstreamEntry::EndBlock:
6013 DoneWithControlBlock = true;
6014 break;
6015
6016 case llvm::BitstreamEntry::Error:
6017 return true;
6018
6019 case llvm::BitstreamEntry::Record:
6020 break;
6021 }
6022
6023 if (DoneWithControlBlock) break;
6024
6025 Record.clear();
6026 StringRef Blob;
6027 Expected<unsigned> MaybeRecCode =
6028 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6029 if (!MaybeRecCode) {
6030 // FIXME this drops the error.
6031 return Failure;
6032 }
6033 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6034 case METADATA:
6035 if (Record[0] != VERSION_MAJOR)
6036 return true;
6037 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6038 return true;
6039 break;
6040 case MODULE_NAME:
6041 Listener.ReadModuleName(ModuleName: Blob);
6042 break;
6043 case MODULE_DIRECTORY:
6044 ModuleDir = std::string(Blob);
6045 break;
6046 case MODULE_MAP_FILE: {
6047 unsigned Idx = 0;
6048 std::string PathStr = ReadString(Record, Idx);
6049 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6050 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6051 break;
6052 }
6053 case INPUT_FILE_OFFSETS: {
6054 if (!NeedsInputFiles)
6055 break;
6056
6057 unsigned NumInputFiles = Record[0];
6058 unsigned NumUserFiles = Record[1];
6059 const llvm::support::unaligned_uint64_t *InputFileOffs =
6060 (const llvm::support::unaligned_uint64_t *)Blob.data();
6061 for (unsigned I = 0; I != NumInputFiles; ++I) {
6062 // Go find this input file.
6063 bool isSystemFile = I >= NumUserFiles;
6064
6065 if (isSystemFile && !NeedsSystemInputFiles)
6066 break; // the rest are system input files
6067
6068 BitstreamCursor &Cursor = InputFilesCursor;
6069 SavedStreamPosition SavedPosition(Cursor);
6070 if (llvm::Error Err =
6071 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6072 // FIXME this drops errors on the floor.
6073 consumeError(Err: std::move(Err));
6074 }
6075
6076 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6077 if (!MaybeCode) {
6078 // FIXME this drops errors on the floor.
6079 consumeError(Err: MaybeCode.takeError());
6080 }
6081 unsigned Code = MaybeCode.get();
6082
6083 RecordData Record;
6084 StringRef Blob;
6085 bool shouldContinue = false;
6086 Expected<unsigned> MaybeRecordType =
6087 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6088 if (!MaybeRecordType) {
6089 // FIXME this drops errors on the floor.
6090 consumeError(Err: MaybeRecordType.takeError());
6091 }
6092 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6093 case INPUT_FILE_HASH:
6094 break;
6095 case INPUT_FILE:
6096 time_t StoredTime = static_cast<time_t>(Record[2]);
6097 bool Overridden = static_cast<bool>(Record[3]);
6098 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6099 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6100 auto FilenameAsRequestedBuf = ResolveImportedPath(
6101 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6102 StringRef Filename;
6103 if (UnresolvedFilename.empty())
6104 Filename = *FilenameAsRequestedBuf;
6105 else {
6106 auto FilenameBuf = ResolveImportedPath(
6107 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6108 Filename = *FilenameBuf;
6109 }
6110 shouldContinue = Listener.visitInputFileAsRequested(
6111 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6112 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6113 break;
6114 }
6115 if (!shouldContinue)
6116 break;
6117 }
6118 break;
6119 }
6120
6121 case IMPORT: {
6122 if (!NeedsImports)
6123 break;
6124
6125 unsigned Idx = 0;
6126 // Read information about the AST file.
6127
6128 // Skip Kind
6129 Idx++;
6130
6131 // Skip ImportLoc
6132 Idx++;
6133
6134 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6135
6136 bool IsStandardCXXModule = Record[Idx++];
6137
6138 // In C++20 Modules, we don't record the path to imported
6139 // modules in the BMI files.
6140 if (IsStandardCXXModule) {
6141 Listener.visitImport(ModuleName, /*Filename=*/"");
6142 continue;
6143 }
6144
6145 // Skip Size and ModTime.
6146 Idx += 1 + 1;
6147 // Skip signature.
6148 Blob = Blob.substr(Start: ASTFileSignature::size);
6149
6150 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6151 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6152 Listener.visitImport(ModuleName, Filename: *Filename);
6153 break;
6154 }
6155
6156 default:
6157 // No other validation to perform.
6158 break;
6159 }
6160 }
6161
6162 // Look for module file extension blocks, if requested.
6163 if (FindModuleFileExtensions) {
6164 BitstreamCursor SavedStream = Stream;
6165 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6166 bool DoneWithExtensionBlock = false;
6167 while (!DoneWithExtensionBlock) {
6168 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6169 if (!MaybeEntry) {
6170 // FIXME this drops the error.
6171 return true;
6172 }
6173 llvm::BitstreamEntry Entry = MaybeEntry.get();
6174
6175 switch (Entry.Kind) {
6176 case llvm::BitstreamEntry::SubBlock:
6177 if (llvm::Error Err = Stream.SkipBlock()) {
6178 // FIXME this drops the error on the floor.
6179 consumeError(Err: std::move(Err));
6180 return true;
6181 }
6182 continue;
6183
6184 case llvm::BitstreamEntry::EndBlock:
6185 DoneWithExtensionBlock = true;
6186 continue;
6187
6188 case llvm::BitstreamEntry::Error:
6189 return true;
6190
6191 case llvm::BitstreamEntry::Record:
6192 break;
6193 }
6194
6195 Record.clear();
6196 StringRef Blob;
6197 Expected<unsigned> MaybeRecCode =
6198 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6199 if (!MaybeRecCode) {
6200 // FIXME this drops the error.
6201 return true;
6202 }
6203 switch (MaybeRecCode.get()) {
6204 case EXTENSION_METADATA: {
6205 ModuleFileExtensionMetadata Metadata;
6206 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6207 return true;
6208
6209 Listener.readModuleFileExtension(Metadata);
6210 break;
6211 }
6212 }
6213 }
6214 }
6215 Stream = std::move(SavedStream);
6216 }
6217
6218 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6219 if (readUnhashedControlBlockImpl(
6220 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6221 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6222 ValidateDiagnosticOptions) != Success)
6223 return true;
6224
6225 return false;
6226}
6227
6228bool ASTReader::isAcceptableASTFile(
6229 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6230 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6231 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6232 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6233 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6234 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6235 SpecificModuleCachePath, FileMgr,
6236 RequireStrictOptionMatches);
6237 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6238 /*FindModuleFileExtensions=*/false, Listener&: validator,
6239 /*ValidateDiagnosticOptions=*/true);
6240}
6241
6242llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
6243 unsigned ClientLoadCapabilities) {
6244 // Enter the submodule block.
6245 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
6246 return Err;
6247
6248 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6249 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6250 // If we don't know the top-level module, there's no point in doing qualified
6251 // lookup of its submodules; it won't find anything anywhere within this tree.
6252 // Let's skip that and avoid some string lookups.
6253 auto CreateModule = !KnowsTopLevelModule
6254 ? &ModuleMap::createModule
6255 : &ModuleMap::findOrCreateModuleFirst;
6256
6257 bool First = true;
6258 Module *CurrentModule = nullptr;
6259 RecordData Record;
6260 while (true) {
6261 Expected<llvm::BitstreamEntry> MaybeEntry =
6262 F.Stream.advanceSkippingSubblocks();
6263 if (!MaybeEntry)
6264 return MaybeEntry.takeError();
6265 llvm::BitstreamEntry Entry = MaybeEntry.get();
6266
6267 switch (Entry.Kind) {
6268 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6269 case llvm::BitstreamEntry::Error:
6270 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6271 Fmt: "malformed block record in AST file");
6272 case llvm::BitstreamEntry::EndBlock:
6273 return llvm::Error::success();
6274 case llvm::BitstreamEntry::Record:
6275 // The interesting case.
6276 break;
6277 }
6278
6279 // Read a record.
6280 StringRef Blob;
6281 Record.clear();
6282 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6283 if (!MaybeKind)
6284 return MaybeKind.takeError();
6285 unsigned Kind = MaybeKind.get();
6286
6287 if ((Kind == SUBMODULE_METADATA) != First)
6288 return llvm::createStringError(
6289 EC: std::errc::illegal_byte_sequence,
6290 Fmt: "submodule metadata record should be at beginning of block");
6291 First = false;
6292
6293 // Submodule information is only valid if we have a current module.
6294 // FIXME: Should we error on these cases?
6295 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
6296 Kind != SUBMODULE_DEFINITION)
6297 continue;
6298
6299 switch (Kind) {
6300 default: // Default behavior: ignore.
6301 break;
6302
6303 case SUBMODULE_DEFINITION: {
6304 if (Record.size() < 13)
6305 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6306 Fmt: "malformed module definition");
6307
6308 StringRef Name = Blob;
6309 unsigned Idx = 0;
6310 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6311 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6312 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6313 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6314 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6315 bool IsFramework = Record[Idx++];
6316 bool IsExplicit = Record[Idx++];
6317 bool IsSystem = Record[Idx++];
6318 bool IsExternC = Record[Idx++];
6319 bool InferSubmodules = Record[Idx++];
6320 bool InferExplicitSubmodules = Record[Idx++];
6321 bool InferExportWildcard = Record[Idx++];
6322 bool ConfigMacrosExhaustive = Record[Idx++];
6323 bool ModuleMapIsPrivate = Record[Idx++];
6324 bool NamedModuleHasInit = Record[Idx++];
6325
6326 Module *ParentModule = nullptr;
6327 if (Parent)
6328 ParentModule = getSubmodule(GlobalID: Parent);
6329
6330 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6331 args&: IsFramework, args&: IsExplicit);
6332
6333 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6334 if (GlobalIndex >= SubmodulesLoaded.size() ||
6335 SubmodulesLoaded[GlobalIndex])
6336 return llvm::createStringError(EC: std::errc::invalid_argument,
6337 Fmt: "too many submodules");
6338
6339 if (!ParentModule) {
6340 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
6341 // Don't emit module relocation error if we have -fno-validate-pch
6342 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6343 DisableValidationForModuleKind::Module)) {
6344 assert(CurFile != F.File && "ModuleManager did not de-duplicate");
6345
6346 Diag(DiagID: diag::err_module_file_conflict)
6347 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
6348 << F.File.getName();
6349
6350 auto CurModMapFile =
6351 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6352 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6353 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6354 Diag(DiagID: diag::note_module_file_conflict)
6355 << CurModMapFile->getName() << ModMapFile->getName();
6356
6357 return llvm::make_error<AlreadyReportedDiagnosticError>();
6358 }
6359 }
6360
6361 F.DidReadTopLevelSubmodule = true;
6362 CurrentModule->setASTFile(F.File);
6363 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6364 }
6365
6366 CurrentModule->Kind = Kind;
6367 // Note that we may be rewriting an existing location and it is important
6368 // to keep doing that. In particular, we would like to prefer a
6369 // `DefinitionLoc` loaded from the module file instead of the location
6370 // created in the current source manager, because it allows the new
6371 // location to be marked as "unaffecting" when writing and avoid creating
6372 // duplicate locations for the same module map file.
6373 CurrentModule->DefinitionLoc = DefinitionLoc;
6374 CurrentModule->Signature = F.Signature;
6375 CurrentModule->IsFromModuleFile = true;
6376 if (InferredAllowedBy.isValid())
6377 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6378 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6379 CurrentModule->IsExternC = IsExternC;
6380 CurrentModule->InferSubmodules = InferSubmodules;
6381 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6382 CurrentModule->InferExportWildcard = InferExportWildcard;
6383 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6384 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6385 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6386
6387 if (!ParentModule && !F.BaseDirectory.empty()) {
6388 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6389 CurrentModule->Directory = *Dir;
6390 } else if (ParentModule && ParentModule->Directory) {
6391 // Submodules inherit the directory from their parent.
6392 CurrentModule->Directory = ParentModule->Directory;
6393 }
6394
6395 if (DeserializationListener)
6396 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6397
6398 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6399
6400 // Clear out data that will be replaced by what is in the module file.
6401 CurrentModule->LinkLibraries.clear();
6402 CurrentModule->ConfigMacros.clear();
6403 CurrentModule->UnresolvedConflicts.clear();
6404 CurrentModule->Conflicts.clear();
6405
6406 // The module is available unless it's missing a requirement; relevant
6407 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6408 // Missing headers that were present when the module was built do not
6409 // make it unavailable -- if we got this far, this must be an explicitly
6410 // imported module file.
6411 CurrentModule->Requirements.clear();
6412 CurrentModule->MissingHeaders.clear();
6413 CurrentModule->IsUnimportable =
6414 ParentModule && ParentModule->IsUnimportable;
6415 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6416 break;
6417 }
6418
6419 case SUBMODULE_UMBRELLA_HEADER: {
6420 SmallString<128> RelativePathName;
6421 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6422 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6423 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6424 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6425 PathRelativeToRootModuleDirectory: RelativePathName);
6426 }
6427 // Note that it's too late at this point to return out of date if the
6428 // name from the PCM doesn't match up with the one in the module map,
6429 // but also quite unlikely since we will have already checked the
6430 // modification time and size of the module map file itself.
6431 }
6432 break;
6433 }
6434
6435 case SUBMODULE_HEADER:
6436 case SUBMODULE_EXCLUDED_HEADER:
6437 case SUBMODULE_PRIVATE_HEADER:
6438 // We lazily associate headers with their modules via the HeaderInfo table.
6439 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6440 // of complete filenames or remove it entirely.
6441 break;
6442
6443 case SUBMODULE_TEXTUAL_HEADER:
6444 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6445 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6446 // them here.
6447 break;
6448
6449 case SUBMODULE_TOPHEADER: {
6450 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6451 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6452 break;
6453 }
6454
6455 case SUBMODULE_UMBRELLA_DIR: {
6456 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6457 if (auto Umbrella =
6458 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6459 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6460 // FIXME: NameAsWritten
6461 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6462 }
6463 }
6464 break;
6465 }
6466
6467 case SUBMODULE_METADATA: {
6468 F.BaseSubmoduleID = getTotalNumSubmodules();
6469 F.LocalNumSubmodules = Record[0];
6470 unsigned LocalBaseSubmoduleID = Record[1];
6471 if (F.LocalNumSubmodules > 0) {
6472 // Introduce the global -> local mapping for submodules within this
6473 // module.
6474 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
6475
6476 // Introduce the local -> global mapping for submodules within this
6477 // module.
6478 F.SubmoduleRemap.insertOrReplace(
6479 Val: std::make_pair(x&: LocalBaseSubmoduleID,
6480 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
6481
6482 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
6483 }
6484 break;
6485 }
6486
6487 case SUBMODULE_IMPORTS:
6488 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6489 UnresolvedModuleRef Unresolved;
6490 Unresolved.File = &F;
6491 Unresolved.Mod = CurrentModule;
6492 Unresolved.ID = Record[Idx];
6493 Unresolved.Kind = UnresolvedModuleRef::Import;
6494 Unresolved.IsWildcard = false;
6495 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6496 }
6497 break;
6498
6499 case SUBMODULE_AFFECTING_MODULES:
6500 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6501 UnresolvedModuleRef Unresolved;
6502 Unresolved.File = &F;
6503 Unresolved.Mod = CurrentModule;
6504 Unresolved.ID = Record[Idx];
6505 Unresolved.Kind = UnresolvedModuleRef::Affecting;
6506 Unresolved.IsWildcard = false;
6507 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6508 }
6509 break;
6510
6511 case SUBMODULE_EXPORTS:
6512 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6513 UnresolvedModuleRef Unresolved;
6514 Unresolved.File = &F;
6515 Unresolved.Mod = CurrentModule;
6516 Unresolved.ID = Record[Idx];
6517 Unresolved.Kind = UnresolvedModuleRef::Export;
6518 Unresolved.IsWildcard = Record[Idx + 1];
6519 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6520 }
6521
6522 // Once we've loaded the set of exports, there's no reason to keep
6523 // the parsed, unresolved exports around.
6524 CurrentModule->UnresolvedExports.clear();
6525 break;
6526
6527 case SUBMODULE_REQUIRES:
6528 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6529 Target: PP.getTargetInfo());
6530 break;
6531
6532 case SUBMODULE_LINK_LIBRARY:
6533 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6534 CurrentModule->LinkLibraries.push_back(
6535 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6536 break;
6537
6538 case SUBMODULE_CONFIG_MACRO:
6539 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6540 break;
6541
6542 case SUBMODULE_CONFLICT: {
6543 UnresolvedModuleRef Unresolved;
6544 Unresolved.File = &F;
6545 Unresolved.Mod = CurrentModule;
6546 Unresolved.ID = Record[0];
6547 Unresolved.Kind = UnresolvedModuleRef::Conflict;
6548 Unresolved.IsWildcard = false;
6549 Unresolved.String = Blob;
6550 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6551 break;
6552 }
6553
6554 case SUBMODULE_INITIALIZERS: {
6555 if (!ContextObj)
6556 break;
6557 // Standard C++ module has its own way to initialize variables.
6558 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6559 SmallVector<GlobalDeclID, 16> Inits;
6560 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6561 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6562 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6563 }
6564 break;
6565 }
6566
6567 case SUBMODULE_EXPORT_AS:
6568 CurrentModule->ExportAsModule = Blob.str();
6569 ModMap.addLinkAsDependency(Mod: CurrentModule);
6570 break;
6571 }
6572 }
6573}
6574
6575/// Parse the record that corresponds to a LangOptions data
6576/// structure.
6577///
6578/// This routine parses the language options from the AST file and then gives
6579/// them to the AST listener if one is set.
6580///
6581/// \returns true if the listener deems the file unacceptable, false otherwise.
6582bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6583 StringRef ModuleFilename, bool Complain,
6584 ASTReaderListener &Listener,
6585 bool AllowCompatibleDifferences) {
6586 LangOptions LangOpts;
6587 unsigned Idx = 0;
6588#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6589 LangOpts.Name = Record[Idx++];
6590#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6591 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6592#include "clang/Basic/LangOptions.def"
6593#define SANITIZER(NAME, ID) \
6594 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6595#include "clang/Basic/Sanitizers.def"
6596
6597 for (unsigned N = Record[Idx++]; N; --N)
6598 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6599
6600 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6601 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6602 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6603
6604 LangOpts.CurrentModule = ReadString(Record, Idx);
6605
6606 // Comment options.
6607 for (unsigned N = Record[Idx++]; N; --N) {
6608 LangOpts.CommentOpts.BlockCommandNames.push_back(
6609 x: ReadString(Record, Idx));
6610 }
6611 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6612
6613 // OpenMP offloading options.
6614 for (unsigned N = Record[Idx++]; N; --N) {
6615 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6616 }
6617
6618 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6619
6620 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6621 AllowCompatibleDifferences);
6622}
6623
6624bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6625 StringRef ModuleFilename, bool Complain,
6626 ASTReaderListener &Listener,
6627 bool AllowCompatibleDifferences) {
6628 unsigned Idx = 0;
6629 CodeGenOptions CGOpts;
6630 using CK = CodeGenOptions::CompatibilityKind;
6631#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6632 if constexpr (CK::Compatibility != CK::Benign) \
6633 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6634#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6635 if constexpr (CK::Compatibility != CK::Benign) \
6636 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6637#define DEBUGOPT(Name, Bits, Default, Compatibility)
6638#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6639#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6640#include "clang/Basic/CodeGenOptions.def"
6641
6642 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6643 AllowCompatibleDifferences);
6644}
6645
6646bool ASTReader::ParseTargetOptions(const RecordData &Record,
6647 StringRef ModuleFilename, bool Complain,
6648 ASTReaderListener &Listener,
6649 bool AllowCompatibleDifferences) {
6650 unsigned Idx = 0;
6651 TargetOptions TargetOpts;
6652 TargetOpts.Triple = ReadString(Record, Idx);
6653 TargetOpts.CPU = ReadString(Record, Idx);
6654 TargetOpts.TuneCPU = ReadString(Record, Idx);
6655 TargetOpts.ABI = ReadString(Record, Idx);
6656 for (unsigned N = Record[Idx++]; N; --N) {
6657 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6658 }
6659 for (unsigned N = Record[Idx++]; N; --N) {
6660 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6661 }
6662
6663 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6664 AllowCompatibleDifferences);
6665}
6666
6667bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6668 StringRef ModuleFilename, bool Complain,
6669 ASTReaderListener &Listener) {
6670 DiagnosticOptions DiagOpts;
6671 unsigned Idx = 0;
6672#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6673#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6674 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6675#include "clang/Basic/DiagnosticOptions.def"
6676
6677 for (unsigned N = Record[Idx++]; N; --N)
6678 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6679 for (unsigned N = Record[Idx++]; N; --N)
6680 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6681
6682 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6683}
6684
6685bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6686 ASTReaderListener &Listener) {
6687 FileSystemOptions FSOpts;
6688 unsigned Idx = 0;
6689 FSOpts.WorkingDir = ReadString(Record, Idx);
6690 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6691}
6692
6693bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6694 StringRef ModuleFilename,
6695 bool Complain,
6696 ASTReaderListener &Listener) {
6697 HeaderSearchOptions HSOpts;
6698 unsigned Idx = 0;
6699 HSOpts.Sysroot = ReadString(Record, Idx);
6700
6701 HSOpts.ResourceDir = ReadString(Record, Idx);
6702 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6703 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6704 HSOpts.DisableModuleHash = Record[Idx++];
6705 HSOpts.ImplicitModuleMaps = Record[Idx++];
6706 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6707 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6708 HSOpts.UseBuiltinIncludes = Record[Idx++];
6709 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6710 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6711 HSOpts.UseLibcxx = Record[Idx++];
6712 std::string ContextHash = ReadString(Record, Idx);
6713
6714 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6715 Complain);
6716}
6717
6718bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6719 ASTReaderListener &Listener) {
6720 HeaderSearchOptions HSOpts;
6721 unsigned Idx = 0;
6722
6723 // Include entries.
6724 for (unsigned N = Record[Idx++]; N; --N) {
6725 std::string Path = ReadString(Record, Idx);
6726 frontend::IncludeDirGroup Group
6727 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6728 bool IsFramework = Record[Idx++];
6729 bool IgnoreSysRoot = Record[Idx++];
6730 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6731 args&: IgnoreSysRoot);
6732 }
6733
6734 // System header prefixes.
6735 for (unsigned N = Record[Idx++]; N; --N) {
6736 std::string Prefix = ReadString(Record, Idx);
6737 bool IsSystemHeader = Record[Idx++];
6738 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6739 }
6740
6741 // VFS overlay files.
6742 for (unsigned N = Record[Idx++]; N; --N) {
6743 std::string VFSOverlayFile = ReadString(Record, Idx);
6744 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6745 }
6746
6747 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6748}
6749
6750bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6751 StringRef ModuleFilename,
6752 bool Complain,
6753 ASTReaderListener &Listener,
6754 std::string &SuggestedPredefines) {
6755 PreprocessorOptions PPOpts;
6756 unsigned Idx = 0;
6757
6758 // Macro definitions/undefs
6759 bool ReadMacros = Record[Idx++];
6760 if (ReadMacros) {
6761 for (unsigned N = Record[Idx++]; N; --N) {
6762 std::string Macro = ReadString(Record, Idx);
6763 bool IsUndef = Record[Idx++];
6764 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6765 }
6766 }
6767
6768 // Includes
6769 for (unsigned N = Record[Idx++]; N; --N) {
6770 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6771 }
6772
6773 // Macro Includes
6774 for (unsigned N = Record[Idx++]; N; --N) {
6775 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6776 }
6777
6778 PPOpts.UsePredefines = Record[Idx++];
6779 PPOpts.DetailedRecord = Record[Idx++];
6780 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6781 PPOpts.ObjCXXARCStandardLibrary =
6782 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6783 SuggestedPredefines.clear();
6784 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6785 Complain, SuggestedPredefines);
6786}
6787
6788std::pair<ModuleFile *, unsigned>
6789ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6790 GlobalPreprocessedEntityMapType::iterator
6791 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6792 assert(I != GlobalPreprocessedEntityMap.end() &&
6793 "Corrupted global preprocessed entity map");
6794 ModuleFile *M = I->second;
6795 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6796 return std::make_pair(x&: M, y&: LocalIndex);
6797}
6798
6799llvm::iterator_range<PreprocessingRecord::iterator>
6800ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6801 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6802 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6803 count: Mod.NumPreprocessedEntities);
6804
6805 return llvm::make_range(x: PreprocessingRecord::iterator(),
6806 y: PreprocessingRecord::iterator());
6807}
6808
6809bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6810 unsigned int ClientLoadCapabilities) {
6811 return ClientLoadCapabilities & ARR_OutOfDate &&
6812 !getModuleManager()
6813 .getModuleCache()
6814 .getInMemoryModuleCache()
6815 .isPCMFinal(Filename: ModuleFileName);
6816}
6817
6818llvm::iterator_range<ASTReader::ModuleDeclIterator>
6819ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6820 return llvm::make_range(
6821 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6822 y: ModuleDeclIterator(this, &Mod,
6823 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6824}
6825
6826SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6827 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6828 assert(I != GlobalSkippedRangeMap.end() &&
6829 "Corrupted global skipped range map");
6830 ModuleFile *M = I->second;
6831 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6832 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6833 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6834 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6835 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6836 assert(Range.isValid());
6837 return Range;
6838}
6839
6840unsigned
6841ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6842 unsigned ModuleFileIndex = ID >> 32;
6843 assert(ModuleFileIndex && "not translating loaded MacroID?");
6844 assert(getModuleManager().size() > ModuleFileIndex - 1);
6845 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6846
6847 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6848 return MF.BasePreprocessedEntityID + ID;
6849}
6850
6851PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6852 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6853 ModuleFile &M = *PPInfo.first;
6854 unsigned LocalIndex = PPInfo.second;
6855 PreprocessedEntityID PPID =
6856 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6857 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6858
6859 if (!PP.getPreprocessingRecord()) {
6860 Error(Msg: "no preprocessing record");
6861 return nullptr;
6862 }
6863
6864 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6865 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6866 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6867 Error(Err: std::move(Err));
6868 return nullptr;
6869 }
6870
6871 Expected<llvm::BitstreamEntry> MaybeEntry =
6872 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6873 if (!MaybeEntry) {
6874 Error(Err: MaybeEntry.takeError());
6875 return nullptr;
6876 }
6877 llvm::BitstreamEntry Entry = MaybeEntry.get();
6878
6879 if (Entry.Kind != llvm::BitstreamEntry::Record)
6880 return nullptr;
6881
6882 // Read the record.
6883 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6884 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6885 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6886 StringRef Blob;
6887 RecordData Record;
6888 Expected<unsigned> MaybeRecType =
6889 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6890 if (!MaybeRecType) {
6891 Error(Err: MaybeRecType.takeError());
6892 return nullptr;
6893 }
6894 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6895 case PPD_MACRO_EXPANSION: {
6896 bool isBuiltin = Record[0];
6897 IdentifierInfo *Name = nullptr;
6898 MacroDefinitionRecord *Def = nullptr;
6899 if (isBuiltin)
6900 Name = getLocalIdentifier(M, LocalID: Record[1]);
6901 else {
6902 PreprocessedEntityID GlobalID =
6903 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6904 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
6905 Def =
6906 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
6907 }
6908
6909 MacroExpansion *ME;
6910 if (isBuiltin)
6911 ME = new (PPRec) MacroExpansion(Name, Range);
6912 else
6913 ME = new (PPRec) MacroExpansion(Def, Range);
6914
6915 return ME;
6916 }
6917
6918 case PPD_MACRO_DEFINITION: {
6919 // Decode the identifier info and then check again; if the macro is
6920 // still defined and associated with the identifier,
6921 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6922 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6923
6924 if (DeserializationListener)
6925 DeserializationListener->MacroDefinitionRead(PPID, MD);
6926
6927 return MD;
6928 }
6929
6930 case PPD_INCLUSION_DIRECTIVE: {
6931 const char *FullFileNameStart = Blob.data() + Record[0];
6932 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6933 OptionalFileEntryRef File;
6934 if (!FullFileName.empty())
6935 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6936
6937 // FIXME: Stable encoding
6938 InclusionDirective::InclusionKind Kind
6939 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6940 InclusionDirective *ID
6941 = new (PPRec) InclusionDirective(PPRec, Kind,
6942 StringRef(Blob.data(), Record[0]),
6943 Record[1], Record[3],
6944 File,
6945 Range);
6946 return ID;
6947 }
6948 }
6949
6950 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6951}
6952
6953/// Find the next module that contains entities and return the ID
6954/// of the first entry.
6955///
6956/// \param SLocMapI points at a chunk of a module that contains no
6957/// preprocessed entities or the entities it contains are not the ones we are
6958/// looking for.
6959unsigned ASTReader::findNextPreprocessedEntity(
6960 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6961 ++SLocMapI;
6962 for (GlobalSLocOffsetMapType::const_iterator
6963 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6964 ModuleFile &M = *SLocMapI->second;
6965 if (M.NumPreprocessedEntities)
6966 return M.BasePreprocessedEntityID;
6967 }
6968
6969 return getTotalNumPreprocessedEntities();
6970}
6971
6972namespace {
6973
6974struct PPEntityComp {
6975 const ASTReader &Reader;
6976 ModuleFile &M;
6977
6978 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6979
6980 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6981 SourceLocation LHS = getLoc(PPE: L);
6982 SourceLocation RHS = getLoc(PPE: R);
6983 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6984 }
6985
6986 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6987 SourceLocation LHS = getLoc(PPE: L);
6988 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6989 }
6990
6991 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6992 SourceLocation RHS = getLoc(PPE: R);
6993 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6994 }
6995
6996 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6997 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
6998 }
6999};
7000
7001} // namespace
7002
7003unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7004 bool EndsAfter) const {
7005 if (SourceMgr.isLocalSourceLocation(Loc))
7006 return getTotalNumPreprocessedEntities();
7007
7008 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7009 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7010 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7011 "Corrupted global sloc offset map");
7012
7013 if (SLocMapI->second->NumPreprocessedEntities == 0)
7014 return findNextPreprocessedEntity(SLocMapI);
7015
7016 ModuleFile &M = *SLocMapI->second;
7017
7018 using pp_iterator = const PPEntityOffset *;
7019
7020 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7021 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7022
7023 size_t Count = M.NumPreprocessedEntities;
7024 size_t Half;
7025 pp_iterator First = pp_begin;
7026 pp_iterator PPI;
7027
7028 if (EndsAfter) {
7029 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7030 comp: PPEntityComp(*this, M));
7031 } else {
7032 // Do a binary search manually instead of using std::lower_bound because
7033 // The end locations of entities may be unordered (when a macro expansion
7034 // is inside another macro argument), but for this case it is not important
7035 // whether we get the first macro expansion or its containing macro.
7036 while (Count > 0) {
7037 Half = Count / 2;
7038 PPI = First;
7039 std::advance(i&: PPI, n: Half);
7040 if (SourceMgr.isBeforeInTranslationUnit(
7041 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7042 First = PPI;
7043 ++First;
7044 Count = Count - Half - 1;
7045 } else
7046 Count = Half;
7047 }
7048 }
7049
7050 if (PPI == pp_end)
7051 return findNextPreprocessedEntity(SLocMapI);
7052
7053 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7054}
7055
7056/// Returns a pair of [Begin, End) indices of preallocated
7057/// preprocessed entities that \arg Range encompasses.
7058std::pair<unsigned, unsigned>
7059 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7060 if (Range.isInvalid())
7061 return std::make_pair(x: 0,y: 0);
7062 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7063
7064 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7065 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7066 return std::make_pair(x&: BeginID, y&: EndID);
7067}
7068
7069/// Optionally returns true or false if the preallocated preprocessed
7070/// entity with index \arg Index came from file \arg FID.
7071std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7072 FileID FID) {
7073 if (FID.isInvalid())
7074 return false;
7075
7076 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7077 ModuleFile &M = *PPInfo.first;
7078 unsigned LocalIndex = PPInfo.second;
7079 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7080
7081 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7082 if (Loc.isInvalid())
7083 return false;
7084
7085 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7086 return true;
7087 else
7088 return false;
7089}
7090
7091namespace {
7092
7093 /// Visitor used to search for information about a header file.
7094 class HeaderFileInfoVisitor {
7095 FileEntryRef FE;
7096 std::optional<HeaderFileInfo> HFI;
7097
7098 public:
7099 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7100
7101 bool operator()(ModuleFile &M) {
7102 HeaderFileInfoLookupTable *Table
7103 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7104 if (!Table)
7105 return false;
7106
7107 // Look in the on-disk hash table for an entry for this file name.
7108 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7109 if (Pos == Table->end())
7110 return false;
7111
7112 HFI = *Pos;
7113 return true;
7114 }
7115
7116 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7117 };
7118
7119} // namespace
7120
7121HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7122 HeaderFileInfoVisitor Visitor(FE);
7123 ModuleMgr.visit(Visitor);
7124 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7125 return *HFI;
7126
7127 return HeaderFileInfo();
7128}
7129
7130void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7131 using DiagState = DiagnosticsEngine::DiagState;
7132 SmallVector<DiagState *, 32> DiagStates;
7133
7134 for (ModuleFile &F : ModuleMgr) {
7135 unsigned Idx = 0;
7136 auto &Record = F.PragmaDiagMappings;
7137 if (Record.empty())
7138 continue;
7139
7140 DiagStates.clear();
7141
7142 auto ReadDiagState = [&](const DiagState &BasedOn,
7143 bool IncludeNonPragmaStates) {
7144 unsigned BackrefID = Record[Idx++];
7145 if (BackrefID != 0)
7146 return DiagStates[BackrefID - 1];
7147
7148 // A new DiagState was created here.
7149 Diag.DiagStates.push_back(x: BasedOn);
7150 DiagState *NewState = &Diag.DiagStates.back();
7151 DiagStates.push_back(Elt: NewState);
7152 unsigned Size = Record[Idx++];
7153 assert(Idx + Size * 2 <= Record.size() &&
7154 "Invalid data, not enough diag/map pairs");
7155 while (Size--) {
7156 unsigned DiagID = Record[Idx++];
7157 DiagnosticMapping NewMapping =
7158 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7159 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7160 continue;
7161
7162 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7163
7164 // If this mapping was specified as a warning but the severity was
7165 // upgraded due to diagnostic settings, simulate the current diagnostic
7166 // settings (and use a warning).
7167 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7168 NewMapping.setSeverity(diag::Severity::Warning);
7169 NewMapping.setUpgradedFromWarning(false);
7170 }
7171
7172 Mapping = NewMapping;
7173 }
7174 return NewState;
7175 };
7176
7177 // Read the first state.
7178 DiagState *FirstState;
7179 if (F.Kind == MK_ImplicitModule) {
7180 // Implicitly-built modules are reused with different diagnostic
7181 // settings. Use the initial diagnostic state from Diag to simulate this
7182 // compilation's diagnostic settings.
7183 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7184 DiagStates.push_back(Elt: FirstState);
7185
7186 // Skip the initial diagnostic state from the serialized module.
7187 assert(Record[1] == 0 &&
7188 "Invalid data, unexpected backref in initial state");
7189 Idx = 3 + Record[2] * 2;
7190 assert(Idx < Record.size() &&
7191 "Invalid data, not enough state change pairs in initial state");
7192 } else if (F.isModule()) {
7193 // For an explicit module, preserve the flags from the module build
7194 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7195 // -Wblah flags.
7196 unsigned Flags = Record[Idx++];
7197 DiagState Initial(*Diag.getDiagnosticIDs());
7198 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7199 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7200 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7201 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7202 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7203 Initial.ExtBehavior = (diag::Severity)Flags;
7204 FirstState = ReadDiagState(Initial, true);
7205
7206 assert(F.OriginalSourceFileID.isValid());
7207
7208 // Set up the root buffer of the module to start with the initial
7209 // diagnostic state of the module itself, to cover files that contain no
7210 // explicit transitions (for which we did not serialize anything).
7211 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7212 .StateTransitions.push_back(Elt: {FirstState, 0});
7213 } else {
7214 // For prefix ASTs, start with whatever the user configured on the
7215 // command line.
7216 Idx++; // Skip flags.
7217 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7218 }
7219
7220 // Read the state transitions.
7221 unsigned NumLocations = Record[Idx++];
7222 while (NumLocations--) {
7223 assert(Idx < Record.size() &&
7224 "Invalid data, missing pragma diagnostic states");
7225 FileID FID = ReadFileID(F, Record, Idx);
7226 assert(FID.isValid() && "invalid FileID for transition");
7227 unsigned Transitions = Record[Idx++];
7228
7229 // Note that we don't need to set up Parent/ParentOffset here, because
7230 // we won't be changing the diagnostic state within imported FileIDs
7231 // (other than perhaps appending to the main source file, which has no
7232 // parent).
7233 auto &F = Diag.DiagStatesByLoc.Files[FID];
7234 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7235 for (unsigned I = 0; I != Transitions; ++I) {
7236 unsigned Offset = Record[Idx++];
7237 auto *State = ReadDiagState(*FirstState, false);
7238 F.StateTransitions.push_back(Elt: {State, Offset});
7239 }
7240 }
7241
7242 // Read the final state.
7243 assert(Idx < Record.size() &&
7244 "Invalid data, missing final pragma diagnostic state");
7245 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7246 auto *CurState = ReadDiagState(*FirstState, false);
7247
7248 if (!F.isModule()) {
7249 Diag.DiagStatesByLoc.CurDiagState = CurState;
7250 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7251
7252 // Preserve the property that the imaginary root file describes the
7253 // current state.
7254 FileID NullFile;
7255 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7256 if (T.empty())
7257 T.push_back(Elt: {CurState, 0});
7258 else
7259 T[0].State = CurState;
7260 }
7261
7262 // Don't try to read these mappings again.
7263 Record.clear();
7264 }
7265}
7266
7267/// Get the correct cursor and offset for loading a type.
7268ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7269 auto [M, Index] = translateTypeIDToIndex(ID);
7270 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7271 M->DeclsBlockStartOffset);
7272}
7273
7274static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7275 switch (code) {
7276#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7277 case TYPE_##CODE_ID: return Type::CLASS_ID;
7278#include "clang/Serialization/TypeBitCodes.def"
7279 default:
7280 return std::nullopt;
7281 }
7282}
7283
7284/// Read and return the type with the given index..
7285///
7286/// The index is the type ID, shifted and minus the number of predefs. This
7287/// routine actually reads the record corresponding to the type at the given
7288/// location. It is a helper routine for GetType, which deals with reading type
7289/// IDs.
7290QualType ASTReader::readTypeRecord(TypeID ID) {
7291 assert(ContextObj && "reading type with no AST context");
7292 ASTContext &Context = *ContextObj;
7293 RecordLocation Loc = TypeCursorForIndex(ID);
7294 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7295
7296 // Keep track of where we are in the stream, then jump back there
7297 // after reading this type.
7298 SavedStreamPosition SavedPosition(DeclsCursor);
7299
7300 ReadingKindTracker ReadingKind(Read_Type, *this);
7301
7302 // Note that we are loading a type record.
7303 Deserializing AType(this);
7304
7305 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7306 Error(Err: std::move(Err));
7307 return QualType();
7308 }
7309 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7310 if (!RawCode) {
7311 Error(Err: RawCode.takeError());
7312 return QualType();
7313 }
7314
7315 ASTRecordReader Record(*this, *Loc.F);
7316 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7317 if (!Code) {
7318 Error(Err: Code.takeError());
7319 return QualType();
7320 }
7321 if (Code.get() == TYPE_EXT_QUAL) {
7322 QualType baseType = Record.readQualType();
7323 Qualifiers quals = Record.readQualifiers();
7324 return Context.getQualifiedType(T: baseType, Qs: quals);
7325 }
7326
7327 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7328 if (!maybeClass) {
7329 Error(Msg: "Unexpected code for type");
7330 return QualType();
7331 }
7332
7333 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7334 return TypeReader.read(kind: *maybeClass);
7335}
7336
7337namespace clang {
7338
7339class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7340 ASTRecordReader &Reader;
7341
7342 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7343 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7344
7345 TypeSourceInfo *GetTypeSourceInfo() {
7346 return Reader.readTypeSourceInfo();
7347 }
7348
7349 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7350 return Reader.readNestedNameSpecifierLoc();
7351 }
7352
7353 Attr *ReadAttr() {
7354 return Reader.readAttr();
7355 }
7356
7357public:
7358 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7359
7360 // We want compile-time assurance that we've enumerated all of
7361 // these, so unfortunately we have to declare them first, then
7362 // define them out-of-line.
7363#define ABSTRACT_TYPELOC(CLASS, PARENT)
7364#define TYPELOC(CLASS, PARENT) \
7365 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7366#include "clang/AST/TypeLocNodes.def"
7367
7368 void VisitFunctionTypeLoc(FunctionTypeLoc);
7369 void VisitArrayTypeLoc(ArrayTypeLoc);
7370 void VisitTagTypeLoc(TagTypeLoc TL);
7371};
7372
7373} // namespace clang
7374
7375void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7376 // nothing to do
7377}
7378
7379void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7380 TL.setBuiltinLoc(readSourceLocation());
7381 if (TL.needsExtraLocalData()) {
7382 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7383 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7384 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7385 TL.setModeAttr(Reader.readInt());
7386 }
7387}
7388
7389void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7390 TL.setNameLoc(readSourceLocation());
7391}
7392
7393void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7394 TL.setStarLoc(readSourceLocation());
7395}
7396
7397void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7398 // nothing to do
7399}
7400
7401void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7402 // nothing to do
7403}
7404
7405void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7406 // nothing to do
7407}
7408
7409void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7410 TL.setExpansionLoc(readSourceLocation());
7411}
7412
7413void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7414 TL.setCaretLoc(readSourceLocation());
7415}
7416
7417void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7418 TL.setAmpLoc(readSourceLocation());
7419}
7420
7421void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7422 TL.setAmpAmpLoc(readSourceLocation());
7423}
7424
7425void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7426 TL.setStarLoc(readSourceLocation());
7427 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7428}
7429
7430void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7431 TL.setLBracketLoc(readSourceLocation());
7432 TL.setRBracketLoc(readSourceLocation());
7433 if (Reader.readBool())
7434 TL.setSizeExpr(Reader.readExpr());
7435 else
7436 TL.setSizeExpr(nullptr);
7437}
7438
7439void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7440 VisitArrayTypeLoc(TL);
7441}
7442
7443void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7444 VisitArrayTypeLoc(TL);
7445}
7446
7447void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7448 VisitArrayTypeLoc(TL);
7449}
7450
7451void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7452 DependentSizedArrayTypeLoc TL) {
7453 VisitArrayTypeLoc(TL);
7454}
7455
7456void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7457 DependentAddressSpaceTypeLoc TL) {
7458
7459 TL.setAttrNameLoc(readSourceLocation());
7460 TL.setAttrOperandParensRange(readSourceRange());
7461 TL.setAttrExprOperand(Reader.readExpr());
7462}
7463
7464void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7465 DependentSizedExtVectorTypeLoc TL) {
7466 TL.setNameLoc(readSourceLocation());
7467}
7468
7469void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7470 TL.setNameLoc(readSourceLocation());
7471}
7472
7473void TypeLocReader::VisitDependentVectorTypeLoc(
7474 DependentVectorTypeLoc TL) {
7475 TL.setNameLoc(readSourceLocation());
7476}
7477
7478void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7479 TL.setNameLoc(readSourceLocation());
7480}
7481
7482void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7483 TL.setAttrNameLoc(readSourceLocation());
7484 TL.setAttrOperandParensRange(readSourceRange());
7485 TL.setAttrRowOperand(Reader.readExpr());
7486 TL.setAttrColumnOperand(Reader.readExpr());
7487}
7488
7489void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7490 DependentSizedMatrixTypeLoc TL) {
7491 TL.setAttrNameLoc(readSourceLocation());
7492 TL.setAttrOperandParensRange(readSourceRange());
7493 TL.setAttrRowOperand(Reader.readExpr());
7494 TL.setAttrColumnOperand(Reader.readExpr());
7495}
7496
7497void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7498 TL.setLocalRangeBegin(readSourceLocation());
7499 TL.setLParenLoc(readSourceLocation());
7500 TL.setRParenLoc(readSourceLocation());
7501 TL.setExceptionSpecRange(readSourceRange());
7502 TL.setLocalRangeEnd(readSourceLocation());
7503 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7504 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7505 }
7506}
7507
7508void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7509 VisitFunctionTypeLoc(TL);
7510}
7511
7512void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7513 VisitFunctionTypeLoc(TL);
7514}
7515
7516void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7517 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7518 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7519 SourceLocation NameLoc = readSourceLocation();
7520 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7521}
7522
7523void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7524 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7525 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7526 SourceLocation NameLoc = readSourceLocation();
7527 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7528}
7529
7530void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7531 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7532 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7533 SourceLocation NameLoc = readSourceLocation();
7534 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7535}
7536
7537void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7538 TL.setTypeofLoc(readSourceLocation());
7539 TL.setLParenLoc(readSourceLocation());
7540 TL.setRParenLoc(readSourceLocation());
7541}
7542
7543void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7544 TL.setTypeofLoc(readSourceLocation());
7545 TL.setLParenLoc(readSourceLocation());
7546 TL.setRParenLoc(readSourceLocation());
7547 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7548}
7549
7550void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7551 TL.setDecltypeLoc(readSourceLocation());
7552 TL.setRParenLoc(readSourceLocation());
7553}
7554
7555void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7556 TL.setEllipsisLoc(readSourceLocation());
7557}
7558
7559void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7560 TL.setKWLoc(readSourceLocation());
7561 TL.setLParenLoc(readSourceLocation());
7562 TL.setRParenLoc(readSourceLocation());
7563 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7564}
7565
7566ConceptReference *ASTRecordReader::readConceptReference() {
7567 auto NNS = readNestedNameSpecifierLoc();
7568 auto TemplateKWLoc = readSourceLocation();
7569 auto ConceptNameLoc = readDeclarationNameInfo();
7570 auto FoundDecl = readDeclAs<NamedDecl>();
7571 auto NamedConcept = readDeclAs<ConceptDecl>();
7572 auto *CR = ConceptReference::Create(
7573 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7574 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7575 return CR;
7576}
7577
7578void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7579 TL.setNameLoc(readSourceLocation());
7580 if (Reader.readBool())
7581 TL.setConceptReference(Reader.readConceptReference());
7582 if (Reader.readBool())
7583 TL.setRParenLoc(readSourceLocation());
7584}
7585
7586void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7587 DeducedTemplateSpecializationTypeLoc TL) {
7588 TL.setElaboratedKeywordLoc(readSourceLocation());
7589 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7590 TL.setTemplateNameLoc(readSourceLocation());
7591}
7592
7593void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7594 TL.setElaboratedKeywordLoc(readSourceLocation());
7595 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7596 TL.setNameLoc(readSourceLocation());
7597}
7598
7599void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7600 VisitTagTypeLoc(TL);
7601}
7602
7603void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7604 VisitTagTypeLoc(TL);
7605}
7606
7607void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7608
7609void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7610 TL.setAttr(ReadAttr());
7611}
7612
7613void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7614 // Nothing to do
7615}
7616
7617void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7618 // Nothing to do.
7619}
7620
7621void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7622 TL.setAttrLoc(readSourceLocation());
7623}
7624
7625void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7626 HLSLAttributedResourceTypeLoc TL) {
7627 // Nothing to do.
7628}
7629
7630void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7631 // Nothing to do.
7632}
7633
7634void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7635 TL.setNameLoc(readSourceLocation());
7636}
7637
7638void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7639 SubstTemplateTypeParmTypeLoc TL) {
7640 TL.setNameLoc(readSourceLocation());
7641}
7642
7643void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7644 SubstTemplateTypeParmPackTypeLoc TL) {
7645 TL.setNameLoc(readSourceLocation());
7646}
7647
7648void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7649 SubstBuiltinTemplatePackTypeLoc TL) {
7650 TL.setNameLoc(readSourceLocation());
7651}
7652
7653void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7654 TemplateSpecializationTypeLoc TL) {
7655 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7656 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7657 SourceLocation TemplateKeywordLoc = readSourceLocation();
7658 SourceLocation NameLoc = readSourceLocation();
7659 SourceLocation LAngleLoc = readSourceLocation();
7660 SourceLocation RAngleLoc = readSourceLocation();
7661 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7662 LAngleLoc, RAngleLoc);
7663 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7664 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7665 Args[I] = Reader.readTemplateArgumentLocInfo(
7666 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7667}
7668
7669void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7670 TL.setLParenLoc(readSourceLocation());
7671 TL.setRParenLoc(readSourceLocation());
7672}
7673
7674void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7675 TL.setElaboratedKeywordLoc(readSourceLocation());
7676 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7677 TL.setNameLoc(readSourceLocation());
7678}
7679
7680void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7681 TL.setEllipsisLoc(readSourceLocation());
7682}
7683
7684void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7685 TL.setNameLoc(readSourceLocation());
7686 TL.setNameEndLoc(readSourceLocation());
7687}
7688
7689void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7690 if (TL.getNumProtocols()) {
7691 TL.setProtocolLAngleLoc(readSourceLocation());
7692 TL.setProtocolRAngleLoc(readSourceLocation());
7693 }
7694 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7695 TL.setProtocolLoc(i, Loc: readSourceLocation());
7696}
7697
7698void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7699 TL.setHasBaseTypeAsWritten(Reader.readBool());
7700 TL.setTypeArgsLAngleLoc(readSourceLocation());
7701 TL.setTypeArgsRAngleLoc(readSourceLocation());
7702 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7703 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7704 TL.setProtocolLAngleLoc(readSourceLocation());
7705 TL.setProtocolRAngleLoc(readSourceLocation());
7706 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7707 TL.setProtocolLoc(i, Loc: readSourceLocation());
7708}
7709
7710void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7711 TL.setStarLoc(readSourceLocation());
7712}
7713
7714void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7715 TL.setKWLoc(readSourceLocation());
7716 TL.setLParenLoc(readSourceLocation());
7717 TL.setRParenLoc(readSourceLocation());
7718}
7719
7720void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7721 TL.setKWLoc(readSourceLocation());
7722}
7723
7724void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7725 TL.setNameLoc(readSourceLocation());
7726}
7727
7728void TypeLocReader::VisitDependentBitIntTypeLoc(
7729 clang::DependentBitIntTypeLoc TL) {
7730 TL.setNameLoc(readSourceLocation());
7731}
7732
7733void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7734 // Nothing to do.
7735}
7736
7737void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7738 TypeLocReader TLR(*this);
7739 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7740 TLR.Visit(TyLoc: TL);
7741}
7742
7743TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7744 QualType InfoTy = readType();
7745 if (InfoTy.isNull())
7746 return nullptr;
7747
7748 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7749 readTypeLoc(TL: TInfo->getTypeLoc());
7750 return TInfo;
7751}
7752
7753static unsigned getIndexForTypeID(serialization::TypeID ID) {
7754 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7755}
7756
7757static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7758 return ID >> 32;
7759}
7760
7761static bool isPredefinedType(serialization::TypeID ID) {
7762 // We don't need to erase the higher bits since if these bits are not 0,
7763 // it must be larger than NUM_PREDEF_TYPE_IDS.
7764 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7765}
7766
7767std::pair<ModuleFile *, unsigned>
7768ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7769 assert(!isPredefinedType(ID) &&
7770 "Predefined type shouldn't be in TypesLoaded");
7771 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7772 assert(ModuleFileIndex && "Untranslated Local Decl?");
7773
7774 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7775 assert(OwningModuleFile &&
7776 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7777
7778 return {OwningModuleFile,
7779 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7780}
7781
7782QualType ASTReader::GetType(TypeID ID) {
7783 assert(ContextObj && "reading type with no AST context");
7784 ASTContext &Context = *ContextObj;
7785
7786 unsigned FastQuals = ID & Qualifiers::FastMask;
7787
7788 if (isPredefinedType(ID)) {
7789 QualType T;
7790 unsigned Index = getIndexForTypeID(ID);
7791 switch ((PredefinedTypeIDs)Index) {
7792 case PREDEF_TYPE_LAST_ID:
7793 // We should never use this one.
7794 llvm_unreachable("Invalid predefined type");
7795 break;
7796 case PREDEF_TYPE_NULL_ID:
7797 return QualType();
7798 case PREDEF_TYPE_VOID_ID:
7799 T = Context.VoidTy;
7800 break;
7801 case PREDEF_TYPE_BOOL_ID:
7802 T = Context.BoolTy;
7803 break;
7804 case PREDEF_TYPE_CHAR_U_ID:
7805 case PREDEF_TYPE_CHAR_S_ID:
7806 // FIXME: Check that the signedness of CharTy is correct!
7807 T = Context.CharTy;
7808 break;
7809 case PREDEF_TYPE_UCHAR_ID:
7810 T = Context.UnsignedCharTy;
7811 break;
7812 case PREDEF_TYPE_USHORT_ID:
7813 T = Context.UnsignedShortTy;
7814 break;
7815 case PREDEF_TYPE_UINT_ID:
7816 T = Context.UnsignedIntTy;
7817 break;
7818 case PREDEF_TYPE_ULONG_ID:
7819 T = Context.UnsignedLongTy;
7820 break;
7821 case PREDEF_TYPE_ULONGLONG_ID:
7822 T = Context.UnsignedLongLongTy;
7823 break;
7824 case PREDEF_TYPE_UINT128_ID:
7825 T = Context.UnsignedInt128Ty;
7826 break;
7827 case PREDEF_TYPE_SCHAR_ID:
7828 T = Context.SignedCharTy;
7829 break;
7830 case PREDEF_TYPE_WCHAR_ID:
7831 T = Context.WCharTy;
7832 break;
7833 case PREDEF_TYPE_SHORT_ID:
7834 T = Context.ShortTy;
7835 break;
7836 case PREDEF_TYPE_INT_ID:
7837 T = Context.IntTy;
7838 break;
7839 case PREDEF_TYPE_LONG_ID:
7840 T = Context.LongTy;
7841 break;
7842 case PREDEF_TYPE_LONGLONG_ID:
7843 T = Context.LongLongTy;
7844 break;
7845 case PREDEF_TYPE_INT128_ID:
7846 T = Context.Int128Ty;
7847 break;
7848 case PREDEF_TYPE_BFLOAT16_ID:
7849 T = Context.BFloat16Ty;
7850 break;
7851 case PREDEF_TYPE_HALF_ID:
7852 T = Context.HalfTy;
7853 break;
7854 case PREDEF_TYPE_FLOAT_ID:
7855 T = Context.FloatTy;
7856 break;
7857 case PREDEF_TYPE_DOUBLE_ID:
7858 T = Context.DoubleTy;
7859 break;
7860 case PREDEF_TYPE_LONGDOUBLE_ID:
7861 T = Context.LongDoubleTy;
7862 break;
7863 case PREDEF_TYPE_SHORT_ACCUM_ID:
7864 T = Context.ShortAccumTy;
7865 break;
7866 case PREDEF_TYPE_ACCUM_ID:
7867 T = Context.AccumTy;
7868 break;
7869 case PREDEF_TYPE_LONG_ACCUM_ID:
7870 T = Context.LongAccumTy;
7871 break;
7872 case PREDEF_TYPE_USHORT_ACCUM_ID:
7873 T = Context.UnsignedShortAccumTy;
7874 break;
7875 case PREDEF_TYPE_UACCUM_ID:
7876 T = Context.UnsignedAccumTy;
7877 break;
7878 case PREDEF_TYPE_ULONG_ACCUM_ID:
7879 T = Context.UnsignedLongAccumTy;
7880 break;
7881 case PREDEF_TYPE_SHORT_FRACT_ID:
7882 T = Context.ShortFractTy;
7883 break;
7884 case PREDEF_TYPE_FRACT_ID:
7885 T = Context.FractTy;
7886 break;
7887 case PREDEF_TYPE_LONG_FRACT_ID:
7888 T = Context.LongFractTy;
7889 break;
7890 case PREDEF_TYPE_USHORT_FRACT_ID:
7891 T = Context.UnsignedShortFractTy;
7892 break;
7893 case PREDEF_TYPE_UFRACT_ID:
7894 T = Context.UnsignedFractTy;
7895 break;
7896 case PREDEF_TYPE_ULONG_FRACT_ID:
7897 T = Context.UnsignedLongFractTy;
7898 break;
7899 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7900 T = Context.SatShortAccumTy;
7901 break;
7902 case PREDEF_TYPE_SAT_ACCUM_ID:
7903 T = Context.SatAccumTy;
7904 break;
7905 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7906 T = Context.SatLongAccumTy;
7907 break;
7908 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7909 T = Context.SatUnsignedShortAccumTy;
7910 break;
7911 case PREDEF_TYPE_SAT_UACCUM_ID:
7912 T = Context.SatUnsignedAccumTy;
7913 break;
7914 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7915 T = Context.SatUnsignedLongAccumTy;
7916 break;
7917 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7918 T = Context.SatShortFractTy;
7919 break;
7920 case PREDEF_TYPE_SAT_FRACT_ID:
7921 T = Context.SatFractTy;
7922 break;
7923 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7924 T = Context.SatLongFractTy;
7925 break;
7926 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7927 T = Context.SatUnsignedShortFractTy;
7928 break;
7929 case PREDEF_TYPE_SAT_UFRACT_ID:
7930 T = Context.SatUnsignedFractTy;
7931 break;
7932 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7933 T = Context.SatUnsignedLongFractTy;
7934 break;
7935 case PREDEF_TYPE_FLOAT16_ID:
7936 T = Context.Float16Ty;
7937 break;
7938 case PREDEF_TYPE_FLOAT128_ID:
7939 T = Context.Float128Ty;
7940 break;
7941 case PREDEF_TYPE_IBM128_ID:
7942 T = Context.Ibm128Ty;
7943 break;
7944 case PREDEF_TYPE_OVERLOAD_ID:
7945 T = Context.OverloadTy;
7946 break;
7947 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
7948 T = Context.UnresolvedTemplateTy;
7949 break;
7950 case PREDEF_TYPE_BOUND_MEMBER:
7951 T = Context.BoundMemberTy;
7952 break;
7953 case PREDEF_TYPE_PSEUDO_OBJECT:
7954 T = Context.PseudoObjectTy;
7955 break;
7956 case PREDEF_TYPE_DEPENDENT_ID:
7957 T = Context.DependentTy;
7958 break;
7959 case PREDEF_TYPE_UNKNOWN_ANY:
7960 T = Context.UnknownAnyTy;
7961 break;
7962 case PREDEF_TYPE_NULLPTR_ID:
7963 T = Context.NullPtrTy;
7964 break;
7965 case PREDEF_TYPE_CHAR8_ID:
7966 T = Context.Char8Ty;
7967 break;
7968 case PREDEF_TYPE_CHAR16_ID:
7969 T = Context.Char16Ty;
7970 break;
7971 case PREDEF_TYPE_CHAR32_ID:
7972 T = Context.Char32Ty;
7973 break;
7974 case PREDEF_TYPE_OBJC_ID:
7975 T = Context.ObjCBuiltinIdTy;
7976 break;
7977 case PREDEF_TYPE_OBJC_CLASS:
7978 T = Context.ObjCBuiltinClassTy;
7979 break;
7980 case PREDEF_TYPE_OBJC_SEL:
7981 T = Context.ObjCBuiltinSelTy;
7982 break;
7983#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7984 case PREDEF_TYPE_##Id##_ID: \
7985 T = Context.SingletonId; \
7986 break;
7987#include "clang/Basic/OpenCLImageTypes.def"
7988#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7989 case PREDEF_TYPE_##Id##_ID: \
7990 T = Context.Id##Ty; \
7991 break;
7992#include "clang/Basic/OpenCLExtensionTypes.def"
7993 case PREDEF_TYPE_SAMPLER_ID:
7994 T = Context.OCLSamplerTy;
7995 break;
7996 case PREDEF_TYPE_EVENT_ID:
7997 T = Context.OCLEventTy;
7998 break;
7999 case PREDEF_TYPE_CLK_EVENT_ID:
8000 T = Context.OCLClkEventTy;
8001 break;
8002 case PREDEF_TYPE_QUEUE_ID:
8003 T = Context.OCLQueueTy;
8004 break;
8005 case PREDEF_TYPE_RESERVE_ID_ID:
8006 T = Context.OCLReserveIDTy;
8007 break;
8008 case PREDEF_TYPE_AUTO_DEDUCT:
8009 T = Context.getAutoDeductType();
8010 break;
8011 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8012 T = Context.getAutoRRefDeductType();
8013 break;
8014 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8015 T = Context.ARCUnbridgedCastTy;
8016 break;
8017 case PREDEF_TYPE_BUILTIN_FN:
8018 T = Context.BuiltinFnTy;
8019 break;
8020 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8021 T = Context.IncompleteMatrixIdxTy;
8022 break;
8023 case PREDEF_TYPE_ARRAY_SECTION:
8024 T = Context.ArraySectionTy;
8025 break;
8026 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8027 T = Context.OMPArrayShapingTy;
8028 break;
8029 case PREDEF_TYPE_OMP_ITERATOR:
8030 T = Context.OMPIteratorTy;
8031 break;
8032#define SVE_TYPE(Name, Id, SingletonId) \
8033 case PREDEF_TYPE_##Id##_ID: \
8034 T = Context.SingletonId; \
8035 break;
8036#include "clang/Basic/AArch64ACLETypes.def"
8037#define PPC_VECTOR_TYPE(Name, Id, Size) \
8038 case PREDEF_TYPE_##Id##_ID: \
8039 T = Context.Id##Ty; \
8040 break;
8041#include "clang/Basic/PPCTypes.def"
8042#define RVV_TYPE(Name, Id, SingletonId) \
8043 case PREDEF_TYPE_##Id##_ID: \
8044 T = Context.SingletonId; \
8045 break;
8046#include "clang/Basic/RISCVVTypes.def"
8047#define WASM_TYPE(Name, Id, SingletonId) \
8048 case PREDEF_TYPE_##Id##_ID: \
8049 T = Context.SingletonId; \
8050 break;
8051#include "clang/Basic/WebAssemblyReferenceTypes.def"
8052#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8053 case PREDEF_TYPE_##Id##_ID: \
8054 T = Context.SingletonId; \
8055 break;
8056#include "clang/Basic/AMDGPUTypes.def"
8057#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8058 case PREDEF_TYPE_##Id##_ID: \
8059 T = Context.SingletonId; \
8060 break;
8061#include "clang/Basic/HLSLIntangibleTypes.def"
8062 }
8063
8064 assert(!T.isNull() && "Unknown predefined type");
8065 return T.withFastQualifiers(TQs: FastQuals);
8066 }
8067
8068 unsigned Index = translateTypeIDToIndex(ID).second;
8069
8070 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8071 if (TypesLoaded[Index].isNull()) {
8072 TypesLoaded[Index] = readTypeRecord(ID);
8073 if (TypesLoaded[Index].isNull())
8074 return QualType();
8075
8076 TypesLoaded[Index]->setFromAST();
8077 if (DeserializationListener)
8078 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8079 T: TypesLoaded[Index]);
8080 }
8081
8082 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8083}
8084
8085QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8086 return GetType(ID: getGlobalTypeID(F, LocalID));
8087}
8088
8089serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8090 LocalTypeID LocalID) const {
8091 if (isPredefinedType(ID: LocalID))
8092 return LocalID;
8093
8094 if (!F.ModuleOffsetMap.empty())
8095 ReadModuleOffsetMap(F);
8096
8097 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8098 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8099
8100 if (ModuleFileIndex == 0)
8101 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8102
8103 ModuleFile &MF =
8104 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8105 ModuleFileIndex = MF.Index + 1;
8106 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8107}
8108
8109TemplateArgumentLocInfo
8110ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8111 switch (Kind) {
8112 case TemplateArgument::Expression:
8113 return readExpr();
8114 case TemplateArgument::Type:
8115 return readTypeSourceInfo();
8116 case TemplateArgument::Template:
8117 case TemplateArgument::TemplateExpansion: {
8118 SourceLocation TemplateKWLoc = readSourceLocation();
8119 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8120 SourceLocation TemplateNameLoc = readSourceLocation();
8121 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8122 ? readSourceLocation()
8123 : SourceLocation();
8124 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8125 TemplateNameLoc, EllipsisLoc);
8126 }
8127 case TemplateArgument::Null:
8128 case TemplateArgument::Integral:
8129 case TemplateArgument::Declaration:
8130 case TemplateArgument::NullPtr:
8131 case TemplateArgument::StructuralValue:
8132 case TemplateArgument::Pack:
8133 // FIXME: Is this right?
8134 return TemplateArgumentLocInfo();
8135 }
8136 llvm_unreachable("unexpected template argument loc");
8137}
8138
8139TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8140 TemplateArgument Arg = readTemplateArgument();
8141
8142 if (Arg.getKind() == TemplateArgument::Expression) {
8143 if (readBool()) // bool InfoHasSameExpr.
8144 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8145 }
8146 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8147}
8148
8149void ASTRecordReader::readTemplateArgumentListInfo(
8150 TemplateArgumentListInfo &Result) {
8151 Result.setLAngleLoc(readSourceLocation());
8152 Result.setRAngleLoc(readSourceLocation());
8153 unsigned NumArgsAsWritten = readInt();
8154 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8155 Result.addArgument(Loc: readTemplateArgumentLoc());
8156}
8157
8158const ASTTemplateArgumentListInfo *
8159ASTRecordReader::readASTTemplateArgumentListInfo() {
8160 TemplateArgumentListInfo Result;
8161 readTemplateArgumentListInfo(Result);
8162 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8163}
8164
8165Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8166
8167void ASTReader::CompleteRedeclChain(const Decl *D) {
8168 if (NumCurrentElementsDeserializing) {
8169 // We arrange to not care about the complete redeclaration chain while we're
8170 // deserializing. Just remember that the AST has marked this one as complete
8171 // but that it's not actually complete yet, so we know we still need to
8172 // complete it later.
8173 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8174 return;
8175 }
8176
8177 if (!D->getDeclContext()) {
8178 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8179 return;
8180 }
8181
8182 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8183
8184 // If this is a named declaration, complete it by looking it up
8185 // within its context.
8186 //
8187 // FIXME: Merging a function definition should merge
8188 // all mergeable entities within it.
8189 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8190 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8191 if (!getContext().getLangOpts().CPlusPlus &&
8192 isa<TranslationUnitDecl>(Val: DC)) {
8193 // Outside of C++, we don't have a lookup table for the TU, so update
8194 // the identifier instead. (For C++ modules, we don't store decls
8195 // in the serialized identifier table, so we do the lookup in the TU.)
8196 auto *II = Name.getAsIdentifierInfo();
8197 assert(II && "non-identifier name in C?");
8198 if (II->isOutOfDate())
8199 updateOutOfDateIdentifier(II: *II);
8200 } else
8201 DC->lookup(Name);
8202 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8203 // Find all declarations of this kind from the relevant context.
8204 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8205 auto *DC = cast<DeclContext>(Val: DCDecl);
8206 SmallVector<Decl*, 8> Decls;
8207 FindExternalLexicalDecls(
8208 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8209 }
8210 }
8211 }
8212
8213 RedeclarableTemplateDecl *Template = nullptr;
8214 ArrayRef<TemplateArgument> Args;
8215 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8216 Template = CTSD->getSpecializedTemplate();
8217 Args = CTSD->getTemplateArgs().asArray();
8218 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8219 Template = VTSD->getSpecializedTemplate();
8220 Args = VTSD->getTemplateArgs().asArray();
8221 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8222 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8223 Template = Tmplt;
8224 Args = FD->getTemplateSpecializationArgs()->asArray();
8225 }
8226 }
8227
8228 if (Template)
8229 Template->loadLazySpecializationsImpl(Args);
8230}
8231
8232CXXCtorInitializer **
8233ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8234 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8235 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8236 SavedStreamPosition SavedPosition(Cursor);
8237 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8238 Error(Err: std::move(Err));
8239 return nullptr;
8240 }
8241 ReadingKindTracker ReadingKind(Read_Decl, *this);
8242 Deserializing D(this);
8243
8244 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8245 if (!MaybeCode) {
8246 Error(Err: MaybeCode.takeError());
8247 return nullptr;
8248 }
8249 unsigned Code = MaybeCode.get();
8250
8251 ASTRecordReader Record(*this, *Loc.F);
8252 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8253 if (!MaybeRecCode) {
8254 Error(Err: MaybeRecCode.takeError());
8255 return nullptr;
8256 }
8257 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8258 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8259 return nullptr;
8260 }
8261
8262 return Record.readCXXCtorInitializers();
8263}
8264
8265CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8266 assert(ContextObj && "reading base specifiers with no AST context");
8267 ASTContext &Context = *ContextObj;
8268
8269 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8270 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8271 SavedStreamPosition SavedPosition(Cursor);
8272 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8273 Error(Err: std::move(Err));
8274 return nullptr;
8275 }
8276 ReadingKindTracker ReadingKind(Read_Decl, *this);
8277 Deserializing D(this);
8278
8279 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8280 if (!MaybeCode) {
8281 Error(Err: MaybeCode.takeError());
8282 return nullptr;
8283 }
8284 unsigned Code = MaybeCode.get();
8285
8286 ASTRecordReader Record(*this, *Loc.F);
8287 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8288 if (!MaybeRecCode) {
8289 Error(Err: MaybeCode.takeError());
8290 return nullptr;
8291 }
8292 unsigned RecCode = MaybeRecCode.get();
8293
8294 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8295 Error(Msg: "malformed AST file: missing C++ base specifiers");
8296 return nullptr;
8297 }
8298
8299 unsigned NumBases = Record.readInt();
8300 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8301 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8302 for (unsigned I = 0; I != NumBases; ++I)
8303 Bases[I] = Record.readCXXBaseSpecifier();
8304 return Bases;
8305}
8306
8307GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8308 LocalDeclID LocalID) const {
8309 if (LocalID < NUM_PREDEF_DECL_IDS)
8310 return GlobalDeclID(LocalID.getRawValue());
8311
8312 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8313 DeclID ID = LocalID.getLocalDeclIndex();
8314
8315 if (!F.ModuleOffsetMap.empty())
8316 ReadModuleOffsetMap(F);
8317
8318 ModuleFile *OwningModuleFile =
8319 OwningModuleFileIndex == 0
8320 ? &F
8321 : F.TransitiveImports[OwningModuleFileIndex - 1];
8322
8323 if (OwningModuleFileIndex == 0)
8324 ID -= NUM_PREDEF_DECL_IDS;
8325
8326 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8327 return GlobalDeclID(NewModuleFileIndex, ID);
8328}
8329
8330bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8331 // Predefined decls aren't from any module.
8332 if (ID < NUM_PREDEF_DECL_IDS)
8333 return false;
8334
8335 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8336 return M.Index == ModuleFileIndex - 1;
8337}
8338
8339ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8340 // Predefined decls aren't from any module.
8341 if (ID < NUM_PREDEF_DECL_IDS)
8342 return nullptr;
8343
8344 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8345 assert(ModuleFileIndex && "Untranslated Local Decl?");
8346
8347 return &getModuleManager()[ModuleFileIndex - 1];
8348}
8349
8350ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8351 if (!D->isFromASTFile())
8352 return nullptr;
8353
8354 return getOwningModuleFile(ID: D->getGlobalID());
8355}
8356
8357SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8358 if (ID < NUM_PREDEF_DECL_IDS)
8359 return SourceLocation();
8360
8361 if (Decl *D = GetExistingDecl(ID))
8362 return D->getLocation();
8363
8364 SourceLocation Loc;
8365 DeclCursorForID(ID, Location&: Loc);
8366 return Loc;
8367}
8368
8369Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8370 assert(ContextObj && "reading predefined decl without AST context");
8371 ASTContext &Context = *ContextObj;
8372 Decl *NewLoaded = nullptr;
8373 switch (ID) {
8374 case PREDEF_DECL_NULL_ID:
8375 return nullptr;
8376
8377 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8378 return Context.getTranslationUnitDecl();
8379
8380 case PREDEF_DECL_OBJC_ID_ID:
8381 if (Context.ObjCIdDecl)
8382 return Context.ObjCIdDecl;
8383 NewLoaded = Context.getObjCIdDecl();
8384 break;
8385
8386 case PREDEF_DECL_OBJC_SEL_ID:
8387 if (Context.ObjCSelDecl)
8388 return Context.ObjCSelDecl;
8389 NewLoaded = Context.getObjCSelDecl();
8390 break;
8391
8392 case PREDEF_DECL_OBJC_CLASS_ID:
8393 if (Context.ObjCClassDecl)
8394 return Context.ObjCClassDecl;
8395 NewLoaded = Context.getObjCClassDecl();
8396 break;
8397
8398 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8399 if (Context.ObjCProtocolClassDecl)
8400 return Context.ObjCProtocolClassDecl;
8401 NewLoaded = Context.getObjCProtocolDecl();
8402 break;
8403
8404 case PREDEF_DECL_INT_128_ID:
8405 if (Context.Int128Decl)
8406 return Context.Int128Decl;
8407 NewLoaded = Context.getInt128Decl();
8408 break;
8409
8410 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8411 if (Context.UInt128Decl)
8412 return Context.UInt128Decl;
8413 NewLoaded = Context.getUInt128Decl();
8414 break;
8415
8416 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8417 if (Context.ObjCInstanceTypeDecl)
8418 return Context.ObjCInstanceTypeDecl;
8419 NewLoaded = Context.getObjCInstanceTypeDecl();
8420 break;
8421
8422 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8423 if (Context.BuiltinVaListDecl)
8424 return Context.BuiltinVaListDecl;
8425 NewLoaded = Context.getBuiltinVaListDecl();
8426 break;
8427
8428 case PREDEF_DECL_VA_LIST_TAG:
8429 if (Context.VaListTagDecl)
8430 return Context.VaListTagDecl;
8431 NewLoaded = Context.getVaListTagDecl();
8432 break;
8433
8434 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8435 if (Context.BuiltinMSVaListDecl)
8436 return Context.BuiltinMSVaListDecl;
8437 NewLoaded = Context.getBuiltinMSVaListDecl();
8438 break;
8439
8440 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8441 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8442 return Context.getMSGuidTagDecl();
8443
8444 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8445 if (Context.ExternCContext)
8446 return Context.ExternCContext;
8447 NewLoaded = Context.getExternCContextDecl();
8448 break;
8449
8450 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8451 if (Context.CFConstantStringTypeDecl)
8452 return Context.CFConstantStringTypeDecl;
8453 NewLoaded = Context.getCFConstantStringDecl();
8454 break;
8455
8456 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8457 if (Context.CFConstantStringTagDecl)
8458 return Context.CFConstantStringTagDecl;
8459 NewLoaded = Context.getCFConstantStringTagDecl();
8460 break;
8461
8462 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8463 return Context.getMSTypeInfoTagDecl();
8464
8465#define BuiltinTemplate(BTName) \
8466 case PREDEF_DECL##BTName##_ID: \
8467 if (Context.Decl##BTName) \
8468 return Context.Decl##BTName; \
8469 NewLoaded = Context.get##BTName##Decl(); \
8470 break;
8471#include "clang/Basic/BuiltinTemplates.inc"
8472
8473 case NUM_PREDEF_DECL_IDS:
8474 llvm_unreachable("Invalid decl ID");
8475 break;
8476 }
8477
8478 assert(NewLoaded && "Failed to load predefined decl?");
8479
8480 if (DeserializationListener)
8481 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8482
8483 return NewLoaded;
8484}
8485
8486unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8487 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8488 if (!OwningModuleFile) {
8489 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8490 return GlobalID.getRawValue();
8491 }
8492
8493 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8494}
8495
8496Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8497 assert(ContextObj && "reading decl with no AST context");
8498
8499 if (ID < NUM_PREDEF_DECL_IDS) {
8500 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8501 if (D) {
8502 // Track that we have merged the declaration with ID \p ID into the
8503 // pre-existing predefined declaration \p D.
8504 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8505 if (Merged.empty())
8506 Merged.push_back(Elt: ID);
8507 }
8508 return D;
8509 }
8510
8511 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8512
8513 if (Index >= DeclsLoaded.size()) {
8514 assert(0 && "declaration ID out-of-range for AST file");
8515 Error(Msg: "declaration ID out-of-range for AST file");
8516 return nullptr;
8517 }
8518
8519 return DeclsLoaded[Index];
8520}
8521
8522Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8523 if (ID < NUM_PREDEF_DECL_IDS)
8524 return GetExistingDecl(ID);
8525
8526 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8527
8528 if (Index >= DeclsLoaded.size()) {
8529 assert(0 && "declaration ID out-of-range for AST file");
8530 Error(Msg: "declaration ID out-of-range for AST file");
8531 return nullptr;
8532 }
8533
8534 if (!DeclsLoaded[Index]) {
8535 ReadDeclRecord(ID);
8536 if (DeserializationListener)
8537 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8538 }
8539
8540 return DeclsLoaded[Index];
8541}
8542
8543LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8544 GlobalDeclID GlobalID) {
8545 if (GlobalID < NUM_PREDEF_DECL_IDS)
8546 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8547
8548 if (!M.ModuleOffsetMap.empty())
8549 ReadModuleOffsetMap(F&: M);
8550
8551 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8552 DeclID ID = GlobalID.getLocalDeclIndex();
8553
8554 if (Owner == &M) {
8555 ID += NUM_PREDEF_DECL_IDS;
8556 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8557 }
8558
8559 uint64_t OrignalModuleFileIndex = 0;
8560 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8561 if (M.TransitiveImports[I] == Owner) {
8562 OrignalModuleFileIndex = I + 1;
8563 break;
8564 }
8565
8566 if (!OrignalModuleFileIndex)
8567 return LocalDeclID();
8568
8569 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8570}
8571
8572GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8573 unsigned &Idx) {
8574 if (Idx >= Record.size()) {
8575 Error(Msg: "Corrupted AST file");
8576 return GlobalDeclID(0);
8577 }
8578
8579 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8580}
8581
8582/// Resolve the offset of a statement into a statement.
8583///
8584/// This operation will read a new statement from the external
8585/// source each time it is called, and is meant to be used via a
8586/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8587Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8588 // Switch case IDs are per Decl.
8589 ClearSwitchCaseIDs();
8590
8591 // Offset here is a global offset across the entire chain.
8592 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8593 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8594 Error(Err: std::move(Err));
8595 return nullptr;
8596 }
8597 assert(NumCurrentElementsDeserializing == 0 &&
8598 "should not be called while already deserializing");
8599 Deserializing D(this);
8600 return ReadStmtFromStream(F&: *Loc.F);
8601}
8602
8603bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8604 const Decl *D) {
8605 assert(D);
8606
8607 auto It = SpecLookups.find(Val: D);
8608 if (It == SpecLookups.end())
8609 return false;
8610
8611 // Get Decl may violate the iterator from SpecializationsLookups so we store
8612 // the DeclIDs in ahead.
8613 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8614 It->second.Table.findAll();
8615
8616 // Since we've loaded all the specializations, we can erase it from
8617 // the lookup table.
8618 SpecLookups.erase(I: It);
8619
8620 bool NewSpecsFound = false;
8621 Deserializing LookupResults(this);
8622 for (auto &Info : Infos) {
8623 if (GetExistingDecl(ID: Info))
8624 continue;
8625 NewSpecsFound = true;
8626 GetDecl(ID: Info);
8627 }
8628
8629 return NewSpecsFound;
8630}
8631
8632bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8633 assert(D);
8634
8635 CompleteRedeclChain(D);
8636 bool NewSpecsFound =
8637 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8638 if (OnlyPartial)
8639 return NewSpecsFound;
8640
8641 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8642 return NewSpecsFound;
8643}
8644
8645bool ASTReader::LoadExternalSpecializationsImpl(
8646 SpecLookupTableTy &SpecLookups, const Decl *D,
8647 ArrayRef<TemplateArgument> TemplateArgs) {
8648 assert(D);
8649
8650 reader::LazySpecializationInfoLookupTable *LookupTable = nullptr;
8651 if (auto It = SpecLookups.find(Val: D); It != SpecLookups.end())
8652 LookupTable = &It->getSecond();
8653 if (!LookupTable)
8654 return false;
8655
8656 // NOTE: The getNameForDiagnostic usage in the lambda may mutate the
8657 // `SpecLookups` object.
8658 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8659 std::string Name;
8660 llvm::raw_string_ostream OS(Name);
8661 auto *ND = cast<NamedDecl>(Val: D);
8662 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8663 /*Qualified=*/true);
8664 return Name;
8665 });
8666
8667 Deserializing LookupResults(this);
8668 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8669
8670 // Get Decl may violate the iterator from SpecLookups
8671 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8672 LookupTable->Table.find(EKey: HashValue);
8673
8674 bool NewSpecsFound = false;
8675 for (auto &Info : Infos) {
8676 if (GetExistingDecl(ID: Info))
8677 continue;
8678 NewSpecsFound = true;
8679 GetDecl(ID: Info);
8680 }
8681
8682 return NewSpecsFound;
8683}
8684
8685bool ASTReader::LoadExternalSpecializations(
8686 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8687 assert(D);
8688
8689 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8690 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8691 NewDeclsFound |=
8692 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8693
8694 return NewDeclsFound;
8695}
8696
8697void ASTReader::FindExternalLexicalDecls(
8698 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8699 SmallVectorImpl<Decl *> &Decls) {
8700 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8701
8702 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8703 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8704 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8705 auto K = (Decl::Kind)+LexicalDecls[I];
8706 if (!IsKindWeWant(K))
8707 continue;
8708
8709 auto ID = (DeclID) + LexicalDecls[I + 1];
8710
8711 // Don't add predefined declarations to the lexical context more
8712 // than once.
8713 if (ID < NUM_PREDEF_DECL_IDS) {
8714 if (PredefsVisited[ID])
8715 continue;
8716
8717 PredefsVisited[ID] = true;
8718 }
8719
8720 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8721 assert(D->getKind() == K && "wrong kind for lexical decl");
8722 if (!DC->isDeclInLexicalTraversal(D))
8723 Decls.push_back(Elt: D);
8724 }
8725 }
8726 };
8727
8728 if (isa<TranslationUnitDecl>(Val: DC)) {
8729 for (const auto &Lexical : TULexicalDecls)
8730 Visit(Lexical.first, Lexical.second);
8731 } else {
8732 auto I = LexicalDecls.find(Val: DC);
8733 if (I != LexicalDecls.end())
8734 Visit(I->second.first, I->second.second);
8735 }
8736
8737 ++NumLexicalDeclContextsRead;
8738}
8739
8740namespace {
8741
8742class UnalignedDeclIDComp {
8743 ASTReader &Reader;
8744 ModuleFile &Mod;
8745
8746public:
8747 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8748 : Reader(Reader), Mod(M) {}
8749
8750 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8751 SourceLocation LHS = getLocation(ID: L);
8752 SourceLocation RHS = getLocation(ID: R);
8753 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8754 }
8755
8756 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8757 SourceLocation RHS = getLocation(ID: R);
8758 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8759 }
8760
8761 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8762 SourceLocation LHS = getLocation(ID: L);
8763 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8764 }
8765
8766 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8767 return Reader.getSourceManager().getFileLoc(
8768 Loc: Reader.getSourceLocationForDeclID(
8769 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8770 }
8771};
8772
8773} // namespace
8774
8775void ASTReader::FindFileRegionDecls(FileID File,
8776 unsigned Offset, unsigned Length,
8777 SmallVectorImpl<Decl *> &Decls) {
8778 SourceManager &SM = getSourceManager();
8779
8780 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8781 if (I == FileDeclIDs.end())
8782 return;
8783
8784 FileDeclsInfo &DInfo = I->second;
8785 if (DInfo.Decls.empty())
8786 return;
8787
8788 SourceLocation
8789 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8790 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8791
8792 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8793 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8794 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8795 if (BeginIt != DInfo.Decls.begin())
8796 --BeginIt;
8797
8798 // If we are pointing at a top-level decl inside an objc container, we need
8799 // to backtrack until we find it otherwise we will fail to report that the
8800 // region overlaps with an objc container.
8801 while (BeginIt != DInfo.Decls.begin() &&
8802 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8803 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8804 ->isTopLevelDeclInObjCContainer())
8805 --BeginIt;
8806
8807 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8808 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8809 if (EndIt != DInfo.Decls.end())
8810 ++EndIt;
8811
8812 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8813 ++DIt)
8814 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8815 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8816}
8817
8818bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8819 DeclarationName Name,
8820 const DeclContext *OriginalDC) {
8821 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8822 "DeclContext has no visible decls in storage");
8823 if (!Name)
8824 return false;
8825
8826 // Load the list of declarations.
8827 DeclsSet DS;
8828
8829 auto Find = [&, this](auto &&Table, auto &&Key) {
8830 for (GlobalDeclID ID : Table.find(Key)) {
8831 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8832 if (ND->getDeclName() != Name)
8833 continue;
8834 // Special case for namespaces: There can be a lot of redeclarations of
8835 // some namespaces, and we import a "key declaration" per imported module.
8836 // Since all declarations of a namespace are essentially interchangeable,
8837 // we can optimize namespace look-up by only storing the key declaration
8838 // of the current TU, rather than storing N key declarations where N is
8839 // the # of imported modules that declare that namespace.
8840 // TODO: Try to generalize this optimization to other redeclarable decls.
8841 if (isa<NamespaceDecl>(Val: ND))
8842 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8843 DS.insert(ND);
8844 }
8845 };
8846
8847 Deserializing LookupResults(this);
8848
8849 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8850 // available.
8851 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8852 ++NumVisibleDeclContextsRead;
8853 Find(It->second.Table, Name);
8854 }
8855
8856 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8857 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8858 ++NumModuleLocalVisibleDeclContexts;
8859 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8860 }
8861 };
8862 if (auto *NamedModule =
8863 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8864 : nullptr)
8865 FindModuleLocalLookup(NamedModule);
8866 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8867 // We're going to find a decl but the decl context of the lookup is
8868 // unspecified. In this case, the OriginalDC may be the decl context in other
8869 // module.
8870 if (ContextObj && ContextObj->getCurrentNamedModule())
8871 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8872
8873 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8874 ++NumTULocalVisibleDeclContexts;
8875 Find(It->second.Table, Name);
8876 }
8877
8878 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8879 return !DS.empty();
8880}
8881
8882void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8883 if (!DC->hasExternalVisibleStorage())
8884 return;
8885
8886 DeclsMap Decls;
8887
8888 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8889 auto It = LookupTables.find(DC);
8890 if (It == LookupTables.end())
8891 return;
8892
8893 NumRead++;
8894
8895 for (GlobalDeclID ID : It->second.Table.findAll()) {
8896 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8897 // Special case for namespaces: There can be a lot of redeclarations of
8898 // some namespaces, and we import a "key declaration" per imported module.
8899 // Since all declarations of a namespace are essentially interchangeable,
8900 // we can optimize namespace look-up by only storing the key declaration
8901 // of the current TU, rather than storing N key declarations where N is
8902 // the # of imported modules that declare that namespace.
8903 // TODO: Try to generalize this optimization to other redeclarable decls.
8904 if (isa<NamespaceDecl>(Val: ND))
8905 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8906 Decls[ND->getDeclName()].insert(ND);
8907 }
8908
8909 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8910 };
8911
8912 findAll(Lookups, NumVisibleDeclContextsRead);
8913 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8914 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8915
8916 for (auto &[Name, DS] : Decls)
8917 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8918
8919 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8920}
8921
8922const serialization::reader::DeclContextLookupTable *
8923ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8924 auto I = Lookups.find(Val: Primary);
8925 return I == Lookups.end() ? nullptr : &I->second;
8926}
8927
8928const serialization::reader::ModuleLocalLookupTable *
8929ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
8930 auto I = ModuleLocalLookups.find(Val: Primary);
8931 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
8932}
8933
8934const serialization::reader::DeclContextLookupTable *
8935ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
8936 auto I = TULocalLookups.find(Val: Primary);
8937 return I == TULocalLookups.end() ? nullptr : &I->second;
8938}
8939
8940serialization::reader::LazySpecializationInfoLookupTable *
8941ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
8942 assert(D->isCanonicalDecl());
8943 auto &LookupTable =
8944 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
8945 auto I = LookupTable.find(Val: D);
8946 return I == LookupTable.end() ? nullptr : &I->second;
8947}
8948
8949bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
8950 assert(D->isCanonicalDecl());
8951 return PartialSpecializationsLookups.contains(Val: D) ||
8952 SpecializationsLookups.contains(Val: D);
8953}
8954
8955/// Under non-PCH compilation the consumer receives the objc methods
8956/// before receiving the implementation, and codegen depends on this.
8957/// We simulate this by deserializing and passing to consumer the methods of the
8958/// implementation before passing the deserialized implementation decl.
8959static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8960 ASTConsumer *Consumer) {
8961 assert(ImplD && Consumer);
8962
8963 for (auto *I : ImplD->methods())
8964 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
8965
8966 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8967}
8968
8969void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8970 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8971 PassObjCImplDeclToConsumer(ImplD, Consumer);
8972 else
8973 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
8974}
8975
8976void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
8977 Consumer->HandleVTable(RD);
8978}
8979
8980void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8981 this->Consumer = Consumer;
8982
8983 if (Consumer)
8984 PassInterestingDeclsToConsumer();
8985
8986 if (DeserializationListener)
8987 DeserializationListener->ReaderInitialized(Reader: this);
8988}
8989
8990void ASTReader::PrintStats() {
8991 std::fprintf(stderr, format: "*** AST File Statistics:\n");
8992
8993 unsigned NumTypesLoaded =
8994 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
8995 unsigned NumDeclsLoaded =
8996 DeclsLoaded.size() -
8997 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
8998 unsigned NumIdentifiersLoaded =
8999 IdentifiersLoaded.size() -
9000 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9001 unsigned NumMacrosLoaded =
9002 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9003 unsigned NumSelectorsLoaded =
9004 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9005
9006 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9007 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9008 NumSLocEntriesRead, TotalNumSLocEntries,
9009 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9010 if (!TypesLoaded.empty())
9011 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9012 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9013 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9014 if (!DeclsLoaded.empty())
9015 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9016 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9017 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9018 if (!IdentifiersLoaded.empty())
9019 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9020 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9021 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9022 if (!MacrosLoaded.empty())
9023 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9024 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9025 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9026 if (!SelectorsLoaded.empty())
9027 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9028 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9029 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9030 if (TotalNumStatements)
9031 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9032 NumStatementsRead, TotalNumStatements,
9033 ((float)NumStatementsRead/TotalNumStatements * 100));
9034 if (TotalNumMacros)
9035 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9036 NumMacrosRead, TotalNumMacros,
9037 ((float)NumMacrosRead/TotalNumMacros * 100));
9038 if (TotalLexicalDeclContexts)
9039 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9040 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9041 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9042 * 100));
9043 if (TotalVisibleDeclContexts)
9044 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9045 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9046 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9047 * 100));
9048 if (TotalModuleLocalVisibleDeclContexts)
9049 std::fprintf(
9050 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9051 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9052 ((float)NumModuleLocalVisibleDeclContexts /
9053 TotalModuleLocalVisibleDeclContexts * 100));
9054 if (TotalTULocalVisibleDeclContexts)
9055 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9056 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9057 ((float)NumTULocalVisibleDeclContexts /
9058 TotalTULocalVisibleDeclContexts * 100));
9059 if (TotalNumMethodPoolEntries)
9060 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9061 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9062 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9063 * 100));
9064 if (NumMethodPoolLookups)
9065 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9066 NumMethodPoolHits, NumMethodPoolLookups,
9067 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9068 if (NumMethodPoolTableLookups)
9069 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9070 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9071 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9072 * 100.0));
9073 if (NumIdentifierLookupHits)
9074 std::fprintf(stderr,
9075 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9076 NumIdentifierLookupHits, NumIdentifierLookups,
9077 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9078
9079 if (GlobalIndex) {
9080 std::fprintf(stderr, format: "\n");
9081 GlobalIndex->printStats();
9082 }
9083
9084 std::fprintf(stderr, format: "\n");
9085 dump();
9086 std::fprintf(stderr, format: "\n");
9087}
9088
9089template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9090LLVM_DUMP_METHOD static void
9091dumpModuleIDMap(StringRef Name,
9092 const ContinuousRangeMap<Key, ModuleFile *,
9093 InitialCapacity> &Map) {
9094 if (Map.begin() == Map.end())
9095 return;
9096
9097 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9098
9099 llvm::errs() << Name << ":\n";
9100 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9101 I != IEnd; ++I)
9102 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9103 << "\n";
9104}
9105
9106LLVM_DUMP_METHOD void ASTReader::dump() {
9107 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9108 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9109 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9110 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9111 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9112 dumpModuleIDMap(Name: "Global preprocessed entity map",
9113 Map: GlobalPreprocessedEntityMap);
9114
9115 llvm::errs() << "\n*** PCH/Modules Loaded:";
9116 for (ModuleFile &M : ModuleMgr)
9117 M.dump();
9118}
9119
9120/// Return the amount of memory used by memory buffers, breaking down
9121/// by heap-backed versus mmap'ed memory.
9122void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9123 for (ModuleFile &I : ModuleMgr) {
9124 if (llvm::MemoryBuffer *buf = I.Buffer) {
9125 size_t bytes = buf->getBufferSize();
9126 switch (buf->getBufferKind()) {
9127 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9128 sizes.malloc_bytes += bytes;
9129 break;
9130 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9131 sizes.mmap_bytes += bytes;
9132 break;
9133 }
9134 }
9135 }
9136}
9137
9138void ASTReader::InitializeSema(Sema &S) {
9139 SemaObj = &S;
9140 S.addExternalSource(E: this);
9141
9142 // Makes sure any declarations that were deserialized "too early"
9143 // still get added to the identifier's declaration chains.
9144 for (GlobalDeclID ID : PreloadedDeclIDs) {
9145 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9146 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9147 }
9148 PreloadedDeclIDs.clear();
9149
9150 // FIXME: What happens if these are changed by a module import?
9151 if (!FPPragmaOptions.empty()) {
9152 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9153 FPOptionsOverride NewOverrides =
9154 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9155 SemaObj->CurFPFeatures =
9156 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9157 }
9158
9159 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9160 Decl *D = GetDecl(ID);
9161 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9162 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9163 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9164 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9165 else
9166 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9167 }
9168 DeclsWithEffectsToVerify.clear();
9169
9170 SemaObj->OpenCLFeatures = OpenCLExtensions;
9171
9172 UpdateSema();
9173}
9174
9175void ASTReader::UpdateSema() {
9176 assert(SemaObj && "no Sema to update");
9177
9178 // Load the offsets of the declarations that Sema references.
9179 // They will be lazily deserialized when needed.
9180 if (!SemaDeclRefs.empty()) {
9181 assert(SemaDeclRefs.size() % 3 == 0);
9182 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9183 if (!SemaObj->StdNamespace)
9184 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9185 if (!SemaObj->StdBadAlloc)
9186 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9187 if (!SemaObj->StdAlignValT)
9188 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9189 }
9190 SemaDeclRefs.clear();
9191 }
9192
9193 // Update the state of pragmas. Use the same API as if we had encountered the
9194 // pragma in the source.
9195 if(OptimizeOffPragmaLocation.isValid())
9196 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9197 if (PragmaMSStructState != -1)
9198 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9199 if (PointersToMembersPragmaLocation.isValid()) {
9200 SemaObj->ActOnPragmaMSPointersToMembers(
9201 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9202 PragmaMSPointersToMembersState,
9203 PragmaLoc: PointersToMembersPragmaLocation);
9204 }
9205 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9206 if (!RISCVVecIntrinsicPragma.empty()) {
9207 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9208 "Wrong number of RISCVVecIntrinsicPragma");
9209 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9210 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9211 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9212 }
9213
9214 if (PragmaAlignPackCurrentValue) {
9215 // The bottom of the stack might have a default value. It must be adjusted
9216 // to the current value to ensure that the packing state is preserved after
9217 // popping entries that were included/imported from a PCH/module.
9218 bool DropFirst = false;
9219 if (!PragmaAlignPackStack.empty() &&
9220 PragmaAlignPackStack.front().Location.isInvalid()) {
9221 assert(PragmaAlignPackStack.front().Value ==
9222 SemaObj->AlignPackStack.DefaultValue &&
9223 "Expected a default alignment value");
9224 SemaObj->AlignPackStack.Stack.emplace_back(
9225 Args&: PragmaAlignPackStack.front().SlotLabel,
9226 Args&: SemaObj->AlignPackStack.CurrentValue,
9227 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9228 Args&: PragmaAlignPackStack.front().PushLocation);
9229 DropFirst = true;
9230 }
9231 for (const auto &Entry :
9232 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9233 SemaObj->AlignPackStack.Stack.emplace_back(
9234 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9235 }
9236 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9237 assert(*PragmaAlignPackCurrentValue ==
9238 SemaObj->AlignPackStack.DefaultValue &&
9239 "Expected a default align and pack value");
9240 // Keep the current values.
9241 } else {
9242 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9243 SemaObj->AlignPackStack.CurrentPragmaLocation =
9244 PragmaAlignPackCurrentLocation;
9245 }
9246 }
9247 if (FpPragmaCurrentValue) {
9248 // The bottom of the stack might have a default value. It must be adjusted
9249 // to the current value to ensure that fp-pragma state is preserved after
9250 // popping entries that were included/imported from a PCH/module.
9251 bool DropFirst = false;
9252 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9253 assert(FpPragmaStack.front().Value ==
9254 SemaObj->FpPragmaStack.DefaultValue &&
9255 "Expected a default pragma float_control value");
9256 SemaObj->FpPragmaStack.Stack.emplace_back(
9257 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9258 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9259 Args&: FpPragmaStack.front().PushLocation);
9260 DropFirst = true;
9261 }
9262 for (const auto &Entry :
9263 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9264 SemaObj->FpPragmaStack.Stack.emplace_back(
9265 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9266 if (FpPragmaCurrentLocation.isInvalid()) {
9267 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9268 "Expected a default pragma float_control value");
9269 // Keep the current values.
9270 } else {
9271 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9272 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9273 }
9274 }
9275
9276 // For non-modular AST files, restore visiblity of modules.
9277 for (auto &Import : PendingImportedModulesSema) {
9278 if (Import.ImportLoc.isInvalid())
9279 continue;
9280 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9281 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9282 }
9283 }
9284 PendingImportedModulesSema.clear();
9285}
9286
9287IdentifierInfo *ASTReader::get(StringRef Name) {
9288 // Note that we are loading an identifier.
9289 Deserializing AnIdentifier(this);
9290
9291 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9292 NumIdentifierLookups,
9293 NumIdentifierLookupHits);
9294
9295 // We don't need to do identifier table lookups in C++ modules (we preload
9296 // all interesting declarations, and don't need to use the scope for name
9297 // lookups). Perform the lookup in PCH files, though, since we don't build
9298 // a complete initial identifier table if we're carrying on from a PCH.
9299 if (PP.getLangOpts().CPlusPlus) {
9300 for (auto *F : ModuleMgr.pch_modules())
9301 if (Visitor(*F))
9302 break;
9303 } else {
9304 // If there is a global index, look there first to determine which modules
9305 // provably do not have any results for this identifier.
9306 GlobalModuleIndex::HitSet Hits;
9307 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9308 if (!loadGlobalIndex()) {
9309 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9310 HitsPtr = &Hits;
9311 }
9312 }
9313
9314 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9315 }
9316
9317 IdentifierInfo *II = Visitor.getIdentifierInfo();
9318 markIdentifierUpToDate(II);
9319 return II;
9320}
9321
9322namespace clang {
9323
9324 /// An identifier-lookup iterator that enumerates all of the
9325 /// identifiers stored within a set of AST files.
9326 class ASTIdentifierIterator : public IdentifierIterator {
9327 /// The AST reader whose identifiers are being enumerated.
9328 const ASTReader &Reader;
9329
9330 /// The current index into the chain of AST files stored in
9331 /// the AST reader.
9332 unsigned Index;
9333
9334 /// The current position within the identifier lookup table
9335 /// of the current AST file.
9336 ASTIdentifierLookupTable::key_iterator Current;
9337
9338 /// The end position within the identifier lookup table of
9339 /// the current AST file.
9340 ASTIdentifierLookupTable::key_iterator End;
9341
9342 /// Whether to skip any modules in the ASTReader.
9343 bool SkipModules;
9344
9345 public:
9346 explicit ASTIdentifierIterator(const ASTReader &Reader,
9347 bool SkipModules = false);
9348
9349 StringRef Next() override;
9350 };
9351
9352} // namespace clang
9353
9354ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9355 bool SkipModules)
9356 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9357}
9358
9359StringRef ASTIdentifierIterator::Next() {
9360 while (Current == End) {
9361 // If we have exhausted all of our AST files, we're done.
9362 if (Index == 0)
9363 return StringRef();
9364
9365 --Index;
9366 ModuleFile &F = Reader.ModuleMgr[Index];
9367 if (SkipModules && F.isModule())
9368 continue;
9369
9370 ASTIdentifierLookupTable *IdTable =
9371 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9372 Current = IdTable->key_begin();
9373 End = IdTable->key_end();
9374 }
9375
9376 // We have any identifiers remaining in the current AST file; return
9377 // the next one.
9378 StringRef Result = *Current;
9379 ++Current;
9380 return Result;
9381}
9382
9383namespace {
9384
9385/// A utility for appending two IdentifierIterators.
9386class ChainedIdentifierIterator : public IdentifierIterator {
9387 std::unique_ptr<IdentifierIterator> Current;
9388 std::unique_ptr<IdentifierIterator> Queued;
9389
9390public:
9391 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9392 std::unique_ptr<IdentifierIterator> Second)
9393 : Current(std::move(First)), Queued(std::move(Second)) {}
9394
9395 StringRef Next() override {
9396 if (!Current)
9397 return StringRef();
9398
9399 StringRef result = Current->Next();
9400 if (!result.empty())
9401 return result;
9402
9403 // Try the queued iterator, which may itself be empty.
9404 Current.reset();
9405 std::swap(x&: Current, y&: Queued);
9406 return Next();
9407 }
9408};
9409
9410} // namespace
9411
9412IdentifierIterator *ASTReader::getIdentifiers() {
9413 if (!loadGlobalIndex()) {
9414 std::unique_ptr<IdentifierIterator> ReaderIter(
9415 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9416 std::unique_ptr<IdentifierIterator> ModulesIter(
9417 GlobalIndex->createIdentifierIterator());
9418 return new ChainedIdentifierIterator(std::move(ReaderIter),
9419 std::move(ModulesIter));
9420 }
9421
9422 return new ASTIdentifierIterator(*this);
9423}
9424
9425namespace clang {
9426namespace serialization {
9427
9428 class ReadMethodPoolVisitor {
9429 ASTReader &Reader;
9430 Selector Sel;
9431 unsigned PriorGeneration;
9432 unsigned InstanceBits = 0;
9433 unsigned FactoryBits = 0;
9434 bool InstanceHasMoreThanOneDecl = false;
9435 bool FactoryHasMoreThanOneDecl = false;
9436 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9437 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9438
9439 public:
9440 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9441 unsigned PriorGeneration)
9442 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9443
9444 bool operator()(ModuleFile &M) {
9445 if (!M.SelectorLookupTable)
9446 return false;
9447
9448 // If we've already searched this module file, skip it now.
9449 if (M.Generation <= PriorGeneration)
9450 return true;
9451
9452 ++Reader.NumMethodPoolTableLookups;
9453 ASTSelectorLookupTable *PoolTable
9454 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9455 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9456 if (Pos == PoolTable->end())
9457 return false;
9458
9459 ++Reader.NumMethodPoolTableHits;
9460 ++Reader.NumSelectorsRead;
9461 // FIXME: Not quite happy with the statistics here. We probably should
9462 // disable this tracking when called via LoadSelector.
9463 // Also, should entries without methods count as misses?
9464 ++Reader.NumMethodPoolEntriesRead;
9465 ASTSelectorLookupTrait::data_type Data = *Pos;
9466 if (Reader.DeserializationListener)
9467 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9468
9469 // Append methods in the reverse order, so that later we can process them
9470 // in the order they appear in the source code by iterating through
9471 // the vector in the reverse order.
9472 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9473 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9474 InstanceBits = Data.InstanceBits;
9475 FactoryBits = Data.FactoryBits;
9476 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9477 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9478 return false;
9479 }
9480
9481 /// Retrieve the instance methods found by this visitor.
9482 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9483 return InstanceMethods;
9484 }
9485
9486 /// Retrieve the instance methods found by this visitor.
9487 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9488 return FactoryMethods;
9489 }
9490
9491 unsigned getInstanceBits() const { return InstanceBits; }
9492 unsigned getFactoryBits() const { return FactoryBits; }
9493
9494 bool instanceHasMoreThanOneDecl() const {
9495 return InstanceHasMoreThanOneDecl;
9496 }
9497
9498 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9499 };
9500
9501} // namespace serialization
9502} // namespace clang
9503
9504/// Add the given set of methods to the method list.
9505static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9506 ObjCMethodList &List) {
9507 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9508 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9509}
9510
9511void ASTReader::ReadMethodPool(Selector Sel) {
9512 // Get the selector generation and update it to the current generation.
9513 unsigned &Generation = SelectorGeneration[Sel];
9514 unsigned PriorGeneration = Generation;
9515 Generation = getGeneration();
9516 SelectorOutOfDate[Sel] = false;
9517
9518 // Search for methods defined with this selector.
9519 ++NumMethodPoolLookups;
9520 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9521 ModuleMgr.visit(Visitor);
9522
9523 if (Visitor.getInstanceMethods().empty() &&
9524 Visitor.getFactoryMethods().empty())
9525 return;
9526
9527 ++NumMethodPoolHits;
9528
9529 if (!getSema())
9530 return;
9531
9532 Sema &S = *getSema();
9533 auto &Methods = S.ObjC().MethodPool[Sel];
9534
9535 Methods.first.setBits(Visitor.getInstanceBits());
9536 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9537 Methods.second.setBits(Visitor.getFactoryBits());
9538 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9539
9540 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9541 // when building a module we keep every method individually and may need to
9542 // update hasMoreThanOneDecl as we add the methods.
9543 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9544 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9545}
9546
9547void ASTReader::updateOutOfDateSelector(Selector Sel) {
9548 if (SelectorOutOfDate[Sel])
9549 ReadMethodPool(Sel);
9550}
9551
9552void ASTReader::ReadKnownNamespaces(
9553 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9554 Namespaces.clear();
9555
9556 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9557 if (NamespaceDecl *Namespace
9558 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9559 Namespaces.push_back(Elt: Namespace);
9560 }
9561}
9562
9563void ASTReader::ReadUndefinedButUsed(
9564 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9565 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9566 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9567 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9568 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9569 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9570 }
9571 UndefinedButUsed.clear();
9572}
9573
9574void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9575 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9576 Exprs) {
9577 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9578 FieldDecl *FD =
9579 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9580 uint64_t Count = DelayedDeleteExprs[Idx++];
9581 for (uint64_t C = 0; C < Count; ++C) {
9582 SourceLocation DeleteLoc =
9583 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9584 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9585 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9586 }
9587 }
9588}
9589
9590void ASTReader::ReadTentativeDefinitions(
9591 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9592 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9593 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9594 if (Var)
9595 TentativeDefs.push_back(Elt: Var);
9596 }
9597 TentativeDefinitions.clear();
9598}
9599
9600void ASTReader::ReadUnusedFileScopedDecls(
9601 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9602 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9603 DeclaratorDecl *D
9604 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9605 if (D)
9606 Decls.push_back(Elt: D);
9607 }
9608 UnusedFileScopedDecls.clear();
9609}
9610
9611void ASTReader::ReadDelegatingConstructors(
9612 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9613 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9614 CXXConstructorDecl *D
9615 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9616 if (D)
9617 Decls.push_back(Elt: D);
9618 }
9619 DelegatingCtorDecls.clear();
9620}
9621
9622void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9623 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9624 TypedefNameDecl *D
9625 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9626 if (D)
9627 Decls.push_back(Elt: D);
9628 }
9629 ExtVectorDecls.clear();
9630}
9631
9632void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9633 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9634 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9635 ++I) {
9636 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9637 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9638 if (D)
9639 Decls.insert(X: D);
9640 }
9641 UnusedLocalTypedefNameCandidates.clear();
9642}
9643
9644void ASTReader::ReadDeclsToCheckForDeferredDiags(
9645 llvm::SmallSetVector<Decl *, 4> &Decls) {
9646 for (auto I : DeclsToCheckForDeferredDiags) {
9647 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9648 if (D)
9649 Decls.insert(X: D);
9650 }
9651 DeclsToCheckForDeferredDiags.clear();
9652}
9653
9654void ASTReader::ReadReferencedSelectors(
9655 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9656 if (ReferencedSelectorsData.empty())
9657 return;
9658
9659 // If there are @selector references added them to its pool. This is for
9660 // implementation of -Wselector.
9661 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9662 unsigned I = 0;
9663 while (I < DataSize) {
9664 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9665 SourceLocation SelLoc
9666 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9667 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9668 }
9669 ReferencedSelectorsData.clear();
9670}
9671
9672void ASTReader::ReadWeakUndeclaredIdentifiers(
9673 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9674 if (WeakUndeclaredIdentifiers.empty())
9675 return;
9676
9677 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9678 IdentifierInfo *WeakId
9679 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9680 IdentifierInfo *AliasId
9681 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9682 SourceLocation Loc =
9683 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9684 WeakInfo WI(AliasId, Loc);
9685 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9686 }
9687 WeakUndeclaredIdentifiers.clear();
9688}
9689
9690void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9691 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9692 ExternalVTableUse VT;
9693 VTableUse &TableInfo = VTableUses[Idx++];
9694 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9695 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9696 VT.DefinitionRequired = TableInfo.Used;
9697 VTables.push_back(Elt: VT);
9698 }
9699
9700 VTableUses.clear();
9701}
9702
9703void ASTReader::ReadPendingInstantiations(
9704 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9705 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9706 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9707 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9708 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9709
9710 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9711 }
9712 PendingInstantiations.clear();
9713}
9714
9715void ASTReader::ReadLateParsedTemplates(
9716 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9717 &LPTMap) {
9718 for (auto &LPT : LateParsedTemplates) {
9719 ModuleFile *FMod = LPT.first;
9720 RecordDataImpl &LateParsed = LPT.second;
9721 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9722 /* In loop */) {
9723 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9724
9725 auto LT = std::make_unique<LateParsedTemplate>();
9726 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9727 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9728
9729 ModuleFile *F = getOwningModuleFile(D: LT->D);
9730 assert(F && "No module");
9731
9732 unsigned TokN = LateParsed[Idx++];
9733 LT->Toks.reserve(N: TokN);
9734 for (unsigned T = 0; T < TokN; ++T)
9735 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9736
9737 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9738 }
9739 }
9740
9741 LateParsedTemplates.clear();
9742}
9743
9744void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9745 if (!Lambda->getLambdaContextDecl())
9746 return;
9747
9748 auto LambdaInfo =
9749 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9750 y: Lambda->getLambdaIndexInContext());
9751
9752 // Handle the import and then include case for lambdas.
9753 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9754 Iter != LambdaDeclarationsForMerging.end() &&
9755 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9756 CXXRecordDecl *Previous =
9757 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9758 Lambda->setPreviousDecl(Previous);
9759 return;
9760 }
9761
9762 // Keep track of this lambda so it can be merged with another lambda that
9763 // is loaded later.
9764 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9765}
9766
9767void ASTReader::LoadSelector(Selector Sel) {
9768 // It would be complicated to avoid reading the methods anyway. So don't.
9769 ReadMethodPool(Sel);
9770}
9771
9772void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9773 assert(ID && "Non-zero identifier ID required");
9774 unsigned Index = translateIdentifierIDToIndex(ID).second;
9775 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9776 IdentifiersLoaded[Index] = II;
9777 if (DeserializationListener)
9778 DeserializationListener->IdentifierRead(ID, II);
9779}
9780
9781/// Set the globally-visible declarations associated with the given
9782/// identifier.
9783///
9784/// If the AST reader is currently in a state where the given declaration IDs
9785/// cannot safely be resolved, they are queued until it is safe to resolve
9786/// them.
9787///
9788/// \param II an IdentifierInfo that refers to one or more globally-visible
9789/// declarations.
9790///
9791/// \param DeclIDs the set of declaration IDs with the name @p II that are
9792/// visible at global scope.
9793///
9794/// \param Decls if non-null, this vector will be populated with the set of
9795/// deserialized declarations. These declarations will not be pushed into
9796/// scope.
9797void ASTReader::SetGloballyVisibleDecls(
9798 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9799 SmallVectorImpl<Decl *> *Decls) {
9800 if (NumCurrentElementsDeserializing && !Decls) {
9801 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9802 return;
9803 }
9804
9805 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9806 if (!SemaObj) {
9807 // Queue this declaration so that it will be added to the
9808 // translation unit scope and identifier's declaration chain
9809 // once a Sema object is known.
9810 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9811 continue;
9812 }
9813
9814 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9815
9816 // If we're simply supposed to record the declarations, do so now.
9817 if (Decls) {
9818 Decls->push_back(Elt: D);
9819 continue;
9820 }
9821
9822 // Introduce this declaration into the translation-unit scope
9823 // and add it to the declaration chain for this identifier, so
9824 // that (unqualified) name lookup will find it.
9825 pushExternalDeclIntoScope(D, Name: II);
9826 }
9827}
9828
9829std::pair<ModuleFile *, unsigned>
9830ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9831 if (ID == 0)
9832 return {nullptr, 0};
9833
9834 unsigned ModuleFileIndex = ID >> 32;
9835 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9836
9837 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9838 assert(getModuleManager().size() > ModuleFileIndex - 1);
9839
9840 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9841 assert(LocalID < MF.LocalNumIdentifiers);
9842 return {&MF, MF.BaseIdentifierID + LocalID};
9843}
9844
9845IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9846 if (ID == 0)
9847 return nullptr;
9848
9849 if (IdentifiersLoaded.empty()) {
9850 Error(Msg: "no identifier table in AST file");
9851 return nullptr;
9852 }
9853
9854 auto [M, Index] = translateIdentifierIDToIndex(ID);
9855 if (!IdentifiersLoaded[Index]) {
9856 assert(M != nullptr && "Untranslated Identifier ID?");
9857 assert(Index >= M->BaseIdentifierID);
9858 unsigned LocalIndex = Index - M->BaseIdentifierID;
9859 const unsigned char *Data =
9860 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9861
9862 ASTIdentifierLookupTrait Trait(*this, *M);
9863 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9864 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9865 auto &II = PP.getIdentifierTable().get(Name: Key);
9866 IdentifiersLoaded[Index] = &II;
9867 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9868 markIdentifierFromAST(Reader&: *this, II, IsModule);
9869 if (DeserializationListener)
9870 DeserializationListener->IdentifierRead(ID, II: &II);
9871 }
9872
9873 return IdentifiersLoaded[Index];
9874}
9875
9876IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9877 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9878}
9879
9880IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9881 if (LocalID < NUM_PREDEF_IDENT_IDS)
9882 return LocalID;
9883
9884 if (!M.ModuleOffsetMap.empty())
9885 ReadModuleOffsetMap(F&: M);
9886
9887 unsigned ModuleFileIndex = LocalID >> 32;
9888 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9889 ModuleFile *MF =
9890 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9891 assert(MF && "malformed identifier ID encoding?");
9892
9893 if (!ModuleFileIndex)
9894 LocalID -= NUM_PREDEF_IDENT_IDS;
9895
9896 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
9897}
9898
9899std::pair<ModuleFile *, unsigned>
9900ASTReader::translateMacroIDToIndex(MacroID ID) const {
9901 if (ID == 0)
9902 return {nullptr, 0};
9903
9904 unsigned ModuleFileIndex = ID >> 32;
9905 assert(ModuleFileIndex && "not translating loaded MacroID?");
9906 assert(getModuleManager().size() > ModuleFileIndex - 1);
9907 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9908
9909 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
9910 assert(LocalID < MF.LocalNumMacros);
9911 return {&MF, MF.BaseMacroID + LocalID};
9912}
9913
9914MacroInfo *ASTReader::getMacro(MacroID ID) {
9915 if (ID == 0)
9916 return nullptr;
9917
9918 if (MacrosLoaded.empty()) {
9919 Error(Msg: "no macro table in AST file");
9920 return nullptr;
9921 }
9922
9923 auto [M, Index] = translateMacroIDToIndex(ID);
9924 if (!MacrosLoaded[Index]) {
9925 assert(M != nullptr && "Untranslated Macro ID?");
9926 assert(Index >= M->BaseMacroID);
9927 unsigned LocalIndex = Index - M->BaseMacroID;
9928 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
9929 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
9930
9931 if (DeserializationListener)
9932 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
9933 }
9934
9935 return MacrosLoaded[Index];
9936}
9937
9938MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
9939 if (LocalID < NUM_PREDEF_MACRO_IDS)
9940 return LocalID;
9941
9942 if (!M.ModuleOffsetMap.empty())
9943 ReadModuleOffsetMap(F&: M);
9944
9945 unsigned ModuleFileIndex = LocalID >> 32;
9946 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
9947 ModuleFile *MF =
9948 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9949 assert(MF && "malformed identifier ID encoding?");
9950
9951 if (!ModuleFileIndex) {
9952 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
9953 LocalID -= NUM_PREDEF_MACRO_IDS;
9954 }
9955
9956 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
9957}
9958
9959serialization::SubmoduleID
9960ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
9961 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
9962 return LocalID;
9963
9964 if (!M.ModuleOffsetMap.empty())
9965 ReadModuleOffsetMap(F&: M);
9966
9967 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9968 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
9969 assert(I != M.SubmoduleRemap.end()
9970 && "Invalid index into submodule index remap");
9971
9972 return LocalID + I->second;
9973}
9974
9975Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
9976 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
9977 assert(GlobalID == 0 && "Unhandled global submodule ID");
9978 return nullptr;
9979 }
9980
9981 if (GlobalID > SubmodulesLoaded.size()) {
9982 Error(Msg: "submodule ID out of range in AST file");
9983 return nullptr;
9984 }
9985
9986 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
9987}
9988
9989Module *ASTReader::getModule(unsigned ID) {
9990 return getSubmodule(GlobalID: ID);
9991}
9992
9993ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
9994 if (ID & 1) {
9995 // It's a module, look it up by submodule ID.
9996 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
9997 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
9998 } else {
9999 // It's a prefix (preamble, PCH, ...). Look it up by index.
10000 int IndexFromEnd = static_cast<int>(ID >> 1);
10001 assert(IndexFromEnd && "got reference to unknown module file");
10002 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10003 }
10004}
10005
10006unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10007 if (!M)
10008 return 1;
10009
10010 // For a file representing a module, use the submodule ID of the top-level
10011 // module as the file ID. For any other kind of file, the number of such
10012 // files loaded beforehand will be the same on reload.
10013 // FIXME: Is this true even if we have an explicit module file and a PCH?
10014 if (M->isModule())
10015 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10016
10017 auto PCHModules = getModuleManager().pch_modules();
10018 auto I = llvm::find(Range&: PCHModules, Val: M);
10019 assert(I != PCHModules.end() && "emitting reference to unknown file");
10020 return std::distance(first: I, last: PCHModules.end()) << 1;
10021}
10022
10023std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10024 if (Module *M = getSubmodule(GlobalID: ID))
10025 return ASTSourceDescriptor(*M);
10026
10027 // If there is only a single PCH, return it instead.
10028 // Chained PCH are not supported.
10029 const auto &PCHChain = ModuleMgr.pch_modules();
10030 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10031 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10032 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10033 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10034 return ASTSourceDescriptor(ModuleName,
10035 llvm::sys::path::parent_path(path: MF.FileName),
10036 FileName, MF.Signature);
10037 }
10038 return std::nullopt;
10039}
10040
10041ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10042 auto I = DefinitionSource.find(Val: FD);
10043 if (I == DefinitionSource.end())
10044 return EK_ReplyHazy;
10045 return I->second ? EK_Never : EK_Always;
10046}
10047
10048bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10049 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10050}
10051
10052Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10053 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10054}
10055
10056Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10057 if (ID == 0)
10058 return Selector();
10059
10060 if (ID > SelectorsLoaded.size()) {
10061 Error(Msg: "selector ID out of range in AST file");
10062 return Selector();
10063 }
10064
10065 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10066 // Load this selector from the selector table.
10067 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10068 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10069 ModuleFile &M = *I->second;
10070 ASTSelectorLookupTrait Trait(*this, M);
10071 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10072 SelectorsLoaded[ID - 1] =
10073 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10074 if (DeserializationListener)
10075 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10076 }
10077
10078 return SelectorsLoaded[ID - 1];
10079}
10080
10081Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10082 return DecodeSelector(ID);
10083}
10084
10085uint32_t ASTReader::GetNumExternalSelectors() {
10086 // ID 0 (the null selector) is considered an external selector.
10087 return getTotalNumSelectors() + 1;
10088}
10089
10090serialization::SelectorID
10091ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10092 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10093 return LocalID;
10094
10095 if (!M.ModuleOffsetMap.empty())
10096 ReadModuleOffsetMap(F&: M);
10097
10098 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10099 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10100 assert(I != M.SelectorRemap.end()
10101 && "Invalid index into selector index remap");
10102
10103 return LocalID + I->second;
10104}
10105
10106DeclarationNameLoc
10107ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10108 switch (Name.getNameKind()) {
10109 case DeclarationName::CXXConstructorName:
10110 case DeclarationName::CXXDestructorName:
10111 case DeclarationName::CXXConversionFunctionName:
10112 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10113
10114 case DeclarationName::CXXOperatorName:
10115 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10116
10117 case DeclarationName::CXXLiteralOperatorName:
10118 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10119 Loc: readSourceLocation());
10120
10121 case DeclarationName::Identifier:
10122 case DeclarationName::ObjCZeroArgSelector:
10123 case DeclarationName::ObjCOneArgSelector:
10124 case DeclarationName::ObjCMultiArgSelector:
10125 case DeclarationName::CXXUsingDirective:
10126 case DeclarationName::CXXDeductionGuideName:
10127 break;
10128 }
10129 return DeclarationNameLoc();
10130}
10131
10132DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10133 DeclarationNameInfo NameInfo;
10134 NameInfo.setName(readDeclarationName());
10135 NameInfo.setLoc(readSourceLocation());
10136 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10137 return NameInfo;
10138}
10139
10140TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10141 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10142}
10143
10144SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10145 auto Kind = readInt();
10146 auto ResultType = readQualType();
10147 auto Value = readAPInt();
10148 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10149 assert(Op.isValid());
10150 return Op;
10151}
10152
10153void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10154 Info.QualifierLoc = readNestedNameSpecifierLoc();
10155 unsigned NumTPLists = readInt();
10156 Info.NumTemplParamLists = NumTPLists;
10157 if (NumTPLists) {
10158 Info.TemplParamLists =
10159 new (getContext()) TemplateParameterList *[NumTPLists];
10160 for (unsigned i = 0; i != NumTPLists; ++i)
10161 Info.TemplParamLists[i] = readTemplateParameterList();
10162 }
10163}
10164
10165TemplateParameterList *
10166ASTRecordReader::readTemplateParameterList() {
10167 SourceLocation TemplateLoc = readSourceLocation();
10168 SourceLocation LAngleLoc = readSourceLocation();
10169 SourceLocation RAngleLoc = readSourceLocation();
10170
10171 unsigned NumParams = readInt();
10172 SmallVector<NamedDecl *, 16> Params;
10173 Params.reserve(N: NumParams);
10174 while (NumParams--)
10175 Params.push_back(Elt: readDeclAs<NamedDecl>());
10176
10177 bool HasRequiresClause = readBool();
10178 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10179
10180 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10181 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10182 return TemplateParams;
10183}
10184
10185void ASTRecordReader::readTemplateArgumentList(
10186 SmallVectorImpl<TemplateArgument> &TemplArgs,
10187 bool Canonicalize) {
10188 unsigned NumTemplateArgs = readInt();
10189 TemplArgs.reserve(N: NumTemplateArgs);
10190 while (NumTemplateArgs--)
10191 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10192}
10193
10194/// Read a UnresolvedSet structure.
10195void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10196 unsigned NumDecls = readInt();
10197 Set.reserve(C&: getContext(), N: NumDecls);
10198 while (NumDecls--) {
10199 GlobalDeclID ID = readDeclID();
10200 AccessSpecifier AS = (AccessSpecifier) readInt();
10201 Set.addLazyDecl(C&: getContext(), ID, AS);
10202 }
10203}
10204
10205CXXBaseSpecifier
10206ASTRecordReader::readCXXBaseSpecifier() {
10207 bool isVirtual = readBool();
10208 bool isBaseOfClass = readBool();
10209 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10210 bool inheritConstructors = readBool();
10211 TypeSourceInfo *TInfo = readTypeSourceInfo();
10212 SourceRange Range = readSourceRange();
10213 SourceLocation EllipsisLoc = readSourceLocation();
10214 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10215 EllipsisLoc);
10216 Result.setInheritConstructors(inheritConstructors);
10217 return Result;
10218}
10219
10220CXXCtorInitializer **
10221ASTRecordReader::readCXXCtorInitializers() {
10222 ASTContext &Context = getContext();
10223 unsigned NumInitializers = readInt();
10224 assert(NumInitializers && "wrote ctor initializers but have no inits");
10225 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10226 for (unsigned i = 0; i != NumInitializers; ++i) {
10227 TypeSourceInfo *TInfo = nullptr;
10228 bool IsBaseVirtual = false;
10229 FieldDecl *Member = nullptr;
10230 IndirectFieldDecl *IndirectMember = nullptr;
10231
10232 CtorInitializerType Type = (CtorInitializerType) readInt();
10233 switch (Type) {
10234 case CTOR_INITIALIZER_BASE:
10235 TInfo = readTypeSourceInfo();
10236 IsBaseVirtual = readBool();
10237 break;
10238
10239 case CTOR_INITIALIZER_DELEGATING:
10240 TInfo = readTypeSourceInfo();
10241 break;
10242
10243 case CTOR_INITIALIZER_MEMBER:
10244 Member = readDeclAs<FieldDecl>();
10245 break;
10246
10247 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10248 IndirectMember = readDeclAs<IndirectFieldDecl>();
10249 break;
10250 }
10251
10252 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10253 Expr *Init = readExpr();
10254 SourceLocation LParenLoc = readSourceLocation();
10255 SourceLocation RParenLoc = readSourceLocation();
10256
10257 CXXCtorInitializer *BOMInit;
10258 if (Type == CTOR_INITIALIZER_BASE)
10259 BOMInit = new (Context)
10260 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10261 RParenLoc, MemberOrEllipsisLoc);
10262 else if (Type == CTOR_INITIALIZER_DELEGATING)
10263 BOMInit = new (Context)
10264 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10265 else if (Member)
10266 BOMInit = new (Context)
10267 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10268 Init, RParenLoc);
10269 else
10270 BOMInit = new (Context)
10271 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10272 LParenLoc, Init, RParenLoc);
10273
10274 if (/*IsWritten*/readBool()) {
10275 unsigned SourceOrder = readInt();
10276 BOMInit->setSourceOrder(SourceOrder);
10277 }
10278
10279 CtorInitializers[i] = BOMInit;
10280 }
10281
10282 return CtorInitializers;
10283}
10284
10285NestedNameSpecifierLoc
10286ASTRecordReader::readNestedNameSpecifierLoc() {
10287 ASTContext &Context = getContext();
10288 unsigned N = readInt();
10289 NestedNameSpecifierLocBuilder Builder;
10290 for (unsigned I = 0; I != N; ++I) {
10291 auto Kind = readNestedNameSpecifierKind();
10292 switch (Kind) {
10293 case NestedNameSpecifier::Kind::Namespace: {
10294 auto *NS = readDeclAs<NamespaceBaseDecl>();
10295 SourceRange Range = readSourceRange();
10296 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10297 break;
10298 }
10299
10300 case NestedNameSpecifier::Kind::Type: {
10301 TypeSourceInfo *T = readTypeSourceInfo();
10302 if (!T)
10303 return NestedNameSpecifierLoc();
10304 SourceLocation ColonColonLoc = readSourceLocation();
10305 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10306 break;
10307 }
10308
10309 case NestedNameSpecifier::Kind::Global: {
10310 SourceLocation ColonColonLoc = readSourceLocation();
10311 Builder.MakeGlobal(Context, ColonColonLoc);
10312 break;
10313 }
10314
10315 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10316 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10317 SourceRange Range = readSourceRange();
10318 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10319 break;
10320 }
10321
10322 case NestedNameSpecifier::Kind::Null:
10323 llvm_unreachable("unexpected null nested name specifier");
10324 }
10325 }
10326
10327 return Builder.getWithLocInContext(Context);
10328}
10329
10330SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10331 unsigned &Idx) {
10332 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10333 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10334 return SourceRange(beg, end);
10335}
10336
10337llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10338 const StringRef Blob) {
10339 unsigned Count = Record[0];
10340 const char *Byte = Blob.data();
10341 llvm::BitVector Ret = llvm::BitVector(Count, false);
10342 for (unsigned I = 0; I < Count; ++Byte)
10343 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10344 if (*Byte & (1 << Bit))
10345 Ret[I] = true;
10346 return Ret;
10347}
10348
10349/// Read a floating-point value
10350llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10351 return llvm::APFloat(Sem, readAPInt());
10352}
10353
10354// Read a string
10355std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10356 unsigned Len = Record[Idx++];
10357 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10358 Idx += Len;
10359 return Result;
10360}
10361
10362StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10363 StringRef &Blob) {
10364 unsigned Len = Record[Idx++];
10365 StringRef Result = Blob.substr(Start: 0, N: Len);
10366 Blob = Blob.substr(Start: Len);
10367 return Result;
10368}
10369
10370std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10371 unsigned &Idx) {
10372 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10373}
10374
10375std::string ASTReader::ReadPath(StringRef BaseDirectory,
10376 const RecordData &Record, unsigned &Idx) {
10377 std::string Filename = ReadString(Record, Idx);
10378 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10379}
10380
10381std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10382 const RecordData &Record, unsigned &Idx,
10383 StringRef &Blob) {
10384 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10385 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10386}
10387
10388VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10389 unsigned &Idx) {
10390 unsigned Major = Record[Idx++];
10391 unsigned Minor = Record[Idx++];
10392 unsigned Subminor = Record[Idx++];
10393 if (Minor == 0)
10394 return VersionTuple(Major);
10395 if (Subminor == 0)
10396 return VersionTuple(Major, Minor - 1);
10397 return VersionTuple(Major, Minor - 1, Subminor - 1);
10398}
10399
10400CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10401 const RecordData &Record,
10402 unsigned &Idx) {
10403 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10404 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10405}
10406
10407DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10408 return Diag(Loc: CurrentImportLoc, DiagID);
10409}
10410
10411DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10412 return Diags.Report(Loc, DiagID);
10413}
10414
10415void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10416 llvm::function_ref<void()> Fn) {
10417 // When Sema is available, avoid duplicate errors.
10418 if (SemaObj) {
10419 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10420 return;
10421 }
10422
10423 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10424}
10425
10426/// Retrieve the identifier table associated with the
10427/// preprocessor.
10428IdentifierTable &ASTReader::getIdentifierTable() {
10429 return PP.getIdentifierTable();
10430}
10431
10432/// Record that the given ID maps to the given switch-case
10433/// statement.
10434void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10435 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10436 "Already have a SwitchCase with this ID");
10437 (*CurrSwitchCaseStmts)[ID] = SC;
10438}
10439
10440/// Retrieve the switch-case statement with the given ID.
10441SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10442 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10443 return (*CurrSwitchCaseStmts)[ID];
10444}
10445
10446void ASTReader::ClearSwitchCaseIDs() {
10447 CurrSwitchCaseStmts->clear();
10448}
10449
10450void ASTReader::ReadComments() {
10451 ASTContext &Context = getContext();
10452 std::vector<RawComment *> Comments;
10453 for (SmallVectorImpl<std::pair<BitstreamCursor,
10454 serialization::ModuleFile *>>::iterator
10455 I = CommentsCursors.begin(),
10456 E = CommentsCursors.end();
10457 I != E; ++I) {
10458 Comments.clear();
10459 BitstreamCursor &Cursor = I->first;
10460 serialization::ModuleFile &F = *I->second;
10461 SavedStreamPosition SavedPosition(Cursor);
10462
10463 RecordData Record;
10464 while (true) {
10465 Expected<llvm::BitstreamEntry> MaybeEntry =
10466 Cursor.advanceSkippingSubblocks(
10467 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10468 if (!MaybeEntry) {
10469 Error(Err: MaybeEntry.takeError());
10470 return;
10471 }
10472 llvm::BitstreamEntry Entry = MaybeEntry.get();
10473
10474 switch (Entry.Kind) {
10475 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10476 case llvm::BitstreamEntry::Error:
10477 Error(Msg: "malformed block record in AST file");
10478 return;
10479 case llvm::BitstreamEntry::EndBlock:
10480 goto NextCursor;
10481 case llvm::BitstreamEntry::Record:
10482 // The interesting case.
10483 break;
10484 }
10485
10486 // Read a record.
10487 Record.clear();
10488 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10489 if (!MaybeComment) {
10490 Error(Err: MaybeComment.takeError());
10491 return;
10492 }
10493 switch ((CommentRecordTypes)MaybeComment.get()) {
10494 case COMMENTS_RAW_COMMENT: {
10495 unsigned Idx = 0;
10496 SourceRange SR = ReadSourceRange(F, Record, Idx);
10497 RawComment::CommentKind Kind =
10498 (RawComment::CommentKind) Record[Idx++];
10499 bool IsTrailingComment = Record[Idx++];
10500 bool IsAlmostTrailingComment = Record[Idx++];
10501 Comments.push_back(x: new (Context) RawComment(
10502 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10503 break;
10504 }
10505 }
10506 }
10507 NextCursor:
10508 for (RawComment *C : Comments) {
10509 SourceLocation CommentLoc = C->getBeginLoc();
10510 if (CommentLoc.isValid()) {
10511 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10512 if (Loc.first.isValid())
10513 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10514 }
10515 }
10516 }
10517}
10518
10519void ASTReader::visitInputFileInfos(
10520 serialization::ModuleFile &MF, bool IncludeSystem,
10521 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10522 bool IsSystem)>
10523 Visitor) {
10524 unsigned NumUserInputs = MF.NumUserInputFiles;
10525 unsigned NumInputs = MF.InputFilesLoaded.size();
10526 assert(NumUserInputs <= NumInputs);
10527 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10528 for (unsigned I = 0; I < N; ++I) {
10529 bool IsSystem = I >= NumUserInputs;
10530 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10531 Visitor(IFI, IsSystem);
10532 }
10533}
10534
10535void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10536 bool IncludeSystem, bool Complain,
10537 llvm::function_ref<void(const serialization::InputFile &IF,
10538 bool isSystem)> Visitor) {
10539 unsigned NumUserInputs = MF.NumUserInputFiles;
10540 unsigned NumInputs = MF.InputFilesLoaded.size();
10541 assert(NumUserInputs <= NumInputs);
10542 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10543 for (unsigned I = 0; I < N; ++I) {
10544 bool IsSystem = I >= NumUserInputs;
10545 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10546 Visitor(IF, IsSystem);
10547 }
10548}
10549
10550void ASTReader::visitTopLevelModuleMaps(
10551 serialization::ModuleFile &MF,
10552 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10553 unsigned NumInputs = MF.InputFilesLoaded.size();
10554 for (unsigned I = 0; I < NumInputs; ++I) {
10555 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10556 if (IFI.TopLevel && IFI.ModuleMap)
10557 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10558 Visitor(*FE);
10559 }
10560}
10561
10562void ASTReader::finishPendingActions() {
10563 while (!PendingIdentifierInfos.empty() ||
10564 !PendingDeducedFunctionTypes.empty() ||
10565 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10566 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10567 !PendingUpdateRecords.empty() ||
10568 !PendingObjCExtensionIvarRedeclarations.empty()) {
10569 // If any identifiers with corresponding top-level declarations have
10570 // been loaded, load those declarations now.
10571 using TopLevelDeclsMap =
10572 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10573 TopLevelDeclsMap TopLevelDecls;
10574
10575 while (!PendingIdentifierInfos.empty()) {
10576 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10577 SmallVector<GlobalDeclID, 4> DeclIDs =
10578 std::move(PendingIdentifierInfos.back().second);
10579 PendingIdentifierInfos.pop_back();
10580
10581 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10582 }
10583
10584 // Load each function type that we deferred loading because it was a
10585 // deduced type that might refer to a local type declared within itself.
10586 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10587 auto *FD = PendingDeducedFunctionTypes[I].first;
10588 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10589
10590 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10591 // If we gave a function a deduced return type, remember that we need to
10592 // propagate that along the redeclaration chain.
10593 if (DT->isDeduced()) {
10594 PendingDeducedTypeUpdates.insert(
10595 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10596 continue;
10597 }
10598
10599 // The function has undeduced DeduceType return type. We hope we can
10600 // find the deduced type by iterating the redecls in other modules
10601 // later.
10602 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10603 continue;
10604 }
10605 }
10606 PendingDeducedFunctionTypes.clear();
10607
10608 // Load each variable type that we deferred loading because it was a
10609 // deduced type that might refer to a local type declared within itself.
10610 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10611 auto *VD = PendingDeducedVarTypes[I].first;
10612 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10613 }
10614 PendingDeducedVarTypes.clear();
10615
10616 // Load pending declaration chains.
10617 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10618 loadPendingDeclChain(D: PendingDeclChains[I].first,
10619 LocalOffset: PendingDeclChains[I].second);
10620 PendingDeclChains.clear();
10621
10622 // Make the most recent of the top-level declarations visible.
10623 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10624 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10625 IdentifierInfo *II = TLD->first;
10626 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10627 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10628 }
10629 }
10630
10631 // Load any pending macro definitions.
10632 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10633 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10634 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10635 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10636 // Initialize the macro history from chained-PCHs ahead of module imports.
10637 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10638 ++IDIdx) {
10639 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10640 if (!Info.M->isModule())
10641 resolvePendingMacro(II, PMInfo: Info);
10642 }
10643 // Handle module imports.
10644 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10645 ++IDIdx) {
10646 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10647 if (Info.M->isModule())
10648 resolvePendingMacro(II, PMInfo: Info);
10649 }
10650 }
10651 PendingMacroIDs.clear();
10652
10653 // Wire up the DeclContexts for Decls that we delayed setting until
10654 // recursive loading is completed.
10655 while (!PendingDeclContextInfos.empty()) {
10656 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10657 PendingDeclContextInfos.pop_front();
10658 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10659 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10660 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10661 }
10662
10663 // Perform any pending declaration updates.
10664 while (!PendingUpdateRecords.empty()) {
10665 auto Update = PendingUpdateRecords.pop_back_val();
10666 ReadingKindTracker ReadingKind(Read_Decl, *this);
10667 loadDeclUpdateRecords(Record&: Update);
10668 }
10669
10670 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10671 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10672 auto DuplicateIvars =
10673 PendingObjCExtensionIvarRedeclarations.back().second;
10674 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10675 StructuralEquivalenceContext Ctx(
10676 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10677 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10678 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10679 /*Complain =*/false,
10680 /*ErrorOnTagTypeMismatch =*/true);
10681 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10682 // Merge redeclared ivars with their predecessors.
10683 for (auto IvarPair : DuplicateIvars) {
10684 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10685 // Change semantic DeclContext but keep the lexical one.
10686 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10687 LexicalDC: Ivar->getLexicalDeclContext(),
10688 Ctx&: getContext());
10689 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10690 }
10691 // Invalidate duplicate extension and the cached ivar list.
10692 ExtensionsPair.first->setInvalidDecl();
10693 ExtensionsPair.second->getClassInterface()
10694 ->getDefinition()
10695 ->setIvarList(nullptr);
10696 } else {
10697 for (auto IvarPair : DuplicateIvars) {
10698 Diag(Loc: IvarPair.first->getLocation(),
10699 DiagID: diag::err_duplicate_ivar_declaration)
10700 << IvarPair.first->getIdentifier();
10701 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10702 }
10703 }
10704 PendingObjCExtensionIvarRedeclarations.pop_back();
10705 }
10706 }
10707
10708 // At this point, all update records for loaded decls are in place, so any
10709 // fake class definitions should have become real.
10710 assert(PendingFakeDefinitionData.empty() &&
10711 "faked up a class definition but never saw the real one");
10712
10713 // If we deserialized any C++ or Objective-C class definitions, any
10714 // Objective-C protocol definitions, or any redeclarable templates, make sure
10715 // that all redeclarations point to the definitions. Note that this can only
10716 // happen now, after the redeclaration chains have been fully wired.
10717 for (Decl *D : PendingDefinitions) {
10718 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10719 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10720 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10721 R = R->getPreviousDecl()) {
10722 assert((R == D) ==
10723 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10724 "declaration thinks it's the definition but it isn't");
10725 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10726 }
10727 }
10728
10729 continue;
10730 }
10731
10732 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10733 // Make sure that the ObjCInterfaceType points at the definition.
10734 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10735 ->Decl = ID;
10736
10737 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10738 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10739
10740 continue;
10741 }
10742
10743 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10744 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10745 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10746
10747 continue;
10748 }
10749
10750 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10751 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10752 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10753 }
10754 PendingDefinitions.clear();
10755
10756 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10757 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10758 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10759 return VD->isThisDeclarationADefinition() ||
10760 VD->isThisDeclarationADemotedDefinition();
10761
10762 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10763 return TD->isThisDeclarationADefinition() ||
10764 TD->isThisDeclarationADemotedDefinition();
10765
10766 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10767 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10768
10769 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10770 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10771
10772 // Conservatively return false here.
10773 return false;
10774 };
10775
10776 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10777 return hasDefinitionImpl(D, hasDefinitionImpl);
10778 };
10779
10780 // It is not good to prevent multiple declarations since the forward
10781 // declaration is common. Let's try to avoid duplicated definitions
10782 // only.
10783 if (!hasDefinition(D) || !hasDefinition(Previous))
10784 continue;
10785
10786 Module *PM = Previous->getOwningModule();
10787 Module *DM = D->getOwningModule();
10788 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10789 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10790 << (DM ? DM->getTopLevelModuleName() : "global module");
10791 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10792 }
10793 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10794
10795 // Load the bodies of any functions or methods we've encountered. We do
10796 // this now (delayed) so that we can be sure that the declaration chains
10797 // have been fully wired up (hasBody relies on this).
10798 // FIXME: We shouldn't require complete redeclaration chains here.
10799 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10800 PBEnd = PendingBodies.end();
10801 PB != PBEnd; ++PB) {
10802 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10803 // FIXME: Check for =delete/=default?
10804 const FunctionDecl *Defn = nullptr;
10805 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10806 FD->setLazyBody(PB->second);
10807 } else {
10808 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10809 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10810
10811 if (!FD->isLateTemplateParsed() &&
10812 !NonConstDefn->isLateTemplateParsed() &&
10813 // We only perform ODR checks for decls not in the explicit
10814 // global module fragment.
10815 !shouldSkipCheckingODR(D: FD) &&
10816 !shouldSkipCheckingODR(D: NonConstDefn) &&
10817 FD->getODRHash() != NonConstDefn->getODRHash()) {
10818 if (!isa<CXXMethodDecl>(Val: FD)) {
10819 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10820 } else if (FD->getLexicalParent()->isFileContext() &&
10821 NonConstDefn->getLexicalParent()->isFileContext()) {
10822 // Only diagnose out-of-line method definitions. If they are
10823 // in class definitions, then an error will be generated when
10824 // processing the class bodies.
10825 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10826 }
10827 }
10828 }
10829 continue;
10830 }
10831
10832 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10833 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10834 MD->setLazyBody(PB->second);
10835 }
10836 PendingBodies.clear();
10837
10838 // Inform any classes that had members added that they now have more members.
10839 for (auto [RD, MD] : PendingAddedClassMembers) {
10840 RD->addedMember(D: MD);
10841 }
10842 PendingAddedClassMembers.clear();
10843
10844 // Do some cleanup.
10845 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10846 getContext().deduplicateMergedDefinitionsFor(ND);
10847 PendingMergedDefinitionsToDeduplicate.clear();
10848
10849 // For each decl chain that we wanted to complete while deserializing, mark
10850 // it as "still needs to be completed".
10851 for (Decl *D : PendingIncompleteDeclChains)
10852 markIncompleteDeclChain(D);
10853 PendingIncompleteDeclChains.clear();
10854
10855 assert(PendingIdentifierInfos.empty() &&
10856 "Should be empty at the end of finishPendingActions");
10857 assert(PendingDeducedFunctionTypes.empty() &&
10858 "Should be empty at the end of finishPendingActions");
10859 assert(PendingDeducedVarTypes.empty() &&
10860 "Should be empty at the end of finishPendingActions");
10861 assert(PendingDeclChains.empty() &&
10862 "Should be empty at the end of finishPendingActions");
10863 assert(PendingMacroIDs.empty() &&
10864 "Should be empty at the end of finishPendingActions");
10865 assert(PendingDeclContextInfos.empty() &&
10866 "Should be empty at the end of finishPendingActions");
10867 assert(PendingUpdateRecords.empty() &&
10868 "Should be empty at the end of finishPendingActions");
10869 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10870 "Should be empty at the end of finishPendingActions");
10871 assert(PendingFakeDefinitionData.empty() &&
10872 "Should be empty at the end of finishPendingActions");
10873 assert(PendingDefinitions.empty() &&
10874 "Should be empty at the end of finishPendingActions");
10875 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10876 "Should be empty at the end of finishPendingActions");
10877 assert(PendingBodies.empty() &&
10878 "Should be empty at the end of finishPendingActions");
10879 assert(PendingAddedClassMembers.empty() &&
10880 "Should be empty at the end of finishPendingActions");
10881 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10882 "Should be empty at the end of finishPendingActions");
10883 assert(PendingIncompleteDeclChains.empty() &&
10884 "Should be empty at the end of finishPendingActions");
10885}
10886
10887void ASTReader::diagnoseOdrViolations() {
10888 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10889 PendingRecordOdrMergeFailures.empty() &&
10890 PendingFunctionOdrMergeFailures.empty() &&
10891 PendingEnumOdrMergeFailures.empty() &&
10892 PendingObjCInterfaceOdrMergeFailures.empty() &&
10893 PendingObjCProtocolOdrMergeFailures.empty())
10894 return;
10895
10896 // Trigger the import of the full definition of each class that had any
10897 // odr-merging problems, so we can produce better diagnostics for them.
10898 // These updates may in turn find and diagnose some ODR failures, so take
10899 // ownership of the set first.
10900 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10901 PendingOdrMergeFailures.clear();
10902 for (auto &Merge : OdrMergeFailures) {
10903 Merge.first->buildLookup();
10904 Merge.first->decls_begin();
10905 Merge.first->bases_begin();
10906 Merge.first->vbases_begin();
10907 for (auto &RecordPair : Merge.second) {
10908 auto *RD = RecordPair.first;
10909 RD->decls_begin();
10910 RD->bases_begin();
10911 RD->vbases_begin();
10912 }
10913 }
10914
10915 // Trigger the import of the full definition of each record in C/ObjC.
10916 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
10917 PendingRecordOdrMergeFailures.clear();
10918 for (auto &Merge : RecordOdrMergeFailures) {
10919 Merge.first->decls_begin();
10920 for (auto &D : Merge.second)
10921 D->decls_begin();
10922 }
10923
10924 // Trigger the import of the full interface definition.
10925 auto ObjCInterfaceOdrMergeFailures =
10926 std::move(PendingObjCInterfaceOdrMergeFailures);
10927 PendingObjCInterfaceOdrMergeFailures.clear();
10928 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10929 Merge.first->decls_begin();
10930 for (auto &InterfacePair : Merge.second)
10931 InterfacePair.first->decls_begin();
10932 }
10933
10934 // Trigger the import of functions.
10935 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10936 PendingFunctionOdrMergeFailures.clear();
10937 for (auto &Merge : FunctionOdrMergeFailures) {
10938 Merge.first->buildLookup();
10939 Merge.first->decls_begin();
10940 Merge.first->getBody();
10941 for (auto &FD : Merge.second) {
10942 FD->buildLookup();
10943 FD->decls_begin();
10944 FD->getBody();
10945 }
10946 }
10947
10948 // Trigger the import of enums.
10949 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10950 PendingEnumOdrMergeFailures.clear();
10951 for (auto &Merge : EnumOdrMergeFailures) {
10952 Merge.first->decls_begin();
10953 for (auto &Enum : Merge.second) {
10954 Enum->decls_begin();
10955 }
10956 }
10957
10958 // Trigger the import of the full protocol definition.
10959 auto ObjCProtocolOdrMergeFailures =
10960 std::move(PendingObjCProtocolOdrMergeFailures);
10961 PendingObjCProtocolOdrMergeFailures.clear();
10962 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10963 Merge.first->decls_begin();
10964 for (auto &ProtocolPair : Merge.second)
10965 ProtocolPair.first->decls_begin();
10966 }
10967
10968 // For each declaration from a merged context, check that the canonical
10969 // definition of that context also contains a declaration of the same
10970 // entity.
10971 //
10972 // Caution: this loop does things that might invalidate iterators into
10973 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10974 while (!PendingOdrMergeChecks.empty()) {
10975 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10976
10977 // FIXME: Skip over implicit declarations for now. This matters for things
10978 // like implicitly-declared special member functions. This isn't entirely
10979 // correct; we can end up with multiple unmerged declarations of the same
10980 // implicit entity.
10981 if (D->isImplicit())
10982 continue;
10983
10984 DeclContext *CanonDef = D->getDeclContext();
10985
10986 bool Found = false;
10987 const Decl *DCanon = D->getCanonicalDecl();
10988
10989 for (auto *RI : D->redecls()) {
10990 if (RI->getLexicalDeclContext() == CanonDef) {
10991 Found = true;
10992 break;
10993 }
10994 }
10995 if (Found)
10996 continue;
10997
10998 // Quick check failed, time to do the slow thing. Note, we can't just
10999 // look up the name of D in CanonDef here, because the member that is
11000 // in CanonDef might not be found by name lookup (it might have been
11001 // replaced by a more recent declaration in the lookup table), and we
11002 // can't necessarily find it in the redeclaration chain because it might
11003 // be merely mergeable, not redeclarable.
11004 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11005 for (auto *CanonMember : CanonDef->decls()) {
11006 if (CanonMember->getCanonicalDecl() == DCanon) {
11007 // This can happen if the declaration is merely mergeable and not
11008 // actually redeclarable (we looked for redeclarations earlier).
11009 //
11010 // FIXME: We should be able to detect this more efficiently, without
11011 // pulling in all of the members of CanonDef.
11012 Found = true;
11013 break;
11014 }
11015 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11016 if (ND->getDeclName() == D->getDeclName())
11017 Candidates.push_back(Elt: ND);
11018 }
11019
11020 if (!Found) {
11021 // The AST doesn't like TagDecls becoming invalid after they've been
11022 // completed. We only really need to mark FieldDecls as invalid here.
11023 if (!isa<TagDecl>(Val: D))
11024 D->setInvalidDecl();
11025
11026 // Ensure we don't accidentally recursively enter deserialization while
11027 // we're producing our diagnostic.
11028 Deserializing RecursionGuard(this);
11029
11030 std::string CanonDefModule =
11031 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11032 D: cast<Decl>(Val: CanonDef));
11033 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11034 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11035 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11036
11037 if (Candidates.empty())
11038 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11039 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11040 else {
11041 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11042 Diag(Loc: Candidates[I]->getLocation(),
11043 DiagID: diag::note_module_odr_violation_possible_decl)
11044 << Candidates[I];
11045 }
11046
11047 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11048 }
11049 }
11050
11051 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11052 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11053 ObjCInterfaceOdrMergeFailures.empty() &&
11054 ObjCProtocolOdrMergeFailures.empty())
11055 return;
11056
11057 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11058 getPreprocessor().getLangOpts());
11059
11060 // Issue any pending ODR-failure diagnostics.
11061 for (auto &Merge : OdrMergeFailures) {
11062 // If we've already pointed out a specific problem with this class, don't
11063 // bother issuing a general "something's different" diagnostic.
11064 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11065 continue;
11066
11067 bool Diagnosed = false;
11068 CXXRecordDecl *FirstRecord = Merge.first;
11069 for (auto &RecordPair : Merge.second) {
11070 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11071 SecondDD: RecordPair.second)) {
11072 Diagnosed = true;
11073 break;
11074 }
11075 }
11076
11077 if (!Diagnosed) {
11078 // All definitions are updates to the same declaration. This happens if a
11079 // module instantiates the declaration of a class template specialization
11080 // and two or more other modules instantiate its definition.
11081 //
11082 // FIXME: Indicate which modules had instantiations of this definition.
11083 // FIXME: How can this even happen?
11084 Diag(Loc: Merge.first->getLocation(),
11085 DiagID: diag::err_module_odr_violation_different_instantiations)
11086 << Merge.first;
11087 }
11088 }
11089
11090 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11091 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11092 for (auto &Merge : RecordOdrMergeFailures) {
11093 // If we've already pointed out a specific problem with this class, don't
11094 // bother issuing a general "something's different" diagnostic.
11095 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11096 continue;
11097
11098 RecordDecl *FirstRecord = Merge.first;
11099 bool Diagnosed = false;
11100 for (auto *SecondRecord : Merge.second) {
11101 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11102 Diagnosed = true;
11103 break;
11104 }
11105 }
11106 (void)Diagnosed;
11107 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11108 }
11109
11110 // Issue ODR failures diagnostics for functions.
11111 for (auto &Merge : FunctionOdrMergeFailures) {
11112 FunctionDecl *FirstFunction = Merge.first;
11113 bool Diagnosed = false;
11114 for (auto &SecondFunction : Merge.second) {
11115 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11116 Diagnosed = true;
11117 break;
11118 }
11119 }
11120 (void)Diagnosed;
11121 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11122 }
11123
11124 // Issue ODR failures diagnostics for enums.
11125 for (auto &Merge : EnumOdrMergeFailures) {
11126 // If we've already pointed out a specific problem with this enum, don't
11127 // bother issuing a general "something's different" diagnostic.
11128 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11129 continue;
11130
11131 EnumDecl *FirstEnum = Merge.first;
11132 bool Diagnosed = false;
11133 for (auto &SecondEnum : Merge.second) {
11134 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11135 Diagnosed = true;
11136 break;
11137 }
11138 }
11139 (void)Diagnosed;
11140 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11141 }
11142
11143 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11144 // If we've already pointed out a specific problem with this interface,
11145 // don't bother issuing a general "something's different" diagnostic.
11146 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11147 continue;
11148
11149 bool Diagnosed = false;
11150 ObjCInterfaceDecl *FirstID = Merge.first;
11151 for (auto &InterfacePair : Merge.second) {
11152 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11153 SecondDD: InterfacePair.second)) {
11154 Diagnosed = true;
11155 break;
11156 }
11157 }
11158 (void)Diagnosed;
11159 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11160 }
11161
11162 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11163 // If we've already pointed out a specific problem with this protocol,
11164 // don't bother issuing a general "something's different" diagnostic.
11165 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11166 continue;
11167
11168 ObjCProtocolDecl *FirstProtocol = Merge.first;
11169 bool Diagnosed = false;
11170 for (auto &ProtocolPair : Merge.second) {
11171 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11172 SecondDD: ProtocolPair.second)) {
11173 Diagnosed = true;
11174 break;
11175 }
11176 }
11177 (void)Diagnosed;
11178 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11179 }
11180}
11181
11182void ASTReader::StartedDeserializing() {
11183 if (llvm::Timer *T = ReadTimer.get();
11184 ++NumCurrentElementsDeserializing == 1 && T)
11185 ReadTimeRegion.emplace(args&: T);
11186}
11187
11188void ASTReader::FinishedDeserializing() {
11189 assert(NumCurrentElementsDeserializing &&
11190 "FinishedDeserializing not paired with StartedDeserializing");
11191 if (NumCurrentElementsDeserializing == 1) {
11192 // We decrease NumCurrentElementsDeserializing only after pending actions
11193 // are finished, to avoid recursively re-calling finishPendingActions().
11194 finishPendingActions();
11195 }
11196 --NumCurrentElementsDeserializing;
11197
11198 if (NumCurrentElementsDeserializing == 0) {
11199 {
11200 // Guard variable to avoid recursively entering the process of passing
11201 // decls to consumer.
11202 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11203 /*NewValue=*/false);
11204
11205 // Propagate exception specification and deduced type updates along
11206 // redeclaration chains.
11207 //
11208 // We do this now rather than in finishPendingActions because we want to
11209 // be able to walk the complete redeclaration chains of the updated decls.
11210 while (!PendingExceptionSpecUpdates.empty() ||
11211 !PendingDeducedTypeUpdates.empty() ||
11212 !PendingUndeducedFunctionDecls.empty()) {
11213 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11214 PendingExceptionSpecUpdates.clear();
11215 for (auto Update : ESUpdates) {
11216 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11217 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11218 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11219 if (auto *Listener = getContext().getASTMutationListener())
11220 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11221 for (auto *Redecl : Update.second->redecls())
11222 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11223 }
11224
11225 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11226 PendingDeducedTypeUpdates.clear();
11227 for (auto Update : DTUpdates) {
11228 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11229 // FIXME: If the return type is already deduced, check that it
11230 // matches.
11231 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11232 ResultType: Update.second);
11233 }
11234
11235 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11236 PendingUndeducedFunctionDecls.clear();
11237 // We hope we can find the deduced type for the functions by iterating
11238 // redeclarations in other modules.
11239 for (FunctionDecl *UndeducedFD : UDTUpdates)
11240 (void)UndeducedFD->getMostRecentDecl();
11241 }
11242
11243 ReadTimeRegion.reset();
11244
11245 diagnoseOdrViolations();
11246 }
11247
11248 // We are not in recursive loading, so it's safe to pass the "interesting"
11249 // decls to the consumer.
11250 if (Consumer)
11251 PassInterestingDeclsToConsumer();
11252 }
11253}
11254
11255void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11256 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11257 // Remove any fake results before adding any real ones.
11258 auto It = PendingFakeLookupResults.find(Key: II);
11259 if (It != PendingFakeLookupResults.end()) {
11260 for (auto *ND : It->second)
11261 SemaObj->IdResolver.RemoveDecl(D: ND);
11262 // FIXME: this works around module+PCH performance issue.
11263 // Rather than erase the result from the map, which is O(n), just clear
11264 // the vector of NamedDecls.
11265 It->second.clear();
11266 }
11267 }
11268
11269 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11270 SemaObj->TUScope->AddDecl(D);
11271 } else if (SemaObj->TUScope) {
11272 // Adding the decl to IdResolver may have failed because it was already in
11273 // (even though it was not added in scope). If it is already in, make sure
11274 // it gets in the scope as well.
11275 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11276 SemaObj->TUScope->AddDecl(D);
11277 }
11278}
11279
11280ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11281 ASTContext *Context,
11282 const PCHContainerReader &PCHContainerRdr,
11283 const CodeGenOptions &CodeGenOpts,
11284 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11285 StringRef isysroot,
11286 DisableValidationForModuleKind DisableValidationKind,
11287 bool AllowASTWithCompilerErrors,
11288 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11289 bool ForceValidateUserInputs,
11290 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11291 std::unique_ptr<llvm::Timer> ReadTimer)
11292 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11293 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11294 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11295 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11296 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11297 StackHandler(Diags), PP(PP), ContextObj(Context),
11298 CodeGenOpts(CodeGenOpts),
11299 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11300 PP.getHeaderSearchInfo()),
11301 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11302 DisableValidationKind(DisableValidationKind),
11303 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11304 AllowConfigurationMismatch(AllowConfigurationMismatch),
11305 ValidateSystemInputs(ValidateSystemInputs),
11306 ForceValidateUserInputs(ForceValidateUserInputs),
11307 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11308 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11309 SourceMgr.setExternalSLocEntrySource(this);
11310
11311 PathBuf.reserve(N: 256);
11312
11313 for (const auto &Ext : Extensions) {
11314 auto BlockName = Ext->getExtensionMetadata().BlockName;
11315 auto Known = ModuleFileExtensions.find(Key: BlockName);
11316 if (Known != ModuleFileExtensions.end()) {
11317 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11318 << BlockName;
11319 continue;
11320 }
11321
11322 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11323 }
11324}
11325
11326ASTReader::~ASTReader() {
11327 if (OwnsDeserializationListener)
11328 delete DeserializationListener;
11329}
11330
11331IdentifierResolver &ASTReader::getIdResolver() {
11332 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11333}
11334
11335Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11336 unsigned AbbrevID) {
11337 Idx = 0;
11338 Record.clear();
11339 return Cursor.readRecord(AbbrevID, Vals&: Record);
11340}
11341//===----------------------------------------------------------------------===//
11342//// OMPClauseReader implementation
11343////===----------------------------------------------------------------------===//
11344
11345// This has to be in namespace clang because it's friended by all
11346// of the OMP clauses.
11347namespace clang {
11348
11349class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11350 ASTRecordReader &Record;
11351 ASTContext &Context;
11352
11353public:
11354 OMPClauseReader(ASTRecordReader &Record)
11355 : Record(Record), Context(Record.getContext()) {}
11356#define GEN_CLANG_CLAUSE_CLASS
11357#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11358#include "llvm/Frontend/OpenMP/OMP.inc"
11359 OMPClause *readClause();
11360 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11361 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11362};
11363
11364} // end namespace clang
11365
11366OMPClause *ASTRecordReader::readOMPClause() {
11367 return OMPClauseReader(*this).readClause();
11368}
11369
11370OMPClause *OMPClauseReader::readClause() {
11371 OMPClause *C = nullptr;
11372 switch (llvm::omp::Clause(Record.readInt())) {
11373 case llvm::omp::OMPC_if:
11374 C = new (Context) OMPIfClause();
11375 break;
11376 case llvm::omp::OMPC_final:
11377 C = new (Context) OMPFinalClause();
11378 break;
11379 case llvm::omp::OMPC_num_threads:
11380 C = new (Context) OMPNumThreadsClause();
11381 break;
11382 case llvm::omp::OMPC_safelen:
11383 C = new (Context) OMPSafelenClause();
11384 break;
11385 case llvm::omp::OMPC_simdlen:
11386 C = new (Context) OMPSimdlenClause();
11387 break;
11388 case llvm::omp::OMPC_sizes: {
11389 unsigned NumSizes = Record.readInt();
11390 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11391 break;
11392 }
11393 case llvm::omp::OMPC_permutation: {
11394 unsigned NumLoops = Record.readInt();
11395 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11396 break;
11397 }
11398 case llvm::omp::OMPC_full:
11399 C = OMPFullClause::CreateEmpty(C: Context);
11400 break;
11401 case llvm::omp::OMPC_partial:
11402 C = OMPPartialClause::CreateEmpty(C: Context);
11403 break;
11404 case llvm::omp::OMPC_looprange:
11405 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11406 break;
11407 case llvm::omp::OMPC_allocator:
11408 C = new (Context) OMPAllocatorClause();
11409 break;
11410 case llvm::omp::OMPC_collapse:
11411 C = new (Context) OMPCollapseClause();
11412 break;
11413 case llvm::omp::OMPC_default:
11414 C = new (Context) OMPDefaultClause();
11415 break;
11416 case llvm::omp::OMPC_proc_bind:
11417 C = new (Context) OMPProcBindClause();
11418 break;
11419 case llvm::omp::OMPC_schedule:
11420 C = new (Context) OMPScheduleClause();
11421 break;
11422 case llvm::omp::OMPC_ordered:
11423 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11424 break;
11425 case llvm::omp::OMPC_nowait:
11426 C = new (Context) OMPNowaitClause();
11427 break;
11428 case llvm::omp::OMPC_untied:
11429 C = new (Context) OMPUntiedClause();
11430 break;
11431 case llvm::omp::OMPC_mergeable:
11432 C = new (Context) OMPMergeableClause();
11433 break;
11434 case llvm::omp::OMPC_threadset:
11435 C = new (Context) OMPThreadsetClause();
11436 break;
11437 case llvm::omp::OMPC_transparent:
11438 C = new (Context) OMPTransparentClause();
11439 break;
11440 case llvm::omp::OMPC_read:
11441 C = new (Context) OMPReadClause();
11442 break;
11443 case llvm::omp::OMPC_write:
11444 C = new (Context) OMPWriteClause();
11445 break;
11446 case llvm::omp::OMPC_update:
11447 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11448 break;
11449 case llvm::omp::OMPC_capture:
11450 C = new (Context) OMPCaptureClause();
11451 break;
11452 case llvm::omp::OMPC_compare:
11453 C = new (Context) OMPCompareClause();
11454 break;
11455 case llvm::omp::OMPC_fail:
11456 C = new (Context) OMPFailClause();
11457 break;
11458 case llvm::omp::OMPC_seq_cst:
11459 C = new (Context) OMPSeqCstClause();
11460 break;
11461 case llvm::omp::OMPC_acq_rel:
11462 C = new (Context) OMPAcqRelClause();
11463 break;
11464 case llvm::omp::OMPC_absent: {
11465 unsigned NumKinds = Record.readInt();
11466 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11467 break;
11468 }
11469 case llvm::omp::OMPC_holds:
11470 C = new (Context) OMPHoldsClause();
11471 break;
11472 case llvm::omp::OMPC_contains: {
11473 unsigned NumKinds = Record.readInt();
11474 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11475 break;
11476 }
11477 case llvm::omp::OMPC_no_openmp:
11478 C = new (Context) OMPNoOpenMPClause();
11479 break;
11480 case llvm::omp::OMPC_no_openmp_routines:
11481 C = new (Context) OMPNoOpenMPRoutinesClause();
11482 break;
11483 case llvm::omp::OMPC_no_openmp_constructs:
11484 C = new (Context) OMPNoOpenMPConstructsClause();
11485 break;
11486 case llvm::omp::OMPC_no_parallelism:
11487 C = new (Context) OMPNoParallelismClause();
11488 break;
11489 case llvm::omp::OMPC_acquire:
11490 C = new (Context) OMPAcquireClause();
11491 break;
11492 case llvm::omp::OMPC_release:
11493 C = new (Context) OMPReleaseClause();
11494 break;
11495 case llvm::omp::OMPC_relaxed:
11496 C = new (Context) OMPRelaxedClause();
11497 break;
11498 case llvm::omp::OMPC_weak:
11499 C = new (Context) OMPWeakClause();
11500 break;
11501 case llvm::omp::OMPC_threads:
11502 C = new (Context) OMPThreadsClause();
11503 break;
11504 case llvm::omp::OMPC_simd:
11505 C = new (Context) OMPSIMDClause();
11506 break;
11507 case llvm::omp::OMPC_nogroup:
11508 C = new (Context) OMPNogroupClause();
11509 break;
11510 case llvm::omp::OMPC_unified_address:
11511 C = new (Context) OMPUnifiedAddressClause();
11512 break;
11513 case llvm::omp::OMPC_unified_shared_memory:
11514 C = new (Context) OMPUnifiedSharedMemoryClause();
11515 break;
11516 case llvm::omp::OMPC_reverse_offload:
11517 C = new (Context) OMPReverseOffloadClause();
11518 break;
11519 case llvm::omp::OMPC_dynamic_allocators:
11520 C = new (Context) OMPDynamicAllocatorsClause();
11521 break;
11522 case llvm::omp::OMPC_atomic_default_mem_order:
11523 C = new (Context) OMPAtomicDefaultMemOrderClause();
11524 break;
11525 case llvm::omp::OMPC_self_maps:
11526 C = new (Context) OMPSelfMapsClause();
11527 break;
11528 case llvm::omp::OMPC_at:
11529 C = new (Context) OMPAtClause();
11530 break;
11531 case llvm::omp::OMPC_severity:
11532 C = new (Context) OMPSeverityClause();
11533 break;
11534 case llvm::omp::OMPC_message:
11535 C = new (Context) OMPMessageClause();
11536 break;
11537 case llvm::omp::OMPC_private:
11538 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11539 break;
11540 case llvm::omp::OMPC_firstprivate:
11541 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11542 break;
11543 case llvm::omp::OMPC_lastprivate:
11544 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11545 break;
11546 case llvm::omp::OMPC_shared:
11547 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11548 break;
11549 case llvm::omp::OMPC_reduction: {
11550 unsigned N = Record.readInt();
11551 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11552 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11553 break;
11554 }
11555 case llvm::omp::OMPC_task_reduction:
11556 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11557 break;
11558 case llvm::omp::OMPC_in_reduction:
11559 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11560 break;
11561 case llvm::omp::OMPC_linear:
11562 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11563 break;
11564 case llvm::omp::OMPC_aligned:
11565 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11566 break;
11567 case llvm::omp::OMPC_copyin:
11568 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11569 break;
11570 case llvm::omp::OMPC_copyprivate:
11571 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11572 break;
11573 case llvm::omp::OMPC_flush:
11574 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11575 break;
11576 case llvm::omp::OMPC_depobj:
11577 C = OMPDepobjClause::CreateEmpty(C: Context);
11578 break;
11579 case llvm::omp::OMPC_depend: {
11580 unsigned NumVars = Record.readInt();
11581 unsigned NumLoops = Record.readInt();
11582 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11583 break;
11584 }
11585 case llvm::omp::OMPC_device:
11586 C = new (Context) OMPDeviceClause();
11587 break;
11588 case llvm::omp::OMPC_map: {
11589 OMPMappableExprListSizeTy Sizes;
11590 Sizes.NumVars = Record.readInt();
11591 Sizes.NumUniqueDeclarations = Record.readInt();
11592 Sizes.NumComponentLists = Record.readInt();
11593 Sizes.NumComponents = Record.readInt();
11594 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11595 break;
11596 }
11597 case llvm::omp::OMPC_num_teams:
11598 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11599 break;
11600 case llvm::omp::OMPC_thread_limit:
11601 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11602 break;
11603 case llvm::omp::OMPC_priority:
11604 C = new (Context) OMPPriorityClause();
11605 break;
11606 case llvm::omp::OMPC_grainsize:
11607 C = new (Context) OMPGrainsizeClause();
11608 break;
11609 case llvm::omp::OMPC_num_tasks:
11610 C = new (Context) OMPNumTasksClause();
11611 break;
11612 case llvm::omp::OMPC_hint:
11613 C = new (Context) OMPHintClause();
11614 break;
11615 case llvm::omp::OMPC_dist_schedule:
11616 C = new (Context) OMPDistScheduleClause();
11617 break;
11618 case llvm::omp::OMPC_defaultmap:
11619 C = new (Context) OMPDefaultmapClause();
11620 break;
11621 case llvm::omp::OMPC_to: {
11622 OMPMappableExprListSizeTy Sizes;
11623 Sizes.NumVars = Record.readInt();
11624 Sizes.NumUniqueDeclarations = Record.readInt();
11625 Sizes.NumComponentLists = Record.readInt();
11626 Sizes.NumComponents = Record.readInt();
11627 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11628 break;
11629 }
11630 case llvm::omp::OMPC_from: {
11631 OMPMappableExprListSizeTy Sizes;
11632 Sizes.NumVars = Record.readInt();
11633 Sizes.NumUniqueDeclarations = Record.readInt();
11634 Sizes.NumComponentLists = Record.readInt();
11635 Sizes.NumComponents = Record.readInt();
11636 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11637 break;
11638 }
11639 case llvm::omp::OMPC_use_device_ptr: {
11640 OMPMappableExprListSizeTy Sizes;
11641 Sizes.NumVars = Record.readInt();
11642 Sizes.NumUniqueDeclarations = Record.readInt();
11643 Sizes.NumComponentLists = Record.readInt();
11644 Sizes.NumComponents = Record.readInt();
11645 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11646 break;
11647 }
11648 case llvm::omp::OMPC_use_device_addr: {
11649 OMPMappableExprListSizeTy Sizes;
11650 Sizes.NumVars = Record.readInt();
11651 Sizes.NumUniqueDeclarations = Record.readInt();
11652 Sizes.NumComponentLists = Record.readInt();
11653 Sizes.NumComponents = Record.readInt();
11654 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11655 break;
11656 }
11657 case llvm::omp::OMPC_is_device_ptr: {
11658 OMPMappableExprListSizeTy Sizes;
11659 Sizes.NumVars = Record.readInt();
11660 Sizes.NumUniqueDeclarations = Record.readInt();
11661 Sizes.NumComponentLists = Record.readInt();
11662 Sizes.NumComponents = Record.readInt();
11663 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11664 break;
11665 }
11666 case llvm::omp::OMPC_has_device_addr: {
11667 OMPMappableExprListSizeTy Sizes;
11668 Sizes.NumVars = Record.readInt();
11669 Sizes.NumUniqueDeclarations = Record.readInt();
11670 Sizes.NumComponentLists = Record.readInt();
11671 Sizes.NumComponents = Record.readInt();
11672 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11673 break;
11674 }
11675 case llvm::omp::OMPC_allocate:
11676 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11677 break;
11678 case llvm::omp::OMPC_nontemporal:
11679 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11680 break;
11681 case llvm::omp::OMPC_inclusive:
11682 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11683 break;
11684 case llvm::omp::OMPC_exclusive:
11685 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11686 break;
11687 case llvm::omp::OMPC_order:
11688 C = new (Context) OMPOrderClause();
11689 break;
11690 case llvm::omp::OMPC_init:
11691 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
11692 break;
11693 case llvm::omp::OMPC_use:
11694 C = new (Context) OMPUseClause();
11695 break;
11696 case llvm::omp::OMPC_destroy:
11697 C = new (Context) OMPDestroyClause();
11698 break;
11699 case llvm::omp::OMPC_novariants:
11700 C = new (Context) OMPNovariantsClause();
11701 break;
11702 case llvm::omp::OMPC_nocontext:
11703 C = new (Context) OMPNocontextClause();
11704 break;
11705 case llvm::omp::OMPC_detach:
11706 C = new (Context) OMPDetachClause();
11707 break;
11708 case llvm::omp::OMPC_uses_allocators:
11709 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11710 break;
11711 case llvm::omp::OMPC_affinity:
11712 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11713 break;
11714 case llvm::omp::OMPC_filter:
11715 C = new (Context) OMPFilterClause();
11716 break;
11717 case llvm::omp::OMPC_bind:
11718 C = OMPBindClause::CreateEmpty(C: Context);
11719 break;
11720 case llvm::omp::OMPC_align:
11721 C = new (Context) OMPAlignClause();
11722 break;
11723 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11724 C = new (Context) OMPXDynCGroupMemClause();
11725 break;
11726 case llvm::omp::OMPC_dyn_groupprivate:
11727 C = new (Context) OMPDynGroupprivateClause();
11728 break;
11729 case llvm::omp::OMPC_doacross: {
11730 unsigned NumVars = Record.readInt();
11731 unsigned NumLoops = Record.readInt();
11732 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11733 break;
11734 }
11735 case llvm::omp::OMPC_ompx_attribute:
11736 C = new (Context) OMPXAttributeClause();
11737 break;
11738 case llvm::omp::OMPC_ompx_bare:
11739 C = new (Context) OMPXBareClause();
11740 break;
11741#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11742 case llvm::omp::Enum: \
11743 break;
11744#include "llvm/Frontend/OpenMP/OMPKinds.def"
11745 default:
11746 break;
11747 }
11748 assert(C && "Unknown OMPClause type");
11749
11750 Visit(S: C);
11751 C->setLocStart(Record.readSourceLocation());
11752 C->setLocEnd(Record.readSourceLocation());
11753
11754 return C;
11755}
11756
11757void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11758 C->setPreInitStmt(S: Record.readSubStmt(),
11759 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11760}
11761
11762void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11763 VisitOMPClauseWithPreInit(C);
11764 C->setPostUpdateExpr(Record.readSubExpr());
11765}
11766
11767void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11768 VisitOMPClauseWithPreInit(C);
11769 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11770 C->setNameModifierLoc(Record.readSourceLocation());
11771 C->setColonLoc(Record.readSourceLocation());
11772 C->setCondition(Record.readSubExpr());
11773 C->setLParenLoc(Record.readSourceLocation());
11774}
11775
11776void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11777 VisitOMPClauseWithPreInit(C);
11778 C->setCondition(Record.readSubExpr());
11779 C->setLParenLoc(Record.readSourceLocation());
11780}
11781
11782void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11783 VisitOMPClauseWithPreInit(C);
11784 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11785 C->setNumThreads(Record.readSubExpr());
11786 C->setModifierLoc(Record.readSourceLocation());
11787 C->setLParenLoc(Record.readSourceLocation());
11788}
11789
11790void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11791 C->setSafelen(Record.readSubExpr());
11792 C->setLParenLoc(Record.readSourceLocation());
11793}
11794
11795void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11796 C->setSimdlen(Record.readSubExpr());
11797 C->setLParenLoc(Record.readSourceLocation());
11798}
11799
11800void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11801 for (Expr *&E : C->getSizesRefs())
11802 E = Record.readSubExpr();
11803 C->setLParenLoc(Record.readSourceLocation());
11804}
11805
11806void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11807 for (Expr *&E : C->getArgsRefs())
11808 E = Record.readSubExpr();
11809 C->setLParenLoc(Record.readSourceLocation());
11810}
11811
11812void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11813
11814void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11815 C->setFactor(Record.readSubExpr());
11816 C->setLParenLoc(Record.readSourceLocation());
11817}
11818
11819void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11820 C->setFirst(Record.readSubExpr());
11821 C->setCount(Record.readSubExpr());
11822 C->setLParenLoc(Record.readSourceLocation());
11823 C->setFirstLoc(Record.readSourceLocation());
11824 C->setCountLoc(Record.readSourceLocation());
11825}
11826
11827void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11828 C->setAllocator(Record.readExpr());
11829 C->setLParenLoc(Record.readSourceLocation());
11830}
11831
11832void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11833 C->setNumForLoops(Record.readSubExpr());
11834 C->setLParenLoc(Record.readSourceLocation());
11835}
11836
11837void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11838 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11839 C->setLParenLoc(Record.readSourceLocation());
11840 C->setDefaultKindKwLoc(Record.readSourceLocation());
11841 C->setDefaultVariableCategory(
11842 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11843 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11844}
11845
11846// Read the parameter of threadset clause. This will have been saved when
11847// OMPClauseWriter is called.
11848void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11849 C->setLParenLoc(Record.readSourceLocation());
11850 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11851 C->setThreadsetKindLoc(ThreadsetKindLoc);
11852 OpenMPThreadsetKind TKind =
11853 static_cast<OpenMPThreadsetKind>(Record.readInt());
11854 C->setThreadsetKind(TKind);
11855}
11856
11857void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11858 C->setLParenLoc(Record.readSourceLocation());
11859 C->setImpexTypeKind(Record.readSubExpr());
11860}
11861
11862void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11863 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11864 C->setLParenLoc(Record.readSourceLocation());
11865 C->setProcBindKindKwLoc(Record.readSourceLocation());
11866}
11867
11868void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11869 VisitOMPClauseWithPreInit(C);
11870 C->setScheduleKind(
11871 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11872 C->setFirstScheduleModifier(
11873 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11874 C->setSecondScheduleModifier(
11875 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11876 C->setChunkSize(Record.readSubExpr());
11877 C->setLParenLoc(Record.readSourceLocation());
11878 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11879 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11880 C->setScheduleKindLoc(Record.readSourceLocation());
11881 C->setCommaLoc(Record.readSourceLocation());
11882}
11883
11884void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11885 C->setNumForLoops(Record.readSubExpr());
11886 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11887 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11888 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11889 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11890 C->setLParenLoc(Record.readSourceLocation());
11891}
11892
11893void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11894 C->setEventHandler(Record.readSubExpr());
11895 C->setLParenLoc(Record.readSourceLocation());
11896}
11897
11898void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
11899 C->setCondition(Record.readSubExpr());
11900 C->setLParenLoc(Record.readSourceLocation());
11901}
11902
11903void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11904
11905void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11906
11907void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11908
11909void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11910
11911void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11912 if (C->isExtended()) {
11913 C->setLParenLoc(Record.readSourceLocation());
11914 C->setArgumentLoc(Record.readSourceLocation());
11915 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11916 }
11917}
11918
11919void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11920
11921void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11922
11923// Read the parameter of fail clause. This will have been saved when
11924// OMPClauseWriter is called.
11925void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
11926 C->setLParenLoc(Record.readSourceLocation());
11927 SourceLocation FailParameterLoc = Record.readSourceLocation();
11928 C->setFailParameterLoc(FailParameterLoc);
11929 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
11930 C->setFailParameter(CKind);
11931}
11932
11933void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
11934 unsigned Count = C->getDirectiveKinds().size();
11935 C->setLParenLoc(Record.readSourceLocation());
11936 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11937 DKVec.reserve(N: Count);
11938 for (unsigned I = 0; I < Count; I++) {
11939 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11940 }
11941 C->setDirectiveKinds(DKVec);
11942}
11943
11944void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
11945 C->setExpr(Record.readExpr());
11946 C->setLParenLoc(Record.readSourceLocation());
11947}
11948
11949void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
11950 unsigned Count = C->getDirectiveKinds().size();
11951 C->setLParenLoc(Record.readSourceLocation());
11952 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11953 DKVec.reserve(N: Count);
11954 for (unsigned I = 0; I < Count; I++) {
11955 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11956 }
11957 C->setDirectiveKinds(DKVec);
11958}
11959
11960void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
11961
11962void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
11963 OMPNoOpenMPRoutinesClause *) {}
11964
11965void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
11966 OMPNoOpenMPConstructsClause *) {}
11967
11968void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
11969
11970void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11971
11972void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11973
11974void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11975
11976void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11977
11978void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11979
11980void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
11981
11982void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11983
11984void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11985
11986void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11987
11988void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
11989 unsigned NumVars = C->varlist_size();
11990 SmallVector<Expr *, 16> Vars;
11991 Vars.reserve(N: NumVars);
11992 for (unsigned I = 0; I != NumVars; ++I)
11993 Vars.push_back(Elt: Record.readSubExpr());
11994 C->setVarRefs(Vars);
11995 C->setIsTarget(Record.readBool());
11996 C->setIsTargetSync(Record.readBool());
11997 C->setLParenLoc(Record.readSourceLocation());
11998 C->setVarLoc(Record.readSourceLocation());
11999}
12000
12001void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12002 C->setInteropVar(Record.readSubExpr());
12003 C->setLParenLoc(Record.readSourceLocation());
12004 C->setVarLoc(Record.readSourceLocation());
12005}
12006
12007void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12008 C->setInteropVar(Record.readSubExpr());
12009 C->setLParenLoc(Record.readSourceLocation());
12010 C->setVarLoc(Record.readSourceLocation());
12011}
12012
12013void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12014 VisitOMPClauseWithPreInit(C);
12015 C->setCondition(Record.readSubExpr());
12016 C->setLParenLoc(Record.readSourceLocation());
12017}
12018
12019void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12020 VisitOMPClauseWithPreInit(C);
12021 C->setCondition(Record.readSubExpr());
12022 C->setLParenLoc(Record.readSourceLocation());
12023}
12024
12025void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12026
12027void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12028 OMPUnifiedSharedMemoryClause *) {}
12029
12030void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12031
12032void
12033OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12034}
12035
12036void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12037 OMPAtomicDefaultMemOrderClause *C) {
12038 C->setAtomicDefaultMemOrderKind(
12039 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12040 C->setLParenLoc(Record.readSourceLocation());
12041 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12042}
12043
12044void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12045
12046void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12047 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12048 C->setLParenLoc(Record.readSourceLocation());
12049 C->setAtKindKwLoc(Record.readSourceLocation());
12050}
12051
12052void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12053 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12054 C->setLParenLoc(Record.readSourceLocation());
12055 C->setSeverityKindKwLoc(Record.readSourceLocation());
12056}
12057
12058void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12059 VisitOMPClauseWithPreInit(C);
12060 C->setMessageString(Record.readSubExpr());
12061 C->setLParenLoc(Record.readSourceLocation());
12062}
12063
12064void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12065 C->setLParenLoc(Record.readSourceLocation());
12066 unsigned NumVars = C->varlist_size();
12067 SmallVector<Expr *, 16> Vars;
12068 Vars.reserve(N: NumVars);
12069 for (unsigned i = 0; i != NumVars; ++i)
12070 Vars.push_back(Elt: Record.readSubExpr());
12071 C->setVarRefs(Vars);
12072 Vars.clear();
12073 for (unsigned i = 0; i != NumVars; ++i)
12074 Vars.push_back(Elt: Record.readSubExpr());
12075 C->setPrivateCopies(Vars);
12076}
12077
12078void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12079 VisitOMPClauseWithPreInit(C);
12080 C->setLParenLoc(Record.readSourceLocation());
12081 unsigned NumVars = C->varlist_size();
12082 SmallVector<Expr *, 16> Vars;
12083 Vars.reserve(N: NumVars);
12084 for (unsigned i = 0; i != NumVars; ++i)
12085 Vars.push_back(Elt: Record.readSubExpr());
12086 C->setVarRefs(Vars);
12087 Vars.clear();
12088 for (unsigned i = 0; i != NumVars; ++i)
12089 Vars.push_back(Elt: Record.readSubExpr());
12090 C->setPrivateCopies(Vars);
12091 Vars.clear();
12092 for (unsigned i = 0; i != NumVars; ++i)
12093 Vars.push_back(Elt: Record.readSubExpr());
12094 C->setInits(Vars);
12095}
12096
12097void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12098 VisitOMPClauseWithPostUpdate(C);
12099 C->setLParenLoc(Record.readSourceLocation());
12100 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12101 C->setKindLoc(Record.readSourceLocation());
12102 C->setColonLoc(Record.readSourceLocation());
12103 unsigned NumVars = C->varlist_size();
12104 SmallVector<Expr *, 16> Vars;
12105 Vars.reserve(N: NumVars);
12106 for (unsigned i = 0; i != NumVars; ++i)
12107 Vars.push_back(Elt: Record.readSubExpr());
12108 C->setVarRefs(Vars);
12109 Vars.clear();
12110 for (unsigned i = 0; i != NumVars; ++i)
12111 Vars.push_back(Elt: Record.readSubExpr());
12112 C->setPrivateCopies(Vars);
12113 Vars.clear();
12114 for (unsigned i = 0; i != NumVars; ++i)
12115 Vars.push_back(Elt: Record.readSubExpr());
12116 C->setSourceExprs(Vars);
12117 Vars.clear();
12118 for (unsigned i = 0; i != NumVars; ++i)
12119 Vars.push_back(Elt: Record.readSubExpr());
12120 C->setDestinationExprs(Vars);
12121 Vars.clear();
12122 for (unsigned i = 0; i != NumVars; ++i)
12123 Vars.push_back(Elt: Record.readSubExpr());
12124 C->setAssignmentOps(Vars);
12125}
12126
12127void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12128 C->setLParenLoc(Record.readSourceLocation());
12129 unsigned NumVars = C->varlist_size();
12130 SmallVector<Expr *, 16> Vars;
12131 Vars.reserve(N: NumVars);
12132 for (unsigned i = 0; i != NumVars; ++i)
12133 Vars.push_back(Elt: Record.readSubExpr());
12134 C->setVarRefs(Vars);
12135}
12136
12137void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12138 VisitOMPClauseWithPostUpdate(C);
12139 C->setLParenLoc(Record.readSourceLocation());
12140 C->setModifierLoc(Record.readSourceLocation());
12141 C->setColonLoc(Record.readSourceLocation());
12142 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12143 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12144 C->setQualifierLoc(NNSL);
12145 C->setNameInfo(DNI);
12146
12147 unsigned NumVars = C->varlist_size();
12148 SmallVector<Expr *, 16> Vars;
12149 Vars.reserve(N: NumVars);
12150 for (unsigned i = 0; i != NumVars; ++i)
12151 Vars.push_back(Elt: Record.readSubExpr());
12152 C->setVarRefs(Vars);
12153 Vars.clear();
12154 for (unsigned i = 0; i != NumVars; ++i)
12155 Vars.push_back(Elt: Record.readSubExpr());
12156 C->setPrivates(Vars);
12157 Vars.clear();
12158 for (unsigned i = 0; i != NumVars; ++i)
12159 Vars.push_back(Elt: Record.readSubExpr());
12160 C->setLHSExprs(Vars);
12161 Vars.clear();
12162 for (unsigned i = 0; i != NumVars; ++i)
12163 Vars.push_back(Elt: Record.readSubExpr());
12164 C->setRHSExprs(Vars);
12165 Vars.clear();
12166 for (unsigned i = 0; i != NumVars; ++i)
12167 Vars.push_back(Elt: Record.readSubExpr());
12168 C->setReductionOps(Vars);
12169 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12170 Vars.clear();
12171 for (unsigned i = 0; i != NumVars; ++i)
12172 Vars.push_back(Elt: Record.readSubExpr());
12173 C->setInscanCopyOps(Vars);
12174 Vars.clear();
12175 for (unsigned i = 0; i != NumVars; ++i)
12176 Vars.push_back(Elt: Record.readSubExpr());
12177 C->setInscanCopyArrayTemps(Vars);
12178 Vars.clear();
12179 for (unsigned i = 0; i != NumVars; ++i)
12180 Vars.push_back(Elt: Record.readSubExpr());
12181 C->setInscanCopyArrayElems(Vars);
12182 }
12183 unsigned NumFlags = Record.readInt();
12184 SmallVector<bool, 16> Flags;
12185 Flags.reserve(N: NumFlags);
12186 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12187 Flags.push_back(Elt: Record.readInt());
12188 C->setPrivateVariableReductionFlags(Flags);
12189}
12190
12191void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12192 VisitOMPClauseWithPostUpdate(C);
12193 C->setLParenLoc(Record.readSourceLocation());
12194 C->setColonLoc(Record.readSourceLocation());
12195 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12196 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12197 C->setQualifierLoc(NNSL);
12198 C->setNameInfo(DNI);
12199
12200 unsigned NumVars = C->varlist_size();
12201 SmallVector<Expr *, 16> Vars;
12202 Vars.reserve(N: NumVars);
12203 for (unsigned I = 0; I != NumVars; ++I)
12204 Vars.push_back(Elt: Record.readSubExpr());
12205 C->setVarRefs(Vars);
12206 Vars.clear();
12207 for (unsigned I = 0; I != NumVars; ++I)
12208 Vars.push_back(Elt: Record.readSubExpr());
12209 C->setPrivates(Vars);
12210 Vars.clear();
12211 for (unsigned I = 0; I != NumVars; ++I)
12212 Vars.push_back(Elt: Record.readSubExpr());
12213 C->setLHSExprs(Vars);
12214 Vars.clear();
12215 for (unsigned I = 0; I != NumVars; ++I)
12216 Vars.push_back(Elt: Record.readSubExpr());
12217 C->setRHSExprs(Vars);
12218 Vars.clear();
12219 for (unsigned I = 0; I != NumVars; ++I)
12220 Vars.push_back(Elt: Record.readSubExpr());
12221 C->setReductionOps(Vars);
12222}
12223
12224void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12225 VisitOMPClauseWithPostUpdate(C);
12226 C->setLParenLoc(Record.readSourceLocation());
12227 C->setColonLoc(Record.readSourceLocation());
12228 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12229 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12230 C->setQualifierLoc(NNSL);
12231 C->setNameInfo(DNI);
12232
12233 unsigned NumVars = C->varlist_size();
12234 SmallVector<Expr *, 16> Vars;
12235 Vars.reserve(N: NumVars);
12236 for (unsigned I = 0; I != NumVars; ++I)
12237 Vars.push_back(Elt: Record.readSubExpr());
12238 C->setVarRefs(Vars);
12239 Vars.clear();
12240 for (unsigned I = 0; I != NumVars; ++I)
12241 Vars.push_back(Elt: Record.readSubExpr());
12242 C->setPrivates(Vars);
12243 Vars.clear();
12244 for (unsigned I = 0; I != NumVars; ++I)
12245 Vars.push_back(Elt: Record.readSubExpr());
12246 C->setLHSExprs(Vars);
12247 Vars.clear();
12248 for (unsigned I = 0; I != NumVars; ++I)
12249 Vars.push_back(Elt: Record.readSubExpr());
12250 C->setRHSExprs(Vars);
12251 Vars.clear();
12252 for (unsigned I = 0; I != NumVars; ++I)
12253 Vars.push_back(Elt: Record.readSubExpr());
12254 C->setReductionOps(Vars);
12255 Vars.clear();
12256 for (unsigned I = 0; I != NumVars; ++I)
12257 Vars.push_back(Elt: Record.readSubExpr());
12258 C->setTaskgroupDescriptors(Vars);
12259}
12260
12261void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12262 VisitOMPClauseWithPostUpdate(C);
12263 C->setLParenLoc(Record.readSourceLocation());
12264 C->setColonLoc(Record.readSourceLocation());
12265 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12266 C->setModifierLoc(Record.readSourceLocation());
12267 unsigned NumVars = C->varlist_size();
12268 SmallVector<Expr *, 16> Vars;
12269 Vars.reserve(N: NumVars);
12270 for (unsigned i = 0; i != NumVars; ++i)
12271 Vars.push_back(Elt: Record.readSubExpr());
12272 C->setVarRefs(Vars);
12273 Vars.clear();
12274 for (unsigned i = 0; i != NumVars; ++i)
12275 Vars.push_back(Elt: Record.readSubExpr());
12276 C->setPrivates(Vars);
12277 Vars.clear();
12278 for (unsigned i = 0; i != NumVars; ++i)
12279 Vars.push_back(Elt: Record.readSubExpr());
12280 C->setInits(Vars);
12281 Vars.clear();
12282 for (unsigned i = 0; i != NumVars; ++i)
12283 Vars.push_back(Elt: Record.readSubExpr());
12284 C->setUpdates(Vars);
12285 Vars.clear();
12286 for (unsigned i = 0; i != NumVars; ++i)
12287 Vars.push_back(Elt: Record.readSubExpr());
12288 C->setFinals(Vars);
12289 C->setStep(Record.readSubExpr());
12290 C->setCalcStep(Record.readSubExpr());
12291 Vars.clear();
12292 for (unsigned I = 0; I != NumVars + 1; ++I)
12293 Vars.push_back(Elt: Record.readSubExpr());
12294 C->setUsedExprs(Vars);
12295}
12296
12297void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12298 C->setLParenLoc(Record.readSourceLocation());
12299 C->setColonLoc(Record.readSourceLocation());
12300 unsigned NumVars = C->varlist_size();
12301 SmallVector<Expr *, 16> Vars;
12302 Vars.reserve(N: NumVars);
12303 for (unsigned i = 0; i != NumVars; ++i)
12304 Vars.push_back(Elt: Record.readSubExpr());
12305 C->setVarRefs(Vars);
12306 C->setAlignment(Record.readSubExpr());
12307}
12308
12309void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12310 C->setLParenLoc(Record.readSourceLocation());
12311 unsigned NumVars = C->varlist_size();
12312 SmallVector<Expr *, 16> Exprs;
12313 Exprs.reserve(N: NumVars);
12314 for (unsigned i = 0; i != NumVars; ++i)
12315 Exprs.push_back(Elt: Record.readSubExpr());
12316 C->setVarRefs(Exprs);
12317 Exprs.clear();
12318 for (unsigned i = 0; i != NumVars; ++i)
12319 Exprs.push_back(Elt: Record.readSubExpr());
12320 C->setSourceExprs(Exprs);
12321 Exprs.clear();
12322 for (unsigned i = 0; i != NumVars; ++i)
12323 Exprs.push_back(Elt: Record.readSubExpr());
12324 C->setDestinationExprs(Exprs);
12325 Exprs.clear();
12326 for (unsigned i = 0; i != NumVars; ++i)
12327 Exprs.push_back(Elt: Record.readSubExpr());
12328 C->setAssignmentOps(Exprs);
12329}
12330
12331void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12332 C->setLParenLoc(Record.readSourceLocation());
12333 unsigned NumVars = C->varlist_size();
12334 SmallVector<Expr *, 16> Exprs;
12335 Exprs.reserve(N: NumVars);
12336 for (unsigned i = 0; i != NumVars; ++i)
12337 Exprs.push_back(Elt: Record.readSubExpr());
12338 C->setVarRefs(Exprs);
12339 Exprs.clear();
12340 for (unsigned i = 0; i != NumVars; ++i)
12341 Exprs.push_back(Elt: Record.readSubExpr());
12342 C->setSourceExprs(Exprs);
12343 Exprs.clear();
12344 for (unsigned i = 0; i != NumVars; ++i)
12345 Exprs.push_back(Elt: Record.readSubExpr());
12346 C->setDestinationExprs(Exprs);
12347 Exprs.clear();
12348 for (unsigned i = 0; i != NumVars; ++i)
12349 Exprs.push_back(Elt: Record.readSubExpr());
12350 C->setAssignmentOps(Exprs);
12351}
12352
12353void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12354 C->setLParenLoc(Record.readSourceLocation());
12355 unsigned NumVars = C->varlist_size();
12356 SmallVector<Expr *, 16> Vars;
12357 Vars.reserve(N: NumVars);
12358 for (unsigned i = 0; i != NumVars; ++i)
12359 Vars.push_back(Elt: Record.readSubExpr());
12360 C->setVarRefs(Vars);
12361}
12362
12363void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12364 C->setDepobj(Record.readSubExpr());
12365 C->setLParenLoc(Record.readSourceLocation());
12366}
12367
12368void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12369 C->setLParenLoc(Record.readSourceLocation());
12370 C->setModifier(Record.readSubExpr());
12371 C->setDependencyKind(
12372 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12373 C->setDependencyLoc(Record.readSourceLocation());
12374 C->setColonLoc(Record.readSourceLocation());
12375 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12376 unsigned NumVars = C->varlist_size();
12377 SmallVector<Expr *, 16> Vars;
12378 Vars.reserve(N: NumVars);
12379 for (unsigned I = 0; I != NumVars; ++I)
12380 Vars.push_back(Elt: Record.readSubExpr());
12381 C->setVarRefs(Vars);
12382 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12383 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12384}
12385
12386void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12387 VisitOMPClauseWithPreInit(C);
12388 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12389 C->setDevice(Record.readSubExpr());
12390 C->setModifierLoc(Record.readSourceLocation());
12391 C->setLParenLoc(Record.readSourceLocation());
12392}
12393
12394void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12395 C->setLParenLoc(Record.readSourceLocation());
12396 bool HasIteratorModifier = false;
12397 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12398 C->setMapTypeModifier(
12399 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12400 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12401 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12402 HasIteratorModifier = true;
12403 }
12404 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12405 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12406 C->setMapType(
12407 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12408 C->setMapLoc(Record.readSourceLocation());
12409 C->setColonLoc(Record.readSourceLocation());
12410 auto NumVars = C->varlist_size();
12411 auto UniqueDecls = C->getUniqueDeclarationsNum();
12412 auto TotalLists = C->getTotalComponentListNum();
12413 auto TotalComponents = C->getTotalComponentsNum();
12414
12415 SmallVector<Expr *, 16> Vars;
12416 Vars.reserve(N: NumVars);
12417 for (unsigned i = 0; i != NumVars; ++i)
12418 Vars.push_back(Elt: Record.readExpr());
12419 C->setVarRefs(Vars);
12420
12421 SmallVector<Expr *, 16> UDMappers;
12422 UDMappers.reserve(N: NumVars);
12423 for (unsigned I = 0; I < NumVars; ++I)
12424 UDMappers.push_back(Elt: Record.readExpr());
12425 C->setUDMapperRefs(UDMappers);
12426
12427 if (HasIteratorModifier)
12428 C->setIteratorModifier(Record.readExpr());
12429
12430 SmallVector<ValueDecl *, 16> Decls;
12431 Decls.reserve(N: UniqueDecls);
12432 for (unsigned i = 0; i < UniqueDecls; ++i)
12433 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12434 C->setUniqueDecls(Decls);
12435
12436 SmallVector<unsigned, 16> ListsPerDecl;
12437 ListsPerDecl.reserve(N: UniqueDecls);
12438 for (unsigned i = 0; i < UniqueDecls; ++i)
12439 ListsPerDecl.push_back(Elt: Record.readInt());
12440 C->setDeclNumLists(ListsPerDecl);
12441
12442 SmallVector<unsigned, 32> ListSizes;
12443 ListSizes.reserve(N: TotalLists);
12444 for (unsigned i = 0; i < TotalLists; ++i)
12445 ListSizes.push_back(Elt: Record.readInt());
12446 C->setComponentListSizes(ListSizes);
12447
12448 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12449 Components.reserve(N: TotalComponents);
12450 for (unsigned i = 0; i < TotalComponents; ++i) {
12451 Expr *AssociatedExprPr = Record.readExpr();
12452 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12453 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12454 /*IsNonContiguous=*/Args: false);
12455 }
12456 C->setComponents(Components, CLSs: ListSizes);
12457}
12458
12459void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12460 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12461 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12462 C->setLParenLoc(Record.readSourceLocation());
12463 C->setColonLoc(Record.readSourceLocation());
12464 C->setAllocator(Record.readSubExpr());
12465 C->setAlignment(Record.readSubExpr());
12466 unsigned NumVars = C->varlist_size();
12467 SmallVector<Expr *, 16> Vars;
12468 Vars.reserve(N: NumVars);
12469 for (unsigned i = 0; i != NumVars; ++i)
12470 Vars.push_back(Elt: Record.readSubExpr());
12471 C->setVarRefs(Vars);
12472}
12473
12474void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12475 VisitOMPClauseWithPreInit(C);
12476 C->setLParenLoc(Record.readSourceLocation());
12477 unsigned NumVars = C->varlist_size();
12478 SmallVector<Expr *, 16> Vars;
12479 Vars.reserve(N: NumVars);
12480 for (unsigned I = 0; I != NumVars; ++I)
12481 Vars.push_back(Elt: Record.readSubExpr());
12482 C->setVarRefs(Vars);
12483}
12484
12485void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12486 VisitOMPClauseWithPreInit(C);
12487 C->setLParenLoc(Record.readSourceLocation());
12488 unsigned NumVars = C->varlist_size();
12489 SmallVector<Expr *, 16> Vars;
12490 Vars.reserve(N: NumVars);
12491 for (unsigned I = 0; I != NumVars; ++I)
12492 Vars.push_back(Elt: Record.readSubExpr());
12493 C->setVarRefs(Vars);
12494}
12495
12496void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12497 VisitOMPClauseWithPreInit(C);
12498 C->setPriority(Record.readSubExpr());
12499 C->setLParenLoc(Record.readSourceLocation());
12500}
12501
12502void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12503 VisitOMPClauseWithPreInit(C);
12504 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12505 C->setGrainsize(Record.readSubExpr());
12506 C->setModifierLoc(Record.readSourceLocation());
12507 C->setLParenLoc(Record.readSourceLocation());
12508}
12509
12510void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12511 VisitOMPClauseWithPreInit(C);
12512 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12513 C->setNumTasks(Record.readSubExpr());
12514 C->setModifierLoc(Record.readSourceLocation());
12515 C->setLParenLoc(Record.readSourceLocation());
12516}
12517
12518void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12519 C->setHint(Record.readSubExpr());
12520 C->setLParenLoc(Record.readSourceLocation());
12521}
12522
12523void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12524 VisitOMPClauseWithPreInit(C);
12525 C->setDistScheduleKind(
12526 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12527 C->setChunkSize(Record.readSubExpr());
12528 C->setLParenLoc(Record.readSourceLocation());
12529 C->setDistScheduleKindLoc(Record.readSourceLocation());
12530 C->setCommaLoc(Record.readSourceLocation());
12531}
12532
12533void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12534 C->setDefaultmapKind(
12535 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12536 C->setDefaultmapModifier(
12537 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12538 C->setLParenLoc(Record.readSourceLocation());
12539 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12540 C->setDefaultmapKindLoc(Record.readSourceLocation());
12541}
12542
12543void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12544 C->setLParenLoc(Record.readSourceLocation());
12545 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12546 C->setMotionModifier(
12547 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12548 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12549 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12550 C->setIteratorModifier(Record.readExpr());
12551 }
12552 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12553 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12554 C->setColonLoc(Record.readSourceLocation());
12555 auto NumVars = C->varlist_size();
12556 auto UniqueDecls = C->getUniqueDeclarationsNum();
12557 auto TotalLists = C->getTotalComponentListNum();
12558 auto TotalComponents = C->getTotalComponentsNum();
12559
12560 SmallVector<Expr *, 16> Vars;
12561 Vars.reserve(N: NumVars);
12562 for (unsigned i = 0; i != NumVars; ++i)
12563 Vars.push_back(Elt: Record.readSubExpr());
12564 C->setVarRefs(Vars);
12565
12566 SmallVector<Expr *, 16> UDMappers;
12567 UDMappers.reserve(N: NumVars);
12568 for (unsigned I = 0; I < NumVars; ++I)
12569 UDMappers.push_back(Elt: Record.readSubExpr());
12570 C->setUDMapperRefs(UDMappers);
12571
12572 SmallVector<ValueDecl *, 16> Decls;
12573 Decls.reserve(N: UniqueDecls);
12574 for (unsigned i = 0; i < UniqueDecls; ++i)
12575 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12576 C->setUniqueDecls(Decls);
12577
12578 SmallVector<unsigned, 16> ListsPerDecl;
12579 ListsPerDecl.reserve(N: UniqueDecls);
12580 for (unsigned i = 0; i < UniqueDecls; ++i)
12581 ListsPerDecl.push_back(Elt: Record.readInt());
12582 C->setDeclNumLists(ListsPerDecl);
12583
12584 SmallVector<unsigned, 32> ListSizes;
12585 ListSizes.reserve(N: TotalLists);
12586 for (unsigned i = 0; i < TotalLists; ++i)
12587 ListSizes.push_back(Elt: Record.readInt());
12588 C->setComponentListSizes(ListSizes);
12589
12590 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12591 Components.reserve(N: TotalComponents);
12592 for (unsigned i = 0; i < TotalComponents; ++i) {
12593 Expr *AssociatedExprPr = Record.readSubExpr();
12594 bool IsNonContiguous = Record.readBool();
12595 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12596 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12597 }
12598 C->setComponents(Components, CLSs: ListSizes);
12599}
12600
12601void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12602 C->setLParenLoc(Record.readSourceLocation());
12603 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12604 C->setMotionModifier(
12605 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12606 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12607 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12608 C->setIteratorModifier(Record.readExpr());
12609 }
12610 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12611 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12612 C->setColonLoc(Record.readSourceLocation());
12613 auto NumVars = C->varlist_size();
12614 auto UniqueDecls = C->getUniqueDeclarationsNum();
12615 auto TotalLists = C->getTotalComponentListNum();
12616 auto TotalComponents = C->getTotalComponentsNum();
12617
12618 SmallVector<Expr *, 16> Vars;
12619 Vars.reserve(N: NumVars);
12620 for (unsigned i = 0; i != NumVars; ++i)
12621 Vars.push_back(Elt: Record.readSubExpr());
12622 C->setVarRefs(Vars);
12623
12624 SmallVector<Expr *, 16> UDMappers;
12625 UDMappers.reserve(N: NumVars);
12626 for (unsigned I = 0; I < NumVars; ++I)
12627 UDMappers.push_back(Elt: Record.readSubExpr());
12628 C->setUDMapperRefs(UDMappers);
12629
12630 SmallVector<ValueDecl *, 16> Decls;
12631 Decls.reserve(N: UniqueDecls);
12632 for (unsigned i = 0; i < UniqueDecls; ++i)
12633 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12634 C->setUniqueDecls(Decls);
12635
12636 SmallVector<unsigned, 16> ListsPerDecl;
12637 ListsPerDecl.reserve(N: UniqueDecls);
12638 for (unsigned i = 0; i < UniqueDecls; ++i)
12639 ListsPerDecl.push_back(Elt: Record.readInt());
12640 C->setDeclNumLists(ListsPerDecl);
12641
12642 SmallVector<unsigned, 32> ListSizes;
12643 ListSizes.reserve(N: TotalLists);
12644 for (unsigned i = 0; i < TotalLists; ++i)
12645 ListSizes.push_back(Elt: Record.readInt());
12646 C->setComponentListSizes(ListSizes);
12647
12648 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12649 Components.reserve(N: TotalComponents);
12650 for (unsigned i = 0; i < TotalComponents; ++i) {
12651 Expr *AssociatedExprPr = Record.readSubExpr();
12652 bool IsNonContiguous = Record.readBool();
12653 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12654 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12655 }
12656 C->setComponents(Components, CLSs: ListSizes);
12657}
12658
12659void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12660 C->setLParenLoc(Record.readSourceLocation());
12661 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12662 C->setFallbackModifierLoc(Record.readSourceLocation());
12663 auto NumVars = C->varlist_size();
12664 auto UniqueDecls = C->getUniqueDeclarationsNum();
12665 auto TotalLists = C->getTotalComponentListNum();
12666 auto TotalComponents = C->getTotalComponentsNum();
12667
12668 SmallVector<Expr *, 16> Vars;
12669 Vars.reserve(N: NumVars);
12670 for (unsigned i = 0; i != NumVars; ++i)
12671 Vars.push_back(Elt: Record.readSubExpr());
12672 C->setVarRefs(Vars);
12673 Vars.clear();
12674 for (unsigned i = 0; i != NumVars; ++i)
12675 Vars.push_back(Elt: Record.readSubExpr());
12676 C->setPrivateCopies(Vars);
12677 Vars.clear();
12678 for (unsigned i = 0; i != NumVars; ++i)
12679 Vars.push_back(Elt: Record.readSubExpr());
12680 C->setInits(Vars);
12681
12682 SmallVector<ValueDecl *, 16> Decls;
12683 Decls.reserve(N: UniqueDecls);
12684 for (unsigned i = 0; i < UniqueDecls; ++i)
12685 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12686 C->setUniqueDecls(Decls);
12687
12688 SmallVector<unsigned, 16> ListsPerDecl;
12689 ListsPerDecl.reserve(N: UniqueDecls);
12690 for (unsigned i = 0; i < UniqueDecls; ++i)
12691 ListsPerDecl.push_back(Elt: Record.readInt());
12692 C->setDeclNumLists(ListsPerDecl);
12693
12694 SmallVector<unsigned, 32> ListSizes;
12695 ListSizes.reserve(N: TotalLists);
12696 for (unsigned i = 0; i < TotalLists; ++i)
12697 ListSizes.push_back(Elt: Record.readInt());
12698 C->setComponentListSizes(ListSizes);
12699
12700 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12701 Components.reserve(N: TotalComponents);
12702 for (unsigned i = 0; i < TotalComponents; ++i) {
12703 auto *AssociatedExprPr = Record.readSubExpr();
12704 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12705 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12706 /*IsNonContiguous=*/Args: false);
12707 }
12708 C->setComponents(Components, CLSs: ListSizes);
12709}
12710
12711void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12712 C->setLParenLoc(Record.readSourceLocation());
12713 auto NumVars = C->varlist_size();
12714 auto UniqueDecls = C->getUniqueDeclarationsNum();
12715 auto TotalLists = C->getTotalComponentListNum();
12716 auto TotalComponents = C->getTotalComponentsNum();
12717
12718 SmallVector<Expr *, 16> Vars;
12719 Vars.reserve(N: NumVars);
12720 for (unsigned i = 0; i != NumVars; ++i)
12721 Vars.push_back(Elt: Record.readSubExpr());
12722 C->setVarRefs(Vars);
12723
12724 SmallVector<ValueDecl *, 16> Decls;
12725 Decls.reserve(N: UniqueDecls);
12726 for (unsigned i = 0; i < UniqueDecls; ++i)
12727 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12728 C->setUniqueDecls(Decls);
12729
12730 SmallVector<unsigned, 16> ListsPerDecl;
12731 ListsPerDecl.reserve(N: UniqueDecls);
12732 for (unsigned i = 0; i < UniqueDecls; ++i)
12733 ListsPerDecl.push_back(Elt: Record.readInt());
12734 C->setDeclNumLists(ListsPerDecl);
12735
12736 SmallVector<unsigned, 32> ListSizes;
12737 ListSizes.reserve(N: TotalLists);
12738 for (unsigned i = 0; i < TotalLists; ++i)
12739 ListSizes.push_back(Elt: Record.readInt());
12740 C->setComponentListSizes(ListSizes);
12741
12742 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12743 Components.reserve(N: TotalComponents);
12744 for (unsigned i = 0; i < TotalComponents; ++i) {
12745 Expr *AssociatedExpr = Record.readSubExpr();
12746 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12747 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12748 /*IsNonContiguous*/ Args: false);
12749 }
12750 C->setComponents(Components, CLSs: ListSizes);
12751}
12752
12753void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12754 C->setLParenLoc(Record.readSourceLocation());
12755 auto NumVars = C->varlist_size();
12756 auto UniqueDecls = C->getUniqueDeclarationsNum();
12757 auto TotalLists = C->getTotalComponentListNum();
12758 auto TotalComponents = C->getTotalComponentsNum();
12759
12760 SmallVector<Expr *, 16> Vars;
12761 Vars.reserve(N: NumVars);
12762 for (unsigned i = 0; i != NumVars; ++i)
12763 Vars.push_back(Elt: Record.readSubExpr());
12764 C->setVarRefs(Vars);
12765 Vars.clear();
12766
12767 SmallVector<ValueDecl *, 16> Decls;
12768 Decls.reserve(N: UniqueDecls);
12769 for (unsigned i = 0; i < UniqueDecls; ++i)
12770 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12771 C->setUniqueDecls(Decls);
12772
12773 SmallVector<unsigned, 16> ListsPerDecl;
12774 ListsPerDecl.reserve(N: UniqueDecls);
12775 for (unsigned i = 0; i < UniqueDecls; ++i)
12776 ListsPerDecl.push_back(Elt: Record.readInt());
12777 C->setDeclNumLists(ListsPerDecl);
12778
12779 SmallVector<unsigned, 32> ListSizes;
12780 ListSizes.reserve(N: TotalLists);
12781 for (unsigned i = 0; i < TotalLists; ++i)
12782 ListSizes.push_back(Elt: Record.readInt());
12783 C->setComponentListSizes(ListSizes);
12784
12785 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12786 Components.reserve(N: TotalComponents);
12787 for (unsigned i = 0; i < TotalComponents; ++i) {
12788 Expr *AssociatedExpr = Record.readSubExpr();
12789 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12790 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12791 /*IsNonContiguous=*/Args: false);
12792 }
12793 C->setComponents(Components, CLSs: ListSizes);
12794}
12795
12796void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12797 C->setLParenLoc(Record.readSourceLocation());
12798 auto NumVars = C->varlist_size();
12799 auto UniqueDecls = C->getUniqueDeclarationsNum();
12800 auto TotalLists = C->getTotalComponentListNum();
12801 auto TotalComponents = C->getTotalComponentsNum();
12802
12803 SmallVector<Expr *, 16> Vars;
12804 Vars.reserve(N: NumVars);
12805 for (unsigned I = 0; I != NumVars; ++I)
12806 Vars.push_back(Elt: Record.readSubExpr());
12807 C->setVarRefs(Vars);
12808 Vars.clear();
12809
12810 SmallVector<ValueDecl *, 16> Decls;
12811 Decls.reserve(N: UniqueDecls);
12812 for (unsigned I = 0; I < UniqueDecls; ++I)
12813 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12814 C->setUniqueDecls(Decls);
12815
12816 SmallVector<unsigned, 16> ListsPerDecl;
12817 ListsPerDecl.reserve(N: UniqueDecls);
12818 for (unsigned I = 0; I < UniqueDecls; ++I)
12819 ListsPerDecl.push_back(Elt: Record.readInt());
12820 C->setDeclNumLists(ListsPerDecl);
12821
12822 SmallVector<unsigned, 32> ListSizes;
12823 ListSizes.reserve(N: TotalLists);
12824 for (unsigned i = 0; i < TotalLists; ++i)
12825 ListSizes.push_back(Elt: Record.readInt());
12826 C->setComponentListSizes(ListSizes);
12827
12828 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12829 Components.reserve(N: TotalComponents);
12830 for (unsigned I = 0; I < TotalComponents; ++I) {
12831 Expr *AssociatedExpr = Record.readSubExpr();
12832 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12833 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12834 /*IsNonContiguous=*/Args: false);
12835 }
12836 C->setComponents(Components, CLSs: ListSizes);
12837}
12838
12839void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12840 C->setLParenLoc(Record.readSourceLocation());
12841 unsigned NumVars = C->varlist_size();
12842 SmallVector<Expr *, 16> Vars;
12843 Vars.reserve(N: NumVars);
12844 for (unsigned i = 0; i != NumVars; ++i)
12845 Vars.push_back(Elt: Record.readSubExpr());
12846 C->setVarRefs(Vars);
12847 Vars.clear();
12848 Vars.reserve(N: NumVars);
12849 for (unsigned i = 0; i != NumVars; ++i)
12850 Vars.push_back(Elt: Record.readSubExpr());
12851 C->setPrivateRefs(Vars);
12852}
12853
12854void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12855 C->setLParenLoc(Record.readSourceLocation());
12856 unsigned NumVars = C->varlist_size();
12857 SmallVector<Expr *, 16> Vars;
12858 Vars.reserve(N: NumVars);
12859 for (unsigned i = 0; i != NumVars; ++i)
12860 Vars.push_back(Elt: Record.readSubExpr());
12861 C->setVarRefs(Vars);
12862}
12863
12864void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12865 C->setLParenLoc(Record.readSourceLocation());
12866 unsigned NumVars = C->varlist_size();
12867 SmallVector<Expr *, 16> Vars;
12868 Vars.reserve(N: NumVars);
12869 for (unsigned i = 0; i != NumVars; ++i)
12870 Vars.push_back(Elt: Record.readSubExpr());
12871 C->setVarRefs(Vars);
12872}
12873
12874void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12875 C->setLParenLoc(Record.readSourceLocation());
12876 unsigned NumOfAllocators = C->getNumberOfAllocators();
12877 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12878 Data.reserve(N: NumOfAllocators);
12879 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12880 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12881 D.Allocator = Record.readSubExpr();
12882 D.AllocatorTraits = Record.readSubExpr();
12883 D.LParenLoc = Record.readSourceLocation();
12884 D.RParenLoc = Record.readSourceLocation();
12885 }
12886 C->setAllocatorsData(Data);
12887}
12888
12889void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12890 C->setLParenLoc(Record.readSourceLocation());
12891 C->setModifier(Record.readSubExpr());
12892 C->setColonLoc(Record.readSourceLocation());
12893 unsigned NumOfLocators = C->varlist_size();
12894 SmallVector<Expr *, 4> Locators;
12895 Locators.reserve(N: NumOfLocators);
12896 for (unsigned I = 0; I != NumOfLocators; ++I)
12897 Locators.push_back(Elt: Record.readSubExpr());
12898 C->setVarRefs(Locators);
12899}
12900
12901void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12902 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12903 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
12904 C->setLParenLoc(Record.readSourceLocation());
12905 C->setKindKwLoc(Record.readSourceLocation());
12906 C->setModifierKwLoc(Record.readSourceLocation());
12907}
12908
12909void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12910 VisitOMPClauseWithPreInit(C);
12911 C->setThreadID(Record.readSubExpr());
12912 C->setLParenLoc(Record.readSourceLocation());
12913}
12914
12915void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12916 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12917 C->setLParenLoc(Record.readSourceLocation());
12918 C->setBindKindLoc(Record.readSourceLocation());
12919}
12920
12921void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12922 C->setAlignment(Record.readExpr());
12923 C->setLParenLoc(Record.readSourceLocation());
12924}
12925
12926void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
12927 VisitOMPClauseWithPreInit(C);
12928 C->setSize(Record.readSubExpr());
12929 C->setLParenLoc(Record.readSourceLocation());
12930}
12931
12932void OMPClauseReader::VisitOMPDynGroupprivateClause(
12933 OMPDynGroupprivateClause *C) {
12934 VisitOMPClauseWithPreInit(C);
12935 C->setDynGroupprivateModifier(
12936 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
12937 C->setDynGroupprivateFallbackModifier(
12938 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
12939 C->setSize(Record.readSubExpr());
12940 C->setLParenLoc(Record.readSourceLocation());
12941 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
12942 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
12943}
12944
12945void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
12946 C->setLParenLoc(Record.readSourceLocation());
12947 C->setDependenceType(
12948 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
12949 C->setDependenceLoc(Record.readSourceLocation());
12950 C->setColonLoc(Record.readSourceLocation());
12951 unsigned NumVars = C->varlist_size();
12952 SmallVector<Expr *, 16> Vars;
12953 Vars.reserve(N: NumVars);
12954 for (unsigned I = 0; I != NumVars; ++I)
12955 Vars.push_back(Elt: Record.readSubExpr());
12956 C->setVarRefs(Vars);
12957 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12958 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12959}
12960
12961void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
12962 AttrVec Attrs;
12963 Record.readAttributes(Attrs);
12964 C->setAttrs(Attrs);
12965 C->setLocStart(Record.readSourceLocation());
12966 C->setLParenLoc(Record.readSourceLocation());
12967 C->setLocEnd(Record.readSourceLocation());
12968}
12969
12970void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
12971
12972OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12973 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12974 TI.Sets.resize(N: readUInt32());
12975 for (auto &Set : TI.Sets) {
12976 Set.Kind = readEnum<llvm::omp::TraitSet>();
12977 Set.Selectors.resize(N: readUInt32());
12978 for (auto &Selector : Set.Selectors) {
12979 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12980 Selector.ScoreOrCondition = nullptr;
12981 if (readBool())
12982 Selector.ScoreOrCondition = readExprRef();
12983 Selector.Properties.resize(N: readUInt32());
12984 for (auto &Property : Selector.Properties)
12985 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12986 }
12987 }
12988 return &TI;
12989}
12990
12991void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12992 if (!Data)
12993 return;
12994 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12995 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12996 skipInts(N: 3);
12997 }
12998 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12999 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13000 Clauses[I] = readOMPClause();
13001 Data->setClauses(Clauses);
13002 if (Data->hasAssociatedStmt())
13003 Data->setAssociatedStmt(readStmt());
13004 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13005 Data->getChildren()[I] = readStmt();
13006}
13007
13008SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13009 unsigned NumVars = readInt();
13010 llvm::SmallVector<Expr *> VarList;
13011 for (unsigned I = 0; I < NumVars; ++I)
13012 VarList.push_back(Elt: readExpr());
13013 return VarList;
13014}
13015
13016SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13017 unsigned NumExprs = readInt();
13018 llvm::SmallVector<Expr *> ExprList;
13019 for (unsigned I = 0; I < NumExprs; ++I)
13020 ExprList.push_back(Elt: readSubExpr());
13021 return ExprList;
13022}
13023
13024OpenACCClause *ASTRecordReader::readOpenACCClause() {
13025 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13026 SourceLocation BeginLoc = readSourceLocation();
13027 SourceLocation EndLoc = readSourceLocation();
13028
13029 switch (ClauseKind) {
13030 case OpenACCClauseKind::Default: {
13031 SourceLocation LParenLoc = readSourceLocation();
13032 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13033 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13034 EndLoc);
13035 }
13036 case OpenACCClauseKind::If: {
13037 SourceLocation LParenLoc = readSourceLocation();
13038 Expr *CondExpr = readSubExpr();
13039 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13040 EndLoc);
13041 }
13042 case OpenACCClauseKind::Self: {
13043 SourceLocation LParenLoc = readSourceLocation();
13044 bool isConditionExprClause = readBool();
13045 if (isConditionExprClause) {
13046 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13047 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13048 ConditionExpr: CondExpr, EndLoc);
13049 }
13050 unsigned NumVars = readInt();
13051 llvm::SmallVector<Expr *> VarList;
13052 for (unsigned I = 0; I < NumVars; ++I)
13053 VarList.push_back(Elt: readSubExpr());
13054 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13055 EndLoc);
13056 }
13057 case OpenACCClauseKind::NumGangs: {
13058 SourceLocation LParenLoc = readSourceLocation();
13059 unsigned NumClauses = readInt();
13060 llvm::SmallVector<Expr *> IntExprs;
13061 for (unsigned I = 0; I < NumClauses; ++I)
13062 IntExprs.push_back(Elt: readSubExpr());
13063 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13064 IntExprs, EndLoc);
13065 }
13066 case OpenACCClauseKind::NumWorkers: {
13067 SourceLocation LParenLoc = readSourceLocation();
13068 Expr *IntExpr = readSubExpr();
13069 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13070 IntExpr, EndLoc);
13071 }
13072 case OpenACCClauseKind::DeviceNum: {
13073 SourceLocation LParenLoc = readSourceLocation();
13074 Expr *IntExpr = readSubExpr();
13075 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13076 IntExpr, EndLoc);
13077 }
13078 case OpenACCClauseKind::DefaultAsync: {
13079 SourceLocation LParenLoc = readSourceLocation();
13080 Expr *IntExpr = readSubExpr();
13081 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13082 IntExpr, EndLoc);
13083 }
13084 case OpenACCClauseKind::VectorLength: {
13085 SourceLocation LParenLoc = readSourceLocation();
13086 Expr *IntExpr = readSubExpr();
13087 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13088 IntExpr, EndLoc);
13089 }
13090 case OpenACCClauseKind::Private: {
13091 SourceLocation LParenLoc = readSourceLocation();
13092 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13093
13094 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13095 for (unsigned I = 0; I < VarList.size(); ++I) {
13096 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13097 VarDecl *Alloca = readDeclAs<VarDecl>();
13098 RecipeList.push_back(Elt: {Alloca});
13099 }
13100
13101 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13102 VarList, InitRecipes: RecipeList, EndLoc);
13103 }
13104 case OpenACCClauseKind::Host: {
13105 SourceLocation LParenLoc = readSourceLocation();
13106 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13107 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13108 EndLoc);
13109 }
13110 case OpenACCClauseKind::Device: {
13111 SourceLocation LParenLoc = readSourceLocation();
13112 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13113 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13114 VarList, EndLoc);
13115 }
13116 case OpenACCClauseKind::FirstPrivate: {
13117 SourceLocation LParenLoc = readSourceLocation();
13118 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13119 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13120 for (unsigned I = 0; I < VarList.size(); ++I) {
13121 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13122 VarDecl *Recipe = readDeclAs<VarDecl>();
13123 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13124 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13125 }
13126
13127 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13128 VarList, InitRecipes: RecipeList, EndLoc);
13129 }
13130 case OpenACCClauseKind::Attach: {
13131 SourceLocation LParenLoc = readSourceLocation();
13132 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13133 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13134 VarList, EndLoc);
13135 }
13136 case OpenACCClauseKind::Detach: {
13137 SourceLocation LParenLoc = readSourceLocation();
13138 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13139 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13140 VarList, EndLoc);
13141 }
13142 case OpenACCClauseKind::Delete: {
13143 SourceLocation LParenLoc = readSourceLocation();
13144 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13145 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13146 VarList, EndLoc);
13147 }
13148 case OpenACCClauseKind::UseDevice: {
13149 SourceLocation LParenLoc = readSourceLocation();
13150 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13151 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13152 VarList, EndLoc);
13153 }
13154 case OpenACCClauseKind::DevicePtr: {
13155 SourceLocation LParenLoc = readSourceLocation();
13156 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13157 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13158 VarList, EndLoc);
13159 }
13160 case OpenACCClauseKind::NoCreate: {
13161 SourceLocation LParenLoc = readSourceLocation();
13162 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13163 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13164 VarList, EndLoc);
13165 }
13166 case OpenACCClauseKind::Present: {
13167 SourceLocation LParenLoc = readSourceLocation();
13168 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13169 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13170 VarList, EndLoc);
13171 }
13172 case OpenACCClauseKind::PCopy:
13173 case OpenACCClauseKind::PresentOrCopy:
13174 case OpenACCClauseKind::Copy: {
13175 SourceLocation LParenLoc = readSourceLocation();
13176 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13177 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13178 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13179 LParenLoc, Mods: ModList, VarList, EndLoc);
13180 }
13181 case OpenACCClauseKind::CopyIn:
13182 case OpenACCClauseKind::PCopyIn:
13183 case OpenACCClauseKind::PresentOrCopyIn: {
13184 SourceLocation LParenLoc = readSourceLocation();
13185 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13186 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13187 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13188 LParenLoc, Mods: ModList, VarList, EndLoc);
13189 }
13190 case OpenACCClauseKind::CopyOut:
13191 case OpenACCClauseKind::PCopyOut:
13192 case OpenACCClauseKind::PresentOrCopyOut: {
13193 SourceLocation LParenLoc = readSourceLocation();
13194 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13195 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13196 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13197 LParenLoc, Mods: ModList, VarList, EndLoc);
13198 }
13199 case OpenACCClauseKind::Create:
13200 case OpenACCClauseKind::PCreate:
13201 case OpenACCClauseKind::PresentOrCreate: {
13202 SourceLocation LParenLoc = readSourceLocation();
13203 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13204 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13205 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13206 LParenLoc, Mods: ModList, VarList, EndLoc);
13207 }
13208 case OpenACCClauseKind::Async: {
13209 SourceLocation LParenLoc = readSourceLocation();
13210 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13211 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13212 IntExpr: AsyncExpr, EndLoc);
13213 }
13214 case OpenACCClauseKind::Wait: {
13215 SourceLocation LParenLoc = readSourceLocation();
13216 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13217 SourceLocation QueuesLoc = readSourceLocation();
13218 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13219 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13220 DevNumExpr, QueuesLoc, QueueIdExprs,
13221 EndLoc);
13222 }
13223 case OpenACCClauseKind::DeviceType:
13224 case OpenACCClauseKind::DType: {
13225 SourceLocation LParenLoc = readSourceLocation();
13226 llvm::SmallVector<DeviceTypeArgument> Archs;
13227 unsigned NumArchs = readInt();
13228
13229 for (unsigned I = 0; I < NumArchs; ++I) {
13230 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13231 SourceLocation Loc = readSourceLocation();
13232 Archs.emplace_back(Args&: Loc, Args&: Ident);
13233 }
13234
13235 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13236 LParenLoc, Archs, EndLoc);
13237 }
13238 case OpenACCClauseKind::Reduction: {
13239 SourceLocation LParenLoc = readSourceLocation();
13240 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13241 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13242 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13243
13244 for (unsigned I = 0; I < VarList.size(); ++I) {
13245 VarDecl *Recipe = readDeclAs<VarDecl>();
13246
13247 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13248 3 * sizeof(int *));
13249
13250 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13251 unsigned NumCombiners = readInt();
13252 for (unsigned I = 0; I < NumCombiners; ++I) {
13253 VarDecl *LHS = readDeclAs<VarDecl>();
13254 VarDecl *RHS = readDeclAs<VarDecl>();
13255 Expr *Op = readExpr();
13256
13257 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13258 }
13259
13260 RecipeList.push_back(Elt: {Recipe, Combiners});
13261 }
13262
13263 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13264 VarList, Recipes: RecipeList, EndLoc);
13265 }
13266 case OpenACCClauseKind::Seq:
13267 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13268 case OpenACCClauseKind::NoHost:
13269 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13270 case OpenACCClauseKind::Finalize:
13271 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13272 case OpenACCClauseKind::IfPresent:
13273 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13274 case OpenACCClauseKind::Independent:
13275 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13276 case OpenACCClauseKind::Auto:
13277 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13278 case OpenACCClauseKind::Collapse: {
13279 SourceLocation LParenLoc = readSourceLocation();
13280 bool HasForce = readBool();
13281 Expr *LoopCount = readSubExpr();
13282 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13283 HasForce, LoopCount, EndLoc);
13284 }
13285 case OpenACCClauseKind::Tile: {
13286 SourceLocation LParenLoc = readSourceLocation();
13287 unsigned NumClauses = readInt();
13288 llvm::SmallVector<Expr *> SizeExprs;
13289 for (unsigned I = 0; I < NumClauses; ++I)
13290 SizeExprs.push_back(Elt: readSubExpr());
13291 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13292 SizeExprs, EndLoc);
13293 }
13294 case OpenACCClauseKind::Gang: {
13295 SourceLocation LParenLoc = readSourceLocation();
13296 unsigned NumExprs = readInt();
13297 llvm::SmallVector<OpenACCGangKind> GangKinds;
13298 llvm::SmallVector<Expr *> Exprs;
13299 for (unsigned I = 0; I < NumExprs; ++I) {
13300 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13301 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13302 Exprs.push_back(Elt: readExpr());
13303 }
13304 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13305 GangKinds, IntExprs: Exprs, EndLoc);
13306 }
13307 case OpenACCClauseKind::Worker: {
13308 SourceLocation LParenLoc = readSourceLocation();
13309 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13310 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13311 IntExpr: WorkerExpr, EndLoc);
13312 }
13313 case OpenACCClauseKind::Vector: {
13314 SourceLocation LParenLoc = readSourceLocation();
13315 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13316 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13317 IntExpr: VectorExpr, EndLoc);
13318 }
13319 case OpenACCClauseKind::Link: {
13320 SourceLocation LParenLoc = readSourceLocation();
13321 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13322 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13323 EndLoc);
13324 }
13325 case OpenACCClauseKind::DeviceResident: {
13326 SourceLocation LParenLoc = readSourceLocation();
13327 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13328 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13329 LParenLoc, VarList, EndLoc);
13330 }
13331
13332 case OpenACCClauseKind::Bind: {
13333 SourceLocation LParenLoc = readSourceLocation();
13334 bool IsString = readBool();
13335 if (IsString)
13336 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13337 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13338 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13339 ID: readIdentifier(), EndLoc);
13340 }
13341 case OpenACCClauseKind::Shortloop:
13342 case OpenACCClauseKind::Invalid:
13343 llvm_unreachable("Clause serialization not yet implemented");
13344 }
13345 llvm_unreachable("Invalid Clause Kind");
13346}
13347
13348void ASTRecordReader::readOpenACCClauseList(
13349 MutableArrayRef<const OpenACCClause *> Clauses) {
13350 for (unsigned I = 0; I < Clauses.size(); ++I)
13351 Clauses[I] = readOpenACCClause();
13352}
13353
13354void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13355 unsigned NumVars = readInt();
13356 A->Clauses.resize(N: NumVars);
13357 readOpenACCClauseList(Clauses: A->Clauses);
13358}
13359
13360static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13361 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13362 // module name?
13363 llvm::FoldingSetNodeID ID;
13364 ID.AddString(String: PrimaryModuleName);
13365 return ID.computeStableHash();
13366}
13367
13368UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13369 if (!M)
13370 return std::nullopt;
13371
13372 if (M->isHeaderLikeModule())
13373 return std::nullopt;
13374
13375 if (M->isGlobalModule())
13376 return std::nullopt;
13377
13378 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13379 return getStableHashForModuleName(PrimaryModuleName);
13380}
13381