1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ASTCommon.h"
14#include "ASTReaderInternals.h"
15#include "TemplateArgumentHasher.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTMutationListener.h"
19#include "clang/AST/ASTStructuralEquivalence.h"
20#include "clang/AST/ASTUnresolvedSet.h"
21#include "clang/AST/AbstractTypeReader.h"
22#include "clang/AST/Attr.h"
23#include "clang/AST/Decl.h"
24#include "clang/AST/DeclBase.h"
25#include "clang/AST/DeclCXX.h"
26#include "clang/AST/DeclFriend.h"
27#include "clang/AST/DeclGroup.h"
28#include "clang/AST/DeclObjC.h"
29#include "clang/AST/DeclTemplate.h"
30#include "clang/AST/DeclarationName.h"
31#include "clang/AST/Expr.h"
32#include "clang/AST/ExprCXX.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/NestedNameSpecifier.h"
35#include "clang/AST/ODRDiagsEmitter.h"
36#include "clang/AST/OpenACCClause.h"
37#include "clang/AST/OpenMPClause.h"
38#include "clang/AST/RawCommentList.h"
39#include "clang/AST/TemplateBase.h"
40#include "clang/AST/TemplateName.h"
41#include "clang/AST/Type.h"
42#include "clang/AST/TypeLoc.h"
43#include "clang/AST/TypeLocVisitor.h"
44#include "clang/AST/UnresolvedSet.h"
45#include "clang/Basic/ASTSourceDescriptor.h"
46#include "clang/Basic/CommentOptions.h"
47#include "clang/Basic/Diagnostic.h"
48#include "clang/Basic/DiagnosticIDs.h"
49#include "clang/Basic/DiagnosticOptions.h"
50#include "clang/Basic/DiagnosticSema.h"
51#include "clang/Basic/FileManager.h"
52#include "clang/Basic/FileSystemOptions.h"
53#include "clang/Basic/IdentifierTable.h"
54#include "clang/Basic/LLVM.h"
55#include "clang/Basic/LangOptions.h"
56#include "clang/Basic/Module.h"
57#include "clang/Basic/ObjCRuntime.h"
58#include "clang/Basic/OpenACCKinds.h"
59#include "clang/Basic/OpenMPKinds.h"
60#include "clang/Basic/OperatorKinds.h"
61#include "clang/Basic/PragmaKinds.h"
62#include "clang/Basic/Sanitizers.h"
63#include "clang/Basic/SourceLocation.h"
64#include "clang/Basic/SourceManager.h"
65#include "clang/Basic/SourceManagerInternals.h"
66#include "clang/Basic/Specifiers.h"
67#include "clang/Basic/TargetInfo.h"
68#include "clang/Basic/TargetOptions.h"
69#include "clang/Basic/TokenKinds.h"
70#include "clang/Basic/Version.h"
71#include "clang/Lex/HeaderSearch.h"
72#include "clang/Lex/HeaderSearchOptions.h"
73#include "clang/Lex/MacroInfo.h"
74#include "clang/Lex/ModuleMap.h"
75#include "clang/Lex/PreprocessingRecord.h"
76#include "clang/Lex/Preprocessor.h"
77#include "clang/Lex/PreprocessorOptions.h"
78#include "clang/Lex/Token.h"
79#include "clang/Sema/ObjCMethodList.h"
80#include "clang/Sema/Scope.h"
81#include "clang/Sema/Sema.h"
82#include "clang/Sema/SemaCUDA.h"
83#include "clang/Sema/SemaObjC.h"
84#include "clang/Sema/Weak.h"
85#include "clang/Serialization/ASTBitCodes.h"
86#include "clang/Serialization/ASTDeserializationListener.h"
87#include "clang/Serialization/ASTRecordReader.h"
88#include "clang/Serialization/ContinuousRangeMap.h"
89#include "clang/Serialization/GlobalModuleIndex.h"
90#include "clang/Serialization/InMemoryModuleCache.h"
91#include "clang/Serialization/ModuleCache.h"
92#include "clang/Serialization/ModuleFile.h"
93#include "clang/Serialization/ModuleFileExtension.h"
94#include "clang/Serialization/ModuleManager.h"
95#include "clang/Serialization/PCHContainerOperations.h"
96#include "clang/Serialization/SerializationDiagnostic.h"
97#include "llvm/ADT/APFloat.h"
98#include "llvm/ADT/APInt.h"
99#include "llvm/ADT/ArrayRef.h"
100#include "llvm/ADT/DenseMap.h"
101#include "llvm/ADT/FoldingSet.h"
102#include "llvm/ADT/IntrusiveRefCntPtr.h"
103#include "llvm/ADT/STLExtras.h"
104#include "llvm/ADT/ScopeExit.h"
105#include "llvm/ADT/Sequence.h"
106#include "llvm/ADT/SmallPtrSet.h"
107#include "llvm/ADT/SmallVector.h"
108#include "llvm/ADT/StringExtras.h"
109#include "llvm/ADT/StringMap.h"
110#include "llvm/ADT/StringRef.h"
111#include "llvm/ADT/iterator_range.h"
112#include "llvm/Bitstream/BitstreamReader.h"
113#include "llvm/Support/Compiler.h"
114#include "llvm/Support/Compression.h"
115#include "llvm/Support/DJB.h"
116#include "llvm/Support/Endian.h"
117#include "llvm/Support/Error.h"
118#include "llvm/Support/ErrorHandling.h"
119#include "llvm/Support/LEB128.h"
120#include "llvm/Support/MemoryBuffer.h"
121#include "llvm/Support/Path.h"
122#include "llvm/Support/SaveAndRestore.h"
123#include "llvm/Support/TimeProfiler.h"
124#include "llvm/Support/Timer.h"
125#include "llvm/Support/VersionTuple.h"
126#include "llvm/Support/raw_ostream.h"
127#include "llvm/TargetParser/Triple.h"
128#include <algorithm>
129#include <cassert>
130#include <cstddef>
131#include <cstdint>
132#include <cstdio>
133#include <ctime>
134#include <iterator>
135#include <limits>
136#include <map>
137#include <memory>
138#include <optional>
139#include <string>
140#include <system_error>
141#include <tuple>
142#include <utility>
143#include <vector>
144
145using namespace clang;
146using namespace clang::serialization;
147using namespace clang::serialization::reader;
148using llvm::BitstreamCursor;
149
150//===----------------------------------------------------------------------===//
151// ChainedASTReaderListener implementation
152//===----------------------------------------------------------------------===//
153
154bool
155ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
156 return First->ReadFullVersionInformation(FullVersion) ||
157 Second->ReadFullVersionInformation(FullVersion);
158}
159
160void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
161 First->ReadModuleName(ModuleName);
162 Second->ReadModuleName(ModuleName);
163}
164
165void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
166 First->ReadModuleMapFile(ModuleMapPath);
167 Second->ReadModuleMapFile(ModuleMapPath);
168}
169
170bool ChainedASTReaderListener::ReadLanguageOptions(
171 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
172 bool AllowCompatibleDifferences) {
173 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
174 AllowCompatibleDifferences) ||
175 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
176 AllowCompatibleDifferences);
177}
178
179bool ChainedASTReaderListener::ReadCodeGenOptions(
180 const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,
181 bool AllowCompatibleDifferences) {
182 return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
183 AllowCompatibleDifferences) ||
184 Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
185 AllowCompatibleDifferences);
186}
187
188bool ChainedASTReaderListener::ReadTargetOptions(
189 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
190 bool AllowCompatibleDifferences) {
191 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
192 AllowCompatibleDifferences) ||
193 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
194 AllowCompatibleDifferences);
195}
196
197bool ChainedASTReaderListener::ReadDiagnosticOptions(
198 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
199 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
200 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
201}
202
203bool
204ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
205 bool Complain) {
206 return First->ReadFileSystemOptions(FSOpts, Complain) ||
207 Second->ReadFileSystemOptions(FSOpts, Complain);
208}
209
210bool ChainedASTReaderListener::ReadHeaderSearchOptions(
211 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
212 StringRef ContextHash, bool Complain) {
213 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
214 Complain) ||
215 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
216 Complain);
217}
218
219bool ChainedASTReaderListener::ReadPreprocessorOptions(
220 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
221 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
222 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
223 Complain, SuggestedPredefines) ||
224 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
225 Complain, SuggestedPredefines);
226}
227
228void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
229 uint32_t Value) {
230 First->ReadCounter(M, Value);
231 Second->ReadCounter(M, Value);
232}
233
234bool ChainedASTReaderListener::needsInputFileVisitation() {
235 return First->needsInputFileVisitation() ||
236 Second->needsInputFileVisitation();
237}
238
239bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
240 return First->needsSystemInputFileVisitation() ||
241 Second->needsSystemInputFileVisitation();
242}
243
244void ChainedASTReaderListener::visitModuleFile(ModuleFileName Filename,
245 ModuleKind Kind,
246 bool DirectlyImported) {
247 First->visitModuleFile(Filename, Kind, DirectlyImported);
248 Second->visitModuleFile(Filename, Kind, DirectlyImported);
249}
250
251bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
252 bool isSystem,
253 bool isOverridden,
254 bool isExplicitModule) {
255 bool Continue = false;
256 if (First->needsInputFileVisitation() &&
257 (!isSystem || First->needsSystemInputFileVisitation()))
258 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
259 isExplicitModule);
260 if (Second->needsInputFileVisitation() &&
261 (!isSystem || Second->needsSystemInputFileVisitation()))
262 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
263 isExplicitModule);
264 return Continue;
265}
266
267void ChainedASTReaderListener::readModuleFileExtension(
268 const ModuleFileExtensionMetadata &Metadata) {
269 First->readModuleFileExtension(Metadata);
270 Second->readModuleFileExtension(Metadata);
271}
272
273//===----------------------------------------------------------------------===//
274// PCH validator implementation
275//===----------------------------------------------------------------------===//
276
277ASTReaderListener::~ASTReaderListener() = default;
278
279/// Compare the given set of language options against an existing set of
280/// language options.
281///
282/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
283/// \param AllowCompatibleDifferences If true, differences between compatible
284/// language options will be permitted.
285///
286/// \returns true if the languagae options mis-match, false otherwise.
287static bool checkLanguageOptions(const LangOptions &LangOpts,
288 const LangOptions &ExistingLangOpts,
289 StringRef ModuleFilename,
290 DiagnosticsEngine *Diags,
291 bool AllowCompatibleDifferences = true) {
292 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
293 using CK = LangOptions::CompatibilityKind;
294
295#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
296 if constexpr (CK::Compatibility != CK::Benign) { \
297 if ((CK::Compatibility == CK::NotCompatible) || \
298 (CK::Compatibility == CK::Compatible && \
299 !AllowCompatibleDifferences)) { \
300 if (ExistingLangOpts.Name != LangOpts.Name) { \
301 if (Diags) { \
302 if (Bits == 1) \
303 Diags->Report(diag::err_ast_file_langopt_mismatch) \
304 << Description << LangOpts.Name << ExistingLangOpts.Name \
305 << ModuleFilename; \
306 else \
307 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
308 << Description << ModuleFilename; \
309 } \
310 return true; \
311 } \
312 } \
313 }
314
315#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
316 if constexpr (CK::Compatibility != CK::Benign) { \
317 if ((CK::Compatibility == CK::NotCompatible) || \
318 (CK::Compatibility == CK::Compatible && \
319 !AllowCompatibleDifferences)) { \
320 if (ExistingLangOpts.Name != LangOpts.Name) { \
321 if (Diags) \
322 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
323 << Description << ModuleFilename; \
324 return true; \
325 } \
326 } \
327 }
328
329#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
330 if constexpr (CK::Compatibility != CK::Benign) { \
331 if ((CK::Compatibility == CK::NotCompatible) || \
332 (CK::Compatibility == CK::Compatible && \
333 !AllowCompatibleDifferences)) { \
334 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
335 if (Diags) \
336 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
337 << Description << ModuleFilename; \
338 return true; \
339 } \
340 } \
341 }
342
343#include "clang/Basic/LangOptions.def"
344
345 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
346 if (Diags)
347 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
348 << "module features" << ModuleFilename;
349 return true;
350 }
351
352 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
353 if (Diags)
354 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
355 << "target Objective-C runtime" << ModuleFilename;
356 return true;
357 }
358
359 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
360 LangOpts.CommentOpts.BlockCommandNames) {
361 if (Diags)
362 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
363 << "block command names" << ModuleFilename;
364 return true;
365 }
366
367 // Sanitizer feature mismatches are treated as compatible differences. If
368 // compatible differences aren't allowed, we still only want to check for
369 // mismatches of non-modular sanitizers (the only ones which can affect AST
370 // generation).
371 if (!AllowCompatibleDifferences) {
372 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
373 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
374 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
375 ExistingSanitizers.clear(K: ModularSanitizers);
376 ImportedSanitizers.clear(K: ModularSanitizers);
377 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
378 const std::string Flag = "-fsanitize=";
379 if (Diags) {
380#define SANITIZER(NAME, ID) \
381 { \
382 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
383 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
384 if (InExistingModule != InImportedModule) \
385 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
386 << InExistingModule << ModuleFilename << (Flag + NAME); \
387 }
388#include "clang/Basic/Sanitizers.def"
389 }
390 return true;
391 }
392 }
393
394 return false;
395}
396
397static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
398 const CodeGenOptions &ExistingCGOpts,
399 StringRef ModuleFilename,
400 DiagnosticsEngine *Diags,
401 bool AllowCompatibleDifferences = true) {
402 // FIXME: Specify and print a description for each option instead of the name.
403 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
404 using CK = CodeGenOptions::CompatibilityKind;
405#define CODEGENOPT(Name, Bits, Default, Compatibility) \
406 if constexpr (CK::Compatibility != CK::Benign) { \
407 if ((CK::Compatibility == CK::NotCompatible) || \
408 (CK::Compatibility == CK::Compatible && \
409 !AllowCompatibleDifferences)) { \
410 if (ExistingCGOpts.Name != CGOpts.Name) { \
411 if (Diags) { \
412 if (Bits == 1) \
413 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
414 << #Name << CGOpts.Name << ExistingCGOpts.Name \
415 << ModuleFilename; \
416 else \
417 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
418 << #Name << ModuleFilename; \
419 } \
420 return true; \
421 } \
422 } \
423 }
424
425#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
426 if constexpr (CK::Compatibility != CK::Benign) { \
427 if ((CK::Compatibility == CK::NotCompatible) || \
428 (CK::Compatibility == CK::Compatible && \
429 !AllowCompatibleDifferences)) { \
430 if (ExistingCGOpts.Name != CGOpts.Name) { \
431 if (Diags) \
432 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
433 << #Name << ModuleFilename; \
434 return true; \
435 } \
436 } \
437 }
438#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
439 if constexpr (CK::Compatibility != CK::Benign) { \
440 if ((CK::Compatibility == CK::NotCompatible) || \
441 (CK::Compatibility == CK::Compatible && \
442 !AllowCompatibleDifferences)) { \
443 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
444 if (Diags) \
445 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
446 << #Name << ModuleFilename; \
447 return true; \
448 } \
449 } \
450 }
451#define DEBUGOPT(Name, Bits, Default, Compatibility)
452#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
453#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
454#include "clang/Basic/CodeGenOptions.def"
455
456 return false;
457}
458
459static std::vector<std::string>
460accumulateFeaturesAsWritten(std::vector<std::string> FeaturesAsWritten) {
461 llvm::erase_if(C&: FeaturesAsWritten, P: [](const std::string &S) {
462 return S.empty() || (S[0] != '+' && S[0] != '-');
463 });
464 llvm::stable_sort(Range&: FeaturesAsWritten,
465 C: [](const std::string &A, const std::string &B) {
466 return A.substr(pos: 1) < B.substr(pos: 1);
467 });
468 auto NewRend =
469 std::unique(first: FeaturesAsWritten.rbegin(), last: FeaturesAsWritten.rend(),
470 binary_pred: [](const std::string &A, const std::string &B) {
471 return A.substr(pos: 1) == B.substr(pos: 1);
472 });
473 // Because we are operating on reverse iterators, the duplicate elements
474 // are actually at the beginning.
475 FeaturesAsWritten.erase(first: FeaturesAsWritten.begin(), last: NewRend.base());
476 return FeaturesAsWritten;
477}
478
479/// Compare the given set of target options against an existing set of
480/// target options.
481///
482/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
483///
484/// \returns true if the target options mis-match, false otherwise.
485static bool checkTargetOptions(const TargetOptions &TargetOpts,
486 const TargetOptions &ExistingTargetOpts,
487 StringRef ModuleFilename,
488 DiagnosticsEngine *Diags,
489 bool AllowCompatibleDifferences = true) {
490#define CHECK_TARGET_OPT(Field, Name) \
491 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
492 if (Diags) \
493 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
494 << ModuleFilename << Name << TargetOpts.Field \
495 << ExistingTargetOpts.Field; \
496 return true; \
497 }
498
499 // The triple and ABI must match exactly.
500 CHECK_TARGET_OPT(Triple, "target");
501 CHECK_TARGET_OPT(ABI, "target ABI");
502
503 // We can tolerate different CPUs in many cases, notably when one CPU
504 // supports a strict superset of another. When allowing compatible
505 // differences skip this check.
506 if (!AllowCompatibleDifferences) {
507 CHECK_TARGET_OPT(CPU, "target CPU");
508 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
509 }
510
511#undef CHECK_TARGET_OPT
512
513 // Compare feature sets.
514 // Alternatively, we could be diffing TargetOpts.Features, but that would
515 // clutter the output with implied features.
516 std::vector<std::string> ExistingFeatures =
517 accumulateFeaturesAsWritten(FeaturesAsWritten: ExistingTargetOpts.FeaturesAsWritten);
518 std::vector<std::string> ReadFeatures =
519 accumulateFeaturesAsWritten(FeaturesAsWritten: TargetOpts.FeaturesAsWritten);
520
521 // We compute the set difference in both directions explicitly so that we can
522 // diagnose the differences differently.
523 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
524 std::set_difference(
525 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
526 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
527 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
528 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
529 result: std::back_inserter(x&: UnmatchedReadFeatures));
530
531 // If we are allowing compatible differences and the read feature set is
532 // a strict subset of the existing feature set, there is nothing to diagnose.
533 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
534 return false;
535
536 if (Diags) {
537 for (StringRef Feature : UnmatchedReadFeatures)
538 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
539 << /* is-existing-feature */ false << ModuleFilename << Feature;
540 for (StringRef Feature : UnmatchedExistingFeatures)
541 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
542 << /* is-existing-feature */ true << ModuleFilename << Feature;
543 }
544
545 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
546}
547
548bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
549 StringRef ModuleFilename, bool Complain,
550 bool AllowCompatibleDifferences) {
551 const LangOptions &ExistingLangOpts = PP.getLangOpts();
552 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
553 Diags: Complain ? &Reader.Diags : nullptr,
554 AllowCompatibleDifferences);
555}
556
557bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
558 StringRef ModuleFilename, bool Complain,
559 bool AllowCompatibleDifferences) {
560 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
561 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
562 Diags: Complain ? &Reader.Diags : nullptr,
563 AllowCompatibleDifferences);
564}
565
566bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
567 StringRef ModuleFilename, bool Complain,
568 bool AllowCompatibleDifferences) {
569 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
570 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
571 Diags: Complain ? &Reader.Diags : nullptr,
572 AllowCompatibleDifferences);
573}
574
575namespace {
576
577using MacroDefinitionsMap =
578 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
579
580class DeclsSet {
581 SmallVector<NamedDecl *, 64> Decls;
582 llvm::SmallPtrSet<NamedDecl *, 8> Found;
583
584public:
585 operator ArrayRef<NamedDecl *>() const { return Decls; }
586
587 bool empty() const { return Decls.empty(); }
588
589 bool insert(NamedDecl *ND) {
590 auto [_, Inserted] = Found.insert(Ptr: ND);
591 if (Inserted)
592 Decls.push_back(Elt: ND);
593 return Inserted;
594 }
595};
596
597using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
598
599} // namespace
600
601static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
602 DiagnosticsEngine &Diags,
603 StringRef ModuleFilename,
604 bool Complain) {
605 using Level = DiagnosticsEngine::Level;
606
607 // Check current mappings for new -Werror mappings, and the stored mappings
608 // for cases that were explicitly mapped to *not* be errors that are now
609 // errors because of options like -Werror.
610 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
611
612 for (DiagnosticsEngine *MappingSource : MappingSources) {
613 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
614 diag::kind DiagID = DiagIDMappingPair.first;
615 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
616 if (CurLevel < DiagnosticsEngine::Error)
617 continue; // not significant
618 Level StoredLevel =
619 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
620 if (StoredLevel < DiagnosticsEngine::Error) {
621 if (Complain)
622 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
623 << "-Werror=" + Diags.getDiagnosticIDs()
624 ->getWarningOptionForDiag(DiagID)
625 .str()
626 << ModuleFilename;
627 return true;
628 }
629 }
630 }
631
632 return false;
633}
634
635static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
636 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
637 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
638 return true;
639 return Ext >= diag::Severity::Error;
640}
641
642static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
643 DiagnosticsEngine &Diags,
644 StringRef ModuleFilename, bool IsSystem,
645 bool SystemHeaderWarningsInModule,
646 bool Complain) {
647 // Top-level options
648 if (IsSystem) {
649 if (Diags.getSuppressSystemWarnings())
650 return false;
651 // If -Wsystem-headers was not enabled before, and it was not explicit,
652 // be conservative
653 if (StoredDiags.getSuppressSystemWarnings() &&
654 !SystemHeaderWarningsInModule) {
655 if (Complain)
656 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
657 << "-Wsystem-headers" << ModuleFilename;
658 return true;
659 }
660 }
661
662 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
663 if (Complain)
664 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
665 << "-Werror" << ModuleFilename;
666 return true;
667 }
668
669 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
670 !StoredDiags.getEnableAllWarnings()) {
671 if (Complain)
672 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
673 << "-Weverything -Werror" << ModuleFilename;
674 return true;
675 }
676
677 if (isExtHandlingFromDiagsError(Diags) &&
678 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
679 if (Complain)
680 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
681 << "-pedantic-errors" << ModuleFilename;
682 return true;
683 }
684
685 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
686 Complain);
687}
688
689/// Return the top import module if it is implicit, nullptr otherwise.
690static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
691 Preprocessor &PP) {
692 // If the original import came from a file explicitly generated by the user,
693 // don't check the diagnostic mappings.
694 // FIXME: currently this is approximated by checking whether this is not a
695 // module import of an implicitly-loaded module file.
696 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
697 // the transitive closure of its imports, since unrelated modules cannot be
698 // imported until after this module finishes validation.
699 ModuleFile *TopImport = &*ModuleMgr.rbegin();
700 while (!TopImport->ImportedBy.empty())
701 TopImport = TopImport->ImportedBy[0];
702 if (TopImport->Kind != MK_ImplicitModule)
703 return nullptr;
704
705 StringRef ModuleName = TopImport->ModuleName;
706 assert(!ModuleName.empty() && "diagnostic options read before module name");
707
708 Module *M =
709 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
710 assert(M && "missing module");
711 return M;
712}
713
714bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
715 StringRef ModuleFilename,
716 bool Complain) {
717 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
718 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
719 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
720 // This should never fail, because we would have processed these options
721 // before writing them to an ASTFile.
722 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
723 VFS&: PP.getFileManager().getVirtualFileSystem(),
724 /*Report*/ ReportDiags: false);
725
726 ModuleManager &ModuleMgr = Reader.getModuleManager();
727 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
728
729 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
730 if (!TopM)
731 return false;
732
733 Module *Importer = PP.getCurrentModule();
734
735 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
736 bool SystemHeaderWarningsInModule =
737 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
738 Element: Importer->Name);
739
740 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
741 // contains the union of their flags.
742 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
743 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
744 Complain);
745}
746
747/// Collect the macro definitions provided by the given preprocessor
748/// options.
749static void
750collectMacroDefinitions(const PreprocessorOptions &PPOpts,
751 MacroDefinitionsMap &Macros,
752 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
753 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
754 StringRef Macro = PPOpts.Macros[I].first;
755 bool IsUndef = PPOpts.Macros[I].second;
756
757 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
758 StringRef MacroName = MacroPair.first;
759 StringRef MacroBody = MacroPair.second;
760
761 // For an #undef'd macro, we only care about the name.
762 if (IsUndef) {
763 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
764 if (MacroNames && Inserted)
765 MacroNames->push_back(Elt: MacroName);
766
767 It->second = std::make_pair(x: "", y: true);
768 continue;
769 }
770
771 // For a #define'd macro, figure out the actual definition.
772 if (MacroName.size() == Macro.size())
773 MacroBody = "1";
774 else {
775 // Note: GCC drops anything following an end-of-line character.
776 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
777 MacroBody = MacroBody.substr(Start: 0, N: End);
778 }
779
780 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
781 if (MacroNames && Inserted)
782 MacroNames->push_back(Elt: MacroName);
783 It->second = std::make_pair(x&: MacroBody, y: false);
784 }
785}
786
787enum OptionValidation {
788 OptionValidateNone,
789 OptionValidateContradictions,
790 OptionValidateStrictMatches,
791};
792
793/// Check the preprocessor options deserialized from the control block
794/// against the preprocessor options in an existing preprocessor.
795///
796/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
797/// \param Validation If set to OptionValidateNone, ignore differences in
798/// preprocessor options. If set to OptionValidateContradictions,
799/// require that options passed both in the AST file and on the command
800/// line (-D or -U) match, but tolerate options missing in one or the
801/// other. If set to OptionValidateContradictions, require that there
802/// are no differences in the options between the two.
803static bool checkPreprocessorOptions(
804 const PreprocessorOptions &PPOpts,
805 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
806 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
807 std::string &SuggestedPredefines, const LangOptions &LangOpts,
808 OptionValidation Validation = OptionValidateContradictions) {
809 if (ReadMacros) {
810 // Check macro definitions.
811 MacroDefinitionsMap ASTFileMacros;
812 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
813 MacroDefinitionsMap ExistingMacros;
814 SmallVector<StringRef, 4> ExistingMacroNames;
815 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
816 MacroNames: &ExistingMacroNames);
817
818 // Use a line marker to enter the <command line> file, as the defines and
819 // undefines here will have come from the command line.
820 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
821
822 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
823 // Dig out the macro definition in the existing preprocessor options.
824 StringRef MacroName = ExistingMacroNames[I];
825 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
826
827 // Check whether we know anything about this macro name or not.
828 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
829 ASTFileMacros.find(Key: MacroName);
830 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
831 if (Validation == OptionValidateStrictMatches) {
832 // If strict matches are requested, don't tolerate any extra defines
833 // on the command line that are missing in the AST file.
834 if (Diags) {
835 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
836 << MacroName << true << ModuleFilename;
837 }
838 return true;
839 }
840 // FIXME: Check whether this identifier was referenced anywhere in the
841 // AST file. If so, we should reject the AST file. Unfortunately, this
842 // information isn't in the control block. What shall we do about it?
843
844 if (Existing.second) {
845 SuggestedPredefines += "#undef ";
846 SuggestedPredefines += MacroName.str();
847 SuggestedPredefines += '\n';
848 } else {
849 SuggestedPredefines += "#define ";
850 SuggestedPredefines += MacroName.str();
851 SuggestedPredefines += ' ';
852 SuggestedPredefines += Existing.first.str();
853 SuggestedPredefines += '\n';
854 }
855 continue;
856 }
857
858 // If the macro was defined in one but undef'd in the other, we have a
859 // conflict.
860 if (Existing.second != Known->second.second) {
861 if (Diags) {
862 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
863 << MacroName << Known->second.second << ModuleFilename;
864 }
865 return true;
866 }
867
868 // If the macro was #undef'd in both, or if the macro bodies are
869 // identical, it's fine.
870 if (Existing.second || Existing.first == Known->second.first) {
871 ASTFileMacros.erase(I: Known);
872 continue;
873 }
874
875 // The macro bodies differ; complain.
876 if (Diags) {
877 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
878 << MacroName << Known->second.first << Existing.first
879 << ModuleFilename;
880 }
881 return true;
882 }
883
884 // Leave the <command line> file and return to <built-in>.
885 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
886
887 if (Validation == OptionValidateStrictMatches) {
888 // If strict matches are requested, don't tolerate any extra defines in
889 // the AST file that are missing on the command line.
890 for (const auto &MacroName : ASTFileMacros.keys()) {
891 if (Diags) {
892 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
893 << MacroName << false << ModuleFilename;
894 }
895 return true;
896 }
897 }
898 }
899
900 // Check whether we're using predefines.
901 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
902 Validation != OptionValidateNone) {
903 if (Diags) {
904 Diags->Report(DiagID: diag::err_ast_file_undef)
905 << ExistingPPOpts.UsePredefines << ModuleFilename;
906 }
907 return true;
908 }
909
910 // Detailed record is important since it is used for the module cache hash.
911 if (LangOpts.Modules &&
912 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
913 Validation != OptionValidateNone) {
914 if (Diags) {
915 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
916 << PPOpts.DetailedRecord << ModuleFilename;
917 }
918 return true;
919 }
920
921 // Compute the #include and #include_macros lines we need.
922 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
923 StringRef File = ExistingPPOpts.Includes[I];
924
925 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
926 !ExistingPPOpts.PCHThroughHeader.empty()) {
927 // In case the through header is an include, we must add all the includes
928 // to the predefines so the start point can be determined.
929 SuggestedPredefines += "#include \"";
930 SuggestedPredefines += File;
931 SuggestedPredefines += "\"\n";
932 continue;
933 }
934
935 if (File == ExistingPPOpts.ImplicitPCHInclude)
936 continue;
937
938 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
939 continue;
940
941 SuggestedPredefines += "#include \"";
942 SuggestedPredefines += File;
943 SuggestedPredefines += "\"\n";
944 }
945
946 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
947 StringRef File = ExistingPPOpts.MacroIncludes[I];
948 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
949 continue;
950
951 SuggestedPredefines += "#__include_macros \"";
952 SuggestedPredefines += File;
953 SuggestedPredefines += "\"\n##\n";
954 }
955
956 return false;
957}
958
959bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
960 StringRef ModuleFilename,
961 bool ReadMacros, bool Complain,
962 std::string &SuggestedPredefines) {
963 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
964
965 return checkPreprocessorOptions(
966 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
967 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
968 SuggestedPredefines, LangOpts: PP.getLangOpts());
969}
970
971bool SimpleASTReaderListener::ReadPreprocessorOptions(
972 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
973 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
974 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
975 ModuleFilename, ReadMacros, Diags: nullptr,
976 FileMgr&: PP.getFileManager(), SuggestedPredefines,
977 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
978}
979
980/// Check that the specified and the existing module cache paths are equivalent.
981///
982/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
983/// \returns true when the module cache paths differ.
984static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
985 StringRef ExistingSpecificModuleCachePath,
986 StringRef ASTFilename,
987 DiagnosticsEngine *Diags,
988 const LangOptions &LangOpts,
989 const PreprocessorOptions &PPOpts,
990 const HeaderSearchOptions &HSOpts,
991 const HeaderSearchOptions &ASTFileHSOpts) {
992 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
993 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
994 ContextHash: std::string(ContextHash));
995
996 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
997 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
998 return false;
999 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1000 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
1001 if (EqualOrErr && *EqualOrErr)
1002 return false;
1003 if (Diags) {
1004 // If the module cache arguments provided from the command line are the
1005 // same, the mismatch must come from other arguments of the configuration
1006 // and not directly the cache path.
1007 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
1008 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
1009 if (EqualOrErr && *EqualOrErr)
1010 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
1011 else
1012 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
1013 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
1014 << ASTFilename;
1015 }
1016 return true;
1017}
1018
1019bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1020 StringRef ASTFilename,
1021 StringRef ContextHash,
1022 bool Complain) {
1023 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1024 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1025 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1026 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1027 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1028 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1029}
1030
1031void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1032 PP.setCounterValue(Value);
1033}
1034
1035//===----------------------------------------------------------------------===//
1036// AST reader implementation
1037//===----------------------------------------------------------------------===//
1038
1039static uint64_t readULEB(const unsigned char *&P) {
1040 unsigned Length = 0;
1041 const char *Error = nullptr;
1042
1043 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1044 if (Error)
1045 llvm::report_fatal_error(reason: Error);
1046 P += Length;
1047 return Val;
1048}
1049
1050/// Read ULEB-encoded key length and data length.
1051static std::pair<unsigned, unsigned>
1052readULEBKeyDataLength(const unsigned char *&P) {
1053 unsigned KeyLen = readULEB(P);
1054 if ((unsigned)KeyLen != KeyLen)
1055 llvm::report_fatal_error(reason: "key too large");
1056
1057 unsigned DataLen = readULEB(P);
1058 if ((unsigned)DataLen != DataLen)
1059 llvm::report_fatal_error(reason: "data too large");
1060
1061 return std::make_pair(x&: KeyLen, y&: DataLen);
1062}
1063
1064void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1065 bool TakeOwnership) {
1066 DeserializationListener = Listener;
1067 OwnsDeserializationListener = TakeOwnership;
1068}
1069
1070unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1071 return serialization::ComputeHash(Sel);
1072}
1073
1074LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1075 LocalDeclID ID(Value);
1076#ifndef NDEBUG
1077 if (!MF.ModuleOffsetMap.empty())
1078 Reader.ReadModuleOffsetMap(MF);
1079
1080 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1081 unsigned LocalDeclID = ID.getLocalDeclIndex();
1082
1083 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1084
1085 ModuleFile *OwningModuleFile =
1086 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1087 assert(OwningModuleFile);
1088
1089 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1090
1091 if (!ModuleFileIndex)
1092 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1093
1094 assert(LocalDeclID < LocalNumDecls);
1095#endif
1096 (void)Reader;
1097 (void)MF;
1098 return ID;
1099}
1100
1101LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1102 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1103 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1104 return LocalDeclID::get(Reader, MF, Value);
1105}
1106
1107std::pair<unsigned, unsigned>
1108ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1109 return readULEBKeyDataLength(P&: d);
1110}
1111
1112ASTSelectorLookupTrait::internal_key_type
1113ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1114 using namespace llvm::support;
1115
1116 SelectorTable &SelTable = Reader.getContext().Selectors;
1117 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1118 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1119 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1120 if (N == 0)
1121 return SelTable.getNullarySelector(ID: FirstII);
1122 else if (N == 1)
1123 return SelTable.getUnarySelector(ID: FirstII);
1124
1125 SmallVector<const IdentifierInfo *, 16> Args;
1126 Args.push_back(Elt: FirstII);
1127 for (unsigned I = 1; I != N; ++I)
1128 Args.push_back(Elt: Reader.getLocalIdentifier(
1129 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1130
1131 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1132}
1133
1134ASTSelectorLookupTrait::data_type
1135ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1136 unsigned DataLen) {
1137 using namespace llvm::support;
1138
1139 data_type Result;
1140
1141 Result.ID = Reader.getGlobalSelectorID(
1142 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1143 unsigned FullInstanceBits =
1144 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1145 unsigned FullFactoryBits =
1146 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1147 Result.InstanceBits = FullInstanceBits & 0x3;
1148 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1149 Result.FactoryBits = FullFactoryBits & 0x3;
1150 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1151 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1152 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1153
1154 // Load instance methods
1155 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1156 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1157 F, LocalID: LocalDeclID::get(
1158 Reader, MF&: F,
1159 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1160 Result.Instance.push_back(Elt: Method);
1161 }
1162
1163 // Load factory methods
1164 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1165 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1166 F, LocalID: LocalDeclID::get(
1167 Reader, MF&: F,
1168 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1169 Result.Factory.push_back(Elt: Method);
1170 }
1171
1172 return Result;
1173}
1174
1175unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1176 return llvm::djbHash(Buffer: a);
1177}
1178
1179std::pair<unsigned, unsigned>
1180ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1181 return readULEBKeyDataLength(P&: d);
1182}
1183
1184ASTIdentifierLookupTraitBase::internal_key_type
1185ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1186 assert(n >= 2 && d[n-1] == '\0');
1187 return StringRef((const char*) d, n-1);
1188}
1189
1190/// Whether the given identifier is "interesting".
1191static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1192 bool IsModule) {
1193 bool IsInteresting =
1194 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1195 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1196 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1197 return II.hadMacroDefinition() || II.isPoisoned() ||
1198 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1199 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1200 II.getFETokenInfo());
1201}
1202
1203static bool readBit(unsigned &Bits) {
1204 bool Value = Bits & 0x1;
1205 Bits >>= 1;
1206 return Value;
1207}
1208
1209IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1210 using namespace llvm::support;
1211
1212 IdentifierID RawID =
1213 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1214 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1215}
1216
1217static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1218 bool IsModule) {
1219 if (!II.isFromAST()) {
1220 II.setIsFromAST();
1221 if (isInterestingIdentifier(Reader, II, IsModule))
1222 II.setChangedSinceDeserialization();
1223 }
1224}
1225
1226IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1227 const unsigned char* d,
1228 unsigned DataLen) {
1229 using namespace llvm::support;
1230
1231 IdentifierID RawID =
1232 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1233 bool IsInteresting = RawID & 0x01;
1234
1235 DataLen -= sizeof(IdentifierID);
1236
1237 // Wipe out the "is interesting" bit.
1238 RawID = RawID >> 1;
1239
1240 // Build the IdentifierInfo and link the identifier ID with it.
1241 IdentifierInfo *II = KnownII;
1242 if (!II) {
1243 II = &Reader.getIdentifierTable().getOwn(Name: k);
1244 KnownII = II;
1245 }
1246 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1247 markIdentifierFromAST(Reader, II&: *II, IsModule);
1248 Reader.markIdentifierUpToDate(II);
1249
1250 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1251 if (!IsInteresting) {
1252 // For uninteresting identifiers, there's nothing else to do. Just notify
1253 // the reader that we've finished loading this identifier.
1254 Reader.SetIdentifierInfo(ID, II);
1255 return II;
1256 }
1257
1258 unsigned ObjCOrBuiltinID =
1259 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1260 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1261 bool CPlusPlusOperatorKeyword = readBit(Bits);
1262 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1263 bool Poisoned = readBit(Bits);
1264 bool ExtensionToken = readBit(Bits);
1265 bool HasMacroDefinition = readBit(Bits);
1266
1267 assert(Bits == 0 && "Extra bits in the identifier?");
1268 DataLen -= sizeof(uint16_t) * 2;
1269
1270 // Set or check the various bits in the IdentifierInfo structure.
1271 // Token IDs are read-only.
1272 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1273 II->revertTokenIDToIdentifier();
1274 if (!F.isModule())
1275 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1276 assert(II->isExtensionToken() == ExtensionToken &&
1277 "Incorrect extension token flag");
1278 (void)ExtensionToken;
1279 if (Poisoned)
1280 II->setIsPoisoned(true);
1281 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1282 "Incorrect C++ operator keyword flag");
1283 (void)CPlusPlusOperatorKeyword;
1284
1285 // If this identifier has a macro definition, deserialize it or notify the
1286 // visitor the actual definition is in a different module.
1287 if (HasMacroDefinition) {
1288 uint32_t MacroDirectivesOffset =
1289 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1290 DataLen -= 4;
1291
1292 if (MacroDirectivesOffset)
1293 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1294 else
1295 hasMacroDefinitionInDependencies = true;
1296 }
1297
1298 Reader.SetIdentifierInfo(ID, II);
1299
1300 // Read all of the declarations visible at global scope with this
1301 // name.
1302 if (DataLen > 0) {
1303 SmallVector<GlobalDeclID, 4> DeclIDs;
1304 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1305 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1306 F, LocalID: LocalDeclID::get(
1307 Reader, MF&: F,
1308 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1309 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1310 }
1311
1312 return II;
1313}
1314
1315DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1316 : Kind(Name.getNameKind()) {
1317 switch (Kind) {
1318 case DeclarationName::Identifier:
1319 Data = (uint64_t)Name.getAsIdentifierInfo();
1320 break;
1321 case DeclarationName::ObjCZeroArgSelector:
1322 case DeclarationName::ObjCOneArgSelector:
1323 case DeclarationName::ObjCMultiArgSelector:
1324 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1325 break;
1326 case DeclarationName::CXXOperatorName:
1327 Data = Name.getCXXOverloadedOperator();
1328 break;
1329 case DeclarationName::CXXLiteralOperatorName:
1330 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1331 break;
1332 case DeclarationName::CXXDeductionGuideName:
1333 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1334 ->getDeclName().getAsIdentifierInfo();
1335 break;
1336 case DeclarationName::CXXConstructorName:
1337 case DeclarationName::CXXDestructorName:
1338 case DeclarationName::CXXConversionFunctionName:
1339 case DeclarationName::CXXUsingDirective:
1340 Data = 0;
1341 break;
1342 }
1343}
1344
1345unsigned DeclarationNameKey::getHash() const {
1346 llvm::FoldingSetNodeID ID;
1347 ID.AddInteger(I: Kind);
1348
1349 switch (Kind) {
1350 case DeclarationName::Identifier:
1351 case DeclarationName::CXXLiteralOperatorName:
1352 case DeclarationName::CXXDeductionGuideName:
1353 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1354 break;
1355 case DeclarationName::ObjCZeroArgSelector:
1356 case DeclarationName::ObjCOneArgSelector:
1357 case DeclarationName::ObjCMultiArgSelector:
1358 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1359 break;
1360 case DeclarationName::CXXOperatorName:
1361 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1362 break;
1363 case DeclarationName::CXXConstructorName:
1364 case DeclarationName::CXXDestructorName:
1365 case DeclarationName::CXXConversionFunctionName:
1366 case DeclarationName::CXXUsingDirective:
1367 break;
1368 }
1369
1370 return ID.computeStableHash();
1371}
1372
1373ModuleFile *
1374ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1375 using namespace llvm::support;
1376
1377 uint32_t ModuleFileID =
1378 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1379 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1380}
1381
1382std::pair<unsigned, unsigned>
1383ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1384 return readULEBKeyDataLength(P&: d);
1385}
1386
1387DeclarationNameKey
1388ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1389 using namespace llvm::support;
1390
1391 auto Kind = (DeclarationName::NameKind)*d++;
1392 uint64_t Data;
1393 switch (Kind) {
1394 case DeclarationName::Identifier:
1395 case DeclarationName::CXXLiteralOperatorName:
1396 case DeclarationName::CXXDeductionGuideName:
1397 Data = (uint64_t)Reader.getLocalIdentifier(
1398 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1399 break;
1400 case DeclarationName::ObjCZeroArgSelector:
1401 case DeclarationName::ObjCOneArgSelector:
1402 case DeclarationName::ObjCMultiArgSelector:
1403 Data = (uint64_t)Reader
1404 .getLocalSelector(
1405 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1406 .getAsOpaquePtr();
1407 break;
1408 case DeclarationName::CXXOperatorName:
1409 Data = *d++; // OverloadedOperatorKind
1410 break;
1411 case DeclarationName::CXXConstructorName:
1412 case DeclarationName::CXXDestructorName:
1413 case DeclarationName::CXXConversionFunctionName:
1414 case DeclarationName::CXXUsingDirective:
1415 Data = 0;
1416 break;
1417 }
1418
1419 return DeclarationNameKey(Kind, Data);
1420}
1421
1422ASTDeclContextNameLookupTrait::internal_key_type
1423ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1424 return ReadKeyBase(d);
1425}
1426
1427void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1428 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1429 using namespace llvm::support;
1430
1431 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1432 LocalDeclID ID = LocalDeclID::get(
1433 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1434 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1435 }
1436}
1437
1438void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1439 const unsigned char *d,
1440 unsigned DataLen,
1441 data_type_builder &Val) {
1442 ReadDataIntoImpl(d, DataLen, Val);
1443}
1444
1445ModuleLocalNameLookupTrait::hash_value_type
1446ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1447 llvm::FoldingSetNodeID ID;
1448 ID.AddInteger(I: Key.first.getHash());
1449 ID.AddInteger(I: Key.second);
1450 return ID.computeStableHash();
1451}
1452
1453ModuleLocalNameLookupTrait::internal_key_type
1454ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1455 DeclarationNameKey Name(Key.first);
1456
1457 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1458 if (!ModuleHash)
1459 return {Name, 0};
1460
1461 return {Name, *ModuleHash};
1462}
1463
1464ModuleLocalNameLookupTrait::internal_key_type
1465ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1466 DeclarationNameKey Name = ReadKeyBase(d);
1467 unsigned PrimaryModuleHash =
1468 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1469 return {Name, PrimaryModuleHash};
1470}
1471
1472void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1473 const unsigned char *d,
1474 unsigned DataLen,
1475 data_type_builder &Val) {
1476 ReadDataIntoImpl(d, DataLen, Val);
1477}
1478
1479ModuleFile *
1480LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1481 using namespace llvm::support;
1482
1483 uint32_t ModuleFileID =
1484 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1485 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1486}
1487
1488LazySpecializationInfoLookupTrait::internal_key_type
1489LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1490 using namespace llvm::support;
1491 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1492}
1493
1494std::pair<unsigned, unsigned>
1495LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1496 return readULEBKeyDataLength(P&: d);
1497}
1498
1499void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1500 const unsigned char *d,
1501 unsigned DataLen,
1502 data_type_builder &Val) {
1503 using namespace llvm::support;
1504
1505 for (unsigned NumDecls =
1506 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1507 NumDecls; --NumDecls) {
1508 LocalDeclID LocalID = LocalDeclID::get(
1509 Reader, MF&: F,
1510 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1511 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1512 }
1513}
1514
1515bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1516 BitstreamCursor &Cursor,
1517 uint64_t Offset,
1518 DeclContext *DC) {
1519 assert(Offset != 0);
1520
1521 SavedStreamPosition SavedPosition(Cursor);
1522 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1523 Error(Err: std::move(Err));
1524 return true;
1525 }
1526
1527 RecordData Record;
1528 StringRef Blob;
1529 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1530 if (!MaybeCode) {
1531 Error(Err: MaybeCode.takeError());
1532 return true;
1533 }
1534 unsigned Code = MaybeCode.get();
1535
1536 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1537 if (!MaybeRecCode) {
1538 Error(Err: MaybeRecCode.takeError());
1539 return true;
1540 }
1541 unsigned RecCode = MaybeRecCode.get();
1542 if (RecCode != DECL_CONTEXT_LEXICAL) {
1543 Error(Msg: "Expected lexical block");
1544 return true;
1545 }
1546
1547 assert(!isa<TranslationUnitDecl>(DC) &&
1548 "expected a TU_UPDATE_LEXICAL record for TU");
1549 // If we are handling a C++ class template instantiation, we can see multiple
1550 // lexical updates for the same record. It's important that we select only one
1551 // of them, so that field numbering works properly. Just pick the first one we
1552 // see.
1553 auto &Lex = LexicalDecls[DC];
1554 if (!Lex.first) {
1555 Lex = std::make_pair(
1556 x: &M, y: llvm::ArrayRef(
1557 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1558 Blob.size() / sizeof(DeclID)));
1559 }
1560 DC->setHasExternalLexicalStorage(true);
1561 return false;
1562}
1563
1564bool ASTReader::ReadVisibleDeclContextStorage(
1565 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1566 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1567 assert(Offset != 0);
1568
1569 SavedStreamPosition SavedPosition(Cursor);
1570 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1571 Error(Err: std::move(Err));
1572 return true;
1573 }
1574
1575 RecordData Record;
1576 StringRef Blob;
1577 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1578 if (!MaybeCode) {
1579 Error(Err: MaybeCode.takeError());
1580 return true;
1581 }
1582 unsigned Code = MaybeCode.get();
1583
1584 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1585 if (!MaybeRecCode) {
1586 Error(Err: MaybeRecCode.takeError());
1587 return true;
1588 }
1589 unsigned RecCode = MaybeRecCode.get();
1590 switch (VisibleKind) {
1591 case VisibleDeclContextStorageKind::GenerallyVisible:
1592 if (RecCode != DECL_CONTEXT_VISIBLE) {
1593 Error(Msg: "Expected visible lookup table block");
1594 return true;
1595 }
1596 break;
1597 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1598 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1599 Error(Msg: "Expected module local visible lookup table block");
1600 return true;
1601 }
1602 break;
1603 case VisibleDeclContextStorageKind::TULocalVisible:
1604 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1605 Error(Msg: "Expected TU local lookup table block");
1606 return true;
1607 }
1608 break;
1609 }
1610
1611 // We can't safely determine the primary context yet, so delay attaching the
1612 // lookup table until we're done with recursive deserialization.
1613 auto *Data = (const unsigned char*)Blob.data();
1614 switch (VisibleKind) {
1615 case VisibleDeclContextStorageKind::GenerallyVisible:
1616 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1617 break;
1618 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1619 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1620 break;
1621 case VisibleDeclContextStorageKind::TULocalVisible:
1622 if (M.Kind == MK_MainFile)
1623 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1624 break;
1625 }
1626 return false;
1627}
1628
1629void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1630 ModuleFile &M, bool IsPartial) {
1631 D = D->getCanonicalDecl();
1632 auto &SpecLookups =
1633 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1634 SpecLookups[D].Table.add(File: &M, Data,
1635 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1636}
1637
1638bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1639 uint64_t Offset, Decl *D, bool IsPartial) {
1640 assert(Offset != 0);
1641
1642 SavedStreamPosition SavedPosition(Cursor);
1643 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1644 Error(Err: std::move(Err));
1645 return true;
1646 }
1647
1648 RecordData Record;
1649 StringRef Blob;
1650 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1651 if (!MaybeCode) {
1652 Error(Err: MaybeCode.takeError());
1653 return true;
1654 }
1655 unsigned Code = MaybeCode.get();
1656
1657 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1658 if (!MaybeRecCode) {
1659 Error(Err: MaybeRecCode.takeError());
1660 return true;
1661 }
1662 unsigned RecCode = MaybeRecCode.get();
1663 if (RecCode != DECL_SPECIALIZATIONS &&
1664 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1665 Error(Msg: "Expected decl specs block");
1666 return true;
1667 }
1668
1669 auto *Data = (const unsigned char *)Blob.data();
1670 AddSpecializations(D, Data, M, IsPartial);
1671 return false;
1672}
1673
1674void ASTReader::Error(StringRef Msg) const {
1675 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1676 if (PP.getLangOpts().Modules &&
1677 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1678 Diag(DiagID: diag::note_module_cache_path)
1679 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1680 }
1681}
1682
1683void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1684 StringRef Arg3) const {
1685 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1686}
1687
1688namespace {
1689struct AlreadyReportedDiagnosticError
1690 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1691 static char ID;
1692
1693 void log(raw_ostream &OS) const override {
1694 llvm_unreachable("reporting an already-reported diagnostic error");
1695 }
1696
1697 std::error_code convertToErrorCode() const override {
1698 return llvm::inconvertibleErrorCode();
1699 }
1700};
1701
1702char AlreadyReportedDiagnosticError::ID = 0;
1703} // namespace
1704
1705void ASTReader::Error(llvm::Error &&Err) const {
1706 handleAllErrors(
1707 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1708 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1709}
1710
1711//===----------------------------------------------------------------------===//
1712// Source Manager Deserialization
1713//===----------------------------------------------------------------------===//
1714
1715/// Read the line table in the source manager block.
1716void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1717 unsigned Idx = 0;
1718 LineTableInfo &LineTable = SourceMgr.getLineTable();
1719
1720 // Parse the file names
1721 std::map<int, int> FileIDs;
1722 FileIDs[-1] = -1; // For unspecified filenames.
1723 for (unsigned I = 0; Record[Idx]; ++I) {
1724 // Extract the file name
1725 auto Filename = ReadPath(F, Record, Idx);
1726 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1727 }
1728 ++Idx;
1729
1730 // Parse the line entries
1731 std::vector<LineEntry> Entries;
1732 while (Idx < Record.size()) {
1733 FileID FID = ReadFileID(F, Record, Idx);
1734
1735 // Extract the line entries
1736 unsigned NumEntries = Record[Idx++];
1737 assert(NumEntries && "no line entries for file ID");
1738 Entries.clear();
1739 Entries.reserve(n: NumEntries);
1740 for (unsigned I = 0; I != NumEntries; ++I) {
1741 unsigned FileOffset = Record[Idx++];
1742 unsigned LineNo = Record[Idx++];
1743 int FilenameID = FileIDs[Record[Idx++]];
1744 SrcMgr::CharacteristicKind FileKind
1745 = (SrcMgr::CharacteristicKind)Record[Idx++];
1746 unsigned IncludeOffset = Record[Idx++];
1747 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1748 FileKind, IncludeOffset));
1749 }
1750 LineTable.AddEntry(FID, Entries);
1751 }
1752}
1753
1754/// Read a source manager block
1755llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1756 using namespace SrcMgr;
1757
1758 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1759
1760 // Set the source-location entry cursor to the current position in
1761 // the stream. This cursor will be used to read the contents of the
1762 // source manager block initially, and then lazily read
1763 // source-location entries as needed.
1764 SLocEntryCursor = F.Stream;
1765
1766 // The stream itself is going to skip over the source manager block.
1767 if (llvm::Error Err = F.Stream.SkipBlock())
1768 return Err;
1769
1770 // Enter the source manager block.
1771 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1772 return Err;
1773 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1774
1775 RecordData Record;
1776 while (true) {
1777 Expected<llvm::BitstreamEntry> MaybeE =
1778 SLocEntryCursor.advanceSkippingSubblocks();
1779 if (!MaybeE)
1780 return MaybeE.takeError();
1781 llvm::BitstreamEntry E = MaybeE.get();
1782
1783 switch (E.Kind) {
1784 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1785 case llvm::BitstreamEntry::Error:
1786 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1787 Fmt: "malformed block record in AST file");
1788 case llvm::BitstreamEntry::EndBlock:
1789 return llvm::Error::success();
1790 case llvm::BitstreamEntry::Record:
1791 // The interesting case.
1792 break;
1793 }
1794
1795 // Read a record.
1796 Record.clear();
1797 StringRef Blob;
1798 Expected<unsigned> MaybeRecord =
1799 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1800 if (!MaybeRecord)
1801 return MaybeRecord.takeError();
1802 switch (MaybeRecord.get()) {
1803 default: // Default behavior: ignore.
1804 break;
1805
1806 case SM_SLOC_FILE_ENTRY:
1807 case SM_SLOC_BUFFER_ENTRY:
1808 case SM_SLOC_EXPANSION_ENTRY:
1809 // Once we hit one of the source location entries, we're done.
1810 return llvm::Error::success();
1811 }
1812 }
1813}
1814
1815llvm::Expected<SourceLocation::UIntTy>
1816ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1817 BitstreamCursor &Cursor = F->SLocEntryCursor;
1818 SavedStreamPosition SavedPosition(Cursor);
1819 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1820 F->SLocEntryOffsets[Index]))
1821 return std::move(Err);
1822
1823 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1824 if (!MaybeEntry)
1825 return MaybeEntry.takeError();
1826
1827 llvm::BitstreamEntry Entry = MaybeEntry.get();
1828 if (Entry.Kind != llvm::BitstreamEntry::Record)
1829 return llvm::createStringError(
1830 EC: std::errc::illegal_byte_sequence,
1831 Fmt: "incorrectly-formatted source location entry in AST file");
1832
1833 RecordData Record;
1834 StringRef Blob;
1835 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1836 if (!MaybeSLOC)
1837 return MaybeSLOC.takeError();
1838
1839 switch (MaybeSLOC.get()) {
1840 default:
1841 return llvm::createStringError(
1842 EC: std::errc::illegal_byte_sequence,
1843 Fmt: "incorrectly-formatted source location entry in AST file");
1844 case SM_SLOC_FILE_ENTRY:
1845 case SM_SLOC_BUFFER_ENTRY:
1846 case SM_SLOC_EXPANSION_ENTRY:
1847 return F->SLocEntryBaseOffset + Record[0];
1848 }
1849}
1850
1851int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1852 auto SLocMapI =
1853 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1854 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1855 "Corrupted global sloc offset map");
1856 ModuleFile *F = SLocMapI->second;
1857
1858 bool Invalid = false;
1859
1860 auto It = llvm::upper_bound(
1861 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1862 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1863 int ID = F->SLocEntryBaseID + LocalIndex;
1864 std::size_t Index = -ID - 2;
1865 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1866 assert(!SourceMgr.SLocEntryLoaded[Index]);
1867 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1868 if (!MaybeEntryOffset) {
1869 Error(Err: MaybeEntryOffset.takeError());
1870 Invalid = true;
1871 return true;
1872 }
1873 SourceMgr.LoadedSLocEntryTable[Index] =
1874 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1875 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1876 }
1877 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1878 });
1879
1880 if (Invalid)
1881 return 0;
1882
1883 // The iterator points to the first entry with start offset greater than the
1884 // offset of interest. The previous entry must contain the offset of interest.
1885 return F->SLocEntryBaseID + *std::prev(x: It);
1886}
1887
1888bool ASTReader::ReadSLocEntry(int ID) {
1889 if (ID == 0)
1890 return false;
1891
1892 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1893 Error(Msg: "source location entry ID out-of-range for AST file");
1894 return true;
1895 }
1896
1897 // Local helper to read the (possibly-compressed) buffer data following the
1898 // entry record.
1899 auto ReadBuffer = [this](
1900 BitstreamCursor &SLocEntryCursor,
1901 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1902 RecordData Record;
1903 StringRef Blob;
1904 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1905 if (!MaybeCode) {
1906 Error(Err: MaybeCode.takeError());
1907 return nullptr;
1908 }
1909 unsigned Code = MaybeCode.get();
1910
1911 Expected<unsigned> MaybeRecCode =
1912 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1913 if (!MaybeRecCode) {
1914 Error(Err: MaybeRecCode.takeError());
1915 return nullptr;
1916 }
1917 unsigned RecCode = MaybeRecCode.get();
1918
1919 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1920 // Inspect the first byte to differentiate zlib (\x78) and zstd
1921 // (little-endian 0xFD2FB528).
1922 const llvm::compression::Format F =
1923 Blob.size() > 0 && Blob.data()[0] == 0x78
1924 ? llvm::compression::Format::Zlib
1925 : llvm::compression::Format::Zstd;
1926 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1927 Error(Msg: Reason);
1928 return nullptr;
1929 }
1930 SmallVector<uint8_t, 0> Decompressed;
1931 if (llvm::Error E = llvm::compression::decompress(
1932 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1933 Error(Msg: "could not decompress embedded file contents: " +
1934 llvm::toString(E: std::move(E)));
1935 return nullptr;
1936 }
1937 return llvm::MemoryBuffer::getMemBufferCopy(
1938 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1939 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1940 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1941 } else {
1942 Error(Msg: "AST record has invalid code");
1943 return nullptr;
1944 }
1945 };
1946
1947 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1948 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1949 BitNo: F->SLocEntryOffsetsBase +
1950 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1951 Error(Err: std::move(Err));
1952 return true;
1953 }
1954
1955 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1956 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1957
1958 ++NumSLocEntriesRead;
1959 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1960 if (!MaybeEntry) {
1961 Error(Err: MaybeEntry.takeError());
1962 return true;
1963 }
1964 llvm::BitstreamEntry Entry = MaybeEntry.get();
1965
1966 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1967 Error(Msg: "incorrectly-formatted source location entry in AST file");
1968 return true;
1969 }
1970
1971 RecordData Record;
1972 StringRef Blob;
1973 Expected<unsigned> MaybeSLOC =
1974 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1975 if (!MaybeSLOC) {
1976 Error(Err: MaybeSLOC.takeError());
1977 return true;
1978 }
1979 switch (MaybeSLOC.get()) {
1980 default:
1981 Error(Msg: "incorrectly-formatted source location entry in AST file");
1982 return true;
1983
1984 case SM_SLOC_FILE_ENTRY: {
1985 // We will detect whether a file changed and return 'Failure' for it, but
1986 // we will also try to fail gracefully by setting up the SLocEntry.
1987 unsigned InputID = Record[4];
1988 InputFile IF = getInputFile(F&: *F, ID: InputID);
1989 OptionalFileEntryRef File = IF.getFile();
1990 bool OverriddenBuffer = IF.isOverridden();
1991
1992 // Note that we only check if a File was returned. If it was out-of-date
1993 // we have complained but we will continue creating a FileID to recover
1994 // gracefully.
1995 if (!File)
1996 return true;
1997
1998 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1999 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
2000 // This is the module's main file.
2001 IncludeLoc = getImportLocation(F);
2002 }
2003 SrcMgr::CharacteristicKind
2004 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2005 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
2006 LoadedOffset: BaseOffset + Record[0]);
2007 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2008 FileInfo.NumCreatedFIDs = Record[5];
2009 if (Record[3])
2010 FileInfo.setHasLineDirectives();
2011
2012 unsigned NumFileDecls = Record[7];
2013 if (NumFileDecls && ContextObj) {
2014 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
2015 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
2016 FileDeclIDs[FID] =
2017 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
2018 }
2019
2020 const SrcMgr::ContentCache &ContentCache =
2021 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2022 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2023 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2024 !ContentCache.getBufferIfLoaded()) {
2025 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2026 if (!Buffer)
2027 return true;
2028 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2029 }
2030
2031 break;
2032 }
2033
2034 case SM_SLOC_BUFFER_ENTRY: {
2035 const char *Name = Blob.data();
2036 unsigned Offset = Record[0];
2037 SrcMgr::CharacteristicKind
2038 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2039 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2040 if (IncludeLoc.isInvalid() && F->isModule()) {
2041 IncludeLoc = getImportLocation(F);
2042 }
2043
2044 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2045 if (!Buffer)
2046 return true;
2047 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2048 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2049 if (Record[3]) {
2050 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2051 FileInfo.setHasLineDirectives();
2052 }
2053 break;
2054 }
2055
2056 case SM_SLOC_EXPANSION_ENTRY: {
2057 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2058 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2059 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2060 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2061 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2062 LoadedOffset: BaseOffset + Record[0]);
2063 break;
2064 }
2065 }
2066
2067 return false;
2068}
2069
2070std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2071 if (ID == 0)
2072 return std::make_pair(x: SourceLocation(), y: "");
2073
2074 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2075 Error(Msg: "source location entry ID out-of-range for AST file");
2076 return std::make_pair(x: SourceLocation(), y: "");
2077 }
2078
2079 // Find which module file this entry lands in.
2080 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2081 if (!M->isModule())
2082 return std::make_pair(x: SourceLocation(), y: "");
2083
2084 // FIXME: Can we map this down to a particular submodule? That would be
2085 // ideal.
2086 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2087}
2088
2089/// Find the location where the module F is imported.
2090SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2091 if (F->ImportLoc.isValid())
2092 return F->ImportLoc;
2093
2094 // Otherwise we have a PCH. It's considered to be "imported" at the first
2095 // location of its includer.
2096 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2097 // Main file is the importer.
2098 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2099 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2100 }
2101 return F->ImportedBy[0]->FirstLoc;
2102}
2103
2104/// Enter a subblock of the specified BlockID with the specified cursor. Read
2105/// the abbreviations that are at the top of the block and then leave the cursor
2106/// pointing into the block.
2107llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2108 unsigned BlockID,
2109 uint64_t *StartOfBlockOffset) {
2110 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2111 return Err;
2112
2113 if (StartOfBlockOffset)
2114 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2115
2116 while (true) {
2117 uint64_t Offset = Cursor.GetCurrentBitNo();
2118 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2119 if (!MaybeCode)
2120 return MaybeCode.takeError();
2121 unsigned Code = MaybeCode.get();
2122
2123 // We expect all abbrevs to be at the start of the block.
2124 if (Code != llvm::bitc::DEFINE_ABBREV) {
2125 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2126 return Err;
2127 return llvm::Error::success();
2128 }
2129 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2130 return Err;
2131 }
2132}
2133
2134Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2135 unsigned &Idx) {
2136 Token Tok;
2137 Tok.startToken();
2138 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2139 Tok.setKind((tok::TokenKind)Record[Idx++]);
2140 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2141
2142 if (Tok.isAnnotation()) {
2143 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2144 switch (Tok.getKind()) {
2145 case tok::annot_pragma_loop_hint: {
2146 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2147 Info->PragmaName = ReadToken(M, Record, Idx);
2148 Info->Option = ReadToken(M, Record, Idx);
2149 unsigned NumTokens = Record[Idx++];
2150 SmallVector<Token, 4> Toks;
2151 Toks.reserve(N: NumTokens);
2152 for (unsigned I = 0; I < NumTokens; ++I)
2153 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2154 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2155 Tok.setAnnotationValue(static_cast<void *>(Info));
2156 break;
2157 }
2158 case tok::annot_pragma_pack: {
2159 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2160 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2161 auto SlotLabel = ReadString(Record, Idx);
2162 Info->SlotLabel =
2163 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2164 Info->Alignment = ReadToken(M, Record, Idx);
2165 Tok.setAnnotationValue(static_cast<void *>(Info));
2166 break;
2167 }
2168 // Some annotation tokens do not use the PtrData field.
2169 case tok::annot_pragma_openmp:
2170 case tok::annot_pragma_openmp_end:
2171 case tok::annot_pragma_unused:
2172 case tok::annot_pragma_openacc:
2173 case tok::annot_pragma_openacc_end:
2174 case tok::annot_repl_input_end:
2175 break;
2176 default:
2177 llvm_unreachable("missing deserialization code for annotation token");
2178 }
2179 } else {
2180 Tok.setLength(Record[Idx++]);
2181 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2182 Tok.setIdentifierInfo(II);
2183 }
2184 return Tok;
2185}
2186
2187MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2188 BitstreamCursor &Stream = F.MacroCursor;
2189
2190 // Keep track of where we are in the stream, then jump back there
2191 // after reading this macro.
2192 SavedStreamPosition SavedPosition(Stream);
2193
2194 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2195 // FIXME this drops errors on the floor.
2196 consumeError(Err: std::move(Err));
2197 return nullptr;
2198 }
2199 RecordData Record;
2200 SmallVector<IdentifierInfo*, 16> MacroParams;
2201 MacroInfo *Macro = nullptr;
2202 llvm::MutableArrayRef<Token> MacroTokens;
2203
2204 while (true) {
2205 // Advance to the next record, but if we get to the end of the block, don't
2206 // pop it (removing all the abbreviations from the cursor) since we want to
2207 // be able to reseek within the block and read entries.
2208 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2209 Expected<llvm::BitstreamEntry> MaybeEntry =
2210 Stream.advanceSkippingSubblocks(Flags);
2211 if (!MaybeEntry) {
2212 Error(Err: MaybeEntry.takeError());
2213 return Macro;
2214 }
2215 llvm::BitstreamEntry Entry = MaybeEntry.get();
2216
2217 switch (Entry.Kind) {
2218 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2219 case llvm::BitstreamEntry::Error:
2220 Error(Msg: "malformed block record in AST file");
2221 return Macro;
2222 case llvm::BitstreamEntry::EndBlock:
2223 return Macro;
2224 case llvm::BitstreamEntry::Record:
2225 // The interesting case.
2226 break;
2227 }
2228
2229 // Read a record.
2230 Record.clear();
2231 PreprocessorRecordTypes RecType;
2232 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2233 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2234 else {
2235 Error(Err: MaybeRecType.takeError());
2236 return Macro;
2237 }
2238 switch (RecType) {
2239 case PP_MODULE_MACRO:
2240 case PP_MACRO_DIRECTIVE_HISTORY:
2241 return Macro;
2242
2243 case PP_MACRO_OBJECT_LIKE:
2244 case PP_MACRO_FUNCTION_LIKE: {
2245 // If we already have a macro, that means that we've hit the end
2246 // of the definition of the macro we were looking for. We're
2247 // done.
2248 if (Macro)
2249 return Macro;
2250
2251 unsigned NextIndex = 1; // Skip identifier ID.
2252 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2253 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2254 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2255 MI->setIsUsed(Record[NextIndex++]);
2256 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2257 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2258 PPAllocator&: PP.getPreprocessorAllocator());
2259 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2260 // Decode function-like macro info.
2261 bool isC99VarArgs = Record[NextIndex++];
2262 bool isGNUVarArgs = Record[NextIndex++];
2263 bool hasCommaPasting = Record[NextIndex++];
2264 MacroParams.clear();
2265 unsigned NumArgs = Record[NextIndex++];
2266 for (unsigned i = 0; i != NumArgs; ++i)
2267 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2268
2269 // Install function-like macro info.
2270 MI->setIsFunctionLike();
2271 if (isC99VarArgs) MI->setIsC99Varargs();
2272 if (isGNUVarArgs) MI->setIsGNUVarargs();
2273 if (hasCommaPasting) MI->setHasCommaPasting();
2274 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2275 }
2276
2277 // Remember that we saw this macro last so that we add the tokens that
2278 // form its body to it.
2279 Macro = MI;
2280
2281 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2282 Record[NextIndex]) {
2283 // We have a macro definition. Register the association
2284 PreprocessedEntityID
2285 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2286 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2287 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2288 PreprocessingRecord::PPEntityID PPID =
2289 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2290 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2291 Val: PPRec.getPreprocessedEntity(PPID));
2292 if (PPDef)
2293 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2294 }
2295
2296 ++NumMacrosRead;
2297 break;
2298 }
2299
2300 case PP_TOKEN: {
2301 // If we see a TOKEN before a PP_MACRO_*, then the file is
2302 // erroneous, just pretend we didn't see this.
2303 if (!Macro) break;
2304 if (MacroTokens.empty()) {
2305 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2306 return Macro;
2307 }
2308
2309 unsigned Idx = 0;
2310 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2311 MacroTokens = MacroTokens.drop_front();
2312 break;
2313 }
2314 }
2315 }
2316}
2317
2318PreprocessedEntityID
2319ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2320 PreprocessedEntityID LocalID) const {
2321 if (!M.ModuleOffsetMap.empty())
2322 ReadModuleOffsetMap(F&: M);
2323
2324 unsigned ModuleFileIndex = LocalID >> 32;
2325 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2326 ModuleFile *MF =
2327 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2328 assert(MF && "malformed identifier ID encoding?");
2329
2330 if (!ModuleFileIndex) {
2331 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2332 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2333 }
2334
2335 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2336}
2337
2338OptionalFileEntryRef
2339HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2340 FileManager &FileMgr = Reader.getFileManager();
2341 if (!Key.Imported)
2342 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2343
2344 auto Resolved =
2345 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2346 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2347}
2348
2349unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2350 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2351 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2352 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2353 return llvm::xxh3_64bits(data: buf);
2354}
2355
2356HeaderFileInfoTrait::internal_key_type
2357HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2358 internal_key_type ikey = {.Size: ekey.getSize(),
2359 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2360 .Filename: ekey.getName(), /*Imported*/ false};
2361 return ikey;
2362}
2363
2364bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2365 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2366 return false;
2367
2368 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2369 return true;
2370
2371 // Determine whether the actual files are equivalent.
2372 OptionalFileEntryRef FEA = getFile(Key: a);
2373 OptionalFileEntryRef FEB = getFile(Key: b);
2374 return FEA && FEA == FEB;
2375}
2376
2377std::pair<unsigned, unsigned>
2378HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2379 return readULEBKeyDataLength(P&: d);
2380}
2381
2382HeaderFileInfoTrait::internal_key_type
2383HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2384 using namespace llvm::support;
2385
2386 internal_key_type ikey;
2387 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2388 ikey.ModTime =
2389 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2390 ikey.Filename = (const char *)d;
2391 ikey.Imported = true;
2392 return ikey;
2393}
2394
2395HeaderFileInfoTrait::data_type
2396HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2397 unsigned DataLen) {
2398 using namespace llvm::support;
2399
2400 const unsigned char *End = d + DataLen;
2401 HeaderFileInfo HFI;
2402 unsigned Flags = *d++;
2403
2404 OptionalFileEntryRef FE;
2405 bool Included = (Flags >> 6) & 0x01;
2406 if (Included)
2407 if ((FE = getFile(Key: key)))
2408 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2409 // deserialize this header file info again.
2410 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2411
2412 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2413 HFI.isImport |= (Flags >> 5) & 0x01;
2414 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2415 HFI.DirInfo = (Flags >> 1) & 0x07;
2416 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2417 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2418
2419 assert((End - d) % 4 == 0 &&
2420 "Wrong data length in HeaderFileInfo deserialization");
2421 while (d != End) {
2422 uint32_t LocalSMID =
2423 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2424 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2425 LocalSMID >>= 3;
2426
2427 // This header is part of a module. Associate it with the module to enable
2428 // implicit module import.
2429 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2430 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2431 ModuleMap &ModMap =
2432 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2433
2434 if (FE || (FE = getFile(Key: key))) {
2435 // FIXME: NameAsWritten
2436 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2437 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2438 }
2439 HFI.mergeModuleMembership(Role: HeaderRole);
2440 }
2441
2442 // This HeaderFileInfo was externally loaded.
2443 HFI.External = true;
2444 HFI.IsValid = true;
2445 return HFI;
2446}
2447
2448void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2449 uint32_t MacroDirectivesOffset) {
2450 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2451 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2452}
2453
2454void ASTReader::ReadDefinedMacros() {
2455 // Note that we are loading defined macros.
2456 Deserializing Macros(this);
2457
2458 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2459 BitstreamCursor &MacroCursor = I.MacroCursor;
2460
2461 // If there was no preprocessor block, skip this file.
2462 if (MacroCursor.getBitcodeBytes().empty())
2463 continue;
2464
2465 BitstreamCursor Cursor = MacroCursor;
2466 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2467 Error(Err: std::move(Err));
2468 return;
2469 }
2470
2471 RecordData Record;
2472 while (true) {
2473 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2474 if (!MaybeE) {
2475 Error(Err: MaybeE.takeError());
2476 return;
2477 }
2478 llvm::BitstreamEntry E = MaybeE.get();
2479
2480 switch (E.Kind) {
2481 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2482 case llvm::BitstreamEntry::Error:
2483 Error(Msg: "malformed block record in AST file");
2484 return;
2485 case llvm::BitstreamEntry::EndBlock:
2486 goto NextCursor;
2487
2488 case llvm::BitstreamEntry::Record: {
2489 Record.clear();
2490 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2491 if (!MaybeRecord) {
2492 Error(Err: MaybeRecord.takeError());
2493 return;
2494 }
2495 switch (MaybeRecord.get()) {
2496 default: // Default behavior: ignore.
2497 break;
2498
2499 case PP_MACRO_OBJECT_LIKE:
2500 case PP_MACRO_FUNCTION_LIKE: {
2501 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2502 if (II->isOutOfDate())
2503 updateOutOfDateIdentifier(II: *II);
2504 break;
2505 }
2506
2507 case PP_TOKEN:
2508 // Ignore tokens.
2509 break;
2510 }
2511 break;
2512 }
2513 }
2514 }
2515 NextCursor: ;
2516 }
2517}
2518
2519namespace {
2520
2521 /// Visitor class used to look up identifirs in an AST file.
2522 class IdentifierLookupVisitor {
2523 StringRef Name;
2524 unsigned NameHash;
2525 unsigned PriorGeneration;
2526 unsigned &NumIdentifierLookups;
2527 unsigned &NumIdentifierLookupHits;
2528 IdentifierInfo *Found = nullptr;
2529
2530 public:
2531 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2532 unsigned &NumIdentifierLookups,
2533 unsigned &NumIdentifierLookupHits)
2534 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2535 PriorGeneration(PriorGeneration),
2536 NumIdentifierLookups(NumIdentifierLookups),
2537 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2538
2539 bool operator()(ModuleFile &M) {
2540 // If we've already searched this module file, skip it now.
2541 if (M.Generation <= PriorGeneration)
2542 return true;
2543
2544 ASTIdentifierLookupTable *IdTable
2545 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2546 if (!IdTable)
2547 return false;
2548
2549 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2550 Found);
2551 ++NumIdentifierLookups;
2552 ASTIdentifierLookupTable::iterator Pos =
2553 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2554 if (Pos == IdTable->end())
2555 return false;
2556
2557 // Dereferencing the iterator has the effect of building the
2558 // IdentifierInfo node and populating it with the various
2559 // declarations it needs.
2560 ++NumIdentifierLookupHits;
2561 Found = *Pos;
2562 if (Trait.hasMoreInformationInDependencies()) {
2563 // Look for the identifier in extra modules as they contain more info.
2564 return false;
2565 }
2566 return true;
2567 }
2568
2569 // Retrieve the identifier info found within the module
2570 // files.
2571 IdentifierInfo *getIdentifierInfo() const { return Found; }
2572 };
2573
2574} // namespace
2575
2576void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2577 // Note that we are loading an identifier.
2578 Deserializing AnIdentifier(this);
2579
2580 unsigned PriorGeneration = 0;
2581 if (getContext().getLangOpts().Modules)
2582 PriorGeneration = IdentifierGeneration[&II];
2583
2584 // If there is a global index, look there first to determine which modules
2585 // provably do not have any results for this identifier.
2586 GlobalModuleIndex::HitSet Hits;
2587 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2588 if (!loadGlobalIndex()) {
2589 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2590 HitsPtr = &Hits;
2591 }
2592 }
2593
2594 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2595 NumIdentifierLookups,
2596 NumIdentifierLookupHits);
2597 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2598 markIdentifierUpToDate(II: &II);
2599}
2600
2601void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2602 if (!II)
2603 return;
2604
2605 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2606
2607 // Update the generation for this identifier.
2608 if (getContext().getLangOpts().Modules)
2609 IdentifierGeneration[II] = getGeneration();
2610}
2611
2612MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2613 unsigned &Idx) {
2614 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2615 uint64_t LocalIndex = Record[Idx++];
2616 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2617}
2618
2619void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2620 const PendingMacroInfo &PMInfo) {
2621 ModuleFile &M = *PMInfo.M;
2622
2623 BitstreamCursor &Cursor = M.MacroCursor;
2624 SavedStreamPosition SavedPosition(Cursor);
2625 if (llvm::Error Err =
2626 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2627 Error(Err: std::move(Err));
2628 return;
2629 }
2630
2631 struct ModuleMacroRecord {
2632 SubmoduleID SubModID;
2633 MacroInfo *MI;
2634 SmallVector<SubmoduleID, 8> Overrides;
2635 };
2636 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2637
2638 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2639 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2640 // macro histroy.
2641 RecordData Record;
2642 while (true) {
2643 Expected<llvm::BitstreamEntry> MaybeEntry =
2644 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2645 if (!MaybeEntry) {
2646 Error(Err: MaybeEntry.takeError());
2647 return;
2648 }
2649 llvm::BitstreamEntry Entry = MaybeEntry.get();
2650
2651 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2652 Error(Msg: "malformed block record in AST file");
2653 return;
2654 }
2655
2656 Record.clear();
2657 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2658 if (!MaybePP) {
2659 Error(Err: MaybePP.takeError());
2660 return;
2661 }
2662 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2663 case PP_MACRO_DIRECTIVE_HISTORY:
2664 break;
2665
2666 case PP_MODULE_MACRO: {
2667 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2668 auto &Info = ModuleMacros.back();
2669 unsigned Idx = 0;
2670 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2671 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2672 for (int I = Idx, N = Record.size(); I != N; ++I)
2673 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2674 continue;
2675 }
2676
2677 default:
2678 Error(Msg: "malformed block record in AST file");
2679 return;
2680 }
2681
2682 // We found the macro directive history; that's the last record
2683 // for this macro.
2684 break;
2685 }
2686
2687 // Module macros are listed in reverse dependency order.
2688 {
2689 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2690 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2691 for (auto &MMR : ModuleMacros) {
2692 Overrides.clear();
2693 for (unsigned ModID : MMR.Overrides) {
2694 Module *Mod = getSubmodule(GlobalID: ModID);
2695 auto *Macro = PP.getModuleMacro(Mod, II);
2696 assert(Macro && "missing definition for overridden macro");
2697 Overrides.push_back(Elt: Macro);
2698 }
2699
2700 bool Inserted = false;
2701 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2702 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2703 }
2704 }
2705
2706 // Don't read the directive history for a module; we don't have anywhere
2707 // to put it.
2708 if (M.isModule())
2709 return;
2710
2711 // Deserialize the macro directives history in reverse source-order.
2712 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2713 unsigned Idx = 0, N = Record.size();
2714 while (Idx < N) {
2715 MacroDirective *MD = nullptr;
2716 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2717 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2718 switch (K) {
2719 case MacroDirective::MD_Define: {
2720 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2721 MD = PP.AllocateDefMacroDirective(MI, Loc);
2722 break;
2723 }
2724 case MacroDirective::MD_Undefine:
2725 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2726 break;
2727 case MacroDirective::MD_Visibility:
2728 bool isPublic = Record[Idx++];
2729 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2730 break;
2731 }
2732
2733 if (!Latest)
2734 Latest = MD;
2735 if (Earliest)
2736 Earliest->setPrevious(MD);
2737 Earliest = MD;
2738 }
2739
2740 if (Latest)
2741 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2742}
2743
2744bool ASTReader::shouldDisableValidationForFile(
2745 const serialization::ModuleFile &M) const {
2746 if (DisableValidationKind == DisableValidationForModuleKind::None)
2747 return false;
2748
2749 // If a PCH is loaded and validation is disabled for PCH then disable
2750 // validation for the PCH and the modules it loads.
2751 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2752
2753 switch (K) {
2754 case MK_MainFile:
2755 case MK_Preamble:
2756 case MK_PCH:
2757 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2758 case MK_ImplicitModule:
2759 case MK_ExplicitModule:
2760 case MK_PrebuiltModule:
2761 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2762 }
2763
2764 return false;
2765}
2766
2767static std::pair<StringRef, StringRef>
2768getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2769 const StringRef InputBlob) {
2770 uint16_t AsRequestedLength = Record[7];
2771 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2772 InputBlob.substr(Start: AsRequestedLength)};
2773}
2774
2775InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2776 // If this ID is bogus, just return an empty input file.
2777 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2778 return InputFileInfo();
2779
2780 // If we've already loaded this input file, return it.
2781 if (F.InputFileInfosLoaded[ID - 1].isValid())
2782 return F.InputFileInfosLoaded[ID - 1];
2783
2784 // Go find this input file.
2785 BitstreamCursor &Cursor = F.InputFilesCursor;
2786 SavedStreamPosition SavedPosition(Cursor);
2787 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2788 F.InputFileOffsets[ID - 1])) {
2789 // FIXME this drops errors on the floor.
2790 consumeError(Err: std::move(Err));
2791 }
2792
2793 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2794 if (!MaybeCode) {
2795 // FIXME this drops errors on the floor.
2796 consumeError(Err: MaybeCode.takeError());
2797 }
2798 unsigned Code = MaybeCode.get();
2799 RecordData Record;
2800 StringRef Blob;
2801
2802 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2803 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2804 "invalid record type for input file");
2805 else {
2806 // FIXME this drops errors on the floor.
2807 consumeError(Err: Maybe.takeError());
2808 }
2809
2810 assert(Record[0] == ID && "Bogus stored ID or offset");
2811 InputFileInfo R;
2812 R.StoredSize = static_cast<off_t>(Record[1]);
2813 R.StoredTime = static_cast<time_t>(Record[2]);
2814 R.Overridden = static_cast<bool>(Record[3]);
2815 R.Transient = static_cast<bool>(Record[4]);
2816 R.TopLevel = static_cast<bool>(Record[5]);
2817 R.ModuleMap = static_cast<bool>(Record[6]);
2818 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2819 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2820 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2821 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2822 ? UnresolvedFilenameAsRequested
2823 : UnresolvedFilename;
2824
2825 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2826 if (!MaybeEntry) // FIXME this drops errors on the floor.
2827 consumeError(Err: MaybeEntry.takeError());
2828 llvm::BitstreamEntry Entry = MaybeEntry.get();
2829 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2830 "expected record type for input file hash");
2831
2832 Record.clear();
2833 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2834 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2835 "invalid record type for input file hash");
2836 else {
2837 // FIXME this drops errors on the floor.
2838 consumeError(Err: Maybe.takeError());
2839 }
2840 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2841 static_cast<uint64_t>(Record[0]);
2842
2843 // Note that we've loaded this input file info.
2844 F.InputFileInfosLoaded[ID - 1] = R;
2845 return R;
2846}
2847
2848static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2849InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2850 // If this ID is bogus, just return an empty input file.
2851 if (ID == 0 || ID > F.InputFilesLoaded.size())
2852 return InputFile();
2853
2854 // If we've already loaded this input file, return it.
2855 if (F.InputFilesLoaded[ID-1].getFile())
2856 return F.InputFilesLoaded[ID-1];
2857
2858 if (F.InputFilesLoaded[ID-1].isNotFound())
2859 return InputFile();
2860
2861 // Go find this input file.
2862 BitstreamCursor &Cursor = F.InputFilesCursor;
2863 SavedStreamPosition SavedPosition(Cursor);
2864 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2865 F.InputFileOffsets[ID - 1])) {
2866 // FIXME this drops errors on the floor.
2867 consumeError(Err: std::move(Err));
2868 }
2869
2870 InputFileInfo FI = getInputFileInfo(F, ID);
2871 off_t StoredSize = FI.StoredSize;
2872 time_t StoredTime = FI.StoredTime;
2873 bool Overridden = FI.Overridden;
2874 bool Transient = FI.Transient;
2875 auto Filename =
2876 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2877 uint64_t StoredContentHash = FI.ContentHash;
2878
2879 // For standard C++ modules, we don't need to check the inputs.
2880 bool SkipChecks = F.StandardCXXModule;
2881
2882 const HeaderSearchOptions &HSOpts =
2883 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2884
2885 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2886 // modules.
2887 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2888 SkipChecks = false;
2889 Overridden = false;
2890 }
2891
2892 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2893
2894 // For an overridden file, create a virtual file with the stored
2895 // size/timestamp.
2896 if ((Overridden || Transient || SkipChecks) && !File)
2897 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2898
2899 if (!File) {
2900 if (Complain) {
2901 std::string ErrorStr = "could not find file '";
2902 ErrorStr += *Filename;
2903 ErrorStr += "' referenced by AST file '";
2904 ErrorStr += F.FileName.str();
2905 ErrorStr += "'";
2906 Error(Msg: ErrorStr);
2907 }
2908 // Record that we didn't find the file.
2909 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2910 return InputFile();
2911 }
2912
2913 // Check if there was a request to override the contents of the file
2914 // that was part of the precompiled header. Overriding such a file
2915 // can lead to problems when lexing using the source locations from the
2916 // PCH.
2917 SourceManager &SM = getSourceManager();
2918 // FIXME: Reject if the overrides are different.
2919 if ((!Overridden && !Transient) && !SkipChecks &&
2920 SM.isFileOverridden(File: *File)) {
2921 if (Complain)
2922 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2923
2924 // After emitting the diagnostic, bypass the overriding file to recover
2925 // (this creates a separate FileEntry).
2926 File = SM.bypassFileContentsOverride(File: *File);
2927 if (!File) {
2928 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2929 return InputFile();
2930 }
2931 }
2932
2933 auto HasInputContentChanged = [&](Change OriginalChange) {
2934 assert(ValidateASTInputFilesContent &&
2935 "We should only check the content of the inputs with "
2936 "ValidateASTInputFilesContent enabled.");
2937
2938 if (StoredContentHash == 0)
2939 return OriginalChange;
2940
2941 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2942 if (!MemBuffOrError) {
2943 if (!Complain)
2944 return OriginalChange;
2945 std::string ErrorStr = "could not get buffer for file '";
2946 ErrorStr += File->getName();
2947 ErrorStr += "'";
2948 Error(Msg: ErrorStr);
2949 return OriginalChange;
2950 }
2951
2952 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2953 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2954 return Change{.Kind: Change::None};
2955
2956 return Change{.Kind: Change::Content};
2957 };
2958 auto HasInputFileChanged = [&]() {
2959 if (StoredSize != File->getSize())
2960 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2961 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2962 StoredTime != File->getModificationTime()) {
2963 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2964 .New: File->getModificationTime()};
2965
2966 // In case the modification time changes but not the content,
2967 // accept the cached file as legit.
2968 if (ValidateASTInputFilesContent)
2969 return HasInputContentChanged(MTimeChange);
2970
2971 return MTimeChange;
2972 }
2973 return Change{.Kind: Change::None};
2974 };
2975
2976 bool IsOutOfDate = false;
2977 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2978 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2979 // enabled, it is better to check the contents of the inputs. Since we can't
2980 // get correct modified time information for inputs from overriden inputs.
2981 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2982 F.StandardCXXModule && FileChange.Kind == Change::None)
2983 FileChange = HasInputContentChanged(FileChange);
2984
2985 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2986 // it is better to check the content of the input files because we cannot rely
2987 // on the file modification time, which will be the same (zero) for these
2988 // files.
2989 if (!StoredTime && ValidateASTInputFilesContent &&
2990 FileChange.Kind == Change::None)
2991 FileChange = HasInputContentChanged(FileChange);
2992
2993 // For an overridden file, there is nothing to validate.
2994 if (!Overridden && FileChange.Kind != Change::None) {
2995 if (Complain) {
2996 // Build a list of the PCH imports that got us here (in reverse).
2997 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2998 while (!ImportStack.back()->ImportedBy.empty())
2999 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
3000
3001 // The top-level AST file is stale.
3002 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
3003 Diag(DiagID: diag::err_fe_ast_file_modified)
3004 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
3005 << TopLevelASTFileName;
3006 Diag(DiagID: diag::note_fe_ast_file_modified)
3007 << FileChange.Kind << (FileChange.Old && FileChange.New)
3008 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
3009 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3010
3011 // Print the import stack.
3012 if (ImportStack.size() > 1) {
3013 Diag(DiagID: diag::note_ast_file_required_by)
3014 << *Filename << ImportStack[0]->FileName;
3015 for (unsigned I = 1; I < ImportStack.size(); ++I)
3016 Diag(DiagID: diag::note_ast_file_required_by)
3017 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3018 }
3019
3020 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3021 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3022 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3023 << F.InputFilesValidationStatus;
3024 }
3025
3026 IsOutOfDate = true;
3027 }
3028 // FIXME: If the file is overridden and we've already opened it,
3029 // issue an error (or split it into a separate FileEntry).
3030
3031 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3032
3033 // Note that we've loaded this input file.
3034 F.InputFilesLoaded[ID-1] = IF;
3035 return IF;
3036}
3037
3038ASTReader::TemporarilyOwnedStringRef
3039ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3040 ModuleFile &ModF) {
3041 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3042}
3043
3044ASTReader::TemporarilyOwnedStringRef
3045ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3046 StringRef Prefix) {
3047 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3048
3049 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3050 Path == "<built-in>" || Path == "<command line>")
3051 return {Path, Buf};
3052
3053 Buf.clear();
3054 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3055 StringRef ResolvedPath{Buf.data(), Buf.size()};
3056 return {ResolvedPath, Buf};
3057}
3058
3059std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3060 StringRef P,
3061 ModuleFile &ModF) {
3062 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3063}
3064
3065std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3066 StringRef P,
3067 StringRef Prefix) {
3068 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3069 return ResolvedPath->str();
3070}
3071
3072static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3073 switch (ARR) {
3074 case ASTReader::Failure: return true;
3075 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3076 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3077 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3078 case ASTReader::ConfigurationMismatch:
3079 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3080 case ASTReader::HadErrors: return true;
3081 case ASTReader::Success: return false;
3082 }
3083
3084 llvm_unreachable("unknown ASTReadResult");
3085}
3086
3087ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3088 BitstreamCursor &Stream, StringRef Filename,
3089 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3090 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3091 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3092 // FIXME this drops errors on the floor.
3093 consumeError(Err: std::move(Err));
3094 return Failure;
3095 }
3096
3097 // Read all of the records in the options block.
3098 RecordData Record;
3099 ASTReadResult Result = Success;
3100 while (true) {
3101 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3102 if (!MaybeEntry) {
3103 // FIXME this drops errors on the floor.
3104 consumeError(Err: MaybeEntry.takeError());
3105 return Failure;
3106 }
3107 llvm::BitstreamEntry Entry = MaybeEntry.get();
3108
3109 switch (Entry.Kind) {
3110 case llvm::BitstreamEntry::Error:
3111 case llvm::BitstreamEntry::SubBlock:
3112 return Failure;
3113
3114 case llvm::BitstreamEntry::EndBlock:
3115 return Result;
3116
3117 case llvm::BitstreamEntry::Record:
3118 // The interesting case.
3119 break;
3120 }
3121
3122 // Read and process a record.
3123 Record.clear();
3124 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3125 if (!MaybeRecordType) {
3126 // FIXME this drops errors on the floor.
3127 consumeError(Err: MaybeRecordType.takeError());
3128 return Failure;
3129 }
3130 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3131 case LANGUAGE_OPTIONS: {
3132 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3133 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3134 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3135 Result = ConfigurationMismatch;
3136 break;
3137 }
3138
3139 case CODEGEN_OPTIONS: {
3140 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3141 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3142 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3143 Result = ConfigurationMismatch;
3144 break;
3145 }
3146
3147 case TARGET_OPTIONS: {
3148 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3149 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3150 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3151 Result = ConfigurationMismatch;
3152 break;
3153 }
3154
3155 case FILE_SYSTEM_OPTIONS: {
3156 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3157 if (!AllowCompatibleConfigurationMismatch &&
3158 ParseFileSystemOptions(Record, Complain, Listener))
3159 Result = ConfigurationMismatch;
3160 break;
3161 }
3162
3163 case HEADER_SEARCH_OPTIONS: {
3164 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3165 if (!AllowCompatibleConfigurationMismatch &&
3166 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3167 Result = ConfigurationMismatch;
3168 break;
3169 }
3170
3171 case PREPROCESSOR_OPTIONS:
3172 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3173 if (!AllowCompatibleConfigurationMismatch &&
3174 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3175 SuggestedPredefines))
3176 Result = ConfigurationMismatch;
3177 break;
3178 }
3179 }
3180}
3181
3182/// Returns {build-session validation applies, MF was validated this session}.
3183static std::pair<bool, bool>
3184wasValidatedInBuildSession(const ModuleFile &MF,
3185 const HeaderSearchOptions &HSOpts) {
3186 const bool EnablesBSValidation =
3187 HSOpts.ModulesValidateOncePerBuildSession && MF.Kind == MK_ImplicitModule;
3188 const bool WasValidated =
3189 EnablesBSValidation &&
3190 MF.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp;
3191 return {EnablesBSValidation, WasValidated};
3192}
3193
3194ASTReader::RelocationResult
3195ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3196 // Don't emit module relocation errors if we have -fno-validate-pch.
3197 const bool IgnoreError =
3198 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3199 DisableValidationForModuleKind::Module);
3200
3201 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3202 return {std::nullopt, IgnoreError};
3203
3204 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3205
3206 if (!DirectoryCheck &&
3207 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3208 return {std::nullopt, IgnoreError};
3209
3210 const HeaderSearchOptions &HSOpts =
3211 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3212
3213 // When only validating modules once per build session,
3214 // Skip check if the timestamp is up to date or module was built in same build
3215 // session.
3216 auto [EnablesBSValidation, WasValidated] =
3217 wasValidatedInBuildSession(MF: F, HSOpts);
3218 if (WasValidated)
3219 return {std::nullopt, IgnoreError};
3220 if (EnablesBSValidation &&
3221 static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
3222 return {std::nullopt, IgnoreError};
3223
3224 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3225
3226 // If we've already loaded a module map file covering this module, we may
3227 // have a better path for it (relative to the current build if doing directory
3228 // check).
3229 Module *M = PP.getHeaderSearchInfo().lookupModule(
3230 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3231 /*AllowSearch=*/DirectoryCheck,
3232 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3233
3234 return {M, IgnoreError};
3235}
3236
3237ASTReader::ASTReadResult
3238ASTReader::ReadControlBlock(ModuleFile &F,
3239 SmallVectorImpl<ImportedModule> &Loaded,
3240 const ModuleFile *ImportedBy,
3241 unsigned ClientLoadCapabilities) {
3242 BitstreamCursor &Stream = F.Stream;
3243
3244 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3245 Error(Err: std::move(Err));
3246 return Failure;
3247 }
3248
3249 // Lambda to read the unhashed control block the first time it's called.
3250 //
3251 // For PCM files, the unhashed control block cannot be read until after the
3252 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3253 // need to look ahead before reading the IMPORTS record. For consistency,
3254 // this block is always read somehow (see BitstreamEntry::EndBlock).
3255 bool HasReadUnhashedControlBlock = false;
3256 auto readUnhashedControlBlockOnce = [&]() {
3257 if (!HasReadUnhashedControlBlock) {
3258 HasReadUnhashedControlBlock = true;
3259 if (ASTReadResult Result =
3260 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3261 return Result;
3262 }
3263 return Success;
3264 };
3265
3266 bool DisableValidation = shouldDisableValidationForFile(M: F);
3267
3268 // Read all of the records and blocks in the control block.
3269 RecordData Record;
3270 unsigned NumInputs = 0;
3271 unsigned NumUserInputs = 0;
3272 StringRef BaseDirectoryAsWritten;
3273 while (true) {
3274 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3275 if (!MaybeEntry) {
3276 Error(Err: MaybeEntry.takeError());
3277 return Failure;
3278 }
3279 llvm::BitstreamEntry Entry = MaybeEntry.get();
3280
3281 switch (Entry.Kind) {
3282 case llvm::BitstreamEntry::Error:
3283 Error(Msg: "malformed block record in AST file");
3284 return Failure;
3285 case llvm::BitstreamEntry::EndBlock: {
3286 // Validate the module before returning. This call catches an AST with
3287 // no module name and no imports.
3288 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3289 return Result;
3290
3291 // Validate input files.
3292 const HeaderSearchOptions &HSOpts =
3293 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3294
3295 // All user input files reside at the index range [0, NumUserInputs), and
3296 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3297 // loaded module files, ignore missing inputs.
3298 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3299 F.Kind != MK_PrebuiltModule) {
3300 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3301
3302 // If we are reading a module, we will create a verification timestamp,
3303 // so we verify all input files. Otherwise, verify only user input
3304 // files.
3305
3306 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3307 F.InputFilesValidationStatus = ValidateSystemInputs
3308 ? InputFilesValidation::AllFiles
3309 : InputFilesValidation::UserFiles;
3310 auto [_, WasValidated] = wasValidatedInBuildSession(MF: F, HSOpts);
3311 if (WasValidated) {
3312 N = ForceValidateUserInputs ? NumUserInputs : 0;
3313 F.InputFilesValidationStatus =
3314 ForceValidateUserInputs
3315 ? InputFilesValidation::UserFiles
3316 : InputFilesValidation::SkippedInBuildSession;
3317 }
3318
3319 if (N != 0)
3320 Diag(DiagID: diag::remark_module_validation)
3321 << N << F.ModuleName << F.FileName;
3322
3323 for (unsigned I = 0; I < N; ++I) {
3324 InputFile IF = getInputFile(F, ID: I+1, Complain);
3325 if (!IF.getFile() || IF.isOutOfDate())
3326 return OutOfDate;
3327 }
3328 } else {
3329 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3330 }
3331
3332 if (Listener)
3333 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind, DirectlyImported: F.isDirectlyImported());
3334
3335 if (Listener && Listener->needsInputFileVisitation()) {
3336 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3337 : NumUserInputs;
3338 for (unsigned I = 0; I < N; ++I) {
3339 bool IsSystem = I >= NumUserInputs;
3340 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3341 auto FilenameAsRequested = ResolveImportedPath(
3342 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3343 Listener->visitInputFile(
3344 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3345 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3346 }
3347 }
3348
3349 return Success;
3350 }
3351
3352 case llvm::BitstreamEntry::SubBlock:
3353 switch (Entry.ID) {
3354 case INPUT_FILES_BLOCK_ID:
3355 F.InputFilesCursor = Stream;
3356 if (llvm::Error Err = Stream.SkipBlock()) {
3357 Error(Err: std::move(Err));
3358 return Failure;
3359 }
3360 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3361 Error(Msg: "malformed block record in AST file");
3362 return Failure;
3363 }
3364 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3365 continue;
3366
3367 case OPTIONS_BLOCK_ID:
3368 // If we're reading the first module for this group, check its options
3369 // are compatible with ours. For modules it imports, no further checking
3370 // is required, because we checked them when we built it.
3371 if (Listener && !ImportedBy) {
3372 // Should we allow the configuration of the module file to differ from
3373 // the configuration of the current translation unit in a compatible
3374 // way?
3375 //
3376 // FIXME: Allow this for files explicitly specified with -include-pch.
3377 bool AllowCompatibleConfigurationMismatch =
3378 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3379
3380 ASTReadResult Result =
3381 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3382 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3383 SuggestedPredefines);
3384 if (Result == Failure) {
3385 Error(Msg: "malformed block record in AST file");
3386 return Result;
3387 }
3388
3389 if (DisableValidation ||
3390 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3391 Result = Success;
3392
3393 // If we can't load the module, exit early since we likely
3394 // will rebuild the module anyway. The stream may be in the
3395 // middle of a block.
3396 if (Result != Success)
3397 return Result;
3398 } else if (llvm::Error Err = Stream.SkipBlock()) {
3399 Error(Err: std::move(Err));
3400 return Failure;
3401 }
3402 continue;
3403
3404 default:
3405 if (llvm::Error Err = Stream.SkipBlock()) {
3406 Error(Err: std::move(Err));
3407 return Failure;
3408 }
3409 continue;
3410 }
3411
3412 case llvm::BitstreamEntry::Record:
3413 // The interesting case.
3414 break;
3415 }
3416
3417 // Read and process a record.
3418 Record.clear();
3419 StringRef Blob;
3420 Expected<unsigned> MaybeRecordType =
3421 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3422 if (!MaybeRecordType) {
3423 Error(Err: MaybeRecordType.takeError());
3424 return Failure;
3425 }
3426 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3427 case METADATA: {
3428 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3429 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3430 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3431 : diag::err_ast_file_version_too_new)
3432 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3433 return VersionMismatch;
3434 }
3435
3436 bool hasErrors = Record[7];
3437 if (hasErrors && !DisableValidation) {
3438 // If requested by the caller and the module hasn't already been read
3439 // or compiled, mark modules on error as out-of-date.
3440 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3441 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3442 return OutOfDate;
3443
3444 if (!AllowASTWithCompilerErrors) {
3445 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3446 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3447 return HadErrors;
3448 }
3449 }
3450 if (hasErrors) {
3451 Diags.ErrorOccurred = true;
3452 Diags.UncompilableErrorOccurred = true;
3453 Diags.UnrecoverableErrorOccurred = true;
3454 }
3455
3456 F.RelocatablePCH = Record[4];
3457 // Relative paths in a relocatable PCH are relative to our sysroot.
3458 if (F.RelocatablePCH)
3459 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3460
3461 F.StandardCXXModule = Record[5];
3462
3463 F.HasTimestamps = Record[6];
3464
3465 const std::string &CurBranch = getClangFullRepositoryVersion();
3466 StringRef ASTBranch = Blob;
3467 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3468 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3469 Diag(DiagID: diag::err_ast_file_different_branch)
3470 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3471 << CurBranch;
3472 return VersionMismatch;
3473 }
3474 break;
3475 }
3476
3477 case IMPORT: {
3478 // Validate the AST before processing any imports (otherwise, untangling
3479 // them can be error-prone and expensive). A module will have a name and
3480 // will already have been validated, but this catches the PCH case.
3481 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3482 return Result;
3483
3484 unsigned Idx = 0;
3485 // Read information about the AST file.
3486 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3487
3488 // The import location will be the local one for now; we will adjust
3489 // all import locations of module imports after the global source
3490 // location info are setup, in ReadAST.
3491 auto [ImportLoc, ImportModuleFileIndex] =
3492 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3493 // The import location must belong to the current module file itself.
3494 assert(ImportModuleFileIndex == 0);
3495
3496 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3497
3498 bool IsImportingStdCXXModule = Record[Idx++];
3499
3500 off_t StoredSize = 0;
3501 time_t StoredModTime = 0;
3502 unsigned FileNameKind = 0;
3503 ASTFileSignature StoredSignature;
3504 ModuleFileName ImportedFile;
3505 std::string StoredFile;
3506 bool IgnoreImportedByNote = false;
3507
3508 // For prebuilt and explicit modules first consult the file map for
3509 // an override. Note that here we don't search prebuilt module
3510 // directories if we're not importing standard c++ module, only the
3511 // explicit name to file mappings. Also, we will still verify the
3512 // size/signature making sure it is essentially the same file but
3513 // perhaps in a different location.
3514 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3515 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3516 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3517
3518 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3519 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3520 return Missing;
3521 }
3522
3523 if (!IsImportingStdCXXModule) {
3524 StoredSize = (off_t)Record[Idx++];
3525 StoredModTime = (time_t)Record[Idx++];
3526 FileNameKind = (unsigned)Record[Idx++];
3527
3528 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3529 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3530 Last: SignatureBytes.end());
3531 Blob = Blob.substr(Start: ASTFileSignature::size);
3532
3533 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3534 if (ImportedFile.empty()) {
3535 ImportedFile = ModuleFileName::makeFromRaw(Name: StoredFile, RawKind: FileNameKind);
3536 } else if (!getDiags().isIgnored(
3537 DiagID: diag::warn_module_file_mapping_mismatch,
3538 Loc: CurrentImportLoc)) {
3539 auto ImportedFileRef =
3540 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3541 auto StoredFileRef =
3542 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3543 if ((ImportedFileRef && StoredFileRef) &&
3544 (*ImportedFileRef != *StoredFileRef)) {
3545 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3546 << ImportedFile << StoredFile;
3547 Diag(DiagID: diag::note_module_file_imported_by)
3548 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3549 IgnoreImportedByNote = true;
3550 }
3551 }
3552 }
3553
3554 // If our client can't cope with us being out of date, we can't cope with
3555 // our dependency being missing.
3556 unsigned Capabilities = ClientLoadCapabilities;
3557 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3558 Capabilities &= ~ARR_Missing;
3559
3560 // Load the AST file.
3561 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3562 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3563 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3564
3565 // Check the AST we just read from ImportedFile contains a different
3566 // module than we expected (ImportedName). This can occur for C++20
3567 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3568 if (IsImportingStdCXXModule) {
3569 if (const auto *Imported =
3570 getModuleManager().lookupByFileName(FileName: ImportedFile);
3571 Imported != nullptr && Imported->ModuleName != ImportedName) {
3572 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3573 Result = Missing;
3574 }
3575 }
3576
3577 // If we diagnosed a problem, produce a backtrace.
3578 bool recompilingFinalized = Result == OutOfDate &&
3579 (Capabilities & ARR_OutOfDate) &&
3580 getModuleManager()
3581 .getModuleCache()
3582 .getInMemoryModuleCache()
3583 .isPCMFinal(Filename: F.FileName);
3584 if (!IgnoreImportedByNote &&
3585 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3586 Diag(DiagID: diag::note_module_file_imported_by)
3587 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3588
3589 switch (Result) {
3590 case Failure: return Failure;
3591 // If we have to ignore the dependency, we'll have to ignore this too.
3592 case Missing:
3593 case OutOfDate: return OutOfDate;
3594 case VersionMismatch: return VersionMismatch;
3595 case ConfigurationMismatch: return ConfigurationMismatch;
3596 case HadErrors: return HadErrors;
3597 case Success: break;
3598 }
3599 break;
3600 }
3601
3602 case ORIGINAL_FILE:
3603 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3604 F.ActualOriginalSourceFileName = std::string(Blob);
3605 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3606 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3607 break;
3608
3609 case ORIGINAL_FILE_ID:
3610 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3611 break;
3612
3613 case MODULE_NAME:
3614 F.ModuleName = std::string(Blob);
3615 Diag(DiagID: diag::remark_module_import)
3616 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3617 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3618 if (Listener)
3619 Listener->ReadModuleName(ModuleName: F.ModuleName);
3620
3621 // Validate the AST as soon as we have a name so we can exit early on
3622 // failure.
3623 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3624 return Result;
3625
3626 break;
3627
3628 case MODULE_DIRECTORY: {
3629 // Save the BaseDirectory as written in the PCM for computing the module
3630 // filename for the ModuleCache.
3631 BaseDirectoryAsWritten = Blob;
3632 assert(!F.ModuleName.empty() &&
3633 "MODULE_DIRECTORY found before MODULE_NAME");
3634 F.BaseDirectory = std::string(Blob);
3635
3636 auto [MaybeM, IgnoreError] =
3637 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3638 if (!MaybeM.has_value())
3639 break;
3640
3641 Module *M = MaybeM.value();
3642 if (!M || !M->Directory)
3643 break;
3644 if (IgnoreError) {
3645 F.BaseDirectory = std::string(M->Directory->getName());
3646 break;
3647 }
3648 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3649 break;
3650
3651 // If we're implicitly loading a module, the base directory can't
3652 // change between the build and use.
3653 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3654 if (BuildDir && (*BuildDir == M->Directory)) {
3655 F.BaseDirectory = std::string(M->Directory->getName());
3656 break;
3657 }
3658 Diag(DiagID: diag::remark_module_relocated)
3659 << F.ModuleName << Blob << M->Directory->getName();
3660
3661 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3662 Diag(DiagID: diag::err_imported_module_relocated)
3663 << F.ModuleName << Blob << M->Directory->getName();
3664 return OutOfDate;
3665 }
3666
3667 case MODULE_MAP_FILE:
3668 if (ASTReadResult Result =
3669 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3670 return Result;
3671 break;
3672
3673 case INPUT_FILE_OFFSETS:
3674 NumInputs = Record[0];
3675 NumUserInputs = Record[1];
3676 F.InputFileOffsets =
3677 (const llvm::support::unaligned_uint64_t *)Blob.data();
3678 F.InputFilesLoaded.resize(new_size: NumInputs);
3679 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3680 F.NumUserInputFiles = NumUserInputs;
3681 break;
3682 }
3683 }
3684}
3685
3686llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3687 unsigned ClientLoadCapabilities) {
3688 BitstreamCursor &Stream = F.Stream;
3689
3690 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3691 return Err;
3692 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3693
3694 // Read all of the records and blocks for the AST file.
3695 RecordData Record;
3696 while (true) {
3697 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3698 if (!MaybeEntry)
3699 return MaybeEntry.takeError();
3700 llvm::BitstreamEntry Entry = MaybeEntry.get();
3701
3702 switch (Entry.Kind) {
3703 case llvm::BitstreamEntry::Error:
3704 return llvm::createStringError(
3705 EC: std::errc::illegal_byte_sequence,
3706 Fmt: "error at end of module block in AST file");
3707 case llvm::BitstreamEntry::EndBlock:
3708 // Outside of C++, we do not store a lookup map for the translation unit.
3709 // Instead, mark it as needing a lookup map to be built if this module
3710 // contains any declarations lexically within it (which it always does!).
3711 // This usually has no cost, since we very rarely need the lookup map for
3712 // the translation unit outside C++.
3713 if (ASTContext *Ctx = ContextObj) {
3714 DeclContext *DC = Ctx->getTranslationUnitDecl();
3715 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3716 DC->setMustBuildLookupTable();
3717 }
3718
3719 return llvm::Error::success();
3720 case llvm::BitstreamEntry::SubBlock:
3721 switch (Entry.ID) {
3722 case DECLTYPES_BLOCK_ID:
3723 // We lazily load the decls block, but we want to set up the
3724 // DeclsCursor cursor to point into it. Clone our current bitcode
3725 // cursor to it, enter the block and read the abbrevs in that block.
3726 // With the main cursor, we just skip over it.
3727 F.DeclsCursor = Stream;
3728 if (llvm::Error Err = Stream.SkipBlock())
3729 return Err;
3730 if (llvm::Error Err = ReadBlockAbbrevs(
3731 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3732 return Err;
3733 break;
3734
3735 case PREPROCESSOR_BLOCK_ID:
3736 F.MacroCursor = Stream;
3737 if (!PP.getExternalSource())
3738 PP.setExternalSource(this);
3739
3740 if (llvm::Error Err = Stream.SkipBlock())
3741 return Err;
3742 if (llvm::Error Err =
3743 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3744 return Err;
3745 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3746 break;
3747
3748 case PREPROCESSOR_DETAIL_BLOCK_ID:
3749 F.PreprocessorDetailCursor = Stream;
3750
3751 if (llvm::Error Err = Stream.SkipBlock()) {
3752 return Err;
3753 }
3754 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3755 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3756 return Err;
3757 F.PreprocessorDetailStartOffset
3758 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3759
3760 if (!PP.getPreprocessingRecord())
3761 PP.createPreprocessingRecord();
3762 if (!PP.getPreprocessingRecord()->getExternalSource())
3763 PP.getPreprocessingRecord()->SetExternalSource(*this);
3764 break;
3765
3766 case SOURCE_MANAGER_BLOCK_ID:
3767 if (llvm::Error Err = ReadSourceManagerBlock(F))
3768 return Err;
3769 break;
3770
3771 case SUBMODULE_BLOCK_ID:
3772 F.SubmodulesCursor = Stream;
3773 if (llvm::Error Err = Stream.SkipBlock())
3774 return Err;
3775 if (llvm::Error Err =
3776 ReadBlockAbbrevs(Cursor&: F.SubmodulesCursor, BlockID: SUBMODULE_BLOCK_ID))
3777 return Err;
3778 F.SubmodulesOffsetBase = F.SubmodulesCursor.GetCurrentBitNo();
3779 break;
3780
3781 case COMMENTS_BLOCK_ID: {
3782 BitstreamCursor C = Stream;
3783
3784 if (llvm::Error Err = Stream.SkipBlock())
3785 return Err;
3786 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3787 return Err;
3788 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3789 break;
3790 }
3791
3792 default:
3793 if (llvm::Error Err = Stream.SkipBlock())
3794 return Err;
3795 break;
3796 }
3797 continue;
3798
3799 case llvm::BitstreamEntry::Record:
3800 // The interesting case.
3801 break;
3802 }
3803
3804 // Read and process a record.
3805 Record.clear();
3806 StringRef Blob;
3807 Expected<unsigned> MaybeRecordType =
3808 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3809 if (!MaybeRecordType)
3810 return MaybeRecordType.takeError();
3811 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3812
3813 // If we're not loading an AST context, we don't care about most records.
3814 if (!ContextObj) {
3815 switch (RecordType) {
3816 case IDENTIFIER_TABLE:
3817 case IDENTIFIER_OFFSET:
3818 case INTERESTING_IDENTIFIERS:
3819 case STATISTICS:
3820 case PP_ASSUME_NONNULL_LOC:
3821 case PP_CONDITIONAL_STACK:
3822 case PP_COUNTER_VALUE:
3823 case SOURCE_LOCATION_OFFSETS:
3824 case MODULE_OFFSET_MAP:
3825 case SOURCE_MANAGER_LINE_TABLE:
3826 case PPD_ENTITIES_OFFSETS:
3827 case HEADER_SEARCH_TABLE:
3828 case IMPORTED_MODULES:
3829 case MACRO_OFFSET:
3830 case SUBMODULE_METADATA:
3831 break;
3832 default:
3833 continue;
3834 }
3835 }
3836
3837 switch (RecordType) {
3838 default: // Default behavior: ignore.
3839 break;
3840
3841 case SUBMODULE_METADATA: {
3842 F.BaseSubmoduleID = getTotalNumSubmodules();
3843 F.LocalNumSubmodules = Record[0];
3844 F.LocalBaseSubmoduleID = Record[1];
3845 F.LocalTopLevelSubmoduleID = Record[2];
3846 F.SubmoduleOffsets =
3847 (const llvm::support::unaligned_uint64_t *)Blob.data();
3848 if (F.LocalNumSubmodules > 0) {
3849 // Introduce the global -> local mapping for submodules within this
3850 // module.
3851 GlobalSubmoduleMap.insert(
3852 Val: std::make_pair(x: getTotalNumSubmodules() + 1, y: &F));
3853
3854 // Introduce the local -> global mapping for submodules within this
3855 // module.
3856 F.SubmoduleRemap.insertOrReplace(
3857 Val: std::make_pair(x&: F.LocalBaseSubmoduleID,
3858 y: F.BaseSubmoduleID - F.LocalBaseSubmoduleID));
3859
3860 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
3861 }
3862
3863 auto ReadSubmodule = [&](unsigned LocalID) -> Module * {
3864 return getSubmodule(GlobalID: getGlobalSubmoduleID(M&: F, LocalID));
3865 };
3866
3867 if (PP.getHeaderSearchInfo().getModuleMap().findModule(Name: F.ModuleName)) {
3868 // If we already knew about this module, make sure to bring all
3869 // submodules up to date.
3870 for (unsigned Index = 0; Index != F.LocalNumSubmodules; ++Index) {
3871 unsigned LocalID =
3872 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
3873 ReadSubmodule(LocalID);
3874 }
3875 } else {
3876 // If we didn't know this module, we loaded it transitively. Deserialize
3877 // just the top-level module to register it with ModuleMap, but load the
3878 // rest lazily.
3879 ReadSubmodule(F.LocalTopLevelSubmoduleID);
3880 }
3881
3882 break;
3883 }
3884
3885 case TYPE_OFFSET: {
3886 if (F.LocalNumTypes != 0)
3887 return llvm::createStringError(
3888 EC: std::errc::illegal_byte_sequence,
3889 Fmt: "duplicate TYPE_OFFSET record in AST file");
3890 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3891 F.LocalNumTypes = Record[0];
3892 F.BaseTypeIndex = getTotalNumTypes();
3893
3894 if (F.LocalNumTypes > 0)
3895 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3896
3897 break;
3898 }
3899
3900 case DECL_OFFSET: {
3901 if (F.LocalNumDecls != 0)
3902 return llvm::createStringError(
3903 EC: std::errc::illegal_byte_sequence,
3904 Fmt: "duplicate DECL_OFFSET record in AST file");
3905 F.DeclOffsets = (const DeclOffset *)Blob.data();
3906 F.LocalNumDecls = Record[0];
3907 F.BaseDeclIndex = getTotalNumDecls();
3908
3909 if (F.LocalNumDecls > 0)
3910 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3911
3912 break;
3913 }
3914
3915 case TU_UPDATE_LEXICAL: {
3916 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3917 LexicalContents Contents(
3918 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3919 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3920 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3921 TU->setHasExternalLexicalStorage(true);
3922 break;
3923 }
3924
3925 case UPDATE_VISIBLE: {
3926 unsigned Idx = 0;
3927 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3928 auto *Data = (const unsigned char*)Blob.data();
3929 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3930 // If we've already loaded the decl, perform the updates when we finish
3931 // loading this block.
3932 if (Decl *D = GetExistingDecl(ID))
3933 PendingUpdateRecords.push_back(
3934 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3935 break;
3936 }
3937
3938 case UPDATE_MODULE_LOCAL_VISIBLE: {
3939 unsigned Idx = 0;
3940 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3941 auto *Data = (const unsigned char *)Blob.data();
3942 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3943 // If we've already loaded the decl, perform the updates when we finish
3944 // loading this block.
3945 if (Decl *D = GetExistingDecl(ID))
3946 PendingUpdateRecords.push_back(
3947 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3948 break;
3949 }
3950
3951 case UPDATE_TU_LOCAL_VISIBLE: {
3952 if (F.Kind != MK_MainFile)
3953 break;
3954 unsigned Idx = 0;
3955 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3956 auto *Data = (const unsigned char *)Blob.data();
3957 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3958 // If we've already loaded the decl, perform the updates when we finish
3959 // loading this block.
3960 if (Decl *D = GetExistingDecl(ID))
3961 PendingUpdateRecords.push_back(
3962 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3963 break;
3964 }
3965
3966 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3967 unsigned Idx = 0;
3968 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3969 auto *Data = (const unsigned char *)Blob.data();
3970 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3971 // If we've already loaded the decl, perform the updates when we finish
3972 // loading this block.
3973 if (Decl *D = GetExistingDecl(ID))
3974 PendingUpdateRecords.push_back(
3975 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3976 break;
3977 }
3978
3979 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3980 unsigned Idx = 0;
3981 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3982 auto *Data = (const unsigned char *)Blob.data();
3983 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3984 // If we've already loaded the decl, perform the updates when we finish
3985 // loading this block.
3986 if (Decl *D = GetExistingDecl(ID))
3987 PendingUpdateRecords.push_back(
3988 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3989 break;
3990 }
3991
3992 case IDENTIFIER_TABLE:
3993 F.IdentifierTableData =
3994 reinterpret_cast<const unsigned char *>(Blob.data());
3995 if (Record[0]) {
3996 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3997 Buckets: F.IdentifierTableData + Record[0],
3998 Payload: F.IdentifierTableData + sizeof(uint32_t),
3999 Base: F.IdentifierTableData,
4000 InfoObj: ASTIdentifierLookupTrait(*this, F));
4001
4002 PP.getIdentifierTable().setExternalIdentifierLookup(this);
4003 }
4004 break;
4005
4006 case IDENTIFIER_OFFSET: {
4007 if (F.LocalNumIdentifiers != 0)
4008 return llvm::createStringError(
4009 EC: std::errc::illegal_byte_sequence,
4010 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
4011 F.IdentifierOffsets = (const uint32_t *)Blob.data();
4012 F.LocalNumIdentifiers = Record[0];
4013 F.BaseIdentifierID = getTotalNumIdentifiers();
4014
4015 if (F.LocalNumIdentifiers > 0)
4016 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
4017 + F.LocalNumIdentifiers);
4018 break;
4019 }
4020
4021 case INTERESTING_IDENTIFIERS:
4022 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
4023 break;
4024
4025 case EAGERLY_DESERIALIZED_DECLS:
4026 // FIXME: Skip reading this record if our ASTConsumer doesn't care
4027 // about "interesting" decls (for instance, if we're building a module).
4028 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4029 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4030 break;
4031
4032 case MODULAR_CODEGEN_DECLS:
4033 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
4034 // them (ie: if we're not codegenerating this module).
4035 if (F.Kind == MK_MainFile ||
4036 getContext().getLangOpts().BuildingPCHWithObjectFile)
4037 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4038 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4039 break;
4040
4041 case SPECIAL_TYPES:
4042 if (SpecialTypes.empty()) {
4043 for (unsigned I = 0, N = Record.size(); I != N; ++I)
4044 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
4045 break;
4046 }
4047
4048 if (Record.empty())
4049 break;
4050
4051 if (SpecialTypes.size() != Record.size())
4052 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4053 Fmt: "invalid special-types record");
4054
4055 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4056 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
4057 if (!SpecialTypes[I])
4058 SpecialTypes[I] = ID;
4059 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
4060 // merge step?
4061 }
4062 break;
4063
4064 case STATISTICS:
4065 TotalNumStatements += Record[0];
4066 TotalNumMacros += Record[1];
4067 TotalLexicalDeclContexts += Record[2];
4068 TotalVisibleDeclContexts += Record[3];
4069 TotalModuleLocalVisibleDeclContexts += Record[4];
4070 TotalTULocalVisibleDeclContexts += Record[5];
4071 break;
4072
4073 case UNUSED_FILESCOPED_DECLS:
4074 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4075 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4076 break;
4077
4078 case DELEGATING_CTORS:
4079 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4080 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4081 break;
4082
4083 case WEAK_UNDECLARED_IDENTIFIERS:
4084 if (Record.size() % 3 != 0)
4085 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4086 Fmt: "invalid weak identifiers record");
4087
4088 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4089 // files. This isn't the way to do it :)
4090 WeakUndeclaredIdentifiers.clear();
4091
4092 // Translate the weak, undeclared identifiers into global IDs.
4093 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4094 WeakUndeclaredIdentifiers.push_back(
4095 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4096 WeakUndeclaredIdentifiers.push_back(
4097 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4098 WeakUndeclaredIdentifiers.push_back(
4099 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4100 }
4101 break;
4102
4103 case EXTNAME_UNDECLARED_IDENTIFIERS:
4104 if (Record.size() % 3 != 0)
4105 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4106 Fmt: "invalid extname identifiers record");
4107
4108 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4109 // non-original PCH files. This isn't the way to do it :)
4110 ExtnameUndeclaredIdentifiers.clear();
4111
4112 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4113 // global IDs.
4114 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4115 ExtnameUndeclaredIdentifiers.push_back(
4116 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4117 ExtnameUndeclaredIdentifiers.push_back(
4118 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4119 ExtnameUndeclaredIdentifiers.push_back(
4120 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4121 }
4122 break;
4123
4124 case SELECTOR_OFFSETS: {
4125 F.SelectorOffsets = (const uint32_t *)Blob.data();
4126 F.LocalNumSelectors = Record[0];
4127 unsigned LocalBaseSelectorID = Record[1];
4128 F.BaseSelectorID = getTotalNumSelectors();
4129
4130 if (F.LocalNumSelectors > 0) {
4131 // Introduce the global -> local mapping for selectors within this
4132 // module.
4133 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4134
4135 // Introduce the local -> global mapping for selectors within this
4136 // module.
4137 F.SelectorRemap.insertOrReplace(
4138 Val: std::make_pair(x&: LocalBaseSelectorID,
4139 y: F.BaseSelectorID - LocalBaseSelectorID));
4140
4141 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4142 }
4143 break;
4144 }
4145
4146 case METHOD_POOL:
4147 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4148 if (Record[0])
4149 F.SelectorLookupTable
4150 = ASTSelectorLookupTable::Create(
4151 Buckets: F.SelectorLookupTableData + Record[0],
4152 Base: F.SelectorLookupTableData,
4153 InfoObj: ASTSelectorLookupTrait(*this, F));
4154 TotalNumMethodPoolEntries += Record[1];
4155 break;
4156
4157 case REFERENCED_SELECTOR_POOL:
4158 if (!Record.empty()) {
4159 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4160 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4161 LocalID: Record[Idx++]));
4162 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4163 getRawEncoding());
4164 }
4165 }
4166 break;
4167
4168 case PP_ASSUME_NONNULL_LOC: {
4169 unsigned Idx = 0;
4170 if (!Record.empty())
4171 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4172 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4173 break;
4174 }
4175
4176 case PP_UNSAFE_BUFFER_USAGE: {
4177 if (!Record.empty()) {
4178 SmallVector<SourceLocation, 64> SrcLocs;
4179 unsigned Idx = 0;
4180 while (Idx < Record.size())
4181 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4182 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4183 }
4184 break;
4185 }
4186
4187 case PP_CONDITIONAL_STACK:
4188 if (!Record.empty()) {
4189 unsigned Idx = 0, End = Record.size() - 1;
4190 bool ReachedEOFWhileSkipping = Record[Idx++];
4191 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4192 if (ReachedEOFWhileSkipping) {
4193 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4194 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4195 bool FoundNonSkipPortion = Record[Idx++];
4196 bool FoundElse = Record[Idx++];
4197 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4198 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4199 args&: FoundElse, args&: ElseLoc);
4200 }
4201 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4202 while (Idx < End) {
4203 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4204 bool WasSkipping = Record[Idx++];
4205 bool FoundNonSkip = Record[Idx++];
4206 bool FoundElse = Record[Idx++];
4207 ConditionalStack.push_back(
4208 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4209 }
4210 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4211 }
4212 break;
4213
4214 case PP_COUNTER_VALUE:
4215 if (!Record.empty() && Listener)
4216 Listener->ReadCounter(M: F, Value: Record[0]);
4217 break;
4218
4219 case FILE_SORTED_DECLS:
4220 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4221 F.NumFileSortedDecls = Record[0];
4222 break;
4223
4224 case SOURCE_LOCATION_OFFSETS: {
4225 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4226 F.LocalNumSLocEntries = Record[0];
4227 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4228 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4229 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4230 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4231 TotalSize: SLocSpaceSize);
4232 if (!F.SLocEntryBaseID) {
4233 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4234 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4235 return llvm::createStringError(EC: std::errc::invalid_argument,
4236 Fmt: "ran out of source locations");
4237 }
4238 // Make our entry in the range map. BaseID is negative and growing, so
4239 // we invert it. Because we invert it, though, we need the other end of
4240 // the range.
4241 unsigned RangeStart =
4242 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4243 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4244 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4245
4246 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4247 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4248 GlobalSLocOffsetMap.insert(
4249 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4250 - SLocSpaceSize,y: &F));
4251
4252 TotalNumSLocEntries += F.LocalNumSLocEntries;
4253 break;
4254 }
4255
4256 case MODULE_OFFSET_MAP:
4257 F.ModuleOffsetMap = Blob;
4258 break;
4259
4260 case SOURCE_MANAGER_LINE_TABLE:
4261 ParseLineTable(F, Record);
4262 break;
4263
4264 case EXT_VECTOR_DECLS:
4265 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4266 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4267 break;
4268
4269 case VTABLE_USES:
4270 if (Record.size() % 3 != 0)
4271 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4272 Fmt: "Invalid VTABLE_USES record");
4273
4274 // Later tables overwrite earlier ones.
4275 // FIXME: Modules will have some trouble with this. This is clearly not
4276 // the right way to do this.
4277 VTableUses.clear();
4278
4279 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4280 VTableUses.push_back(
4281 Elt: {.ID: ReadDeclID(F, Record, Idx),
4282 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4283 .Used: (bool)Record[Idx++]});
4284 }
4285 break;
4286
4287 case PENDING_IMPLICIT_INSTANTIATIONS:
4288
4289 if (Record.size() % 2 != 0)
4290 return llvm::createStringError(
4291 EC: std::errc::illegal_byte_sequence,
4292 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4293
4294 // For standard C++20 module, we will only reads the instantiations
4295 // if it is the main file.
4296 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4297 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4298 PendingInstantiations.push_back(
4299 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4300 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4301 }
4302 }
4303 break;
4304
4305 case SEMA_DECL_REFS:
4306 if (Record.size() != 3)
4307 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4308 Fmt: "Invalid SEMA_DECL_REFS block");
4309 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4310 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4311 break;
4312
4313 case PPD_ENTITIES_OFFSETS: {
4314 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4315 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4316 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4317
4318 unsigned StartingID;
4319 if (!PP.getPreprocessingRecord())
4320 PP.createPreprocessingRecord();
4321 if (!PP.getPreprocessingRecord()->getExternalSource())
4322 PP.getPreprocessingRecord()->SetExternalSource(*this);
4323 StartingID
4324 = PP.getPreprocessingRecord()
4325 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4326 F.BasePreprocessedEntityID = StartingID;
4327
4328 if (F.NumPreprocessedEntities > 0) {
4329 // Introduce the global -> local mapping for preprocessed entities in
4330 // this module.
4331 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4332 }
4333
4334 break;
4335 }
4336
4337 case PPD_SKIPPED_RANGES: {
4338 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4339 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4340 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4341
4342 if (!PP.getPreprocessingRecord())
4343 PP.createPreprocessingRecord();
4344 if (!PP.getPreprocessingRecord()->getExternalSource())
4345 PP.getPreprocessingRecord()->SetExternalSource(*this);
4346 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4347 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4348
4349 if (F.NumPreprocessedSkippedRanges > 0)
4350 GlobalSkippedRangeMap.insert(
4351 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4352 break;
4353 }
4354
4355 case DECL_UPDATE_OFFSETS:
4356 if (Record.size() % 2 != 0)
4357 return llvm::createStringError(
4358 EC: std::errc::illegal_byte_sequence,
4359 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4360 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4361 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4362 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4363
4364 // If we've already loaded the decl, perform the updates when we finish
4365 // loading this block.
4366 if (Decl *D = GetExistingDecl(ID))
4367 PendingUpdateRecords.push_back(
4368 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4369 }
4370 break;
4371
4372 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4373 if (Record.size() % 5 != 0)
4374 return llvm::createStringError(
4375 EC: std::errc::illegal_byte_sequence,
4376 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4377 "file");
4378 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4379 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4380
4381 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4382 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4383 uint64_t LocalLexicalOffset = Record[I++];
4384 uint64_t LexicalOffset =
4385 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4386 uint64_t LocalVisibleOffset = Record[I++];
4387 uint64_t VisibleOffset =
4388 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4389 uint64_t LocalModuleLocalOffset = Record[I++];
4390 uint64_t ModuleLocalOffset =
4391 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4392 uint64_t TULocalLocalOffset = Record[I++];
4393 uint64_t TULocalOffset =
4394 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4395
4396 DelayedNamespaceOffsetMap[ID] = {
4397 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4398
4399 assert(!GetExistingDecl(ID) &&
4400 "We shouldn't load the namespace in the front of delayed "
4401 "namespace lexical and visible block");
4402 }
4403 break;
4404 }
4405
4406 case RELATED_DECLS_MAP:
4407 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4408 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4409 auto &RelatedDecls = RelatedDeclsMap[ID];
4410 unsigned NN = Record[I++];
4411 RelatedDecls.reserve(N: NN);
4412 for (unsigned II = 0; II < NN; II++)
4413 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4414 }
4415 break;
4416
4417 case OBJC_CATEGORIES_MAP:
4418 if (F.LocalNumObjCCategoriesInMap != 0)
4419 return llvm::createStringError(
4420 EC: std::errc::illegal_byte_sequence,
4421 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4422
4423 F.LocalNumObjCCategoriesInMap = Record[0];
4424 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4425 break;
4426
4427 case OBJC_CATEGORIES:
4428 F.ObjCCategories.swap(RHS&: Record);
4429 break;
4430
4431 case CUDA_SPECIAL_DECL_REFS:
4432 // Later tables overwrite earlier ones.
4433 // FIXME: Modules will have trouble with this.
4434 CUDASpecialDeclRefs.clear();
4435 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4436 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4437 break;
4438
4439 case HEADER_SEARCH_TABLE:
4440 F.HeaderFileInfoTableData = Blob.data();
4441 F.LocalNumHeaderFileInfos = Record[1];
4442 if (Record[0]) {
4443 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4444 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4445 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4446 InfoObj: HeaderFileInfoTrait(*this, F));
4447
4448 PP.getHeaderSearchInfo().SetExternalSource(this);
4449 if (!PP.getHeaderSearchInfo().getExternalLookup())
4450 PP.getHeaderSearchInfo().SetExternalLookup(this);
4451 }
4452 break;
4453
4454 case FP_PRAGMA_OPTIONS:
4455 // Later tables overwrite earlier ones.
4456 FPPragmaOptions.swap(RHS&: Record);
4457 break;
4458
4459 case DECLS_WITH_EFFECTS_TO_VERIFY:
4460 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4461 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4462 break;
4463
4464 case OPENCL_EXTENSIONS:
4465 for (unsigned I = 0, E = Record.size(); I != E; ) {
4466 auto Name = ReadString(Record, Idx&: I);
4467 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4468 OptInfo.Supported = Record[I++] != 0;
4469 OptInfo.Enabled = Record[I++] != 0;
4470 OptInfo.WithPragma = Record[I++] != 0;
4471 OptInfo.Avail = Record[I++];
4472 OptInfo.Core = Record[I++];
4473 OptInfo.Opt = Record[I++];
4474 }
4475 break;
4476
4477 case TENTATIVE_DEFINITIONS:
4478 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4479 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4480 break;
4481
4482 case KNOWN_NAMESPACES:
4483 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4484 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4485 break;
4486
4487 case UNDEFINED_BUT_USED:
4488 if (Record.size() % 2 != 0)
4489 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4490 Fmt: "invalid undefined-but-used record");
4491 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4492 UndefinedButUsed.push_back(
4493 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4494 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4495 }
4496 break;
4497
4498 case DELETE_EXPRS_TO_ANALYZE:
4499 for (unsigned I = 0, N = Record.size(); I != N;) {
4500 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4501 const uint64_t Count = Record[I++];
4502 DelayedDeleteExprs.push_back(Elt: Count);
4503 for (uint64_t C = 0; C < Count; ++C) {
4504 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4505 bool IsArrayForm = Record[I++] == 1;
4506 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4507 }
4508 }
4509 break;
4510
4511 case VTABLES_TO_EMIT:
4512 if (F.Kind == MK_MainFile ||
4513 getContext().getLangOpts().BuildingPCHWithObjectFile)
4514 for (unsigned I = 0, N = Record.size(); I != N;)
4515 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4516 break;
4517
4518 case IMPORTED_MODULES:
4519 if (!F.isModule()) {
4520 // If we aren't loading a module (which has its own exports), make
4521 // all of the imported modules visible.
4522 // FIXME: Deal with macros-only imports.
4523 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4524 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4525 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4526 if (GlobalID) {
4527 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4528 if (DeserializationListener)
4529 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4530 }
4531 }
4532 }
4533 break;
4534
4535 case MACRO_OFFSET: {
4536 if (F.LocalNumMacros != 0)
4537 return llvm::createStringError(
4538 EC: std::errc::illegal_byte_sequence,
4539 Fmt: "duplicate MACRO_OFFSET record in AST file");
4540 F.MacroOffsets = (const uint32_t *)Blob.data();
4541 F.LocalNumMacros = Record[0];
4542 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4543 F.BaseMacroID = getTotalNumMacros();
4544
4545 if (F.LocalNumMacros > 0)
4546 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4547 break;
4548 }
4549
4550 case LATE_PARSED_TEMPLATE:
4551 LateParsedTemplates.emplace_back(
4552 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4553 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4554 break;
4555
4556 case OPTIMIZE_PRAGMA_OPTIONS:
4557 if (Record.size() != 1)
4558 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4559 Fmt: "invalid pragma optimize record");
4560 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4561 break;
4562
4563 case MSSTRUCT_PRAGMA_OPTIONS:
4564 if (Record.size() != 1)
4565 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4566 Fmt: "invalid pragma ms_struct record");
4567 PragmaMSStructState = Record[0];
4568 break;
4569
4570 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4571 if (Record.size() != 2)
4572 return llvm::createStringError(
4573 EC: std::errc::illegal_byte_sequence,
4574 Fmt: "invalid pragma pointers to members record");
4575 PragmaMSPointersToMembersState = Record[0];
4576 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4577 break;
4578
4579 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4580 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4581 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4582 break;
4583
4584 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4585 if (Record.size() != 1)
4586 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4587 Fmt: "invalid cuda pragma options record");
4588 ForceHostDeviceDepth = Record[0];
4589 break;
4590
4591 case ALIGN_PACK_PRAGMA_OPTIONS: {
4592 if (Record.size() < 3)
4593 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4594 Fmt: "invalid pragma pack record");
4595 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4596 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4597 unsigned NumStackEntries = Record[2];
4598 unsigned Idx = 3;
4599 // Reset the stack when importing a new module.
4600 PragmaAlignPackStack.clear();
4601 for (unsigned I = 0; I < NumStackEntries; ++I) {
4602 PragmaAlignPackStackEntry Entry;
4603 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4604 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4605 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4606 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4607 Entry.SlotLabel = PragmaAlignPackStrings.back();
4608 PragmaAlignPackStack.push_back(Elt: Entry);
4609 }
4610 break;
4611 }
4612
4613 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4614 if (Record.size() < 3)
4615 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4616 Fmt: "invalid pragma float control record");
4617 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4618 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4619 unsigned NumStackEntries = Record[2];
4620 unsigned Idx = 3;
4621 // Reset the stack when importing a new module.
4622 FpPragmaStack.clear();
4623 for (unsigned I = 0; I < NumStackEntries; ++I) {
4624 FpPragmaStackEntry Entry;
4625 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4626 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4627 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4628 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4629 Entry.SlotLabel = FpPragmaStrings.back();
4630 FpPragmaStack.push_back(Elt: Entry);
4631 }
4632 break;
4633 }
4634
4635 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4636 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4637 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4638 break;
4639
4640 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4641 unsigned NumRecords = Record.front();
4642 // Last record which is used to keep number of valid records.
4643 if (Record.size() - 1 != NumRecords)
4644 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4645 Fmt: "invalid rvv intrinsic pragma record");
4646
4647 if (RISCVVecIntrinsicPragma.empty())
4648 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4649 // There might be multiple precompiled modules imported, we need to union
4650 // them all.
4651 for (unsigned i = 0; i < NumRecords; ++i)
4652 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4653 break;
4654 }
4655 }
4656 }
4657}
4658
4659void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4660 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4661
4662 // Additional remapping information.
4663 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4664 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4665 F.ModuleOffsetMap = StringRef();
4666
4667 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4668 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4669 RemapBuilder SelectorRemap(F.SelectorRemap);
4670
4671 auto &ImportedModuleVector = F.TransitiveImports;
4672 assert(ImportedModuleVector.empty());
4673
4674 while (Data < DataEnd) {
4675 // FIXME: Looking up dependency modules by filename is horrible. Let's
4676 // start fixing this with prebuilt, explicit and implicit modules and see
4677 // how it goes...
4678 using namespace llvm::support;
4679 ModuleKind Kind = static_cast<ModuleKind>(
4680 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4681 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4682 StringRef Name = StringRef((const char*)Data, Len);
4683 Data += Len;
4684 ModuleFile *OM =
4685 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4686 Kind == MK_ImplicitModule
4687 ? ModuleMgr.lookupByModuleName(ModName: Name)
4688 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4689 if (!OM)
4690 OM = ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeInMemory(Name));
4691 if (!OM) {
4692 std::string Msg = "refers to unknown module, cannot find ";
4693 Msg.append(str: std::string(Name));
4694 Error(Msg);
4695 return;
4696 }
4697
4698 ImportedModuleVector.push_back(Elt: OM);
4699
4700 uint32_t SubmoduleIDOffset =
4701 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4702 uint32_t SelectorIDOffset =
4703 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4704
4705 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4706 RemapBuilder &Remap) {
4707 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4708 if (Offset != None)
4709 Remap.insert(Val: std::make_pair(x&: Offset,
4710 y: static_cast<int>(BaseOffset - Offset)));
4711 };
4712
4713 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4714 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4715 }
4716}
4717
4718ASTReader::ASTReadResult
4719ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4720 const ModuleFile *ImportedBy,
4721 unsigned ClientLoadCapabilities) {
4722 unsigned Idx = 0;
4723 F.ModuleMapPath = ReadPath(F, Record, Idx);
4724
4725 // Try to resolve ModuleName in the current header search context and
4726 // verify that it is found in the same module map file as we saved. If the
4727 // top-level AST file is a main file, skip this check because there is no
4728 // usable header search context.
4729 assert(!F.ModuleName.empty() &&
4730 "MODULE_NAME should come before MODULE_MAP_FILE");
4731 auto [MaybeM, IgnoreError] =
4732 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4733 if (MaybeM.has_value()) {
4734 // An implicitly-loaded module file should have its module listed in some
4735 // module map file that we've already loaded.
4736 Module *M = MaybeM.value();
4737 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4738 OptionalFileEntryRef ModMap =
4739 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4740 if (!IgnoreError && !ModMap) {
4741 if (M && M->Directory)
4742 Diag(DiagID: diag::remark_module_relocated)
4743 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4744
4745 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4746 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4747 // This module was defined by an imported (explicit) module.
4748 Diag(DiagID: diag::err_module_file_conflict)
4749 << F.ModuleName << F.FileName << *ASTFileName;
4750 // TODO: Add a note with the module map paths if they differ.
4751 } else {
4752 // This module was built with a different module map.
4753 Diag(DiagID: diag::err_imported_module_not_found)
4754 << F.ModuleName << F.FileName
4755 << (ImportedBy ? ImportedBy->FileName.str() : "")
4756 << F.ModuleMapPath << !ImportedBy;
4757 // In case it was imported by a PCH, there's a chance the user is
4758 // just missing to include the search path to the directory containing
4759 // the modulemap.
4760 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4761 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4762 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4763 }
4764 }
4765 return OutOfDate;
4766 }
4767
4768 assert(M && M->Name == F.ModuleName && "found module with different name");
4769
4770 // Check the primary module map file.
4771 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4772 if (!StoredModMap || *StoredModMap != ModMap) {
4773 assert(ModMap && "found module is missing module map file");
4774 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4775 "top-level import should be verified");
4776 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4777 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4778 Diag(DiagID: diag::err_imported_module_modmap_changed)
4779 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4780 << ModMap->getName() << F.ModuleMapPath << NotImported;
4781 return OutOfDate;
4782 }
4783
4784 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4785 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4786 // FIXME: we should use input files rather than storing names.
4787 std::string Filename = ReadPath(F, Record, Idx);
4788 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4789 if (!SF) {
4790 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4791 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4792 return OutOfDate;
4793 }
4794 AdditionalStoredMaps.insert(V: *SF);
4795 }
4796
4797 // Check any additional module map files (e.g. module.private.modulemap)
4798 // that are not in the pcm.
4799 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4800 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4801 // Remove files that match
4802 // Note: SmallPtrSet::erase is really remove
4803 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4804 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4805 Diag(DiagID: diag::err_module_different_modmap)
4806 << F.ModuleName << /*new*/0 << ModMap.getName();
4807 return OutOfDate;
4808 }
4809 }
4810 }
4811
4812 // Check any additional module map files that are in the pcm, but not
4813 // found in header search. Cases that match are already removed.
4814 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4815 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4816 Diag(DiagID: diag::err_module_different_modmap)
4817 << F.ModuleName << /*not new*/1 << ModMap.getName();
4818 return OutOfDate;
4819 }
4820 }
4821
4822 if (Listener)
4823 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4824 return Success;
4825}
4826
4827/// Move the given method to the back of the global list of methods.
4828static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4829 // Find the entry for this selector in the method pool.
4830 SemaObjC::GlobalMethodPool::iterator Known =
4831 S.ObjC().MethodPool.find(Val: Method->getSelector());
4832 if (Known == S.ObjC().MethodPool.end())
4833 return;
4834
4835 // Retrieve the appropriate method list.
4836 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4837 : Known->second.second;
4838 bool Found = false;
4839 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4840 if (!Found) {
4841 if (List->getMethod() == Method) {
4842 Found = true;
4843 } else {
4844 // Keep searching.
4845 continue;
4846 }
4847 }
4848
4849 if (List->getNext())
4850 List->setMethod(List->getNext()->getMethod());
4851 else
4852 List->setMethod(Method);
4853 }
4854}
4855
4856void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4857 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4858 for (Decl *D : Names) {
4859 bool wasHidden = !D->isUnconditionallyVisible();
4860 D->setVisibleDespiteOwningModule();
4861
4862 if (wasHidden && SemaObj) {
4863 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4864 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4865 }
4866 }
4867 }
4868}
4869
4870void ASTReader::makeModuleVisible(Module *Mod,
4871 Module::NameVisibilityKind NameVisibility,
4872 SourceLocation ImportLoc) {
4873 llvm::SmallPtrSet<Module *, 4> Visited;
4874 SmallVector<Module *, 4> Stack;
4875 Stack.push_back(Elt: Mod);
4876 while (!Stack.empty()) {
4877 Mod = Stack.pop_back_val();
4878
4879 if (NameVisibility <= Mod->NameVisibility) {
4880 // This module already has this level of visibility (or greater), so
4881 // there is nothing more to do.
4882 continue;
4883 }
4884
4885 if (Mod->isUnimportable()) {
4886 // Modules that aren't importable cannot be made visible.
4887 continue;
4888 }
4889
4890 // Update the module's name visibility.
4891 Mod->NameVisibility = NameVisibility;
4892
4893 // If we've already deserialized any names from this module,
4894 // mark them as visible.
4895 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4896 if (Hidden != HiddenNamesMap.end()) {
4897 auto HiddenNames = std::move(*Hidden);
4898 HiddenNamesMap.erase(I: Hidden);
4899 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4900 assert(!HiddenNamesMap.contains(Mod) &&
4901 "making names visible added hidden names");
4902 }
4903
4904 // Push any exported modules onto the stack to be marked as visible.
4905 SmallVector<Module *, 16> Exports;
4906 Mod->getExportedModules(Exported&: Exports);
4907 for (SmallVectorImpl<Module *>::iterator
4908 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4909 Module *Exported = *I;
4910 if (Visited.insert(Ptr: Exported).second)
4911 Stack.push_back(Elt: Exported);
4912 }
4913 }
4914}
4915
4916/// We've merged the definition \p MergedDef into the existing definition
4917/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4918/// visible.
4919void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4920 NamedDecl *MergedDef) {
4921 if (!Def->isUnconditionallyVisible()) {
4922 // If MergedDef is visible or becomes visible, make the definition visible.
4923 if (MergedDef->isUnconditionallyVisible())
4924 Def->setVisibleDespiteOwningModule();
4925 else {
4926 getContext().mergeDefinitionIntoModule(
4927 ND: Def, M: MergedDef->getImportedOwningModule(),
4928 /*NotifyListeners*/ false);
4929 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4930 }
4931 }
4932}
4933
4934bool ASTReader::loadGlobalIndex() {
4935 if (GlobalIndex)
4936 return false;
4937
4938 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4939 !PP.getLangOpts().Modules)
4940 return true;
4941
4942 // Try to load the global index.
4943 TriedLoadingGlobalIndex = true;
4944 StringRef SpecificModuleCachePath =
4945 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4946 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4947 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4948 if (llvm::Error Err = std::move(Result.second)) {
4949 assert(!Result.first);
4950 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4951 return true;
4952 }
4953
4954 GlobalIndex.reset(p: Result.first);
4955 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4956 return false;
4957}
4958
4959bool ASTReader::isGlobalIndexUnavailable() const {
4960 return PP.getLangOpts().Modules && UseGlobalIndex &&
4961 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4962}
4963
4964/// Given a cursor at the start of an AST file, scan ahead and drop the
4965/// cursor into the start of the given block ID, returning false on success and
4966/// true on failure.
4967static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4968 while (true) {
4969 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4970 if (!MaybeEntry) {
4971 // FIXME this drops errors on the floor.
4972 consumeError(Err: MaybeEntry.takeError());
4973 return true;
4974 }
4975 llvm::BitstreamEntry Entry = MaybeEntry.get();
4976
4977 switch (Entry.Kind) {
4978 case llvm::BitstreamEntry::Error:
4979 case llvm::BitstreamEntry::EndBlock:
4980 return true;
4981
4982 case llvm::BitstreamEntry::Record:
4983 // Ignore top-level records.
4984 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4985 break;
4986 else {
4987 // FIXME this drops errors on the floor.
4988 consumeError(Err: Skipped.takeError());
4989 return true;
4990 }
4991
4992 case llvm::BitstreamEntry::SubBlock:
4993 if (Entry.ID == BlockID) {
4994 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4995 // FIXME this drops the error on the floor.
4996 consumeError(Err: std::move(Err));
4997 return true;
4998 }
4999 // Found it!
5000 return false;
5001 }
5002
5003 if (llvm::Error Err = Cursor.SkipBlock()) {
5004 // FIXME this drops the error on the floor.
5005 consumeError(Err: std::move(Err));
5006 return true;
5007 }
5008 }
5009 }
5010}
5011
5012ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
5013 ModuleKind Type,
5014 SourceLocation ImportLoc,
5015 unsigned ClientLoadCapabilities,
5016 ModuleFile **NewLoadedModuleFile) {
5017 llvm::TimeTraceScope scope("ReadAST", FileName);
5018
5019 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
5020 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
5021 CurrentDeserializingModuleKind, Type);
5022
5023 // Defer any pending actions until we get to the end of reading the AST file.
5024 Deserializing AnASTFile(this);
5025
5026 // Bump the generation number.
5027 unsigned PreviousGeneration = 0;
5028 if (ContextObj)
5029 PreviousGeneration = incrementGeneration(C&: *ContextObj);
5030
5031 unsigned NumModules = ModuleMgr.size();
5032 SmallVector<ImportedModule, 4> Loaded;
5033 if (ASTReadResult ReadResult =
5034 ReadASTCore(FileName, Type, ImportLoc,
5035 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
5036 ClientLoadCapabilities)) {
5037 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
5038
5039 // If we find that any modules are unusable, the global index is going
5040 // to be out-of-date. Just remove it.
5041 GlobalIndex.reset();
5042 ModuleMgr.setGlobalIndex(nullptr);
5043 return ReadResult;
5044 }
5045
5046 if (NewLoadedModuleFile && !Loaded.empty())
5047 *NewLoadedModuleFile = Loaded.back().Mod;
5048
5049 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
5050 // remove modules from this point. Various fields are updated during reading
5051 // the AST block and removing the modules would result in dangling pointers.
5052 // They are generally only incidentally dereferenced, ie. a binary search
5053 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
5054 // be dereferenced but it wouldn't actually be used.
5055
5056 // Load the AST blocks of all of the modules that we loaded. We can still
5057 // hit errors parsing the ASTs at this point.
5058 for (ImportedModule &M : Loaded) {
5059 ModuleFile &F = *M.Mod;
5060 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
5061
5062 // Read the AST block.
5063 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
5064 Error(Err: std::move(Err));
5065 return Failure;
5066 }
5067
5068 // The AST block should always have a definition for the main module.
5069 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5070 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5071 return Failure;
5072 }
5073
5074 // Read the extension blocks.
5075 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5076 if (llvm::Error Err = ReadExtensionBlock(F)) {
5077 Error(Err: std::move(Err));
5078 return Failure;
5079 }
5080 }
5081
5082 // Once read, set the ModuleFile bit base offset and update the size in
5083 // bits of all files we've seen.
5084 F.GlobalBitOffset = TotalModulesSizeInBits;
5085 TotalModulesSizeInBits += F.SizeInBits;
5086 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5087 }
5088
5089 // Preload source locations and interesting indentifiers.
5090 for (ImportedModule &M : Loaded) {
5091 ModuleFile &F = *M.Mod;
5092
5093 // Map the original source file ID into the ID space of the current
5094 // compilation.
5095 if (F.OriginalSourceFileID.isValid())
5096 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5097
5098 for (auto Offset : F.PreloadIdentifierOffsets) {
5099 const unsigned char *Data = F.IdentifierTableData + Offset;
5100
5101 ASTIdentifierLookupTrait Trait(*this, F);
5102 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5103 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5104
5105 IdentifierInfo *II;
5106 if (!PP.getLangOpts().CPlusPlus) {
5107 // Identifiers present in both the module file and the importing
5108 // instance are marked out-of-date so that they can be deserialized
5109 // on next use via ASTReader::updateOutOfDateIdentifier().
5110 // Identifiers present in the module file but not in the importing
5111 // instance are ignored for now, preventing growth of the identifier
5112 // table. They will be deserialized on first use via ASTReader::get().
5113 auto It = PP.getIdentifierTable().find(Name: Key);
5114 if (It == PP.getIdentifierTable().end())
5115 continue;
5116 II = It->second;
5117 } else {
5118 // With C++ modules, not many identifiers are considered interesting.
5119 // All identifiers in the module file can be placed into the identifier
5120 // table of the importing instance and marked as out-of-date. This makes
5121 // ASTReader::get() a no-op, and deserialization will take place on
5122 // first/next use via ASTReader::updateOutOfDateIdentifier().
5123 II = &PP.getIdentifierTable().getOwn(Name: Key);
5124 }
5125
5126 II->setOutOfDate(true);
5127
5128 // Mark this identifier as being from an AST file so that we can track
5129 // whether we need to serialize it.
5130 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5131
5132 // Associate the ID with the identifier so that the writer can reuse it.
5133 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5134 SetIdentifierInfo(ID, II);
5135 }
5136 }
5137
5138 // Builtins and library builtins have already been initialized. Mark all
5139 // identifiers as out-of-date, so that they are deserialized on first use.
5140 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5141 for (auto &Id : PP.getIdentifierTable())
5142 Id.second->setOutOfDate(true);
5143
5144 // Mark selectors as out of date.
5145 for (const auto &Sel : SelectorGeneration)
5146 SelectorOutOfDate[Sel.first] = true;
5147
5148 // Setup the import locations and notify the module manager that we've
5149 // committed to these module files.
5150 for (ImportedModule &M : Loaded) {
5151 ModuleFile &F = *M.Mod;
5152
5153 ModuleMgr.moduleFileAccepted(MF: &F);
5154
5155 // Set the import location.
5156 F.DirectImportLoc = ImportLoc;
5157 // FIXME: We assume that locations from PCH / preamble do not need
5158 // any translation.
5159 if (!M.ImportedBy)
5160 F.ImportLoc = M.ImportLoc;
5161 else
5162 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5163 }
5164
5165 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5166 // Might be unnecessary as use declarations are only used to build the
5167 // module itself.
5168
5169 if (ContextObj)
5170 InitializeContext();
5171
5172 if (SemaObj)
5173 UpdateSema();
5174
5175 if (DeserializationListener)
5176 DeserializationListener->ReaderInitialized(Reader: this);
5177
5178 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5179 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5180 // If this AST file is a precompiled preamble, then set the
5181 // preamble file ID of the source manager to the file source file
5182 // from which the preamble was built.
5183 if (Type == MK_Preamble) {
5184 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5185 } else if (Type == MK_MainFile) {
5186 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5187 }
5188 }
5189
5190 // For any Objective-C class definitions we have already loaded, make sure
5191 // that we load any additional categories.
5192 if (ContextObj) {
5193 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5194 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5195 D: ObjCClassesLoaded[I], PreviousGeneration);
5196 }
5197 }
5198
5199 const HeaderSearchOptions &HSOpts =
5200 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5201 if (HSOpts.ModulesValidateOncePerBuildSession) {
5202 // Now we are certain that the module and all modules it depends on are
5203 // up-to-date. For implicitly-built module files, ensure the corresponding
5204 // timestamp files are up-to-date in this build session.
5205 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5206 ImportedModule &M = Loaded[I];
5207 if (M.Mod->Kind == MK_ImplicitModule &&
5208 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5209 getModuleManager().getModuleCache().updateModuleTimestamp(
5210 ModuleFilename: M.Mod->FileName);
5211 }
5212 }
5213
5214 return Success;
5215}
5216
5217static ASTFileSignature readASTFileSignature(StringRef PCH);
5218
5219/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5220static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5221 // FIXME checking magic headers is done in other places such as
5222 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5223 // always done the same. Unify it all with a helper.
5224 if (!Stream.canSkipToPos(pos: 4))
5225 return llvm::createStringError(
5226 EC: std::errc::illegal_byte_sequence,
5227 Fmt: "file too small to contain precompiled file magic");
5228 for (unsigned C : {'C', 'P', 'C', 'H'})
5229 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5230 if (Res.get() != C)
5231 return llvm::createStringError(
5232 EC: std::errc::illegal_byte_sequence,
5233 Fmt: "file doesn't start with precompiled file magic");
5234 } else
5235 return Res.takeError();
5236 return llvm::Error::success();
5237}
5238
5239static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5240 switch (Kind) {
5241 case MK_PCH:
5242 return 0; // PCH
5243 case MK_ImplicitModule:
5244 case MK_ExplicitModule:
5245 case MK_PrebuiltModule:
5246 return 1; // module
5247 case MK_MainFile:
5248 case MK_Preamble:
5249 return 2; // main source file
5250 }
5251 llvm_unreachable("unknown module kind");
5252}
5253
5254ASTReader::ASTReadResult ASTReader::ReadASTCore(
5255 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5256 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5257 off_t ExpectedSize, time_t ExpectedModTime,
5258 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5259 auto Result = ModuleMgr.addModule(
5260 FileName, Type, ImportLoc, ImportedBy, Generation: getGeneration(), ExpectedSize,
5261 ExpectedModTime, ExpectedSignature, ReadSignature: readASTFileSignature);
5262 ModuleFile *M = Result.getModule();
5263
5264 switch (Result.getKind()) {
5265 case AddModuleResult::AlreadyLoaded: {
5266 Diag(DiagID: diag::remark_module_import)
5267 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5268 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5269 return Success;
5270 }
5271
5272 case AddModuleResult::NewlyLoaded:
5273 // Load module file below.
5274 break;
5275
5276 case AddModuleResult::Missing:
5277 // The module file was missing; if the client can handle that, return
5278 // it.
5279 if (ClientLoadCapabilities & ARR_Missing)
5280 return Missing;
5281
5282 // Otherwise, return an error.
5283 Diag(DiagID: diag::err_ast_file_not_found)
5284 << moduleKindForDiagnostic(Kind: Type) << FileName;
5285 if (!Result.getBufferError().empty())
5286 Diag(DiagID: diag::note_ast_file_buffer_failed) << Result.getBufferError();
5287 return Failure;
5288
5289 case AddModuleResult::OutOfDate:
5290 // We couldn't load the module file because it is out-of-date. If the
5291 // client can handle out-of-date, return it.
5292 if (ClientLoadCapabilities & ARR_OutOfDate)
5293 return OutOfDate;
5294
5295 // Otherwise, return an error.
5296 Diag(DiagID: diag::err_ast_file_out_of_date)
5297 << moduleKindForDiagnostic(Kind: Type) << FileName;
5298 for (const auto &C : Result.getChanges()) {
5299 Diag(DiagID: diag::note_fe_ast_file_modified)
5300 << C.Kind << (C.Old && C.New) << llvm::itostr(X: C.Old.value_or(u: 0))
5301 << llvm::itostr(X: C.New.value_or(u: 0));
5302 }
5303 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
5304 << Result.getValidationStatus();
5305 if (!Result.getSignatureError().empty())
5306 Diag(DiagID: diag::note_ast_file_signature_failed) << Result.getSignatureError();
5307 return Failure;
5308
5309 case AddModuleResult::None:
5310 llvm_unreachable("Unexpected value from adding module.");
5311 }
5312
5313 assert(M && "Missing module file");
5314
5315 bool ShouldFinalizePCM = false;
5316 llvm::scope_exit FinalizeOrDropPCM([&]() {
5317 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5318 if (ShouldFinalizePCM)
5319 MC.finalizePCM(Filename: FileName);
5320 else
5321 MC.tryToDropPCM(Filename: FileName);
5322 });
5323 ModuleFile &F = *M;
5324 BitstreamCursor &Stream = F.Stream;
5325 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5326 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5327
5328 // Sniff for the signature.
5329 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5330 Diag(DiagID: diag::err_ast_file_invalid)
5331 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5332 return Failure;
5333 }
5334
5335 // This is used for compatibility with older PCH formats.
5336 bool HaveReadControlBlock = false;
5337 while (true) {
5338 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5339 if (!MaybeEntry) {
5340 Error(Err: MaybeEntry.takeError());
5341 return Failure;
5342 }
5343 llvm::BitstreamEntry Entry = MaybeEntry.get();
5344
5345 switch (Entry.Kind) {
5346 case llvm::BitstreamEntry::Error:
5347 case llvm::BitstreamEntry::Record:
5348 case llvm::BitstreamEntry::EndBlock:
5349 Error(Msg: "invalid record at top-level of AST file");
5350 return Failure;
5351
5352 case llvm::BitstreamEntry::SubBlock:
5353 break;
5354 }
5355
5356 switch (Entry.ID) {
5357 case CONTROL_BLOCK_ID:
5358 HaveReadControlBlock = true;
5359 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5360 case Success:
5361 // Check that we didn't try to load a non-module AST file as a module.
5362 //
5363 // FIXME: Should we also perform the converse check? Loading a module as
5364 // a PCH file sort of works, but it's a bit wonky.
5365 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5366 Type == MK_PrebuiltModule) &&
5367 F.ModuleName.empty()) {
5368 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5369 if (Result != OutOfDate ||
5370 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5371 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5372 return Result;
5373 }
5374 break;
5375
5376 case Failure: return Failure;
5377 case Missing: return Missing;
5378 case OutOfDate: return OutOfDate;
5379 case VersionMismatch: return VersionMismatch;
5380 case ConfigurationMismatch: return ConfigurationMismatch;
5381 case HadErrors: return HadErrors;
5382 }
5383 break;
5384
5385 case AST_BLOCK_ID:
5386 if (!HaveReadControlBlock) {
5387 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5388 Diag(DiagID: diag::err_ast_file_version_too_old)
5389 << moduleKindForDiagnostic(Kind: Type) << FileName;
5390 return VersionMismatch;
5391 }
5392
5393 // Record that we've loaded this module.
5394 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5395 ShouldFinalizePCM = true;
5396 return Success;
5397
5398 default:
5399 if (llvm::Error Err = Stream.SkipBlock()) {
5400 Error(Err: std::move(Err));
5401 return Failure;
5402 }
5403 break;
5404 }
5405 }
5406
5407 llvm_unreachable("unexpected break; expected return");
5408}
5409
5410ASTReader::ASTReadResult
5411ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5412 unsigned ClientLoadCapabilities) {
5413 const HeaderSearchOptions &HSOpts =
5414 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5415 bool AllowCompatibleConfigurationMismatch =
5416 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5417 bool DisableValidation = shouldDisableValidationForFile(M: F);
5418
5419 ASTReadResult Result = readUnhashedControlBlockImpl(
5420 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5421 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5422 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5423
5424 // If F was directly imported by another module, it's implicitly validated by
5425 // the importing module.
5426 if (DisableValidation || WasImportedBy ||
5427 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5428 return Success;
5429
5430 if (Result == Failure) {
5431 Error(Msg: "malformed block record in AST file");
5432 return Failure;
5433 }
5434
5435 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5436 // If this module has already been finalized in the ModuleCache, we're stuck
5437 // with it; we can only load a single version of each module.
5438 //
5439 // This can happen when a module is imported in two contexts: in one, as a
5440 // user module; in another, as a system module (due to an import from
5441 // another module marked with the [system] flag). It usually indicates a
5442 // bug in the module map: this module should also be marked with [system].
5443 //
5444 // If -Wno-system-headers (the default), and the first import is as a
5445 // system module, then validation will fail during the as-user import,
5446 // since -Werror flags won't have been validated. However, it's reasonable
5447 // to treat this consistently as a system module.
5448 //
5449 // If -Wsystem-headers, the PCM on disk was built with
5450 // -Wno-system-headers, and the first import is as a user module, then
5451 // validation will fail during the as-system import since the PCM on disk
5452 // doesn't guarantee that -Werror was respected. However, the -Werror
5453 // flags were checked during the initial as-user import.
5454 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5455 Filename: F.FileName)) {
5456 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5457 return Success;
5458 }
5459 }
5460
5461 return Result;
5462}
5463
5464ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5465 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5466 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5467 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5468 // Initialize a stream.
5469 BitstreamCursor Stream(StreamData);
5470
5471 // Sniff for the signature.
5472 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5473 // FIXME this drops the error on the floor.
5474 consumeError(Err: std::move(Err));
5475 return Failure;
5476 }
5477
5478 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5479 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5480 return Failure;
5481
5482 // Read all of the records in the options block.
5483 RecordData Record;
5484 ASTReadResult Result = Success;
5485 while (true) {
5486 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5487 if (!MaybeEntry) {
5488 // FIXME this drops the error on the floor.
5489 consumeError(Err: MaybeEntry.takeError());
5490 return Failure;
5491 }
5492 llvm::BitstreamEntry Entry = MaybeEntry.get();
5493
5494 switch (Entry.Kind) {
5495 case llvm::BitstreamEntry::Error:
5496 case llvm::BitstreamEntry::SubBlock:
5497 return Failure;
5498
5499 case llvm::BitstreamEntry::EndBlock:
5500 return Result;
5501
5502 case llvm::BitstreamEntry::Record:
5503 // The interesting case.
5504 break;
5505 }
5506
5507 // Read and process a record.
5508 Record.clear();
5509 StringRef Blob;
5510 Expected<unsigned> MaybeRecordType =
5511 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5512 if (!MaybeRecordType) {
5513 // FIXME this drops the error.
5514 return Failure;
5515 }
5516 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5517 case SIGNATURE:
5518 if (F) {
5519 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5520 assert(F->Signature != ASTFileSignature::createDummy() &&
5521 "Dummy AST file signature not backpatched in ASTWriter.");
5522 }
5523 break;
5524 case AST_BLOCK_HASH:
5525 if (F) {
5526 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5527 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5528 "Dummy AST block hash not backpatched in ASTWriter.");
5529 }
5530 break;
5531 case DIAGNOSTIC_OPTIONS: {
5532 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5533 if (Listener && ValidateDiagnosticOptions &&
5534 !AllowCompatibleConfigurationMismatch &&
5535 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5536 Result = OutOfDate; // Don't return early. Read the signature.
5537 break;
5538 }
5539 case HEADER_SEARCH_PATHS: {
5540 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5541 if (Listener && !AllowCompatibleConfigurationMismatch &&
5542 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5543 Result = ConfigurationMismatch;
5544 break;
5545 }
5546 case DIAG_PRAGMA_MAPPINGS:
5547 if (!F)
5548 break;
5549 if (F->PragmaDiagMappings.empty())
5550 F->PragmaDiagMappings.swap(RHS&: Record);
5551 else
5552 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5553 From: Record.begin(), To: Record.end());
5554 break;
5555 case HEADER_SEARCH_ENTRY_USAGE:
5556 if (F)
5557 F->SearchPathUsage = ReadBitVector(Record, Blob);
5558 break;
5559 case VFS_USAGE:
5560 if (F)
5561 F->VFSUsage = ReadBitVector(Record, Blob);
5562 break;
5563 }
5564 }
5565}
5566
5567/// Parse a record and blob containing module file extension metadata.
5568static bool parseModuleFileExtensionMetadata(
5569 const SmallVectorImpl<uint64_t> &Record,
5570 StringRef Blob,
5571 ModuleFileExtensionMetadata &Metadata) {
5572 if (Record.size() < 4) return true;
5573
5574 Metadata.MajorVersion = Record[0];
5575 Metadata.MinorVersion = Record[1];
5576
5577 unsigned BlockNameLen = Record[2];
5578 unsigned UserInfoLen = Record[3];
5579
5580 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5581
5582 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5583 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5584 Blob.data() + BlockNameLen + UserInfoLen);
5585 return false;
5586}
5587
5588llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5589 BitstreamCursor &Stream = F.Stream;
5590
5591 RecordData Record;
5592 while (true) {
5593 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5594 if (!MaybeEntry)
5595 return MaybeEntry.takeError();
5596 llvm::BitstreamEntry Entry = MaybeEntry.get();
5597
5598 switch (Entry.Kind) {
5599 case llvm::BitstreamEntry::SubBlock:
5600 if (llvm::Error Err = Stream.SkipBlock())
5601 return Err;
5602 continue;
5603 case llvm::BitstreamEntry::EndBlock:
5604 return llvm::Error::success();
5605 case llvm::BitstreamEntry::Error:
5606 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5607 Fmt: "malformed block record in AST file");
5608 case llvm::BitstreamEntry::Record:
5609 break;
5610 }
5611
5612 Record.clear();
5613 StringRef Blob;
5614 Expected<unsigned> MaybeRecCode =
5615 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5616 if (!MaybeRecCode)
5617 return MaybeRecCode.takeError();
5618 switch (MaybeRecCode.get()) {
5619 case EXTENSION_METADATA: {
5620 ModuleFileExtensionMetadata Metadata;
5621 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5622 return llvm::createStringError(
5623 EC: std::errc::illegal_byte_sequence,
5624 Fmt: "malformed EXTENSION_METADATA in AST file");
5625
5626 // Find a module file extension with this block name.
5627 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5628 if (Known == ModuleFileExtensions.end()) break;
5629
5630 // Form a reader.
5631 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5632 Mod&: F, Stream)) {
5633 F.ExtensionReaders.push_back(x: std::move(Reader));
5634 }
5635
5636 break;
5637 }
5638 }
5639 }
5640
5641 llvm_unreachable("ReadExtensionBlock should return from while loop");
5642}
5643
5644void ASTReader::InitializeContext() {
5645 assert(ContextObj && "no context to initialize");
5646 ASTContext &Context = *ContextObj;
5647
5648 // If there's a listener, notify them that we "read" the translation unit.
5649 if (DeserializationListener)
5650 DeserializationListener->DeclRead(
5651 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5652 D: Context.getTranslationUnitDecl());
5653
5654 // FIXME: Find a better way to deal with collisions between these
5655 // built-in types. Right now, we just ignore the problem.
5656
5657 // Load the special types.
5658 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5659 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5660 if (!Context.CFConstantStringTypeDecl)
5661 Context.setCFConstantStringType(GetType(ID: String));
5662 }
5663
5664 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5665 QualType FileType = GetType(ID: File);
5666 if (FileType.isNull()) {
5667 Error(Msg: "FILE type is NULL");
5668 return;
5669 }
5670
5671 if (!Context.FILEDecl) {
5672 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5673 Context.setFILEDecl(Typedef->getDecl());
5674 else {
5675 const TagType *Tag = FileType->getAs<TagType>();
5676 if (!Tag) {
5677 Error(Msg: "Invalid FILE type in AST file");
5678 return;
5679 }
5680 Context.setFILEDecl(Tag->getDecl());
5681 }
5682 }
5683 }
5684
5685 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5686 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5687 if (Jmp_bufType.isNull()) {
5688 Error(Msg: "jmp_buf type is NULL");
5689 return;
5690 }
5691
5692 if (!Context.jmp_bufDecl) {
5693 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5694 Context.setjmp_bufDecl(Typedef->getDecl());
5695 else {
5696 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5697 if (!Tag) {
5698 Error(Msg: "Invalid jmp_buf type in AST file");
5699 return;
5700 }
5701 Context.setjmp_bufDecl(Tag->getDecl());
5702 }
5703 }
5704 }
5705
5706 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5707 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5708 if (Sigjmp_bufType.isNull()) {
5709 Error(Msg: "sigjmp_buf type is NULL");
5710 return;
5711 }
5712
5713 if (!Context.sigjmp_bufDecl) {
5714 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5715 Context.setsigjmp_bufDecl(Typedef->getDecl());
5716 else {
5717 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5718 assert(Tag && "Invalid sigjmp_buf type in AST file");
5719 Context.setsigjmp_bufDecl(Tag->getDecl());
5720 }
5721 }
5722 }
5723
5724 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5725 if (Context.ObjCIdRedefinitionType.isNull())
5726 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5727 }
5728
5729 if (TypeID ObjCClassRedef =
5730 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5731 if (Context.ObjCClassRedefinitionType.isNull())
5732 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5733 }
5734
5735 if (TypeID ObjCSelRedef =
5736 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5737 if (Context.ObjCSelRedefinitionType.isNull())
5738 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5739 }
5740
5741 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5742 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5743 if (Ucontext_tType.isNull()) {
5744 Error(Msg: "ucontext_t type is NULL");
5745 return;
5746 }
5747
5748 if (!Context.ucontext_tDecl) {
5749 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5750 Context.setucontext_tDecl(Typedef->getDecl());
5751 else {
5752 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5753 assert(Tag && "Invalid ucontext_t type in AST file");
5754 Context.setucontext_tDecl(Tag->getDecl());
5755 }
5756 }
5757 }
5758 }
5759
5760 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5761
5762 // If there were any CUDA special declarations, deserialize them.
5763 if (!CUDASpecialDeclRefs.empty()) {
5764 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5765 Context.setcudaConfigureCallDecl(
5766 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5767 Context.setcudaGetParameterBufferDecl(
5768 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5769 Context.setcudaLaunchDeviceDecl(
5770 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5771 }
5772
5773 // Re-export any modules that were imported by a non-module AST file.
5774 // FIXME: This does not make macro-only imports visible again.
5775 for (auto &Import : PendingImportedModules) {
5776 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5777 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5778 /*ImportLoc=*/Import.ImportLoc);
5779 if (Import.ImportLoc.isValid())
5780 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5781 // This updates visibility for Preprocessor only. For Sema, which can be
5782 // nullptr here, we do the same later, in UpdateSema().
5783 }
5784 }
5785
5786 // Hand off these modules to Sema.
5787 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5788 PendingImportedModules.clear();
5789}
5790
5791void ASTReader::finalizeForWriting() {
5792 // Nothing to do for now.
5793}
5794
5795/// Reads and return the signature record from \p PCH's control block, or
5796/// else returns 0.
5797static ASTFileSignature readASTFileSignature(StringRef PCH) {
5798 BitstreamCursor Stream(PCH);
5799 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5800 // FIXME this drops the error on the floor.
5801 consumeError(Err: std::move(Err));
5802 return ASTFileSignature();
5803 }
5804
5805 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5806 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5807 return ASTFileSignature();
5808
5809 // Scan for SIGNATURE inside the diagnostic options block.
5810 ASTReader::RecordData Record;
5811 while (true) {
5812 Expected<llvm::BitstreamEntry> MaybeEntry =
5813 Stream.advanceSkippingSubblocks();
5814 if (!MaybeEntry) {
5815 // FIXME this drops the error on the floor.
5816 consumeError(Err: MaybeEntry.takeError());
5817 return ASTFileSignature();
5818 }
5819 llvm::BitstreamEntry Entry = MaybeEntry.get();
5820
5821 if (Entry.Kind != llvm::BitstreamEntry::Record)
5822 return ASTFileSignature();
5823
5824 Record.clear();
5825 StringRef Blob;
5826 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5827 if (!MaybeRecord) {
5828 // FIXME this drops the error on the floor.
5829 consumeError(Err: MaybeRecord.takeError());
5830 return ASTFileSignature();
5831 }
5832 if (SIGNATURE == MaybeRecord.get()) {
5833 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5834 assert(Signature != ASTFileSignature::createDummy() &&
5835 "Dummy AST file signature not backpatched in ASTWriter.");
5836 return Signature;
5837 }
5838 }
5839}
5840
5841/// Retrieve the name of the original source file name
5842/// directly from the AST file, without actually loading the AST
5843/// file.
5844std::string ASTReader::getOriginalSourceFile(
5845 const std::string &ASTFileName, FileManager &FileMgr,
5846 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5847 // Open the AST file.
5848 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5849 /*RequiresNullTerminator=*/false,
5850 /*MaybeLimit=*/std::nullopt,
5851 /*IsText=*/false);
5852 if (!Buffer) {
5853 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5854 << ASTFileName << Buffer.getError().message();
5855 return std::string();
5856 }
5857
5858 // Initialize the stream
5859 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5860
5861 // Sniff for the signature.
5862 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5863 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5864 return std::string();
5865 }
5866
5867 // Scan for the CONTROL_BLOCK_ID block.
5868 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5869 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5870 return std::string();
5871 }
5872
5873 // Scan for ORIGINAL_FILE inside the control block.
5874 RecordData Record;
5875 while (true) {
5876 Expected<llvm::BitstreamEntry> MaybeEntry =
5877 Stream.advanceSkippingSubblocks();
5878 if (!MaybeEntry) {
5879 // FIXME this drops errors on the floor.
5880 consumeError(Err: MaybeEntry.takeError());
5881 return std::string();
5882 }
5883 llvm::BitstreamEntry Entry = MaybeEntry.get();
5884
5885 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5886 return std::string();
5887
5888 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5889 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5890 return std::string();
5891 }
5892
5893 Record.clear();
5894 StringRef Blob;
5895 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5896 if (!MaybeRecord) {
5897 // FIXME this drops the errors on the floor.
5898 consumeError(Err: MaybeRecord.takeError());
5899 return std::string();
5900 }
5901 if (ORIGINAL_FILE == MaybeRecord.get())
5902 return Blob.str();
5903 }
5904}
5905
5906namespace {
5907
5908 class SimplePCHValidator : public ASTReaderListener {
5909 const LangOptions &ExistingLangOpts;
5910 const CodeGenOptions &ExistingCGOpts;
5911 const TargetOptions &ExistingTargetOpts;
5912 const PreprocessorOptions &ExistingPPOpts;
5913 const HeaderSearchOptions &ExistingHSOpts;
5914 std::string ExistingSpecificModuleCachePath;
5915 FileManager &FileMgr;
5916 bool StrictOptionMatches;
5917
5918 public:
5919 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5920 const CodeGenOptions &ExistingCGOpts,
5921 const TargetOptions &ExistingTargetOpts,
5922 const PreprocessorOptions &ExistingPPOpts,
5923 const HeaderSearchOptions &ExistingHSOpts,
5924 StringRef ExistingSpecificModuleCachePath,
5925 FileManager &FileMgr, bool StrictOptionMatches)
5926 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5927 ExistingTargetOpts(ExistingTargetOpts),
5928 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5929 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5930 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5931
5932 bool ReadLanguageOptions(const LangOptions &LangOpts,
5933 StringRef ModuleFilename, bool Complain,
5934 bool AllowCompatibleDifferences) override {
5935 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5936 Diags: nullptr, AllowCompatibleDifferences);
5937 }
5938
5939 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5940 StringRef ModuleFilename, bool Complain,
5941 bool AllowCompatibleDifferences) override {
5942 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5943 Diags: nullptr, AllowCompatibleDifferences);
5944 }
5945
5946 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5947 StringRef ModuleFilename, bool Complain,
5948 bool AllowCompatibleDifferences) override {
5949 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
5950 Diags: nullptr, AllowCompatibleDifferences);
5951 }
5952
5953 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5954 StringRef ASTFilename, StringRef ContextHash,
5955 bool Complain) override {
5956 return checkModuleCachePath(
5957 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
5958 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5959 }
5960
5961 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5962 StringRef ModuleFilename, bool ReadMacros,
5963 bool Complain,
5964 std::string &SuggestedPredefines) override {
5965 return checkPreprocessorOptions(
5966 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5967 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5968 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5969 : OptionValidateContradictions);
5970 }
5971 };
5972
5973} // namespace
5974
5975bool ASTReader::readASTFileControlBlock(
5976 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5977 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5978 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5979 unsigned ClientLoadCapabilities) {
5980 // Open the AST file.
5981 off_t Size;
5982 time_t ModTime;
5983 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5984 llvm::MemoryBuffer *Buffer =
5985 ModCache.getInMemoryModuleCache().lookupPCM(Filename, Size, ModTime);
5986 if (!Buffer) {
5987 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5988 // read again later, but we do not have the context here to determine if it
5989 // is safe to change the result of InMemoryModuleCache::getPCMState().
5990
5991 // FIXME: This allows use of the VFS; we do not allow use of the
5992 // VFS when actually loading a module.
5993 auto Entry = Filename == "-" ? FileMgr.getSTDIN()
5994 : FileMgr.getFileRef(Filename,
5995 /*OpenFile=*/false,
5996 /*CacheFailure=*/true,
5997 /*IsText=*/false);
5998 if (!Entry) {
5999 llvm::consumeError(Err: Entry.takeError());
6000 return true;
6001 }
6002 auto BufferOrErr =
6003 FileMgr.getBufferForFile(Entry: *Entry,
6004 /*IsVolatile=*/isVolatile: false,
6005 /*RequiresNullTerminator=*/false,
6006 /*MaybeLimit=*/std::nullopt,
6007 /*IsText=*/false);
6008 if (!BufferOrErr)
6009 return true;
6010 OwnedBuffer = std::move(*BufferOrErr);
6011 Buffer = OwnedBuffer.get();
6012 }
6013
6014 // Initialize the stream
6015 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
6016 BitstreamCursor Stream(Bytes);
6017
6018 // Sniff for the signature.
6019 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
6020 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
6021 return true;
6022 }
6023
6024 // Scan for the CONTROL_BLOCK_ID block.
6025 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
6026 return true;
6027
6028 bool NeedsInputFiles = Listener.needsInputFileVisitation();
6029 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
6030 bool NeedsImports = Listener.needsImportVisitation();
6031 BitstreamCursor InputFilesCursor;
6032 uint64_t InputFilesOffsetBase = 0;
6033
6034 RecordData Record;
6035 std::string ModuleDir;
6036 bool DoneWithControlBlock = false;
6037 SmallString<0> PathBuf;
6038 PathBuf.reserve(N: 256);
6039 // Additional path buffer to use when multiple paths need to be resolved.
6040 // For example, when deserializing input files that contains a path that was
6041 // resolved from a vfs overlay and an external location.
6042 SmallString<0> AdditionalPathBuf;
6043 AdditionalPathBuf.reserve(N: 256);
6044 while (!DoneWithControlBlock) {
6045 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6046 if (!MaybeEntry) {
6047 // FIXME this drops the error on the floor.
6048 consumeError(Err: MaybeEntry.takeError());
6049 return true;
6050 }
6051 llvm::BitstreamEntry Entry = MaybeEntry.get();
6052
6053 switch (Entry.Kind) {
6054 case llvm::BitstreamEntry::SubBlock: {
6055 switch (Entry.ID) {
6056 case OPTIONS_BLOCK_ID: {
6057 std::string IgnoredSuggestedPredefines;
6058 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6059 /*AllowCompatibleConfigurationMismatch*/ false,
6060 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6061 return true;
6062 break;
6063 }
6064
6065 case INPUT_FILES_BLOCK_ID:
6066 InputFilesCursor = Stream;
6067 if (llvm::Error Err = Stream.SkipBlock()) {
6068 // FIXME this drops the error on the floor.
6069 consumeError(Err: std::move(Err));
6070 return true;
6071 }
6072 if (NeedsInputFiles &&
6073 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6074 return true;
6075 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6076 break;
6077
6078 default:
6079 if (llvm::Error Err = Stream.SkipBlock()) {
6080 // FIXME this drops the error on the floor.
6081 consumeError(Err: std::move(Err));
6082 return true;
6083 }
6084 break;
6085 }
6086
6087 continue;
6088 }
6089
6090 case llvm::BitstreamEntry::EndBlock:
6091 DoneWithControlBlock = true;
6092 break;
6093
6094 case llvm::BitstreamEntry::Error:
6095 return true;
6096
6097 case llvm::BitstreamEntry::Record:
6098 break;
6099 }
6100
6101 if (DoneWithControlBlock) break;
6102
6103 Record.clear();
6104 StringRef Blob;
6105 Expected<unsigned> MaybeRecCode =
6106 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6107 if (!MaybeRecCode) {
6108 // FIXME this drops the error.
6109 return Failure;
6110 }
6111 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6112 case METADATA:
6113 if (Record[0] != VERSION_MAJOR)
6114 return true;
6115 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6116 return true;
6117 break;
6118 case MODULE_NAME:
6119 Listener.ReadModuleName(ModuleName: Blob);
6120 break;
6121 case MODULE_DIRECTORY:
6122 ModuleDir = std::string(Blob);
6123 break;
6124 case MODULE_MAP_FILE: {
6125 unsigned Idx = 0;
6126 std::string PathStr = ReadString(Record, Idx);
6127 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6128 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6129 break;
6130 }
6131 case INPUT_FILE_OFFSETS: {
6132 if (!NeedsInputFiles)
6133 break;
6134
6135 unsigned NumInputFiles = Record[0];
6136 unsigned NumUserFiles = Record[1];
6137 const llvm::support::unaligned_uint64_t *InputFileOffs =
6138 (const llvm::support::unaligned_uint64_t *)Blob.data();
6139 for (unsigned I = 0; I != NumInputFiles; ++I) {
6140 // Go find this input file.
6141 bool isSystemFile = I >= NumUserFiles;
6142
6143 if (isSystemFile && !NeedsSystemInputFiles)
6144 break; // the rest are system input files
6145
6146 BitstreamCursor &Cursor = InputFilesCursor;
6147 SavedStreamPosition SavedPosition(Cursor);
6148 if (llvm::Error Err =
6149 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6150 // FIXME this drops errors on the floor.
6151 consumeError(Err: std::move(Err));
6152 }
6153
6154 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6155 if (!MaybeCode) {
6156 // FIXME this drops errors on the floor.
6157 consumeError(Err: MaybeCode.takeError());
6158 }
6159 unsigned Code = MaybeCode.get();
6160
6161 RecordData Record;
6162 StringRef Blob;
6163 bool shouldContinue = false;
6164 Expected<unsigned> MaybeRecordType =
6165 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6166 if (!MaybeRecordType) {
6167 // FIXME this drops errors on the floor.
6168 consumeError(Err: MaybeRecordType.takeError());
6169 }
6170 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6171 case INPUT_FILE_HASH:
6172 break;
6173 case INPUT_FILE:
6174 time_t StoredTime = static_cast<time_t>(Record[2]);
6175 bool Overridden = static_cast<bool>(Record[3]);
6176 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6177 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6178 auto FilenameAsRequestedBuf = ResolveImportedPath(
6179 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6180 StringRef Filename;
6181 if (UnresolvedFilename.empty())
6182 Filename = *FilenameAsRequestedBuf;
6183 else {
6184 auto FilenameBuf = ResolveImportedPath(
6185 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6186 Filename = *FilenameBuf;
6187 }
6188 shouldContinue = Listener.visitInputFileAsRequested(
6189 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6190 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6191 break;
6192 }
6193 if (!shouldContinue)
6194 break;
6195 }
6196 break;
6197 }
6198
6199 case IMPORT: {
6200 if (!NeedsImports)
6201 break;
6202
6203 unsigned Idx = 0;
6204 // Read information about the AST file.
6205
6206 // Skip Kind
6207 Idx++;
6208
6209 // Skip ImportLoc
6210 Idx++;
6211
6212 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6213
6214 bool IsStandardCXXModule = Record[Idx++];
6215
6216 // In C++20 Modules, we don't record the path to imported
6217 // modules in the BMI files.
6218 if (IsStandardCXXModule) {
6219 Listener.visitImport(ModuleName, /*Filename=*/"");
6220 continue;
6221 }
6222
6223 // Skip Size, ModTime and ImplicitModuleSuffix.
6224 Idx += 1 + 1 + 1;
6225 // Skip signature.
6226 Blob = Blob.substr(Start: ASTFileSignature::size);
6227
6228 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6229 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6230 Listener.visitImport(ModuleName, Filename: *Filename);
6231 break;
6232 }
6233
6234 default:
6235 // No other validation to perform.
6236 break;
6237 }
6238 }
6239
6240 // Look for module file extension blocks, if requested.
6241 if (FindModuleFileExtensions) {
6242 BitstreamCursor SavedStream = Stream;
6243 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6244 bool DoneWithExtensionBlock = false;
6245 while (!DoneWithExtensionBlock) {
6246 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6247 if (!MaybeEntry) {
6248 // FIXME this drops the error.
6249 return true;
6250 }
6251 llvm::BitstreamEntry Entry = MaybeEntry.get();
6252
6253 switch (Entry.Kind) {
6254 case llvm::BitstreamEntry::SubBlock:
6255 if (llvm::Error Err = Stream.SkipBlock()) {
6256 // FIXME this drops the error on the floor.
6257 consumeError(Err: std::move(Err));
6258 return true;
6259 }
6260 continue;
6261
6262 case llvm::BitstreamEntry::EndBlock:
6263 DoneWithExtensionBlock = true;
6264 continue;
6265
6266 case llvm::BitstreamEntry::Error:
6267 return true;
6268
6269 case llvm::BitstreamEntry::Record:
6270 break;
6271 }
6272
6273 Record.clear();
6274 StringRef Blob;
6275 Expected<unsigned> MaybeRecCode =
6276 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6277 if (!MaybeRecCode) {
6278 // FIXME this drops the error.
6279 return true;
6280 }
6281 switch (MaybeRecCode.get()) {
6282 case EXTENSION_METADATA: {
6283 ModuleFileExtensionMetadata Metadata;
6284 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6285 return true;
6286
6287 Listener.readModuleFileExtension(Metadata);
6288 break;
6289 }
6290 }
6291 }
6292 }
6293 Stream = std::move(SavedStream);
6294 }
6295
6296 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6297 if (readUnhashedControlBlockImpl(
6298 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6299 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6300 ValidateDiagnosticOptions) != Success)
6301 return true;
6302
6303 return false;
6304}
6305
6306bool ASTReader::isAcceptableASTFile(
6307 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6308 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6309 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6310 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6311 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6312 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6313 SpecificModuleCachePath, FileMgr,
6314 RequireStrictOptionMatches);
6315 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6316 /*FindModuleFileExtensions=*/false, Listener&: validator,
6317 /*ValidateDiagnosticOptions=*/true);
6318}
6319
6320Module *ASTReader::getSubmodule(uint32_t GlobalID) {
6321 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6322 assert(GlobalID == 0 && "Unhandled global submodule ID");
6323 return nullptr;
6324 }
6325
6326 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6327 if (GlobalIndex >= SubmodulesLoaded.size()) {
6328 Error(Msg: "submodule ID out of range in AST file");
6329 return nullptr;
6330 }
6331
6332 if (SubmodulesLoaded[GlobalIndex])
6333 return SubmodulesLoaded[GlobalIndex];
6334
6335 GlobalSubmoduleMapType::iterator It = GlobalSubmoduleMap.find(K: GlobalID);
6336 assert(It != GlobalSubmoduleMap.end());
6337 ModuleFile &F = *It->second;
6338 unsigned Index = GlobalID - F.BaseSubmoduleID - NUM_PREDEF_SUBMODULE_IDS;
6339 [[maybe_unused]] unsigned LocalID =
6340 Index + F.LocalBaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS;
6341
6342 BitstreamCursor &Cursor = F.SubmodulesCursor;
6343 SavedStreamPosition SavedPosition(Cursor);
6344 unsigned Offset = F.SubmoduleOffsets[Index];
6345 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.SubmodulesOffsetBase + Offset)) {
6346 Error(Err: std::move(Err));
6347 return nullptr;
6348 }
6349
6350 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6351 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6352 // If we don't know the top-level module, there's no point in doing qualified
6353 // lookup of its submodules; it won't find anything anywhere within this tree.
6354 // Let's skip that and avoid some string lookups.
6355 auto CreateModule = !KnowsTopLevelModule
6356 ? &ModuleMap::createModule
6357 : &ModuleMap::findOrCreateModuleFirst;
6358
6359 Module *CurrentModule = nullptr;
6360 RecordData Record;
6361 while (true) {
6362 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
6363 if (!MaybeEntry) {
6364 Error(Err: MaybeEntry.takeError());
6365 return nullptr;
6366 }
6367 llvm::BitstreamEntry Entry = MaybeEntry.get();
6368
6369 switch (Entry.Kind) {
6370 case llvm::BitstreamEntry::SubBlock:
6371 case llvm::BitstreamEntry::Error:
6372 case llvm::BitstreamEntry::EndBlock: {
6373 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6374 Fmt: "malformed block record in AST file"));
6375 return nullptr;
6376 }
6377 case llvm::BitstreamEntry::Record:
6378 // The interesting case.
6379 break;
6380 }
6381
6382 // Read a record.
6383 StringRef Blob;
6384 Record.clear();
6385 Expected<unsigned> MaybeKind = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6386 if (!MaybeKind) {
6387 Error(Err: MaybeKind.takeError());
6388 return nullptr;
6389 }
6390 auto Kind = static_cast<SubmoduleRecordTypes>(MaybeKind.get());
6391
6392 switch (Kind) {
6393 case SUBMODULE_END:
6394 if (!CurrentModule) {
6395 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6396 Fmt: "malformed module definition"));
6397 return nullptr;
6398 }
6399 return CurrentModule;
6400
6401 case SUBMODULE_DEFINITION: {
6402 if (Record.size() < 13) {
6403 Error(Err: llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6404 Fmt: "malformed module definition"));
6405 return nullptr;
6406 }
6407
6408 StringRef Name = Blob;
6409 unsigned Idx = 0;
6410 [[maybe_unused]] unsigned ReadLocalID = Record[Idx++];
6411 assert(LocalID == ReadLocalID);
6412 assert(GlobalID == getGlobalSubmoduleID(F, ReadLocalID));
6413 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6414 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6415 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6416 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6417 bool IsFramework = Record[Idx++];
6418 bool IsExplicit = Record[Idx++];
6419 bool IsSystem = Record[Idx++];
6420 bool IsExternC = Record[Idx++];
6421 bool InferSubmodules = Record[Idx++];
6422 bool InferExplicitSubmodules = Record[Idx++];
6423 bool InferExportWildcard = Record[Idx++];
6424 bool ConfigMacrosExhaustive = Record[Idx++];
6425 bool ModuleMapIsPrivate = Record[Idx++];
6426 bool NamedModuleHasInit = Record[Idx++];
6427
6428 Module *ParentModule = nullptr;
6429 if (Parent) {
6430 ParentModule = getSubmodule(GlobalID: Parent);
6431 if (!ParentModule)
6432 return nullptr;
6433 }
6434
6435 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6436 args&: IsFramework, args&: IsExplicit);
6437
6438 if (!ParentModule) {
6439 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6440 CurrentModule->getASTFileKey()) {
6441 // Don't emit module relocation error if we have -fno-validate-pch
6442 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6443 DisableValidationForModuleKind::Module)) {
6444 assert(*CurFileKey != F.FileKey &&
6445 "ModuleManager did not de-duplicate");
6446
6447 Diag(DiagID: diag::err_module_file_conflict)
6448 << CurrentModule->getTopLevelModuleName()
6449 << *CurrentModule->getASTFileName() << F.FileName;
6450
6451 auto CurModMapFile =
6452 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6453 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6454 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6455 Diag(DiagID: diag::note_module_file_conflict)
6456 << CurModMapFile->getName() << ModMapFile->getName();
6457
6458 return nullptr;
6459 }
6460 }
6461
6462 F.DidReadTopLevelSubmodule = true;
6463 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6464 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6465 }
6466
6467 CurrentModule->Kind = Kind;
6468 // Note that we may be rewriting an existing location and it is important
6469 // to keep doing that. In particular, we would like to prefer a
6470 // `DefinitionLoc` loaded from the module file instead of the location
6471 // created in the current source manager, because it allows the new
6472 // location to be marked as "unaffecting" when writing and avoid creating
6473 // duplicate locations for the same module map file.
6474 CurrentModule->DefinitionLoc = DefinitionLoc;
6475 CurrentModule->Signature = F.Signature;
6476 CurrentModule->IsFromModuleFile = true;
6477 if (InferredAllowedBy.isValid())
6478 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6479 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6480 CurrentModule->IsExternC = IsExternC;
6481 CurrentModule->InferSubmodules = InferSubmodules;
6482 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6483 CurrentModule->InferExportWildcard = InferExportWildcard;
6484 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6485 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6486 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6487
6488 if (!ParentModule && !F.BaseDirectory.empty()) {
6489 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6490 CurrentModule->Directory = *Dir;
6491 } else if (ParentModule && ParentModule->Directory) {
6492 // Submodules inherit the directory from their parent.
6493 CurrentModule->Directory = ParentModule->Directory;
6494 }
6495
6496 if (DeserializationListener)
6497 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6498
6499 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6500
6501 // Clear out data that will be replaced by what is in the module file.
6502 CurrentModule->LinkLibraries.clear();
6503 CurrentModule->ConfigMacros.clear();
6504 CurrentModule->UnresolvedConflicts.clear();
6505 CurrentModule->Conflicts.clear();
6506
6507 // The module is available unless it's missing a requirement; relevant
6508 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6509 // Missing headers that were present when the module was built do not
6510 // make it unavailable -- if we got this far, this must be an explicitly
6511 // imported module file.
6512 CurrentModule->Requirements.clear();
6513 CurrentModule->MissingHeaders.clear();
6514 CurrentModule->IsUnimportable =
6515 ParentModule && ParentModule->IsUnimportable;
6516 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6517 break;
6518 }
6519
6520 case SUBMODULE_UMBRELLA_HEADER: {
6521 SmallString<128> RelativePathName;
6522 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6523 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6524 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6525 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6526 PathRelativeToRootModuleDirectory: RelativePathName);
6527 }
6528 // Note that it's too late at this point to return out of date if the
6529 // name from the PCM doesn't match up with the one in the module map,
6530 // but also quite unlikely since we will have already checked the
6531 // modification time and size of the module map file itself.
6532 }
6533 break;
6534 }
6535
6536 case SUBMODULE_HEADER:
6537 case SUBMODULE_EXCLUDED_HEADER:
6538 case SUBMODULE_PRIVATE_HEADER:
6539 // We lazily associate headers with their modules via the HeaderInfo table.
6540 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6541 // of complete filenames or remove it entirely.
6542 break;
6543
6544 case SUBMODULE_TEXTUAL_HEADER:
6545 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6546 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6547 // them here.
6548 break;
6549
6550 case SUBMODULE_TOPHEADER: {
6551 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6552 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6553 break;
6554 }
6555
6556 case SUBMODULE_UMBRELLA_DIR: {
6557 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6558 if (auto Umbrella =
6559 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6560 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6561 // FIXME: NameAsWritten
6562 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6563 }
6564 }
6565 break;
6566 }
6567
6568 case SUBMODULE_IMPORTS:
6569 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6570 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6571 CurrentModule->Imports.push_back(Elt: ModuleRef(this, GlobalID));
6572 }
6573 break;
6574
6575 case SUBMODULE_AFFECTING_MODULES:
6576 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6577 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6578 CurrentModule->AffectingClangModules.push_back(
6579 Elt: ModuleRef(this, GlobalID));
6580 }
6581 break;
6582
6583 case SUBMODULE_EXPORTS:
6584 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6585 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx]);
6586 bool IsWildcard = Record[Idx + 1];
6587 ModuleRef ExportedMod =
6588 GlobalID ? ModuleRef(this, GlobalID) : ModuleRef();
6589 if (ExportedMod || IsWildcard)
6590 CurrentModule->Exports.push_back(Elt: {ExportedMod, IsWildcard});
6591 }
6592
6593 // Once we've loaded the set of exports, there's no reason to keep
6594 // the parsed, unresolved exports around.
6595 CurrentModule->UnresolvedExports.clear();
6596 break;
6597
6598 case SUBMODULE_REQUIRES:
6599 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6600 Target: PP.getTargetInfo());
6601 break;
6602
6603 case SUBMODULE_LINK_LIBRARY:
6604 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6605 CurrentModule->LinkLibraries.push_back(
6606 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6607 break;
6608
6609 case SUBMODULE_CONFIG_MACRO:
6610 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6611 break;
6612
6613 case SUBMODULE_CONFLICT: {
6614 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6615 Module::Conflict Conflict;
6616 Conflict.Other = ModuleRef(this, GlobalID);
6617 Conflict.Message = Blob.str();
6618 CurrentModule->Conflicts.push_back(x: Conflict);
6619 break;
6620 }
6621
6622 case SUBMODULE_INITIALIZERS: {
6623 if (!ContextObj)
6624 break;
6625 // Standard C++ module has its own way to initialize variables.
6626 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6627 SmallVector<GlobalDeclID, 16> Inits;
6628 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6629 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6630 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6631 }
6632 break;
6633 }
6634
6635 case SUBMODULE_EXPORT_AS:
6636 CurrentModule->ExportAsModule = Blob.str();
6637 ModMap.addLinkAsDependency(Mod: CurrentModule);
6638 break;
6639
6640 case SUBMODULE_CHILD: {
6641 // Record a not-yet-loaded direct child for on-demand deserialization.
6642 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[0]);
6643 CurrentModule->addSubmodule(Name: Blob, ExternalSource: this, SubmoduleID: GlobalID);
6644 break;
6645 }
6646 }
6647 }
6648}
6649
6650/// Parse the record that corresponds to a LangOptions data
6651/// structure.
6652///
6653/// This routine parses the language options from the AST file and then gives
6654/// them to the AST listener if one is set.
6655///
6656/// \returns true if the listener deems the file unacceptable, false otherwise.
6657bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6658 StringRef ModuleFilename, bool Complain,
6659 ASTReaderListener &Listener,
6660 bool AllowCompatibleDifferences) {
6661 LangOptions LangOpts;
6662 unsigned Idx = 0;
6663#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6664 LangOpts.Name = Record[Idx++];
6665#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6666 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6667#include "clang/Basic/LangOptions.def"
6668#define SANITIZER(NAME, ID) \
6669 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6670#include "clang/Basic/Sanitizers.def"
6671
6672 for (unsigned N = Record[Idx++]; N; --N)
6673 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6674
6675 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6676 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6677 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6678
6679 LangOpts.CurrentModule = ReadString(Record, Idx);
6680
6681 // Comment options.
6682 for (unsigned N = Record[Idx++]; N; --N) {
6683 LangOpts.CommentOpts.BlockCommandNames.push_back(
6684 x: ReadString(Record, Idx));
6685 }
6686 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6687
6688 // OpenMP offloading options.
6689 for (unsigned N = Record[Idx++]; N; --N) {
6690 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6691 }
6692
6693 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6694
6695 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6696 AllowCompatibleDifferences);
6697}
6698
6699bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6700 StringRef ModuleFilename, bool Complain,
6701 ASTReaderListener &Listener,
6702 bool AllowCompatibleDifferences) {
6703 unsigned Idx = 0;
6704 CodeGenOptions CGOpts;
6705 using CK = CodeGenOptions::CompatibilityKind;
6706#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6707 if constexpr (CK::Compatibility != CK::Benign) \
6708 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6709#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6710 if constexpr (CK::Compatibility != CK::Benign) \
6711 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6712#define DEBUGOPT(Name, Bits, Default, Compatibility)
6713#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6714#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6715#include "clang/Basic/CodeGenOptions.def"
6716
6717 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6718 AllowCompatibleDifferences);
6719}
6720
6721bool ASTReader::ParseTargetOptions(const RecordData &Record,
6722 StringRef ModuleFilename, bool Complain,
6723 ASTReaderListener &Listener,
6724 bool AllowCompatibleDifferences) {
6725 unsigned Idx = 0;
6726 TargetOptions TargetOpts;
6727 TargetOpts.Triple = ReadString(Record, Idx);
6728 TargetOpts.CPU = ReadString(Record, Idx);
6729 TargetOpts.TuneCPU = ReadString(Record, Idx);
6730 TargetOpts.ABI = ReadString(Record, Idx);
6731 for (unsigned N = Record[Idx++]; N; --N) {
6732 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6733 }
6734 for (unsigned N = Record[Idx++]; N; --N) {
6735 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6736 }
6737
6738 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6739 AllowCompatibleDifferences);
6740}
6741
6742bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6743 StringRef ModuleFilename, bool Complain,
6744 ASTReaderListener &Listener) {
6745 DiagnosticOptions DiagOpts;
6746 unsigned Idx = 0;
6747#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6748#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6749 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6750#include "clang/Basic/DiagnosticOptions.def"
6751
6752 for (unsigned N = Record[Idx++]; N; --N)
6753 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6754 for (unsigned N = Record[Idx++]; N; --N)
6755 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6756
6757 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6758}
6759
6760bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6761 ASTReaderListener &Listener) {
6762 FileSystemOptions FSOpts;
6763 unsigned Idx = 0;
6764 FSOpts.WorkingDir = ReadString(Record, Idx);
6765 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6766}
6767
6768bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6769 StringRef ModuleFilename,
6770 bool Complain,
6771 ASTReaderListener &Listener) {
6772 HeaderSearchOptions HSOpts;
6773 unsigned Idx = 0;
6774 HSOpts.Sysroot = ReadString(Record, Idx);
6775
6776 HSOpts.ResourceDir = ReadString(Record, Idx);
6777 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6778 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6779 HSOpts.DisableModuleHash = Record[Idx++];
6780 HSOpts.ImplicitModuleMaps = Record[Idx++];
6781 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6782 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6783 HSOpts.UseBuiltinIncludes = Record[Idx++];
6784 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6785 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6786 HSOpts.UseLibcxx = Record[Idx++];
6787 std::string ContextHash = ReadString(Record, Idx);
6788
6789 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6790 Complain);
6791}
6792
6793bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6794 ASTReaderListener &Listener) {
6795 HeaderSearchOptions HSOpts;
6796 unsigned Idx = 0;
6797
6798 // Include entries.
6799 for (unsigned N = Record[Idx++]; N; --N) {
6800 std::string Path = ReadString(Record, Idx);
6801 frontend::IncludeDirGroup Group
6802 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6803 bool IsFramework = Record[Idx++];
6804 bool IgnoreSysRoot = Record[Idx++];
6805 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6806 args&: IgnoreSysRoot);
6807 }
6808
6809 // System header prefixes.
6810 for (unsigned N = Record[Idx++]; N; --N) {
6811 std::string Prefix = ReadString(Record, Idx);
6812 bool IsSystemHeader = Record[Idx++];
6813 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6814 }
6815
6816 // VFS overlay files.
6817 for (unsigned N = Record[Idx++]; N; --N) {
6818 std::string VFSOverlayFile = ReadString(Record, Idx);
6819 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6820 }
6821
6822 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6823}
6824
6825bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6826 StringRef ModuleFilename,
6827 bool Complain,
6828 ASTReaderListener &Listener,
6829 std::string &SuggestedPredefines) {
6830 PreprocessorOptions PPOpts;
6831 unsigned Idx = 0;
6832
6833 // Macro definitions/undefs
6834 bool ReadMacros = Record[Idx++];
6835 if (ReadMacros) {
6836 for (unsigned N = Record[Idx++]; N; --N) {
6837 std::string Macro = ReadString(Record, Idx);
6838 bool IsUndef = Record[Idx++];
6839 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6840 }
6841 }
6842
6843 // Includes
6844 for (unsigned N = Record[Idx++]; N; --N) {
6845 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6846 }
6847
6848 // Macro Includes
6849 for (unsigned N = Record[Idx++]; N; --N) {
6850 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6851 }
6852
6853 PPOpts.UsePredefines = Record[Idx++];
6854 PPOpts.DetailedRecord = Record[Idx++];
6855 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6856 PPOpts.ObjCXXARCStandardLibrary =
6857 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6858 SuggestedPredefines.clear();
6859 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6860 Complain, SuggestedPredefines);
6861}
6862
6863std::pair<ModuleFile *, unsigned>
6864ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6865 GlobalPreprocessedEntityMapType::iterator
6866 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6867 assert(I != GlobalPreprocessedEntityMap.end() &&
6868 "Corrupted global preprocessed entity map");
6869 ModuleFile *M = I->second;
6870 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6871 return std::make_pair(x&: M, y&: LocalIndex);
6872}
6873
6874llvm::iterator_range<PreprocessingRecord::iterator>
6875ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6876 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6877 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6878 count: Mod.NumPreprocessedEntities);
6879
6880 return llvm::make_range(x: PreprocessingRecord::iterator(),
6881 y: PreprocessingRecord::iterator());
6882}
6883
6884bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6885 unsigned int ClientLoadCapabilities) {
6886 return ClientLoadCapabilities & ARR_OutOfDate &&
6887 !getModuleManager()
6888 .getModuleCache()
6889 .getInMemoryModuleCache()
6890 .isPCMFinal(Filename: ModuleFileName);
6891}
6892
6893llvm::iterator_range<ASTReader::ModuleDeclIterator>
6894ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6895 return llvm::make_range(
6896 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6897 y: ModuleDeclIterator(this, &Mod,
6898 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6899}
6900
6901SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6902 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6903 assert(I != GlobalSkippedRangeMap.end() &&
6904 "Corrupted global skipped range map");
6905 ModuleFile *M = I->second;
6906 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6907 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6908 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6909 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6910 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6911 assert(Range.isValid());
6912 return Range;
6913}
6914
6915unsigned
6916ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6917 unsigned ModuleFileIndex = ID >> 32;
6918 assert(ModuleFileIndex && "not translating loaded MacroID?");
6919 assert(getModuleManager().size() > ModuleFileIndex - 1);
6920 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6921
6922 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6923 return MF.BasePreprocessedEntityID + ID;
6924}
6925
6926PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6927 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6928 ModuleFile &M = *PPInfo.first;
6929 unsigned LocalIndex = PPInfo.second;
6930 PreprocessedEntityID PPID =
6931 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6932 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6933
6934 if (!PP.getPreprocessingRecord()) {
6935 Error(Msg: "no preprocessing record");
6936 return nullptr;
6937 }
6938
6939 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6940 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6941 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6942 Error(Err: std::move(Err));
6943 return nullptr;
6944 }
6945
6946 Expected<llvm::BitstreamEntry> MaybeEntry =
6947 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6948 if (!MaybeEntry) {
6949 Error(Err: MaybeEntry.takeError());
6950 return nullptr;
6951 }
6952 llvm::BitstreamEntry Entry = MaybeEntry.get();
6953
6954 if (Entry.Kind != llvm::BitstreamEntry::Record)
6955 return nullptr;
6956
6957 // Read the record.
6958 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6959 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6960 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6961 StringRef Blob;
6962 RecordData Record;
6963 Expected<unsigned> MaybeRecType =
6964 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6965 if (!MaybeRecType) {
6966 Error(Err: MaybeRecType.takeError());
6967 return nullptr;
6968 }
6969 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6970 case PPD_MACRO_EXPANSION: {
6971 bool isBuiltin = Record[0];
6972 IdentifierInfo *Name = nullptr;
6973 MacroDefinitionRecord *Def = nullptr;
6974 if (isBuiltin)
6975 Name = getLocalIdentifier(M, LocalID: Record[1]);
6976 else {
6977 PreprocessedEntityID GlobalID =
6978 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6979 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
6980 Def =
6981 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
6982 }
6983
6984 MacroExpansion *ME;
6985 if (isBuiltin)
6986 ME = new (PPRec) MacroExpansion(Name, Range);
6987 else
6988 ME = new (PPRec) MacroExpansion(Def, Range);
6989
6990 return ME;
6991 }
6992
6993 case PPD_MACRO_DEFINITION: {
6994 // Decode the identifier info and then check again; if the macro is
6995 // still defined and associated with the identifier,
6996 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6997 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6998
6999 if (DeserializationListener)
7000 DeserializationListener->MacroDefinitionRead(PPID, MD);
7001
7002 return MD;
7003 }
7004
7005 case PPD_INCLUSION_DIRECTIVE: {
7006 const char *FullFileNameStart = Blob.data() + Record[0];
7007 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
7008 OptionalFileEntryRef File;
7009 if (!FullFileName.empty())
7010 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
7011
7012 // FIXME: Stable encoding
7013 InclusionDirective::InclusionKind Kind
7014 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
7015 InclusionDirective *ID
7016 = new (PPRec) InclusionDirective(PPRec, Kind,
7017 StringRef(Blob.data(), Record[0]),
7018 Record[1], Record[3],
7019 File,
7020 Range);
7021 return ID;
7022 }
7023 }
7024
7025 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
7026}
7027
7028/// Find the next module that contains entities and return the ID
7029/// of the first entry.
7030///
7031/// \param SLocMapI points at a chunk of a module that contains no
7032/// preprocessed entities or the entities it contains are not the ones we are
7033/// looking for.
7034unsigned ASTReader::findNextPreprocessedEntity(
7035 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
7036 ++SLocMapI;
7037 for (GlobalSLocOffsetMapType::const_iterator
7038 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
7039 ModuleFile &M = *SLocMapI->second;
7040 if (M.NumPreprocessedEntities)
7041 return M.BasePreprocessedEntityID;
7042 }
7043
7044 return getTotalNumPreprocessedEntities();
7045}
7046
7047namespace {
7048
7049struct PPEntityComp {
7050 const ASTReader &Reader;
7051 ModuleFile &M;
7052
7053 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7054
7055 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7056 SourceLocation LHS = getLoc(PPE: L);
7057 SourceLocation RHS = getLoc(PPE: R);
7058 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7059 }
7060
7061 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7062 SourceLocation LHS = getLoc(PPE: L);
7063 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7064 }
7065
7066 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7067 SourceLocation RHS = getLoc(PPE: R);
7068 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7069 }
7070
7071 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7072 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7073 }
7074};
7075
7076} // namespace
7077
7078unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7079 bool EndsAfter) const {
7080 if (SourceMgr.isLocalSourceLocation(Loc))
7081 return getTotalNumPreprocessedEntities();
7082
7083 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7084 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7085 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7086 "Corrupted global sloc offset map");
7087
7088 if (SLocMapI->second->NumPreprocessedEntities == 0)
7089 return findNextPreprocessedEntity(SLocMapI);
7090
7091 ModuleFile &M = *SLocMapI->second;
7092
7093 using pp_iterator = const PPEntityOffset *;
7094
7095 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7096 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7097
7098 size_t Count = M.NumPreprocessedEntities;
7099 size_t Half;
7100 pp_iterator First = pp_begin;
7101 pp_iterator PPI;
7102
7103 if (EndsAfter) {
7104 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7105 comp: PPEntityComp(*this, M));
7106 } else {
7107 // Do a binary search manually instead of using std::lower_bound because
7108 // The end locations of entities may be unordered (when a macro expansion
7109 // is inside another macro argument), but for this case it is not important
7110 // whether we get the first macro expansion or its containing macro.
7111 while (Count > 0) {
7112 Half = Count / 2;
7113 PPI = First;
7114 std::advance(i&: PPI, n: Half);
7115 if (SourceMgr.isBeforeInTranslationUnit(
7116 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7117 First = PPI;
7118 ++First;
7119 Count = Count - Half - 1;
7120 } else
7121 Count = Half;
7122 }
7123 }
7124
7125 if (PPI == pp_end)
7126 return findNextPreprocessedEntity(SLocMapI);
7127
7128 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7129}
7130
7131/// Returns a pair of [Begin, End) indices of preallocated
7132/// preprocessed entities that \arg Range encompasses.
7133std::pair<unsigned, unsigned>
7134 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7135 if (Range.isInvalid())
7136 return std::make_pair(x: 0,y: 0);
7137 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7138
7139 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7140 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7141 return std::make_pair(x&: BeginID, y&: EndID);
7142}
7143
7144/// Optionally returns true or false if the preallocated preprocessed
7145/// entity with index \arg Index came from file \arg FID.
7146std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7147 FileID FID) {
7148 if (FID.isInvalid())
7149 return false;
7150
7151 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7152 ModuleFile &M = *PPInfo.first;
7153 unsigned LocalIndex = PPInfo.second;
7154 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7155
7156 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7157 if (Loc.isInvalid())
7158 return false;
7159
7160 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7161 return true;
7162 else
7163 return false;
7164}
7165
7166namespace {
7167
7168 /// Visitor used to search for information about a header file.
7169 class HeaderFileInfoVisitor {
7170 FileEntryRef FE;
7171 std::optional<HeaderFileInfo> HFI;
7172
7173 public:
7174 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7175
7176 bool operator()(ModuleFile &M) {
7177 HeaderFileInfoLookupTable *Table
7178 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7179 if (!Table)
7180 return false;
7181
7182 // Look in the on-disk hash table for an entry for this file name.
7183 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7184 if (Pos == Table->end())
7185 return false;
7186
7187 HFI = *Pos;
7188 return true;
7189 }
7190
7191 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7192 };
7193
7194} // namespace
7195
7196HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7197 HeaderFileInfoVisitor Visitor(FE);
7198 ModuleMgr.visit(Visitor);
7199 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7200 return *HFI;
7201
7202 return HeaderFileInfo();
7203}
7204
7205void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7206 using DiagState = DiagnosticsEngine::DiagState;
7207 SmallVector<DiagState *, 32> DiagStates;
7208
7209 for (ModuleFile &F : ModuleMgr) {
7210 unsigned Idx = 0;
7211 auto &Record = F.PragmaDiagMappings;
7212 if (Record.empty())
7213 continue;
7214
7215 DiagStates.clear();
7216
7217 auto ReadDiagState = [&](const DiagState &BasedOn,
7218 bool IncludeNonPragmaStates) {
7219 unsigned BackrefID = Record[Idx++];
7220 if (BackrefID != 0)
7221 return DiagStates[BackrefID - 1];
7222
7223 // A new DiagState was created here.
7224 Diag.DiagStates.push_back(x: BasedOn);
7225 DiagState *NewState = &Diag.DiagStates.back();
7226 DiagStates.push_back(Elt: NewState);
7227 unsigned Size = Record[Idx++];
7228 assert(Idx + Size * 2 <= Record.size() &&
7229 "Invalid data, not enough diag/map pairs");
7230 while (Size--) {
7231 unsigned DiagID = Record[Idx++];
7232 DiagnosticMapping NewMapping =
7233 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7234 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7235 continue;
7236
7237 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7238
7239 // If this mapping was specified as a warning but the severity was
7240 // upgraded due to diagnostic settings, simulate the current diagnostic
7241 // settings (and use a warning).
7242 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7243 NewMapping.setSeverity(diag::Severity::Warning);
7244 NewMapping.setUpgradedFromWarning(false);
7245 }
7246
7247 Mapping = NewMapping;
7248 }
7249 return NewState;
7250 };
7251
7252 // Read the first state.
7253 DiagState *FirstState;
7254 if (F.Kind == MK_ImplicitModule) {
7255 // Implicitly-built modules are reused with different diagnostic
7256 // settings. Use the initial diagnostic state from Diag to simulate this
7257 // compilation's diagnostic settings.
7258 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7259 DiagStates.push_back(Elt: FirstState);
7260
7261 // Skip the initial diagnostic state from the serialized module.
7262 assert(Record[1] == 0 &&
7263 "Invalid data, unexpected backref in initial state");
7264 Idx = 3 + Record[2] * 2;
7265 assert(Idx < Record.size() &&
7266 "Invalid data, not enough state change pairs in initial state");
7267 } else if (F.isModule()) {
7268 // For an explicit module, preserve the flags from the module build
7269 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7270 // -Wblah flags.
7271 unsigned Flags = Record[Idx++];
7272 DiagState Initial(*Diag.getDiagnosticIDs());
7273 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7274 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7275 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7276 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7277 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7278 Initial.ExtBehavior = (diag::Severity)Flags;
7279 FirstState = ReadDiagState(Initial, true);
7280
7281 assert(F.OriginalSourceFileID.isValid());
7282
7283 // Set up the root buffer of the module to start with the initial
7284 // diagnostic state of the module itself, to cover files that contain no
7285 // explicit transitions (for which we did not serialize anything).
7286 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7287 .StateTransitions.push_back(Elt: {FirstState, 0});
7288 } else {
7289 // For prefix ASTs, start with whatever the user configured on the
7290 // command line.
7291 Idx++; // Skip flags.
7292 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7293 }
7294
7295 // Read the state transitions.
7296 unsigned NumLocations = Record[Idx++];
7297 while (NumLocations--) {
7298 assert(Idx < Record.size() &&
7299 "Invalid data, missing pragma diagnostic states");
7300 FileID FID = ReadFileID(F, Record, Idx);
7301 assert(FID.isValid() && "invalid FileID for transition");
7302 unsigned Transitions = Record[Idx++];
7303
7304 // Note that we don't need to set up Parent/ParentOffset here, because
7305 // we won't be changing the diagnostic state within imported FileIDs
7306 // (other than perhaps appending to the main source file, which has no
7307 // parent).
7308 auto &F = Diag.DiagStatesByLoc.Files[FID];
7309 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7310 for (unsigned I = 0; I != Transitions; ++I) {
7311 unsigned Offset = Record[Idx++];
7312 auto *State = ReadDiagState(*FirstState, false);
7313 F.StateTransitions.push_back(Elt: {State, Offset});
7314 }
7315 }
7316
7317 // Read the final state.
7318 assert(Idx < Record.size() &&
7319 "Invalid data, missing final pragma diagnostic state");
7320 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7321 auto *CurState = ReadDiagState(*FirstState, false);
7322
7323 if (!F.isModule()) {
7324 Diag.DiagStatesByLoc.CurDiagState = CurState;
7325 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7326
7327 // Preserve the property that the imaginary root file describes the
7328 // current state.
7329 FileID NullFile;
7330 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7331 if (T.empty())
7332 T.push_back(Elt: {CurState, 0});
7333 else
7334 T[0].State = CurState;
7335 }
7336
7337 // Restore the push stack so that unmatched pushes from a preamble are
7338 // visible when the main file is parsed, allowing the corresponding
7339 // `#pragma diagnostic pop` to succeed.
7340 assert(Idx < Record.size() &&
7341 "Invalid data, missing diagnostic push stack");
7342 unsigned NumPushes = Record[Idx++];
7343 for (unsigned I = 0; I != NumPushes; ++I) {
7344 auto *State = ReadDiagState(*FirstState, false);
7345 if (!F.isModule())
7346 Diag.DiagStateOnPushStack.push_back(x: State);
7347 }
7348
7349 // Don't try to read these mappings again.
7350 Record.clear();
7351 }
7352}
7353
7354/// Get the correct cursor and offset for loading a type.
7355ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7356 auto [M, Index] = translateTypeIDToIndex(ID);
7357 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7358 M->DeclsBlockStartOffset);
7359}
7360
7361static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7362 switch (code) {
7363#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7364 case TYPE_##CODE_ID: return Type::CLASS_ID;
7365#include "clang/Serialization/TypeBitCodes.def"
7366 default:
7367 return std::nullopt;
7368 }
7369}
7370
7371/// Read and return the type with the given index..
7372///
7373/// The index is the type ID, shifted and minus the number of predefs. This
7374/// routine actually reads the record corresponding to the type at the given
7375/// location. It is a helper routine for GetType, which deals with reading type
7376/// IDs.
7377QualType ASTReader::readTypeRecord(TypeID ID) {
7378 assert(ContextObj && "reading type with no AST context");
7379 ASTContext &Context = *ContextObj;
7380 RecordLocation Loc = TypeCursorForIndex(ID);
7381 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7382
7383 // Keep track of where we are in the stream, then jump back there
7384 // after reading this type.
7385 SavedStreamPosition SavedPosition(DeclsCursor);
7386
7387 ReadingKindTracker ReadingKind(Read_Type, *this);
7388
7389 // Note that we are loading a type record.
7390 Deserializing AType(this);
7391
7392 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7393 Error(Err: std::move(Err));
7394 return QualType();
7395 }
7396 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7397 if (!RawCode) {
7398 Error(Err: RawCode.takeError());
7399 return QualType();
7400 }
7401
7402 ASTRecordReader Record(*this, *Loc.F);
7403 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7404 if (!Code) {
7405 Error(Err: Code.takeError());
7406 return QualType();
7407 }
7408 if (Code.get() == TYPE_EXT_QUAL) {
7409 QualType baseType = Record.readQualType();
7410 Qualifiers quals = Record.readQualifiers();
7411 return Context.getQualifiedType(T: baseType, Qs: quals);
7412 }
7413
7414 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7415 if (!maybeClass) {
7416 Error(Msg: "Unexpected code for type");
7417 return QualType();
7418 }
7419
7420 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7421 return TypeReader.read(kind: *maybeClass);
7422}
7423
7424namespace clang {
7425
7426class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7427 ASTRecordReader &Reader;
7428
7429 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7430 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7431
7432 TypeSourceInfo *GetTypeSourceInfo() {
7433 return Reader.readTypeSourceInfo();
7434 }
7435
7436 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7437 return Reader.readNestedNameSpecifierLoc();
7438 }
7439
7440 Attr *ReadAttr() {
7441 return Reader.readAttr();
7442 }
7443
7444public:
7445 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7446
7447 // We want compile-time assurance that we've enumerated all of
7448 // these, so unfortunately we have to declare them first, then
7449 // define them out-of-line.
7450#define ABSTRACT_TYPELOC(CLASS, PARENT)
7451#define TYPELOC(CLASS, PARENT) \
7452 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7453#include "clang/AST/TypeLocNodes.def"
7454
7455 void VisitFunctionTypeLoc(FunctionTypeLoc);
7456 void VisitArrayTypeLoc(ArrayTypeLoc);
7457 void VisitTagTypeLoc(TagTypeLoc TL);
7458};
7459
7460} // namespace clang
7461
7462void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7463 // nothing to do
7464}
7465
7466void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7467 TL.setBuiltinLoc(readSourceLocation());
7468 if (TL.needsExtraLocalData()) {
7469 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7470 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7471 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7472 TL.setModeAttr(Reader.readInt());
7473 }
7474}
7475
7476void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7477 TL.setNameLoc(readSourceLocation());
7478}
7479
7480void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7481 TL.setStarLoc(readSourceLocation());
7482}
7483
7484void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7485 // nothing to do
7486}
7487
7488void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7489 // nothing to do
7490}
7491
7492void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7493 // nothing to do
7494}
7495
7496void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7497 TL.setExpansionLoc(readSourceLocation());
7498}
7499
7500void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7501 TL.setCaretLoc(readSourceLocation());
7502}
7503
7504void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7505 TL.setAmpLoc(readSourceLocation());
7506}
7507
7508void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7509 TL.setAmpAmpLoc(readSourceLocation());
7510}
7511
7512void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7513 TL.setStarLoc(readSourceLocation());
7514 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7515}
7516
7517void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7518 TL.setLBracketLoc(readSourceLocation());
7519 TL.setRBracketLoc(readSourceLocation());
7520 if (Reader.readBool())
7521 TL.setSizeExpr(Reader.readExpr());
7522 else
7523 TL.setSizeExpr(nullptr);
7524}
7525
7526void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7527 VisitArrayTypeLoc(TL);
7528}
7529
7530void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7531 VisitArrayTypeLoc(TL);
7532}
7533
7534void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7535 VisitArrayTypeLoc(TL);
7536}
7537
7538void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7539 DependentSizedArrayTypeLoc TL) {
7540 VisitArrayTypeLoc(TL);
7541}
7542
7543void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7544 DependentAddressSpaceTypeLoc TL) {
7545
7546 TL.setAttrNameLoc(readSourceLocation());
7547 TL.setAttrOperandParensRange(readSourceRange());
7548 TL.setAttrExprOperand(Reader.readExpr());
7549}
7550
7551void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7552 DependentSizedExtVectorTypeLoc TL) {
7553 TL.setNameLoc(readSourceLocation());
7554}
7555
7556void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7557 TL.setNameLoc(readSourceLocation());
7558}
7559
7560void TypeLocReader::VisitDependentVectorTypeLoc(
7561 DependentVectorTypeLoc TL) {
7562 TL.setNameLoc(readSourceLocation());
7563}
7564
7565void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7566 TL.setNameLoc(readSourceLocation());
7567}
7568
7569void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7570 TL.setAttrNameLoc(readSourceLocation());
7571 TL.setAttrOperandParensRange(readSourceRange());
7572 TL.setAttrRowOperand(Reader.readExpr());
7573 TL.setAttrColumnOperand(Reader.readExpr());
7574}
7575
7576void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7577 DependentSizedMatrixTypeLoc TL) {
7578 TL.setAttrNameLoc(readSourceLocation());
7579 TL.setAttrOperandParensRange(readSourceRange());
7580 TL.setAttrRowOperand(Reader.readExpr());
7581 TL.setAttrColumnOperand(Reader.readExpr());
7582}
7583
7584void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7585 TL.setLocalRangeBegin(readSourceLocation());
7586 TL.setLParenLoc(readSourceLocation());
7587 TL.setRParenLoc(readSourceLocation());
7588 TL.setExceptionSpecRange(readSourceRange());
7589 TL.setLocalRangeEnd(readSourceLocation());
7590 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7591 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7592 }
7593}
7594
7595void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7596 VisitFunctionTypeLoc(TL);
7597}
7598
7599void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7600 VisitFunctionTypeLoc(TL);
7601}
7602
7603void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7604 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7605 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7606 SourceLocation NameLoc = readSourceLocation();
7607 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7608}
7609
7610void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7611 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7612 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7613 SourceLocation NameLoc = readSourceLocation();
7614 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7615}
7616
7617void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7618 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7619 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7620 SourceLocation NameLoc = readSourceLocation();
7621 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7622}
7623
7624void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7625 TL.setTypeofLoc(readSourceLocation());
7626 TL.setLParenLoc(readSourceLocation());
7627 TL.setRParenLoc(readSourceLocation());
7628}
7629
7630void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7631 TL.setTypeofLoc(readSourceLocation());
7632 TL.setLParenLoc(readSourceLocation());
7633 TL.setRParenLoc(readSourceLocation());
7634 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7635}
7636
7637void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7638 TL.setDecltypeLoc(readSourceLocation());
7639 TL.setRParenLoc(readSourceLocation());
7640}
7641
7642void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7643 TL.setEllipsisLoc(readSourceLocation());
7644}
7645
7646void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7647 TL.setKWLoc(readSourceLocation());
7648 TL.setLParenLoc(readSourceLocation());
7649 TL.setRParenLoc(readSourceLocation());
7650 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7651}
7652
7653ConceptReference *ASTRecordReader::readConceptReference() {
7654 auto NNS = readNestedNameSpecifierLoc();
7655 auto TemplateKWLoc = readSourceLocation();
7656 auto ConceptNameLoc = readDeclarationNameInfo();
7657 auto FoundDecl = readDeclAs<NamedDecl>();
7658 auto NamedConcept = readDeclAs<ConceptDecl>();
7659 auto *CR = ConceptReference::Create(
7660 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7661 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7662 return CR;
7663}
7664
7665void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7666 TL.setNameLoc(readSourceLocation());
7667 if (Reader.readBool())
7668 TL.setConceptReference(Reader.readConceptReference());
7669 if (Reader.readBool())
7670 TL.setRParenLoc(readSourceLocation());
7671}
7672
7673void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7674 DeducedTemplateSpecializationTypeLoc TL) {
7675 TL.setElaboratedKeywordLoc(readSourceLocation());
7676 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7677 TL.setTemplateNameLoc(readSourceLocation());
7678}
7679
7680void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7681 TL.setElaboratedKeywordLoc(readSourceLocation());
7682 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7683 TL.setNameLoc(readSourceLocation());
7684}
7685
7686void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7687 VisitTagTypeLoc(TL);
7688}
7689
7690void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7691 VisitTagTypeLoc(TL);
7692}
7693
7694void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7695
7696void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7697 TL.setAttr(ReadAttr());
7698}
7699
7700void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7701 // Nothing to do
7702}
7703
7704void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7705 // Nothing to do.
7706}
7707
7708void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7709 TL.setAttrLoc(readSourceLocation());
7710}
7711
7712void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7713 HLSLAttributedResourceTypeLoc TL) {
7714 // Nothing to do.
7715}
7716
7717void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7718 // Nothing to do.
7719}
7720
7721void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7722 TL.setNameLoc(readSourceLocation());
7723}
7724
7725void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7726 SubstTemplateTypeParmTypeLoc TL) {
7727 TL.setNameLoc(readSourceLocation());
7728}
7729
7730void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7731 SubstTemplateTypeParmPackTypeLoc TL) {
7732 TL.setNameLoc(readSourceLocation());
7733}
7734
7735void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7736 SubstBuiltinTemplatePackTypeLoc TL) {
7737 TL.setNameLoc(readSourceLocation());
7738}
7739
7740void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7741 TemplateSpecializationTypeLoc TL) {
7742 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7743 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7744 SourceLocation TemplateKeywordLoc = readSourceLocation();
7745 SourceLocation NameLoc = readSourceLocation();
7746 SourceLocation LAngleLoc = readSourceLocation();
7747 SourceLocation RAngleLoc = readSourceLocation();
7748 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7749 LAngleLoc, RAngleLoc);
7750 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7751 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7752 Args[I] = Reader.readTemplateArgumentLocInfo(
7753 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7754}
7755
7756void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7757 TL.setLParenLoc(readSourceLocation());
7758 TL.setRParenLoc(readSourceLocation());
7759}
7760
7761void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7762 TL.setElaboratedKeywordLoc(readSourceLocation());
7763 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7764 TL.setNameLoc(readSourceLocation());
7765}
7766
7767void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7768 TL.setEllipsisLoc(readSourceLocation());
7769}
7770
7771void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7772 TL.setNameLoc(readSourceLocation());
7773 TL.setNameEndLoc(readSourceLocation());
7774}
7775
7776void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7777 if (TL.getNumProtocols()) {
7778 TL.setProtocolLAngleLoc(readSourceLocation());
7779 TL.setProtocolRAngleLoc(readSourceLocation());
7780 }
7781 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7782 TL.setProtocolLoc(i, Loc: readSourceLocation());
7783}
7784
7785void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7786 TL.setHasBaseTypeAsWritten(Reader.readBool());
7787 TL.setTypeArgsLAngleLoc(readSourceLocation());
7788 TL.setTypeArgsRAngleLoc(readSourceLocation());
7789 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7790 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7791 TL.setProtocolLAngleLoc(readSourceLocation());
7792 TL.setProtocolRAngleLoc(readSourceLocation());
7793 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7794 TL.setProtocolLoc(i, Loc: readSourceLocation());
7795}
7796
7797void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7798 TL.setStarLoc(readSourceLocation());
7799}
7800
7801void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7802 TL.setKWLoc(readSourceLocation());
7803 TL.setLParenLoc(readSourceLocation());
7804 TL.setRParenLoc(readSourceLocation());
7805}
7806
7807void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7808 TL.setKWLoc(readSourceLocation());
7809}
7810
7811void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7812 TL.setNameLoc(readSourceLocation());
7813}
7814
7815void TypeLocReader::VisitDependentBitIntTypeLoc(
7816 clang::DependentBitIntTypeLoc TL) {
7817 TL.setNameLoc(readSourceLocation());
7818}
7819
7820void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7821 // Nothing to do.
7822}
7823
7824void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7825 TypeLocReader TLR(*this);
7826 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7827 TLR.Visit(TyLoc: TL);
7828}
7829
7830TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7831 QualType InfoTy = readType();
7832 if (InfoTy.isNull())
7833 return nullptr;
7834
7835 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7836 readTypeLoc(TL: TInfo->getTypeLoc());
7837 return TInfo;
7838}
7839
7840static unsigned getIndexForTypeID(serialization::TypeID ID) {
7841 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7842}
7843
7844static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7845 return ID >> 32;
7846}
7847
7848static bool isPredefinedType(serialization::TypeID ID) {
7849 // We don't need to erase the higher bits since if these bits are not 0,
7850 // it must be larger than NUM_PREDEF_TYPE_IDS.
7851 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7852}
7853
7854std::pair<ModuleFile *, unsigned>
7855ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7856 assert(!isPredefinedType(ID) &&
7857 "Predefined type shouldn't be in TypesLoaded");
7858 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7859 assert(ModuleFileIndex && "Untranslated Local Decl?");
7860
7861 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7862 assert(OwningModuleFile &&
7863 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7864
7865 return {OwningModuleFile,
7866 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7867}
7868
7869QualType ASTReader::GetType(TypeID ID) {
7870 assert(ContextObj && "reading type with no AST context");
7871 ASTContext &Context = *ContextObj;
7872
7873 unsigned FastQuals = ID & Qualifiers::FastMask;
7874
7875 if (isPredefinedType(ID)) {
7876 QualType T;
7877 unsigned Index = getIndexForTypeID(ID);
7878 switch ((PredefinedTypeIDs)Index) {
7879 case PREDEF_TYPE_LAST_ID:
7880 // We should never use this one.
7881 llvm_unreachable("Invalid predefined type");
7882 break;
7883 case PREDEF_TYPE_NULL_ID:
7884 return QualType();
7885 case PREDEF_TYPE_VOID_ID:
7886 T = Context.VoidTy;
7887 break;
7888 case PREDEF_TYPE_BOOL_ID:
7889 T = Context.BoolTy;
7890 break;
7891 case PREDEF_TYPE_CHAR_U_ID:
7892 case PREDEF_TYPE_CHAR_S_ID:
7893 // FIXME: Check that the signedness of CharTy is correct!
7894 T = Context.CharTy;
7895 break;
7896 case PREDEF_TYPE_UCHAR_ID:
7897 T = Context.UnsignedCharTy;
7898 break;
7899 case PREDEF_TYPE_USHORT_ID:
7900 T = Context.UnsignedShortTy;
7901 break;
7902 case PREDEF_TYPE_UINT_ID:
7903 T = Context.UnsignedIntTy;
7904 break;
7905 case PREDEF_TYPE_ULONG_ID:
7906 T = Context.UnsignedLongTy;
7907 break;
7908 case PREDEF_TYPE_ULONGLONG_ID:
7909 T = Context.UnsignedLongLongTy;
7910 break;
7911 case PREDEF_TYPE_UINT128_ID:
7912 T = Context.UnsignedInt128Ty;
7913 break;
7914 case PREDEF_TYPE_SCHAR_ID:
7915 T = Context.SignedCharTy;
7916 break;
7917 case PREDEF_TYPE_WCHAR_ID:
7918 T = Context.WCharTy;
7919 break;
7920 case PREDEF_TYPE_SHORT_ID:
7921 T = Context.ShortTy;
7922 break;
7923 case PREDEF_TYPE_INT_ID:
7924 T = Context.IntTy;
7925 break;
7926 case PREDEF_TYPE_LONG_ID:
7927 T = Context.LongTy;
7928 break;
7929 case PREDEF_TYPE_LONGLONG_ID:
7930 T = Context.LongLongTy;
7931 break;
7932 case PREDEF_TYPE_INT128_ID:
7933 T = Context.Int128Ty;
7934 break;
7935 case PREDEF_TYPE_BFLOAT16_ID:
7936 T = Context.BFloat16Ty;
7937 break;
7938 case PREDEF_TYPE_HALF_ID:
7939 T = Context.HalfTy;
7940 break;
7941 case PREDEF_TYPE_FLOAT_ID:
7942 T = Context.FloatTy;
7943 break;
7944 case PREDEF_TYPE_DOUBLE_ID:
7945 T = Context.DoubleTy;
7946 break;
7947 case PREDEF_TYPE_LONGDOUBLE_ID:
7948 T = Context.LongDoubleTy;
7949 break;
7950 case PREDEF_TYPE_SHORT_ACCUM_ID:
7951 T = Context.ShortAccumTy;
7952 break;
7953 case PREDEF_TYPE_ACCUM_ID:
7954 T = Context.AccumTy;
7955 break;
7956 case PREDEF_TYPE_LONG_ACCUM_ID:
7957 T = Context.LongAccumTy;
7958 break;
7959 case PREDEF_TYPE_USHORT_ACCUM_ID:
7960 T = Context.UnsignedShortAccumTy;
7961 break;
7962 case PREDEF_TYPE_UACCUM_ID:
7963 T = Context.UnsignedAccumTy;
7964 break;
7965 case PREDEF_TYPE_ULONG_ACCUM_ID:
7966 T = Context.UnsignedLongAccumTy;
7967 break;
7968 case PREDEF_TYPE_SHORT_FRACT_ID:
7969 T = Context.ShortFractTy;
7970 break;
7971 case PREDEF_TYPE_FRACT_ID:
7972 T = Context.FractTy;
7973 break;
7974 case PREDEF_TYPE_LONG_FRACT_ID:
7975 T = Context.LongFractTy;
7976 break;
7977 case PREDEF_TYPE_USHORT_FRACT_ID:
7978 T = Context.UnsignedShortFractTy;
7979 break;
7980 case PREDEF_TYPE_UFRACT_ID:
7981 T = Context.UnsignedFractTy;
7982 break;
7983 case PREDEF_TYPE_ULONG_FRACT_ID:
7984 T = Context.UnsignedLongFractTy;
7985 break;
7986 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7987 T = Context.SatShortAccumTy;
7988 break;
7989 case PREDEF_TYPE_SAT_ACCUM_ID:
7990 T = Context.SatAccumTy;
7991 break;
7992 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7993 T = Context.SatLongAccumTy;
7994 break;
7995 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7996 T = Context.SatUnsignedShortAccumTy;
7997 break;
7998 case PREDEF_TYPE_SAT_UACCUM_ID:
7999 T = Context.SatUnsignedAccumTy;
8000 break;
8001 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
8002 T = Context.SatUnsignedLongAccumTy;
8003 break;
8004 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
8005 T = Context.SatShortFractTy;
8006 break;
8007 case PREDEF_TYPE_SAT_FRACT_ID:
8008 T = Context.SatFractTy;
8009 break;
8010 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
8011 T = Context.SatLongFractTy;
8012 break;
8013 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
8014 T = Context.SatUnsignedShortFractTy;
8015 break;
8016 case PREDEF_TYPE_SAT_UFRACT_ID:
8017 T = Context.SatUnsignedFractTy;
8018 break;
8019 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
8020 T = Context.SatUnsignedLongFractTy;
8021 break;
8022 case PREDEF_TYPE_FLOAT16_ID:
8023 T = Context.Float16Ty;
8024 break;
8025 case PREDEF_TYPE_FLOAT128_ID:
8026 T = Context.Float128Ty;
8027 break;
8028 case PREDEF_TYPE_IBM128_ID:
8029 T = Context.Ibm128Ty;
8030 break;
8031 case PREDEF_TYPE_OVERLOAD_ID:
8032 T = Context.OverloadTy;
8033 break;
8034 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
8035 T = Context.UnresolvedTemplateTy;
8036 break;
8037 case PREDEF_TYPE_BOUND_MEMBER:
8038 T = Context.BoundMemberTy;
8039 break;
8040 case PREDEF_TYPE_PSEUDO_OBJECT:
8041 T = Context.PseudoObjectTy;
8042 break;
8043 case PREDEF_TYPE_DEPENDENT_ID:
8044 T = Context.DependentTy;
8045 break;
8046 case PREDEF_TYPE_UNKNOWN_ANY:
8047 T = Context.UnknownAnyTy;
8048 break;
8049 case PREDEF_TYPE_NULLPTR_ID:
8050 T = Context.NullPtrTy;
8051 break;
8052 case PREDEF_TYPE_CHAR8_ID:
8053 T = Context.Char8Ty;
8054 break;
8055 case PREDEF_TYPE_CHAR16_ID:
8056 T = Context.Char16Ty;
8057 break;
8058 case PREDEF_TYPE_CHAR32_ID:
8059 T = Context.Char32Ty;
8060 break;
8061 case PREDEF_TYPE_OBJC_ID:
8062 T = Context.ObjCBuiltinIdTy;
8063 break;
8064 case PREDEF_TYPE_OBJC_CLASS:
8065 T = Context.ObjCBuiltinClassTy;
8066 break;
8067 case PREDEF_TYPE_OBJC_SEL:
8068 T = Context.ObjCBuiltinSelTy;
8069 break;
8070#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8071 case PREDEF_TYPE_##Id##_ID: \
8072 T = Context.SingletonId; \
8073 break;
8074#include "clang/Basic/OpenCLImageTypes.def"
8075#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8076 case PREDEF_TYPE_##Id##_ID: \
8077 T = Context.Id##Ty; \
8078 break;
8079#include "clang/Basic/OpenCLExtensionTypes.def"
8080 case PREDEF_TYPE_SAMPLER_ID:
8081 T = Context.OCLSamplerTy;
8082 break;
8083 case PREDEF_TYPE_EVENT_ID:
8084 T = Context.OCLEventTy;
8085 break;
8086 case PREDEF_TYPE_CLK_EVENT_ID:
8087 T = Context.OCLClkEventTy;
8088 break;
8089 case PREDEF_TYPE_QUEUE_ID:
8090 T = Context.OCLQueueTy;
8091 break;
8092 case PREDEF_TYPE_RESERVE_ID_ID:
8093 T = Context.OCLReserveIDTy;
8094 break;
8095 case PREDEF_TYPE_AUTO_DEDUCT:
8096 T = Context.getAutoDeductType();
8097 break;
8098 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8099 T = Context.getAutoRRefDeductType();
8100 break;
8101 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8102 T = Context.ARCUnbridgedCastTy;
8103 break;
8104 case PREDEF_TYPE_BUILTIN_FN:
8105 T = Context.BuiltinFnTy;
8106 break;
8107 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8108 T = Context.IncompleteMatrixIdxTy;
8109 break;
8110 case PREDEF_TYPE_ARRAY_SECTION:
8111 T = Context.ArraySectionTy;
8112 break;
8113 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8114 T = Context.OMPArrayShapingTy;
8115 break;
8116 case PREDEF_TYPE_OMP_ITERATOR:
8117 T = Context.OMPIteratorTy;
8118 break;
8119#define SVE_TYPE(Name, Id, SingletonId) \
8120 case PREDEF_TYPE_##Id##_ID: \
8121 T = Context.SingletonId; \
8122 break;
8123#include "clang/Basic/AArch64ACLETypes.def"
8124#define PPC_VECTOR_TYPE(Name, Id, Size) \
8125 case PREDEF_TYPE_##Id##_ID: \
8126 T = Context.Id##Ty; \
8127 break;
8128#include "clang/Basic/PPCTypes.def"
8129#define RVV_TYPE(Name, Id, SingletonId) \
8130 case PREDEF_TYPE_##Id##_ID: \
8131 T = Context.SingletonId; \
8132 break;
8133#include "clang/Basic/RISCVVTypes.def"
8134#define WASM_TYPE(Name, Id, SingletonId) \
8135 case PREDEF_TYPE_##Id##_ID: \
8136 T = Context.SingletonId; \
8137 break;
8138#include "clang/Basic/WebAssemblyReferenceTypes.def"
8139#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8140 case PREDEF_TYPE_##Id##_ID: \
8141 T = Context.SingletonId; \
8142 break;
8143#include "clang/Basic/AMDGPUTypes.def"
8144#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8145 case PREDEF_TYPE_##Id##_ID: \
8146 T = Context.SingletonId; \
8147 break;
8148#include "clang/Basic/HLSLIntangibleTypes.def"
8149 }
8150
8151 assert(!T.isNull() && "Unknown predefined type");
8152 return T.withFastQualifiers(TQs: FastQuals);
8153 }
8154
8155 unsigned Index = translateTypeIDToIndex(ID).second;
8156
8157 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8158 if (TypesLoaded[Index].isNull()) {
8159 TypesLoaded[Index] = readTypeRecord(ID);
8160 if (TypesLoaded[Index].isNull())
8161 return QualType();
8162
8163 TypesLoaded[Index]->setFromAST();
8164 if (DeserializationListener)
8165 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8166 T: TypesLoaded[Index]);
8167 }
8168
8169 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8170}
8171
8172QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8173 return GetType(ID: getGlobalTypeID(F, LocalID));
8174}
8175
8176serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8177 LocalTypeID LocalID) const {
8178 if (isPredefinedType(ID: LocalID))
8179 return LocalID;
8180
8181 if (!F.ModuleOffsetMap.empty())
8182 ReadModuleOffsetMap(F);
8183
8184 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8185 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8186
8187 if (ModuleFileIndex == 0)
8188 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8189
8190 ModuleFile &MF =
8191 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8192 ModuleFileIndex = MF.Index + 1;
8193 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8194}
8195
8196TemplateArgumentLocInfo
8197ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8198 switch (Kind) {
8199 case TemplateArgument::Expression:
8200 return readExpr();
8201 case TemplateArgument::Type:
8202 return readTypeSourceInfo();
8203 case TemplateArgument::Template:
8204 case TemplateArgument::TemplateExpansion: {
8205 SourceLocation TemplateKWLoc = readSourceLocation();
8206 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8207 SourceLocation TemplateNameLoc = readSourceLocation();
8208 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8209 ? readSourceLocation()
8210 : SourceLocation();
8211 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8212 TemplateNameLoc, EllipsisLoc);
8213 }
8214 case TemplateArgument::Null:
8215 case TemplateArgument::Integral:
8216 case TemplateArgument::Declaration:
8217 case TemplateArgument::NullPtr:
8218 case TemplateArgument::StructuralValue:
8219 case TemplateArgument::Pack:
8220 // FIXME: Is this right?
8221 return TemplateArgumentLocInfo();
8222 }
8223 llvm_unreachable("unexpected template argument loc");
8224}
8225
8226TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8227 TemplateArgument Arg = readTemplateArgument();
8228
8229 if (Arg.getKind() == TemplateArgument::Expression) {
8230 if (readBool()) // bool InfoHasSameExpr.
8231 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8232 }
8233 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8234}
8235
8236void ASTRecordReader::readTemplateArgumentListInfo(
8237 TemplateArgumentListInfo &Result) {
8238 Result.setLAngleLoc(readSourceLocation());
8239 Result.setRAngleLoc(readSourceLocation());
8240 unsigned NumArgsAsWritten = readInt();
8241 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8242 Result.addArgument(Loc: readTemplateArgumentLoc());
8243}
8244
8245const ASTTemplateArgumentListInfo *
8246ASTRecordReader::readASTTemplateArgumentListInfo() {
8247 TemplateArgumentListInfo Result;
8248 readTemplateArgumentListInfo(Result);
8249 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8250}
8251
8252Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8253
8254void ASTReader::CompleteRedeclChain(const Decl *D) {
8255 if (NumCurrentElementsDeserializing) {
8256 // We arrange to not care about the complete redeclaration chain while we're
8257 // deserializing. Just remember that the AST has marked this one as complete
8258 // but that it's not actually complete yet, so we know we still need to
8259 // complete it later.
8260 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8261 return;
8262 }
8263
8264 if (!D->getDeclContext()) {
8265 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8266 return;
8267 }
8268
8269 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8270
8271 // If this is a named declaration, complete it by looking it up
8272 // within its context.
8273 //
8274 // FIXME: Merging a function definition should merge
8275 // all mergeable entities within it.
8276 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8277 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8278 if (!getContext().getLangOpts().CPlusPlus &&
8279 isa<TranslationUnitDecl>(Val: DC)) {
8280 // Outside of C++, we don't have a lookup table for the TU, so update
8281 // the identifier instead. (For C++ modules, we don't store decls
8282 // in the serialized identifier table, so we do the lookup in the TU.)
8283 auto *II = Name.getAsIdentifierInfo();
8284 assert(II && "non-identifier name in C?");
8285 if (II->isOutOfDate())
8286 updateOutOfDateIdentifier(II: *II);
8287 } else
8288 DC->lookup(Name);
8289 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8290 // Find all declarations of this kind from the relevant context.
8291 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8292 auto *DC = cast<DeclContext>(Val: DCDecl);
8293 SmallVector<Decl*, 8> Decls;
8294 FindExternalLexicalDecls(
8295 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8296 }
8297 }
8298 }
8299
8300 RedeclarableTemplateDecl *Template = nullptr;
8301 ArrayRef<TemplateArgument> Args;
8302 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8303 Template = CTSD->getSpecializedTemplate();
8304 Args = CTSD->getTemplateArgs().asArray();
8305 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8306 Template = VTSD->getSpecializedTemplate();
8307 Args = VTSD->getTemplateArgs().asArray();
8308 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8309 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8310 Template = Tmplt;
8311 Args = FD->getTemplateSpecializationArgs()->asArray();
8312 }
8313 }
8314
8315 if (Template)
8316 Template->loadLazySpecializationsImpl(Args);
8317}
8318
8319CXXCtorInitializer **
8320ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8321 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8322 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8323 SavedStreamPosition SavedPosition(Cursor);
8324 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8325 Error(Err: std::move(Err));
8326 return nullptr;
8327 }
8328 ReadingKindTracker ReadingKind(Read_Decl, *this);
8329 Deserializing D(this);
8330
8331 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8332 if (!MaybeCode) {
8333 Error(Err: MaybeCode.takeError());
8334 return nullptr;
8335 }
8336 unsigned Code = MaybeCode.get();
8337
8338 ASTRecordReader Record(*this, *Loc.F);
8339 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8340 if (!MaybeRecCode) {
8341 Error(Err: MaybeRecCode.takeError());
8342 return nullptr;
8343 }
8344 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8345 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8346 return nullptr;
8347 }
8348
8349 return Record.readCXXCtorInitializers();
8350}
8351
8352CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8353 assert(ContextObj && "reading base specifiers with no AST context");
8354 ASTContext &Context = *ContextObj;
8355
8356 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8357 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8358 SavedStreamPosition SavedPosition(Cursor);
8359 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8360 Error(Err: std::move(Err));
8361 return nullptr;
8362 }
8363 ReadingKindTracker ReadingKind(Read_Decl, *this);
8364 Deserializing D(this);
8365
8366 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8367 if (!MaybeCode) {
8368 Error(Err: MaybeCode.takeError());
8369 return nullptr;
8370 }
8371 unsigned Code = MaybeCode.get();
8372
8373 ASTRecordReader Record(*this, *Loc.F);
8374 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8375 if (!MaybeRecCode) {
8376 Error(Err: MaybeCode.takeError());
8377 return nullptr;
8378 }
8379 unsigned RecCode = MaybeRecCode.get();
8380
8381 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8382 Error(Msg: "malformed AST file: missing C++ base specifiers");
8383 return nullptr;
8384 }
8385
8386 unsigned NumBases = Record.readInt();
8387 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8388 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8389 for (unsigned I = 0; I != NumBases; ++I)
8390 Bases[I] = Record.readCXXBaseSpecifier();
8391 return Bases;
8392}
8393
8394GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8395 LocalDeclID LocalID) const {
8396 if (LocalID < NUM_PREDEF_DECL_IDS)
8397 return GlobalDeclID(LocalID.getRawValue());
8398
8399 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8400 DeclID ID = LocalID.getLocalDeclIndex();
8401
8402 if (!F.ModuleOffsetMap.empty())
8403 ReadModuleOffsetMap(F);
8404
8405 ModuleFile *OwningModuleFile =
8406 OwningModuleFileIndex == 0
8407 ? &F
8408 : F.TransitiveImports[OwningModuleFileIndex - 1];
8409
8410 if (OwningModuleFileIndex == 0)
8411 ID -= NUM_PREDEF_DECL_IDS;
8412
8413 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8414 return GlobalDeclID(NewModuleFileIndex, ID);
8415}
8416
8417bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8418 // Predefined decls aren't from any module.
8419 if (ID < NUM_PREDEF_DECL_IDS)
8420 return false;
8421
8422 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8423 return M.Index == ModuleFileIndex - 1;
8424}
8425
8426ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8427 // Predefined decls aren't from any module.
8428 if (ID < NUM_PREDEF_DECL_IDS)
8429 return nullptr;
8430
8431 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8432 assert(ModuleFileIndex && "Untranslated Local Decl?");
8433
8434 return &getModuleManager()[ModuleFileIndex - 1];
8435}
8436
8437ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8438 if (!D->isFromASTFile())
8439 return nullptr;
8440
8441 return getOwningModuleFile(ID: D->getGlobalID());
8442}
8443
8444SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8445 if (ID < NUM_PREDEF_DECL_IDS)
8446 return SourceLocation();
8447
8448 if (Decl *D = GetExistingDecl(ID))
8449 return D->getLocation();
8450
8451 SourceLocation Loc;
8452 DeclCursorForID(ID, Location&: Loc);
8453 return Loc;
8454}
8455
8456Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8457 assert(ContextObj && "reading predefined decl without AST context");
8458 ASTContext &Context = *ContextObj;
8459 Decl *NewLoaded = nullptr;
8460 switch (ID) {
8461 case PREDEF_DECL_NULL_ID:
8462 return nullptr;
8463
8464 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8465 return Context.getTranslationUnitDecl();
8466
8467 case PREDEF_DECL_OBJC_ID_ID:
8468 if (Context.ObjCIdDecl)
8469 return Context.ObjCIdDecl;
8470 NewLoaded = Context.getObjCIdDecl();
8471 break;
8472
8473 case PREDEF_DECL_OBJC_SEL_ID:
8474 if (Context.ObjCSelDecl)
8475 return Context.ObjCSelDecl;
8476 NewLoaded = Context.getObjCSelDecl();
8477 break;
8478
8479 case PREDEF_DECL_OBJC_CLASS_ID:
8480 if (Context.ObjCClassDecl)
8481 return Context.ObjCClassDecl;
8482 NewLoaded = Context.getObjCClassDecl();
8483 break;
8484
8485 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8486 if (Context.ObjCProtocolClassDecl)
8487 return Context.ObjCProtocolClassDecl;
8488 NewLoaded = Context.getObjCProtocolDecl();
8489 break;
8490
8491 case PREDEF_DECL_INT_128_ID:
8492 if (Context.Int128Decl)
8493 return Context.Int128Decl;
8494 NewLoaded = Context.getInt128Decl();
8495 break;
8496
8497 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8498 if (Context.UInt128Decl)
8499 return Context.UInt128Decl;
8500 NewLoaded = Context.getUInt128Decl();
8501 break;
8502
8503 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8504 if (Context.ObjCInstanceTypeDecl)
8505 return Context.ObjCInstanceTypeDecl;
8506 NewLoaded = Context.getObjCInstanceTypeDecl();
8507 break;
8508
8509 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8510 if (Context.BuiltinVaListDecl)
8511 return Context.BuiltinVaListDecl;
8512 NewLoaded = Context.getBuiltinVaListDecl();
8513 break;
8514
8515 case PREDEF_DECL_VA_LIST_TAG:
8516 if (Context.VaListTagDecl)
8517 return Context.VaListTagDecl;
8518 NewLoaded = Context.getVaListTagDecl();
8519 break;
8520
8521 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8522 if (Context.BuiltinMSVaListDecl)
8523 return Context.BuiltinMSVaListDecl;
8524 NewLoaded = Context.getBuiltinMSVaListDecl();
8525 break;
8526
8527 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8528 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8529 return Context.getMSGuidTagDecl();
8530
8531 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8532 if (Context.ExternCContext)
8533 return Context.ExternCContext;
8534 NewLoaded = Context.getExternCContextDecl();
8535 break;
8536
8537 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8538 if (Context.CFConstantStringTypeDecl)
8539 return Context.CFConstantStringTypeDecl;
8540 NewLoaded = Context.getCFConstantStringDecl();
8541 break;
8542
8543 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8544 if (Context.CFConstantStringTagDecl)
8545 return Context.CFConstantStringTagDecl;
8546 NewLoaded = Context.getCFConstantStringTagDecl();
8547 break;
8548
8549 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8550 return Context.getMSTypeInfoTagDecl();
8551
8552#define BuiltinTemplate(BTName) \
8553 case PREDEF_DECL##BTName##_ID: \
8554 if (Context.Decl##BTName) \
8555 return Context.Decl##BTName; \
8556 NewLoaded = Context.get##BTName##Decl(); \
8557 break;
8558#include "clang/Basic/BuiltinTemplates.inc"
8559
8560 case NUM_PREDEF_DECL_IDS:
8561 llvm_unreachable("Invalid decl ID");
8562 break;
8563 }
8564
8565 assert(NewLoaded && "Failed to load predefined decl?");
8566
8567 if (DeserializationListener)
8568 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8569
8570 return NewLoaded;
8571}
8572
8573unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8574 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8575 if (!OwningModuleFile) {
8576 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8577 return GlobalID.getRawValue();
8578 }
8579
8580 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8581}
8582
8583Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8584 assert(ContextObj && "reading decl with no AST context");
8585
8586 if (ID < NUM_PREDEF_DECL_IDS) {
8587 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8588 if (D) {
8589 // Track that we have merged the declaration with ID \p ID into the
8590 // pre-existing predefined declaration \p D.
8591 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8592 if (Merged.empty())
8593 Merged.push_back(Elt: ID);
8594 }
8595 return D;
8596 }
8597
8598 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8599
8600 if (Index >= DeclsLoaded.size()) {
8601 assert(0 && "declaration ID out-of-range for AST file");
8602 Error(Msg: "declaration ID out-of-range for AST file");
8603 return nullptr;
8604 }
8605
8606 return DeclsLoaded[Index];
8607}
8608
8609Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8610 if (ID < NUM_PREDEF_DECL_IDS)
8611 return GetExistingDecl(ID);
8612
8613 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8614
8615 if (Index >= DeclsLoaded.size()) {
8616 assert(0 && "declaration ID out-of-range for AST file");
8617 Error(Msg: "declaration ID out-of-range for AST file");
8618 return nullptr;
8619 }
8620
8621 if (!DeclsLoaded[Index]) {
8622 ReadDeclRecord(ID);
8623 if (DeserializationListener)
8624 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8625 }
8626
8627 return DeclsLoaded[Index];
8628}
8629
8630LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8631 GlobalDeclID GlobalID) {
8632 if (GlobalID < NUM_PREDEF_DECL_IDS)
8633 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8634
8635 if (!M.ModuleOffsetMap.empty())
8636 ReadModuleOffsetMap(F&: M);
8637
8638 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8639 DeclID ID = GlobalID.getLocalDeclIndex();
8640
8641 if (Owner == &M) {
8642 ID += NUM_PREDEF_DECL_IDS;
8643 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8644 }
8645
8646 uint64_t OrignalModuleFileIndex = 0;
8647 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8648 if (M.TransitiveImports[I] == Owner) {
8649 OrignalModuleFileIndex = I + 1;
8650 break;
8651 }
8652
8653 if (!OrignalModuleFileIndex)
8654 return LocalDeclID();
8655
8656 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8657}
8658
8659GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8660 unsigned &Idx) {
8661 if (Idx >= Record.size()) {
8662 Error(Msg: "Corrupted AST file");
8663 return GlobalDeclID(0);
8664 }
8665
8666 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8667}
8668
8669/// Resolve the offset of a statement into a statement.
8670///
8671/// This operation will read a new statement from the external
8672/// source each time it is called, and is meant to be used via a
8673/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8674Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8675 // Switch case IDs are per Decl.
8676 ClearSwitchCaseIDs();
8677
8678 // Offset here is a global offset across the entire chain.
8679 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8680 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8681 Error(Err: std::move(Err));
8682 return nullptr;
8683 }
8684 assert(NumCurrentElementsDeserializing == 0 &&
8685 "should not be called while already deserializing");
8686 Deserializing D(this);
8687 return ReadStmtFromStream(F&: *Loc.F);
8688}
8689
8690bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8691 const Decl *D) {
8692 assert(D);
8693
8694 auto It = SpecLookups.find(Val: D);
8695 if (It == SpecLookups.end())
8696 return false;
8697
8698 // Get Decl may violate the iterator from SpecializationsLookups so we store
8699 // the DeclIDs in ahead.
8700 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8701 It->second.Table.findAll();
8702
8703 // Since we've loaded all the specializations, we can erase it from
8704 // the lookup table.
8705 SpecLookups.erase(I: It);
8706
8707 bool NewSpecsFound = false;
8708 Deserializing LookupResults(this);
8709 for (auto &Info : Infos) {
8710 if (GetExistingDecl(ID: Info))
8711 continue;
8712 NewSpecsFound = true;
8713 GetDecl(ID: Info);
8714 }
8715
8716 return NewSpecsFound;
8717}
8718
8719bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8720 assert(D);
8721
8722 CompleteRedeclChain(D);
8723 bool NewSpecsFound =
8724 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8725 if (OnlyPartial)
8726 return NewSpecsFound;
8727
8728 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8729 return NewSpecsFound;
8730}
8731
8732bool ASTReader::LoadExternalSpecializationsImpl(
8733 SpecLookupTableTy &SpecLookups, const Decl *D,
8734 ArrayRef<TemplateArgument> TemplateArgs) {
8735 assert(D);
8736
8737 auto It = SpecLookups.find(Val: D);
8738 if (It == SpecLookups.end())
8739 return false;
8740
8741 Deserializing LookupResults(this);
8742 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8743
8744 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8745 It->second.Table.find(EKey: HashValue);
8746
8747 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8748 std::string Name;
8749 llvm::raw_string_ostream OS(Name);
8750 auto *ND = cast<NamedDecl>(Val: D);
8751 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8752 /*Qualified=*/true);
8753 return Name;
8754 });
8755
8756 bool NewSpecsFound = false;
8757 for (auto &Info : Infos) {
8758 if (GetExistingDecl(ID: Info))
8759 continue;
8760 NewSpecsFound = true;
8761 GetDecl(ID: Info);
8762 }
8763
8764 return NewSpecsFound;
8765}
8766
8767bool ASTReader::LoadExternalSpecializations(
8768 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8769 assert(D);
8770
8771 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8772 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8773 NewDeclsFound |=
8774 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8775
8776 return NewDeclsFound;
8777}
8778
8779void ASTReader::FindExternalLexicalDecls(
8780 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8781 SmallVectorImpl<Decl *> &Decls) {
8782 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8783
8784 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8785 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8786 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8787 auto K = (Decl::Kind)+LexicalDecls[I];
8788 if (!IsKindWeWant(K))
8789 continue;
8790
8791 auto ID = (DeclID) + LexicalDecls[I + 1];
8792
8793 // Don't add predefined declarations to the lexical context more
8794 // than once.
8795 if (ID < NUM_PREDEF_DECL_IDS) {
8796 if (PredefsVisited[ID])
8797 continue;
8798
8799 PredefsVisited[ID] = true;
8800 }
8801
8802 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8803 assert(D->getKind() == K && "wrong kind for lexical decl");
8804 if (!DC->isDeclInLexicalTraversal(D))
8805 Decls.push_back(Elt: D);
8806 }
8807 }
8808 };
8809
8810 if (isa<TranslationUnitDecl>(Val: DC)) {
8811 for (const auto &Lexical : TULexicalDecls)
8812 Visit(Lexical.first, Lexical.second);
8813 } else {
8814 auto I = LexicalDecls.find(Val: DC);
8815 if (I != LexicalDecls.end())
8816 Visit(I->second.first, I->second.second);
8817 }
8818
8819 ++NumLexicalDeclContextsRead;
8820}
8821
8822namespace {
8823
8824class UnalignedDeclIDComp {
8825 ASTReader &Reader;
8826 ModuleFile &Mod;
8827
8828public:
8829 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8830 : Reader(Reader), Mod(M) {}
8831
8832 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8833 SourceLocation LHS = getLocation(ID: L);
8834 SourceLocation RHS = getLocation(ID: R);
8835 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8836 }
8837
8838 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8839 SourceLocation RHS = getLocation(ID: R);
8840 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8841 }
8842
8843 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8844 SourceLocation LHS = getLocation(ID: L);
8845 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8846 }
8847
8848 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8849 return Reader.getSourceManager().getFileLoc(
8850 Loc: Reader.getSourceLocationForDeclID(
8851 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8852 }
8853};
8854
8855} // namespace
8856
8857void ASTReader::FindFileRegionDecls(FileID File,
8858 unsigned Offset, unsigned Length,
8859 SmallVectorImpl<Decl *> &Decls) {
8860 SourceManager &SM = getSourceManager();
8861
8862 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8863 if (I == FileDeclIDs.end())
8864 return;
8865
8866 FileDeclsInfo &DInfo = I->second;
8867 if (DInfo.Decls.empty())
8868 return;
8869
8870 SourceLocation
8871 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8872 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8873
8874 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8875 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8876 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8877 if (BeginIt != DInfo.Decls.begin())
8878 --BeginIt;
8879
8880 // If we are pointing at a top-level decl inside an objc container, we need
8881 // to backtrack until we find it otherwise we will fail to report that the
8882 // region overlaps with an objc container.
8883 while (BeginIt != DInfo.Decls.begin() &&
8884 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8885 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8886 ->isTopLevelDeclInObjCContainer())
8887 --BeginIt;
8888
8889 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8890 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8891 if (EndIt != DInfo.Decls.end())
8892 ++EndIt;
8893
8894 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8895 ++DIt)
8896 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8897 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8898}
8899
8900bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8901 DeclarationName Name,
8902 const DeclContext *OriginalDC) {
8903 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8904 "DeclContext has no visible decls in storage");
8905 if (!Name)
8906 return false;
8907
8908 // Load the list of declarations.
8909 DeclsSet DS;
8910
8911 auto Find = [&, this](auto &&Table, auto &&Key) {
8912 for (GlobalDeclID ID : Table.find(Key)) {
8913 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8914 if (ND->getDeclName() != Name)
8915 continue;
8916 // Special case for namespaces: There can be a lot of redeclarations of
8917 // some namespaces, and we import a "key declaration" per imported module.
8918 // Since all declarations of a namespace are essentially interchangeable,
8919 // we can optimize namespace look-up by only storing the key declaration
8920 // of the current TU, rather than storing N key declarations where N is
8921 // the # of imported modules that declare that namespace.
8922 // TODO: Try to generalize this optimization to other redeclarable decls.
8923 if (isa<NamespaceDecl>(Val: ND))
8924 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8925 DS.insert(ND);
8926 }
8927 };
8928
8929 Deserializing LookupResults(this);
8930
8931 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8932 // available.
8933 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8934 ++NumVisibleDeclContextsRead;
8935 Find(It->second.Table, Name);
8936 }
8937
8938 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8939 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8940 ++NumModuleLocalVisibleDeclContexts;
8941 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8942 }
8943 };
8944 if (auto *NamedModule =
8945 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8946 : nullptr)
8947 FindModuleLocalLookup(NamedModule);
8948 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8949 // We're going to find a decl but the decl context of the lookup is
8950 // unspecified. In this case, the OriginalDC may be the decl context in other
8951 // module.
8952 if (ContextObj && ContextObj->getCurrentNamedModule())
8953 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8954
8955 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8956 ++NumTULocalVisibleDeclContexts;
8957 Find(It->second.Table, Name);
8958 }
8959
8960 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8961 return !DS.empty();
8962}
8963
8964void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8965 if (!DC->hasExternalVisibleStorage())
8966 return;
8967
8968 DeclsMap Decls;
8969
8970 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8971 auto It = LookupTables.find(DC);
8972 if (It == LookupTables.end())
8973 return;
8974
8975 NumRead++;
8976
8977 for (GlobalDeclID ID : It->second.Table.findAll()) {
8978 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8979 // Special case for namespaces: There can be a lot of redeclarations of
8980 // some namespaces, and we import a "key declaration" per imported module.
8981 // Since all declarations of a namespace are essentially interchangeable,
8982 // we can optimize namespace look-up by only storing the key declaration
8983 // of the current TU, rather than storing N key declarations where N is
8984 // the # of imported modules that declare that namespace.
8985 // TODO: Try to generalize this optimization to other redeclarable decls.
8986 if (isa<NamespaceDecl>(Val: ND))
8987 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8988 Decls[ND->getDeclName()].insert(ND);
8989 }
8990
8991 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8992 };
8993
8994 findAll(Lookups, NumVisibleDeclContextsRead);
8995 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8996 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8997
8998 for (auto &[Name, DS] : Decls)
8999 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
9000
9001 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
9002}
9003
9004const serialization::reader::DeclContextLookupTable *
9005ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
9006 auto I = Lookups.find(Val: Primary);
9007 return I == Lookups.end() ? nullptr : &I->second;
9008}
9009
9010const serialization::reader::ModuleLocalLookupTable *
9011ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
9012 auto I = ModuleLocalLookups.find(Val: Primary);
9013 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
9014}
9015
9016const serialization::reader::DeclContextLookupTable *
9017ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
9018 auto I = TULocalLookups.find(Val: Primary);
9019 return I == TULocalLookups.end() ? nullptr : &I->second;
9020}
9021
9022serialization::reader::LazySpecializationInfoLookupTable *
9023ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
9024 assert(D->isCanonicalDecl());
9025 auto &LookupTable =
9026 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
9027 auto I = LookupTable.find(Val: D);
9028 return I == LookupTable.end() ? nullptr : &I->second;
9029}
9030
9031bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
9032 assert(D->isCanonicalDecl());
9033 return PartialSpecializationsLookups.contains(Val: D) ||
9034 SpecializationsLookups.contains(Val: D);
9035}
9036
9037/// Under non-PCH compilation the consumer receives the objc methods
9038/// before receiving the implementation, and codegen depends on this.
9039/// We simulate this by deserializing and passing to consumer the methods of the
9040/// implementation before passing the deserialized implementation decl.
9041static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
9042 ASTConsumer *Consumer) {
9043 assert(ImplD && Consumer);
9044
9045 for (auto *I : ImplD->methods())
9046 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
9047
9048 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
9049}
9050
9051void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
9052 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
9053 PassObjCImplDeclToConsumer(ImplD, Consumer);
9054 else
9055 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9056}
9057
9058void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9059 Consumer->HandleVTable(RD);
9060}
9061
9062void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9063 this->Consumer = Consumer;
9064
9065 if (Consumer)
9066 PassInterestingDeclsToConsumer();
9067
9068 if (DeserializationListener)
9069 DeserializationListener->ReaderInitialized(Reader: this);
9070}
9071
9072void ASTReader::PrintStats() {
9073 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9074
9075 unsigned NumTypesLoaded =
9076 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9077 unsigned NumDeclsLoaded =
9078 DeclsLoaded.size() -
9079 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9080 unsigned NumIdentifiersLoaded =
9081 IdentifiersLoaded.size() -
9082 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9083 unsigned NumMacrosLoaded =
9084 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9085 unsigned NumSelectorsLoaded =
9086 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9087
9088 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9089 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9090 NumSLocEntriesRead, TotalNumSLocEntries,
9091 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9092 if (!TypesLoaded.empty())
9093 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9094 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9095 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9096 if (!DeclsLoaded.empty())
9097 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9098 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9099 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9100 if (!IdentifiersLoaded.empty())
9101 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9102 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9103 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9104 if (!MacrosLoaded.empty())
9105 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9106 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9107 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9108 if (!SelectorsLoaded.empty())
9109 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9110 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9111 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9112 if (TotalNumStatements)
9113 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9114 NumStatementsRead, TotalNumStatements,
9115 ((float)NumStatementsRead/TotalNumStatements * 100));
9116 if (TotalNumMacros)
9117 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9118 NumMacrosRead, TotalNumMacros,
9119 ((float)NumMacrosRead/TotalNumMacros * 100));
9120 if (TotalLexicalDeclContexts)
9121 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9122 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9123 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9124 * 100));
9125 if (TotalVisibleDeclContexts)
9126 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9127 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9128 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9129 * 100));
9130 if (TotalModuleLocalVisibleDeclContexts)
9131 std::fprintf(
9132 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9133 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9134 ((float)NumModuleLocalVisibleDeclContexts /
9135 TotalModuleLocalVisibleDeclContexts * 100));
9136 if (TotalTULocalVisibleDeclContexts)
9137 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9138 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9139 ((float)NumTULocalVisibleDeclContexts /
9140 TotalTULocalVisibleDeclContexts * 100));
9141 if (TotalNumMethodPoolEntries)
9142 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9143 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9144 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9145 * 100));
9146 if (NumMethodPoolLookups)
9147 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9148 NumMethodPoolHits, NumMethodPoolLookups,
9149 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9150 if (NumMethodPoolTableLookups)
9151 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9152 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9153 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9154 * 100.0));
9155 if (NumIdentifierLookupHits)
9156 std::fprintf(stderr,
9157 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9158 NumIdentifierLookupHits, NumIdentifierLookups,
9159 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9160
9161 if (GlobalIndex) {
9162 std::fprintf(stderr, format: "\n");
9163 GlobalIndex->printStats();
9164 }
9165
9166 std::fprintf(stderr, format: "\n");
9167 dump();
9168 std::fprintf(stderr, format: "\n");
9169}
9170
9171template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9172LLVM_DUMP_METHOD static void
9173dumpModuleIDMap(StringRef Name,
9174 const ContinuousRangeMap<Key, ModuleFile *,
9175 InitialCapacity> &Map) {
9176 if (Map.begin() == Map.end())
9177 return;
9178
9179 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9180
9181 llvm::errs() << Name << ":\n";
9182 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9183 I != IEnd; ++I)
9184 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9185 << "\n";
9186}
9187
9188LLVM_DUMP_METHOD void ASTReader::dump() {
9189 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9190 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9191 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9192 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9193 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9194 dumpModuleIDMap(Name: "Global preprocessed entity map",
9195 Map: GlobalPreprocessedEntityMap);
9196
9197 llvm::errs() << "\n*** PCH/Modules Loaded:";
9198 for (ModuleFile &M : ModuleMgr)
9199 M.dump();
9200}
9201
9202/// Return the amount of memory used by memory buffers, breaking down
9203/// by heap-backed versus mmap'ed memory.
9204void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9205 for (ModuleFile &I : ModuleMgr) {
9206 if (llvm::MemoryBuffer *buf = I.Buffer) {
9207 size_t bytes = buf->getBufferSize();
9208 switch (buf->getBufferKind()) {
9209 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9210 sizes.malloc_bytes += bytes;
9211 break;
9212 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9213 sizes.mmap_bytes += bytes;
9214 break;
9215 }
9216 }
9217 }
9218}
9219
9220void ASTReader::InitializeSema(Sema &S) {
9221 SemaObj = &S;
9222 S.addExternalSource(E: this);
9223
9224 // Makes sure any declarations that were deserialized "too early"
9225 // still get added to the identifier's declaration chains.
9226 for (GlobalDeclID ID : PreloadedDeclIDs) {
9227 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9228 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9229 }
9230 PreloadedDeclIDs.clear();
9231
9232 // FIXME: What happens if these are changed by a module import?
9233 if (!FPPragmaOptions.empty()) {
9234 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9235 FPOptionsOverride NewOverrides =
9236 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9237 SemaObj->CurFPFeatures =
9238 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9239 }
9240
9241 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9242 Decl *D = GetDecl(ID);
9243 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9244 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9245 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9246 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9247 else
9248 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9249 }
9250 DeclsWithEffectsToVerify.clear();
9251
9252 SemaObj->OpenCLFeatures = OpenCLExtensions;
9253
9254 UpdateSema();
9255}
9256
9257void ASTReader::UpdateSema() {
9258 assert(SemaObj && "no Sema to update");
9259
9260 // Load the offsets of the declarations that Sema references.
9261 // They will be lazily deserialized when needed.
9262 if (!SemaDeclRefs.empty()) {
9263 assert(SemaDeclRefs.size() % 3 == 0);
9264 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9265 if (!SemaObj->StdNamespace)
9266 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9267 if (!SemaObj->StdBadAlloc)
9268 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9269 if (!SemaObj->StdAlignValT)
9270 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9271 }
9272 SemaDeclRefs.clear();
9273 }
9274
9275 // Update the state of pragmas. Use the same API as if we had encountered the
9276 // pragma in the source.
9277 if(OptimizeOffPragmaLocation.isValid())
9278 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9279 if (PragmaMSStructState != -1)
9280 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9281 if (PointersToMembersPragmaLocation.isValid()) {
9282 SemaObj->ActOnPragmaMSPointersToMembers(
9283 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9284 PragmaMSPointersToMembersState,
9285 PragmaLoc: PointersToMembersPragmaLocation);
9286 }
9287 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9288 if (!RISCVVecIntrinsicPragma.empty()) {
9289 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9290 "Wrong number of RISCVVecIntrinsicPragma");
9291 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9292 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9293 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9294 }
9295
9296 if (PragmaAlignPackCurrentValue) {
9297 // The bottom of the stack might have a default value. It must be adjusted
9298 // to the current value to ensure that the packing state is preserved after
9299 // popping entries that were included/imported from a PCH/module.
9300 bool DropFirst = false;
9301 if (!PragmaAlignPackStack.empty() &&
9302 PragmaAlignPackStack.front().Location.isInvalid()) {
9303 assert(PragmaAlignPackStack.front().Value ==
9304 SemaObj->AlignPackStack.DefaultValue &&
9305 "Expected a default alignment value");
9306 SemaObj->AlignPackStack.Stack.emplace_back(
9307 Args&: PragmaAlignPackStack.front().SlotLabel,
9308 Args&: SemaObj->AlignPackStack.CurrentValue,
9309 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9310 Args&: PragmaAlignPackStack.front().PushLocation);
9311 DropFirst = true;
9312 }
9313 for (const auto &Entry :
9314 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9315 SemaObj->AlignPackStack.Stack.emplace_back(
9316 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9317 }
9318 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9319 assert(*PragmaAlignPackCurrentValue ==
9320 SemaObj->AlignPackStack.DefaultValue &&
9321 "Expected a default align and pack value");
9322 // Keep the current values.
9323 } else {
9324 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9325 SemaObj->AlignPackStack.CurrentPragmaLocation =
9326 PragmaAlignPackCurrentLocation;
9327 }
9328 }
9329 if (FpPragmaCurrentValue) {
9330 // The bottom of the stack might have a default value. It must be adjusted
9331 // to the current value to ensure that fp-pragma state is preserved after
9332 // popping entries that were included/imported from a PCH/module.
9333 bool DropFirst = false;
9334 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9335 assert(FpPragmaStack.front().Value ==
9336 SemaObj->FpPragmaStack.DefaultValue &&
9337 "Expected a default pragma float_control value");
9338 SemaObj->FpPragmaStack.Stack.emplace_back(
9339 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9340 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9341 Args&: FpPragmaStack.front().PushLocation);
9342 DropFirst = true;
9343 }
9344 for (const auto &Entry :
9345 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9346 SemaObj->FpPragmaStack.Stack.emplace_back(
9347 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9348 if (FpPragmaCurrentLocation.isInvalid()) {
9349 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9350 "Expected a default pragma float_control value");
9351 // Keep the current values.
9352 } else {
9353 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9354 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9355 }
9356 }
9357
9358 // For non-modular AST files, restore visiblity of modules.
9359 for (auto &Import : PendingImportedModulesSema) {
9360 if (Import.ImportLoc.isInvalid())
9361 continue;
9362 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9363 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9364 }
9365 }
9366 PendingImportedModulesSema.clear();
9367}
9368
9369IdentifierInfo *ASTReader::get(StringRef Name) {
9370 // Note that we are loading an identifier.
9371 Deserializing AnIdentifier(this);
9372
9373 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9374 NumIdentifierLookups,
9375 NumIdentifierLookupHits);
9376
9377 // We don't need to do identifier table lookups in C++ modules (we preload
9378 // all interesting declarations, and don't need to use the scope for name
9379 // lookups). Perform the lookup in PCH files, though, since we don't build
9380 // a complete initial identifier table if we're carrying on from a PCH.
9381 if (PP.getLangOpts().CPlusPlus) {
9382 for (auto *F : ModuleMgr.pch_modules())
9383 if (Visitor(*F))
9384 break;
9385 } else {
9386 // If there is a global index, look there first to determine which modules
9387 // provably do not have any results for this identifier.
9388 GlobalModuleIndex::HitSet Hits;
9389 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9390 if (!loadGlobalIndex()) {
9391 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9392 HitsPtr = &Hits;
9393 }
9394 }
9395
9396 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9397 }
9398
9399 IdentifierInfo *II = Visitor.getIdentifierInfo();
9400 markIdentifierUpToDate(II);
9401 return II;
9402}
9403
9404namespace clang {
9405
9406 /// An identifier-lookup iterator that enumerates all of the
9407 /// identifiers stored within a set of AST files.
9408 class ASTIdentifierIterator : public IdentifierIterator {
9409 /// The AST reader whose identifiers are being enumerated.
9410 const ASTReader &Reader;
9411
9412 /// The current index into the chain of AST files stored in
9413 /// the AST reader.
9414 unsigned Index;
9415
9416 /// The current position within the identifier lookup table
9417 /// of the current AST file.
9418 ASTIdentifierLookupTable::key_iterator Current;
9419
9420 /// The end position within the identifier lookup table of
9421 /// the current AST file.
9422 ASTIdentifierLookupTable::key_iterator End;
9423
9424 /// Whether to skip any modules in the ASTReader.
9425 bool SkipModules;
9426
9427 public:
9428 explicit ASTIdentifierIterator(const ASTReader &Reader,
9429 bool SkipModules = false);
9430
9431 StringRef Next() override;
9432 };
9433
9434} // namespace clang
9435
9436ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9437 bool SkipModules)
9438 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9439}
9440
9441StringRef ASTIdentifierIterator::Next() {
9442 while (Current == End) {
9443 // If we have exhausted all of our AST files, we're done.
9444 if (Index == 0)
9445 return StringRef();
9446
9447 --Index;
9448 ModuleFile &F = Reader.ModuleMgr[Index];
9449 if (SkipModules && F.isModule())
9450 continue;
9451
9452 ASTIdentifierLookupTable *IdTable =
9453 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9454 Current = IdTable->key_begin();
9455 End = IdTable->key_end();
9456 }
9457
9458 // We have any identifiers remaining in the current AST file; return
9459 // the next one.
9460 StringRef Result = *Current;
9461 ++Current;
9462 return Result;
9463}
9464
9465namespace {
9466
9467/// A utility for appending two IdentifierIterators.
9468class ChainedIdentifierIterator : public IdentifierIterator {
9469 std::unique_ptr<IdentifierIterator> Current;
9470 std::unique_ptr<IdentifierIterator> Queued;
9471
9472public:
9473 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9474 std::unique_ptr<IdentifierIterator> Second)
9475 : Current(std::move(First)), Queued(std::move(Second)) {}
9476
9477 StringRef Next() override {
9478 if (!Current)
9479 return StringRef();
9480
9481 StringRef result = Current->Next();
9482 if (!result.empty())
9483 return result;
9484
9485 // Try the queued iterator, which may itself be empty.
9486 Current.reset();
9487 std::swap(x&: Current, y&: Queued);
9488 return Next();
9489 }
9490};
9491
9492} // namespace
9493
9494IdentifierIterator *ASTReader::getIdentifiers() {
9495 if (!loadGlobalIndex()) {
9496 std::unique_ptr<IdentifierIterator> ReaderIter(
9497 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9498 std::unique_ptr<IdentifierIterator> ModulesIter(
9499 GlobalIndex->createIdentifierIterator());
9500 return new ChainedIdentifierIterator(std::move(ReaderIter),
9501 std::move(ModulesIter));
9502 }
9503
9504 return new ASTIdentifierIterator(*this);
9505}
9506
9507namespace clang {
9508namespace serialization {
9509
9510 class ReadMethodPoolVisitor {
9511 ASTReader &Reader;
9512 Selector Sel;
9513 unsigned PriorGeneration;
9514 unsigned InstanceBits = 0;
9515 unsigned FactoryBits = 0;
9516 bool InstanceHasMoreThanOneDecl = false;
9517 bool FactoryHasMoreThanOneDecl = false;
9518 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9519 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9520
9521 public:
9522 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9523 unsigned PriorGeneration)
9524 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9525
9526 bool operator()(ModuleFile &M) {
9527 if (!M.SelectorLookupTable)
9528 return false;
9529
9530 // If we've already searched this module file, skip it now.
9531 if (M.Generation <= PriorGeneration)
9532 return true;
9533
9534 ++Reader.NumMethodPoolTableLookups;
9535 ASTSelectorLookupTable *PoolTable
9536 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9537 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9538 if (Pos == PoolTable->end())
9539 return false;
9540
9541 ++Reader.NumMethodPoolTableHits;
9542 ++Reader.NumSelectorsRead;
9543 // FIXME: Not quite happy with the statistics here. We probably should
9544 // disable this tracking when called via LoadSelector.
9545 // Also, should entries without methods count as misses?
9546 ++Reader.NumMethodPoolEntriesRead;
9547 ASTSelectorLookupTrait::data_type Data = *Pos;
9548 if (Reader.DeserializationListener)
9549 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9550
9551 // Append methods in the reverse order, so that later we can process them
9552 // in the order they appear in the source code by iterating through
9553 // the vector in the reverse order.
9554 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9555 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9556 InstanceBits = Data.InstanceBits;
9557 FactoryBits = Data.FactoryBits;
9558 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9559 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9560 return false;
9561 }
9562
9563 /// Retrieve the instance methods found by this visitor.
9564 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9565 return InstanceMethods;
9566 }
9567
9568 /// Retrieve the instance methods found by this visitor.
9569 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9570 return FactoryMethods;
9571 }
9572
9573 unsigned getInstanceBits() const { return InstanceBits; }
9574 unsigned getFactoryBits() const { return FactoryBits; }
9575
9576 bool instanceHasMoreThanOneDecl() const {
9577 return InstanceHasMoreThanOneDecl;
9578 }
9579
9580 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9581 };
9582
9583} // namespace serialization
9584} // namespace clang
9585
9586/// Add the given set of methods to the method list.
9587static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9588 ObjCMethodList &List) {
9589 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9590 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9591}
9592
9593void ASTReader::ReadMethodPool(Selector Sel) {
9594 // Get the selector generation and update it to the current generation.
9595 unsigned &Generation = SelectorGeneration[Sel];
9596 unsigned PriorGeneration = Generation;
9597 Generation = getGeneration();
9598 SelectorOutOfDate[Sel] = false;
9599
9600 // Search for methods defined with this selector.
9601 ++NumMethodPoolLookups;
9602 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9603 ModuleMgr.visit(Visitor);
9604
9605 if (Visitor.getInstanceMethods().empty() &&
9606 Visitor.getFactoryMethods().empty())
9607 return;
9608
9609 ++NumMethodPoolHits;
9610
9611 if (!getSema())
9612 return;
9613
9614 Sema &S = *getSema();
9615 auto &Methods = S.ObjC().MethodPool[Sel];
9616
9617 Methods.first.setBits(Visitor.getInstanceBits());
9618 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9619 Methods.second.setBits(Visitor.getFactoryBits());
9620 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9621
9622 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9623 // when building a module we keep every method individually and may need to
9624 // update hasMoreThanOneDecl as we add the methods.
9625 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9626 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9627}
9628
9629void ASTReader::updateOutOfDateSelector(Selector Sel) {
9630 if (SelectorOutOfDate[Sel])
9631 ReadMethodPool(Sel);
9632}
9633
9634void ASTReader::ReadKnownNamespaces(
9635 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9636 Namespaces.clear();
9637
9638 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9639 if (NamespaceDecl *Namespace
9640 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9641 Namespaces.push_back(Elt: Namespace);
9642 }
9643}
9644
9645void ASTReader::ReadUndefinedButUsed(
9646 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9647 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9648 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9649 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9650 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9651 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9652 }
9653 UndefinedButUsed.clear();
9654}
9655
9656void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9657 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9658 Exprs) {
9659 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9660 FieldDecl *FD =
9661 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9662 uint64_t Count = DelayedDeleteExprs[Idx++];
9663 for (uint64_t C = 0; C < Count; ++C) {
9664 SourceLocation DeleteLoc =
9665 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9666 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9667 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9668 }
9669 }
9670}
9671
9672void ASTReader::ReadTentativeDefinitions(
9673 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9674 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9675 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9676 if (Var)
9677 TentativeDefs.push_back(Elt: Var);
9678 }
9679 TentativeDefinitions.clear();
9680}
9681
9682void ASTReader::ReadUnusedFileScopedDecls(
9683 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9684 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9685 DeclaratorDecl *D
9686 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9687 if (D)
9688 Decls.push_back(Elt: D);
9689 }
9690 UnusedFileScopedDecls.clear();
9691}
9692
9693void ASTReader::ReadDelegatingConstructors(
9694 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9695 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9696 CXXConstructorDecl *D
9697 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9698 if (D)
9699 Decls.push_back(Elt: D);
9700 }
9701 DelegatingCtorDecls.clear();
9702}
9703
9704void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9705 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9706 TypedefNameDecl *D
9707 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9708 if (D)
9709 Decls.push_back(Elt: D);
9710 }
9711 ExtVectorDecls.clear();
9712}
9713
9714void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9715 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9716 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9717 ++I) {
9718 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9719 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9720 if (D)
9721 Decls.insert(X: D);
9722 }
9723 UnusedLocalTypedefNameCandidates.clear();
9724}
9725
9726void ASTReader::ReadDeclsToCheckForDeferredDiags(
9727 llvm::SmallSetVector<Decl *, 4> &Decls) {
9728 for (auto I : DeclsToCheckForDeferredDiags) {
9729 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9730 if (D)
9731 Decls.insert(X: D);
9732 }
9733 DeclsToCheckForDeferredDiags.clear();
9734}
9735
9736void ASTReader::ReadReferencedSelectors(
9737 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9738 if (ReferencedSelectorsData.empty())
9739 return;
9740
9741 // If there are @selector references added them to its pool. This is for
9742 // implementation of -Wselector.
9743 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9744 unsigned I = 0;
9745 while (I < DataSize) {
9746 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9747 SourceLocation SelLoc
9748 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9749 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9750 }
9751 ReferencedSelectorsData.clear();
9752}
9753
9754void ASTReader::ReadWeakUndeclaredIdentifiers(
9755 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9756 if (WeakUndeclaredIdentifiers.empty())
9757 return;
9758
9759 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9760 IdentifierInfo *WeakId
9761 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9762 IdentifierInfo *AliasId
9763 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9764 SourceLocation Loc =
9765 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9766 WeakInfo WI(AliasId, Loc);
9767 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9768 }
9769 WeakUndeclaredIdentifiers.clear();
9770}
9771
9772void ASTReader::ReadExtnameUndeclaredIdentifiers(
9773 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9774 if (ExtnameUndeclaredIdentifiers.empty())
9775 return;
9776
9777 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9778 IdentifierInfo *NameId =
9779 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9780 IdentifierInfo *ExtnameId =
9781 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I + 1]);
9782 SourceLocation Loc =
9783 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I + 2]);
9784 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9785 Ctx&: getContext(), Label: ExtnameId->getName(),
9786 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9787 AttributeCommonInfo::Form::Pragma()));
9788 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9789 }
9790 ExtnameUndeclaredIdentifiers.clear();
9791}
9792
9793void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9794 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9795 ExternalVTableUse VT;
9796 VTableUse &TableInfo = VTableUses[Idx++];
9797 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9798 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9799 VT.DefinitionRequired = TableInfo.Used;
9800 VTables.push_back(Elt: VT);
9801 }
9802
9803 VTableUses.clear();
9804}
9805
9806void ASTReader::ReadPendingInstantiations(
9807 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9808 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9809 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9810 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9811 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9812
9813 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9814 }
9815 PendingInstantiations.clear();
9816}
9817
9818void ASTReader::ReadLateParsedTemplates(
9819 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9820 &LPTMap) {
9821 for (auto &LPT : LateParsedTemplates) {
9822 ModuleFile *FMod = LPT.first;
9823 RecordDataImpl &LateParsed = LPT.second;
9824 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9825 /* In loop */) {
9826 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9827
9828 auto LT = std::make_unique<LateParsedTemplate>();
9829 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9830 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9831
9832 ModuleFile *F = getOwningModuleFile(D: LT->D);
9833 assert(F && "No module");
9834
9835 unsigned TokN = LateParsed[Idx++];
9836 LT->Toks.reserve(N: TokN);
9837 for (unsigned T = 0; T < TokN; ++T)
9838 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9839
9840 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9841 }
9842 }
9843
9844 LateParsedTemplates.clear();
9845}
9846
9847void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9848 if (!Lambda->getLambdaContextDecl())
9849 return;
9850
9851 auto LambdaInfo =
9852 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9853 y: Lambda->getLambdaIndexInContext());
9854
9855 // Handle the import and then include case for lambdas.
9856 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9857 Iter != LambdaDeclarationsForMerging.end() &&
9858 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9859 CXXRecordDecl *Previous =
9860 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9861 Lambda->setPreviousDecl(Previous);
9862 return;
9863 }
9864
9865 // Keep track of this lambda so it can be merged with another lambda that
9866 // is loaded later.
9867 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9868}
9869
9870void ASTReader::LoadSelector(Selector Sel) {
9871 // It would be complicated to avoid reading the methods anyway. So don't.
9872 ReadMethodPool(Sel);
9873}
9874
9875void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9876 assert(ID && "Non-zero identifier ID required");
9877 unsigned Index = translateIdentifierIDToIndex(ID).second;
9878 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9879 IdentifiersLoaded[Index] = II;
9880 if (DeserializationListener)
9881 DeserializationListener->IdentifierRead(ID, II);
9882}
9883
9884/// Set the globally-visible declarations associated with the given
9885/// identifier.
9886///
9887/// If the AST reader is currently in a state where the given declaration IDs
9888/// cannot safely be resolved, they are queued until it is safe to resolve
9889/// them.
9890///
9891/// \param II an IdentifierInfo that refers to one or more globally-visible
9892/// declarations.
9893///
9894/// \param DeclIDs the set of declaration IDs with the name @p II that are
9895/// visible at global scope.
9896///
9897/// \param Decls if non-null, this vector will be populated with the set of
9898/// deserialized declarations. These declarations will not be pushed into
9899/// scope.
9900void ASTReader::SetGloballyVisibleDecls(
9901 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9902 SmallVectorImpl<Decl *> *Decls) {
9903 if (NumCurrentElementsDeserializing && !Decls) {
9904 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9905 return;
9906 }
9907
9908 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9909 if (!SemaObj) {
9910 // Queue this declaration so that it will be added to the
9911 // translation unit scope and identifier's declaration chain
9912 // once a Sema object is known.
9913 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9914 continue;
9915 }
9916
9917 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9918
9919 // If we're simply supposed to record the declarations, do so now.
9920 if (Decls) {
9921 Decls->push_back(Elt: D);
9922 continue;
9923 }
9924
9925 // Introduce this declaration into the translation-unit scope
9926 // and add it to the declaration chain for this identifier, so
9927 // that (unqualified) name lookup will find it.
9928 pushExternalDeclIntoScope(D, Name: II);
9929 }
9930}
9931
9932std::pair<ModuleFile *, unsigned>
9933ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9934 if (ID == 0)
9935 return {nullptr, 0};
9936
9937 unsigned ModuleFileIndex = ID >> 32;
9938 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9939
9940 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9941 assert(getModuleManager().size() > ModuleFileIndex - 1);
9942
9943 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9944 assert(LocalID < MF.LocalNumIdentifiers);
9945 return {&MF, MF.BaseIdentifierID + LocalID};
9946}
9947
9948IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9949 if (ID == 0)
9950 return nullptr;
9951
9952 if (IdentifiersLoaded.empty()) {
9953 Error(Msg: "no identifier table in AST file");
9954 return nullptr;
9955 }
9956
9957 auto [M, Index] = translateIdentifierIDToIndex(ID);
9958 if (!IdentifiersLoaded[Index]) {
9959 assert(M != nullptr && "Untranslated Identifier ID?");
9960 assert(Index >= M->BaseIdentifierID);
9961 unsigned LocalIndex = Index - M->BaseIdentifierID;
9962 const unsigned char *Data =
9963 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9964
9965 ASTIdentifierLookupTrait Trait(*this, *M);
9966 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9967 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9968 auto &II = PP.getIdentifierTable().get(Name: Key);
9969 IdentifiersLoaded[Index] = &II;
9970 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9971 markIdentifierFromAST(Reader&: *this, II, IsModule);
9972 if (DeserializationListener)
9973 DeserializationListener->IdentifierRead(ID, II: &II);
9974 }
9975
9976 return IdentifiersLoaded[Index];
9977}
9978
9979IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9980 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9981}
9982
9983IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9984 if (LocalID < NUM_PREDEF_IDENT_IDS)
9985 return LocalID;
9986
9987 if (!M.ModuleOffsetMap.empty())
9988 ReadModuleOffsetMap(F&: M);
9989
9990 unsigned ModuleFileIndex = LocalID >> 32;
9991 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9992 ModuleFile *MF =
9993 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9994 assert(MF && "malformed identifier ID encoding?");
9995
9996 if (!ModuleFileIndex)
9997 LocalID -= NUM_PREDEF_IDENT_IDS;
9998
9999 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
10000}
10001
10002std::pair<ModuleFile *, unsigned>
10003ASTReader::translateMacroIDToIndex(MacroID ID) const {
10004 if (ID == 0)
10005 return {nullptr, 0};
10006
10007 unsigned ModuleFileIndex = ID >> 32;
10008 assert(ModuleFileIndex && "not translating loaded MacroID?");
10009 assert(getModuleManager().size() > ModuleFileIndex - 1);
10010 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
10011
10012 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
10013 assert(LocalID < MF.LocalNumMacros);
10014 return {&MF, MF.BaseMacroID + LocalID};
10015}
10016
10017MacroInfo *ASTReader::getMacro(MacroID ID) {
10018 if (ID == 0)
10019 return nullptr;
10020
10021 if (MacrosLoaded.empty()) {
10022 Error(Msg: "no macro table in AST file");
10023 return nullptr;
10024 }
10025
10026 auto [M, Index] = translateMacroIDToIndex(ID);
10027 if (!MacrosLoaded[Index]) {
10028 assert(M != nullptr && "Untranslated Macro ID?");
10029 assert(Index >= M->BaseMacroID);
10030 unsigned LocalIndex = Index - M->BaseMacroID;
10031 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
10032 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
10033
10034 if (DeserializationListener)
10035 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
10036 }
10037
10038 return MacrosLoaded[Index];
10039}
10040
10041MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
10042 if (LocalID < NUM_PREDEF_MACRO_IDS)
10043 return LocalID;
10044
10045 if (!M.ModuleOffsetMap.empty())
10046 ReadModuleOffsetMap(F&: M);
10047
10048 unsigned ModuleFileIndex = LocalID >> 32;
10049 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
10050 ModuleFile *MF =
10051 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
10052 assert(MF && "malformed identifier ID encoding?");
10053
10054 if (!ModuleFileIndex) {
10055 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10056 LocalID -= NUM_PREDEF_MACRO_IDS;
10057 }
10058
10059 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10060}
10061
10062serialization::SubmoduleID
10063ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10064 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10065 return LocalID;
10066
10067 if (!M.ModuleOffsetMap.empty())
10068 ReadModuleOffsetMap(F&: M);
10069
10070 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10071 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10072 assert(I != M.SubmoduleRemap.end()
10073 && "Invalid index into submodule index remap");
10074
10075 return LocalID + I->second;
10076}
10077
10078Module *ASTReader::getModule(unsigned ID) {
10079 return getSubmodule(GlobalID: ID);
10080}
10081
10082ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10083 if (ID & 1) {
10084 // It's a module, look it up by submodule ID.
10085 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10086 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10087 } else {
10088 // It's a prefix (preamble, PCH, ...). Look it up by index.
10089 int IndexFromEnd = static_cast<int>(ID >> 1);
10090 assert(IndexFromEnd && "got reference to unknown module file");
10091 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10092 }
10093}
10094
10095unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10096 if (!M)
10097 return 1;
10098
10099 // For a file representing a module, use the submodule ID of the top-level
10100 // module as the file ID. For any other kind of file, the number of such
10101 // files loaded beforehand will be the same on reload.
10102 // FIXME: Is this true even if we have an explicit module file and a PCH?
10103 if (M->isModule())
10104 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10105
10106 auto PCHModules = getModuleManager().pch_modules();
10107 auto I = llvm::find(Range&: PCHModules, Val: M);
10108 assert(I != PCHModules.end() && "emitting reference to unknown file");
10109 return std::distance(first: I, last: PCHModules.end()) << 1;
10110}
10111
10112std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10113 if (Module *M = getSubmodule(GlobalID: ID))
10114 return ASTSourceDescriptor(*M);
10115
10116 // If there is only a single PCH, return it instead.
10117 // Chained PCH are not supported.
10118 const auto &PCHChain = ModuleMgr.pch_modules();
10119 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10120 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10121 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10122 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10123 return ASTSourceDescriptor(ModuleName,
10124 llvm::sys::path::parent_path(path: MF.FileName),
10125 FileName, MF.Signature);
10126 }
10127 return std::nullopt;
10128}
10129
10130ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10131 auto I = DefinitionSource.find(Val: FD);
10132 if (I == DefinitionSource.end())
10133 return EK_ReplyHazy;
10134 return I->second ? EK_Never : EK_Always;
10135}
10136
10137bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10138 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10139}
10140
10141Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10142 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10143}
10144
10145Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10146 if (ID == 0)
10147 return Selector();
10148
10149 if (ID > SelectorsLoaded.size()) {
10150 Error(Msg: "selector ID out of range in AST file");
10151 return Selector();
10152 }
10153
10154 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10155 // Load this selector from the selector table.
10156 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10157 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10158 ModuleFile &M = *I->second;
10159 ASTSelectorLookupTrait Trait(*this, M);
10160 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10161 SelectorsLoaded[ID - 1] =
10162 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10163 if (DeserializationListener)
10164 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10165 }
10166
10167 return SelectorsLoaded[ID - 1];
10168}
10169
10170Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10171 return DecodeSelector(ID);
10172}
10173
10174uint32_t ASTReader::GetNumExternalSelectors() {
10175 // ID 0 (the null selector) is considered an external selector.
10176 return getTotalNumSelectors() + 1;
10177}
10178
10179serialization::SelectorID
10180ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10181 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10182 return LocalID;
10183
10184 if (!M.ModuleOffsetMap.empty())
10185 ReadModuleOffsetMap(F&: M);
10186
10187 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10188 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10189 assert(I != M.SelectorRemap.end()
10190 && "Invalid index into selector index remap");
10191
10192 return LocalID + I->second;
10193}
10194
10195DeclarationNameLoc
10196ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10197 switch (Name.getNameKind()) {
10198 case DeclarationName::CXXConstructorName:
10199 case DeclarationName::CXXDestructorName:
10200 case DeclarationName::CXXConversionFunctionName:
10201 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10202
10203 case DeclarationName::CXXOperatorName:
10204 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10205
10206 case DeclarationName::CXXLiteralOperatorName:
10207 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10208 Loc: readSourceLocation());
10209
10210 case DeclarationName::Identifier:
10211 case DeclarationName::ObjCZeroArgSelector:
10212 case DeclarationName::ObjCOneArgSelector:
10213 case DeclarationName::ObjCMultiArgSelector:
10214 case DeclarationName::CXXUsingDirective:
10215 case DeclarationName::CXXDeductionGuideName:
10216 break;
10217 }
10218 return DeclarationNameLoc();
10219}
10220
10221DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10222 DeclarationNameInfo NameInfo;
10223 NameInfo.setName(readDeclarationName());
10224 NameInfo.setLoc(readSourceLocation());
10225 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10226 return NameInfo;
10227}
10228
10229TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10230 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10231}
10232
10233SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10234 auto Kind = readInt();
10235 auto ResultType = readQualType();
10236 auto Value = readAPInt();
10237 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10238 assert(Op.isValid());
10239 return Op;
10240}
10241
10242void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10243 Info.QualifierLoc = readNestedNameSpecifierLoc();
10244 unsigned NumTPLists = readInt();
10245 Info.NumTemplParamLists = NumTPLists;
10246 if (NumTPLists) {
10247 Info.TemplParamLists =
10248 new (getContext()) TemplateParameterList *[NumTPLists];
10249 for (unsigned i = 0; i != NumTPLists; ++i)
10250 Info.TemplParamLists[i] = readTemplateParameterList();
10251 }
10252}
10253
10254TemplateParameterList *
10255ASTRecordReader::readTemplateParameterList() {
10256 SourceLocation TemplateLoc = readSourceLocation();
10257 SourceLocation LAngleLoc = readSourceLocation();
10258 SourceLocation RAngleLoc = readSourceLocation();
10259
10260 unsigned NumParams = readInt();
10261 SmallVector<NamedDecl *, 16> Params;
10262 Params.reserve(N: NumParams);
10263 while (NumParams--)
10264 Params.push_back(Elt: readDeclAs<NamedDecl>());
10265
10266 bool HasRequiresClause = readBool();
10267 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10268
10269 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10270 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10271 return TemplateParams;
10272}
10273
10274void ASTRecordReader::readTemplateArgumentList(
10275 SmallVectorImpl<TemplateArgument> &TemplArgs,
10276 bool Canonicalize) {
10277 unsigned NumTemplateArgs = readInt();
10278 TemplArgs.reserve(N: NumTemplateArgs);
10279 while (NumTemplateArgs--)
10280 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10281}
10282
10283/// Read a UnresolvedSet structure.
10284void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10285 unsigned NumDecls = readInt();
10286 Set.reserve(C&: getContext(), N: NumDecls);
10287 while (NumDecls--) {
10288 GlobalDeclID ID = readDeclID();
10289 AccessSpecifier AS = (AccessSpecifier) readInt();
10290 Set.addLazyDecl(C&: getContext(), ID, AS);
10291 }
10292}
10293
10294CXXBaseSpecifier
10295ASTRecordReader::readCXXBaseSpecifier() {
10296 bool isVirtual = readBool();
10297 bool isBaseOfClass = readBool();
10298 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10299 bool inheritConstructors = readBool();
10300 TypeSourceInfo *TInfo = readTypeSourceInfo();
10301 SourceRange Range = readSourceRange();
10302 SourceLocation EllipsisLoc = readSourceLocation();
10303 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10304 EllipsisLoc);
10305 Result.setInheritConstructors(inheritConstructors);
10306 return Result;
10307}
10308
10309CXXCtorInitializer **
10310ASTRecordReader::readCXXCtorInitializers() {
10311 ASTContext &Context = getContext();
10312 unsigned NumInitializers = readInt();
10313 assert(NumInitializers && "wrote ctor initializers but have no inits");
10314 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10315 for (unsigned i = 0; i != NumInitializers; ++i) {
10316 TypeSourceInfo *TInfo = nullptr;
10317 bool IsBaseVirtual = false;
10318 FieldDecl *Member = nullptr;
10319 IndirectFieldDecl *IndirectMember = nullptr;
10320
10321 CtorInitializerType Type = (CtorInitializerType) readInt();
10322 switch (Type) {
10323 case CTOR_INITIALIZER_BASE:
10324 TInfo = readTypeSourceInfo();
10325 IsBaseVirtual = readBool();
10326 break;
10327
10328 case CTOR_INITIALIZER_DELEGATING:
10329 TInfo = readTypeSourceInfo();
10330 break;
10331
10332 case CTOR_INITIALIZER_MEMBER:
10333 Member = readDeclAs<FieldDecl>();
10334 break;
10335
10336 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10337 IndirectMember = readDeclAs<IndirectFieldDecl>();
10338 break;
10339 }
10340
10341 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10342 Expr *Init = readExpr();
10343 SourceLocation LParenLoc = readSourceLocation();
10344 SourceLocation RParenLoc = readSourceLocation();
10345
10346 CXXCtorInitializer *BOMInit;
10347 if (Type == CTOR_INITIALIZER_BASE)
10348 BOMInit = new (Context)
10349 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10350 RParenLoc, MemberOrEllipsisLoc);
10351 else if (Type == CTOR_INITIALIZER_DELEGATING)
10352 BOMInit = new (Context)
10353 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10354 else if (Member)
10355 BOMInit = new (Context)
10356 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10357 Init, RParenLoc);
10358 else
10359 BOMInit = new (Context)
10360 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10361 LParenLoc, Init, RParenLoc);
10362
10363 if (/*IsWritten*/readBool()) {
10364 unsigned SourceOrder = readInt();
10365 BOMInit->setSourceOrder(SourceOrder);
10366 }
10367
10368 CtorInitializers[i] = BOMInit;
10369 }
10370
10371 return CtorInitializers;
10372}
10373
10374NestedNameSpecifierLoc
10375ASTRecordReader::readNestedNameSpecifierLoc() {
10376 ASTContext &Context = getContext();
10377 unsigned N = readInt();
10378 NestedNameSpecifierLocBuilder Builder;
10379 for (unsigned I = 0; I != N; ++I) {
10380 auto Kind = readNestedNameSpecifierKind();
10381 switch (Kind) {
10382 case NestedNameSpecifier::Kind::Namespace: {
10383 auto *NS = readDeclAs<NamespaceBaseDecl>();
10384 SourceRange Range = readSourceRange();
10385 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10386 break;
10387 }
10388
10389 case NestedNameSpecifier::Kind::Type: {
10390 TypeSourceInfo *T = readTypeSourceInfo();
10391 if (!T)
10392 return NestedNameSpecifierLoc();
10393 SourceLocation ColonColonLoc = readSourceLocation();
10394 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10395 break;
10396 }
10397
10398 case NestedNameSpecifier::Kind::Global: {
10399 SourceLocation ColonColonLoc = readSourceLocation();
10400 Builder.MakeGlobal(Context, ColonColonLoc);
10401 break;
10402 }
10403
10404 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10405 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10406 SourceRange Range = readSourceRange();
10407 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10408 break;
10409 }
10410
10411 case NestedNameSpecifier::Kind::Null:
10412 llvm_unreachable("unexpected null nested name specifier");
10413 }
10414 }
10415
10416 return Builder.getWithLocInContext(Context);
10417}
10418
10419SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10420 unsigned &Idx) {
10421 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10422 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10423 return SourceRange(beg, end);
10424}
10425
10426llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10427 const StringRef Blob) {
10428 unsigned Count = Record[0];
10429 const char *Byte = Blob.data();
10430 llvm::BitVector Ret = llvm::BitVector(Count, false);
10431 for (unsigned I = 0; I < Count; ++Byte)
10432 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10433 if (*Byte & (1 << Bit))
10434 Ret[I] = true;
10435 return Ret;
10436}
10437
10438/// Read a floating-point value
10439llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10440 return llvm::APFloat(Sem, readAPInt());
10441}
10442
10443// Read a string
10444std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10445 unsigned Len = Record[Idx++];
10446 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10447 Idx += Len;
10448 return Result;
10449}
10450
10451StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10452 StringRef &Blob) {
10453 unsigned Len = Record[Idx++];
10454 StringRef Result = Blob.substr(Start: 0, N: Len);
10455 Blob = Blob.substr(Start: Len);
10456 return Result;
10457}
10458
10459std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10460 unsigned &Idx) {
10461 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10462}
10463
10464std::string ASTReader::ReadPath(StringRef BaseDirectory,
10465 const RecordData &Record, unsigned &Idx) {
10466 std::string Filename = ReadString(Record, Idx);
10467 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10468}
10469
10470std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10471 const RecordData &Record, unsigned &Idx,
10472 StringRef &Blob) {
10473 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10474 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10475}
10476
10477VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10478 unsigned &Idx) {
10479 unsigned Major = Record[Idx++];
10480 unsigned Minor = Record[Idx++];
10481 unsigned Subminor = Record[Idx++];
10482 if (Minor == 0)
10483 return VersionTuple(Major);
10484 if (Subminor == 0)
10485 return VersionTuple(Major, Minor - 1);
10486 return VersionTuple(Major, Minor - 1, Subminor - 1);
10487}
10488
10489CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10490 const RecordData &Record,
10491 unsigned &Idx) {
10492 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10493 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10494}
10495
10496DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10497 return Diag(Loc: CurrentImportLoc, DiagID);
10498}
10499
10500DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10501 return Diags.Report(Loc, DiagID);
10502}
10503
10504void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10505 llvm::function_ref<void()> Fn) {
10506 // When Sema is available, avoid duplicate errors.
10507 if (SemaObj) {
10508 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10509 return;
10510 }
10511
10512 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10513}
10514
10515/// Retrieve the identifier table associated with the
10516/// preprocessor.
10517IdentifierTable &ASTReader::getIdentifierTable() {
10518 return PP.getIdentifierTable();
10519}
10520
10521/// Record that the given ID maps to the given switch-case
10522/// statement.
10523void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10524 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10525 "Already have a SwitchCase with this ID");
10526 (*CurrSwitchCaseStmts)[ID] = SC;
10527}
10528
10529/// Retrieve the switch-case statement with the given ID.
10530SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10531 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10532 return (*CurrSwitchCaseStmts)[ID];
10533}
10534
10535void ASTReader::ClearSwitchCaseIDs() {
10536 CurrSwitchCaseStmts->clear();
10537}
10538
10539void ASTReader::ReadComments() {
10540 ASTContext &Context = getContext();
10541 std::vector<RawComment *> Comments;
10542 for (SmallVectorImpl<std::pair<BitstreamCursor,
10543 serialization::ModuleFile *>>::iterator
10544 I = CommentsCursors.begin(),
10545 E = CommentsCursors.end();
10546 I != E; ++I) {
10547 Comments.clear();
10548 BitstreamCursor &Cursor = I->first;
10549 serialization::ModuleFile &F = *I->second;
10550 SavedStreamPosition SavedPosition(Cursor);
10551
10552 RecordData Record;
10553 while (true) {
10554 Expected<llvm::BitstreamEntry> MaybeEntry =
10555 Cursor.advanceSkippingSubblocks(
10556 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10557 if (!MaybeEntry) {
10558 Error(Err: MaybeEntry.takeError());
10559 return;
10560 }
10561 llvm::BitstreamEntry Entry = MaybeEntry.get();
10562
10563 switch (Entry.Kind) {
10564 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10565 case llvm::BitstreamEntry::Error:
10566 Error(Msg: "malformed block record in AST file");
10567 return;
10568 case llvm::BitstreamEntry::EndBlock:
10569 goto NextCursor;
10570 case llvm::BitstreamEntry::Record:
10571 // The interesting case.
10572 break;
10573 }
10574
10575 // Read a record.
10576 Record.clear();
10577 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10578 if (!MaybeComment) {
10579 Error(Err: MaybeComment.takeError());
10580 return;
10581 }
10582 switch ((CommentRecordTypes)MaybeComment.get()) {
10583 case COMMENTS_RAW_COMMENT: {
10584 unsigned Idx = 0;
10585 SourceRange SR = ReadSourceRange(F, Record, Idx);
10586 RawComment::CommentKind Kind =
10587 (RawComment::CommentKind) Record[Idx++];
10588 bool IsTrailingComment = Record[Idx++];
10589 bool IsAlmostTrailingComment = Record[Idx++];
10590 Comments.push_back(x: new (Context) RawComment(
10591 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10592 break;
10593 }
10594 }
10595 }
10596 NextCursor:
10597 for (RawComment *C : Comments) {
10598 SourceLocation CommentLoc = C->getBeginLoc();
10599 if (CommentLoc.isValid()) {
10600 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10601 if (Loc.first.isValid())
10602 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10603 }
10604 }
10605 }
10606}
10607
10608void ASTReader::visitInputFileInfos(
10609 serialization::ModuleFile &MF, bool IncludeSystem,
10610 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10611 bool IsSystem)>
10612 Visitor) {
10613 unsigned NumUserInputs = MF.NumUserInputFiles;
10614 unsigned NumInputs = MF.InputFilesLoaded.size();
10615 assert(NumUserInputs <= NumInputs);
10616 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10617 for (unsigned I = 0; I < N; ++I) {
10618 bool IsSystem = I >= NumUserInputs;
10619 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10620 Visitor(IFI, IsSystem);
10621 }
10622}
10623
10624void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10625 bool IncludeSystem, bool Complain,
10626 llvm::function_ref<void(const serialization::InputFile &IF,
10627 bool isSystem)> Visitor) {
10628 unsigned NumUserInputs = MF.NumUserInputFiles;
10629 unsigned NumInputs = MF.InputFilesLoaded.size();
10630 assert(NumUserInputs <= NumInputs);
10631 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10632 for (unsigned I = 0; I < N; ++I) {
10633 bool IsSystem = I >= NumUserInputs;
10634 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10635 Visitor(IF, IsSystem);
10636 }
10637}
10638
10639void ASTReader::visitTopLevelModuleMaps(
10640 serialization::ModuleFile &MF,
10641 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10642 unsigned NumInputs = MF.InputFilesLoaded.size();
10643 for (unsigned I = 0; I < NumInputs; ++I) {
10644 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10645 if (IFI.TopLevel && IFI.ModuleMap)
10646 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10647 Visitor(*FE);
10648 }
10649}
10650
10651void ASTReader::finishPendingActions() {
10652 while (!PendingIdentifierInfos.empty() ||
10653 !PendingDeducedFunctionTypes.empty() ||
10654 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10655 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10656 !PendingUpdateRecords.empty() ||
10657 !PendingObjCExtensionIvarRedeclarations.empty()) {
10658 // If any identifiers with corresponding top-level declarations have
10659 // been loaded, load those declarations now.
10660 using TopLevelDeclsMap =
10661 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10662 TopLevelDeclsMap TopLevelDecls;
10663
10664 while (!PendingIdentifierInfos.empty()) {
10665 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10666 SmallVector<GlobalDeclID, 4> DeclIDs =
10667 std::move(PendingIdentifierInfos.back().second);
10668 PendingIdentifierInfos.pop_back();
10669
10670 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10671 }
10672
10673 // Load each function type that we deferred loading because it was a
10674 // deduced type that might refer to a local type declared within itself.
10675 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10676 auto *FD = PendingDeducedFunctionTypes[I].first;
10677 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10678
10679 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10680 // If we gave a function a deduced return type, remember that we need to
10681 // propagate that along the redeclaration chain.
10682 if (DT->isDeduced()) {
10683 PendingDeducedTypeUpdates.insert(
10684 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10685 continue;
10686 }
10687
10688 // The function has undeduced DeduceType return type. We hope we can
10689 // find the deduced type by iterating the redecls in other modules
10690 // later.
10691 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10692 continue;
10693 }
10694 }
10695 PendingDeducedFunctionTypes.clear();
10696
10697 // Load each variable type that we deferred loading because it was a
10698 // deduced type that might refer to a local type declared within itself.
10699 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10700 auto *VD = PendingDeducedVarTypes[I].first;
10701 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10702 }
10703 PendingDeducedVarTypes.clear();
10704
10705 // Load pending declaration chains.
10706 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10707 loadPendingDeclChain(D: PendingDeclChains[I].first,
10708 LocalOffset: PendingDeclChains[I].second);
10709 PendingDeclChains.clear();
10710
10711 // Make the most recent of the top-level declarations visible.
10712 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10713 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10714 IdentifierInfo *II = TLD->first;
10715 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10716 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10717 }
10718 }
10719
10720 // Load any pending macro definitions.
10721 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10722 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10723 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10724 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10725 // Initialize the macro history from chained-PCHs ahead of module imports.
10726 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10727 ++IDIdx) {
10728 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10729 if (!Info.M->isModule())
10730 resolvePendingMacro(II, PMInfo: Info);
10731 }
10732 // Handle module imports.
10733 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10734 ++IDIdx) {
10735 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10736 if (Info.M->isModule())
10737 resolvePendingMacro(II, PMInfo: Info);
10738 }
10739 }
10740 PendingMacroIDs.clear();
10741
10742 // Wire up the DeclContexts for Decls that we delayed setting until
10743 // recursive loading is completed.
10744 while (!PendingDeclContextInfos.empty()) {
10745 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10746 PendingDeclContextInfos.pop_front();
10747 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10748 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10749 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10750 }
10751
10752 // Perform any pending declaration updates.
10753 while (!PendingUpdateRecords.empty()) {
10754 auto Update = PendingUpdateRecords.pop_back_val();
10755 ReadingKindTracker ReadingKind(Read_Decl, *this);
10756 loadDeclUpdateRecords(Record&: Update);
10757 }
10758
10759 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10760 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10761 auto DuplicateIvars =
10762 PendingObjCExtensionIvarRedeclarations.back().second;
10763 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10764 StructuralEquivalenceContext Ctx(
10765 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10766 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10767 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10768 /*Complain =*/false,
10769 /*ErrorOnTagTypeMismatch =*/true);
10770 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10771 // Merge redeclared ivars with their predecessors.
10772 for (auto IvarPair : DuplicateIvars) {
10773 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10774 // Change semantic DeclContext but keep the lexical one.
10775 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10776 LexicalDC: Ivar->getLexicalDeclContext(),
10777 Ctx&: getContext());
10778 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10779 }
10780 // Invalidate duplicate extension and the cached ivar list.
10781 ExtensionsPair.first->setInvalidDecl();
10782 ExtensionsPair.second->getClassInterface()
10783 ->getDefinition()
10784 ->setIvarList(nullptr);
10785 } else {
10786 for (auto IvarPair : DuplicateIvars) {
10787 Diag(Loc: IvarPair.first->getLocation(),
10788 DiagID: diag::err_duplicate_ivar_declaration)
10789 << IvarPair.first->getIdentifier();
10790 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10791 }
10792 }
10793 PendingObjCExtensionIvarRedeclarations.pop_back();
10794 }
10795 }
10796
10797 // At this point, all update records for loaded decls are in place, so any
10798 // fake class definitions should have become real.
10799 assert(PendingFakeDefinitionData.empty() &&
10800 "faked up a class definition but never saw the real one");
10801
10802 // If we deserialized any C++ or Objective-C class definitions, any
10803 // Objective-C protocol definitions, or any redeclarable templates, make sure
10804 // that all redeclarations point to the definitions. Note that this can only
10805 // happen now, after the redeclaration chains have been fully wired.
10806 for (Decl *D : PendingDefinitions) {
10807 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10808 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10809 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10810 R = R->getPreviousDecl()) {
10811 assert((R == D) ==
10812 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10813 "declaration thinks it's the definition but it isn't");
10814 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10815 }
10816 }
10817
10818 continue;
10819 }
10820
10821 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10822 // Make sure that the ObjCInterfaceType points at the definition.
10823 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10824 ->Decl = ID;
10825
10826 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10827 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10828
10829 continue;
10830 }
10831
10832 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10833 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10834 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10835
10836 continue;
10837 }
10838
10839 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10840 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10841 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10842 }
10843 PendingDefinitions.clear();
10844
10845 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10846 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10847 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10848 return VD->isThisDeclarationADefinition() ||
10849 VD->isThisDeclarationADemotedDefinition();
10850
10851 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10852 return TD->isThisDeclarationADefinition() ||
10853 TD->isThisDeclarationADemotedDefinition();
10854
10855 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10856 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10857
10858 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10859 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10860
10861 // Conservatively return false here.
10862 return false;
10863 };
10864
10865 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10866 return hasDefinitionImpl(D, hasDefinitionImpl);
10867 };
10868
10869 // It is not good to prevent multiple declarations since the forward
10870 // declaration is common. Let's try to avoid duplicated definitions
10871 // only.
10872 if (!hasDefinition(D) || !hasDefinition(Previous))
10873 continue;
10874
10875 Module *PM = Previous->getOwningModule();
10876 Module *DM = D->getOwningModule();
10877 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10878 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10879 << (DM ? DM->getTopLevelModuleName() : "global module");
10880 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10881 }
10882 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10883
10884 // Load the bodies of any functions or methods we've encountered. We do
10885 // this now (delayed) so that we can be sure that the declaration chains
10886 // have been fully wired up (hasBody relies on this).
10887 // FIXME: We shouldn't require complete redeclaration chains here.
10888 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10889 PBEnd = PendingBodies.end();
10890 PB != PBEnd; ++PB) {
10891 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10892 // FIXME: Check for =delete/=default?
10893 const FunctionDecl *Defn = nullptr;
10894 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10895 FD->setLazyBody(PB->second);
10896 } else {
10897 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10898 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10899
10900 if (!FD->isLateTemplateParsed() &&
10901 !NonConstDefn->isLateTemplateParsed() &&
10902 // We only perform ODR checks for decls not in the explicit
10903 // global module fragment.
10904 !shouldSkipCheckingODR(D: FD) &&
10905 !shouldSkipCheckingODR(D: NonConstDefn) &&
10906 FD->getODRHash() != NonConstDefn->getODRHash()) {
10907 if (!isa<CXXMethodDecl>(Val: FD)) {
10908 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10909 } else if (FD->getLexicalParent()->isFileContext() &&
10910 NonConstDefn->getLexicalParent()->isFileContext()) {
10911 // Only diagnose out-of-line method definitions. If they are
10912 // in class definitions, then an error will be generated when
10913 // processing the class bodies.
10914 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10915 }
10916 }
10917 }
10918 continue;
10919 }
10920
10921 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10922 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10923 MD->setLazyBody(PB->second);
10924 }
10925 PendingBodies.clear();
10926
10927 // Inform any classes that had members added that they now have more members.
10928 for (auto [RD, MD] : PendingAddedClassMembers) {
10929 RD->addedMember(D: MD);
10930 }
10931 PendingAddedClassMembers.clear();
10932
10933 // Do some cleanup.
10934 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10935 getContext().deduplicateMergedDefinitionsFor(ND);
10936 PendingMergedDefinitionsToDeduplicate.clear();
10937
10938 // For each decl chain that we wanted to complete while deserializing, mark
10939 // it as "still needs to be completed".
10940 for (Decl *D : PendingIncompleteDeclChains)
10941 markIncompleteDeclChain(D);
10942 PendingIncompleteDeclChains.clear();
10943
10944 assert(PendingIdentifierInfos.empty() &&
10945 "Should be empty at the end of finishPendingActions");
10946 assert(PendingDeducedFunctionTypes.empty() &&
10947 "Should be empty at the end of finishPendingActions");
10948 assert(PendingDeducedVarTypes.empty() &&
10949 "Should be empty at the end of finishPendingActions");
10950 assert(PendingDeclChains.empty() &&
10951 "Should be empty at the end of finishPendingActions");
10952 assert(PendingMacroIDs.empty() &&
10953 "Should be empty at the end of finishPendingActions");
10954 assert(PendingDeclContextInfos.empty() &&
10955 "Should be empty at the end of finishPendingActions");
10956 assert(PendingUpdateRecords.empty() &&
10957 "Should be empty at the end of finishPendingActions");
10958 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10959 "Should be empty at the end of finishPendingActions");
10960 assert(PendingFakeDefinitionData.empty() &&
10961 "Should be empty at the end of finishPendingActions");
10962 assert(PendingDefinitions.empty() &&
10963 "Should be empty at the end of finishPendingActions");
10964 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10965 "Should be empty at the end of finishPendingActions");
10966 assert(PendingBodies.empty() &&
10967 "Should be empty at the end of finishPendingActions");
10968 assert(PendingAddedClassMembers.empty() &&
10969 "Should be empty at the end of finishPendingActions");
10970 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10971 "Should be empty at the end of finishPendingActions");
10972 assert(PendingIncompleteDeclChains.empty() &&
10973 "Should be empty at the end of finishPendingActions");
10974}
10975
10976void ASTReader::diagnoseOdrViolations() {
10977 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10978 PendingRecordOdrMergeFailures.empty() &&
10979 PendingFunctionOdrMergeFailures.empty() &&
10980 PendingEnumOdrMergeFailures.empty() &&
10981 PendingObjCInterfaceOdrMergeFailures.empty() &&
10982 PendingObjCProtocolOdrMergeFailures.empty())
10983 return;
10984
10985 // Trigger the import of the full definition of each class that had any
10986 // odr-merging problems, so we can produce better diagnostics for them.
10987 // These updates may in turn find and diagnose some ODR failures, so take
10988 // ownership of the set first.
10989 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10990 PendingOdrMergeFailures.clear();
10991 for (auto &Merge : OdrMergeFailures) {
10992 Merge.first->buildLookup();
10993 Merge.first->decls_begin();
10994 Merge.first->bases_begin();
10995 Merge.first->vbases_begin();
10996 for (auto &RecordPair : Merge.second) {
10997 auto *RD = RecordPair.first;
10998 RD->decls_begin();
10999 RD->bases_begin();
11000 RD->vbases_begin();
11001 }
11002 }
11003
11004 // Trigger the import of the full definition of each record in C/ObjC.
11005 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
11006 PendingRecordOdrMergeFailures.clear();
11007 for (auto &Merge : RecordOdrMergeFailures) {
11008 Merge.first->decls_begin();
11009 for (auto &D : Merge.second)
11010 D->decls_begin();
11011 }
11012
11013 // Trigger the import of the full interface definition.
11014 auto ObjCInterfaceOdrMergeFailures =
11015 std::move(PendingObjCInterfaceOdrMergeFailures);
11016 PendingObjCInterfaceOdrMergeFailures.clear();
11017 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11018 Merge.first->decls_begin();
11019 for (auto &InterfacePair : Merge.second)
11020 InterfacePair.first->decls_begin();
11021 }
11022
11023 // Trigger the import of functions.
11024 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
11025 PendingFunctionOdrMergeFailures.clear();
11026 for (auto &Merge : FunctionOdrMergeFailures) {
11027 Merge.first->buildLookup();
11028 Merge.first->decls_begin();
11029 Merge.first->getBody();
11030 for (auto &FD : Merge.second) {
11031 FD->buildLookup();
11032 FD->decls_begin();
11033 FD->getBody();
11034 }
11035 }
11036
11037 // Trigger the import of enums.
11038 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
11039 PendingEnumOdrMergeFailures.clear();
11040 for (auto &Merge : EnumOdrMergeFailures) {
11041 Merge.first->decls_begin();
11042 for (auto &Enum : Merge.second) {
11043 Enum->decls_begin();
11044 }
11045 }
11046
11047 // Trigger the import of the full protocol definition.
11048 auto ObjCProtocolOdrMergeFailures =
11049 std::move(PendingObjCProtocolOdrMergeFailures);
11050 PendingObjCProtocolOdrMergeFailures.clear();
11051 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11052 Merge.first->decls_begin();
11053 for (auto &ProtocolPair : Merge.second)
11054 ProtocolPair.first->decls_begin();
11055 }
11056
11057 // For each declaration from a merged context, check that the canonical
11058 // definition of that context also contains a declaration of the same
11059 // entity.
11060 //
11061 // Caution: this loop does things that might invalidate iterators into
11062 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11063 while (!PendingOdrMergeChecks.empty()) {
11064 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11065
11066 // FIXME: Skip over implicit declarations for now. This matters for things
11067 // like implicitly-declared special member functions. This isn't entirely
11068 // correct; we can end up with multiple unmerged declarations of the same
11069 // implicit entity.
11070 if (D->isImplicit())
11071 continue;
11072
11073 DeclContext *CanonDef = D->getDeclContext();
11074
11075 bool Found = false;
11076 const Decl *DCanon = D->getCanonicalDecl();
11077
11078 for (auto *RI : D->redecls()) {
11079 if (RI->getLexicalDeclContext() == CanonDef) {
11080 Found = true;
11081 break;
11082 }
11083 }
11084 if (Found)
11085 continue;
11086
11087 // Quick check failed, time to do the slow thing. Note, we can't just
11088 // look up the name of D in CanonDef here, because the member that is
11089 // in CanonDef might not be found by name lookup (it might have been
11090 // replaced by a more recent declaration in the lookup table), and we
11091 // can't necessarily find it in the redeclaration chain because it might
11092 // be merely mergeable, not redeclarable.
11093 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11094 for (auto *CanonMember : CanonDef->decls()) {
11095 if (CanonMember->getCanonicalDecl() == DCanon) {
11096 // This can happen if the declaration is merely mergeable and not
11097 // actually redeclarable (we looked for redeclarations earlier).
11098 //
11099 // FIXME: We should be able to detect this more efficiently, without
11100 // pulling in all of the members of CanonDef.
11101 Found = true;
11102 break;
11103 }
11104 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11105 if (ND->getDeclName() == D->getDeclName())
11106 Candidates.push_back(Elt: ND);
11107 }
11108
11109 if (!Found) {
11110 // The AST doesn't like TagDecls becoming invalid after they've been
11111 // completed. We only really need to mark FieldDecls as invalid here.
11112 if (!isa<TagDecl>(Val: D))
11113 D->setInvalidDecl();
11114
11115 // Ensure we don't accidentally recursively enter deserialization while
11116 // we're producing our diagnostic.
11117 Deserializing RecursionGuard(this);
11118
11119 std::string CanonDefModule =
11120 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11121 D: cast<Decl>(Val: CanonDef));
11122 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11123 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11124 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11125
11126 if (Candidates.empty())
11127 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11128 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11129 else {
11130 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11131 Diag(Loc: Candidates[I]->getLocation(),
11132 DiagID: diag::note_module_odr_violation_possible_decl)
11133 << Candidates[I];
11134 }
11135
11136 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11137 }
11138 }
11139
11140 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11141 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11142 ObjCInterfaceOdrMergeFailures.empty() &&
11143 ObjCProtocolOdrMergeFailures.empty())
11144 return;
11145
11146 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11147 getPreprocessor().getLangOpts());
11148
11149 // Issue any pending ODR-failure diagnostics.
11150 for (auto &Merge : OdrMergeFailures) {
11151 // If we've already pointed out a specific problem with this class, don't
11152 // bother issuing a general "something's different" diagnostic.
11153 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11154 continue;
11155
11156 bool Diagnosed = false;
11157 CXXRecordDecl *FirstRecord = Merge.first;
11158 for (auto &RecordPair : Merge.second) {
11159 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11160 SecondDD: RecordPair.second)) {
11161 Diagnosed = true;
11162 break;
11163 }
11164 }
11165
11166 if (!Diagnosed) {
11167 // All definitions are updates to the same declaration. This happens if a
11168 // module instantiates the declaration of a class template specialization
11169 // and two or more other modules instantiate its definition.
11170 //
11171 // FIXME: Indicate which modules had instantiations of this definition.
11172 // FIXME: How can this even happen?
11173 Diag(Loc: Merge.first->getLocation(),
11174 DiagID: diag::err_module_odr_violation_different_instantiations)
11175 << Merge.first;
11176 }
11177 }
11178
11179 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11180 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11181 for (auto &Merge : RecordOdrMergeFailures) {
11182 // If we've already pointed out a specific problem with this class, don't
11183 // bother issuing a general "something's different" diagnostic.
11184 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11185 continue;
11186
11187 RecordDecl *FirstRecord = Merge.first;
11188 bool Diagnosed = false;
11189 for (auto *SecondRecord : Merge.second) {
11190 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11191 Diagnosed = true;
11192 break;
11193 }
11194 }
11195 (void)Diagnosed;
11196 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11197 }
11198
11199 // Issue ODR failures diagnostics for functions.
11200 for (auto &Merge : FunctionOdrMergeFailures) {
11201 FunctionDecl *FirstFunction = Merge.first;
11202 bool Diagnosed = false;
11203 for (auto &SecondFunction : Merge.second) {
11204 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11205 Diagnosed = true;
11206 break;
11207 }
11208 }
11209 (void)Diagnosed;
11210 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11211 }
11212
11213 // Issue ODR failures diagnostics for enums.
11214 for (auto &Merge : EnumOdrMergeFailures) {
11215 // If we've already pointed out a specific problem with this enum, don't
11216 // bother issuing a general "something's different" diagnostic.
11217 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11218 continue;
11219
11220 EnumDecl *FirstEnum = Merge.first;
11221 bool Diagnosed = false;
11222 for (auto &SecondEnum : Merge.second) {
11223 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11224 Diagnosed = true;
11225 break;
11226 }
11227 }
11228 (void)Diagnosed;
11229 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11230 }
11231
11232 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11233 // If we've already pointed out a specific problem with this interface,
11234 // don't bother issuing a general "something's different" diagnostic.
11235 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11236 continue;
11237
11238 bool Diagnosed = false;
11239 ObjCInterfaceDecl *FirstID = Merge.first;
11240 for (auto &InterfacePair : Merge.second) {
11241 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11242 SecondDD: InterfacePair.second)) {
11243 Diagnosed = true;
11244 break;
11245 }
11246 }
11247 (void)Diagnosed;
11248 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11249 }
11250
11251 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11252 // If we've already pointed out a specific problem with this protocol,
11253 // don't bother issuing a general "something's different" diagnostic.
11254 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11255 continue;
11256
11257 ObjCProtocolDecl *FirstProtocol = Merge.first;
11258 bool Diagnosed = false;
11259 for (auto &ProtocolPair : Merge.second) {
11260 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11261 SecondDD: ProtocolPair.second)) {
11262 Diagnosed = true;
11263 break;
11264 }
11265 }
11266 (void)Diagnosed;
11267 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11268 }
11269}
11270
11271void ASTReader::StartedDeserializing() {
11272 if (llvm::Timer *T = ReadTimer.get();
11273 ++NumCurrentElementsDeserializing == 1 && T)
11274 ReadTimeRegion.emplace(args&: T);
11275}
11276
11277void ASTReader::FinishedDeserializing() {
11278 assert(NumCurrentElementsDeserializing &&
11279 "FinishedDeserializing not paired with StartedDeserializing");
11280 if (NumCurrentElementsDeserializing == 1) {
11281 // We decrease NumCurrentElementsDeserializing only after pending actions
11282 // are finished, to avoid recursively re-calling finishPendingActions().
11283 finishPendingActions();
11284 }
11285 --NumCurrentElementsDeserializing;
11286
11287 if (NumCurrentElementsDeserializing == 0) {
11288 {
11289 // Guard variable to avoid recursively entering the process of passing
11290 // decls to consumer.
11291 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11292 /*NewValue=*/false);
11293
11294 // Propagate exception specification and deduced type updates along
11295 // redeclaration chains.
11296 //
11297 // We do this now rather than in finishPendingActions because we want to
11298 // be able to walk the complete redeclaration chains of the updated decls.
11299 while (!PendingExceptionSpecUpdates.empty() ||
11300 !PendingDeducedTypeUpdates.empty() ||
11301 !PendingUndeducedFunctionDecls.empty()) {
11302 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11303 PendingExceptionSpecUpdates.clear();
11304 for (auto Update : ESUpdates) {
11305 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11306 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11307 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11308 if (auto *Listener = getContext().getASTMutationListener())
11309 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11310 for (auto *Redecl : Update.second->redecls())
11311 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11312 }
11313
11314 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11315 PendingDeducedTypeUpdates.clear();
11316 for (auto Update : DTUpdates) {
11317 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11318 // FIXME: If the return type is already deduced, check that it
11319 // matches.
11320 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11321 ResultType: Update.second);
11322 }
11323
11324 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11325 PendingUndeducedFunctionDecls.clear();
11326 // We hope we can find the deduced type for the functions by iterating
11327 // redeclarations in other modules.
11328 for (FunctionDecl *UndeducedFD : UDTUpdates)
11329 (void)UndeducedFD->getMostRecentDecl();
11330 }
11331
11332 ReadTimeRegion.reset();
11333
11334 diagnoseOdrViolations();
11335 }
11336
11337 // We are not in recursive loading, so it's safe to pass the "interesting"
11338 // decls to the consumer.
11339 if (Consumer)
11340 PassInterestingDeclsToConsumer();
11341 }
11342}
11343
11344void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11345 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11346 // Remove any fake results before adding any real ones.
11347 auto It = PendingFakeLookupResults.find(Key: II);
11348 if (It != PendingFakeLookupResults.end()) {
11349 for (auto *ND : It->second)
11350 SemaObj->IdResolver.RemoveDecl(D: ND);
11351 // FIXME: this works around module+PCH performance issue.
11352 // Rather than erase the result from the map, which is O(n), just clear
11353 // the vector of NamedDecls.
11354 It->second.clear();
11355 }
11356 }
11357
11358 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11359 SemaObj->TUScope->AddDecl(D);
11360 } else if (SemaObj->TUScope) {
11361 // Adding the decl to IdResolver may have failed because it was already in
11362 // (even though it was not added in scope). If it is already in, make sure
11363 // it gets in the scope as well.
11364 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11365 SemaObj->TUScope->AddDecl(D);
11366 }
11367}
11368
11369ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11370 ASTContext *Context,
11371 const PCHContainerReader &PCHContainerRdr,
11372 const CodeGenOptions &CodeGenOpts,
11373 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11374 StringRef isysroot,
11375 DisableValidationForModuleKind DisableValidationKind,
11376 bool AllowASTWithCompilerErrors,
11377 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11378 bool ForceValidateUserInputs,
11379 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11380 std::unique_ptr<llvm::Timer> ReadTimer)
11381 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11382 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11383 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11384 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11385 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11386 StackHandler(Diags), PP(PP), ContextObj(Context),
11387 CodeGenOpts(CodeGenOpts),
11388 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11389 PP.getHeaderSearchInfo()),
11390 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11391 DisableValidationKind(DisableValidationKind),
11392 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11393 AllowConfigurationMismatch(AllowConfigurationMismatch),
11394 ValidateSystemInputs(ValidateSystemInputs),
11395 ForceValidateUserInputs(ForceValidateUserInputs),
11396 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11397 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11398 SourceMgr.setExternalSLocEntrySource(this);
11399
11400 PathBuf.reserve(N: 256);
11401
11402 for (const auto &Ext : Extensions) {
11403 auto BlockName = Ext->getExtensionMetadata().BlockName;
11404 auto Known = ModuleFileExtensions.find(Key: BlockName);
11405 if (Known != ModuleFileExtensions.end()) {
11406 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11407 << BlockName;
11408 continue;
11409 }
11410
11411 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11412 }
11413}
11414
11415ASTReader::~ASTReader() {
11416 if (OwnsDeserializationListener)
11417 delete DeserializationListener;
11418}
11419
11420IdentifierResolver &ASTReader::getIdResolver() {
11421 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11422}
11423
11424Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11425 unsigned AbbrevID) {
11426 Idx = 0;
11427 Record.clear();
11428 return Cursor.readRecord(AbbrevID, Vals&: Record);
11429}
11430//===----------------------------------------------------------------------===//
11431//// OMPClauseReader implementation
11432////===----------------------------------------------------------------------===//
11433
11434// This has to be in namespace clang because it's friended by all
11435// of the OMP clauses.
11436namespace clang {
11437
11438class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11439 ASTRecordReader &Record;
11440 ASTContext &Context;
11441
11442public:
11443 OMPClauseReader(ASTRecordReader &Record)
11444 : Record(Record), Context(Record.getContext()) {}
11445#define GEN_CLANG_CLAUSE_CLASS
11446#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11447#include "llvm/Frontend/OpenMP/OMP.inc"
11448 OMPClause *readClause();
11449 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11450 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11451};
11452
11453} // end namespace clang
11454
11455OMPClause *ASTRecordReader::readOMPClause() {
11456 return OMPClauseReader(*this).readClause();
11457}
11458
11459OMPClause *OMPClauseReader::readClause() {
11460 OMPClause *C = nullptr;
11461 switch (llvm::omp::Clause(Record.readInt())) {
11462 case llvm::omp::OMPC_if:
11463 C = new (Context) OMPIfClause();
11464 break;
11465 case llvm::omp::OMPC_final:
11466 C = new (Context) OMPFinalClause();
11467 break;
11468 case llvm::omp::OMPC_num_threads:
11469 C = new (Context) OMPNumThreadsClause();
11470 break;
11471 case llvm::omp::OMPC_safelen:
11472 C = new (Context) OMPSafelenClause();
11473 break;
11474 case llvm::omp::OMPC_simdlen:
11475 C = new (Context) OMPSimdlenClause();
11476 break;
11477 case llvm::omp::OMPC_sizes: {
11478 unsigned NumSizes = Record.readInt();
11479 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11480 break;
11481 }
11482 case llvm::omp::OMPC_counts: {
11483 unsigned NumCounts = Record.readInt();
11484 C = OMPCountsClause::CreateEmpty(C: Context, NumCounts);
11485 break;
11486 }
11487 case llvm::omp::OMPC_permutation: {
11488 unsigned NumLoops = Record.readInt();
11489 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11490 break;
11491 }
11492 case llvm::omp::OMPC_full:
11493 C = OMPFullClause::CreateEmpty(C: Context);
11494 break;
11495 case llvm::omp::OMPC_partial:
11496 C = OMPPartialClause::CreateEmpty(C: Context);
11497 break;
11498 case llvm::omp::OMPC_looprange:
11499 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11500 break;
11501 case llvm::omp::OMPC_allocator:
11502 C = new (Context) OMPAllocatorClause();
11503 break;
11504 case llvm::omp::OMPC_collapse:
11505 C = new (Context) OMPCollapseClause();
11506 break;
11507 case llvm::omp::OMPC_default:
11508 C = new (Context) OMPDefaultClause();
11509 break;
11510 case llvm::omp::OMPC_proc_bind:
11511 C = new (Context) OMPProcBindClause();
11512 break;
11513 case llvm::omp::OMPC_schedule:
11514 C = new (Context) OMPScheduleClause();
11515 break;
11516 case llvm::omp::OMPC_ordered:
11517 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11518 break;
11519 case llvm::omp::OMPC_nowait:
11520 C = new (Context) OMPNowaitClause();
11521 break;
11522 case llvm::omp::OMPC_untied:
11523 C = new (Context) OMPUntiedClause();
11524 break;
11525 case llvm::omp::OMPC_mergeable:
11526 C = new (Context) OMPMergeableClause();
11527 break;
11528 case llvm::omp::OMPC_threadset:
11529 C = new (Context) OMPThreadsetClause();
11530 break;
11531 case llvm::omp::OMPC_transparent:
11532 C = new (Context) OMPTransparentClause();
11533 break;
11534 case llvm::omp::OMPC_read:
11535 C = new (Context) OMPReadClause();
11536 break;
11537 case llvm::omp::OMPC_write:
11538 C = new (Context) OMPWriteClause();
11539 break;
11540 case llvm::omp::OMPC_update:
11541 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11542 break;
11543 case llvm::omp::OMPC_capture:
11544 C = new (Context) OMPCaptureClause();
11545 break;
11546 case llvm::omp::OMPC_compare:
11547 C = new (Context) OMPCompareClause();
11548 break;
11549 case llvm::omp::OMPC_fail:
11550 C = new (Context) OMPFailClause();
11551 break;
11552 case llvm::omp::OMPC_seq_cst:
11553 C = new (Context) OMPSeqCstClause();
11554 break;
11555 case llvm::omp::OMPC_acq_rel:
11556 C = new (Context) OMPAcqRelClause();
11557 break;
11558 case llvm::omp::OMPC_absent: {
11559 unsigned NumKinds = Record.readInt();
11560 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11561 break;
11562 }
11563 case llvm::omp::OMPC_holds:
11564 C = new (Context) OMPHoldsClause();
11565 break;
11566 case llvm::omp::OMPC_contains: {
11567 unsigned NumKinds = Record.readInt();
11568 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11569 break;
11570 }
11571 case llvm::omp::OMPC_no_openmp:
11572 C = new (Context) OMPNoOpenMPClause();
11573 break;
11574 case llvm::omp::OMPC_no_openmp_routines:
11575 C = new (Context) OMPNoOpenMPRoutinesClause();
11576 break;
11577 case llvm::omp::OMPC_no_openmp_constructs:
11578 C = new (Context) OMPNoOpenMPConstructsClause();
11579 break;
11580 case llvm::omp::OMPC_no_parallelism:
11581 C = new (Context) OMPNoParallelismClause();
11582 break;
11583 case llvm::omp::OMPC_acquire:
11584 C = new (Context) OMPAcquireClause();
11585 break;
11586 case llvm::omp::OMPC_release:
11587 C = new (Context) OMPReleaseClause();
11588 break;
11589 case llvm::omp::OMPC_relaxed:
11590 C = new (Context) OMPRelaxedClause();
11591 break;
11592 case llvm::omp::OMPC_weak:
11593 C = new (Context) OMPWeakClause();
11594 break;
11595 case llvm::omp::OMPC_threads:
11596 C = new (Context) OMPThreadsClause();
11597 break;
11598 case llvm::omp::OMPC_simd:
11599 C = new (Context) OMPSIMDClause();
11600 break;
11601 case llvm::omp::OMPC_nogroup:
11602 C = new (Context) OMPNogroupClause();
11603 break;
11604 case llvm::omp::OMPC_unified_address:
11605 C = new (Context) OMPUnifiedAddressClause();
11606 break;
11607 case llvm::omp::OMPC_unified_shared_memory:
11608 C = new (Context) OMPUnifiedSharedMemoryClause();
11609 break;
11610 case llvm::omp::OMPC_reverse_offload:
11611 C = new (Context) OMPReverseOffloadClause();
11612 break;
11613 case llvm::omp::OMPC_dynamic_allocators:
11614 C = new (Context) OMPDynamicAllocatorsClause();
11615 break;
11616 case llvm::omp::OMPC_atomic_default_mem_order:
11617 C = new (Context) OMPAtomicDefaultMemOrderClause();
11618 break;
11619 case llvm::omp::OMPC_self_maps:
11620 C = new (Context) OMPSelfMapsClause();
11621 break;
11622 case llvm::omp::OMPC_at:
11623 C = new (Context) OMPAtClause();
11624 break;
11625 case llvm::omp::OMPC_severity:
11626 C = new (Context) OMPSeverityClause();
11627 break;
11628 case llvm::omp::OMPC_message:
11629 C = new (Context) OMPMessageClause();
11630 break;
11631 case llvm::omp::OMPC_private:
11632 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11633 break;
11634 case llvm::omp::OMPC_firstprivate:
11635 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11636 break;
11637 case llvm::omp::OMPC_lastprivate:
11638 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11639 break;
11640 case llvm::omp::OMPC_shared:
11641 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11642 break;
11643 case llvm::omp::OMPC_reduction: {
11644 unsigned N = Record.readInt();
11645 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11646 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11647 break;
11648 }
11649 case llvm::omp::OMPC_task_reduction:
11650 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11651 break;
11652 case llvm::omp::OMPC_in_reduction:
11653 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11654 break;
11655 case llvm::omp::OMPC_linear:
11656 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11657 break;
11658 case llvm::omp::OMPC_aligned:
11659 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11660 break;
11661 case llvm::omp::OMPC_copyin:
11662 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11663 break;
11664 case llvm::omp::OMPC_copyprivate:
11665 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11666 break;
11667 case llvm::omp::OMPC_flush:
11668 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11669 break;
11670 case llvm::omp::OMPC_depobj:
11671 C = OMPDepobjClause::CreateEmpty(C: Context);
11672 break;
11673 case llvm::omp::OMPC_depend: {
11674 unsigned NumVars = Record.readInt();
11675 unsigned NumLoops = Record.readInt();
11676 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11677 break;
11678 }
11679 case llvm::omp::OMPC_device:
11680 C = new (Context) OMPDeviceClause();
11681 break;
11682 case llvm::omp::OMPC_map: {
11683 OMPMappableExprListSizeTy Sizes;
11684 Sizes.NumVars = Record.readInt();
11685 Sizes.NumUniqueDeclarations = Record.readInt();
11686 Sizes.NumComponentLists = Record.readInt();
11687 Sizes.NumComponents = Record.readInt();
11688 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11689 break;
11690 }
11691 case llvm::omp::OMPC_num_teams:
11692 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11693 break;
11694 case llvm::omp::OMPC_thread_limit:
11695 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11696 break;
11697 case llvm::omp::OMPC_priority:
11698 C = new (Context) OMPPriorityClause();
11699 break;
11700 case llvm::omp::OMPC_grainsize:
11701 C = new (Context) OMPGrainsizeClause();
11702 break;
11703 case llvm::omp::OMPC_num_tasks:
11704 C = new (Context) OMPNumTasksClause();
11705 break;
11706 case llvm::omp::OMPC_hint:
11707 C = new (Context) OMPHintClause();
11708 break;
11709 case llvm::omp::OMPC_dist_schedule:
11710 C = new (Context) OMPDistScheduleClause();
11711 break;
11712 case llvm::omp::OMPC_defaultmap:
11713 C = new (Context) OMPDefaultmapClause();
11714 break;
11715 case llvm::omp::OMPC_to: {
11716 OMPMappableExprListSizeTy Sizes;
11717 Sizes.NumVars = Record.readInt();
11718 Sizes.NumUniqueDeclarations = Record.readInt();
11719 Sizes.NumComponentLists = Record.readInt();
11720 Sizes.NumComponents = Record.readInt();
11721 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11722 break;
11723 }
11724 case llvm::omp::OMPC_from: {
11725 OMPMappableExprListSizeTy Sizes;
11726 Sizes.NumVars = Record.readInt();
11727 Sizes.NumUniqueDeclarations = Record.readInt();
11728 Sizes.NumComponentLists = Record.readInt();
11729 Sizes.NumComponents = Record.readInt();
11730 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11731 break;
11732 }
11733 case llvm::omp::OMPC_use_device_ptr: {
11734 OMPMappableExprListSizeTy Sizes;
11735 Sizes.NumVars = Record.readInt();
11736 Sizes.NumUniqueDeclarations = Record.readInt();
11737 Sizes.NumComponentLists = Record.readInt();
11738 Sizes.NumComponents = Record.readInt();
11739 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11740 break;
11741 }
11742 case llvm::omp::OMPC_use_device_addr: {
11743 OMPMappableExprListSizeTy Sizes;
11744 Sizes.NumVars = Record.readInt();
11745 Sizes.NumUniqueDeclarations = Record.readInt();
11746 Sizes.NumComponentLists = Record.readInt();
11747 Sizes.NumComponents = Record.readInt();
11748 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11749 break;
11750 }
11751 case llvm::omp::OMPC_is_device_ptr: {
11752 OMPMappableExprListSizeTy Sizes;
11753 Sizes.NumVars = Record.readInt();
11754 Sizes.NumUniqueDeclarations = Record.readInt();
11755 Sizes.NumComponentLists = Record.readInt();
11756 Sizes.NumComponents = Record.readInt();
11757 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11758 break;
11759 }
11760 case llvm::omp::OMPC_has_device_addr: {
11761 OMPMappableExprListSizeTy Sizes;
11762 Sizes.NumVars = Record.readInt();
11763 Sizes.NumUniqueDeclarations = Record.readInt();
11764 Sizes.NumComponentLists = Record.readInt();
11765 Sizes.NumComponents = Record.readInt();
11766 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11767 break;
11768 }
11769 case llvm::omp::OMPC_allocate:
11770 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11771 break;
11772 case llvm::omp::OMPC_nontemporal:
11773 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11774 break;
11775 case llvm::omp::OMPC_inclusive:
11776 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11777 break;
11778 case llvm::omp::OMPC_exclusive:
11779 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11780 break;
11781 case llvm::omp::OMPC_order:
11782 C = new (Context) OMPOrderClause();
11783 break;
11784 case llvm::omp::OMPC_init: {
11785 unsigned VarListSize = Record.readInt();
11786 unsigned NumAttrs = Record.readInt();
11787 C = OMPInitClause::CreateEmpty(C: Context, /*NumPrefs=*/VarListSize - 1,
11788 NumAttrs);
11789 break;
11790 }
11791 case llvm::omp::OMPC_use:
11792 C = new (Context) OMPUseClause();
11793 break;
11794 case llvm::omp::OMPC_destroy:
11795 C = new (Context) OMPDestroyClause();
11796 break;
11797 case llvm::omp::OMPC_novariants:
11798 C = new (Context) OMPNovariantsClause();
11799 break;
11800 case llvm::omp::OMPC_nocontext:
11801 C = new (Context) OMPNocontextClause();
11802 break;
11803 case llvm::omp::OMPC_detach:
11804 C = new (Context) OMPDetachClause();
11805 break;
11806 case llvm::omp::OMPC_uses_allocators:
11807 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11808 break;
11809 case llvm::omp::OMPC_affinity:
11810 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11811 break;
11812 case llvm::omp::OMPC_filter:
11813 C = new (Context) OMPFilterClause();
11814 break;
11815 case llvm::omp::OMPC_bind:
11816 C = OMPBindClause::CreateEmpty(C: Context);
11817 break;
11818 case llvm::omp::OMPC_align:
11819 C = new (Context) OMPAlignClause();
11820 break;
11821 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11822 C = new (Context) OMPXDynCGroupMemClause();
11823 break;
11824 case llvm::omp::OMPC_dyn_groupprivate:
11825 C = new (Context) OMPDynGroupprivateClause();
11826 break;
11827 case llvm::omp::OMPC_doacross: {
11828 unsigned NumVars = Record.readInt();
11829 unsigned NumLoops = Record.readInt();
11830 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11831 break;
11832 }
11833 case llvm::omp::OMPC_ompx_attribute:
11834 C = new (Context) OMPXAttributeClause();
11835 break;
11836 case llvm::omp::OMPC_ompx_bare:
11837 C = new (Context) OMPXBareClause();
11838 break;
11839#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11840 case llvm::omp::Enum: \
11841 break;
11842#include "llvm/Frontend/OpenMP/OMPKinds.def"
11843 default:
11844 break;
11845 }
11846 assert(C && "Unknown OMPClause type");
11847
11848 Visit(S: C);
11849 C->setLocStart(Record.readSourceLocation());
11850 C->setLocEnd(Record.readSourceLocation());
11851
11852 return C;
11853}
11854
11855void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11856 C->setPreInitStmt(S: Record.readSubStmt(),
11857 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11858}
11859
11860void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11861 VisitOMPClauseWithPreInit(C);
11862 C->setPostUpdateExpr(Record.readSubExpr());
11863}
11864
11865void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11866 VisitOMPClauseWithPreInit(C);
11867 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11868 C->setNameModifierLoc(Record.readSourceLocation());
11869 C->setColonLoc(Record.readSourceLocation());
11870 C->setCondition(Record.readSubExpr());
11871 C->setLParenLoc(Record.readSourceLocation());
11872}
11873
11874void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11875 VisitOMPClauseWithPreInit(C);
11876 C->setCondition(Record.readSubExpr());
11877 C->setLParenLoc(Record.readSourceLocation());
11878}
11879
11880void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11881 VisitOMPClauseWithPreInit(C);
11882 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11883 C->setNumThreads(Record.readSubExpr());
11884 C->setModifierLoc(Record.readSourceLocation());
11885 C->setLParenLoc(Record.readSourceLocation());
11886}
11887
11888void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11889 C->setSafelen(Record.readSubExpr());
11890 C->setLParenLoc(Record.readSourceLocation());
11891}
11892
11893void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11894 C->setSimdlen(Record.readSubExpr());
11895 C->setLParenLoc(Record.readSourceLocation());
11896}
11897
11898void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11899 for (Expr *&E : C->getSizesRefs())
11900 E = Record.readSubExpr();
11901 C->setLParenLoc(Record.readSourceLocation());
11902}
11903
11904void OMPClauseReader::VisitOMPCountsClause(OMPCountsClause *C) {
11905 bool HasFill = Record.readBool();
11906 if (HasFill)
11907 C->setOmpFillIndex(Record.readInt());
11908 C->setOmpFillLoc(Record.readSourceLocation());
11909 for (Expr *&E : C->getCountsRefs())
11910 E = Record.readSubExpr();
11911 C->setLParenLoc(Record.readSourceLocation());
11912}
11913
11914void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11915 for (Expr *&E : C->getArgsRefs())
11916 E = Record.readSubExpr();
11917 C->setLParenLoc(Record.readSourceLocation());
11918}
11919
11920void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11921
11922void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11923 C->setFactor(Record.readSubExpr());
11924 C->setLParenLoc(Record.readSourceLocation());
11925}
11926
11927void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11928 C->setFirst(Record.readSubExpr());
11929 C->setCount(Record.readSubExpr());
11930 C->setLParenLoc(Record.readSourceLocation());
11931 C->setFirstLoc(Record.readSourceLocation());
11932 C->setCountLoc(Record.readSourceLocation());
11933}
11934
11935void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11936 C->setAllocator(Record.readExpr());
11937 C->setLParenLoc(Record.readSourceLocation());
11938}
11939
11940void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11941 C->setNumForLoops(Record.readSubExpr());
11942 C->setLParenLoc(Record.readSourceLocation());
11943}
11944
11945void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11946 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11947 C->setLParenLoc(Record.readSourceLocation());
11948 C->setDefaultKindKwLoc(Record.readSourceLocation());
11949 C->setDefaultVariableCategory(
11950 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11951 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11952}
11953
11954// Read the parameter of threadset clause. This will have been saved when
11955// OMPClauseWriter is called.
11956void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11957 C->setLParenLoc(Record.readSourceLocation());
11958 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11959 C->setThreadsetKindLoc(ThreadsetKindLoc);
11960 OpenMPThreadsetKind TKind =
11961 static_cast<OpenMPThreadsetKind>(Record.readInt());
11962 C->setThreadsetKind(TKind);
11963}
11964
11965void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11966 C->setLParenLoc(Record.readSourceLocation());
11967 C->setImpexTypeKind(Record.readSubExpr());
11968}
11969
11970void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11971 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11972 C->setLParenLoc(Record.readSourceLocation());
11973 C->setProcBindKindKwLoc(Record.readSourceLocation());
11974}
11975
11976void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11977 VisitOMPClauseWithPreInit(C);
11978 C->setScheduleKind(
11979 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11980 C->setFirstScheduleModifier(
11981 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11982 C->setSecondScheduleModifier(
11983 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11984 C->setChunkSize(Record.readSubExpr());
11985 C->setLParenLoc(Record.readSourceLocation());
11986 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11987 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11988 C->setScheduleKindLoc(Record.readSourceLocation());
11989 C->setCommaLoc(Record.readSourceLocation());
11990}
11991
11992void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11993 C->setNumForLoops(Record.readSubExpr());
11994 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11995 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11996 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11997 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11998 C->setLParenLoc(Record.readSourceLocation());
11999}
12000
12001void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12002 C->setEventHandler(Record.readSubExpr());
12003 C->setLParenLoc(Record.readSourceLocation());
12004}
12005
12006void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
12007 C->setCondition(Record.readSubExpr());
12008 C->setLParenLoc(Record.readSourceLocation());
12009}
12010
12011void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12012
12013void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12014
12015void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12016
12017void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12018
12019void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12020 if (C->isExtended()) {
12021 C->setLParenLoc(Record.readSourceLocation());
12022 C->setArgumentLoc(Record.readSourceLocation());
12023 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12024 }
12025}
12026
12027void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12028
12029void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12030
12031// Read the parameter of fail clause. This will have been saved when
12032// OMPClauseWriter is called.
12033void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
12034 C->setLParenLoc(Record.readSourceLocation());
12035 SourceLocation FailParameterLoc = Record.readSourceLocation();
12036 C->setFailParameterLoc(FailParameterLoc);
12037 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
12038 C->setFailParameter(CKind);
12039}
12040
12041void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
12042 unsigned Count = C->getDirectiveKinds().size();
12043 C->setLParenLoc(Record.readSourceLocation());
12044 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12045 DKVec.reserve(N: Count);
12046 for (unsigned I = 0; I < Count; I++) {
12047 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12048 }
12049 C->setDirectiveKinds(DKVec);
12050}
12051
12052void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
12053 C->setExpr(Record.readExpr());
12054 C->setLParenLoc(Record.readSourceLocation());
12055}
12056
12057void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
12058 unsigned Count = C->getDirectiveKinds().size();
12059 C->setLParenLoc(Record.readSourceLocation());
12060 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12061 DKVec.reserve(N: Count);
12062 for (unsigned I = 0; I < Count; I++) {
12063 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12064 }
12065 C->setDirectiveKinds(DKVec);
12066}
12067
12068void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12069
12070void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12071 OMPNoOpenMPRoutinesClause *) {}
12072
12073void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12074 OMPNoOpenMPConstructsClause *) {}
12075
12076void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12077
12078void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12079
12080void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12081
12082void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12083
12084void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12085
12086void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12087
12088void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12089
12090void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12091
12092void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12093
12094void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12095
12096void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12097 unsigned NumVars = C->varlist_size();
12098 SmallVector<Expr *, 16> Vars;
12099 Vars.reserve(N: NumVars);
12100 for (unsigned I = 0; I != NumVars; ++I)
12101 Vars.push_back(Elt: Record.readSubExpr());
12102 C->setVarRefs(Vars);
12103 C->setIsTarget(Record.readBool());
12104 C->setIsTargetSync(Record.readBool());
12105 C->setHasPreferAttrs(Record.readBool());
12106
12107 unsigned NumPrefs = C->varlist_size() - 1;
12108 SmallVector<unsigned, 4> Counts;
12109 SmallVector<Expr *, 8> Attrs;
12110 Counts.reserve(N: NumPrefs);
12111 for (unsigned I = 0; I < NumPrefs; ++I) {
12112 unsigned NA = Record.readInt();
12113 Counts.push_back(Elt: NA);
12114 for (unsigned J = 0; J < NA; ++J)
12115 Attrs.push_back(Elt: Record.readSubExpr());
12116 }
12117 C->setAttrs(Counts, Attrs);
12118
12119 C->setLParenLoc(Record.readSourceLocation());
12120 C->setVarLoc(Record.readSourceLocation());
12121}
12122
12123void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12124 C->setInteropVar(Record.readSubExpr());
12125 C->setLParenLoc(Record.readSourceLocation());
12126 C->setVarLoc(Record.readSourceLocation());
12127}
12128
12129void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12130 C->setInteropVar(Record.readSubExpr());
12131 C->setLParenLoc(Record.readSourceLocation());
12132 C->setVarLoc(Record.readSourceLocation());
12133}
12134
12135void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12136 VisitOMPClauseWithPreInit(C);
12137 C->setCondition(Record.readSubExpr());
12138 C->setLParenLoc(Record.readSourceLocation());
12139}
12140
12141void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12142 VisitOMPClauseWithPreInit(C);
12143 C->setCondition(Record.readSubExpr());
12144 C->setLParenLoc(Record.readSourceLocation());
12145}
12146
12147void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12148
12149void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12150 OMPUnifiedSharedMemoryClause *) {}
12151
12152void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12153
12154void
12155OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12156}
12157
12158void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12159 OMPAtomicDefaultMemOrderClause *C) {
12160 C->setAtomicDefaultMemOrderKind(
12161 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12162 C->setLParenLoc(Record.readSourceLocation());
12163 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12164}
12165
12166void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12167
12168void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12169 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12170 C->setLParenLoc(Record.readSourceLocation());
12171 C->setAtKindKwLoc(Record.readSourceLocation());
12172}
12173
12174void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12175 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12176 C->setLParenLoc(Record.readSourceLocation());
12177 C->setSeverityKindKwLoc(Record.readSourceLocation());
12178}
12179
12180void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12181 VisitOMPClauseWithPreInit(C);
12182 C->setMessageString(Record.readSubExpr());
12183 C->setLParenLoc(Record.readSourceLocation());
12184}
12185
12186void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12187 C->setLParenLoc(Record.readSourceLocation());
12188 unsigned NumVars = C->varlist_size();
12189 SmallVector<Expr *, 16> Vars;
12190 Vars.reserve(N: NumVars);
12191 for (unsigned i = 0; i != NumVars; ++i)
12192 Vars.push_back(Elt: Record.readSubExpr());
12193 C->setVarRefs(Vars);
12194 Vars.clear();
12195 for (unsigned i = 0; i != NumVars; ++i)
12196 Vars.push_back(Elt: Record.readSubExpr());
12197 C->setPrivateCopies(Vars);
12198}
12199
12200void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12201 VisitOMPClauseWithPreInit(C);
12202 C->setLParenLoc(Record.readSourceLocation());
12203 unsigned NumVars = C->varlist_size();
12204 SmallVector<Expr *, 16> Vars;
12205 Vars.reserve(N: NumVars);
12206 for (unsigned i = 0; i != NumVars; ++i)
12207 Vars.push_back(Elt: Record.readSubExpr());
12208 C->setVarRefs(Vars);
12209 Vars.clear();
12210 for (unsigned i = 0; i != NumVars; ++i)
12211 Vars.push_back(Elt: Record.readSubExpr());
12212 C->setPrivateCopies(Vars);
12213 Vars.clear();
12214 for (unsigned i = 0; i != NumVars; ++i)
12215 Vars.push_back(Elt: Record.readSubExpr());
12216 C->setInits(Vars);
12217}
12218
12219void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12220 VisitOMPClauseWithPostUpdate(C);
12221 C->setLParenLoc(Record.readSourceLocation());
12222 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12223 C->setKindLoc(Record.readSourceLocation());
12224 C->setColonLoc(Record.readSourceLocation());
12225 unsigned NumVars = C->varlist_size();
12226 SmallVector<Expr *, 16> Vars;
12227 Vars.reserve(N: NumVars);
12228 for (unsigned i = 0; i != NumVars; ++i)
12229 Vars.push_back(Elt: Record.readSubExpr());
12230 C->setVarRefs(Vars);
12231 Vars.clear();
12232 for (unsigned i = 0; i != NumVars; ++i)
12233 Vars.push_back(Elt: Record.readSubExpr());
12234 C->setPrivateCopies(Vars);
12235 Vars.clear();
12236 for (unsigned i = 0; i != NumVars; ++i)
12237 Vars.push_back(Elt: Record.readSubExpr());
12238 C->setSourceExprs(Vars);
12239 Vars.clear();
12240 for (unsigned i = 0; i != NumVars; ++i)
12241 Vars.push_back(Elt: Record.readSubExpr());
12242 C->setDestinationExprs(Vars);
12243 Vars.clear();
12244 for (unsigned i = 0; i != NumVars; ++i)
12245 Vars.push_back(Elt: Record.readSubExpr());
12246 C->setAssignmentOps(Vars);
12247}
12248
12249void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12250 C->setLParenLoc(Record.readSourceLocation());
12251 unsigned NumVars = C->varlist_size();
12252 SmallVector<Expr *, 16> Vars;
12253 Vars.reserve(N: NumVars);
12254 for (unsigned i = 0; i != NumVars; ++i)
12255 Vars.push_back(Elt: Record.readSubExpr());
12256 C->setVarRefs(Vars);
12257}
12258
12259void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12260 VisitOMPClauseWithPostUpdate(C);
12261 C->setLParenLoc(Record.readSourceLocation());
12262 C->setModifierLoc(Record.readSourceLocation());
12263 C->setColonLoc(Record.readSourceLocation());
12264 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12265 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12266 C->setQualifierLoc(NNSL);
12267 C->setNameInfo(DNI);
12268
12269 unsigned NumVars = C->varlist_size();
12270 SmallVector<Expr *, 16> Vars;
12271 Vars.reserve(N: NumVars);
12272 for (unsigned i = 0; i != NumVars; ++i)
12273 Vars.push_back(Elt: Record.readSubExpr());
12274 C->setVarRefs(Vars);
12275 Vars.clear();
12276 for (unsigned i = 0; i != NumVars; ++i)
12277 Vars.push_back(Elt: Record.readSubExpr());
12278 C->setPrivates(Vars);
12279 Vars.clear();
12280 for (unsigned i = 0; i != NumVars; ++i)
12281 Vars.push_back(Elt: Record.readSubExpr());
12282 C->setLHSExprs(Vars);
12283 Vars.clear();
12284 for (unsigned i = 0; i != NumVars; ++i)
12285 Vars.push_back(Elt: Record.readSubExpr());
12286 C->setRHSExprs(Vars);
12287 Vars.clear();
12288 for (unsigned i = 0; i != NumVars; ++i)
12289 Vars.push_back(Elt: Record.readSubExpr());
12290 C->setReductionOps(Vars);
12291 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12292 Vars.clear();
12293 for (unsigned i = 0; i != NumVars; ++i)
12294 Vars.push_back(Elt: Record.readSubExpr());
12295 C->setInscanCopyOps(Vars);
12296 Vars.clear();
12297 for (unsigned i = 0; i != NumVars; ++i)
12298 Vars.push_back(Elt: Record.readSubExpr());
12299 C->setInscanCopyArrayTemps(Vars);
12300 Vars.clear();
12301 for (unsigned i = 0; i != NumVars; ++i)
12302 Vars.push_back(Elt: Record.readSubExpr());
12303 C->setInscanCopyArrayElems(Vars);
12304 }
12305 unsigned NumFlags = Record.readInt();
12306 SmallVector<bool, 16> Flags;
12307 Flags.reserve(N: NumFlags);
12308 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12309 Flags.push_back(Elt: Record.readInt());
12310 C->setPrivateVariableReductionFlags(Flags);
12311}
12312
12313void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12314 VisitOMPClauseWithPostUpdate(C);
12315 C->setLParenLoc(Record.readSourceLocation());
12316 C->setColonLoc(Record.readSourceLocation());
12317 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12318 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12319 C->setQualifierLoc(NNSL);
12320 C->setNameInfo(DNI);
12321
12322 unsigned NumVars = C->varlist_size();
12323 SmallVector<Expr *, 16> Vars;
12324 Vars.reserve(N: NumVars);
12325 for (unsigned I = 0; I != NumVars; ++I)
12326 Vars.push_back(Elt: Record.readSubExpr());
12327 C->setVarRefs(Vars);
12328 Vars.clear();
12329 for (unsigned I = 0; I != NumVars; ++I)
12330 Vars.push_back(Elt: Record.readSubExpr());
12331 C->setPrivates(Vars);
12332 Vars.clear();
12333 for (unsigned I = 0; I != NumVars; ++I)
12334 Vars.push_back(Elt: Record.readSubExpr());
12335 C->setLHSExprs(Vars);
12336 Vars.clear();
12337 for (unsigned I = 0; I != NumVars; ++I)
12338 Vars.push_back(Elt: Record.readSubExpr());
12339 C->setRHSExprs(Vars);
12340 Vars.clear();
12341 for (unsigned I = 0; I != NumVars; ++I)
12342 Vars.push_back(Elt: Record.readSubExpr());
12343 C->setReductionOps(Vars);
12344}
12345
12346void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12347 VisitOMPClauseWithPostUpdate(C);
12348 C->setLParenLoc(Record.readSourceLocation());
12349 C->setColonLoc(Record.readSourceLocation());
12350 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12351 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12352 C->setQualifierLoc(NNSL);
12353 C->setNameInfo(DNI);
12354
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 Vars.clear();
12362 for (unsigned I = 0; I != NumVars; ++I)
12363 Vars.push_back(Elt: Record.readSubExpr());
12364 C->setPrivates(Vars);
12365 Vars.clear();
12366 for (unsigned I = 0; I != NumVars; ++I)
12367 Vars.push_back(Elt: Record.readSubExpr());
12368 C->setLHSExprs(Vars);
12369 Vars.clear();
12370 for (unsigned I = 0; I != NumVars; ++I)
12371 Vars.push_back(Elt: Record.readSubExpr());
12372 C->setRHSExprs(Vars);
12373 Vars.clear();
12374 for (unsigned I = 0; I != NumVars; ++I)
12375 Vars.push_back(Elt: Record.readSubExpr());
12376 C->setReductionOps(Vars);
12377 Vars.clear();
12378 for (unsigned I = 0; I != NumVars; ++I)
12379 Vars.push_back(Elt: Record.readSubExpr());
12380 C->setTaskgroupDescriptors(Vars);
12381}
12382
12383void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12384 VisitOMPClauseWithPostUpdate(C);
12385 C->setLParenLoc(Record.readSourceLocation());
12386 C->setColonLoc(Record.readSourceLocation());
12387 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12388 C->setModifierLoc(Record.readSourceLocation());
12389 unsigned NumVars = C->varlist_size();
12390 SmallVector<Expr *, 16> Vars;
12391 Vars.reserve(N: NumVars);
12392 for (unsigned i = 0; i != NumVars; ++i)
12393 Vars.push_back(Elt: Record.readSubExpr());
12394 C->setVarRefs(Vars);
12395 Vars.clear();
12396 for (unsigned i = 0; i != NumVars; ++i)
12397 Vars.push_back(Elt: Record.readSubExpr());
12398 C->setPrivates(Vars);
12399 Vars.clear();
12400 for (unsigned i = 0; i != NumVars; ++i)
12401 Vars.push_back(Elt: Record.readSubExpr());
12402 C->setInits(Vars);
12403 Vars.clear();
12404 for (unsigned i = 0; i != NumVars; ++i)
12405 Vars.push_back(Elt: Record.readSubExpr());
12406 C->setUpdates(Vars);
12407 Vars.clear();
12408 for (unsigned i = 0; i != NumVars; ++i)
12409 Vars.push_back(Elt: Record.readSubExpr());
12410 C->setFinals(Vars);
12411 C->setStep(Record.readSubExpr());
12412 C->setCalcStep(Record.readSubExpr());
12413 Vars.clear();
12414 for (unsigned I = 0; I != NumVars + 1; ++I)
12415 Vars.push_back(Elt: Record.readSubExpr());
12416 C->setUsedExprs(Vars);
12417}
12418
12419void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12420 C->setLParenLoc(Record.readSourceLocation());
12421 C->setColonLoc(Record.readSourceLocation());
12422 unsigned NumVars = C->varlist_size();
12423 SmallVector<Expr *, 16> Vars;
12424 Vars.reserve(N: NumVars);
12425 for (unsigned i = 0; i != NumVars; ++i)
12426 Vars.push_back(Elt: Record.readSubExpr());
12427 C->setVarRefs(Vars);
12428 C->setAlignment(Record.readSubExpr());
12429}
12430
12431void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12432 C->setLParenLoc(Record.readSourceLocation());
12433 unsigned NumVars = C->varlist_size();
12434 SmallVector<Expr *, 16> Exprs;
12435 Exprs.reserve(N: NumVars);
12436 for (unsigned i = 0; i != NumVars; ++i)
12437 Exprs.push_back(Elt: Record.readSubExpr());
12438 C->setVarRefs(Exprs);
12439 Exprs.clear();
12440 for (unsigned i = 0; i != NumVars; ++i)
12441 Exprs.push_back(Elt: Record.readSubExpr());
12442 C->setSourceExprs(Exprs);
12443 Exprs.clear();
12444 for (unsigned i = 0; i != NumVars; ++i)
12445 Exprs.push_back(Elt: Record.readSubExpr());
12446 C->setDestinationExprs(Exprs);
12447 Exprs.clear();
12448 for (unsigned i = 0; i != NumVars; ++i)
12449 Exprs.push_back(Elt: Record.readSubExpr());
12450 C->setAssignmentOps(Exprs);
12451}
12452
12453void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12454 C->setLParenLoc(Record.readSourceLocation());
12455 unsigned NumVars = C->varlist_size();
12456 SmallVector<Expr *, 16> Exprs;
12457 Exprs.reserve(N: NumVars);
12458 for (unsigned i = 0; i != NumVars; ++i)
12459 Exprs.push_back(Elt: Record.readSubExpr());
12460 C->setVarRefs(Exprs);
12461 Exprs.clear();
12462 for (unsigned i = 0; i != NumVars; ++i)
12463 Exprs.push_back(Elt: Record.readSubExpr());
12464 C->setSourceExprs(Exprs);
12465 Exprs.clear();
12466 for (unsigned i = 0; i != NumVars; ++i)
12467 Exprs.push_back(Elt: Record.readSubExpr());
12468 C->setDestinationExprs(Exprs);
12469 Exprs.clear();
12470 for (unsigned i = 0; i != NumVars; ++i)
12471 Exprs.push_back(Elt: Record.readSubExpr());
12472 C->setAssignmentOps(Exprs);
12473}
12474
12475void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *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::VisitOMPDepobjClause(OMPDepobjClause *C) {
12486 C->setDepobj(Record.readSubExpr());
12487 C->setLParenLoc(Record.readSourceLocation());
12488}
12489
12490void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12491 C->setLParenLoc(Record.readSourceLocation());
12492 C->setModifier(Record.readSubExpr());
12493 C->setDependencyKind(
12494 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12495 C->setDependencyLoc(Record.readSourceLocation());
12496 C->setColonLoc(Record.readSourceLocation());
12497 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12498 unsigned NumVars = C->varlist_size();
12499 SmallVector<Expr *, 16> Vars;
12500 Vars.reserve(N: NumVars);
12501 for (unsigned I = 0; I != NumVars; ++I)
12502 Vars.push_back(Elt: Record.readSubExpr());
12503 C->setVarRefs(Vars);
12504 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12505 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12506}
12507
12508void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12509 VisitOMPClauseWithPreInit(C);
12510 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12511 C->setDevice(Record.readSubExpr());
12512 C->setModifierLoc(Record.readSourceLocation());
12513 C->setLParenLoc(Record.readSourceLocation());
12514}
12515
12516void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12517 C->setLParenLoc(Record.readSourceLocation());
12518 bool HasIteratorModifier = false;
12519 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12520 C->setMapTypeModifier(
12521 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12522 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12523 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12524 HasIteratorModifier = true;
12525 }
12526 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12527 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12528 C->setMapType(
12529 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12530 C->setMapLoc(Record.readSourceLocation());
12531 C->setColonLoc(Record.readSourceLocation());
12532 auto NumVars = C->varlist_size();
12533 auto UniqueDecls = C->getUniqueDeclarationsNum();
12534 auto TotalLists = C->getTotalComponentListNum();
12535 auto TotalComponents = C->getTotalComponentsNum();
12536
12537 SmallVector<Expr *, 16> Vars;
12538 Vars.reserve(N: NumVars);
12539 for (unsigned i = 0; i != NumVars; ++i)
12540 Vars.push_back(Elt: Record.readExpr());
12541 C->setVarRefs(Vars);
12542
12543 SmallVector<Expr *, 16> UDMappers;
12544 UDMappers.reserve(N: NumVars);
12545 for (unsigned I = 0; I < NumVars; ++I)
12546 UDMappers.push_back(Elt: Record.readExpr());
12547 C->setUDMapperRefs(UDMappers);
12548
12549 if (HasIteratorModifier)
12550 C->setIteratorModifier(Record.readExpr());
12551
12552 SmallVector<ValueDecl *, 16> Decls;
12553 Decls.reserve(N: UniqueDecls);
12554 for (unsigned i = 0; i < UniqueDecls; ++i)
12555 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12556 C->setUniqueDecls(Decls);
12557
12558 SmallVector<unsigned, 16> ListsPerDecl;
12559 ListsPerDecl.reserve(N: UniqueDecls);
12560 for (unsigned i = 0; i < UniqueDecls; ++i)
12561 ListsPerDecl.push_back(Elt: Record.readInt());
12562 C->setDeclNumLists(ListsPerDecl);
12563
12564 SmallVector<unsigned, 32> ListSizes;
12565 ListSizes.reserve(N: TotalLists);
12566 for (unsigned i = 0; i < TotalLists; ++i)
12567 ListSizes.push_back(Elt: Record.readInt());
12568 C->setComponentListSizes(ListSizes);
12569
12570 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12571 Components.reserve(N: TotalComponents);
12572 for (unsigned i = 0; i < TotalComponents; ++i) {
12573 Expr *AssociatedExprPr = Record.readExpr();
12574 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12575 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12576 /*IsNonContiguous=*/Args: false);
12577 }
12578 C->setComponents(Components, CLSs: ListSizes);
12579}
12580
12581void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12582 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12583 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12584 C->setLParenLoc(Record.readSourceLocation());
12585 C->setColonLoc(Record.readSourceLocation());
12586 C->setAllocator(Record.readSubExpr());
12587 C->setAlignment(Record.readSubExpr());
12588 unsigned NumVars = C->varlist_size();
12589 SmallVector<Expr *, 16> Vars;
12590 Vars.reserve(N: NumVars);
12591 for (unsigned i = 0; i != NumVars; ++i)
12592 Vars.push_back(Elt: Record.readSubExpr());
12593 C->setVarRefs(Vars);
12594}
12595
12596void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12597 C->setModifier(Record.readEnum<OpenMPNumTeamsClauseModifier>());
12598 C->setModifierLoc(Record.readSourceLocation());
12599 C->setModifierExpr(Record.readSubExpr());
12600 VisitOMPClauseWithPreInit(C);
12601 C->setLParenLoc(Record.readSourceLocation());
12602 unsigned NumVars = C->varlist_size();
12603 SmallVector<Expr *, 16> Vars;
12604 Vars.reserve(N: NumVars);
12605 for (unsigned I = 0; I != NumVars; ++I)
12606 Vars.push_back(Elt: Record.readSubExpr());
12607 C->setVarRefs(Vars);
12608}
12609
12610void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12611 VisitOMPClauseWithPreInit(C);
12612 C->setLParenLoc(Record.readSourceLocation());
12613 unsigned NumVars = C->varlist_size();
12614 SmallVector<Expr *, 16> Vars;
12615 Vars.reserve(N: NumVars);
12616 for (unsigned I = 0; I != NumVars; ++I)
12617 Vars.push_back(Elt: Record.readSubExpr());
12618 C->setVarRefs(Vars);
12619}
12620
12621void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12622 VisitOMPClauseWithPreInit(C);
12623 C->setPriority(Record.readSubExpr());
12624 C->setLParenLoc(Record.readSourceLocation());
12625}
12626
12627void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12628 VisitOMPClauseWithPreInit(C);
12629 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12630 C->setGrainsize(Record.readSubExpr());
12631 C->setModifierLoc(Record.readSourceLocation());
12632 C->setLParenLoc(Record.readSourceLocation());
12633}
12634
12635void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12636 VisitOMPClauseWithPreInit(C);
12637 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12638 C->setNumTasks(Record.readSubExpr());
12639 C->setModifierLoc(Record.readSourceLocation());
12640 C->setLParenLoc(Record.readSourceLocation());
12641}
12642
12643void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12644 C->setHint(Record.readSubExpr());
12645 C->setLParenLoc(Record.readSourceLocation());
12646}
12647
12648void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12649 VisitOMPClauseWithPreInit(C);
12650 C->setDistScheduleKind(
12651 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12652 C->setChunkSize(Record.readSubExpr());
12653 C->setLParenLoc(Record.readSourceLocation());
12654 C->setDistScheduleKindLoc(Record.readSourceLocation());
12655 C->setCommaLoc(Record.readSourceLocation());
12656}
12657
12658void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12659 C->setDefaultmapKind(
12660 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12661 C->setDefaultmapModifier(
12662 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12663 C->setLParenLoc(Record.readSourceLocation());
12664 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12665 C->setDefaultmapKindLoc(Record.readSourceLocation());
12666}
12667
12668void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12669 C->setLParenLoc(Record.readSourceLocation());
12670 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12671 C->setMotionModifier(
12672 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12673 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12674 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12675 C->setIteratorModifier(Record.readExpr());
12676 }
12677 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12678 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12679 C->setColonLoc(Record.readSourceLocation());
12680 auto NumVars = C->varlist_size();
12681 auto UniqueDecls = C->getUniqueDeclarationsNum();
12682 auto TotalLists = C->getTotalComponentListNum();
12683 auto TotalComponents = C->getTotalComponentsNum();
12684
12685 SmallVector<Expr *, 16> Vars;
12686 Vars.reserve(N: NumVars);
12687 for (unsigned i = 0; i != NumVars; ++i)
12688 Vars.push_back(Elt: Record.readSubExpr());
12689 C->setVarRefs(Vars);
12690
12691 SmallVector<Expr *, 16> UDMappers;
12692 UDMappers.reserve(N: NumVars);
12693 for (unsigned I = 0; I < NumVars; ++I)
12694 UDMappers.push_back(Elt: Record.readSubExpr());
12695 C->setUDMapperRefs(UDMappers);
12696
12697 SmallVector<ValueDecl *, 16> Decls;
12698 Decls.reserve(N: UniqueDecls);
12699 for (unsigned i = 0; i < UniqueDecls; ++i)
12700 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12701 C->setUniqueDecls(Decls);
12702
12703 SmallVector<unsigned, 16> ListsPerDecl;
12704 ListsPerDecl.reserve(N: UniqueDecls);
12705 for (unsigned i = 0; i < UniqueDecls; ++i)
12706 ListsPerDecl.push_back(Elt: Record.readInt());
12707 C->setDeclNumLists(ListsPerDecl);
12708
12709 SmallVector<unsigned, 32> ListSizes;
12710 ListSizes.reserve(N: TotalLists);
12711 for (unsigned i = 0; i < TotalLists; ++i)
12712 ListSizes.push_back(Elt: Record.readInt());
12713 C->setComponentListSizes(ListSizes);
12714
12715 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12716 Components.reserve(N: TotalComponents);
12717 for (unsigned i = 0; i < TotalComponents; ++i) {
12718 Expr *AssociatedExprPr = Record.readSubExpr();
12719 bool IsNonContiguous = Record.readBool();
12720 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12721 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12722 }
12723 C->setComponents(Components, CLSs: ListSizes);
12724}
12725
12726void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12727 C->setLParenLoc(Record.readSourceLocation());
12728 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12729 C->setMotionModifier(
12730 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12731 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12732 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12733 C->setIteratorModifier(Record.readExpr());
12734 }
12735 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12736 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12737 C->setColonLoc(Record.readSourceLocation());
12738 auto NumVars = C->varlist_size();
12739 auto UniqueDecls = C->getUniqueDeclarationsNum();
12740 auto TotalLists = C->getTotalComponentListNum();
12741 auto TotalComponents = C->getTotalComponentsNum();
12742
12743 SmallVector<Expr *, 16> Vars;
12744 Vars.reserve(N: NumVars);
12745 for (unsigned i = 0; i != NumVars; ++i)
12746 Vars.push_back(Elt: Record.readSubExpr());
12747 C->setVarRefs(Vars);
12748
12749 SmallVector<Expr *, 16> UDMappers;
12750 UDMappers.reserve(N: NumVars);
12751 for (unsigned I = 0; I < NumVars; ++I)
12752 UDMappers.push_back(Elt: Record.readSubExpr());
12753 C->setUDMapperRefs(UDMappers);
12754
12755 SmallVector<ValueDecl *, 16> Decls;
12756 Decls.reserve(N: UniqueDecls);
12757 for (unsigned i = 0; i < UniqueDecls; ++i)
12758 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12759 C->setUniqueDecls(Decls);
12760
12761 SmallVector<unsigned, 16> ListsPerDecl;
12762 ListsPerDecl.reserve(N: UniqueDecls);
12763 for (unsigned i = 0; i < UniqueDecls; ++i)
12764 ListsPerDecl.push_back(Elt: Record.readInt());
12765 C->setDeclNumLists(ListsPerDecl);
12766
12767 SmallVector<unsigned, 32> ListSizes;
12768 ListSizes.reserve(N: TotalLists);
12769 for (unsigned i = 0; i < TotalLists; ++i)
12770 ListSizes.push_back(Elt: Record.readInt());
12771 C->setComponentListSizes(ListSizes);
12772
12773 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12774 Components.reserve(N: TotalComponents);
12775 for (unsigned i = 0; i < TotalComponents; ++i) {
12776 Expr *AssociatedExprPr = Record.readSubExpr();
12777 bool IsNonContiguous = Record.readBool();
12778 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12779 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12780 }
12781 C->setComponents(Components, CLSs: ListSizes);
12782}
12783
12784void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12785 C->setLParenLoc(Record.readSourceLocation());
12786 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12787 C->setFallbackModifierLoc(Record.readSourceLocation());
12788 auto NumVars = C->varlist_size();
12789 auto UniqueDecls = C->getUniqueDeclarationsNum();
12790 auto TotalLists = C->getTotalComponentListNum();
12791 auto TotalComponents = C->getTotalComponentsNum();
12792
12793 SmallVector<Expr *, 16> Vars;
12794 Vars.reserve(N: NumVars);
12795 for (unsigned i = 0; i != NumVars; ++i)
12796 Vars.push_back(Elt: Record.readSubExpr());
12797 C->setVarRefs(Vars);
12798 Vars.clear();
12799 for (unsigned i = 0; i != NumVars; ++i)
12800 Vars.push_back(Elt: Record.readSubExpr());
12801 C->setPrivateCopies(Vars);
12802 Vars.clear();
12803 for (unsigned i = 0; i != NumVars; ++i)
12804 Vars.push_back(Elt: Record.readSubExpr());
12805 C->setInits(Vars);
12806
12807 SmallVector<ValueDecl *, 16> Decls;
12808 Decls.reserve(N: UniqueDecls);
12809 for (unsigned i = 0; i < UniqueDecls; ++i)
12810 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12811 C->setUniqueDecls(Decls);
12812
12813 SmallVector<unsigned, 16> ListsPerDecl;
12814 ListsPerDecl.reserve(N: UniqueDecls);
12815 for (unsigned i = 0; i < UniqueDecls; ++i)
12816 ListsPerDecl.push_back(Elt: Record.readInt());
12817 C->setDeclNumLists(ListsPerDecl);
12818
12819 SmallVector<unsigned, 32> ListSizes;
12820 ListSizes.reserve(N: TotalLists);
12821 for (unsigned i = 0; i < TotalLists; ++i)
12822 ListSizes.push_back(Elt: Record.readInt());
12823 C->setComponentListSizes(ListSizes);
12824
12825 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12826 Components.reserve(N: TotalComponents);
12827 for (unsigned i = 0; i < TotalComponents; ++i) {
12828 auto *AssociatedExprPr = Record.readSubExpr();
12829 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12830 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12831 /*IsNonContiguous=*/Args: false);
12832 }
12833 C->setComponents(Components, CLSs: ListSizes);
12834}
12835
12836void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12837 C->setLParenLoc(Record.readSourceLocation());
12838 auto NumVars = C->varlist_size();
12839 auto UniqueDecls = C->getUniqueDeclarationsNum();
12840 auto TotalLists = C->getTotalComponentListNum();
12841 auto TotalComponents = C->getTotalComponentsNum();
12842
12843 SmallVector<Expr *, 16> Vars;
12844 Vars.reserve(N: NumVars);
12845 for (unsigned i = 0; i != NumVars; ++i)
12846 Vars.push_back(Elt: Record.readSubExpr());
12847 C->setVarRefs(Vars);
12848
12849 SmallVector<ValueDecl *, 16> Decls;
12850 Decls.reserve(N: UniqueDecls);
12851 for (unsigned i = 0; i < UniqueDecls; ++i)
12852 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12853 C->setUniqueDecls(Decls);
12854
12855 SmallVector<unsigned, 16> ListsPerDecl;
12856 ListsPerDecl.reserve(N: UniqueDecls);
12857 for (unsigned i = 0; i < UniqueDecls; ++i)
12858 ListsPerDecl.push_back(Elt: Record.readInt());
12859 C->setDeclNumLists(ListsPerDecl);
12860
12861 SmallVector<unsigned, 32> ListSizes;
12862 ListSizes.reserve(N: TotalLists);
12863 for (unsigned i = 0; i < TotalLists; ++i)
12864 ListSizes.push_back(Elt: Record.readInt());
12865 C->setComponentListSizes(ListSizes);
12866
12867 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12868 Components.reserve(N: TotalComponents);
12869 for (unsigned i = 0; i < TotalComponents; ++i) {
12870 Expr *AssociatedExpr = Record.readSubExpr();
12871 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12872 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12873 /*IsNonContiguous*/ Args: false);
12874 }
12875 C->setComponents(Components, CLSs: ListSizes);
12876}
12877
12878void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12879 C->setLParenLoc(Record.readSourceLocation());
12880 auto NumVars = C->varlist_size();
12881 auto UniqueDecls = C->getUniqueDeclarationsNum();
12882 auto TotalLists = C->getTotalComponentListNum();
12883 auto TotalComponents = C->getTotalComponentsNum();
12884
12885 SmallVector<Expr *, 16> Vars;
12886 Vars.reserve(N: NumVars);
12887 for (unsigned i = 0; i != NumVars; ++i)
12888 Vars.push_back(Elt: Record.readSubExpr());
12889 C->setVarRefs(Vars);
12890 Vars.clear();
12891
12892 SmallVector<ValueDecl *, 16> Decls;
12893 Decls.reserve(N: UniqueDecls);
12894 for (unsigned i = 0; i < UniqueDecls; ++i)
12895 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12896 C->setUniqueDecls(Decls);
12897
12898 SmallVector<unsigned, 16> ListsPerDecl;
12899 ListsPerDecl.reserve(N: UniqueDecls);
12900 for (unsigned i = 0; i < UniqueDecls; ++i)
12901 ListsPerDecl.push_back(Elt: Record.readInt());
12902 C->setDeclNumLists(ListsPerDecl);
12903
12904 SmallVector<unsigned, 32> ListSizes;
12905 ListSizes.reserve(N: TotalLists);
12906 for (unsigned i = 0; i < TotalLists; ++i)
12907 ListSizes.push_back(Elt: Record.readInt());
12908 C->setComponentListSizes(ListSizes);
12909
12910 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12911 Components.reserve(N: TotalComponents);
12912 for (unsigned i = 0; i < TotalComponents; ++i) {
12913 Expr *AssociatedExpr = Record.readSubExpr();
12914 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12915 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12916 /*IsNonContiguous=*/Args: false);
12917 }
12918 C->setComponents(Components, CLSs: ListSizes);
12919}
12920
12921void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12922 C->setLParenLoc(Record.readSourceLocation());
12923 auto NumVars = C->varlist_size();
12924 auto UniqueDecls = C->getUniqueDeclarationsNum();
12925 auto TotalLists = C->getTotalComponentListNum();
12926 auto TotalComponents = C->getTotalComponentsNum();
12927
12928 SmallVector<Expr *, 16> Vars;
12929 Vars.reserve(N: NumVars);
12930 for (unsigned I = 0; I != NumVars; ++I)
12931 Vars.push_back(Elt: Record.readSubExpr());
12932 C->setVarRefs(Vars);
12933 Vars.clear();
12934
12935 SmallVector<ValueDecl *, 16> Decls;
12936 Decls.reserve(N: UniqueDecls);
12937 for (unsigned I = 0; I < UniqueDecls; ++I)
12938 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12939 C->setUniqueDecls(Decls);
12940
12941 SmallVector<unsigned, 16> ListsPerDecl;
12942 ListsPerDecl.reserve(N: UniqueDecls);
12943 for (unsigned I = 0; I < UniqueDecls; ++I)
12944 ListsPerDecl.push_back(Elt: Record.readInt());
12945 C->setDeclNumLists(ListsPerDecl);
12946
12947 SmallVector<unsigned, 32> ListSizes;
12948 ListSizes.reserve(N: TotalLists);
12949 for (unsigned i = 0; i < TotalLists; ++i)
12950 ListSizes.push_back(Elt: Record.readInt());
12951 C->setComponentListSizes(ListSizes);
12952
12953 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12954 Components.reserve(N: TotalComponents);
12955 for (unsigned I = 0; I < TotalComponents; ++I) {
12956 Expr *AssociatedExpr = Record.readSubExpr();
12957 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12958 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12959 /*IsNonContiguous=*/Args: false);
12960 }
12961 C->setComponents(Components, CLSs: ListSizes);
12962}
12963
12964void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12965 C->setLParenLoc(Record.readSourceLocation());
12966 unsigned NumVars = C->varlist_size();
12967 SmallVector<Expr *, 16> Vars;
12968 Vars.reserve(N: NumVars);
12969 for (unsigned i = 0; i != NumVars; ++i)
12970 Vars.push_back(Elt: Record.readSubExpr());
12971 C->setVarRefs(Vars);
12972 Vars.clear();
12973 Vars.reserve(N: NumVars);
12974 for (unsigned i = 0; i != NumVars; ++i)
12975 Vars.push_back(Elt: Record.readSubExpr());
12976 C->setPrivateRefs(Vars);
12977}
12978
12979void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12980 C->setLParenLoc(Record.readSourceLocation());
12981 unsigned NumVars = C->varlist_size();
12982 SmallVector<Expr *, 16> Vars;
12983 Vars.reserve(N: NumVars);
12984 for (unsigned i = 0; i != NumVars; ++i)
12985 Vars.push_back(Elt: Record.readSubExpr());
12986 C->setVarRefs(Vars);
12987}
12988
12989void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12990 C->setLParenLoc(Record.readSourceLocation());
12991 unsigned NumVars = C->varlist_size();
12992 SmallVector<Expr *, 16> Vars;
12993 Vars.reserve(N: NumVars);
12994 for (unsigned i = 0; i != NumVars; ++i)
12995 Vars.push_back(Elt: Record.readSubExpr());
12996 C->setVarRefs(Vars);
12997}
12998
12999void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
13000 C->setLParenLoc(Record.readSourceLocation());
13001 unsigned NumOfAllocators = C->getNumberOfAllocators();
13002 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
13003 Data.reserve(N: NumOfAllocators);
13004 for (unsigned I = 0; I != NumOfAllocators; ++I) {
13005 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
13006 D.Allocator = Record.readSubExpr();
13007 D.AllocatorTraits = Record.readSubExpr();
13008 D.LParenLoc = Record.readSourceLocation();
13009 D.RParenLoc = Record.readSourceLocation();
13010 }
13011 C->setAllocatorsData(Data);
13012}
13013
13014void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
13015 C->setLParenLoc(Record.readSourceLocation());
13016 C->setModifier(Record.readSubExpr());
13017 C->setColonLoc(Record.readSourceLocation());
13018 unsigned NumOfLocators = C->varlist_size();
13019 SmallVector<Expr *, 4> Locators;
13020 Locators.reserve(N: NumOfLocators);
13021 for (unsigned I = 0; I != NumOfLocators; ++I)
13022 Locators.push_back(Elt: Record.readSubExpr());
13023 C->setVarRefs(Locators);
13024}
13025
13026void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
13027 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
13028 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
13029 C->setLParenLoc(Record.readSourceLocation());
13030 C->setKindKwLoc(Record.readSourceLocation());
13031 C->setModifierKwLoc(Record.readSourceLocation());
13032}
13033
13034void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
13035 VisitOMPClauseWithPreInit(C);
13036 C->setThreadID(Record.readSubExpr());
13037 C->setLParenLoc(Record.readSourceLocation());
13038}
13039
13040void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
13041 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
13042 C->setLParenLoc(Record.readSourceLocation());
13043 C->setBindKindLoc(Record.readSourceLocation());
13044}
13045
13046void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
13047 C->setAlignment(Record.readExpr());
13048 C->setLParenLoc(Record.readSourceLocation());
13049}
13050
13051void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
13052 VisitOMPClauseWithPreInit(C);
13053 C->setSize(Record.readSubExpr());
13054 C->setLParenLoc(Record.readSourceLocation());
13055}
13056
13057void OMPClauseReader::VisitOMPDynGroupprivateClause(
13058 OMPDynGroupprivateClause *C) {
13059 VisitOMPClauseWithPreInit(C);
13060 C->setDynGroupprivateModifier(
13061 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
13062 C->setDynGroupprivateFallbackModifier(
13063 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
13064 C->setSize(Record.readSubExpr());
13065 C->setLParenLoc(Record.readSourceLocation());
13066 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
13067 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
13068}
13069
13070void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
13071 C->setLParenLoc(Record.readSourceLocation());
13072 C->setDependenceType(
13073 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
13074 C->setDependenceLoc(Record.readSourceLocation());
13075 C->setColonLoc(Record.readSourceLocation());
13076 unsigned NumVars = C->varlist_size();
13077 SmallVector<Expr *, 16> Vars;
13078 Vars.reserve(N: NumVars);
13079 for (unsigned I = 0; I != NumVars; ++I)
13080 Vars.push_back(Elt: Record.readSubExpr());
13081 C->setVarRefs(Vars);
13082 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13083 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13084}
13085
13086void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13087 AttrVec Attrs;
13088 Record.readAttributes(Attrs);
13089 C->setAttrs(Attrs);
13090 C->setLocStart(Record.readSourceLocation());
13091 C->setLParenLoc(Record.readSourceLocation());
13092 C->setLocEnd(Record.readSourceLocation());
13093}
13094
13095void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13096
13097OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13098 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13099 TI.Sets.resize(N: readUInt32());
13100 for (auto &Set : TI.Sets) {
13101 Set.Kind = readEnum<llvm::omp::TraitSet>();
13102 Set.Selectors.resize(N: readUInt32());
13103 for (auto &Selector : Set.Selectors) {
13104 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13105 Selector.ScoreOrCondition = nullptr;
13106 if (readBool())
13107 Selector.ScoreOrCondition = readExprRef();
13108 Selector.Properties.resize(N: readUInt32());
13109 for (auto &Property : Selector.Properties)
13110 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13111 }
13112 }
13113 return &TI;
13114}
13115
13116void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13117 if (!Data)
13118 return;
13119 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13120 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13121 skipInts(N: 3);
13122 }
13123 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13124 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13125 Clauses[I] = readOMPClause();
13126 Data->setClauses(Clauses);
13127 if (Data->hasAssociatedStmt())
13128 Data->setAssociatedStmt(readStmt());
13129 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13130 Data->getChildren()[I] = readStmt();
13131}
13132
13133SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13134 unsigned NumVars = readInt();
13135 llvm::SmallVector<Expr *> VarList;
13136 for (unsigned I = 0; I < NumVars; ++I)
13137 VarList.push_back(Elt: readExpr());
13138 return VarList;
13139}
13140
13141SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13142 unsigned NumExprs = readInt();
13143 llvm::SmallVector<Expr *> ExprList;
13144 for (unsigned I = 0; I < NumExprs; ++I)
13145 ExprList.push_back(Elt: readSubExpr());
13146 return ExprList;
13147}
13148
13149OpenACCClause *ASTRecordReader::readOpenACCClause() {
13150 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13151 SourceLocation BeginLoc = readSourceLocation();
13152 SourceLocation EndLoc = readSourceLocation();
13153
13154 switch (ClauseKind) {
13155 case OpenACCClauseKind::Default: {
13156 SourceLocation LParenLoc = readSourceLocation();
13157 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13158 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13159 EndLoc);
13160 }
13161 case OpenACCClauseKind::If: {
13162 SourceLocation LParenLoc = readSourceLocation();
13163 Expr *CondExpr = readSubExpr();
13164 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13165 EndLoc);
13166 }
13167 case OpenACCClauseKind::Self: {
13168 SourceLocation LParenLoc = readSourceLocation();
13169 bool isConditionExprClause = readBool();
13170 if (isConditionExprClause) {
13171 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13172 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13173 ConditionExpr: CondExpr, EndLoc);
13174 }
13175 unsigned NumVars = readInt();
13176 llvm::SmallVector<Expr *> VarList;
13177 for (unsigned I = 0; I < NumVars; ++I)
13178 VarList.push_back(Elt: readSubExpr());
13179 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13180 EndLoc);
13181 }
13182 case OpenACCClauseKind::NumGangs: {
13183 SourceLocation LParenLoc = readSourceLocation();
13184 unsigned NumClauses = readInt();
13185 llvm::SmallVector<Expr *> IntExprs;
13186 for (unsigned I = 0; I < NumClauses; ++I)
13187 IntExprs.push_back(Elt: readSubExpr());
13188 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13189 IntExprs, EndLoc);
13190 }
13191 case OpenACCClauseKind::NumWorkers: {
13192 SourceLocation LParenLoc = readSourceLocation();
13193 Expr *IntExpr = readSubExpr();
13194 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13195 IntExpr, EndLoc);
13196 }
13197 case OpenACCClauseKind::DeviceNum: {
13198 SourceLocation LParenLoc = readSourceLocation();
13199 Expr *IntExpr = readSubExpr();
13200 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13201 IntExpr, EndLoc);
13202 }
13203 case OpenACCClauseKind::DefaultAsync: {
13204 SourceLocation LParenLoc = readSourceLocation();
13205 Expr *IntExpr = readSubExpr();
13206 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13207 IntExpr, EndLoc);
13208 }
13209 case OpenACCClauseKind::VectorLength: {
13210 SourceLocation LParenLoc = readSourceLocation();
13211 Expr *IntExpr = readSubExpr();
13212 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13213 IntExpr, EndLoc);
13214 }
13215 case OpenACCClauseKind::Private: {
13216 SourceLocation LParenLoc = readSourceLocation();
13217 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13218
13219 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13220 for (unsigned I = 0; I < VarList.size(); ++I) {
13221 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13222 VarDecl *Alloca = readDeclAs<VarDecl>();
13223 RecipeList.push_back(Elt: {Alloca});
13224 }
13225
13226 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13227 VarList, InitRecipes: RecipeList, EndLoc);
13228 }
13229 case OpenACCClauseKind::Host: {
13230 SourceLocation LParenLoc = readSourceLocation();
13231 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13232 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13233 EndLoc);
13234 }
13235 case OpenACCClauseKind::Device: {
13236 SourceLocation LParenLoc = readSourceLocation();
13237 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13238 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13239 VarList, EndLoc);
13240 }
13241 case OpenACCClauseKind::FirstPrivate: {
13242 SourceLocation LParenLoc = readSourceLocation();
13243 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13244 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13245 for (unsigned I = 0; I < VarList.size(); ++I) {
13246 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13247 VarDecl *Recipe = readDeclAs<VarDecl>();
13248 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13249 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13250 }
13251
13252 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13253 VarList, InitRecipes: RecipeList, EndLoc);
13254 }
13255 case OpenACCClauseKind::Attach: {
13256 SourceLocation LParenLoc = readSourceLocation();
13257 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13258 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13259 VarList, EndLoc);
13260 }
13261 case OpenACCClauseKind::Detach: {
13262 SourceLocation LParenLoc = readSourceLocation();
13263 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13264 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13265 VarList, EndLoc);
13266 }
13267 case OpenACCClauseKind::Delete: {
13268 SourceLocation LParenLoc = readSourceLocation();
13269 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13270 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13271 VarList, EndLoc);
13272 }
13273 case OpenACCClauseKind::UseDevice: {
13274 SourceLocation LParenLoc = readSourceLocation();
13275 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13276 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13277 VarList, EndLoc);
13278 }
13279 case OpenACCClauseKind::DevicePtr: {
13280 SourceLocation LParenLoc = readSourceLocation();
13281 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13282 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13283 VarList, EndLoc);
13284 }
13285 case OpenACCClauseKind::NoCreate: {
13286 SourceLocation LParenLoc = readSourceLocation();
13287 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13288 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13289 VarList, EndLoc);
13290 }
13291 case OpenACCClauseKind::Present: {
13292 SourceLocation LParenLoc = readSourceLocation();
13293 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13294 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13295 VarList, EndLoc);
13296 }
13297 case OpenACCClauseKind::PCopy:
13298 case OpenACCClauseKind::PresentOrCopy:
13299 case OpenACCClauseKind::Copy: {
13300 SourceLocation LParenLoc = readSourceLocation();
13301 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13302 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13303 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13304 LParenLoc, Mods: ModList, VarList, EndLoc);
13305 }
13306 case OpenACCClauseKind::CopyIn:
13307 case OpenACCClauseKind::PCopyIn:
13308 case OpenACCClauseKind::PresentOrCopyIn: {
13309 SourceLocation LParenLoc = readSourceLocation();
13310 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13311 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13312 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13313 LParenLoc, Mods: ModList, VarList, EndLoc);
13314 }
13315 case OpenACCClauseKind::CopyOut:
13316 case OpenACCClauseKind::PCopyOut:
13317 case OpenACCClauseKind::PresentOrCopyOut: {
13318 SourceLocation LParenLoc = readSourceLocation();
13319 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13320 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13321 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13322 LParenLoc, Mods: ModList, VarList, EndLoc);
13323 }
13324 case OpenACCClauseKind::Create:
13325 case OpenACCClauseKind::PCreate:
13326 case OpenACCClauseKind::PresentOrCreate: {
13327 SourceLocation LParenLoc = readSourceLocation();
13328 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13329 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13330 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13331 LParenLoc, Mods: ModList, VarList, EndLoc);
13332 }
13333 case OpenACCClauseKind::Async: {
13334 SourceLocation LParenLoc = readSourceLocation();
13335 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13336 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13337 IntExpr: AsyncExpr, EndLoc);
13338 }
13339 case OpenACCClauseKind::Wait: {
13340 SourceLocation LParenLoc = readSourceLocation();
13341 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13342 SourceLocation QueuesLoc = readSourceLocation();
13343 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13344 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13345 DevNumExpr, QueuesLoc, QueueIdExprs,
13346 EndLoc);
13347 }
13348 case OpenACCClauseKind::DeviceType:
13349 case OpenACCClauseKind::DType: {
13350 SourceLocation LParenLoc = readSourceLocation();
13351 llvm::SmallVector<DeviceTypeArgument> Archs;
13352 unsigned NumArchs = readInt();
13353
13354 for (unsigned I = 0; I < NumArchs; ++I) {
13355 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13356 SourceLocation Loc = readSourceLocation();
13357 Archs.emplace_back(Args&: Loc, Args&: Ident);
13358 }
13359
13360 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13361 LParenLoc, Archs, EndLoc);
13362 }
13363 case OpenACCClauseKind::Reduction: {
13364 SourceLocation LParenLoc = readSourceLocation();
13365 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13366 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13367 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13368
13369 for (unsigned I = 0; I < VarList.size(); ++I) {
13370 VarDecl *Recipe = readDeclAs<VarDecl>();
13371
13372 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13373 3 * sizeof(int *));
13374
13375 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13376 unsigned NumCombiners = readInt();
13377 for (unsigned I = 0; I < NumCombiners; ++I) {
13378 VarDecl *LHS = readDeclAs<VarDecl>();
13379 VarDecl *RHS = readDeclAs<VarDecl>();
13380 Expr *Op = readExpr();
13381
13382 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13383 }
13384
13385 RecipeList.push_back(Elt: {Recipe, Combiners});
13386 }
13387
13388 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13389 VarList, Recipes: RecipeList, EndLoc);
13390 }
13391 case OpenACCClauseKind::Seq:
13392 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13393 case OpenACCClauseKind::NoHost:
13394 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13395 case OpenACCClauseKind::Finalize:
13396 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13397 case OpenACCClauseKind::IfPresent:
13398 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13399 case OpenACCClauseKind::Independent:
13400 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13401 case OpenACCClauseKind::Auto:
13402 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13403 case OpenACCClauseKind::Collapse: {
13404 SourceLocation LParenLoc = readSourceLocation();
13405 bool HasForce = readBool();
13406 Expr *LoopCount = readSubExpr();
13407 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13408 HasForce, LoopCount, EndLoc);
13409 }
13410 case OpenACCClauseKind::Tile: {
13411 SourceLocation LParenLoc = readSourceLocation();
13412 unsigned NumClauses = readInt();
13413 llvm::SmallVector<Expr *> SizeExprs;
13414 for (unsigned I = 0; I < NumClauses; ++I)
13415 SizeExprs.push_back(Elt: readSubExpr());
13416 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13417 SizeExprs, EndLoc);
13418 }
13419 case OpenACCClauseKind::Gang: {
13420 SourceLocation LParenLoc = readSourceLocation();
13421 unsigned NumExprs = readInt();
13422 llvm::SmallVector<OpenACCGangKind> GangKinds;
13423 llvm::SmallVector<Expr *> Exprs;
13424 for (unsigned I = 0; I < NumExprs; ++I) {
13425 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13426 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13427 Exprs.push_back(Elt: readExpr());
13428 }
13429 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13430 GangKinds, IntExprs: Exprs, EndLoc);
13431 }
13432 case OpenACCClauseKind::Worker: {
13433 SourceLocation LParenLoc = readSourceLocation();
13434 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13435 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13436 IntExpr: WorkerExpr, EndLoc);
13437 }
13438 case OpenACCClauseKind::Vector: {
13439 SourceLocation LParenLoc = readSourceLocation();
13440 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13441 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13442 IntExpr: VectorExpr, EndLoc);
13443 }
13444 case OpenACCClauseKind::Link: {
13445 SourceLocation LParenLoc = readSourceLocation();
13446 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13447 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13448 EndLoc);
13449 }
13450 case OpenACCClauseKind::DeviceResident: {
13451 SourceLocation LParenLoc = readSourceLocation();
13452 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13453 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13454 LParenLoc, VarList, EndLoc);
13455 }
13456
13457 case OpenACCClauseKind::Bind: {
13458 SourceLocation LParenLoc = readSourceLocation();
13459 bool IsString = readBool();
13460 if (IsString)
13461 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13462 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13463 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13464 ID: readIdentifier(), EndLoc);
13465 }
13466 case OpenACCClauseKind::Shortloop:
13467 case OpenACCClauseKind::Invalid:
13468 llvm_unreachable("Clause serialization not yet implemented");
13469 }
13470 llvm_unreachable("Invalid Clause Kind");
13471}
13472
13473void ASTRecordReader::readOpenACCClauseList(
13474 MutableArrayRef<const OpenACCClause *> Clauses) {
13475 for (unsigned I = 0; I < Clauses.size(); ++I)
13476 Clauses[I] = readOpenACCClause();
13477}
13478
13479void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13480 unsigned NumVars = readInt();
13481 A->Clauses.resize(N: NumVars);
13482 readOpenACCClauseList(Clauses: A->Clauses);
13483}
13484
13485static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13486 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13487 // module name?
13488 llvm::FoldingSetNodeID ID;
13489 ID.AddString(String: PrimaryModuleName);
13490 return ID.computeStableHash();
13491}
13492
13493UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13494 if (!M)
13495 return std::nullopt;
13496
13497 if (M->isHeaderLikeModule())
13498 return std::nullopt;
13499
13500 if (M->isGlobalModule())
13501 return std::nullopt;
13502
13503 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13504 return getStableHashForModuleName(PrimaryModuleName);
13505}
13506