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 First->visitModuleFile(Filename, Kind);
247 Second->visitModuleFile(Filename, Kind);
248}
249
250bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
251 bool isSystem,
252 bool isOverridden,
253 bool isExplicitModule) {
254 bool Continue = false;
255 if (First->needsInputFileVisitation() &&
256 (!isSystem || First->needsSystemInputFileVisitation()))
257 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
258 isExplicitModule);
259 if (Second->needsInputFileVisitation() &&
260 (!isSystem || Second->needsSystemInputFileVisitation()))
261 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
262 isExplicitModule);
263 return Continue;
264}
265
266void ChainedASTReaderListener::readModuleFileExtension(
267 const ModuleFileExtensionMetadata &Metadata) {
268 First->readModuleFileExtension(Metadata);
269 Second->readModuleFileExtension(Metadata);
270}
271
272//===----------------------------------------------------------------------===//
273// PCH validator implementation
274//===----------------------------------------------------------------------===//
275
276ASTReaderListener::~ASTReaderListener() = default;
277
278/// Compare the given set of language options against an existing set of
279/// language options.
280///
281/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
282/// \param AllowCompatibleDifferences If true, differences between compatible
283/// language options will be permitted.
284///
285/// \returns true if the languagae options mis-match, false otherwise.
286static bool checkLanguageOptions(const LangOptions &LangOpts,
287 const LangOptions &ExistingLangOpts,
288 StringRef ModuleFilename,
289 DiagnosticsEngine *Diags,
290 bool AllowCompatibleDifferences = true) {
291 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
292 using CK = LangOptions::CompatibilityKind;
293
294#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
295 if constexpr (CK::Compatibility != CK::Benign) { \
296 if ((CK::Compatibility == CK::NotCompatible) || \
297 (CK::Compatibility == CK::Compatible && \
298 !AllowCompatibleDifferences)) { \
299 if (ExistingLangOpts.Name != LangOpts.Name) { \
300 if (Diags) { \
301 if (Bits == 1) \
302 Diags->Report(diag::err_ast_file_langopt_mismatch) \
303 << Description << LangOpts.Name << ExistingLangOpts.Name \
304 << ModuleFilename; \
305 else \
306 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
307 << Description << ModuleFilename; \
308 } \
309 return true; \
310 } \
311 } \
312 }
313
314#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
315 if constexpr (CK::Compatibility != CK::Benign) { \
316 if ((CK::Compatibility == CK::NotCompatible) || \
317 (CK::Compatibility == CK::Compatible && \
318 !AllowCompatibleDifferences)) { \
319 if (ExistingLangOpts.Name != LangOpts.Name) { \
320 if (Diags) \
321 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
322 << Description << ModuleFilename; \
323 return true; \
324 } \
325 } \
326 }
327
328#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
329 if constexpr (CK::Compatibility != CK::Benign) { \
330 if ((CK::Compatibility == CK::NotCompatible) || \
331 (CK::Compatibility == CK::Compatible && \
332 !AllowCompatibleDifferences)) { \
333 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
334 if (Diags) \
335 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
336 << Description << ModuleFilename; \
337 return true; \
338 } \
339 } \
340 }
341
342#include "clang/Basic/LangOptions.def"
343
344 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
345 if (Diags)
346 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
347 << "module features" << ModuleFilename;
348 return true;
349 }
350
351 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
352 if (Diags)
353 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
354 << "target Objective-C runtime" << ModuleFilename;
355 return true;
356 }
357
358 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
359 LangOpts.CommentOpts.BlockCommandNames) {
360 if (Diags)
361 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
362 << "block command names" << ModuleFilename;
363 return true;
364 }
365
366 // Sanitizer feature mismatches are treated as compatible differences. If
367 // compatible differences aren't allowed, we still only want to check for
368 // mismatches of non-modular sanitizers (the only ones which can affect AST
369 // generation).
370 if (!AllowCompatibleDifferences) {
371 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
372 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
373 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
374 ExistingSanitizers.clear(K: ModularSanitizers);
375 ImportedSanitizers.clear(K: ModularSanitizers);
376 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
377 const std::string Flag = "-fsanitize=";
378 if (Diags) {
379#define SANITIZER(NAME, ID) \
380 { \
381 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
382 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
383 if (InExistingModule != InImportedModule) \
384 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
385 << InExistingModule << ModuleFilename << (Flag + NAME); \
386 }
387#include "clang/Basic/Sanitizers.def"
388 }
389 return true;
390 }
391 }
392
393 return false;
394}
395
396static bool checkCodegenOptions(const CodeGenOptions &CGOpts,
397 const CodeGenOptions &ExistingCGOpts,
398 StringRef ModuleFilename,
399 DiagnosticsEngine *Diags,
400 bool AllowCompatibleDifferences = true) {
401 // FIXME: Specify and print a description for each option instead of the name.
402 // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.
403 using CK = CodeGenOptions::CompatibilityKind;
404#define CODEGENOPT(Name, Bits, Default, Compatibility) \
405 if constexpr (CK::Compatibility != CK::Benign) { \
406 if ((CK::Compatibility == CK::NotCompatible) || \
407 (CK::Compatibility == CK::Compatible && \
408 !AllowCompatibleDifferences)) { \
409 if (ExistingCGOpts.Name != CGOpts.Name) { \
410 if (Diags) { \
411 if (Bits == 1) \
412 Diags->Report(diag::err_ast_file_codegenopt_mismatch) \
413 << #Name << CGOpts.Name << ExistingCGOpts.Name \
414 << ModuleFilename; \
415 else \
416 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
417 << #Name << ModuleFilename; \
418 } \
419 return true; \
420 } \
421 } \
422 }
423
424#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility) \
425 if constexpr (CK::Compatibility != CK::Benign) { \
426 if ((CK::Compatibility == CK::NotCompatible) || \
427 (CK::Compatibility == CK::Compatible && \
428 !AllowCompatibleDifferences)) { \
429 if (ExistingCGOpts.Name != CGOpts.Name) { \
430 if (Diags) \
431 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
432 << #Name << ModuleFilename; \
433 return true; \
434 } \
435 } \
436 }
437#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
438 if constexpr (CK::Compatibility != CK::Benign) { \
439 if ((CK::Compatibility == CK::NotCompatible) || \
440 (CK::Compatibility == CK::Compatible && \
441 !AllowCompatibleDifferences)) { \
442 if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \
443 if (Diags) \
444 Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \
445 << #Name << ModuleFilename; \
446 return true; \
447 } \
448 } \
449 }
450#define DEBUGOPT(Name, Bits, Default, Compatibility)
451#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
452#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
453#include "clang/Basic/CodeGenOptions.def"
454
455 return false;
456}
457
458/// Compare the given set of target options against an existing set of
459/// target options.
460///
461/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
462///
463/// \returns true if the target options mis-match, false otherwise.
464static bool checkTargetOptions(const TargetOptions &TargetOpts,
465 const TargetOptions &ExistingTargetOpts,
466 StringRef ModuleFilename,
467 DiagnosticsEngine *Diags,
468 bool AllowCompatibleDifferences = true) {
469#define CHECK_TARGET_OPT(Field, Name) \
470 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
471 if (Diags) \
472 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
473 << ModuleFilename << Name << TargetOpts.Field \
474 << ExistingTargetOpts.Field; \
475 return true; \
476 }
477
478 // The triple and ABI must match exactly.
479 CHECK_TARGET_OPT(Triple, "target");
480 CHECK_TARGET_OPT(ABI, "target ABI");
481
482 // We can tolerate different CPUs in many cases, notably when one CPU
483 // supports a strict superset of another. When allowing compatible
484 // differences skip this check.
485 if (!AllowCompatibleDifferences) {
486 CHECK_TARGET_OPT(CPU, "target CPU");
487 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
488 }
489
490#undef CHECK_TARGET_OPT
491
492 // Compare feature sets.
493 SmallVector<StringRef, 4> ExistingFeatures(
494 ExistingTargetOpts.FeaturesAsWritten.begin(),
495 ExistingTargetOpts.FeaturesAsWritten.end());
496 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
497 TargetOpts.FeaturesAsWritten.end());
498 llvm::sort(C&: ExistingFeatures);
499 llvm::sort(C&: ReadFeatures);
500
501 // We compute the set difference in both directions explicitly so that we can
502 // diagnose the differences differently.
503 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
504 std::set_difference(
505 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
506 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
507 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
508 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
509 result: std::back_inserter(x&: UnmatchedReadFeatures));
510
511 // If we are allowing compatible differences and the read feature set is
512 // a strict subset of the existing feature set, there is nothing to diagnose.
513 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
514 return false;
515
516 if (Diags) {
517 for (StringRef Feature : UnmatchedReadFeatures)
518 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
519 << /* is-existing-feature */ false << ModuleFilename << Feature;
520 for (StringRef Feature : UnmatchedExistingFeatures)
521 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
522 << /* is-existing-feature */ true << ModuleFilename << Feature;
523 }
524
525 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
526}
527
528bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
529 StringRef ModuleFilename, bool Complain,
530 bool AllowCompatibleDifferences) {
531 const LangOptions &ExistingLangOpts = PP.getLangOpts();
532 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
533 Diags: Complain ? &Reader.Diags : nullptr,
534 AllowCompatibleDifferences);
535}
536
537bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,
538 StringRef ModuleFilename, bool Complain,
539 bool AllowCompatibleDifferences) {
540 const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();
541 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
542 Diags: Complain ? &Reader.Diags : nullptr,
543 AllowCompatibleDifferences);
544}
545
546bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
547 StringRef ModuleFilename, bool Complain,
548 bool AllowCompatibleDifferences) {
549 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
550 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
551 Diags: Complain ? &Reader.Diags : nullptr,
552 AllowCompatibleDifferences);
553}
554
555namespace {
556
557using MacroDefinitionsMap =
558 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
559
560class DeclsSet {
561 SmallVector<NamedDecl *, 64> Decls;
562 llvm::SmallPtrSet<NamedDecl *, 8> Found;
563
564public:
565 operator ArrayRef<NamedDecl *>() const { return Decls; }
566
567 bool empty() const { return Decls.empty(); }
568
569 bool insert(NamedDecl *ND) {
570 auto [_, Inserted] = Found.insert(Ptr: ND);
571 if (Inserted)
572 Decls.push_back(Elt: ND);
573 return Inserted;
574 }
575};
576
577using DeclsMap = llvm::DenseMap<DeclarationName, DeclsSet>;
578
579} // namespace
580
581static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
582 DiagnosticsEngine &Diags,
583 StringRef ModuleFilename,
584 bool Complain) {
585 using Level = DiagnosticsEngine::Level;
586
587 // Check current mappings for new -Werror mappings, and the stored mappings
588 // for cases that were explicitly mapped to *not* be errors that are now
589 // errors because of options like -Werror.
590 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
591
592 for (DiagnosticsEngine *MappingSource : MappingSources) {
593 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
594 diag::kind DiagID = DiagIDMappingPair.first;
595 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
596 if (CurLevel < DiagnosticsEngine::Error)
597 continue; // not significant
598 Level StoredLevel =
599 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
600 if (StoredLevel < DiagnosticsEngine::Error) {
601 if (Complain)
602 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
603 << "-Werror=" + Diags.getDiagnosticIDs()
604 ->getWarningOptionForDiag(DiagID)
605 .str()
606 << ModuleFilename;
607 return true;
608 }
609 }
610 }
611
612 return false;
613}
614
615static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
616 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
617 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
618 return true;
619 return Ext >= diag::Severity::Error;
620}
621
622static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
623 DiagnosticsEngine &Diags,
624 StringRef ModuleFilename, bool IsSystem,
625 bool SystemHeaderWarningsInModule,
626 bool Complain) {
627 // Top-level options
628 if (IsSystem) {
629 if (Diags.getSuppressSystemWarnings())
630 return false;
631 // If -Wsystem-headers was not enabled before, and it was not explicit,
632 // be conservative
633 if (StoredDiags.getSuppressSystemWarnings() &&
634 !SystemHeaderWarningsInModule) {
635 if (Complain)
636 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
637 << "-Wsystem-headers" << ModuleFilename;
638 return true;
639 }
640 }
641
642 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
643 if (Complain)
644 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
645 << "-Werror" << ModuleFilename;
646 return true;
647 }
648
649 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
650 !StoredDiags.getEnableAllWarnings()) {
651 if (Complain)
652 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
653 << "-Weverything -Werror" << ModuleFilename;
654 return true;
655 }
656
657 if (isExtHandlingFromDiagsError(Diags) &&
658 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
659 if (Complain)
660 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
661 << "-pedantic-errors" << ModuleFilename;
662 return true;
663 }
664
665 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
666 Complain);
667}
668
669/// Return the top import module if it is implicit, nullptr otherwise.
670static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
671 Preprocessor &PP) {
672 // If the original import came from a file explicitly generated by the user,
673 // don't check the diagnostic mappings.
674 // FIXME: currently this is approximated by checking whether this is not a
675 // module import of an implicitly-loaded module file.
676 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
677 // the transitive closure of its imports, since unrelated modules cannot be
678 // imported until after this module finishes validation.
679 ModuleFile *TopImport = &*ModuleMgr.rbegin();
680 while (!TopImport->ImportedBy.empty())
681 TopImport = TopImport->ImportedBy[0];
682 if (TopImport->Kind != MK_ImplicitModule)
683 return nullptr;
684
685 StringRef ModuleName = TopImport->ModuleName;
686 assert(!ModuleName.empty() && "diagnostic options read before module name");
687
688 Module *M =
689 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
690 assert(M && "missing module");
691 return M;
692}
693
694bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
695 StringRef ModuleFilename,
696 bool Complain) {
697 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
698 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
699 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(A&: DiagIDs, A&: DiagOpts);
700 // This should never fail, because we would have processed these options
701 // before writing them to an ASTFile.
702 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
703 VFS&: PP.getFileManager().getVirtualFileSystem(),
704 /*Report*/ ReportDiags: false);
705
706 ModuleManager &ModuleMgr = Reader.getModuleManager();
707 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
708
709 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
710 if (!TopM)
711 return false;
712
713 Module *Importer = PP.getCurrentModule();
714
715 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
716 bool SystemHeaderWarningsInModule =
717 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
718 Element: Importer->Name);
719
720 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
721 // contains the union of their flags.
722 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
723 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
724 Complain);
725}
726
727/// Collect the macro definitions provided by the given preprocessor
728/// options.
729static void
730collectMacroDefinitions(const PreprocessorOptions &PPOpts,
731 MacroDefinitionsMap &Macros,
732 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
733 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
734 StringRef Macro = PPOpts.Macros[I].first;
735 bool IsUndef = PPOpts.Macros[I].second;
736
737 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
738 StringRef MacroName = MacroPair.first;
739 StringRef MacroBody = MacroPair.second;
740
741 // For an #undef'd macro, we only care about the name.
742 if (IsUndef) {
743 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
744 if (MacroNames && Inserted)
745 MacroNames->push_back(Elt: MacroName);
746
747 It->second = std::make_pair(x: "", y: true);
748 continue;
749 }
750
751 // For a #define'd macro, figure out the actual definition.
752 if (MacroName.size() == Macro.size())
753 MacroBody = "1";
754 else {
755 // Note: GCC drops anything following an end-of-line character.
756 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
757 MacroBody = MacroBody.substr(Start: 0, N: End);
758 }
759
760 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
761 if (MacroNames && Inserted)
762 MacroNames->push_back(Elt: MacroName);
763 It->second = std::make_pair(x&: MacroBody, y: false);
764 }
765}
766
767enum OptionValidation {
768 OptionValidateNone,
769 OptionValidateContradictions,
770 OptionValidateStrictMatches,
771};
772
773/// Check the preprocessor options deserialized from the control block
774/// against the preprocessor options in an existing preprocessor.
775///
776/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
777/// \param Validation If set to OptionValidateNone, ignore differences in
778/// preprocessor options. If set to OptionValidateContradictions,
779/// require that options passed both in the AST file and on the command
780/// line (-D or -U) match, but tolerate options missing in one or the
781/// other. If set to OptionValidateContradictions, require that there
782/// are no differences in the options between the two.
783static bool checkPreprocessorOptions(
784 const PreprocessorOptions &PPOpts,
785 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
786 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
787 std::string &SuggestedPredefines, const LangOptions &LangOpts,
788 OptionValidation Validation = OptionValidateContradictions) {
789 if (ReadMacros) {
790 // Check macro definitions.
791 MacroDefinitionsMap ASTFileMacros;
792 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
793 MacroDefinitionsMap ExistingMacros;
794 SmallVector<StringRef, 4> ExistingMacroNames;
795 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
796 MacroNames: &ExistingMacroNames);
797
798 // Use a line marker to enter the <command line> file, as the defines and
799 // undefines here will have come from the command line.
800 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
801
802 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
803 // Dig out the macro definition in the existing preprocessor options.
804 StringRef MacroName = ExistingMacroNames[I];
805 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
806
807 // Check whether we know anything about this macro name or not.
808 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
809 ASTFileMacros.find(Key: MacroName);
810 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
811 if (Validation == OptionValidateStrictMatches) {
812 // If strict matches are requested, don't tolerate any extra defines
813 // on the command line that are missing in the AST file.
814 if (Diags) {
815 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
816 << MacroName << true << ModuleFilename;
817 }
818 return true;
819 }
820 // FIXME: Check whether this identifier was referenced anywhere in the
821 // AST file. If so, we should reject the AST file. Unfortunately, this
822 // information isn't in the control block. What shall we do about it?
823
824 if (Existing.second) {
825 SuggestedPredefines += "#undef ";
826 SuggestedPredefines += MacroName.str();
827 SuggestedPredefines += '\n';
828 } else {
829 SuggestedPredefines += "#define ";
830 SuggestedPredefines += MacroName.str();
831 SuggestedPredefines += ' ';
832 SuggestedPredefines += Existing.first.str();
833 SuggestedPredefines += '\n';
834 }
835 continue;
836 }
837
838 // If the macro was defined in one but undef'd in the other, we have a
839 // conflict.
840 if (Existing.second != Known->second.second) {
841 if (Diags) {
842 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
843 << MacroName << Known->second.second << ModuleFilename;
844 }
845 return true;
846 }
847
848 // If the macro was #undef'd in both, or if the macro bodies are
849 // identical, it's fine.
850 if (Existing.second || Existing.first == Known->second.first) {
851 ASTFileMacros.erase(I: Known);
852 continue;
853 }
854
855 // The macro bodies differ; complain.
856 if (Diags) {
857 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
858 << MacroName << Known->second.first << Existing.first
859 << ModuleFilename;
860 }
861 return true;
862 }
863
864 // Leave the <command line> file and return to <built-in>.
865 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
866
867 if (Validation == OptionValidateStrictMatches) {
868 // If strict matches are requested, don't tolerate any extra defines in
869 // the AST file that are missing on the command line.
870 for (const auto &MacroName : ASTFileMacros.keys()) {
871 if (Diags) {
872 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
873 << MacroName << false << ModuleFilename;
874 }
875 return true;
876 }
877 }
878 }
879
880 // Check whether we're using predefines.
881 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
882 Validation != OptionValidateNone) {
883 if (Diags) {
884 Diags->Report(DiagID: diag::err_ast_file_undef)
885 << ExistingPPOpts.UsePredefines << ModuleFilename;
886 }
887 return true;
888 }
889
890 // Detailed record is important since it is used for the module cache hash.
891 if (LangOpts.Modules &&
892 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
893 Validation != OptionValidateNone) {
894 if (Diags) {
895 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
896 << PPOpts.DetailedRecord << ModuleFilename;
897 }
898 return true;
899 }
900
901 // Compute the #include and #include_macros lines we need.
902 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
903 StringRef File = ExistingPPOpts.Includes[I];
904
905 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
906 !ExistingPPOpts.PCHThroughHeader.empty()) {
907 // In case the through header is an include, we must add all the includes
908 // to the predefines so the start point can be determined.
909 SuggestedPredefines += "#include \"";
910 SuggestedPredefines += File;
911 SuggestedPredefines += "\"\n";
912 continue;
913 }
914
915 if (File == ExistingPPOpts.ImplicitPCHInclude)
916 continue;
917
918 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
919 continue;
920
921 SuggestedPredefines += "#include \"";
922 SuggestedPredefines += File;
923 SuggestedPredefines += "\"\n";
924 }
925
926 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
927 StringRef File = ExistingPPOpts.MacroIncludes[I];
928 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
929 continue;
930
931 SuggestedPredefines += "#__include_macros \"";
932 SuggestedPredefines += File;
933 SuggestedPredefines += "\"\n##\n";
934 }
935
936 return false;
937}
938
939bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
940 StringRef ModuleFilename,
941 bool ReadMacros, bool Complain,
942 std::string &SuggestedPredefines) {
943 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
944
945 return checkPreprocessorOptions(
946 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
947 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
948 SuggestedPredefines, LangOpts: PP.getLangOpts());
949}
950
951bool SimpleASTReaderListener::ReadPreprocessorOptions(
952 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
953 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
954 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
955 ModuleFilename, ReadMacros, Diags: nullptr,
956 FileMgr&: PP.getFileManager(), SuggestedPredefines,
957 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
958}
959
960/// Check that the specified and the existing module cache paths are equivalent.
961///
962/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
963/// \returns true when the module cache paths differ.
964static bool checkModuleCachePath(FileManager &FileMgr, StringRef ContextHash,
965 StringRef ExistingSpecificModuleCachePath,
966 StringRef ASTFilename,
967 DiagnosticsEngine *Diags,
968 const LangOptions &LangOpts,
969 const PreprocessorOptions &PPOpts,
970 const HeaderSearchOptions &HSOpts,
971 const HeaderSearchOptions &ASTFileHSOpts) {
972 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
973 FileMgr, ModuleCachePath: ASTFileHSOpts.ModuleCachePath, DisableModuleHash: ASTFileHSOpts.DisableModuleHash,
974 ContextHash: std::string(ContextHash));
975
976 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
977 SpecificModuleCachePath == ExistingSpecificModuleCachePath)
978 return false;
979 auto EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
980 A: SpecificModuleCachePath, B: ExistingSpecificModuleCachePath);
981 if (EqualOrErr && *EqualOrErr)
982 return false;
983 if (Diags) {
984 // If the module cache arguments provided from the command line are the
985 // same, the mismatch must come from other arguments of the configuration
986 // and not directly the cache path.
987 EqualOrErr = FileMgr.getVirtualFileSystem().equivalent(
988 A: ASTFileHSOpts.ModuleCachePath, B: HSOpts.ModuleCachePath);
989 if (EqualOrErr && *EqualOrErr)
990 Diags->Report(DiagID: clang::diag::warn_ast_file_config_mismatch) << ASTFilename;
991 else
992 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
993 << SpecificModuleCachePath << ExistingSpecificModuleCachePath
994 << ASTFilename;
995 }
996 return true;
997}
998
999bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
1000 StringRef ASTFilename,
1001 StringRef ContextHash,
1002 bool Complain) {
1003 const HeaderSearch &HeaderSearchInfo = PP.getHeaderSearchInfo();
1004 return checkModuleCachePath(FileMgr&: Reader.getFileManager(), ContextHash,
1005 ExistingSpecificModuleCachePath: HeaderSearchInfo.getSpecificModuleCachePath(),
1006 ASTFilename, Diags: Complain ? &Reader.Diags : nullptr,
1007 LangOpts: PP.getLangOpts(), PPOpts: PP.getPreprocessorOpts(),
1008 HSOpts: HeaderSearchInfo.getHeaderSearchOpts(), ASTFileHSOpts: HSOpts);
1009}
1010
1011void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {
1012 PP.setCounterValue(Value);
1013}
1014
1015//===----------------------------------------------------------------------===//
1016// AST reader implementation
1017//===----------------------------------------------------------------------===//
1018
1019static uint64_t readULEB(const unsigned char *&P) {
1020 unsigned Length = 0;
1021 const char *Error = nullptr;
1022
1023 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
1024 if (Error)
1025 llvm::report_fatal_error(reason: Error);
1026 P += Length;
1027 return Val;
1028}
1029
1030/// Read ULEB-encoded key length and data length.
1031static std::pair<unsigned, unsigned>
1032readULEBKeyDataLength(const unsigned char *&P) {
1033 unsigned KeyLen = readULEB(P);
1034 if ((unsigned)KeyLen != KeyLen)
1035 llvm::report_fatal_error(reason: "key too large");
1036
1037 unsigned DataLen = readULEB(P);
1038 if ((unsigned)DataLen != DataLen)
1039 llvm::report_fatal_error(reason: "data too large");
1040
1041 return std::make_pair(x&: KeyLen, y&: DataLen);
1042}
1043
1044void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
1045 bool TakeOwnership) {
1046 DeserializationListener = Listener;
1047 OwnsDeserializationListener = TakeOwnership;
1048}
1049
1050unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
1051 return serialization::ComputeHash(Sel);
1052}
1053
1054LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
1055 LocalDeclID ID(Value);
1056#ifndef NDEBUG
1057 if (!MF.ModuleOffsetMap.empty())
1058 Reader.ReadModuleOffsetMap(MF);
1059
1060 unsigned ModuleFileIndex = ID.getModuleFileIndex();
1061 unsigned LocalDeclID = ID.getLocalDeclIndex();
1062
1063 assert(ModuleFileIndex <= MF.TransitiveImports.size());
1064
1065 ModuleFile *OwningModuleFile =
1066 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
1067 assert(OwningModuleFile);
1068
1069 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
1070
1071 if (!ModuleFileIndex)
1072 LocalNumDecls += NUM_PREDEF_DECL_IDS;
1073
1074 assert(LocalDeclID < LocalNumDecls);
1075#endif
1076 (void)Reader;
1077 (void)MF;
1078 return ID;
1079}
1080
1081LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
1082 unsigned ModuleFileIndex, unsigned LocalDeclID) {
1083 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
1084 return LocalDeclID::get(Reader, MF, Value);
1085}
1086
1087std::pair<unsigned, unsigned>
1088ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
1089 return readULEBKeyDataLength(P&: d);
1090}
1091
1092ASTSelectorLookupTrait::internal_key_type
1093ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
1094 using namespace llvm::support;
1095
1096 SelectorTable &SelTable = Reader.getContext().Selectors;
1097 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1098 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
1099 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1100 if (N == 0)
1101 return SelTable.getNullarySelector(ID: FirstII);
1102 else if (N == 1)
1103 return SelTable.getUnarySelector(ID: FirstII);
1104
1105 SmallVector<const IdentifierInfo *, 16> Args;
1106 Args.push_back(Elt: FirstII);
1107 for (unsigned I = 1; I != N; ++I)
1108 Args.push_back(Elt: Reader.getLocalIdentifier(
1109 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
1110
1111 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
1112}
1113
1114ASTSelectorLookupTrait::data_type
1115ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1116 unsigned DataLen) {
1117 using namespace llvm::support;
1118
1119 data_type Result;
1120
1121 Result.ID = Reader.getGlobalSelectorID(
1122 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1123 unsigned FullInstanceBits =
1124 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1125 unsigned FullFactoryBits =
1126 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1127 Result.InstanceBits = FullInstanceBits & 0x3;
1128 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1129 Result.FactoryBits = FullFactoryBits & 0x3;
1130 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1131 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1132 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1133
1134 // Load instance methods
1135 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1136 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1137 F, LocalID: LocalDeclID::get(
1138 Reader, MF&: F,
1139 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1140 Result.Instance.push_back(Elt: Method);
1141 }
1142
1143 // Load factory methods
1144 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1145 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1146 F, LocalID: LocalDeclID::get(
1147 Reader, MF&: F,
1148 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1149 Result.Factory.push_back(Elt: Method);
1150 }
1151
1152 return Result;
1153}
1154
1155unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1156 return llvm::djbHash(Buffer: a);
1157}
1158
1159std::pair<unsigned, unsigned>
1160ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1161 return readULEBKeyDataLength(P&: d);
1162}
1163
1164ASTIdentifierLookupTraitBase::internal_key_type
1165ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1166 assert(n >= 2 && d[n-1] == '\0');
1167 return StringRef((const char*) d, n-1);
1168}
1169
1170/// Whether the given identifier is "interesting".
1171static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1172 bool IsModule) {
1173 bool IsInteresting =
1174 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1175 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1176 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1177 return II.hadMacroDefinition() || II.isPoisoned() ||
1178 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1179 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1180 II.getFETokenInfo());
1181}
1182
1183static bool readBit(unsigned &Bits) {
1184 bool Value = Bits & 0x1;
1185 Bits >>= 1;
1186 return Value;
1187}
1188
1189IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1190 using namespace llvm::support;
1191
1192 IdentifierID RawID =
1193 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1194 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1195}
1196
1197static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1198 bool IsModule) {
1199 if (!II.isFromAST()) {
1200 II.setIsFromAST();
1201 if (isInterestingIdentifier(Reader, II, IsModule))
1202 II.setChangedSinceDeserialization();
1203 }
1204}
1205
1206IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1207 const unsigned char* d,
1208 unsigned DataLen) {
1209 using namespace llvm::support;
1210
1211 IdentifierID RawID =
1212 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1213 bool IsInteresting = RawID & 0x01;
1214
1215 DataLen -= sizeof(IdentifierID);
1216
1217 // Wipe out the "is interesting" bit.
1218 RawID = RawID >> 1;
1219
1220 // Build the IdentifierInfo and link the identifier ID with it.
1221 IdentifierInfo *II = KnownII;
1222 if (!II) {
1223 II = &Reader.getIdentifierTable().getOwn(Name: k);
1224 KnownII = II;
1225 }
1226 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1227 markIdentifierFromAST(Reader, II&: *II, IsModule);
1228 Reader.markIdentifierUpToDate(II);
1229
1230 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1231 if (!IsInteresting) {
1232 // For uninteresting identifiers, there's nothing else to do. Just notify
1233 // the reader that we've finished loading this identifier.
1234 Reader.SetIdentifierInfo(ID, II);
1235 return II;
1236 }
1237
1238 unsigned ObjCOrBuiltinID =
1239 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1240 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1241 bool CPlusPlusOperatorKeyword = readBit(Bits);
1242 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1243 bool Poisoned = readBit(Bits);
1244 bool ExtensionToken = readBit(Bits);
1245 bool HasMacroDefinition = readBit(Bits);
1246
1247 assert(Bits == 0 && "Extra bits in the identifier?");
1248 DataLen -= sizeof(uint16_t) * 2;
1249
1250 // Set or check the various bits in the IdentifierInfo structure.
1251 // Token IDs are read-only.
1252 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1253 II->revertTokenIDToIdentifier();
1254 if (!F.isModule())
1255 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1256 assert(II->isExtensionToken() == ExtensionToken &&
1257 "Incorrect extension token flag");
1258 (void)ExtensionToken;
1259 if (Poisoned)
1260 II->setIsPoisoned(true);
1261 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1262 "Incorrect C++ operator keyword flag");
1263 (void)CPlusPlusOperatorKeyword;
1264
1265 // If this identifier has a macro definition, deserialize it or notify the
1266 // visitor the actual definition is in a different module.
1267 if (HasMacroDefinition) {
1268 uint32_t MacroDirectivesOffset =
1269 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1270 DataLen -= 4;
1271
1272 if (MacroDirectivesOffset)
1273 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1274 else
1275 hasMacroDefinitionInDependencies = true;
1276 }
1277
1278 Reader.SetIdentifierInfo(ID, II);
1279
1280 // Read all of the declarations visible at global scope with this
1281 // name.
1282 if (DataLen > 0) {
1283 SmallVector<GlobalDeclID, 4> DeclIDs;
1284 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1285 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1286 F, LocalID: LocalDeclID::get(
1287 Reader, MF&: F,
1288 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1289 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1290 }
1291
1292 return II;
1293}
1294
1295DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1296 : Kind(Name.getNameKind()) {
1297 switch (Kind) {
1298 case DeclarationName::Identifier:
1299 Data = (uint64_t)Name.getAsIdentifierInfo();
1300 break;
1301 case DeclarationName::ObjCZeroArgSelector:
1302 case DeclarationName::ObjCOneArgSelector:
1303 case DeclarationName::ObjCMultiArgSelector:
1304 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1305 break;
1306 case DeclarationName::CXXOperatorName:
1307 Data = Name.getCXXOverloadedOperator();
1308 break;
1309 case DeclarationName::CXXLiteralOperatorName:
1310 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1311 break;
1312 case DeclarationName::CXXDeductionGuideName:
1313 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1314 ->getDeclName().getAsIdentifierInfo();
1315 break;
1316 case DeclarationName::CXXConstructorName:
1317 case DeclarationName::CXXDestructorName:
1318 case DeclarationName::CXXConversionFunctionName:
1319 case DeclarationName::CXXUsingDirective:
1320 Data = 0;
1321 break;
1322 }
1323}
1324
1325unsigned DeclarationNameKey::getHash() const {
1326 llvm::FoldingSetNodeID ID;
1327 ID.AddInteger(I: Kind);
1328
1329 switch (Kind) {
1330 case DeclarationName::Identifier:
1331 case DeclarationName::CXXLiteralOperatorName:
1332 case DeclarationName::CXXDeductionGuideName:
1333 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1334 break;
1335 case DeclarationName::ObjCZeroArgSelector:
1336 case DeclarationName::ObjCOneArgSelector:
1337 case DeclarationName::ObjCMultiArgSelector:
1338 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1339 break;
1340 case DeclarationName::CXXOperatorName:
1341 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1342 break;
1343 case DeclarationName::CXXConstructorName:
1344 case DeclarationName::CXXDestructorName:
1345 case DeclarationName::CXXConversionFunctionName:
1346 case DeclarationName::CXXUsingDirective:
1347 break;
1348 }
1349
1350 return ID.computeStableHash();
1351}
1352
1353ModuleFile *
1354ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1355 using namespace llvm::support;
1356
1357 uint32_t ModuleFileID =
1358 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1359 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1360}
1361
1362std::pair<unsigned, unsigned>
1363ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1364 return readULEBKeyDataLength(P&: d);
1365}
1366
1367DeclarationNameKey
1368ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1369 using namespace llvm::support;
1370
1371 auto Kind = (DeclarationName::NameKind)*d++;
1372 uint64_t Data;
1373 switch (Kind) {
1374 case DeclarationName::Identifier:
1375 case DeclarationName::CXXLiteralOperatorName:
1376 case DeclarationName::CXXDeductionGuideName:
1377 Data = (uint64_t)Reader.getLocalIdentifier(
1378 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1379 break;
1380 case DeclarationName::ObjCZeroArgSelector:
1381 case DeclarationName::ObjCOneArgSelector:
1382 case DeclarationName::ObjCMultiArgSelector:
1383 Data = (uint64_t)Reader
1384 .getLocalSelector(
1385 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1386 .getAsOpaquePtr();
1387 break;
1388 case DeclarationName::CXXOperatorName:
1389 Data = *d++; // OverloadedOperatorKind
1390 break;
1391 case DeclarationName::CXXConstructorName:
1392 case DeclarationName::CXXDestructorName:
1393 case DeclarationName::CXXConversionFunctionName:
1394 case DeclarationName::CXXUsingDirective:
1395 Data = 0;
1396 break;
1397 }
1398
1399 return DeclarationNameKey(Kind, Data);
1400}
1401
1402ASTDeclContextNameLookupTrait::internal_key_type
1403ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1404 return ReadKeyBase(d);
1405}
1406
1407void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1408 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1409 using namespace llvm::support;
1410
1411 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1412 LocalDeclID ID = LocalDeclID::get(
1413 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1414 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1415 }
1416}
1417
1418void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1419 const unsigned char *d,
1420 unsigned DataLen,
1421 data_type_builder &Val) {
1422 ReadDataIntoImpl(d, DataLen, Val);
1423}
1424
1425ModuleLocalNameLookupTrait::hash_value_type
1426ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1427 llvm::FoldingSetNodeID ID;
1428 ID.AddInteger(I: Key.first.getHash());
1429 ID.AddInteger(I: Key.second);
1430 return ID.computeStableHash();
1431}
1432
1433ModuleLocalNameLookupTrait::internal_key_type
1434ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1435 DeclarationNameKey Name(Key.first);
1436
1437 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1438 if (!ModuleHash)
1439 return {Name, 0};
1440
1441 return {Name, *ModuleHash};
1442}
1443
1444ModuleLocalNameLookupTrait::internal_key_type
1445ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1446 DeclarationNameKey Name = ReadKeyBase(d);
1447 unsigned PrimaryModuleHash =
1448 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1449 return {Name, PrimaryModuleHash};
1450}
1451
1452void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1453 const unsigned char *d,
1454 unsigned DataLen,
1455 data_type_builder &Val) {
1456 ReadDataIntoImpl(d, DataLen, Val);
1457}
1458
1459ModuleFile *
1460LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1461 using namespace llvm::support;
1462
1463 uint32_t ModuleFileID =
1464 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1465 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1466}
1467
1468LazySpecializationInfoLookupTrait::internal_key_type
1469LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1470 using namespace llvm::support;
1471 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1472}
1473
1474std::pair<unsigned, unsigned>
1475LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1476 return readULEBKeyDataLength(P&: d);
1477}
1478
1479void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1480 const unsigned char *d,
1481 unsigned DataLen,
1482 data_type_builder &Val) {
1483 using namespace llvm::support;
1484
1485 for (unsigned NumDecls =
1486 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1487 NumDecls; --NumDecls) {
1488 LocalDeclID LocalID = LocalDeclID::get(
1489 Reader, MF&: F,
1490 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1491 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1492 }
1493}
1494
1495bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1496 BitstreamCursor &Cursor,
1497 uint64_t Offset,
1498 DeclContext *DC) {
1499 assert(Offset != 0);
1500
1501 SavedStreamPosition SavedPosition(Cursor);
1502 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1503 Error(Err: std::move(Err));
1504 return true;
1505 }
1506
1507 RecordData Record;
1508 StringRef Blob;
1509 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1510 if (!MaybeCode) {
1511 Error(Err: MaybeCode.takeError());
1512 return true;
1513 }
1514 unsigned Code = MaybeCode.get();
1515
1516 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1517 if (!MaybeRecCode) {
1518 Error(Err: MaybeRecCode.takeError());
1519 return true;
1520 }
1521 unsigned RecCode = MaybeRecCode.get();
1522 if (RecCode != DECL_CONTEXT_LEXICAL) {
1523 Error(Msg: "Expected lexical block");
1524 return true;
1525 }
1526
1527 assert(!isa<TranslationUnitDecl>(DC) &&
1528 "expected a TU_UPDATE_LEXICAL record for TU");
1529 // If we are handling a C++ class template instantiation, we can see multiple
1530 // lexical updates for the same record. It's important that we select only one
1531 // of them, so that field numbering works properly. Just pick the first one we
1532 // see.
1533 auto &Lex = LexicalDecls[DC];
1534 if (!Lex.first) {
1535 Lex = std::make_pair(
1536 x: &M, y: llvm::ArrayRef(
1537 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1538 Blob.size() / sizeof(DeclID)));
1539 }
1540 DC->setHasExternalLexicalStorage(true);
1541 return false;
1542}
1543
1544bool ASTReader::ReadVisibleDeclContextStorage(
1545 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1546 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1547 assert(Offset != 0);
1548
1549 SavedStreamPosition SavedPosition(Cursor);
1550 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1551 Error(Err: std::move(Err));
1552 return true;
1553 }
1554
1555 RecordData Record;
1556 StringRef Blob;
1557 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1558 if (!MaybeCode) {
1559 Error(Err: MaybeCode.takeError());
1560 return true;
1561 }
1562 unsigned Code = MaybeCode.get();
1563
1564 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1565 if (!MaybeRecCode) {
1566 Error(Err: MaybeRecCode.takeError());
1567 return true;
1568 }
1569 unsigned RecCode = MaybeRecCode.get();
1570 switch (VisibleKind) {
1571 case VisibleDeclContextStorageKind::GenerallyVisible:
1572 if (RecCode != DECL_CONTEXT_VISIBLE) {
1573 Error(Msg: "Expected visible lookup table block");
1574 return true;
1575 }
1576 break;
1577 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1578 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1579 Error(Msg: "Expected module local visible lookup table block");
1580 return true;
1581 }
1582 break;
1583 case VisibleDeclContextStorageKind::TULocalVisible:
1584 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1585 Error(Msg: "Expected TU local lookup table block");
1586 return true;
1587 }
1588 break;
1589 }
1590
1591 // We can't safely determine the primary context yet, so delay attaching the
1592 // lookup table until we're done with recursive deserialization.
1593 auto *Data = (const unsigned char*)Blob.data();
1594 switch (VisibleKind) {
1595 case VisibleDeclContextStorageKind::GenerallyVisible:
1596 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1597 break;
1598 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1599 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1600 break;
1601 case VisibleDeclContextStorageKind::TULocalVisible:
1602 if (M.Kind == MK_MainFile)
1603 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1604 break;
1605 }
1606 return false;
1607}
1608
1609void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1610 ModuleFile &M, bool IsPartial) {
1611 D = D->getCanonicalDecl();
1612 auto &SpecLookups =
1613 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1614 SpecLookups[D].Table.add(File: &M, Data,
1615 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1616}
1617
1618bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1619 uint64_t Offset, Decl *D, bool IsPartial) {
1620 assert(Offset != 0);
1621
1622 SavedStreamPosition SavedPosition(Cursor);
1623 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1624 Error(Err: std::move(Err));
1625 return true;
1626 }
1627
1628 RecordData Record;
1629 StringRef Blob;
1630 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1631 if (!MaybeCode) {
1632 Error(Err: MaybeCode.takeError());
1633 return true;
1634 }
1635 unsigned Code = MaybeCode.get();
1636
1637 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1638 if (!MaybeRecCode) {
1639 Error(Err: MaybeRecCode.takeError());
1640 return true;
1641 }
1642 unsigned RecCode = MaybeRecCode.get();
1643 if (RecCode != DECL_SPECIALIZATIONS &&
1644 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1645 Error(Msg: "Expected decl specs block");
1646 return true;
1647 }
1648
1649 auto *Data = (const unsigned char *)Blob.data();
1650 AddSpecializations(D, Data, M, IsPartial);
1651 return false;
1652}
1653
1654void ASTReader::Error(StringRef Msg) const {
1655 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1656 if (PP.getLangOpts().Modules &&
1657 !PP.getHeaderSearchInfo().getSpecificModuleCachePath().empty()) {
1658 Diag(DiagID: diag::note_module_cache_path)
1659 << PP.getHeaderSearchInfo().getSpecificModuleCachePath();
1660 }
1661}
1662
1663void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1664 StringRef Arg3) const {
1665 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1666}
1667
1668namespace {
1669struct AlreadyReportedDiagnosticError
1670 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1671 static char ID;
1672
1673 void log(raw_ostream &OS) const override {
1674 llvm_unreachable("reporting an already-reported diagnostic error");
1675 }
1676
1677 std::error_code convertToErrorCode() const override {
1678 return llvm::inconvertibleErrorCode();
1679 }
1680};
1681
1682char AlreadyReportedDiagnosticError::ID = 0;
1683} // namespace
1684
1685void ASTReader::Error(llvm::Error &&Err) const {
1686 handleAllErrors(
1687 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1688 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1689}
1690
1691//===----------------------------------------------------------------------===//
1692// Source Manager Deserialization
1693//===----------------------------------------------------------------------===//
1694
1695/// Read the line table in the source manager block.
1696void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1697 unsigned Idx = 0;
1698 LineTableInfo &LineTable = SourceMgr.getLineTable();
1699
1700 // Parse the file names
1701 std::map<int, int> FileIDs;
1702 FileIDs[-1] = -1; // For unspecified filenames.
1703 for (unsigned I = 0; Record[Idx]; ++I) {
1704 // Extract the file name
1705 auto Filename = ReadPath(F, Record, Idx);
1706 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1707 }
1708 ++Idx;
1709
1710 // Parse the line entries
1711 std::vector<LineEntry> Entries;
1712 while (Idx < Record.size()) {
1713 FileID FID = ReadFileID(F, Record, Idx);
1714
1715 // Extract the line entries
1716 unsigned NumEntries = Record[Idx++];
1717 assert(NumEntries && "no line entries for file ID");
1718 Entries.clear();
1719 Entries.reserve(n: NumEntries);
1720 for (unsigned I = 0; I != NumEntries; ++I) {
1721 unsigned FileOffset = Record[Idx++];
1722 unsigned LineNo = Record[Idx++];
1723 int FilenameID = FileIDs[Record[Idx++]];
1724 SrcMgr::CharacteristicKind FileKind
1725 = (SrcMgr::CharacteristicKind)Record[Idx++];
1726 unsigned IncludeOffset = Record[Idx++];
1727 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1728 FileKind, IncludeOffset));
1729 }
1730 LineTable.AddEntry(FID, Entries);
1731 }
1732}
1733
1734/// Read a source manager block
1735llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1736 using namespace SrcMgr;
1737
1738 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1739
1740 // Set the source-location entry cursor to the current position in
1741 // the stream. This cursor will be used to read the contents of the
1742 // source manager block initially, and then lazily read
1743 // source-location entries as needed.
1744 SLocEntryCursor = F.Stream;
1745
1746 // The stream itself is going to skip over the source manager block.
1747 if (llvm::Error Err = F.Stream.SkipBlock())
1748 return Err;
1749
1750 // Enter the source manager block.
1751 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1752 return Err;
1753 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1754
1755 RecordData Record;
1756 while (true) {
1757 Expected<llvm::BitstreamEntry> MaybeE =
1758 SLocEntryCursor.advanceSkippingSubblocks();
1759 if (!MaybeE)
1760 return MaybeE.takeError();
1761 llvm::BitstreamEntry E = MaybeE.get();
1762
1763 switch (E.Kind) {
1764 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1765 case llvm::BitstreamEntry::Error:
1766 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1767 Fmt: "malformed block record in AST file");
1768 case llvm::BitstreamEntry::EndBlock:
1769 return llvm::Error::success();
1770 case llvm::BitstreamEntry::Record:
1771 // The interesting case.
1772 break;
1773 }
1774
1775 // Read a record.
1776 Record.clear();
1777 StringRef Blob;
1778 Expected<unsigned> MaybeRecord =
1779 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1780 if (!MaybeRecord)
1781 return MaybeRecord.takeError();
1782 switch (MaybeRecord.get()) {
1783 default: // Default behavior: ignore.
1784 break;
1785
1786 case SM_SLOC_FILE_ENTRY:
1787 case SM_SLOC_BUFFER_ENTRY:
1788 case SM_SLOC_EXPANSION_ENTRY:
1789 // Once we hit one of the source location entries, we're done.
1790 return llvm::Error::success();
1791 }
1792 }
1793}
1794
1795llvm::Expected<SourceLocation::UIntTy>
1796ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1797 BitstreamCursor &Cursor = F->SLocEntryCursor;
1798 SavedStreamPosition SavedPosition(Cursor);
1799 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1800 F->SLocEntryOffsets[Index]))
1801 return std::move(Err);
1802
1803 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1804 if (!MaybeEntry)
1805 return MaybeEntry.takeError();
1806
1807 llvm::BitstreamEntry Entry = MaybeEntry.get();
1808 if (Entry.Kind != llvm::BitstreamEntry::Record)
1809 return llvm::createStringError(
1810 EC: std::errc::illegal_byte_sequence,
1811 Fmt: "incorrectly-formatted source location entry in AST file");
1812
1813 RecordData Record;
1814 StringRef Blob;
1815 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1816 if (!MaybeSLOC)
1817 return MaybeSLOC.takeError();
1818
1819 switch (MaybeSLOC.get()) {
1820 default:
1821 return llvm::createStringError(
1822 EC: std::errc::illegal_byte_sequence,
1823 Fmt: "incorrectly-formatted source location entry in AST file");
1824 case SM_SLOC_FILE_ENTRY:
1825 case SM_SLOC_BUFFER_ENTRY:
1826 case SM_SLOC_EXPANSION_ENTRY:
1827 return F->SLocEntryBaseOffset + Record[0];
1828 }
1829}
1830
1831int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1832 auto SLocMapI =
1833 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1834 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1835 "Corrupted global sloc offset map");
1836 ModuleFile *F = SLocMapI->second;
1837
1838 bool Invalid = false;
1839
1840 auto It = llvm::upper_bound(
1841 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1842 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1843 int ID = F->SLocEntryBaseID + LocalIndex;
1844 std::size_t Index = -ID - 2;
1845 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1846 assert(!SourceMgr.SLocEntryLoaded[Index]);
1847 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1848 if (!MaybeEntryOffset) {
1849 Error(Err: MaybeEntryOffset.takeError());
1850 Invalid = true;
1851 return true;
1852 }
1853 SourceMgr.LoadedSLocEntryTable[Index] =
1854 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1855 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1856 }
1857 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1858 });
1859
1860 if (Invalid)
1861 return 0;
1862
1863 // The iterator points to the first entry with start offset greater than the
1864 // offset of interest. The previous entry must contain the offset of interest.
1865 return F->SLocEntryBaseID + *std::prev(x: It);
1866}
1867
1868bool ASTReader::ReadSLocEntry(int ID) {
1869 if (ID == 0)
1870 return false;
1871
1872 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1873 Error(Msg: "source location entry ID out-of-range for AST file");
1874 return true;
1875 }
1876
1877 // Local helper to read the (possibly-compressed) buffer data following the
1878 // entry record.
1879 auto ReadBuffer = [this](
1880 BitstreamCursor &SLocEntryCursor,
1881 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1882 RecordData Record;
1883 StringRef Blob;
1884 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1885 if (!MaybeCode) {
1886 Error(Err: MaybeCode.takeError());
1887 return nullptr;
1888 }
1889 unsigned Code = MaybeCode.get();
1890
1891 Expected<unsigned> MaybeRecCode =
1892 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1893 if (!MaybeRecCode) {
1894 Error(Err: MaybeRecCode.takeError());
1895 return nullptr;
1896 }
1897 unsigned RecCode = MaybeRecCode.get();
1898
1899 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1900 // Inspect the first byte to differentiate zlib (\x78) and zstd
1901 // (little-endian 0xFD2FB528).
1902 const llvm::compression::Format F =
1903 Blob.size() > 0 && Blob.data()[0] == 0x78
1904 ? llvm::compression::Format::Zlib
1905 : llvm::compression::Format::Zstd;
1906 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1907 Error(Msg: Reason);
1908 return nullptr;
1909 }
1910 SmallVector<uint8_t, 0> Decompressed;
1911 if (llvm::Error E = llvm::compression::decompress(
1912 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1913 Error(Msg: "could not decompress embedded file contents: " +
1914 llvm::toString(E: std::move(E)));
1915 return nullptr;
1916 }
1917 return llvm::MemoryBuffer::getMemBufferCopy(
1918 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1919 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1920 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1921 } else {
1922 Error(Msg: "AST record has invalid code");
1923 return nullptr;
1924 }
1925 };
1926
1927 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1928 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1929 BitNo: F->SLocEntryOffsetsBase +
1930 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1931 Error(Err: std::move(Err));
1932 return true;
1933 }
1934
1935 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1936 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1937
1938 ++NumSLocEntriesRead;
1939 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1940 if (!MaybeEntry) {
1941 Error(Err: MaybeEntry.takeError());
1942 return true;
1943 }
1944 llvm::BitstreamEntry Entry = MaybeEntry.get();
1945
1946 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1947 Error(Msg: "incorrectly-formatted source location entry in AST file");
1948 return true;
1949 }
1950
1951 RecordData Record;
1952 StringRef Blob;
1953 Expected<unsigned> MaybeSLOC =
1954 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1955 if (!MaybeSLOC) {
1956 Error(Err: MaybeSLOC.takeError());
1957 return true;
1958 }
1959 switch (MaybeSLOC.get()) {
1960 default:
1961 Error(Msg: "incorrectly-formatted source location entry in AST file");
1962 return true;
1963
1964 case SM_SLOC_FILE_ENTRY: {
1965 // We will detect whether a file changed and return 'Failure' for it, but
1966 // we will also try to fail gracefully by setting up the SLocEntry.
1967 unsigned InputID = Record[4];
1968 InputFile IF = getInputFile(F&: *F, ID: InputID);
1969 OptionalFileEntryRef File = IF.getFile();
1970 bool OverriddenBuffer = IF.isOverridden();
1971
1972 // Note that we only check if a File was returned. If it was out-of-date
1973 // we have complained but we will continue creating a FileID to recover
1974 // gracefully.
1975 if (!File)
1976 return true;
1977
1978 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1979 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1980 // This is the module's main file.
1981 IncludeLoc = getImportLocation(F);
1982 }
1983 SrcMgr::CharacteristicKind
1984 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1985 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1986 LoadedOffset: BaseOffset + Record[0]);
1987 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1988 FileInfo.NumCreatedFIDs = Record[5];
1989 if (Record[3])
1990 FileInfo.setHasLineDirectives();
1991
1992 unsigned NumFileDecls = Record[7];
1993 if (NumFileDecls && ContextObj) {
1994 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1995 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1996 FileDeclIDs[FID] =
1997 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1998 }
1999
2000 const SrcMgr::ContentCache &ContentCache =
2001 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
2002 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
2003 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
2004 !ContentCache.getBufferIfLoaded()) {
2005 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
2006 if (!Buffer)
2007 return true;
2008 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
2009 }
2010
2011 break;
2012 }
2013
2014 case SM_SLOC_BUFFER_ENTRY: {
2015 const char *Name = Blob.data();
2016 unsigned Offset = Record[0];
2017 SrcMgr::CharacteristicKind
2018 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
2019 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2020 if (IncludeLoc.isInvalid() && F->isModule()) {
2021 IncludeLoc = getImportLocation(F);
2022 }
2023
2024 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
2025 if (!Buffer)
2026 return true;
2027 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
2028 LoadedOffset: BaseOffset + Offset, IncludeLoc);
2029 if (Record[3]) {
2030 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
2031 FileInfo.setHasLineDirectives();
2032 }
2033 break;
2034 }
2035
2036 case SM_SLOC_EXPANSION_ENTRY: {
2037 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
2038 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
2039 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
2040 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
2041 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
2042 LoadedOffset: BaseOffset + Record[0]);
2043 break;
2044 }
2045 }
2046
2047 return false;
2048}
2049
2050std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
2051 if (ID == 0)
2052 return std::make_pair(x: SourceLocation(), y: "");
2053
2054 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
2055 Error(Msg: "source location entry ID out-of-range for AST file");
2056 return std::make_pair(x: SourceLocation(), y: "");
2057 }
2058
2059 // Find which module file this entry lands in.
2060 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
2061 if (!M->isModule())
2062 return std::make_pair(x: SourceLocation(), y: "");
2063
2064 // FIXME: Can we map this down to a particular submodule? That would be
2065 // ideal.
2066 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
2067}
2068
2069/// Find the location where the module F is imported.
2070SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
2071 if (F->ImportLoc.isValid())
2072 return F->ImportLoc;
2073
2074 // Otherwise we have a PCH. It's considered to be "imported" at the first
2075 // location of its includer.
2076 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
2077 // Main file is the importer.
2078 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
2079 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2080 }
2081 return F->ImportedBy[0]->FirstLoc;
2082}
2083
2084/// Enter a subblock of the specified BlockID with the specified cursor. Read
2085/// the abbreviations that are at the top of the block and then leave the cursor
2086/// pointing into the block.
2087llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
2088 unsigned BlockID,
2089 uint64_t *StartOfBlockOffset) {
2090 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
2091 return Err;
2092
2093 if (StartOfBlockOffset)
2094 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
2095
2096 while (true) {
2097 uint64_t Offset = Cursor.GetCurrentBitNo();
2098 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2099 if (!MaybeCode)
2100 return MaybeCode.takeError();
2101 unsigned Code = MaybeCode.get();
2102
2103 // We expect all abbrevs to be at the start of the block.
2104 if (Code != llvm::bitc::DEFINE_ABBREV) {
2105 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
2106 return Err;
2107 return llvm::Error::success();
2108 }
2109 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
2110 return Err;
2111 }
2112}
2113
2114Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2115 unsigned &Idx) {
2116 Token Tok;
2117 Tok.startToken();
2118 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2119 Tok.setKind((tok::TokenKind)Record[Idx++]);
2120 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2121
2122 if (Tok.isAnnotation()) {
2123 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2124 switch (Tok.getKind()) {
2125 case tok::annot_pragma_loop_hint: {
2126 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2127 Info->PragmaName = ReadToken(M, Record, Idx);
2128 Info->Option = ReadToken(M, Record, Idx);
2129 unsigned NumTokens = Record[Idx++];
2130 SmallVector<Token, 4> Toks;
2131 Toks.reserve(N: NumTokens);
2132 for (unsigned I = 0; I < NumTokens; ++I)
2133 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2134 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2135 Tok.setAnnotationValue(static_cast<void *>(Info));
2136 break;
2137 }
2138 case tok::annot_pragma_pack: {
2139 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2140 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2141 auto SlotLabel = ReadString(Record, Idx);
2142 Info->SlotLabel =
2143 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2144 Info->Alignment = ReadToken(M, Record, Idx);
2145 Tok.setAnnotationValue(static_cast<void *>(Info));
2146 break;
2147 }
2148 // Some annotation tokens do not use the PtrData field.
2149 case tok::annot_pragma_openmp:
2150 case tok::annot_pragma_openmp_end:
2151 case tok::annot_pragma_unused:
2152 case tok::annot_pragma_openacc:
2153 case tok::annot_pragma_openacc_end:
2154 case tok::annot_repl_input_end:
2155 break;
2156 default:
2157 llvm_unreachable("missing deserialization code for annotation token");
2158 }
2159 } else {
2160 Tok.setLength(Record[Idx++]);
2161 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2162 Tok.setIdentifierInfo(II);
2163 }
2164 return Tok;
2165}
2166
2167MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2168 BitstreamCursor &Stream = F.MacroCursor;
2169
2170 // Keep track of where we are in the stream, then jump back there
2171 // after reading this macro.
2172 SavedStreamPosition SavedPosition(Stream);
2173
2174 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2175 // FIXME this drops errors on the floor.
2176 consumeError(Err: std::move(Err));
2177 return nullptr;
2178 }
2179 RecordData Record;
2180 SmallVector<IdentifierInfo*, 16> MacroParams;
2181 MacroInfo *Macro = nullptr;
2182 llvm::MutableArrayRef<Token> MacroTokens;
2183
2184 while (true) {
2185 // Advance to the next record, but if we get to the end of the block, don't
2186 // pop it (removing all the abbreviations from the cursor) since we want to
2187 // be able to reseek within the block and read entries.
2188 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2189 Expected<llvm::BitstreamEntry> MaybeEntry =
2190 Stream.advanceSkippingSubblocks(Flags);
2191 if (!MaybeEntry) {
2192 Error(Err: MaybeEntry.takeError());
2193 return Macro;
2194 }
2195 llvm::BitstreamEntry Entry = MaybeEntry.get();
2196
2197 switch (Entry.Kind) {
2198 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2199 case llvm::BitstreamEntry::Error:
2200 Error(Msg: "malformed block record in AST file");
2201 return Macro;
2202 case llvm::BitstreamEntry::EndBlock:
2203 return Macro;
2204 case llvm::BitstreamEntry::Record:
2205 // The interesting case.
2206 break;
2207 }
2208
2209 // Read a record.
2210 Record.clear();
2211 PreprocessorRecordTypes RecType;
2212 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2213 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2214 else {
2215 Error(Err: MaybeRecType.takeError());
2216 return Macro;
2217 }
2218 switch (RecType) {
2219 case PP_MODULE_MACRO:
2220 case PP_MACRO_DIRECTIVE_HISTORY:
2221 return Macro;
2222
2223 case PP_MACRO_OBJECT_LIKE:
2224 case PP_MACRO_FUNCTION_LIKE: {
2225 // If we already have a macro, that means that we've hit the end
2226 // of the definition of the macro we were looking for. We're
2227 // done.
2228 if (Macro)
2229 return Macro;
2230
2231 unsigned NextIndex = 1; // Skip identifier ID.
2232 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2233 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2234 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2235 MI->setIsUsed(Record[NextIndex++]);
2236 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2237 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2238 PPAllocator&: PP.getPreprocessorAllocator());
2239 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2240 // Decode function-like macro info.
2241 bool isC99VarArgs = Record[NextIndex++];
2242 bool isGNUVarArgs = Record[NextIndex++];
2243 bool hasCommaPasting = Record[NextIndex++];
2244 MacroParams.clear();
2245 unsigned NumArgs = Record[NextIndex++];
2246 for (unsigned i = 0; i != NumArgs; ++i)
2247 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2248
2249 // Install function-like macro info.
2250 MI->setIsFunctionLike();
2251 if (isC99VarArgs) MI->setIsC99Varargs();
2252 if (isGNUVarArgs) MI->setIsGNUVarargs();
2253 if (hasCommaPasting) MI->setHasCommaPasting();
2254 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2255 }
2256
2257 // Remember that we saw this macro last so that we add the tokens that
2258 // form its body to it.
2259 Macro = MI;
2260
2261 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2262 Record[NextIndex]) {
2263 // We have a macro definition. Register the association
2264 PreprocessedEntityID
2265 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2266 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
2267 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2268 PreprocessingRecord::PPEntityID PPID =
2269 PPRec.getPPEntityID(Index, /*isLoaded=*/true);
2270 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2271 Val: PPRec.getPreprocessedEntity(PPID));
2272 if (PPDef)
2273 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2274 }
2275
2276 ++NumMacrosRead;
2277 break;
2278 }
2279
2280 case PP_TOKEN: {
2281 // If we see a TOKEN before a PP_MACRO_*, then the file is
2282 // erroneous, just pretend we didn't see this.
2283 if (!Macro) break;
2284 if (MacroTokens.empty()) {
2285 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2286 return Macro;
2287 }
2288
2289 unsigned Idx = 0;
2290 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2291 MacroTokens = MacroTokens.drop_front();
2292 break;
2293 }
2294 }
2295 }
2296}
2297
2298PreprocessedEntityID
2299ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2300 PreprocessedEntityID LocalID) const {
2301 if (!M.ModuleOffsetMap.empty())
2302 ReadModuleOffsetMap(F&: M);
2303
2304 unsigned ModuleFileIndex = LocalID >> 32;
2305 LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
2306 ModuleFile *MF =
2307 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
2308 assert(MF && "malformed identifier ID encoding?");
2309
2310 if (!ModuleFileIndex) {
2311 assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);
2312 LocalID -= NUM_PREDEF_PP_ENTITY_IDS;
2313 }
2314
2315 return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;
2316}
2317
2318OptionalFileEntryRef
2319HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2320 FileManager &FileMgr = Reader.getFileManager();
2321 if (!Key.Imported)
2322 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2323
2324 auto Resolved =
2325 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2326 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2327}
2328
2329unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2330 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2331 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2332 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2333 return llvm::xxh3_64bits(data: buf);
2334}
2335
2336HeaderFileInfoTrait::internal_key_type
2337HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2338 internal_key_type ikey = {.Size: ekey.getSize(),
2339 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2340 .Filename: ekey.getName(), /*Imported*/ false};
2341 return ikey;
2342}
2343
2344bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2345 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2346 return false;
2347
2348 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2349 return true;
2350
2351 // Determine whether the actual files are equivalent.
2352 OptionalFileEntryRef FEA = getFile(Key: a);
2353 OptionalFileEntryRef FEB = getFile(Key: b);
2354 return FEA && FEA == FEB;
2355}
2356
2357std::pair<unsigned, unsigned>
2358HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2359 return readULEBKeyDataLength(P&: d);
2360}
2361
2362HeaderFileInfoTrait::internal_key_type
2363HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2364 using namespace llvm::support;
2365
2366 internal_key_type ikey;
2367 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2368 ikey.ModTime =
2369 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2370 ikey.Filename = (const char *)d;
2371 ikey.Imported = true;
2372 return ikey;
2373}
2374
2375HeaderFileInfoTrait::data_type
2376HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2377 unsigned DataLen) {
2378 using namespace llvm::support;
2379
2380 const unsigned char *End = d + DataLen;
2381 HeaderFileInfo HFI;
2382 unsigned Flags = *d++;
2383
2384 OptionalFileEntryRef FE;
2385 bool Included = (Flags >> 6) & 0x01;
2386 if (Included)
2387 if ((FE = getFile(Key: key)))
2388 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2389 // deserialize this header file info again.
2390 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2391
2392 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2393 HFI.isImport |= (Flags >> 5) & 0x01;
2394 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2395 HFI.DirInfo = (Flags >> 1) & 0x07;
2396 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2397 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2398
2399 assert((End - d) % 4 == 0 &&
2400 "Wrong data length in HeaderFileInfo deserialization");
2401 while (d != End) {
2402 uint32_t LocalSMID =
2403 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2404 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2405 LocalSMID >>= 3;
2406
2407 // This header is part of a module. Associate it with the module to enable
2408 // implicit module import.
2409 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2410 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2411 ModuleMap &ModMap =
2412 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2413
2414 if (FE || (FE = getFile(Key: key))) {
2415 // FIXME: NameAsWritten
2416 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2417 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2418 }
2419 HFI.mergeModuleMembership(Role: HeaderRole);
2420 }
2421
2422 // This HeaderFileInfo was externally loaded.
2423 HFI.External = true;
2424 HFI.IsValid = true;
2425 return HFI;
2426}
2427
2428void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2429 uint32_t MacroDirectivesOffset) {
2430 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2431 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2432}
2433
2434void ASTReader::ReadDefinedMacros() {
2435 // Note that we are loading defined macros.
2436 Deserializing Macros(this);
2437
2438 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2439 BitstreamCursor &MacroCursor = I.MacroCursor;
2440
2441 // If there was no preprocessor block, skip this file.
2442 if (MacroCursor.getBitcodeBytes().empty())
2443 continue;
2444
2445 BitstreamCursor Cursor = MacroCursor;
2446 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2447 Error(Err: std::move(Err));
2448 return;
2449 }
2450
2451 RecordData Record;
2452 while (true) {
2453 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2454 if (!MaybeE) {
2455 Error(Err: MaybeE.takeError());
2456 return;
2457 }
2458 llvm::BitstreamEntry E = MaybeE.get();
2459
2460 switch (E.Kind) {
2461 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2462 case llvm::BitstreamEntry::Error:
2463 Error(Msg: "malformed block record in AST file");
2464 return;
2465 case llvm::BitstreamEntry::EndBlock:
2466 goto NextCursor;
2467
2468 case llvm::BitstreamEntry::Record: {
2469 Record.clear();
2470 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2471 if (!MaybeRecord) {
2472 Error(Err: MaybeRecord.takeError());
2473 return;
2474 }
2475 switch (MaybeRecord.get()) {
2476 default: // Default behavior: ignore.
2477 break;
2478
2479 case PP_MACRO_OBJECT_LIKE:
2480 case PP_MACRO_FUNCTION_LIKE: {
2481 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2482 if (II->isOutOfDate())
2483 updateOutOfDateIdentifier(II: *II);
2484 break;
2485 }
2486
2487 case PP_TOKEN:
2488 // Ignore tokens.
2489 break;
2490 }
2491 break;
2492 }
2493 }
2494 }
2495 NextCursor: ;
2496 }
2497}
2498
2499namespace {
2500
2501 /// Visitor class used to look up identifirs in an AST file.
2502 class IdentifierLookupVisitor {
2503 StringRef Name;
2504 unsigned NameHash;
2505 unsigned PriorGeneration;
2506 unsigned &NumIdentifierLookups;
2507 unsigned &NumIdentifierLookupHits;
2508 IdentifierInfo *Found = nullptr;
2509
2510 public:
2511 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2512 unsigned &NumIdentifierLookups,
2513 unsigned &NumIdentifierLookupHits)
2514 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2515 PriorGeneration(PriorGeneration),
2516 NumIdentifierLookups(NumIdentifierLookups),
2517 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2518
2519 bool operator()(ModuleFile &M) {
2520 // If we've already searched this module file, skip it now.
2521 if (M.Generation <= PriorGeneration)
2522 return true;
2523
2524 ASTIdentifierLookupTable *IdTable
2525 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2526 if (!IdTable)
2527 return false;
2528
2529 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2530 Found);
2531 ++NumIdentifierLookups;
2532 ASTIdentifierLookupTable::iterator Pos =
2533 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2534 if (Pos == IdTable->end())
2535 return false;
2536
2537 // Dereferencing the iterator has the effect of building the
2538 // IdentifierInfo node and populating it with the various
2539 // declarations it needs.
2540 ++NumIdentifierLookupHits;
2541 Found = *Pos;
2542 if (Trait.hasMoreInformationInDependencies()) {
2543 // Look for the identifier in extra modules as they contain more info.
2544 return false;
2545 }
2546 return true;
2547 }
2548
2549 // Retrieve the identifier info found within the module
2550 // files.
2551 IdentifierInfo *getIdentifierInfo() const { return Found; }
2552 };
2553
2554} // namespace
2555
2556void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2557 // Note that we are loading an identifier.
2558 Deserializing AnIdentifier(this);
2559
2560 unsigned PriorGeneration = 0;
2561 if (getContext().getLangOpts().Modules)
2562 PriorGeneration = IdentifierGeneration[&II];
2563
2564 // If there is a global index, look there first to determine which modules
2565 // provably do not have any results for this identifier.
2566 GlobalModuleIndex::HitSet Hits;
2567 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2568 if (!loadGlobalIndex()) {
2569 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2570 HitsPtr = &Hits;
2571 }
2572 }
2573
2574 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2575 NumIdentifierLookups,
2576 NumIdentifierLookupHits);
2577 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2578 markIdentifierUpToDate(II: &II);
2579}
2580
2581void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2582 if (!II)
2583 return;
2584
2585 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2586
2587 // Update the generation for this identifier.
2588 if (getContext().getLangOpts().Modules)
2589 IdentifierGeneration[II] = getGeneration();
2590}
2591
2592MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,
2593 unsigned &Idx) {
2594 uint64_t ModuleFileIndex = Record[Idx++] << 32;
2595 uint64_t LocalIndex = Record[Idx++];
2596 return getGlobalMacroID(M&: F, LocalID: (ModuleFileIndex | LocalIndex));
2597}
2598
2599void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2600 const PendingMacroInfo &PMInfo) {
2601 ModuleFile &M = *PMInfo.M;
2602
2603 BitstreamCursor &Cursor = M.MacroCursor;
2604 SavedStreamPosition SavedPosition(Cursor);
2605 if (llvm::Error Err =
2606 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2607 Error(Err: std::move(Err));
2608 return;
2609 }
2610
2611 struct ModuleMacroRecord {
2612 SubmoduleID SubModID;
2613 MacroInfo *MI;
2614 SmallVector<SubmoduleID, 8> Overrides;
2615 };
2616 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2617
2618 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2619 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2620 // macro histroy.
2621 RecordData Record;
2622 while (true) {
2623 Expected<llvm::BitstreamEntry> MaybeEntry =
2624 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2625 if (!MaybeEntry) {
2626 Error(Err: MaybeEntry.takeError());
2627 return;
2628 }
2629 llvm::BitstreamEntry Entry = MaybeEntry.get();
2630
2631 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2632 Error(Msg: "malformed block record in AST file");
2633 return;
2634 }
2635
2636 Record.clear();
2637 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2638 if (!MaybePP) {
2639 Error(Err: MaybePP.takeError());
2640 return;
2641 }
2642 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2643 case PP_MACRO_DIRECTIVE_HISTORY:
2644 break;
2645
2646 case PP_MODULE_MACRO: {
2647 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2648 auto &Info = ModuleMacros.back();
2649 unsigned Idx = 0;
2650 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[Idx++]);
2651 Info.MI = getMacro(ID: ReadMacroID(F&: M, Record, Idx));
2652 for (int I = Idx, N = Record.size(); I != N; ++I)
2653 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2654 continue;
2655 }
2656
2657 default:
2658 Error(Msg: "malformed block record in AST file");
2659 return;
2660 }
2661
2662 // We found the macro directive history; that's the last record
2663 // for this macro.
2664 break;
2665 }
2666
2667 // Module macros are listed in reverse dependency order.
2668 {
2669 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2670 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2671 for (auto &MMR : ModuleMacros) {
2672 Overrides.clear();
2673 for (unsigned ModID : MMR.Overrides) {
2674 Module *Mod = getSubmodule(GlobalID: ModID);
2675 auto *Macro = PP.getModuleMacro(Mod, II);
2676 assert(Macro && "missing definition for overridden macro");
2677 Overrides.push_back(Elt: Macro);
2678 }
2679
2680 bool Inserted = false;
2681 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2682 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2683 }
2684 }
2685
2686 // Don't read the directive history for a module; we don't have anywhere
2687 // to put it.
2688 if (M.isModule())
2689 return;
2690
2691 // Deserialize the macro directives history in reverse source-order.
2692 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2693 unsigned Idx = 0, N = Record.size();
2694 while (Idx < N) {
2695 MacroDirective *MD = nullptr;
2696 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2697 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2698 switch (K) {
2699 case MacroDirective::MD_Define: {
2700 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2701 MD = PP.AllocateDefMacroDirective(MI, Loc);
2702 break;
2703 }
2704 case MacroDirective::MD_Undefine:
2705 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2706 break;
2707 case MacroDirective::MD_Visibility:
2708 bool isPublic = Record[Idx++];
2709 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2710 break;
2711 }
2712
2713 if (!Latest)
2714 Latest = MD;
2715 if (Earliest)
2716 Earliest->setPrevious(MD);
2717 Earliest = MD;
2718 }
2719
2720 if (Latest)
2721 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2722}
2723
2724bool ASTReader::shouldDisableValidationForFile(
2725 const serialization::ModuleFile &M) const {
2726 if (DisableValidationKind == DisableValidationForModuleKind::None)
2727 return false;
2728
2729 // If a PCH is loaded and validation is disabled for PCH then disable
2730 // validation for the PCH and the modules it loads.
2731 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2732
2733 switch (K) {
2734 case MK_MainFile:
2735 case MK_Preamble:
2736 case MK_PCH:
2737 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2738 case MK_ImplicitModule:
2739 case MK_ExplicitModule:
2740 case MK_PrebuiltModule:
2741 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2742 }
2743
2744 return false;
2745}
2746
2747static std::pair<StringRef, StringRef>
2748getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2749 const StringRef InputBlob) {
2750 uint16_t AsRequestedLength = Record[7];
2751 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2752 InputBlob.substr(Start: AsRequestedLength)};
2753}
2754
2755InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2756 // If this ID is bogus, just return an empty input file.
2757 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2758 return InputFileInfo();
2759
2760 // If we've already loaded this input file, return it.
2761 if (F.InputFileInfosLoaded[ID - 1].isValid())
2762 return F.InputFileInfosLoaded[ID - 1];
2763
2764 // Go find this input file.
2765 BitstreamCursor &Cursor = F.InputFilesCursor;
2766 SavedStreamPosition SavedPosition(Cursor);
2767 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2768 F.InputFileOffsets[ID - 1])) {
2769 // FIXME this drops errors on the floor.
2770 consumeError(Err: std::move(Err));
2771 }
2772
2773 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2774 if (!MaybeCode) {
2775 // FIXME this drops errors on the floor.
2776 consumeError(Err: MaybeCode.takeError());
2777 }
2778 unsigned Code = MaybeCode.get();
2779 RecordData Record;
2780 StringRef Blob;
2781
2782 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2783 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2784 "invalid record type for input file");
2785 else {
2786 // FIXME this drops errors on the floor.
2787 consumeError(Err: Maybe.takeError());
2788 }
2789
2790 assert(Record[0] == ID && "Bogus stored ID or offset");
2791 InputFileInfo R;
2792 R.StoredSize = static_cast<off_t>(Record[1]);
2793 R.StoredTime = static_cast<time_t>(Record[2]);
2794 R.Overridden = static_cast<bool>(Record[3]);
2795 R.Transient = static_cast<bool>(Record[4]);
2796 R.TopLevel = static_cast<bool>(Record[5]);
2797 R.ModuleMap = static_cast<bool>(Record[6]);
2798 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2799 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2800 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2801 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2802 ? UnresolvedFilenameAsRequested
2803 : UnresolvedFilename;
2804
2805 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2806 if (!MaybeEntry) // FIXME this drops errors on the floor.
2807 consumeError(Err: MaybeEntry.takeError());
2808 llvm::BitstreamEntry Entry = MaybeEntry.get();
2809 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2810 "expected record type for input file hash");
2811
2812 Record.clear();
2813 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2814 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2815 "invalid record type for input file hash");
2816 else {
2817 // FIXME this drops errors on the floor.
2818 consumeError(Err: Maybe.takeError());
2819 }
2820 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2821 static_cast<uint64_t>(Record[0]);
2822
2823 // Note that we've loaded this input file info.
2824 F.InputFileInfosLoaded[ID - 1] = R;
2825 return R;
2826}
2827
2828static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2829InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2830 // If this ID is bogus, just return an empty input file.
2831 if (ID == 0 || ID > F.InputFilesLoaded.size())
2832 return InputFile();
2833
2834 // If we've already loaded this input file, return it.
2835 if (F.InputFilesLoaded[ID-1].getFile())
2836 return F.InputFilesLoaded[ID-1];
2837
2838 if (F.InputFilesLoaded[ID-1].isNotFound())
2839 return InputFile();
2840
2841 // Go find this input file.
2842 BitstreamCursor &Cursor = F.InputFilesCursor;
2843 SavedStreamPosition SavedPosition(Cursor);
2844 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2845 F.InputFileOffsets[ID - 1])) {
2846 // FIXME this drops errors on the floor.
2847 consumeError(Err: std::move(Err));
2848 }
2849
2850 InputFileInfo FI = getInputFileInfo(F, ID);
2851 off_t StoredSize = FI.StoredSize;
2852 time_t StoredTime = FI.StoredTime;
2853 bool Overridden = FI.Overridden;
2854 bool Transient = FI.Transient;
2855 auto Filename =
2856 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2857 uint64_t StoredContentHash = FI.ContentHash;
2858
2859 // For standard C++ modules, we don't need to check the inputs.
2860 bool SkipChecks = F.StandardCXXModule;
2861
2862 const HeaderSearchOptions &HSOpts =
2863 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2864
2865 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2866 // modules.
2867 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2868 SkipChecks = false;
2869 Overridden = false;
2870 }
2871
2872 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2873
2874 // For an overridden file, create a virtual file with the stored
2875 // size/timestamp.
2876 if ((Overridden || Transient || SkipChecks) && !File)
2877 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2878
2879 if (!File) {
2880 if (Complain) {
2881 std::string ErrorStr = "could not find file '";
2882 ErrorStr += *Filename;
2883 ErrorStr += "' referenced by AST file '";
2884 ErrorStr += F.FileName.str();
2885 ErrorStr += "'";
2886 Error(Msg: ErrorStr);
2887 }
2888 // Record that we didn't find the file.
2889 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2890 return InputFile();
2891 }
2892
2893 // Check if there was a request to override the contents of the file
2894 // that was part of the precompiled header. Overriding such a file
2895 // can lead to problems when lexing using the source locations from the
2896 // PCH.
2897 SourceManager &SM = getSourceManager();
2898 // FIXME: Reject if the overrides are different.
2899 if ((!Overridden && !Transient) && !SkipChecks &&
2900 SM.isFileOverridden(File: *File)) {
2901 if (Complain)
2902 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2903
2904 // After emitting the diagnostic, bypass the overriding file to recover
2905 // (this creates a separate FileEntry).
2906 File = SM.bypassFileContentsOverride(File: *File);
2907 if (!File) {
2908 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2909 return InputFile();
2910 }
2911 }
2912
2913 struct Change {
2914 enum ModificationKind {
2915 Size,
2916 ModTime,
2917 Content,
2918 None,
2919 } Kind;
2920 std::optional<int64_t> Old = std::nullopt;
2921 std::optional<int64_t> New = std::nullopt;
2922 };
2923 auto HasInputContentChanged = [&](Change OriginalChange) {
2924 assert(ValidateASTInputFilesContent &&
2925 "We should only check the content of the inputs with "
2926 "ValidateASTInputFilesContent enabled.");
2927
2928 if (StoredContentHash == 0)
2929 return OriginalChange;
2930
2931 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2932 if (!MemBuffOrError) {
2933 if (!Complain)
2934 return OriginalChange;
2935 std::string ErrorStr = "could not get buffer for file '";
2936 ErrorStr += File->getName();
2937 ErrorStr += "'";
2938 Error(Msg: ErrorStr);
2939 return OriginalChange;
2940 }
2941
2942 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2943 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2944 return Change{.Kind: Change::None};
2945
2946 return Change{.Kind: Change::Content};
2947 };
2948 auto HasInputFileChanged = [&]() {
2949 if (StoredSize != File->getSize())
2950 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2951 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2952 StoredTime != File->getModificationTime()) {
2953 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2954 .New: File->getModificationTime()};
2955
2956 // In case the modification time changes but not the content,
2957 // accept the cached file as legit.
2958 if (ValidateASTInputFilesContent)
2959 return HasInputContentChanged(MTimeChange);
2960
2961 return MTimeChange;
2962 }
2963 return Change{.Kind: Change::None};
2964 };
2965
2966 bool IsOutOfDate = false;
2967 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2968 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2969 // enabled, it is better to check the contents of the inputs. Since we can't
2970 // get correct modified time information for inputs from overriden inputs.
2971 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2972 F.StandardCXXModule && FileChange.Kind == Change::None)
2973 FileChange = HasInputContentChanged(FileChange);
2974
2975 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2976 // it is better to check the content of the input files because we cannot rely
2977 // on the file modification time, which will be the same (zero) for these
2978 // files.
2979 if (!StoredTime && ValidateASTInputFilesContent &&
2980 FileChange.Kind == Change::None)
2981 FileChange = HasInputContentChanged(FileChange);
2982
2983 // For an overridden file, there is nothing to validate.
2984 if (!Overridden && FileChange.Kind != Change::None) {
2985 if (Complain) {
2986 // Build a list of the PCH imports that got us here (in reverse).
2987 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2988 while (!ImportStack.back()->ImportedBy.empty())
2989 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2990
2991 // The top-level AST file is stale.
2992 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
2993 Diag(DiagID: diag::err_fe_ast_file_modified)
2994 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
2995 << TopLevelASTFileName;
2996 Diag(DiagID: diag::note_fe_ast_file_modified)
2997 << FileChange.Kind << (FileChange.Old && FileChange.New)
2998 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
2999 << llvm::itostr(X: FileChange.New.value_or(u: 0));
3000
3001 // Print the import stack.
3002 if (ImportStack.size() > 1) {
3003 Diag(DiagID: diag::note_ast_file_required_by)
3004 << *Filename << ImportStack[0]->FileName;
3005 for (unsigned I = 1; I < ImportStack.size(); ++I)
3006 Diag(DiagID: diag::note_ast_file_required_by)
3007 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
3008 }
3009
3010 if (F.InputFilesValidationStatus == InputFilesValidation::Disabled)
3011 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
3012 Diag(DiagID: diag::note_ast_file_input_files_validation_status)
3013 << F.InputFilesValidationStatus;
3014 }
3015
3016 IsOutOfDate = true;
3017 }
3018 // FIXME: If the file is overridden and we've already opened it,
3019 // issue an error (or split it into a separate FileEntry).
3020
3021 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
3022
3023 // Note that we've loaded this input file.
3024 F.InputFilesLoaded[ID-1] = IF;
3025 return IF;
3026}
3027
3028ASTReader::TemporarilyOwnedStringRef
3029ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3030 ModuleFile &ModF) {
3031 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
3032}
3033
3034ASTReader::TemporarilyOwnedStringRef
3035ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
3036 StringRef Prefix) {
3037 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
3038
3039 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
3040 Path == "<built-in>" || Path == "<command line>")
3041 return {Path, Buf};
3042
3043 Buf.clear();
3044 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
3045 StringRef ResolvedPath{Buf.data(), Buf.size()};
3046 return {ResolvedPath, Buf};
3047}
3048
3049std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3050 StringRef P,
3051 ModuleFile &ModF) {
3052 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
3053}
3054
3055std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
3056 StringRef P,
3057 StringRef Prefix) {
3058 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
3059 return ResolvedPath->str();
3060}
3061
3062static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
3063 switch (ARR) {
3064 case ASTReader::Failure: return true;
3065 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
3066 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
3067 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
3068 case ASTReader::ConfigurationMismatch:
3069 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
3070 case ASTReader::HadErrors: return true;
3071 case ASTReader::Success: return false;
3072 }
3073
3074 llvm_unreachable("unknown ASTReadResult");
3075}
3076
3077ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
3078 BitstreamCursor &Stream, StringRef Filename,
3079 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
3080 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
3081 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
3082 // FIXME this drops errors on the floor.
3083 consumeError(Err: std::move(Err));
3084 return Failure;
3085 }
3086
3087 // Read all of the records in the options block.
3088 RecordData Record;
3089 ASTReadResult Result = Success;
3090 while (true) {
3091 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3092 if (!MaybeEntry) {
3093 // FIXME this drops errors on the floor.
3094 consumeError(Err: MaybeEntry.takeError());
3095 return Failure;
3096 }
3097 llvm::BitstreamEntry Entry = MaybeEntry.get();
3098
3099 switch (Entry.Kind) {
3100 case llvm::BitstreamEntry::Error:
3101 case llvm::BitstreamEntry::SubBlock:
3102 return Failure;
3103
3104 case llvm::BitstreamEntry::EndBlock:
3105 return Result;
3106
3107 case llvm::BitstreamEntry::Record:
3108 // The interesting case.
3109 break;
3110 }
3111
3112 // Read and process a record.
3113 Record.clear();
3114 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
3115 if (!MaybeRecordType) {
3116 // FIXME this drops errors on the floor.
3117 consumeError(Err: MaybeRecordType.takeError());
3118 return Failure;
3119 }
3120 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
3121 case LANGUAGE_OPTIONS: {
3122 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3123 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
3124 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3125 Result = ConfigurationMismatch;
3126 break;
3127 }
3128
3129 case CODEGEN_OPTIONS: {
3130 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3131 if (ParseCodeGenOptions(Record, ModuleFilename: Filename, Complain, Listener,
3132 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3133 Result = ConfigurationMismatch;
3134 break;
3135 }
3136
3137 case TARGET_OPTIONS: {
3138 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3139 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
3140 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3141 Result = ConfigurationMismatch;
3142 break;
3143 }
3144
3145 case FILE_SYSTEM_OPTIONS: {
3146 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3147 if (!AllowCompatibleConfigurationMismatch &&
3148 ParseFileSystemOptions(Record, Complain, Listener))
3149 Result = ConfigurationMismatch;
3150 break;
3151 }
3152
3153 case HEADER_SEARCH_OPTIONS: {
3154 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3155 if (!AllowCompatibleConfigurationMismatch &&
3156 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3157 Result = ConfigurationMismatch;
3158 break;
3159 }
3160
3161 case PREPROCESSOR_OPTIONS:
3162 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3163 if (!AllowCompatibleConfigurationMismatch &&
3164 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3165 SuggestedPredefines))
3166 Result = ConfigurationMismatch;
3167 break;
3168 }
3169 }
3170}
3171
3172ASTReader::RelocationResult
3173ASTReader::getModuleForRelocationChecks(ModuleFile &F, bool DirectoryCheck) {
3174 // Don't emit module relocation errors if we have -fno-validate-pch.
3175 const bool IgnoreError =
3176 bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3177 DisableValidationForModuleKind::Module);
3178
3179 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3180 return {std::nullopt, IgnoreError};
3181
3182 const bool IsImplicitModule = F.Kind == MK_ImplicitModule;
3183
3184 if (!DirectoryCheck &&
3185 (!IsImplicitModule || ModuleMgr.begin()->Kind == MK_MainFile))
3186 return {std::nullopt, IgnoreError};
3187
3188 const HeaderSearchOptions &HSOpts =
3189 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3190
3191 // When only validating modules once per build session,
3192 // Skip check if the timestamp is up to date or module was built in same build
3193 // session.
3194 if (HSOpts.ModulesValidateOncePerBuildSession && IsImplicitModule) {
3195 if (F.InputFilesValidationTimestamp >= HSOpts.BuildSessionTimestamp)
3196 return {std::nullopt, IgnoreError};
3197 if (static_cast<uint64_t>(F.ModTime) >= HSOpts.BuildSessionTimestamp)
3198 return {std::nullopt, IgnoreError};
3199 }
3200
3201 Diag(DiagID: diag::remark_module_check_relocation) << F.ModuleName << F.FileName;
3202
3203 // If we've already loaded a module map file covering this module, we may
3204 // have a better path for it (relative to the current build if doing directory
3205 // check).
3206 Module *M = PP.getHeaderSearchInfo().lookupModule(
3207 ModuleName: F.ModuleName, ImportLoc: DirectoryCheck ? SourceLocation() : F.ImportLoc,
3208 /*AllowSearch=*/DirectoryCheck,
3209 /*AllowExtraModuleMapSearch=*/DirectoryCheck);
3210
3211 return {M, IgnoreError};
3212}
3213
3214ASTReader::ASTReadResult
3215ASTReader::ReadControlBlock(ModuleFile &F,
3216 SmallVectorImpl<ImportedModule> &Loaded,
3217 const ModuleFile *ImportedBy,
3218 unsigned ClientLoadCapabilities) {
3219 BitstreamCursor &Stream = F.Stream;
3220
3221 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3222 Error(Err: std::move(Err));
3223 return Failure;
3224 }
3225
3226 // Lambda to read the unhashed control block the first time it's called.
3227 //
3228 // For PCM files, the unhashed control block cannot be read until after the
3229 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3230 // need to look ahead before reading the IMPORTS record. For consistency,
3231 // this block is always read somehow (see BitstreamEntry::EndBlock).
3232 bool HasReadUnhashedControlBlock = false;
3233 auto readUnhashedControlBlockOnce = [&]() {
3234 if (!HasReadUnhashedControlBlock) {
3235 HasReadUnhashedControlBlock = true;
3236 if (ASTReadResult Result =
3237 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3238 return Result;
3239 }
3240 return Success;
3241 };
3242
3243 bool DisableValidation = shouldDisableValidationForFile(M: F);
3244
3245 // Read all of the records and blocks in the control block.
3246 RecordData Record;
3247 unsigned NumInputs = 0;
3248 unsigned NumUserInputs = 0;
3249 StringRef BaseDirectoryAsWritten;
3250 while (true) {
3251 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3252 if (!MaybeEntry) {
3253 Error(Err: MaybeEntry.takeError());
3254 return Failure;
3255 }
3256 llvm::BitstreamEntry Entry = MaybeEntry.get();
3257
3258 switch (Entry.Kind) {
3259 case llvm::BitstreamEntry::Error:
3260 Error(Msg: "malformed block record in AST file");
3261 return Failure;
3262 case llvm::BitstreamEntry::EndBlock: {
3263 // Validate the module before returning. This call catches an AST with
3264 // no module name and no imports.
3265 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3266 return Result;
3267
3268 // Validate input files.
3269 const HeaderSearchOptions &HSOpts =
3270 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3271
3272 // All user input files reside at the index range [0, NumUserInputs), and
3273 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3274 // loaded module files, ignore missing inputs.
3275 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3276 F.Kind != MK_PrebuiltModule) {
3277 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3278
3279 // If we are reading a module, we will create a verification timestamp,
3280 // so we verify all input files. Otherwise, verify only user input
3281 // files.
3282
3283 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3284 F.InputFilesValidationStatus = ValidateSystemInputs
3285 ? InputFilesValidation::AllFiles
3286 : InputFilesValidation::UserFiles;
3287 if (HSOpts.ModulesValidateOncePerBuildSession &&
3288 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3289 F.Kind == MK_ImplicitModule) {
3290 N = ForceValidateUserInputs ? NumUserInputs : 0;
3291 F.InputFilesValidationStatus =
3292 ForceValidateUserInputs
3293 ? InputFilesValidation::UserFiles
3294 : InputFilesValidation::SkippedInBuildSession;
3295 }
3296
3297 if (N != 0)
3298 Diag(DiagID: diag::remark_module_validation)
3299 << N << F.ModuleName << F.FileName;
3300
3301 for (unsigned I = 0; I < N; ++I) {
3302 InputFile IF = getInputFile(F, ID: I+1, Complain);
3303 if (!IF.getFile() || IF.isOutOfDate())
3304 return OutOfDate;
3305 }
3306 } else {
3307 F.InputFilesValidationStatus = InputFilesValidation::Disabled;
3308 }
3309
3310 if (Listener)
3311 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
3312
3313 if (Listener && Listener->needsInputFileVisitation()) {
3314 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3315 : NumUserInputs;
3316 for (unsigned I = 0; I < N; ++I) {
3317 bool IsSystem = I >= NumUserInputs;
3318 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3319 auto FilenameAsRequested = ResolveImportedPath(
3320 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3321 Listener->visitInputFile(
3322 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3323 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3324 }
3325 }
3326
3327 return Success;
3328 }
3329
3330 case llvm::BitstreamEntry::SubBlock:
3331 switch (Entry.ID) {
3332 case INPUT_FILES_BLOCK_ID:
3333 F.InputFilesCursor = Stream;
3334 if (llvm::Error Err = Stream.SkipBlock()) {
3335 Error(Err: std::move(Err));
3336 return Failure;
3337 }
3338 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3339 Error(Msg: "malformed block record in AST file");
3340 return Failure;
3341 }
3342 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3343 continue;
3344
3345 case OPTIONS_BLOCK_ID:
3346 // If we're reading the first module for this group, check its options
3347 // are compatible with ours. For modules it imports, no further checking
3348 // is required, because we checked them when we built it.
3349 if (Listener && !ImportedBy) {
3350 // Should we allow the configuration of the module file to differ from
3351 // the configuration of the current translation unit in a compatible
3352 // way?
3353 //
3354 // FIXME: Allow this for files explicitly specified with -include-pch.
3355 bool AllowCompatibleConfigurationMismatch =
3356 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3357
3358 ASTReadResult Result =
3359 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3360 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3361 SuggestedPredefines);
3362 if (Result == Failure) {
3363 Error(Msg: "malformed block record in AST file");
3364 return Result;
3365 }
3366
3367 if (DisableValidation ||
3368 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3369 Result = Success;
3370
3371 // If we can't load the module, exit early since we likely
3372 // will rebuild the module anyway. The stream may be in the
3373 // middle of a block.
3374 if (Result != Success)
3375 return Result;
3376 } else if (llvm::Error Err = Stream.SkipBlock()) {
3377 Error(Err: std::move(Err));
3378 return Failure;
3379 }
3380 continue;
3381
3382 default:
3383 if (llvm::Error Err = Stream.SkipBlock()) {
3384 Error(Err: std::move(Err));
3385 return Failure;
3386 }
3387 continue;
3388 }
3389
3390 case llvm::BitstreamEntry::Record:
3391 // The interesting case.
3392 break;
3393 }
3394
3395 // Read and process a record.
3396 Record.clear();
3397 StringRef Blob;
3398 Expected<unsigned> MaybeRecordType =
3399 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3400 if (!MaybeRecordType) {
3401 Error(Err: MaybeRecordType.takeError());
3402 return Failure;
3403 }
3404 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3405 case METADATA: {
3406 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3407 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3408 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3409 : diag::err_ast_file_version_too_new)
3410 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3411 return VersionMismatch;
3412 }
3413
3414 bool hasErrors = Record[7];
3415 if (hasErrors && !DisableValidation) {
3416 // If requested by the caller and the module hasn't already been read
3417 // or compiled, mark modules on error as out-of-date.
3418 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3419 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3420 return OutOfDate;
3421
3422 if (!AllowASTWithCompilerErrors) {
3423 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3424 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3425 return HadErrors;
3426 }
3427 }
3428 if (hasErrors) {
3429 Diags.ErrorOccurred = true;
3430 Diags.UncompilableErrorOccurred = true;
3431 Diags.UnrecoverableErrorOccurred = true;
3432 }
3433
3434 F.RelocatablePCH = Record[4];
3435 // Relative paths in a relocatable PCH are relative to our sysroot.
3436 if (F.RelocatablePCH)
3437 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3438
3439 F.StandardCXXModule = Record[5];
3440
3441 F.HasTimestamps = Record[6];
3442
3443 const std::string &CurBranch = getClangFullRepositoryVersion();
3444 StringRef ASTBranch = Blob;
3445 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3446 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3447 Diag(DiagID: diag::err_ast_file_different_branch)
3448 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3449 << CurBranch;
3450 return VersionMismatch;
3451 }
3452 break;
3453 }
3454
3455 case IMPORT: {
3456 // Validate the AST before processing any imports (otherwise, untangling
3457 // them can be error-prone and expensive). A module will have a name and
3458 // will already have been validated, but this catches the PCH case.
3459 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3460 return Result;
3461
3462 unsigned Idx = 0;
3463 // Read information about the AST file.
3464 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3465
3466 // The import location will be the local one for now; we will adjust
3467 // all import locations of module imports after the global source
3468 // location info are setup, in ReadAST.
3469 auto [ImportLoc, ImportModuleFileIndex] =
3470 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3471 // The import location must belong to the current module file itself.
3472 assert(ImportModuleFileIndex == 0);
3473
3474 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3475
3476 bool IsImportingStdCXXModule = Record[Idx++];
3477
3478 off_t StoredSize = 0;
3479 time_t StoredModTime = 0;
3480 unsigned ImplicitModuleSuffixLength = 0;
3481 ASTFileSignature StoredSignature;
3482 ModuleFileName ImportedFile;
3483 std::string StoredFile;
3484 bool IgnoreImportedByNote = false;
3485
3486 // For prebuilt and explicit modules first consult the file map for
3487 // an override. Note that here we don't search prebuilt module
3488 // directories if we're not importing standard c++ module, only the
3489 // explicit name to file mappings. Also, we will still verify the
3490 // size/signature making sure it is essentially the same file but
3491 // perhaps in a different location.
3492 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3493 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3494 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3495
3496 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3497 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3498 return Missing;
3499 }
3500
3501 if (!IsImportingStdCXXModule) {
3502 StoredSize = (off_t)Record[Idx++];
3503 StoredModTime = (time_t)Record[Idx++];
3504 ImplicitModuleSuffixLength = (unsigned)Record[Idx++];
3505
3506 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3507 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3508 Last: SignatureBytes.end());
3509 Blob = Blob.substr(Start: ASTFileSignature::size);
3510
3511 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3512 if (ImportedFile.empty()) {
3513 ImportedFile = ImplicitModuleSuffixLength
3514 ? ModuleFileName::makeImplicit(
3515 Name: StoredFile, SuffixLength: ImplicitModuleSuffixLength)
3516 : ModuleFileName::makeExplicit(Name: StoredFile);
3517 assert((ImportedKind == MK_ImplicitModule) ==
3518 (ImplicitModuleSuffixLength != 0));
3519 } else if (!getDiags().isIgnored(
3520 DiagID: diag::warn_module_file_mapping_mismatch,
3521 Loc: CurrentImportLoc)) {
3522 auto ImportedFileRef =
3523 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3524 auto StoredFileRef =
3525 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3526 if ((ImportedFileRef && StoredFileRef) &&
3527 (*ImportedFileRef != *StoredFileRef)) {
3528 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3529 << ImportedFile << StoredFile;
3530 Diag(DiagID: diag::note_module_file_imported_by)
3531 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3532 IgnoreImportedByNote = true;
3533 }
3534 }
3535 }
3536
3537 // If our client can't cope with us being out of date, we can't cope with
3538 // our dependency being missing.
3539 unsigned Capabilities = ClientLoadCapabilities;
3540 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3541 Capabilities &= ~ARR_Missing;
3542
3543 // Load the AST file.
3544 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3545 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3546 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3547
3548 // Check the AST we just read from ImportedFile contains a different
3549 // module than we expected (ImportedName). This can occur for C++20
3550 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3551 if (IsImportingStdCXXModule) {
3552 if (const auto *Imported =
3553 getModuleManager().lookupByFileName(FileName: ImportedFile);
3554 Imported != nullptr && Imported->ModuleName != ImportedName) {
3555 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3556 Result = Missing;
3557 }
3558 }
3559
3560 // If we diagnosed a problem, produce a backtrace.
3561 bool recompilingFinalized = Result == OutOfDate &&
3562 (Capabilities & ARR_OutOfDate) &&
3563 getModuleManager()
3564 .getModuleCache()
3565 .getInMemoryModuleCache()
3566 .isPCMFinal(Filename: F.FileName);
3567 if (!IgnoreImportedByNote &&
3568 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3569 Diag(DiagID: diag::note_module_file_imported_by)
3570 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3571
3572 switch (Result) {
3573 case Failure: return Failure;
3574 // If we have to ignore the dependency, we'll have to ignore this too.
3575 case Missing:
3576 case OutOfDate: return OutOfDate;
3577 case VersionMismatch: return VersionMismatch;
3578 case ConfigurationMismatch: return ConfigurationMismatch;
3579 case HadErrors: return HadErrors;
3580 case Success: break;
3581 }
3582 break;
3583 }
3584
3585 case ORIGINAL_FILE:
3586 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3587 F.ActualOriginalSourceFileName = std::string(Blob);
3588 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3589 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3590 break;
3591
3592 case ORIGINAL_FILE_ID:
3593 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3594 break;
3595
3596 case MODULE_NAME:
3597 F.ModuleName = std::string(Blob);
3598 Diag(DiagID: diag::remark_module_import)
3599 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3600 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3601 if (Listener)
3602 Listener->ReadModuleName(ModuleName: F.ModuleName);
3603
3604 // Validate the AST as soon as we have a name so we can exit early on
3605 // failure.
3606 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3607 return Result;
3608
3609 break;
3610
3611 case MODULE_DIRECTORY: {
3612 // Save the BaseDirectory as written in the PCM for computing the module
3613 // filename for the ModuleCache.
3614 BaseDirectoryAsWritten = Blob;
3615 assert(!F.ModuleName.empty() &&
3616 "MODULE_DIRECTORY found before MODULE_NAME");
3617 F.BaseDirectory = std::string(Blob);
3618
3619 auto [MaybeM, IgnoreError] =
3620 getModuleForRelocationChecks(F, /*DirectoryCheck=*/true);
3621 if (!MaybeM.has_value())
3622 break;
3623
3624 Module *M = MaybeM.value();
3625 if (!M || !M->Directory)
3626 break;
3627 if (IgnoreError) {
3628 F.BaseDirectory = std::string(M->Directory->getName());
3629 break;
3630 }
3631 if ((F.Kind == MK_ExplicitModule) || (F.Kind == MK_PrebuiltModule))
3632 break;
3633
3634 // If we're implicitly loading a module, the base directory can't
3635 // change between the build and use.
3636 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3637 if (BuildDir && (*BuildDir == M->Directory)) {
3638 F.BaseDirectory = std::string(M->Directory->getName());
3639 break;
3640 }
3641 Diag(DiagID: diag::remark_module_relocated)
3642 << F.ModuleName << Blob << M->Directory->getName();
3643
3644 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3645 Diag(DiagID: diag::err_imported_module_relocated)
3646 << F.ModuleName << Blob << M->Directory->getName();
3647 return OutOfDate;
3648 }
3649
3650 case MODULE_MAP_FILE:
3651 if (ASTReadResult Result =
3652 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3653 return Result;
3654 break;
3655
3656 case INPUT_FILE_OFFSETS:
3657 NumInputs = Record[0];
3658 NumUserInputs = Record[1];
3659 F.InputFileOffsets =
3660 (const llvm::support::unaligned_uint64_t *)Blob.data();
3661 F.InputFilesLoaded.resize(new_size: NumInputs);
3662 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3663 F.NumUserInputFiles = NumUserInputs;
3664 break;
3665 }
3666 }
3667}
3668
3669llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3670 unsigned ClientLoadCapabilities) {
3671 BitstreamCursor &Stream = F.Stream;
3672
3673 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3674 return Err;
3675 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3676
3677 // Read all of the records and blocks for the AST file.
3678 RecordData Record;
3679 while (true) {
3680 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3681 if (!MaybeEntry)
3682 return MaybeEntry.takeError();
3683 llvm::BitstreamEntry Entry = MaybeEntry.get();
3684
3685 switch (Entry.Kind) {
3686 case llvm::BitstreamEntry::Error:
3687 return llvm::createStringError(
3688 EC: std::errc::illegal_byte_sequence,
3689 Fmt: "error at end of module block in AST file");
3690 case llvm::BitstreamEntry::EndBlock:
3691 // Outside of C++, we do not store a lookup map for the translation unit.
3692 // Instead, mark it as needing a lookup map to be built if this module
3693 // contains any declarations lexically within it (which it always does!).
3694 // This usually has no cost, since we very rarely need the lookup map for
3695 // the translation unit outside C++.
3696 if (ASTContext *Ctx = ContextObj) {
3697 DeclContext *DC = Ctx->getTranslationUnitDecl();
3698 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3699 DC->setMustBuildLookupTable();
3700 }
3701
3702 return llvm::Error::success();
3703 case llvm::BitstreamEntry::SubBlock:
3704 switch (Entry.ID) {
3705 case DECLTYPES_BLOCK_ID:
3706 // We lazily load the decls block, but we want to set up the
3707 // DeclsCursor cursor to point into it. Clone our current bitcode
3708 // cursor to it, enter the block and read the abbrevs in that block.
3709 // With the main cursor, we just skip over it.
3710 F.DeclsCursor = Stream;
3711 if (llvm::Error Err = Stream.SkipBlock())
3712 return Err;
3713 if (llvm::Error Err = ReadBlockAbbrevs(
3714 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3715 return Err;
3716 break;
3717
3718 case PREPROCESSOR_BLOCK_ID:
3719 F.MacroCursor = Stream;
3720 if (!PP.getExternalSource())
3721 PP.setExternalSource(this);
3722
3723 if (llvm::Error Err = Stream.SkipBlock())
3724 return Err;
3725 if (llvm::Error Err =
3726 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3727 return Err;
3728 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3729 break;
3730
3731 case PREPROCESSOR_DETAIL_BLOCK_ID:
3732 F.PreprocessorDetailCursor = Stream;
3733
3734 if (llvm::Error Err = Stream.SkipBlock()) {
3735 return Err;
3736 }
3737 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3738 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3739 return Err;
3740 F.PreprocessorDetailStartOffset
3741 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3742
3743 if (!PP.getPreprocessingRecord())
3744 PP.createPreprocessingRecord();
3745 if (!PP.getPreprocessingRecord()->getExternalSource())
3746 PP.getPreprocessingRecord()->SetExternalSource(*this);
3747 break;
3748
3749 case SOURCE_MANAGER_BLOCK_ID:
3750 if (llvm::Error Err = ReadSourceManagerBlock(F))
3751 return Err;
3752 break;
3753
3754 case SUBMODULE_BLOCK_ID:
3755 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3756 return Err;
3757 break;
3758
3759 case COMMENTS_BLOCK_ID: {
3760 BitstreamCursor C = Stream;
3761
3762 if (llvm::Error Err = Stream.SkipBlock())
3763 return Err;
3764 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3765 return Err;
3766 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3767 break;
3768 }
3769
3770 default:
3771 if (llvm::Error Err = Stream.SkipBlock())
3772 return Err;
3773 break;
3774 }
3775 continue;
3776
3777 case llvm::BitstreamEntry::Record:
3778 // The interesting case.
3779 break;
3780 }
3781
3782 // Read and process a record.
3783 Record.clear();
3784 StringRef Blob;
3785 Expected<unsigned> MaybeRecordType =
3786 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3787 if (!MaybeRecordType)
3788 return MaybeRecordType.takeError();
3789 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3790
3791 // If we're not loading an AST context, we don't care about most records.
3792 if (!ContextObj) {
3793 switch (RecordType) {
3794 case IDENTIFIER_TABLE:
3795 case IDENTIFIER_OFFSET:
3796 case INTERESTING_IDENTIFIERS:
3797 case STATISTICS:
3798 case PP_ASSUME_NONNULL_LOC:
3799 case PP_CONDITIONAL_STACK:
3800 case PP_COUNTER_VALUE:
3801 case SOURCE_LOCATION_OFFSETS:
3802 case MODULE_OFFSET_MAP:
3803 case SOURCE_MANAGER_LINE_TABLE:
3804 case PPD_ENTITIES_OFFSETS:
3805 case HEADER_SEARCH_TABLE:
3806 case IMPORTED_MODULES:
3807 case MACRO_OFFSET:
3808 break;
3809 default:
3810 continue;
3811 }
3812 }
3813
3814 switch (RecordType) {
3815 default: // Default behavior: ignore.
3816 break;
3817
3818 case TYPE_OFFSET: {
3819 if (F.LocalNumTypes != 0)
3820 return llvm::createStringError(
3821 EC: std::errc::illegal_byte_sequence,
3822 Fmt: "duplicate TYPE_OFFSET record in AST file");
3823 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3824 F.LocalNumTypes = Record[0];
3825 F.BaseTypeIndex = getTotalNumTypes();
3826
3827 if (F.LocalNumTypes > 0)
3828 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3829
3830 break;
3831 }
3832
3833 case DECL_OFFSET: {
3834 if (F.LocalNumDecls != 0)
3835 return llvm::createStringError(
3836 EC: std::errc::illegal_byte_sequence,
3837 Fmt: "duplicate DECL_OFFSET record in AST file");
3838 F.DeclOffsets = (const DeclOffset *)Blob.data();
3839 F.LocalNumDecls = Record[0];
3840 F.BaseDeclIndex = getTotalNumDecls();
3841
3842 if (F.LocalNumDecls > 0)
3843 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3844
3845 break;
3846 }
3847
3848 case TU_UPDATE_LEXICAL: {
3849 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3850 LexicalContents Contents(
3851 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3852 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3853 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3854 TU->setHasExternalLexicalStorage(true);
3855 break;
3856 }
3857
3858 case UPDATE_VISIBLE: {
3859 unsigned Idx = 0;
3860 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3861 auto *Data = (const unsigned char*)Blob.data();
3862 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3863 // If we've already loaded the decl, perform the updates when we finish
3864 // loading this block.
3865 if (Decl *D = GetExistingDecl(ID))
3866 PendingUpdateRecords.push_back(
3867 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3868 break;
3869 }
3870
3871 case UPDATE_MODULE_LOCAL_VISIBLE: {
3872 unsigned Idx = 0;
3873 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3874 auto *Data = (const unsigned char *)Blob.data();
3875 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3876 // If we've already loaded the decl, perform the updates when we finish
3877 // loading this block.
3878 if (Decl *D = GetExistingDecl(ID))
3879 PendingUpdateRecords.push_back(
3880 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3881 break;
3882 }
3883
3884 case UPDATE_TU_LOCAL_VISIBLE: {
3885 if (F.Kind != MK_MainFile)
3886 break;
3887 unsigned Idx = 0;
3888 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3889 auto *Data = (const unsigned char *)Blob.data();
3890 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3891 // If we've already loaded the decl, perform the updates when we finish
3892 // loading this block.
3893 if (Decl *D = GetExistingDecl(ID))
3894 PendingUpdateRecords.push_back(
3895 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3896 break;
3897 }
3898
3899 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3900 unsigned Idx = 0;
3901 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3902 auto *Data = (const unsigned char *)Blob.data();
3903 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3904 // If we've already loaded the decl, perform the updates when we finish
3905 // loading this block.
3906 if (Decl *D = GetExistingDecl(ID))
3907 PendingUpdateRecords.push_back(
3908 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3909 break;
3910 }
3911
3912 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3913 unsigned Idx = 0;
3914 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3915 auto *Data = (const unsigned char *)Blob.data();
3916 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3917 // If we've already loaded the decl, perform the updates when we finish
3918 // loading this block.
3919 if (Decl *D = GetExistingDecl(ID))
3920 PendingUpdateRecords.push_back(
3921 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3922 break;
3923 }
3924
3925 case IDENTIFIER_TABLE:
3926 F.IdentifierTableData =
3927 reinterpret_cast<const unsigned char *>(Blob.data());
3928 if (Record[0]) {
3929 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3930 Buckets: F.IdentifierTableData + Record[0],
3931 Payload: F.IdentifierTableData + sizeof(uint32_t),
3932 Base: F.IdentifierTableData,
3933 InfoObj: ASTIdentifierLookupTrait(*this, F));
3934
3935 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3936 }
3937 break;
3938
3939 case IDENTIFIER_OFFSET: {
3940 if (F.LocalNumIdentifiers != 0)
3941 return llvm::createStringError(
3942 EC: std::errc::illegal_byte_sequence,
3943 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3944 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3945 F.LocalNumIdentifiers = Record[0];
3946 F.BaseIdentifierID = getTotalNumIdentifiers();
3947
3948 if (F.LocalNumIdentifiers > 0)
3949 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3950 + F.LocalNumIdentifiers);
3951 break;
3952 }
3953
3954 case INTERESTING_IDENTIFIERS:
3955 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3956 break;
3957
3958 case EAGERLY_DESERIALIZED_DECLS:
3959 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3960 // about "interesting" decls (for instance, if we're building a module).
3961 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3962 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3963 break;
3964
3965 case MODULAR_CODEGEN_DECLS:
3966 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3967 // them (ie: if we're not codegenerating this module).
3968 if (F.Kind == MK_MainFile ||
3969 getContext().getLangOpts().BuildingPCHWithObjectFile)
3970 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3971 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3972 break;
3973
3974 case SPECIAL_TYPES:
3975 if (SpecialTypes.empty()) {
3976 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3977 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3978 break;
3979 }
3980
3981 if (Record.empty())
3982 break;
3983
3984 if (SpecialTypes.size() != Record.size())
3985 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3986 Fmt: "invalid special-types record");
3987
3988 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3989 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3990 if (!SpecialTypes[I])
3991 SpecialTypes[I] = ID;
3992 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3993 // merge step?
3994 }
3995 break;
3996
3997 case STATISTICS:
3998 TotalNumStatements += Record[0];
3999 TotalNumMacros += Record[1];
4000 TotalLexicalDeclContexts += Record[2];
4001 TotalVisibleDeclContexts += Record[3];
4002 TotalModuleLocalVisibleDeclContexts += Record[4];
4003 TotalTULocalVisibleDeclContexts += Record[5];
4004 break;
4005
4006 case UNUSED_FILESCOPED_DECLS:
4007 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4008 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4009 break;
4010
4011 case DELEGATING_CTORS:
4012 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4013 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4014 break;
4015
4016 case WEAK_UNDECLARED_IDENTIFIERS:
4017 if (Record.size() % 3 != 0)
4018 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4019 Fmt: "invalid weak identifiers record");
4020
4021 // FIXME: Ignore weak undeclared identifiers from non-original PCH
4022 // files. This isn't the way to do it :)
4023 WeakUndeclaredIdentifiers.clear();
4024
4025 // Translate the weak, undeclared identifiers into global IDs.
4026 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4027 WeakUndeclaredIdentifiers.push_back(
4028 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4029 WeakUndeclaredIdentifiers.push_back(
4030 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4031 WeakUndeclaredIdentifiers.push_back(
4032 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4033 }
4034 break;
4035
4036 case EXTNAME_UNDECLARED_IDENTIFIERS:
4037 if (Record.size() % 3 != 0)
4038 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4039 Fmt: "invalid extname identifiers record");
4040
4041 // FIXME: Ignore #pragma redefine_extname'd, undeclared identifiers from
4042 // non-original PCH files. This isn't the way to do it :)
4043 ExtnameUndeclaredIdentifiers.clear();
4044
4045 // Translate the #pragma redefine_extname'd, undeclared identifiers into
4046 // global IDs.
4047 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
4048 ExtnameUndeclaredIdentifiers.push_back(
4049 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4050 ExtnameUndeclaredIdentifiers.push_back(
4051 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
4052 ExtnameUndeclaredIdentifiers.push_back(
4053 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4054 }
4055 break;
4056
4057 case SELECTOR_OFFSETS: {
4058 F.SelectorOffsets = (const uint32_t *)Blob.data();
4059 F.LocalNumSelectors = Record[0];
4060 unsigned LocalBaseSelectorID = Record[1];
4061 F.BaseSelectorID = getTotalNumSelectors();
4062
4063 if (F.LocalNumSelectors > 0) {
4064 // Introduce the global -> local mapping for selectors within this
4065 // module.
4066 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
4067
4068 // Introduce the local -> global mapping for selectors within this
4069 // module.
4070 F.SelectorRemap.insertOrReplace(
4071 Val: std::make_pair(x&: LocalBaseSelectorID,
4072 y: F.BaseSelectorID - LocalBaseSelectorID));
4073
4074 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
4075 }
4076 break;
4077 }
4078
4079 case METHOD_POOL:
4080 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
4081 if (Record[0])
4082 F.SelectorLookupTable
4083 = ASTSelectorLookupTable::Create(
4084 Buckets: F.SelectorLookupTableData + Record[0],
4085 Base: F.SelectorLookupTableData,
4086 InfoObj: ASTSelectorLookupTrait(*this, F));
4087 TotalNumMethodPoolEntries += Record[1];
4088 break;
4089
4090 case REFERENCED_SELECTOR_POOL:
4091 if (!Record.empty()) {
4092 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
4093 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
4094 LocalID: Record[Idx++]));
4095 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
4096 getRawEncoding());
4097 }
4098 }
4099 break;
4100
4101 case PP_ASSUME_NONNULL_LOC: {
4102 unsigned Idx = 0;
4103 if (!Record.empty())
4104 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
4105 ReadSourceLocation(ModuleFile&: F, Record, Idx));
4106 break;
4107 }
4108
4109 case PP_UNSAFE_BUFFER_USAGE: {
4110 if (!Record.empty()) {
4111 SmallVector<SourceLocation, 64> SrcLocs;
4112 unsigned Idx = 0;
4113 while (Idx < Record.size())
4114 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
4115 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
4116 }
4117 break;
4118 }
4119
4120 case PP_CONDITIONAL_STACK:
4121 if (!Record.empty()) {
4122 unsigned Idx = 0, End = Record.size() - 1;
4123 bool ReachedEOFWhileSkipping = Record[Idx++];
4124 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
4125 if (ReachedEOFWhileSkipping) {
4126 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4127 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4128 bool FoundNonSkipPortion = Record[Idx++];
4129 bool FoundElse = Record[Idx++];
4130 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4131 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
4132 args&: FoundElse, args&: ElseLoc);
4133 }
4134 SmallVector<PPConditionalInfo, 4> ConditionalStack;
4135 while (Idx < End) {
4136 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
4137 bool WasSkipping = Record[Idx++];
4138 bool FoundNonSkip = Record[Idx++];
4139 bool FoundElse = Record[Idx++];
4140 ConditionalStack.push_back(
4141 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
4142 }
4143 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
4144 }
4145 break;
4146
4147 case PP_COUNTER_VALUE:
4148 if (!Record.empty() && Listener)
4149 Listener->ReadCounter(M: F, Value: Record[0]);
4150 break;
4151
4152 case FILE_SORTED_DECLS:
4153 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
4154 F.NumFileSortedDecls = Record[0];
4155 break;
4156
4157 case SOURCE_LOCATION_OFFSETS: {
4158 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
4159 F.LocalNumSLocEntries = Record[0];
4160 SourceLocation::UIntTy SLocSpaceSize = Record[1];
4161 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
4162 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
4163 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
4164 TotalSize: SLocSpaceSize);
4165 if (!F.SLocEntryBaseID) {
4166 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
4167 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
4168 return llvm::createStringError(EC: std::errc::invalid_argument,
4169 Fmt: "ran out of source locations");
4170 }
4171 // Make our entry in the range map. BaseID is negative and growing, so
4172 // we invert it. Because we invert it, though, we need the other end of
4173 // the range.
4174 unsigned RangeStart =
4175 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
4176 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
4177 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
4178
4179 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
4180 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
4181 GlobalSLocOffsetMap.insert(
4182 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
4183 - SLocSpaceSize,y: &F));
4184
4185 TotalNumSLocEntries += F.LocalNumSLocEntries;
4186 break;
4187 }
4188
4189 case MODULE_OFFSET_MAP:
4190 F.ModuleOffsetMap = Blob;
4191 break;
4192
4193 case SOURCE_MANAGER_LINE_TABLE:
4194 ParseLineTable(F, Record);
4195 break;
4196
4197 case EXT_VECTOR_DECLS:
4198 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4199 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4200 break;
4201
4202 case VTABLE_USES:
4203 if (Record.size() % 3 != 0)
4204 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4205 Fmt: "Invalid VTABLE_USES record");
4206
4207 // Later tables overwrite earlier ones.
4208 // FIXME: Modules will have some trouble with this. This is clearly not
4209 // the right way to do this.
4210 VTableUses.clear();
4211
4212 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
4213 VTableUses.push_back(
4214 Elt: {.ID: ReadDeclID(F, Record, Idx),
4215 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
4216 .Used: (bool)Record[Idx++]});
4217 }
4218 break;
4219
4220 case PENDING_IMPLICIT_INSTANTIATIONS:
4221
4222 if (Record.size() % 2 != 0)
4223 return llvm::createStringError(
4224 EC: std::errc::illegal_byte_sequence,
4225 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4226
4227 // For standard C++20 module, we will only reads the instantiations
4228 // if it is the main file.
4229 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
4230 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4231 PendingInstantiations.push_back(
4232 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4233 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4234 }
4235 }
4236 break;
4237
4238 case SEMA_DECL_REFS:
4239 if (Record.size() != 3)
4240 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4241 Fmt: "Invalid SEMA_DECL_REFS block");
4242 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4243 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4244 break;
4245
4246 case PPD_ENTITIES_OFFSETS: {
4247 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4248 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4249 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4250
4251 unsigned StartingID;
4252 if (!PP.getPreprocessingRecord())
4253 PP.createPreprocessingRecord();
4254 if (!PP.getPreprocessingRecord()->getExternalSource())
4255 PP.getPreprocessingRecord()->SetExternalSource(*this);
4256 StartingID
4257 = PP.getPreprocessingRecord()
4258 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4259 F.BasePreprocessedEntityID = StartingID;
4260
4261 if (F.NumPreprocessedEntities > 0) {
4262 // Introduce the global -> local mapping for preprocessed entities in
4263 // this module.
4264 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4265 }
4266
4267 break;
4268 }
4269
4270 case PPD_SKIPPED_RANGES: {
4271 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4272 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4273 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4274
4275 if (!PP.getPreprocessingRecord())
4276 PP.createPreprocessingRecord();
4277 if (!PP.getPreprocessingRecord()->getExternalSource())
4278 PP.getPreprocessingRecord()->SetExternalSource(*this);
4279 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4280 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4281
4282 if (F.NumPreprocessedSkippedRanges > 0)
4283 GlobalSkippedRangeMap.insert(
4284 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4285 break;
4286 }
4287
4288 case DECL_UPDATE_OFFSETS:
4289 if (Record.size() % 2 != 0)
4290 return llvm::createStringError(
4291 EC: std::errc::illegal_byte_sequence,
4292 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4293 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4294 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4295 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4296
4297 // If we've already loaded the decl, perform the updates when we finish
4298 // loading this block.
4299 if (Decl *D = GetExistingDecl(ID))
4300 PendingUpdateRecords.push_back(
4301 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4302 }
4303 break;
4304
4305 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4306 if (Record.size() % 5 != 0)
4307 return llvm::createStringError(
4308 EC: std::errc::illegal_byte_sequence,
4309 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4310 "file");
4311 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4312 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4313
4314 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4315 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4316 uint64_t LocalLexicalOffset = Record[I++];
4317 uint64_t LexicalOffset =
4318 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4319 uint64_t LocalVisibleOffset = Record[I++];
4320 uint64_t VisibleOffset =
4321 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4322 uint64_t LocalModuleLocalOffset = Record[I++];
4323 uint64_t ModuleLocalOffset =
4324 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4325 uint64_t TULocalLocalOffset = Record[I++];
4326 uint64_t TULocalOffset =
4327 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4328
4329 DelayedNamespaceOffsetMap[ID] = {
4330 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: ModuleLocalOffset, .TULocalOffset: TULocalOffset}, .LexicalOffset: LexicalOffset};
4331
4332 assert(!GetExistingDecl(ID) &&
4333 "We shouldn't load the namespace in the front of delayed "
4334 "namespace lexical and visible block");
4335 }
4336 break;
4337 }
4338
4339 case RELATED_DECLS_MAP:
4340 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4341 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4342 auto &RelatedDecls = RelatedDeclsMap[ID];
4343 unsigned NN = Record[I++];
4344 RelatedDecls.reserve(N: NN);
4345 for (unsigned II = 0; II < NN; II++)
4346 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4347 }
4348 break;
4349
4350 case OBJC_CATEGORIES_MAP:
4351 if (F.LocalNumObjCCategoriesInMap != 0)
4352 return llvm::createStringError(
4353 EC: std::errc::illegal_byte_sequence,
4354 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4355
4356 F.LocalNumObjCCategoriesInMap = Record[0];
4357 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4358 break;
4359
4360 case OBJC_CATEGORIES:
4361 F.ObjCCategories.swap(RHS&: Record);
4362 break;
4363
4364 case CUDA_SPECIAL_DECL_REFS:
4365 // Later tables overwrite earlier ones.
4366 // FIXME: Modules will have trouble with this.
4367 CUDASpecialDeclRefs.clear();
4368 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4369 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4370 break;
4371
4372 case HEADER_SEARCH_TABLE:
4373 F.HeaderFileInfoTableData = Blob.data();
4374 F.LocalNumHeaderFileInfos = Record[1];
4375 if (Record[0]) {
4376 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4377 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4378 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4379 InfoObj: HeaderFileInfoTrait(*this, F));
4380
4381 PP.getHeaderSearchInfo().SetExternalSource(this);
4382 if (!PP.getHeaderSearchInfo().getExternalLookup())
4383 PP.getHeaderSearchInfo().SetExternalLookup(this);
4384 }
4385 break;
4386
4387 case FP_PRAGMA_OPTIONS:
4388 // Later tables overwrite earlier ones.
4389 FPPragmaOptions.swap(RHS&: Record);
4390 break;
4391
4392 case DECLS_WITH_EFFECTS_TO_VERIFY:
4393 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4394 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4395 break;
4396
4397 case OPENCL_EXTENSIONS:
4398 for (unsigned I = 0, E = Record.size(); I != E; ) {
4399 auto Name = ReadString(Record, Idx&: I);
4400 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4401 OptInfo.Supported = Record[I++] != 0;
4402 OptInfo.Enabled = Record[I++] != 0;
4403 OptInfo.WithPragma = Record[I++] != 0;
4404 OptInfo.Avail = Record[I++];
4405 OptInfo.Core = Record[I++];
4406 OptInfo.Opt = Record[I++];
4407 }
4408 break;
4409
4410 case TENTATIVE_DEFINITIONS:
4411 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4412 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4413 break;
4414
4415 case KNOWN_NAMESPACES:
4416 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4417 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4418 break;
4419
4420 case UNDEFINED_BUT_USED:
4421 if (Record.size() % 2 != 0)
4422 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4423 Fmt: "invalid undefined-but-used record");
4424 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4425 UndefinedButUsed.push_back(
4426 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4427 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4428 }
4429 break;
4430
4431 case DELETE_EXPRS_TO_ANALYZE:
4432 for (unsigned I = 0, N = Record.size(); I != N;) {
4433 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4434 const uint64_t Count = Record[I++];
4435 DelayedDeleteExprs.push_back(Elt: Count);
4436 for (uint64_t C = 0; C < Count; ++C) {
4437 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4438 bool IsArrayForm = Record[I++] == 1;
4439 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4440 }
4441 }
4442 break;
4443
4444 case VTABLES_TO_EMIT:
4445 if (F.Kind == MK_MainFile ||
4446 getContext().getLangOpts().BuildingPCHWithObjectFile)
4447 for (unsigned I = 0, N = Record.size(); I != N;)
4448 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4449 break;
4450
4451 case IMPORTED_MODULES:
4452 if (!F.isModule()) {
4453 // If we aren't loading a module (which has its own exports), make
4454 // all of the imported modules visible.
4455 // FIXME: Deal with macros-only imports.
4456 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4457 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4458 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4459 if (GlobalID) {
4460 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4461 if (DeserializationListener)
4462 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4463 }
4464 }
4465 }
4466 break;
4467
4468 case MACRO_OFFSET: {
4469 if (F.LocalNumMacros != 0)
4470 return llvm::createStringError(
4471 EC: std::errc::illegal_byte_sequence,
4472 Fmt: "duplicate MACRO_OFFSET record in AST file");
4473 F.MacroOffsets = (const uint32_t *)Blob.data();
4474 F.LocalNumMacros = Record[0];
4475 F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;
4476 F.BaseMacroID = getTotalNumMacros();
4477
4478 if (F.LocalNumMacros > 0)
4479 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4480 break;
4481 }
4482
4483 case LATE_PARSED_TEMPLATE:
4484 LateParsedTemplates.emplace_back(
4485 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4486 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4487 break;
4488
4489 case OPTIMIZE_PRAGMA_OPTIONS:
4490 if (Record.size() != 1)
4491 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4492 Fmt: "invalid pragma optimize record");
4493 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4494 break;
4495
4496 case MSSTRUCT_PRAGMA_OPTIONS:
4497 if (Record.size() != 1)
4498 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4499 Fmt: "invalid pragma ms_struct record");
4500 PragmaMSStructState = Record[0];
4501 break;
4502
4503 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4504 if (Record.size() != 2)
4505 return llvm::createStringError(
4506 EC: std::errc::illegal_byte_sequence,
4507 Fmt: "invalid pragma pointers to members record");
4508 PragmaMSPointersToMembersState = Record[0];
4509 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4510 break;
4511
4512 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4513 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4514 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4515 break;
4516
4517 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4518 if (Record.size() != 1)
4519 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4520 Fmt: "invalid cuda pragma options record");
4521 ForceHostDeviceDepth = Record[0];
4522 break;
4523
4524 case ALIGN_PACK_PRAGMA_OPTIONS: {
4525 if (Record.size() < 3)
4526 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4527 Fmt: "invalid pragma pack record");
4528 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4529 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4530 unsigned NumStackEntries = Record[2];
4531 unsigned Idx = 3;
4532 // Reset the stack when importing a new module.
4533 PragmaAlignPackStack.clear();
4534 for (unsigned I = 0; I < NumStackEntries; ++I) {
4535 PragmaAlignPackStackEntry Entry;
4536 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4537 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4538 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4539 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4540 Entry.SlotLabel = PragmaAlignPackStrings.back();
4541 PragmaAlignPackStack.push_back(Elt: Entry);
4542 }
4543 break;
4544 }
4545
4546 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4547 if (Record.size() < 3)
4548 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4549 Fmt: "invalid pragma float control record");
4550 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4551 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4552 unsigned NumStackEntries = Record[2];
4553 unsigned Idx = 3;
4554 // Reset the stack when importing a new module.
4555 FpPragmaStack.clear();
4556 for (unsigned I = 0; I < NumStackEntries; ++I) {
4557 FpPragmaStackEntry Entry;
4558 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4559 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4560 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4561 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4562 Entry.SlotLabel = FpPragmaStrings.back();
4563 FpPragmaStack.push_back(Elt: Entry);
4564 }
4565 break;
4566 }
4567
4568 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4569 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4570 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4571 break;
4572
4573 case RISCV_VECTOR_INTRINSICS_PRAGMA: {
4574 unsigned NumRecords = Record.front();
4575 // Last record which is used to keep number of valid records.
4576 if (Record.size() - 1 != NumRecords)
4577 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4578 Fmt: "invalid rvv intrinsic pragma record");
4579
4580 if (RISCVVecIntrinsicPragma.empty())
4581 RISCVVecIntrinsicPragma.append(NumInputs: NumRecords, Elt: 0);
4582 // There might be multiple precompiled modules imported, we need to union
4583 // them all.
4584 for (unsigned i = 0; i < NumRecords; ++i)
4585 RISCVVecIntrinsicPragma[i] |= Record[i + 1];
4586 break;
4587 }
4588 }
4589 }
4590}
4591
4592void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4593 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4594
4595 // Additional remapping information.
4596 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4597 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4598 F.ModuleOffsetMap = StringRef();
4599
4600 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4601 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4602 RemapBuilder SelectorRemap(F.SelectorRemap);
4603
4604 auto &ImportedModuleVector = F.TransitiveImports;
4605 assert(ImportedModuleVector.empty());
4606
4607 while (Data < DataEnd) {
4608 // FIXME: Looking up dependency modules by filename is horrible. Let's
4609 // start fixing this with prebuilt, explicit and implicit modules and see
4610 // how it goes...
4611 using namespace llvm::support;
4612 ModuleKind Kind = static_cast<ModuleKind>(
4613 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4614 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4615 StringRef Name = StringRef((const char*)Data, Len);
4616 Data += Len;
4617 ModuleFile *OM =
4618 (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4619 Kind == MK_ImplicitModule
4620 ? ModuleMgr.lookupByModuleName(ModName: Name)
4621 : ModuleMgr.lookupByFileName(FileName: ModuleFileName::makeExplicit(Name)));
4622 if (!OM) {
4623 std::string Msg = "refers to unknown module, cannot find ";
4624 Msg.append(str: std::string(Name));
4625 Error(Msg);
4626 return;
4627 }
4628
4629 ImportedModuleVector.push_back(Elt: OM);
4630
4631 uint32_t SubmoduleIDOffset =
4632 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4633 uint32_t SelectorIDOffset =
4634 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4635
4636 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4637 RemapBuilder &Remap) {
4638 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4639 if (Offset != None)
4640 Remap.insert(Val: std::make_pair(x&: Offset,
4641 y: static_cast<int>(BaseOffset - Offset)));
4642 };
4643
4644 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4645 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4646 }
4647}
4648
4649ASTReader::ASTReadResult
4650ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4651 const ModuleFile *ImportedBy,
4652 unsigned ClientLoadCapabilities) {
4653 unsigned Idx = 0;
4654 F.ModuleMapPath = ReadPath(F, Record, Idx);
4655
4656 // Try to resolve ModuleName in the current header search context and
4657 // verify that it is found in the same module map file as we saved. If the
4658 // top-level AST file is a main file, skip this check because there is no
4659 // usable header search context.
4660 assert(!F.ModuleName.empty() &&
4661 "MODULE_NAME should come before MODULE_MAP_FILE");
4662 auto [MaybeM, IgnoreError] =
4663 getModuleForRelocationChecks(F, /*DirectoryCheck=*/false);
4664 if (MaybeM.has_value()) {
4665 // An implicitly-loaded module file should have its module listed in some
4666 // module map file that we've already loaded.
4667 Module *M = MaybeM.value();
4668 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4669 OptionalFileEntryRef ModMap =
4670 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4671 if (!IgnoreError && !ModMap) {
4672 if (M && M->Directory)
4673 Diag(DiagID: diag::remark_module_relocated)
4674 << F.ModuleName << F.BaseDirectory << M->Directory->getName();
4675
4676 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4677 if (auto ASTFileName = M ? M->getASTFileName() : nullptr) {
4678 // This module was defined by an imported (explicit) module.
4679 Diag(DiagID: diag::err_module_file_conflict)
4680 << F.ModuleName << F.FileName << *ASTFileName;
4681 // TODO: Add a note with the module map paths if they differ.
4682 } else {
4683 // This module was built with a different module map.
4684 Diag(DiagID: diag::err_imported_module_not_found)
4685 << F.ModuleName << F.FileName
4686 << (ImportedBy ? ImportedBy->FileName.str() : "")
4687 << F.ModuleMapPath << !ImportedBy;
4688 // In case it was imported by a PCH, there's a chance the user is
4689 // just missing to include the search path to the directory containing
4690 // the modulemap.
4691 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4692 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4693 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4694 }
4695 }
4696 return OutOfDate;
4697 }
4698
4699 assert(M && M->Name == F.ModuleName && "found module with different name");
4700
4701 // Check the primary module map file.
4702 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4703 if (!StoredModMap || *StoredModMap != ModMap) {
4704 assert(ModMap && "found module is missing module map file");
4705 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4706 "top-level import should be verified");
4707 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4708 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4709 Diag(DiagID: diag::err_imported_module_modmap_changed)
4710 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4711 << ModMap->getName() << F.ModuleMapPath << NotImported;
4712 return OutOfDate;
4713 }
4714
4715 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4716 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4717 // FIXME: we should use input files rather than storing names.
4718 std::string Filename = ReadPath(F, Record, Idx);
4719 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4720 if (!SF) {
4721 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4722 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4723 return OutOfDate;
4724 }
4725 AdditionalStoredMaps.insert(V: *SF);
4726 }
4727
4728 // Check any additional module map files (e.g. module.private.modulemap)
4729 // that are not in the pcm.
4730 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4731 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4732 // Remove files that match
4733 // Note: SmallPtrSet::erase is really remove
4734 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4735 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4736 Diag(DiagID: diag::err_module_different_modmap)
4737 << F.ModuleName << /*new*/0 << ModMap.getName();
4738 return OutOfDate;
4739 }
4740 }
4741 }
4742
4743 // Check any additional module map files that are in the pcm, but not
4744 // found in header search. Cases that match are already removed.
4745 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4746 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4747 Diag(DiagID: diag::err_module_different_modmap)
4748 << F.ModuleName << /*not new*/1 << ModMap.getName();
4749 return OutOfDate;
4750 }
4751 }
4752
4753 if (Listener)
4754 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4755 return Success;
4756}
4757
4758/// Move the given method to the back of the global list of methods.
4759static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4760 // Find the entry for this selector in the method pool.
4761 SemaObjC::GlobalMethodPool::iterator Known =
4762 S.ObjC().MethodPool.find(Val: Method->getSelector());
4763 if (Known == S.ObjC().MethodPool.end())
4764 return;
4765
4766 // Retrieve the appropriate method list.
4767 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4768 : Known->second.second;
4769 bool Found = false;
4770 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4771 if (!Found) {
4772 if (List->getMethod() == Method) {
4773 Found = true;
4774 } else {
4775 // Keep searching.
4776 continue;
4777 }
4778 }
4779
4780 if (List->getNext())
4781 List->setMethod(List->getNext()->getMethod());
4782 else
4783 List->setMethod(Method);
4784 }
4785}
4786
4787void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4788 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4789 for (Decl *D : Names) {
4790 bool wasHidden = !D->isUnconditionallyVisible();
4791 D->setVisibleDespiteOwningModule();
4792
4793 if (wasHidden && SemaObj) {
4794 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4795 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4796 }
4797 }
4798 }
4799}
4800
4801void ASTReader::makeModuleVisible(Module *Mod,
4802 Module::NameVisibilityKind NameVisibility,
4803 SourceLocation ImportLoc) {
4804 llvm::SmallPtrSet<Module *, 4> Visited;
4805 SmallVector<Module *, 4> Stack;
4806 Stack.push_back(Elt: Mod);
4807 while (!Stack.empty()) {
4808 Mod = Stack.pop_back_val();
4809
4810 if (NameVisibility <= Mod->NameVisibility) {
4811 // This module already has this level of visibility (or greater), so
4812 // there is nothing more to do.
4813 continue;
4814 }
4815
4816 if (Mod->isUnimportable()) {
4817 // Modules that aren't importable cannot be made visible.
4818 continue;
4819 }
4820
4821 // Update the module's name visibility.
4822 Mod->NameVisibility = NameVisibility;
4823
4824 // If we've already deserialized any names from this module,
4825 // mark them as visible.
4826 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4827 if (Hidden != HiddenNamesMap.end()) {
4828 auto HiddenNames = std::move(*Hidden);
4829 HiddenNamesMap.erase(I: Hidden);
4830 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4831 assert(!HiddenNamesMap.contains(Mod) &&
4832 "making names visible added hidden names");
4833 }
4834
4835 // Push any exported modules onto the stack to be marked as visible.
4836 SmallVector<Module *, 16> Exports;
4837 Mod->getExportedModules(Exported&: Exports);
4838 for (SmallVectorImpl<Module *>::iterator
4839 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4840 Module *Exported = *I;
4841 if (Visited.insert(Ptr: Exported).second)
4842 Stack.push_back(Elt: Exported);
4843 }
4844 }
4845}
4846
4847/// We've merged the definition \p MergedDef into the existing definition
4848/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4849/// visible.
4850void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4851 NamedDecl *MergedDef) {
4852 if (!Def->isUnconditionallyVisible()) {
4853 // If MergedDef is visible or becomes visible, make the definition visible.
4854 if (MergedDef->isUnconditionallyVisible())
4855 Def->setVisibleDespiteOwningModule();
4856 else {
4857 getContext().mergeDefinitionIntoModule(
4858 ND: Def, M: MergedDef->getImportedOwningModule(),
4859 /*NotifyListeners*/ false);
4860 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4861 }
4862 }
4863}
4864
4865bool ASTReader::loadGlobalIndex() {
4866 if (GlobalIndex)
4867 return false;
4868
4869 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4870 !PP.getLangOpts().Modules)
4871 return true;
4872
4873 // Try to load the global index.
4874 TriedLoadingGlobalIndex = true;
4875 StringRef SpecificModuleCachePath =
4876 getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
4877 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4878 GlobalModuleIndex::readIndex(Path: SpecificModuleCachePath);
4879 if (llvm::Error Err = std::move(Result.second)) {
4880 assert(!Result.first);
4881 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4882 return true;
4883 }
4884
4885 GlobalIndex.reset(p: Result.first);
4886 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4887 return false;
4888}
4889
4890bool ASTReader::isGlobalIndexUnavailable() const {
4891 return PP.getLangOpts().Modules && UseGlobalIndex &&
4892 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4893}
4894
4895/// Given a cursor at the start of an AST file, scan ahead and drop the
4896/// cursor into the start of the given block ID, returning false on success and
4897/// true on failure.
4898static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4899 while (true) {
4900 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4901 if (!MaybeEntry) {
4902 // FIXME this drops errors on the floor.
4903 consumeError(Err: MaybeEntry.takeError());
4904 return true;
4905 }
4906 llvm::BitstreamEntry Entry = MaybeEntry.get();
4907
4908 switch (Entry.Kind) {
4909 case llvm::BitstreamEntry::Error:
4910 case llvm::BitstreamEntry::EndBlock:
4911 return true;
4912
4913 case llvm::BitstreamEntry::Record:
4914 // Ignore top-level records.
4915 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4916 break;
4917 else {
4918 // FIXME this drops errors on the floor.
4919 consumeError(Err: Skipped.takeError());
4920 return true;
4921 }
4922
4923 case llvm::BitstreamEntry::SubBlock:
4924 if (Entry.ID == BlockID) {
4925 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4926 // FIXME this drops the error on the floor.
4927 consumeError(Err: std::move(Err));
4928 return true;
4929 }
4930 // Found it!
4931 return false;
4932 }
4933
4934 if (llvm::Error Err = Cursor.SkipBlock()) {
4935 // FIXME this drops the error on the floor.
4936 consumeError(Err: std::move(Err));
4937 return true;
4938 }
4939 }
4940 }
4941}
4942
4943ASTReader::ASTReadResult ASTReader::ReadAST(ModuleFileName FileName,
4944 ModuleKind Type,
4945 SourceLocation ImportLoc,
4946 unsigned ClientLoadCapabilities,
4947 ModuleFile **NewLoadedModuleFile) {
4948 llvm::TimeTraceScope scope("ReadAST", FileName);
4949
4950 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4951 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4952 CurrentDeserializingModuleKind, Type);
4953
4954 // Defer any pending actions until we get to the end of reading the AST file.
4955 Deserializing AnASTFile(this);
4956
4957 // Bump the generation number.
4958 unsigned PreviousGeneration = 0;
4959 if (ContextObj)
4960 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4961
4962 unsigned NumModules = ModuleMgr.size();
4963 SmallVector<ImportedModule, 4> Loaded;
4964 if (ASTReadResult ReadResult =
4965 ReadASTCore(FileName, Type, ImportLoc,
4966 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4967 ClientLoadCapabilities)) {
4968 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4969
4970 // If we find that any modules are unusable, the global index is going
4971 // to be out-of-date. Just remove it.
4972 GlobalIndex.reset();
4973 ModuleMgr.setGlobalIndex(nullptr);
4974 return ReadResult;
4975 }
4976
4977 if (NewLoadedModuleFile && !Loaded.empty())
4978 *NewLoadedModuleFile = Loaded.back().Mod;
4979
4980 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4981 // remove modules from this point. Various fields are updated during reading
4982 // the AST block and removing the modules would result in dangling pointers.
4983 // They are generally only incidentally dereferenced, ie. a binary search
4984 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4985 // be dereferenced but it wouldn't actually be used.
4986
4987 // Load the AST blocks of all of the modules that we loaded. We can still
4988 // hit errors parsing the ASTs at this point.
4989 for (ImportedModule &M : Loaded) {
4990 ModuleFile &F = *M.Mod;
4991 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4992
4993 // Read the AST block.
4994 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4995 Error(Err: std::move(Err));
4996 return Failure;
4997 }
4998
4999 // The AST block should always have a definition for the main module.
5000 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
5001 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
5002 return Failure;
5003 }
5004
5005 // Read the extension blocks.
5006 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
5007 if (llvm::Error Err = ReadExtensionBlock(F)) {
5008 Error(Err: std::move(Err));
5009 return Failure;
5010 }
5011 }
5012
5013 // Once read, set the ModuleFile bit base offset and update the size in
5014 // bits of all files we've seen.
5015 F.GlobalBitOffset = TotalModulesSizeInBits;
5016 TotalModulesSizeInBits += F.SizeInBits;
5017 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
5018 }
5019
5020 // Preload source locations and interesting indentifiers.
5021 for (ImportedModule &M : Loaded) {
5022 ModuleFile &F = *M.Mod;
5023
5024 // Map the original source file ID into the ID space of the current
5025 // compilation.
5026 if (F.OriginalSourceFileID.isValid())
5027 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
5028
5029 for (auto Offset : F.PreloadIdentifierOffsets) {
5030 const unsigned char *Data = F.IdentifierTableData + Offset;
5031
5032 ASTIdentifierLookupTrait Trait(*this, F);
5033 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
5034 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
5035
5036 IdentifierInfo *II;
5037 if (!PP.getLangOpts().CPlusPlus) {
5038 // Identifiers present in both the module file and the importing
5039 // instance are marked out-of-date so that they can be deserialized
5040 // on next use via ASTReader::updateOutOfDateIdentifier().
5041 // Identifiers present in the module file but not in the importing
5042 // instance are ignored for now, preventing growth of the identifier
5043 // table. They will be deserialized on first use via ASTReader::get().
5044 auto It = PP.getIdentifierTable().find(Name: Key);
5045 if (It == PP.getIdentifierTable().end())
5046 continue;
5047 II = It->second;
5048 } else {
5049 // With C++ modules, not many identifiers are considered interesting.
5050 // All identifiers in the module file can be placed into the identifier
5051 // table of the importing instance and marked as out-of-date. This makes
5052 // ASTReader::get() a no-op, and deserialization will take place on
5053 // first/next use via ASTReader::updateOutOfDateIdentifier().
5054 II = &PP.getIdentifierTable().getOwn(Name: Key);
5055 }
5056
5057 II->setOutOfDate(true);
5058
5059 // Mark this identifier as being from an AST file so that we can track
5060 // whether we need to serialize it.
5061 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
5062
5063 // Associate the ID with the identifier so that the writer can reuse it.
5064 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
5065 SetIdentifierInfo(ID, II);
5066 }
5067 }
5068
5069 // Builtins and library builtins have already been initialized. Mark all
5070 // identifiers as out-of-date, so that they are deserialized on first use.
5071 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
5072 for (auto &Id : PP.getIdentifierTable())
5073 Id.second->setOutOfDate(true);
5074
5075 // Mark selectors as out of date.
5076 for (const auto &Sel : SelectorGeneration)
5077 SelectorOutOfDate[Sel.first] = true;
5078
5079 // Setup the import locations and notify the module manager that we've
5080 // committed to these module files.
5081 for (ImportedModule &M : Loaded) {
5082 ModuleFile &F = *M.Mod;
5083
5084 ModuleMgr.moduleFileAccepted(MF: &F);
5085
5086 // Set the import location.
5087 F.DirectImportLoc = ImportLoc;
5088 // FIXME: We assume that locations from PCH / preamble do not need
5089 // any translation.
5090 if (!M.ImportedBy)
5091 F.ImportLoc = M.ImportLoc;
5092 else
5093 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
5094 }
5095
5096 // Resolve any unresolved module exports.
5097 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
5098 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
5099 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
5100 Module *ResolvedMod = getSubmodule(GlobalID);
5101
5102 switch (Unresolved.Kind) {
5103 case UnresolvedModuleRef::Conflict:
5104 if (ResolvedMod) {
5105 Module::Conflict Conflict;
5106 Conflict.Other = ResolvedMod;
5107 Conflict.Message = Unresolved.String.str();
5108 Unresolved.Mod->Conflicts.push_back(x: Conflict);
5109 }
5110 continue;
5111
5112 case UnresolvedModuleRef::Import:
5113 if (ResolvedMod)
5114 Unresolved.Mod->Imports.insert(X: ResolvedMod);
5115 continue;
5116
5117 case UnresolvedModuleRef::Affecting:
5118 if (ResolvedMod)
5119 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
5120 continue;
5121
5122 case UnresolvedModuleRef::Export:
5123 if (ResolvedMod || Unresolved.IsWildcard)
5124 Unresolved.Mod->Exports.push_back(
5125 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
5126 continue;
5127 }
5128 }
5129 UnresolvedModuleRefs.clear();
5130
5131 // FIXME: How do we load the 'use'd modules? They may not be submodules.
5132 // Might be unnecessary as use declarations are only used to build the
5133 // module itself.
5134
5135 if (ContextObj)
5136 InitializeContext();
5137
5138 if (SemaObj)
5139 UpdateSema();
5140
5141 if (DeserializationListener)
5142 DeserializationListener->ReaderInitialized(Reader: this);
5143
5144 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
5145 if (PrimaryModule.OriginalSourceFileID.isValid()) {
5146 // If this AST file is a precompiled preamble, then set the
5147 // preamble file ID of the source manager to the file source file
5148 // from which the preamble was built.
5149 if (Type == MK_Preamble) {
5150 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
5151 } else if (Type == MK_MainFile) {
5152 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
5153 }
5154 }
5155
5156 // For any Objective-C class definitions we have already loaded, make sure
5157 // that we load any additional categories.
5158 if (ContextObj) {
5159 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
5160 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
5161 D: ObjCClassesLoaded[I], PreviousGeneration);
5162 }
5163 }
5164
5165 const HeaderSearchOptions &HSOpts =
5166 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5167 if (HSOpts.ModulesValidateOncePerBuildSession) {
5168 // Now we are certain that the module and all modules it depends on are
5169 // up-to-date. For implicitly-built module files, ensure the corresponding
5170 // timestamp files are up-to-date in this build session.
5171 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
5172 ImportedModule &M = Loaded[I];
5173 if (M.Mod->Kind == MK_ImplicitModule &&
5174 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
5175 getModuleManager().getModuleCache().updateModuleTimestamp(
5176 ModuleFilename: M.Mod->FileName);
5177 }
5178 }
5179
5180 return Success;
5181}
5182
5183static ASTFileSignature readASTFileSignature(StringRef PCH);
5184
5185/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
5186static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
5187 // FIXME checking magic headers is done in other places such as
5188 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
5189 // always done the same. Unify it all with a helper.
5190 if (!Stream.canSkipToPos(pos: 4))
5191 return llvm::createStringError(
5192 EC: std::errc::illegal_byte_sequence,
5193 Fmt: "file too small to contain precompiled file magic");
5194 for (unsigned C : {'C', 'P', 'C', 'H'})
5195 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
5196 if (Res.get() != C)
5197 return llvm::createStringError(
5198 EC: std::errc::illegal_byte_sequence,
5199 Fmt: "file doesn't start with precompiled file magic");
5200 } else
5201 return Res.takeError();
5202 return llvm::Error::success();
5203}
5204
5205static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
5206 switch (Kind) {
5207 case MK_PCH:
5208 return 0; // PCH
5209 case MK_ImplicitModule:
5210 case MK_ExplicitModule:
5211 case MK_PrebuiltModule:
5212 return 1; // module
5213 case MK_MainFile:
5214 case MK_Preamble:
5215 return 2; // main source file
5216 }
5217 llvm_unreachable("unknown module kind");
5218}
5219
5220ASTReader::ASTReadResult ASTReader::ReadASTCore(
5221 ModuleFileName FileName, ModuleKind Type, SourceLocation ImportLoc,
5222 ModuleFile *ImportedBy, SmallVectorImpl<ImportedModule> &Loaded,
5223 off_t ExpectedSize, time_t ExpectedModTime,
5224 ASTFileSignature ExpectedSignature, unsigned ClientLoadCapabilities) {
5225 ModuleFile *M;
5226 std::string ErrorStr;
5227 ModuleManager::AddModuleResult AddResult
5228 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
5229 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
5230 ExpectedSignature, ReadSignature: readASTFileSignature,
5231 Module&: M, ErrorStr);
5232
5233 switch (AddResult) {
5234 case ModuleManager::AlreadyLoaded:
5235 Diag(DiagID: diag::remark_module_import)
5236 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5237 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5238 return Success;
5239
5240 case ModuleManager::NewlyLoaded:
5241 // Load module file below.
5242 break;
5243
5244 case ModuleManager::Missing:
5245 // The module file was missing; if the client can handle that, return
5246 // it.
5247 if (ClientLoadCapabilities & ARR_Missing)
5248 return Missing;
5249
5250 // Otherwise, return an error.
5251 Diag(DiagID: diag::err_ast_file_not_found)
5252 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5253 << ErrorStr;
5254 return Failure;
5255
5256 case ModuleManager::OutOfDate:
5257 // We couldn't load the module file because it is out-of-date. If the
5258 // client can handle out-of-date, return it.
5259 if (ClientLoadCapabilities & ARR_OutOfDate)
5260 return OutOfDate;
5261
5262 // Otherwise, return an error.
5263 Diag(DiagID: diag::err_ast_file_out_of_date)
5264 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5265 << ErrorStr;
5266 return Failure;
5267 }
5268
5269 assert(M && "Missing module file");
5270
5271 bool ShouldFinalizePCM = false;
5272 llvm::scope_exit FinalizeOrDropPCM([&]() {
5273 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5274 if (ShouldFinalizePCM)
5275 MC.finalizePCM(Filename: FileName);
5276 else
5277 MC.tryToDropPCM(Filename: FileName);
5278 });
5279 ModuleFile &F = *M;
5280 BitstreamCursor &Stream = F.Stream;
5281 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5282 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5283
5284 // Sniff for the signature.
5285 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5286 Diag(DiagID: diag::err_ast_file_invalid)
5287 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5288 return Failure;
5289 }
5290
5291 // This is used for compatibility with older PCH formats.
5292 bool HaveReadControlBlock = false;
5293 while (true) {
5294 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5295 if (!MaybeEntry) {
5296 Error(Err: MaybeEntry.takeError());
5297 return Failure;
5298 }
5299 llvm::BitstreamEntry Entry = MaybeEntry.get();
5300
5301 switch (Entry.Kind) {
5302 case llvm::BitstreamEntry::Error:
5303 case llvm::BitstreamEntry::Record:
5304 case llvm::BitstreamEntry::EndBlock:
5305 Error(Msg: "invalid record at top-level of AST file");
5306 return Failure;
5307
5308 case llvm::BitstreamEntry::SubBlock:
5309 break;
5310 }
5311
5312 switch (Entry.ID) {
5313 case CONTROL_BLOCK_ID:
5314 HaveReadControlBlock = true;
5315 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5316 case Success:
5317 // Check that we didn't try to load a non-module AST file as a module.
5318 //
5319 // FIXME: Should we also perform the converse check? Loading a module as
5320 // a PCH file sort of works, but it's a bit wonky.
5321 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5322 Type == MK_PrebuiltModule) &&
5323 F.ModuleName.empty()) {
5324 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5325 if (Result != OutOfDate ||
5326 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5327 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5328 return Result;
5329 }
5330 break;
5331
5332 case Failure: return Failure;
5333 case Missing: return Missing;
5334 case OutOfDate: return OutOfDate;
5335 case VersionMismatch: return VersionMismatch;
5336 case ConfigurationMismatch: return ConfigurationMismatch;
5337 case HadErrors: return HadErrors;
5338 }
5339 break;
5340
5341 case AST_BLOCK_ID:
5342 if (!HaveReadControlBlock) {
5343 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5344 Diag(DiagID: diag::err_ast_file_version_too_old)
5345 << moduleKindForDiagnostic(Kind: Type) << FileName;
5346 return VersionMismatch;
5347 }
5348
5349 // Record that we've loaded this module.
5350 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5351 ShouldFinalizePCM = true;
5352 return Success;
5353
5354 default:
5355 if (llvm::Error Err = Stream.SkipBlock()) {
5356 Error(Err: std::move(Err));
5357 return Failure;
5358 }
5359 break;
5360 }
5361 }
5362
5363 llvm_unreachable("unexpected break; expected return");
5364}
5365
5366ASTReader::ASTReadResult
5367ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5368 unsigned ClientLoadCapabilities) {
5369 const HeaderSearchOptions &HSOpts =
5370 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5371 bool AllowCompatibleConfigurationMismatch =
5372 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5373 bool DisableValidation = shouldDisableValidationForFile(M: F);
5374
5375 ASTReadResult Result = readUnhashedControlBlockImpl(
5376 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5377 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5378 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5379
5380 // If F was directly imported by another module, it's implicitly validated by
5381 // the importing module.
5382 if (DisableValidation || WasImportedBy ||
5383 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5384 return Success;
5385
5386 if (Result == Failure) {
5387 Error(Msg: "malformed block record in AST file");
5388 return Failure;
5389 }
5390
5391 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5392 // If this module has already been finalized in the ModuleCache, we're stuck
5393 // with it; we can only load a single version of each module.
5394 //
5395 // This can happen when a module is imported in two contexts: in one, as a
5396 // user module; in another, as a system module (due to an import from
5397 // another module marked with the [system] flag). It usually indicates a
5398 // bug in the module map: this module should also be marked with [system].
5399 //
5400 // If -Wno-system-headers (the default), and the first import is as a
5401 // system module, then validation will fail during the as-user import,
5402 // since -Werror flags won't have been validated. However, it's reasonable
5403 // to treat this consistently as a system module.
5404 //
5405 // If -Wsystem-headers, the PCM on disk was built with
5406 // -Wno-system-headers, and the first import is as a user module, then
5407 // validation will fail during the as-system import since the PCM on disk
5408 // doesn't guarantee that -Werror was respected. However, the -Werror
5409 // flags were checked during the initial as-user import.
5410 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5411 Filename: F.FileName)) {
5412 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5413 return Success;
5414 }
5415 }
5416
5417 return Result;
5418}
5419
5420ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5421 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5422 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5423 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5424 // Initialize a stream.
5425 BitstreamCursor Stream(StreamData);
5426
5427 // Sniff for the signature.
5428 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5429 // FIXME this drops the error on the floor.
5430 consumeError(Err: std::move(Err));
5431 return Failure;
5432 }
5433
5434 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5435 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5436 return Failure;
5437
5438 // Read all of the records in the options block.
5439 RecordData Record;
5440 ASTReadResult Result = Success;
5441 while (true) {
5442 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5443 if (!MaybeEntry) {
5444 // FIXME this drops the error on the floor.
5445 consumeError(Err: MaybeEntry.takeError());
5446 return Failure;
5447 }
5448 llvm::BitstreamEntry Entry = MaybeEntry.get();
5449
5450 switch (Entry.Kind) {
5451 case llvm::BitstreamEntry::Error:
5452 case llvm::BitstreamEntry::SubBlock:
5453 return Failure;
5454
5455 case llvm::BitstreamEntry::EndBlock:
5456 return Result;
5457
5458 case llvm::BitstreamEntry::Record:
5459 // The interesting case.
5460 break;
5461 }
5462
5463 // Read and process a record.
5464 Record.clear();
5465 StringRef Blob;
5466 Expected<unsigned> MaybeRecordType =
5467 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5468 if (!MaybeRecordType) {
5469 // FIXME this drops the error.
5470 return Failure;
5471 }
5472 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5473 case SIGNATURE:
5474 if (F) {
5475 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5476 assert(F->Signature != ASTFileSignature::createDummy() &&
5477 "Dummy AST file signature not backpatched in ASTWriter.");
5478 }
5479 break;
5480 case AST_BLOCK_HASH:
5481 if (F) {
5482 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5483 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5484 "Dummy AST block hash not backpatched in ASTWriter.");
5485 }
5486 break;
5487 case DIAGNOSTIC_OPTIONS: {
5488 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5489 if (Listener && ValidateDiagnosticOptions &&
5490 !AllowCompatibleConfigurationMismatch &&
5491 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5492 Result = OutOfDate; // Don't return early. Read the signature.
5493 break;
5494 }
5495 case HEADER_SEARCH_PATHS: {
5496 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5497 if (Listener && !AllowCompatibleConfigurationMismatch &&
5498 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5499 Result = ConfigurationMismatch;
5500 break;
5501 }
5502 case DIAG_PRAGMA_MAPPINGS:
5503 if (!F)
5504 break;
5505 if (F->PragmaDiagMappings.empty())
5506 F->PragmaDiagMappings.swap(RHS&: Record);
5507 else
5508 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5509 From: Record.begin(), To: Record.end());
5510 break;
5511 case HEADER_SEARCH_ENTRY_USAGE:
5512 if (F)
5513 F->SearchPathUsage = ReadBitVector(Record, Blob);
5514 break;
5515 case VFS_USAGE:
5516 if (F)
5517 F->VFSUsage = ReadBitVector(Record, Blob);
5518 break;
5519 }
5520 }
5521}
5522
5523/// Parse a record and blob containing module file extension metadata.
5524static bool parseModuleFileExtensionMetadata(
5525 const SmallVectorImpl<uint64_t> &Record,
5526 StringRef Blob,
5527 ModuleFileExtensionMetadata &Metadata) {
5528 if (Record.size() < 4) return true;
5529
5530 Metadata.MajorVersion = Record[0];
5531 Metadata.MinorVersion = Record[1];
5532
5533 unsigned BlockNameLen = Record[2];
5534 unsigned UserInfoLen = Record[3];
5535
5536 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5537
5538 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5539 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5540 Blob.data() + BlockNameLen + UserInfoLen);
5541 return false;
5542}
5543
5544llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5545 BitstreamCursor &Stream = F.Stream;
5546
5547 RecordData Record;
5548 while (true) {
5549 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5550 if (!MaybeEntry)
5551 return MaybeEntry.takeError();
5552 llvm::BitstreamEntry Entry = MaybeEntry.get();
5553
5554 switch (Entry.Kind) {
5555 case llvm::BitstreamEntry::SubBlock:
5556 if (llvm::Error Err = Stream.SkipBlock())
5557 return Err;
5558 continue;
5559 case llvm::BitstreamEntry::EndBlock:
5560 return llvm::Error::success();
5561 case llvm::BitstreamEntry::Error:
5562 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5563 Fmt: "malformed block record in AST file");
5564 case llvm::BitstreamEntry::Record:
5565 break;
5566 }
5567
5568 Record.clear();
5569 StringRef Blob;
5570 Expected<unsigned> MaybeRecCode =
5571 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5572 if (!MaybeRecCode)
5573 return MaybeRecCode.takeError();
5574 switch (MaybeRecCode.get()) {
5575 case EXTENSION_METADATA: {
5576 ModuleFileExtensionMetadata Metadata;
5577 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5578 return llvm::createStringError(
5579 EC: std::errc::illegal_byte_sequence,
5580 Fmt: "malformed EXTENSION_METADATA in AST file");
5581
5582 // Find a module file extension with this block name.
5583 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5584 if (Known == ModuleFileExtensions.end()) break;
5585
5586 // Form a reader.
5587 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5588 Mod&: F, Stream)) {
5589 F.ExtensionReaders.push_back(x: std::move(Reader));
5590 }
5591
5592 break;
5593 }
5594 }
5595 }
5596
5597 llvm_unreachable("ReadExtensionBlock should return from while loop");
5598}
5599
5600void ASTReader::InitializeContext() {
5601 assert(ContextObj && "no context to initialize");
5602 ASTContext &Context = *ContextObj;
5603
5604 // If there's a listener, notify them that we "read" the translation unit.
5605 if (DeserializationListener)
5606 DeserializationListener->DeclRead(
5607 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5608 D: Context.getTranslationUnitDecl());
5609
5610 // FIXME: Find a better way to deal with collisions between these
5611 // built-in types. Right now, we just ignore the problem.
5612
5613 // Load the special types.
5614 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5615 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5616 if (!Context.CFConstantStringTypeDecl)
5617 Context.setCFConstantStringType(GetType(ID: String));
5618 }
5619
5620 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5621 QualType FileType = GetType(ID: File);
5622 if (FileType.isNull()) {
5623 Error(Msg: "FILE type is NULL");
5624 return;
5625 }
5626
5627 if (!Context.FILEDecl) {
5628 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5629 Context.setFILEDecl(Typedef->getDecl());
5630 else {
5631 const TagType *Tag = FileType->getAs<TagType>();
5632 if (!Tag) {
5633 Error(Msg: "Invalid FILE type in AST file");
5634 return;
5635 }
5636 Context.setFILEDecl(Tag->getDecl());
5637 }
5638 }
5639 }
5640
5641 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5642 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5643 if (Jmp_bufType.isNull()) {
5644 Error(Msg: "jmp_buf type is NULL");
5645 return;
5646 }
5647
5648 if (!Context.jmp_bufDecl) {
5649 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5650 Context.setjmp_bufDecl(Typedef->getDecl());
5651 else {
5652 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5653 if (!Tag) {
5654 Error(Msg: "Invalid jmp_buf type in AST file");
5655 return;
5656 }
5657 Context.setjmp_bufDecl(Tag->getDecl());
5658 }
5659 }
5660 }
5661
5662 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5663 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5664 if (Sigjmp_bufType.isNull()) {
5665 Error(Msg: "sigjmp_buf type is NULL");
5666 return;
5667 }
5668
5669 if (!Context.sigjmp_bufDecl) {
5670 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5671 Context.setsigjmp_bufDecl(Typedef->getDecl());
5672 else {
5673 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5674 assert(Tag && "Invalid sigjmp_buf type in AST file");
5675 Context.setsigjmp_bufDecl(Tag->getDecl());
5676 }
5677 }
5678 }
5679
5680 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5681 if (Context.ObjCIdRedefinitionType.isNull())
5682 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5683 }
5684
5685 if (TypeID ObjCClassRedef =
5686 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5687 if (Context.ObjCClassRedefinitionType.isNull())
5688 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5689 }
5690
5691 if (TypeID ObjCSelRedef =
5692 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5693 if (Context.ObjCSelRedefinitionType.isNull())
5694 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5695 }
5696
5697 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5698 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5699 if (Ucontext_tType.isNull()) {
5700 Error(Msg: "ucontext_t type is NULL");
5701 return;
5702 }
5703
5704 if (!Context.ucontext_tDecl) {
5705 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5706 Context.setucontext_tDecl(Typedef->getDecl());
5707 else {
5708 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5709 assert(Tag && "Invalid ucontext_t type in AST file");
5710 Context.setucontext_tDecl(Tag->getDecl());
5711 }
5712 }
5713 }
5714 }
5715
5716 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5717
5718 // If there were any CUDA special declarations, deserialize them.
5719 if (!CUDASpecialDeclRefs.empty()) {
5720 assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");
5721 Context.setcudaConfigureCallDecl(
5722 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5723 Context.setcudaGetParameterBufferDecl(
5724 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[1])));
5725 Context.setcudaLaunchDeviceDecl(
5726 cast_or_null<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[2])));
5727 }
5728
5729 // Re-export any modules that were imported by a non-module AST file.
5730 // FIXME: This does not make macro-only imports visible again.
5731 for (auto &Import : PendingImportedModules) {
5732 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5733 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5734 /*ImportLoc=*/Import.ImportLoc);
5735 if (Import.ImportLoc.isValid())
5736 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5737 // This updates visibility for Preprocessor only. For Sema, which can be
5738 // nullptr here, we do the same later, in UpdateSema().
5739 }
5740 }
5741
5742 // Hand off these modules to Sema.
5743 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5744 PendingImportedModules.clear();
5745}
5746
5747void ASTReader::finalizeForWriting() {
5748 // Nothing to do for now.
5749}
5750
5751/// Reads and return the signature record from \p PCH's control block, or
5752/// else returns 0.
5753static ASTFileSignature readASTFileSignature(StringRef PCH) {
5754 BitstreamCursor Stream(PCH);
5755 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5756 // FIXME this drops the error on the floor.
5757 consumeError(Err: std::move(Err));
5758 return ASTFileSignature();
5759 }
5760
5761 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5762 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5763 return ASTFileSignature();
5764
5765 // Scan for SIGNATURE inside the diagnostic options block.
5766 ASTReader::RecordData Record;
5767 while (true) {
5768 Expected<llvm::BitstreamEntry> MaybeEntry =
5769 Stream.advanceSkippingSubblocks();
5770 if (!MaybeEntry) {
5771 // FIXME this drops the error on the floor.
5772 consumeError(Err: MaybeEntry.takeError());
5773 return ASTFileSignature();
5774 }
5775 llvm::BitstreamEntry Entry = MaybeEntry.get();
5776
5777 if (Entry.Kind != llvm::BitstreamEntry::Record)
5778 return ASTFileSignature();
5779
5780 Record.clear();
5781 StringRef Blob;
5782 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5783 if (!MaybeRecord) {
5784 // FIXME this drops the error on the floor.
5785 consumeError(Err: MaybeRecord.takeError());
5786 return ASTFileSignature();
5787 }
5788 if (SIGNATURE == MaybeRecord.get()) {
5789 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5790 assert(Signature != ASTFileSignature::createDummy() &&
5791 "Dummy AST file signature not backpatched in ASTWriter.");
5792 return Signature;
5793 }
5794 }
5795}
5796
5797/// Retrieve the name of the original source file name
5798/// directly from the AST file, without actually loading the AST
5799/// file.
5800std::string ASTReader::getOriginalSourceFile(
5801 const std::string &ASTFileName, FileManager &FileMgr,
5802 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5803 // Open the AST file.
5804 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5805 /*RequiresNullTerminator=*/false,
5806 /*MaybeLimit=*/std::nullopt,
5807 /*IsText=*/false);
5808 if (!Buffer) {
5809 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5810 << ASTFileName << Buffer.getError().message();
5811 return std::string();
5812 }
5813
5814 // Initialize the stream
5815 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5816
5817 // Sniff for the signature.
5818 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5819 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5820 return std::string();
5821 }
5822
5823 // Scan for the CONTROL_BLOCK_ID block.
5824 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5825 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5826 return std::string();
5827 }
5828
5829 // Scan for ORIGINAL_FILE inside the control block.
5830 RecordData Record;
5831 while (true) {
5832 Expected<llvm::BitstreamEntry> MaybeEntry =
5833 Stream.advanceSkippingSubblocks();
5834 if (!MaybeEntry) {
5835 // FIXME this drops errors on the floor.
5836 consumeError(Err: MaybeEntry.takeError());
5837 return std::string();
5838 }
5839 llvm::BitstreamEntry Entry = MaybeEntry.get();
5840
5841 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5842 return std::string();
5843
5844 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5845 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5846 return std::string();
5847 }
5848
5849 Record.clear();
5850 StringRef Blob;
5851 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5852 if (!MaybeRecord) {
5853 // FIXME this drops the errors on the floor.
5854 consumeError(Err: MaybeRecord.takeError());
5855 return std::string();
5856 }
5857 if (ORIGINAL_FILE == MaybeRecord.get())
5858 return Blob.str();
5859 }
5860}
5861
5862namespace {
5863
5864 class SimplePCHValidator : public ASTReaderListener {
5865 const LangOptions &ExistingLangOpts;
5866 const CodeGenOptions &ExistingCGOpts;
5867 const TargetOptions &ExistingTargetOpts;
5868 const PreprocessorOptions &ExistingPPOpts;
5869 const HeaderSearchOptions &ExistingHSOpts;
5870 std::string ExistingSpecificModuleCachePath;
5871 FileManager &FileMgr;
5872 bool StrictOptionMatches;
5873
5874 public:
5875 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5876 const CodeGenOptions &ExistingCGOpts,
5877 const TargetOptions &ExistingTargetOpts,
5878 const PreprocessorOptions &ExistingPPOpts,
5879 const HeaderSearchOptions &ExistingHSOpts,
5880 StringRef ExistingSpecificModuleCachePath,
5881 FileManager &FileMgr, bool StrictOptionMatches)
5882 : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),
5883 ExistingTargetOpts(ExistingTargetOpts),
5884 ExistingPPOpts(ExistingPPOpts), ExistingHSOpts(ExistingHSOpts),
5885 ExistingSpecificModuleCachePath(ExistingSpecificModuleCachePath),
5886 FileMgr(FileMgr), StrictOptionMatches(StrictOptionMatches) {}
5887
5888 bool ReadLanguageOptions(const LangOptions &LangOpts,
5889 StringRef ModuleFilename, bool Complain,
5890 bool AllowCompatibleDifferences) override {
5891 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5892 Diags: nullptr, AllowCompatibleDifferences);
5893 }
5894
5895 bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,
5896 StringRef ModuleFilename, bool Complain,
5897 bool AllowCompatibleDifferences) override {
5898 return checkCodegenOptions(CGOpts: ExistingCGOpts, ExistingCGOpts: CGOpts, ModuleFilename,
5899 Diags: nullptr, AllowCompatibleDifferences);
5900 }
5901
5902 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5903 StringRef ModuleFilename, bool Complain,
5904 bool AllowCompatibleDifferences) override {
5905 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
5906 Diags: nullptr, AllowCompatibleDifferences);
5907 }
5908
5909 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5910 StringRef ASTFilename, StringRef ContextHash,
5911 bool Complain) override {
5912 return checkModuleCachePath(
5913 FileMgr, ContextHash, ExistingSpecificModuleCachePath, ASTFilename,
5914 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts, HSOpts: ExistingHSOpts, ASTFileHSOpts: HSOpts);
5915 }
5916
5917 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5918 StringRef ModuleFilename, bool ReadMacros,
5919 bool Complain,
5920 std::string &SuggestedPredefines) override {
5921 return checkPreprocessorOptions(
5922 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5923 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5924 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5925 : OptionValidateContradictions);
5926 }
5927 };
5928
5929} // namespace
5930
5931bool ASTReader::readASTFileControlBlock(
5932 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5933 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5934 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5935 unsigned ClientLoadCapabilities) {
5936 // Open the AST file.
5937 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5938 llvm::MemoryBuffer *Buffer =
5939 ModCache.getInMemoryModuleCache().lookupPCM(Filename);
5940 if (!Buffer) {
5941 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5942 // read again later, but we do not have the context here to determine if it
5943 // is safe to change the result of InMemoryModuleCache::getPCMState().
5944
5945 // FIXME: This allows use of the VFS; we do not allow use of the
5946 // VFS when actually loading a module.
5947 auto Entry =
5948 Filename == "-" ? FileMgr.getSTDIN() : FileMgr.getFileRef(Filename);
5949 if (!Entry) {
5950 llvm::consumeError(Err: Entry.takeError());
5951 return true;
5952 }
5953 auto BufferOrErr = FileMgr.getBufferForFile(Entry: *Entry);
5954 if (!BufferOrErr)
5955 return true;
5956 OwnedBuffer = std::move(*BufferOrErr);
5957 Buffer = OwnedBuffer.get();
5958 }
5959
5960 // Initialize the stream
5961 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5962 BitstreamCursor Stream(Bytes);
5963
5964 // Sniff for the signature.
5965 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5966 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5967 return true;
5968 }
5969
5970 // Scan for the CONTROL_BLOCK_ID block.
5971 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5972 return true;
5973
5974 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5975 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5976 bool NeedsImports = Listener.needsImportVisitation();
5977 BitstreamCursor InputFilesCursor;
5978 uint64_t InputFilesOffsetBase = 0;
5979
5980 RecordData Record;
5981 std::string ModuleDir;
5982 bool DoneWithControlBlock = false;
5983 SmallString<0> PathBuf;
5984 PathBuf.reserve(N: 256);
5985 // Additional path buffer to use when multiple paths need to be resolved.
5986 // For example, when deserializing input files that contains a path that was
5987 // resolved from a vfs overlay and an external location.
5988 SmallString<0> AdditionalPathBuf;
5989 AdditionalPathBuf.reserve(N: 256);
5990 while (!DoneWithControlBlock) {
5991 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5992 if (!MaybeEntry) {
5993 // FIXME this drops the error on the floor.
5994 consumeError(Err: MaybeEntry.takeError());
5995 return true;
5996 }
5997 llvm::BitstreamEntry Entry = MaybeEntry.get();
5998
5999 switch (Entry.Kind) {
6000 case llvm::BitstreamEntry::SubBlock: {
6001 switch (Entry.ID) {
6002 case OPTIONS_BLOCK_ID: {
6003 std::string IgnoredSuggestedPredefines;
6004 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
6005 /*AllowCompatibleConfigurationMismatch*/ false,
6006 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
6007 return true;
6008 break;
6009 }
6010
6011 case INPUT_FILES_BLOCK_ID:
6012 InputFilesCursor = Stream;
6013 if (llvm::Error Err = Stream.SkipBlock()) {
6014 // FIXME this drops the error on the floor.
6015 consumeError(Err: std::move(Err));
6016 return true;
6017 }
6018 if (NeedsInputFiles &&
6019 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
6020 return true;
6021 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
6022 break;
6023
6024 default:
6025 if (llvm::Error Err = Stream.SkipBlock()) {
6026 // FIXME this drops the error on the floor.
6027 consumeError(Err: std::move(Err));
6028 return true;
6029 }
6030 break;
6031 }
6032
6033 continue;
6034 }
6035
6036 case llvm::BitstreamEntry::EndBlock:
6037 DoneWithControlBlock = true;
6038 break;
6039
6040 case llvm::BitstreamEntry::Error:
6041 return true;
6042
6043 case llvm::BitstreamEntry::Record:
6044 break;
6045 }
6046
6047 if (DoneWithControlBlock) break;
6048
6049 Record.clear();
6050 StringRef Blob;
6051 Expected<unsigned> MaybeRecCode =
6052 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6053 if (!MaybeRecCode) {
6054 // FIXME this drops the error.
6055 return Failure;
6056 }
6057 switch ((ControlRecordTypes)MaybeRecCode.get()) {
6058 case METADATA:
6059 if (Record[0] != VERSION_MAJOR)
6060 return true;
6061 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
6062 return true;
6063 break;
6064 case MODULE_NAME:
6065 Listener.ReadModuleName(ModuleName: Blob);
6066 break;
6067 case MODULE_DIRECTORY:
6068 ModuleDir = std::string(Blob);
6069 break;
6070 case MODULE_MAP_FILE: {
6071 unsigned Idx = 0;
6072 std::string PathStr = ReadString(Record, Idx);
6073 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
6074 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
6075 break;
6076 }
6077 case INPUT_FILE_OFFSETS: {
6078 if (!NeedsInputFiles)
6079 break;
6080
6081 unsigned NumInputFiles = Record[0];
6082 unsigned NumUserFiles = Record[1];
6083 const llvm::support::unaligned_uint64_t *InputFileOffs =
6084 (const llvm::support::unaligned_uint64_t *)Blob.data();
6085 for (unsigned I = 0; I != NumInputFiles; ++I) {
6086 // Go find this input file.
6087 bool isSystemFile = I >= NumUserFiles;
6088
6089 if (isSystemFile && !NeedsSystemInputFiles)
6090 break; // the rest are system input files
6091
6092 BitstreamCursor &Cursor = InputFilesCursor;
6093 SavedStreamPosition SavedPosition(Cursor);
6094 if (llvm::Error Err =
6095 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
6096 // FIXME this drops errors on the floor.
6097 consumeError(Err: std::move(Err));
6098 }
6099
6100 Expected<unsigned> MaybeCode = Cursor.ReadCode();
6101 if (!MaybeCode) {
6102 // FIXME this drops errors on the floor.
6103 consumeError(Err: MaybeCode.takeError());
6104 }
6105 unsigned Code = MaybeCode.get();
6106
6107 RecordData Record;
6108 StringRef Blob;
6109 bool shouldContinue = false;
6110 Expected<unsigned> MaybeRecordType =
6111 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
6112 if (!MaybeRecordType) {
6113 // FIXME this drops errors on the floor.
6114 consumeError(Err: MaybeRecordType.takeError());
6115 }
6116 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
6117 case INPUT_FILE_HASH:
6118 break;
6119 case INPUT_FILE:
6120 time_t StoredTime = static_cast<time_t>(Record[2]);
6121 bool Overridden = static_cast<bool>(Record[3]);
6122 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
6123 getUnresolvedInputFilenames(Record, InputBlob: Blob);
6124 auto FilenameAsRequestedBuf = ResolveImportedPath(
6125 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
6126 StringRef Filename;
6127 if (UnresolvedFilename.empty())
6128 Filename = *FilenameAsRequestedBuf;
6129 else {
6130 auto FilenameBuf = ResolveImportedPath(
6131 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
6132 Filename = *FilenameBuf;
6133 }
6134 shouldContinue = Listener.visitInputFileAsRequested(
6135 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
6136 StoredTime, /*IsExplicitModule=*/isExplicitModule: false);
6137 break;
6138 }
6139 if (!shouldContinue)
6140 break;
6141 }
6142 break;
6143 }
6144
6145 case IMPORT: {
6146 if (!NeedsImports)
6147 break;
6148
6149 unsigned Idx = 0;
6150 // Read information about the AST file.
6151
6152 // Skip Kind
6153 Idx++;
6154
6155 // Skip ImportLoc
6156 Idx++;
6157
6158 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
6159
6160 bool IsStandardCXXModule = Record[Idx++];
6161
6162 // In C++20 Modules, we don't record the path to imported
6163 // modules in the BMI files.
6164 if (IsStandardCXXModule) {
6165 Listener.visitImport(ModuleName, /*Filename=*/"");
6166 continue;
6167 }
6168
6169 // Skip Size, ModTime and ImplicitModuleSuffix.
6170 Idx += 1 + 1 + 1;
6171 // Skip signature.
6172 Blob = Blob.substr(Start: ASTFileSignature::size);
6173
6174 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
6175 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
6176 Listener.visitImport(ModuleName, Filename: *Filename);
6177 break;
6178 }
6179
6180 default:
6181 // No other validation to perform.
6182 break;
6183 }
6184 }
6185
6186 // Look for module file extension blocks, if requested.
6187 if (FindModuleFileExtensions) {
6188 BitstreamCursor SavedStream = Stream;
6189 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
6190 bool DoneWithExtensionBlock = false;
6191 while (!DoneWithExtensionBlock) {
6192 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
6193 if (!MaybeEntry) {
6194 // FIXME this drops the error.
6195 return true;
6196 }
6197 llvm::BitstreamEntry Entry = MaybeEntry.get();
6198
6199 switch (Entry.Kind) {
6200 case llvm::BitstreamEntry::SubBlock:
6201 if (llvm::Error Err = Stream.SkipBlock()) {
6202 // FIXME this drops the error on the floor.
6203 consumeError(Err: std::move(Err));
6204 return true;
6205 }
6206 continue;
6207
6208 case llvm::BitstreamEntry::EndBlock:
6209 DoneWithExtensionBlock = true;
6210 continue;
6211
6212 case llvm::BitstreamEntry::Error:
6213 return true;
6214
6215 case llvm::BitstreamEntry::Record:
6216 break;
6217 }
6218
6219 Record.clear();
6220 StringRef Blob;
6221 Expected<unsigned> MaybeRecCode =
6222 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6223 if (!MaybeRecCode) {
6224 // FIXME this drops the error.
6225 return true;
6226 }
6227 switch (MaybeRecCode.get()) {
6228 case EXTENSION_METADATA: {
6229 ModuleFileExtensionMetadata Metadata;
6230 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
6231 return true;
6232
6233 Listener.readModuleFileExtension(Metadata);
6234 break;
6235 }
6236 }
6237 }
6238 }
6239 Stream = std::move(SavedStream);
6240 }
6241
6242 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6243 if (readUnhashedControlBlockImpl(
6244 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6245 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6246 ValidateDiagnosticOptions) != Success)
6247 return true;
6248
6249 return false;
6250}
6251
6252bool ASTReader::isAcceptableASTFile(
6253 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6254 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6255 const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,
6256 const PreprocessorOptions &PPOpts, const HeaderSearchOptions &HSOpts,
6257 StringRef SpecificModuleCachePath, bool RequireStrictOptionMatches) {
6258 SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, HSOpts,
6259 SpecificModuleCachePath, FileMgr,
6260 RequireStrictOptionMatches);
6261 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6262 /*FindModuleFileExtensions=*/false, Listener&: validator,
6263 /*ValidateDiagnosticOptions=*/true);
6264}
6265
6266llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
6267 unsigned ClientLoadCapabilities) {
6268 // Enter the submodule block.
6269 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
6270 return Err;
6271
6272 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6273 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6274 // If we don't know the top-level module, there's no point in doing qualified
6275 // lookup of its submodules; it won't find anything anywhere within this tree.
6276 // Let's skip that and avoid some string lookups.
6277 auto CreateModule = !KnowsTopLevelModule
6278 ? &ModuleMap::createModule
6279 : &ModuleMap::findOrCreateModuleFirst;
6280
6281 bool First = true;
6282 Module *CurrentModule = nullptr;
6283 RecordData Record;
6284 while (true) {
6285 Expected<llvm::BitstreamEntry> MaybeEntry =
6286 F.Stream.advanceSkippingSubblocks();
6287 if (!MaybeEntry)
6288 return MaybeEntry.takeError();
6289 llvm::BitstreamEntry Entry = MaybeEntry.get();
6290
6291 switch (Entry.Kind) {
6292 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6293 case llvm::BitstreamEntry::Error:
6294 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6295 Fmt: "malformed block record in AST file");
6296 case llvm::BitstreamEntry::EndBlock:
6297 return llvm::Error::success();
6298 case llvm::BitstreamEntry::Record:
6299 // The interesting case.
6300 break;
6301 }
6302
6303 // Read a record.
6304 StringRef Blob;
6305 Record.clear();
6306 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6307 if (!MaybeKind)
6308 return MaybeKind.takeError();
6309 unsigned Kind = MaybeKind.get();
6310
6311 if ((Kind == SUBMODULE_METADATA) != First)
6312 return llvm::createStringError(
6313 EC: std::errc::illegal_byte_sequence,
6314 Fmt: "submodule metadata record should be at beginning of block");
6315 First = false;
6316
6317 // Submodule information is only valid if we have a current module.
6318 // FIXME: Should we error on these cases?
6319 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
6320 Kind != SUBMODULE_DEFINITION)
6321 continue;
6322
6323 switch (Kind) {
6324 default: // Default behavior: ignore.
6325 break;
6326
6327 case SUBMODULE_DEFINITION: {
6328 if (Record.size() < 13)
6329 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6330 Fmt: "malformed module definition");
6331
6332 StringRef Name = Blob;
6333 unsigned Idx = 0;
6334 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6335 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6336 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6337 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6338 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6339 bool IsFramework = Record[Idx++];
6340 bool IsExplicit = Record[Idx++];
6341 bool IsSystem = Record[Idx++];
6342 bool IsExternC = Record[Idx++];
6343 bool InferSubmodules = Record[Idx++];
6344 bool InferExplicitSubmodules = Record[Idx++];
6345 bool InferExportWildcard = Record[Idx++];
6346 bool ConfigMacrosExhaustive = Record[Idx++];
6347 bool ModuleMapIsPrivate = Record[Idx++];
6348 bool NamedModuleHasInit = Record[Idx++];
6349
6350 Module *ParentModule = nullptr;
6351 if (Parent)
6352 ParentModule = getSubmodule(GlobalID: Parent);
6353
6354 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6355 args&: IsFramework, args&: IsExplicit);
6356
6357 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6358 if (GlobalIndex >= SubmodulesLoaded.size() ||
6359 SubmodulesLoaded[GlobalIndex])
6360 return llvm::createStringError(EC: std::errc::invalid_argument,
6361 Fmt: "too many submodules");
6362
6363 if (!ParentModule) {
6364 if ([[maybe_unused]] const ModuleFileKey *CurFileKey =
6365 CurrentModule->getASTFileKey()) {
6366 // Don't emit module relocation error if we have -fno-validate-pch
6367 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6368 DisableValidationForModuleKind::Module)) {
6369 assert(*CurFileKey != F.FileKey &&
6370 "ModuleManager did not de-duplicate");
6371
6372 Diag(DiagID: diag::err_module_file_conflict)
6373 << CurrentModule->getTopLevelModuleName()
6374 << *CurrentModule->getASTFileName() << F.FileName;
6375
6376 auto CurModMapFile =
6377 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6378 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6379 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6380 Diag(DiagID: diag::note_module_file_conflict)
6381 << CurModMapFile->getName() << ModMapFile->getName();
6382
6383 return llvm::make_error<AlreadyReportedDiagnosticError>();
6384 }
6385 }
6386
6387 F.DidReadTopLevelSubmodule = true;
6388 CurrentModule->setASTFileNameAndKey(NewName: F.FileName, NewKey: F.FileKey);
6389 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6390 }
6391
6392 CurrentModule->Kind = Kind;
6393 // Note that we may be rewriting an existing location and it is important
6394 // to keep doing that. In particular, we would like to prefer a
6395 // `DefinitionLoc` loaded from the module file instead of the location
6396 // created in the current source manager, because it allows the new
6397 // location to be marked as "unaffecting" when writing and avoid creating
6398 // duplicate locations for the same module map file.
6399 CurrentModule->DefinitionLoc = DefinitionLoc;
6400 CurrentModule->Signature = F.Signature;
6401 CurrentModule->IsFromModuleFile = true;
6402 if (InferredAllowedBy.isValid())
6403 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6404 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6405 CurrentModule->IsExternC = IsExternC;
6406 CurrentModule->InferSubmodules = InferSubmodules;
6407 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6408 CurrentModule->InferExportWildcard = InferExportWildcard;
6409 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6410 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6411 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6412
6413 if (!ParentModule && !F.BaseDirectory.empty()) {
6414 if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName: F.BaseDirectory))
6415 CurrentModule->Directory = *Dir;
6416 } else if (ParentModule && ParentModule->Directory) {
6417 // Submodules inherit the directory from their parent.
6418 CurrentModule->Directory = ParentModule->Directory;
6419 }
6420
6421 if (DeserializationListener)
6422 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6423
6424 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6425
6426 // Clear out data that will be replaced by what is in the module file.
6427 CurrentModule->LinkLibraries.clear();
6428 CurrentModule->ConfigMacros.clear();
6429 CurrentModule->UnresolvedConflicts.clear();
6430 CurrentModule->Conflicts.clear();
6431
6432 // The module is available unless it's missing a requirement; relevant
6433 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6434 // Missing headers that were present when the module was built do not
6435 // make it unavailable -- if we got this far, this must be an explicitly
6436 // imported module file.
6437 CurrentModule->Requirements.clear();
6438 CurrentModule->MissingHeaders.clear();
6439 CurrentModule->IsUnimportable =
6440 ParentModule && ParentModule->IsUnimportable;
6441 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6442 break;
6443 }
6444
6445 case SUBMODULE_UMBRELLA_HEADER: {
6446 SmallString<128> RelativePathName;
6447 if (auto Umbrella = ModMap.findUmbrellaHeaderForModule(
6448 M: CurrentModule, NameAsWritten: Blob.str(), RelativePathName)) {
6449 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6450 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob,
6451 PathRelativeToRootModuleDirectory: RelativePathName);
6452 }
6453 // Note that it's too late at this point to return out of date if the
6454 // name from the PCM doesn't match up with the one in the module map,
6455 // but also quite unlikely since we will have already checked the
6456 // modification time and size of the module map file itself.
6457 }
6458 break;
6459 }
6460
6461 case SUBMODULE_HEADER:
6462 case SUBMODULE_EXCLUDED_HEADER:
6463 case SUBMODULE_PRIVATE_HEADER:
6464 // We lazily associate headers with their modules via the HeaderInfo table.
6465 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6466 // of complete filenames or remove it entirely.
6467 break;
6468
6469 case SUBMODULE_TEXTUAL_HEADER:
6470 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6471 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6472 // them here.
6473 break;
6474
6475 case SUBMODULE_TOPHEADER: {
6476 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6477 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6478 break;
6479 }
6480
6481 case SUBMODULE_UMBRELLA_DIR: {
6482 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6483 if (auto Umbrella =
6484 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6485 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6486 // FIXME: NameAsWritten
6487 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6488 }
6489 }
6490 break;
6491 }
6492
6493 case SUBMODULE_METADATA: {
6494 F.BaseSubmoduleID = getTotalNumSubmodules();
6495 F.LocalNumSubmodules = Record[0];
6496 unsigned LocalBaseSubmoduleID = Record[1];
6497 if (F.LocalNumSubmodules > 0) {
6498 // Introduce the global -> local mapping for submodules within this
6499 // module.
6500 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
6501
6502 // Introduce the local -> global mapping for submodules within this
6503 // module.
6504 F.SubmoduleRemap.insertOrReplace(
6505 Val: std::make_pair(x&: LocalBaseSubmoduleID,
6506 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
6507
6508 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
6509 }
6510 break;
6511 }
6512
6513 case SUBMODULE_IMPORTS:
6514 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6515 UnresolvedModuleRef Unresolved;
6516 Unresolved.File = &F;
6517 Unresolved.Mod = CurrentModule;
6518 Unresolved.ID = Record[Idx];
6519 Unresolved.Kind = UnresolvedModuleRef::Import;
6520 Unresolved.IsWildcard = false;
6521 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6522 }
6523 break;
6524
6525 case SUBMODULE_AFFECTING_MODULES:
6526 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6527 UnresolvedModuleRef Unresolved;
6528 Unresolved.File = &F;
6529 Unresolved.Mod = CurrentModule;
6530 Unresolved.ID = Record[Idx];
6531 Unresolved.Kind = UnresolvedModuleRef::Affecting;
6532 Unresolved.IsWildcard = false;
6533 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6534 }
6535 break;
6536
6537 case SUBMODULE_EXPORTS:
6538 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6539 UnresolvedModuleRef Unresolved;
6540 Unresolved.File = &F;
6541 Unresolved.Mod = CurrentModule;
6542 Unresolved.ID = Record[Idx];
6543 Unresolved.Kind = UnresolvedModuleRef::Export;
6544 Unresolved.IsWildcard = Record[Idx + 1];
6545 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6546 }
6547
6548 // Once we've loaded the set of exports, there's no reason to keep
6549 // the parsed, unresolved exports around.
6550 CurrentModule->UnresolvedExports.clear();
6551 break;
6552
6553 case SUBMODULE_REQUIRES:
6554 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6555 Target: PP.getTargetInfo());
6556 break;
6557
6558 case SUBMODULE_LINK_LIBRARY:
6559 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6560 CurrentModule->LinkLibraries.push_back(
6561 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6562 break;
6563
6564 case SUBMODULE_CONFIG_MACRO:
6565 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6566 break;
6567
6568 case SUBMODULE_CONFLICT: {
6569 UnresolvedModuleRef Unresolved;
6570 Unresolved.File = &F;
6571 Unresolved.Mod = CurrentModule;
6572 Unresolved.ID = Record[0];
6573 Unresolved.Kind = UnresolvedModuleRef::Conflict;
6574 Unresolved.IsWildcard = false;
6575 Unresolved.String = Blob;
6576 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6577 break;
6578 }
6579
6580 case SUBMODULE_INITIALIZERS: {
6581 if (!ContextObj)
6582 break;
6583 // Standard C++ module has its own way to initialize variables.
6584 if (!F.StandardCXXModule || F.Kind == MK_MainFile) {
6585 SmallVector<GlobalDeclID, 16> Inits;
6586 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6587 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6588 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6589 }
6590 break;
6591 }
6592
6593 case SUBMODULE_EXPORT_AS:
6594 CurrentModule->ExportAsModule = Blob.str();
6595 ModMap.addLinkAsDependency(Mod: CurrentModule);
6596 break;
6597 }
6598 }
6599}
6600
6601/// Parse the record that corresponds to a LangOptions data
6602/// structure.
6603///
6604/// This routine parses the language options from the AST file and then gives
6605/// them to the AST listener if one is set.
6606///
6607/// \returns true if the listener deems the file unacceptable, false otherwise.
6608bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6609 StringRef ModuleFilename, bool Complain,
6610 ASTReaderListener &Listener,
6611 bool AllowCompatibleDifferences) {
6612 LangOptions LangOpts;
6613 unsigned Idx = 0;
6614#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6615 LangOpts.Name = Record[Idx++];
6616#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6617 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6618#include "clang/Basic/LangOptions.def"
6619#define SANITIZER(NAME, ID) \
6620 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6621#include "clang/Basic/Sanitizers.def"
6622
6623 for (unsigned N = Record[Idx++]; N; --N)
6624 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6625
6626 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6627 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6628 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6629
6630 LangOpts.CurrentModule = ReadString(Record, Idx);
6631
6632 // Comment options.
6633 for (unsigned N = Record[Idx++]; N; --N) {
6634 LangOpts.CommentOpts.BlockCommandNames.push_back(
6635 x: ReadString(Record, Idx));
6636 }
6637 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6638
6639 // OpenMP offloading options.
6640 for (unsigned N = Record[Idx++]; N; --N) {
6641 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6642 }
6643
6644 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6645
6646 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6647 AllowCompatibleDifferences);
6648}
6649
6650bool ASTReader::ParseCodeGenOptions(const RecordData &Record,
6651 StringRef ModuleFilename, bool Complain,
6652 ASTReaderListener &Listener,
6653 bool AllowCompatibleDifferences) {
6654 unsigned Idx = 0;
6655 CodeGenOptions CGOpts;
6656 using CK = CodeGenOptions::CompatibilityKind;
6657#define CODEGENOPT(Name, Bits, Default, Compatibility) \
6658 if constexpr (CK::Compatibility != CK::Benign) \
6659 CGOpts.Name = static_cast<unsigned>(Record[Idx++]);
6660#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility) \
6661 if constexpr (CK::Compatibility != CK::Benign) \
6662 CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));
6663#define DEBUGOPT(Name, Bits, Default, Compatibility)
6664#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)
6665#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)
6666#include "clang/Basic/CodeGenOptions.def"
6667
6668 return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,
6669 AllowCompatibleDifferences);
6670}
6671
6672bool ASTReader::ParseTargetOptions(const RecordData &Record,
6673 StringRef ModuleFilename, bool Complain,
6674 ASTReaderListener &Listener,
6675 bool AllowCompatibleDifferences) {
6676 unsigned Idx = 0;
6677 TargetOptions TargetOpts;
6678 TargetOpts.Triple = ReadString(Record, Idx);
6679 TargetOpts.CPU = ReadString(Record, Idx);
6680 TargetOpts.TuneCPU = ReadString(Record, Idx);
6681 TargetOpts.ABI = ReadString(Record, Idx);
6682 for (unsigned N = Record[Idx++]; N; --N) {
6683 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6684 }
6685 for (unsigned N = Record[Idx++]; N; --N) {
6686 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6687 }
6688
6689 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6690 AllowCompatibleDifferences);
6691}
6692
6693bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6694 StringRef ModuleFilename, bool Complain,
6695 ASTReaderListener &Listener) {
6696 DiagnosticOptions DiagOpts;
6697 unsigned Idx = 0;
6698#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6699#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6700 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6701#include "clang/Basic/DiagnosticOptions.def"
6702
6703 for (unsigned N = Record[Idx++]; N; --N)
6704 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6705 for (unsigned N = Record[Idx++]; N; --N)
6706 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6707
6708 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6709}
6710
6711bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6712 ASTReaderListener &Listener) {
6713 FileSystemOptions FSOpts;
6714 unsigned Idx = 0;
6715 FSOpts.WorkingDir = ReadString(Record, Idx);
6716 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6717}
6718
6719bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6720 StringRef ModuleFilename,
6721 bool Complain,
6722 ASTReaderListener &Listener) {
6723 HeaderSearchOptions HSOpts;
6724 unsigned Idx = 0;
6725 HSOpts.Sysroot = ReadString(Record, Idx);
6726
6727 HSOpts.ResourceDir = ReadString(Record, Idx);
6728 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6729 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6730 HSOpts.DisableModuleHash = Record[Idx++];
6731 HSOpts.ImplicitModuleMaps = Record[Idx++];
6732 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6733 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6734 HSOpts.UseBuiltinIncludes = Record[Idx++];
6735 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6736 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6737 HSOpts.UseLibcxx = Record[Idx++];
6738 std::string ContextHash = ReadString(Record, Idx);
6739
6740 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename, ContextHash,
6741 Complain);
6742}
6743
6744bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6745 ASTReaderListener &Listener) {
6746 HeaderSearchOptions HSOpts;
6747 unsigned Idx = 0;
6748
6749 // Include entries.
6750 for (unsigned N = Record[Idx++]; N; --N) {
6751 std::string Path = ReadString(Record, Idx);
6752 frontend::IncludeDirGroup Group
6753 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6754 bool IsFramework = Record[Idx++];
6755 bool IgnoreSysRoot = Record[Idx++];
6756 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6757 args&: IgnoreSysRoot);
6758 }
6759
6760 // System header prefixes.
6761 for (unsigned N = Record[Idx++]; N; --N) {
6762 std::string Prefix = ReadString(Record, Idx);
6763 bool IsSystemHeader = Record[Idx++];
6764 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6765 }
6766
6767 // VFS overlay files.
6768 for (unsigned N = Record[Idx++]; N; --N) {
6769 std::string VFSOverlayFile = ReadString(Record, Idx);
6770 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6771 }
6772
6773 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6774}
6775
6776bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6777 StringRef ModuleFilename,
6778 bool Complain,
6779 ASTReaderListener &Listener,
6780 std::string &SuggestedPredefines) {
6781 PreprocessorOptions PPOpts;
6782 unsigned Idx = 0;
6783
6784 // Macro definitions/undefs
6785 bool ReadMacros = Record[Idx++];
6786 if (ReadMacros) {
6787 for (unsigned N = Record[Idx++]; N; --N) {
6788 std::string Macro = ReadString(Record, Idx);
6789 bool IsUndef = Record[Idx++];
6790 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6791 }
6792 }
6793
6794 // Includes
6795 for (unsigned N = Record[Idx++]; N; --N) {
6796 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6797 }
6798
6799 // Macro Includes
6800 for (unsigned N = Record[Idx++]; N; --N) {
6801 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6802 }
6803
6804 PPOpts.UsePredefines = Record[Idx++];
6805 PPOpts.DetailedRecord = Record[Idx++];
6806 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6807 PPOpts.ObjCXXARCStandardLibrary =
6808 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6809 SuggestedPredefines.clear();
6810 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6811 Complain, SuggestedPredefines);
6812}
6813
6814std::pair<ModuleFile *, unsigned>
6815ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6816 GlobalPreprocessedEntityMapType::iterator
6817 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6818 assert(I != GlobalPreprocessedEntityMap.end() &&
6819 "Corrupted global preprocessed entity map");
6820 ModuleFile *M = I->second;
6821 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6822 return std::make_pair(x&: M, y&: LocalIndex);
6823}
6824
6825llvm::iterator_range<PreprocessingRecord::iterator>
6826ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6827 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6828 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6829 count: Mod.NumPreprocessedEntities);
6830
6831 return llvm::make_range(x: PreprocessingRecord::iterator(),
6832 y: PreprocessingRecord::iterator());
6833}
6834
6835bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6836 unsigned int ClientLoadCapabilities) {
6837 return ClientLoadCapabilities & ARR_OutOfDate &&
6838 !getModuleManager()
6839 .getModuleCache()
6840 .getInMemoryModuleCache()
6841 .isPCMFinal(Filename: ModuleFileName);
6842}
6843
6844llvm::iterator_range<ASTReader::ModuleDeclIterator>
6845ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6846 return llvm::make_range(
6847 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6848 y: ModuleDeclIterator(this, &Mod,
6849 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6850}
6851
6852SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6853 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6854 assert(I != GlobalSkippedRangeMap.end() &&
6855 "Corrupted global skipped range map");
6856 ModuleFile *M = I->second;
6857 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6858 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6859 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6860 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6861 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6862 assert(Range.isValid());
6863 return Range;
6864}
6865
6866unsigned
6867ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {
6868 unsigned ModuleFileIndex = ID >> 32;
6869 assert(ModuleFileIndex && "not translating loaded MacroID?");
6870 assert(getModuleManager().size() > ModuleFileIndex - 1);
6871 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
6872
6873 ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(N: 32);
6874 return MF.BasePreprocessedEntityID + ID;
6875}
6876
6877PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6878 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6879 ModuleFile &M = *PPInfo.first;
6880 unsigned LocalIndex = PPInfo.second;
6881 PreprocessedEntityID PPID =
6882 (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;
6883 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6884
6885 if (!PP.getPreprocessingRecord()) {
6886 Error(Msg: "no preprocessing record");
6887 return nullptr;
6888 }
6889
6890 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6891 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6892 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6893 Error(Err: std::move(Err));
6894 return nullptr;
6895 }
6896
6897 Expected<llvm::BitstreamEntry> MaybeEntry =
6898 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6899 if (!MaybeEntry) {
6900 Error(Err: MaybeEntry.takeError());
6901 return nullptr;
6902 }
6903 llvm::BitstreamEntry Entry = MaybeEntry.get();
6904
6905 if (Entry.Kind != llvm::BitstreamEntry::Record)
6906 return nullptr;
6907
6908 // Read the record.
6909 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6910 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6911 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6912 StringRef Blob;
6913 RecordData Record;
6914 Expected<unsigned> MaybeRecType =
6915 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6916 if (!MaybeRecType) {
6917 Error(Err: MaybeRecType.takeError());
6918 return nullptr;
6919 }
6920 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6921 case PPD_MACRO_EXPANSION: {
6922 bool isBuiltin = Record[0];
6923 IdentifierInfo *Name = nullptr;
6924 MacroDefinitionRecord *Def = nullptr;
6925 if (isBuiltin)
6926 Name = getLocalIdentifier(M, LocalID: Record[1]);
6927 else {
6928 PreprocessedEntityID GlobalID =
6929 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6930 unsigned Index = translatePreprocessedEntityIDToIndex(ID: GlobalID);
6931 Def =
6932 cast<MacroDefinitionRecord>(Val: PPRec.getLoadedPreprocessedEntity(Index));
6933 }
6934
6935 MacroExpansion *ME;
6936 if (isBuiltin)
6937 ME = new (PPRec) MacroExpansion(Name, Range);
6938 else
6939 ME = new (PPRec) MacroExpansion(Def, Range);
6940
6941 return ME;
6942 }
6943
6944 case PPD_MACRO_DEFINITION: {
6945 // Decode the identifier info and then check again; if the macro is
6946 // still defined and associated with the identifier,
6947 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6948 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6949
6950 if (DeserializationListener)
6951 DeserializationListener->MacroDefinitionRead(PPID, MD);
6952
6953 return MD;
6954 }
6955
6956 case PPD_INCLUSION_DIRECTIVE: {
6957 const char *FullFileNameStart = Blob.data() + Record[0];
6958 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6959 OptionalFileEntryRef File;
6960 if (!FullFileName.empty())
6961 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6962
6963 // FIXME: Stable encoding
6964 InclusionDirective::InclusionKind Kind
6965 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6966 InclusionDirective *ID
6967 = new (PPRec) InclusionDirective(PPRec, Kind,
6968 StringRef(Blob.data(), Record[0]),
6969 Record[1], Record[3],
6970 File,
6971 Range);
6972 return ID;
6973 }
6974 }
6975
6976 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6977}
6978
6979/// Find the next module that contains entities and return the ID
6980/// of the first entry.
6981///
6982/// \param SLocMapI points at a chunk of a module that contains no
6983/// preprocessed entities or the entities it contains are not the ones we are
6984/// looking for.
6985unsigned ASTReader::findNextPreprocessedEntity(
6986 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6987 ++SLocMapI;
6988 for (GlobalSLocOffsetMapType::const_iterator
6989 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6990 ModuleFile &M = *SLocMapI->second;
6991 if (M.NumPreprocessedEntities)
6992 return M.BasePreprocessedEntityID;
6993 }
6994
6995 return getTotalNumPreprocessedEntities();
6996}
6997
6998namespace {
6999
7000struct PPEntityComp {
7001 const ASTReader &Reader;
7002 ModuleFile &M;
7003
7004 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
7005
7006 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
7007 SourceLocation LHS = getLoc(PPE: L);
7008 SourceLocation RHS = getLoc(PPE: R);
7009 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7010 }
7011
7012 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
7013 SourceLocation LHS = getLoc(PPE: L);
7014 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7015 }
7016
7017 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
7018 SourceLocation RHS = getLoc(PPE: R);
7019 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7020 }
7021
7022 SourceLocation getLoc(const PPEntityOffset &PPE) const {
7023 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
7024 }
7025};
7026
7027} // namespace
7028
7029unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,
7030 bool EndsAfter) const {
7031 if (SourceMgr.isLocalSourceLocation(Loc))
7032 return getTotalNumPreprocessedEntities();
7033
7034 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
7035 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
7036 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
7037 "Corrupted global sloc offset map");
7038
7039 if (SLocMapI->second->NumPreprocessedEntities == 0)
7040 return findNextPreprocessedEntity(SLocMapI);
7041
7042 ModuleFile &M = *SLocMapI->second;
7043
7044 using pp_iterator = const PPEntityOffset *;
7045
7046 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
7047 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
7048
7049 size_t Count = M.NumPreprocessedEntities;
7050 size_t Half;
7051 pp_iterator First = pp_begin;
7052 pp_iterator PPI;
7053
7054 if (EndsAfter) {
7055 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
7056 comp: PPEntityComp(*this, M));
7057 } else {
7058 // Do a binary search manually instead of using std::lower_bound because
7059 // The end locations of entities may be unordered (when a macro expansion
7060 // is inside another macro argument), but for this case it is not important
7061 // whether we get the first macro expansion or its containing macro.
7062 while (Count > 0) {
7063 Half = Count / 2;
7064 PPI = First;
7065 std::advance(i&: PPI, n: Half);
7066 if (SourceMgr.isBeforeInTranslationUnit(
7067 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
7068 First = PPI;
7069 ++First;
7070 Count = Count - Half - 1;
7071 } else
7072 Count = Half;
7073 }
7074 }
7075
7076 if (PPI == pp_end)
7077 return findNextPreprocessedEntity(SLocMapI);
7078
7079 return M.BasePreprocessedEntityID + (PPI - pp_begin);
7080}
7081
7082/// Returns a pair of [Begin, End) indices of preallocated
7083/// preprocessed entities that \arg Range encompasses.
7084std::pair<unsigned, unsigned>
7085 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
7086 if (Range.isInvalid())
7087 return std::make_pair(x: 0,y: 0);
7088 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
7089
7090 unsigned BeginID = findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
7091 unsigned EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
7092 return std::make_pair(x&: BeginID, y&: EndID);
7093}
7094
7095/// Optionally returns true or false if the preallocated preprocessed
7096/// entity with index \arg Index came from file \arg FID.
7097std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
7098 FileID FID) {
7099 if (FID.isInvalid())
7100 return false;
7101
7102 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
7103 ModuleFile &M = *PPInfo.first;
7104 unsigned LocalIndex = PPInfo.second;
7105 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
7106
7107 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
7108 if (Loc.isInvalid())
7109 return false;
7110
7111 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
7112 return true;
7113 else
7114 return false;
7115}
7116
7117namespace {
7118
7119 /// Visitor used to search for information about a header file.
7120 class HeaderFileInfoVisitor {
7121 FileEntryRef FE;
7122 std::optional<HeaderFileInfo> HFI;
7123
7124 public:
7125 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
7126
7127 bool operator()(ModuleFile &M) {
7128 HeaderFileInfoLookupTable *Table
7129 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
7130 if (!Table)
7131 return false;
7132
7133 // Look in the on-disk hash table for an entry for this file name.
7134 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
7135 if (Pos == Table->end())
7136 return false;
7137
7138 HFI = *Pos;
7139 return true;
7140 }
7141
7142 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
7143 };
7144
7145} // namespace
7146
7147HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
7148 HeaderFileInfoVisitor Visitor(FE);
7149 ModuleMgr.visit(Visitor);
7150 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
7151 return *HFI;
7152
7153 return HeaderFileInfo();
7154}
7155
7156void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
7157 using DiagState = DiagnosticsEngine::DiagState;
7158 SmallVector<DiagState *, 32> DiagStates;
7159
7160 for (ModuleFile &F : ModuleMgr) {
7161 unsigned Idx = 0;
7162 auto &Record = F.PragmaDiagMappings;
7163 if (Record.empty())
7164 continue;
7165
7166 DiagStates.clear();
7167
7168 auto ReadDiagState = [&](const DiagState &BasedOn,
7169 bool IncludeNonPragmaStates) {
7170 unsigned BackrefID = Record[Idx++];
7171 if (BackrefID != 0)
7172 return DiagStates[BackrefID - 1];
7173
7174 // A new DiagState was created here.
7175 Diag.DiagStates.push_back(x: BasedOn);
7176 DiagState *NewState = &Diag.DiagStates.back();
7177 DiagStates.push_back(Elt: NewState);
7178 unsigned Size = Record[Idx++];
7179 assert(Idx + Size * 2 <= Record.size() &&
7180 "Invalid data, not enough diag/map pairs");
7181 while (Size--) {
7182 unsigned DiagID = Record[Idx++];
7183 DiagnosticMapping NewMapping =
7184 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
7185 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
7186 continue;
7187
7188 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
7189
7190 // If this mapping was specified as a warning but the severity was
7191 // upgraded due to diagnostic settings, simulate the current diagnostic
7192 // settings (and use a warning).
7193 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
7194 NewMapping.setSeverity(diag::Severity::Warning);
7195 NewMapping.setUpgradedFromWarning(false);
7196 }
7197
7198 Mapping = NewMapping;
7199 }
7200 return NewState;
7201 };
7202
7203 // Read the first state.
7204 DiagState *FirstState;
7205 if (F.Kind == MK_ImplicitModule) {
7206 // Implicitly-built modules are reused with different diagnostic
7207 // settings. Use the initial diagnostic state from Diag to simulate this
7208 // compilation's diagnostic settings.
7209 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
7210 DiagStates.push_back(Elt: FirstState);
7211
7212 // Skip the initial diagnostic state from the serialized module.
7213 assert(Record[1] == 0 &&
7214 "Invalid data, unexpected backref in initial state");
7215 Idx = 3 + Record[2] * 2;
7216 assert(Idx < Record.size() &&
7217 "Invalid data, not enough state change pairs in initial state");
7218 } else if (F.isModule()) {
7219 // For an explicit module, preserve the flags from the module build
7220 // command line (-w, -Weverything, -Werror, ...) along with any explicit
7221 // -Wblah flags.
7222 unsigned Flags = Record[Idx++];
7223 DiagState Initial(*Diag.getDiagnosticIDs());
7224 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
7225 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
7226 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
7227 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
7228 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
7229 Initial.ExtBehavior = (diag::Severity)Flags;
7230 FirstState = ReadDiagState(Initial, true);
7231
7232 assert(F.OriginalSourceFileID.isValid());
7233
7234 // Set up the root buffer of the module to start with the initial
7235 // diagnostic state of the module itself, to cover files that contain no
7236 // explicit transitions (for which we did not serialize anything).
7237 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
7238 .StateTransitions.push_back(Elt: {FirstState, 0});
7239 } else {
7240 // For prefix ASTs, start with whatever the user configured on the
7241 // command line.
7242 Idx++; // Skip flags.
7243 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
7244 }
7245
7246 // Read the state transitions.
7247 unsigned NumLocations = Record[Idx++];
7248 while (NumLocations--) {
7249 assert(Idx < Record.size() &&
7250 "Invalid data, missing pragma diagnostic states");
7251 FileID FID = ReadFileID(F, Record, Idx);
7252 assert(FID.isValid() && "invalid FileID for transition");
7253 unsigned Transitions = Record[Idx++];
7254
7255 // Note that we don't need to set up Parent/ParentOffset here, because
7256 // we won't be changing the diagnostic state within imported FileIDs
7257 // (other than perhaps appending to the main source file, which has no
7258 // parent).
7259 auto &F = Diag.DiagStatesByLoc.Files[FID];
7260 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
7261 for (unsigned I = 0; I != Transitions; ++I) {
7262 unsigned Offset = Record[Idx++];
7263 auto *State = ReadDiagState(*FirstState, false);
7264 F.StateTransitions.push_back(Elt: {State, Offset});
7265 }
7266 }
7267
7268 // Read the final state.
7269 assert(Idx < Record.size() &&
7270 "Invalid data, missing final pragma diagnostic state");
7271 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
7272 auto *CurState = ReadDiagState(*FirstState, false);
7273
7274 if (!F.isModule()) {
7275 Diag.DiagStatesByLoc.CurDiagState = CurState;
7276 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
7277
7278 // Preserve the property that the imaginary root file describes the
7279 // current state.
7280 FileID NullFile;
7281 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7282 if (T.empty())
7283 T.push_back(Elt: {CurState, 0});
7284 else
7285 T[0].State = CurState;
7286 }
7287
7288 // Don't try to read these mappings again.
7289 Record.clear();
7290 }
7291}
7292
7293/// Get the correct cursor and offset for loading a type.
7294ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7295 auto [M, Index] = translateTypeIDToIndex(ID);
7296 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7297 M->DeclsBlockStartOffset);
7298}
7299
7300static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7301 switch (code) {
7302#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7303 case TYPE_##CODE_ID: return Type::CLASS_ID;
7304#include "clang/Serialization/TypeBitCodes.def"
7305 default:
7306 return std::nullopt;
7307 }
7308}
7309
7310/// Read and return the type with the given index..
7311///
7312/// The index is the type ID, shifted and minus the number of predefs. This
7313/// routine actually reads the record corresponding to the type at the given
7314/// location. It is a helper routine for GetType, which deals with reading type
7315/// IDs.
7316QualType ASTReader::readTypeRecord(TypeID ID) {
7317 assert(ContextObj && "reading type with no AST context");
7318 ASTContext &Context = *ContextObj;
7319 RecordLocation Loc = TypeCursorForIndex(ID);
7320 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7321
7322 // Keep track of where we are in the stream, then jump back there
7323 // after reading this type.
7324 SavedStreamPosition SavedPosition(DeclsCursor);
7325
7326 ReadingKindTracker ReadingKind(Read_Type, *this);
7327
7328 // Note that we are loading a type record.
7329 Deserializing AType(this);
7330
7331 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7332 Error(Err: std::move(Err));
7333 return QualType();
7334 }
7335 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7336 if (!RawCode) {
7337 Error(Err: RawCode.takeError());
7338 return QualType();
7339 }
7340
7341 ASTRecordReader Record(*this, *Loc.F);
7342 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7343 if (!Code) {
7344 Error(Err: Code.takeError());
7345 return QualType();
7346 }
7347 if (Code.get() == TYPE_EXT_QUAL) {
7348 QualType baseType = Record.readQualType();
7349 Qualifiers quals = Record.readQualifiers();
7350 return Context.getQualifiedType(T: baseType, Qs: quals);
7351 }
7352
7353 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7354 if (!maybeClass) {
7355 Error(Msg: "Unexpected code for type");
7356 return QualType();
7357 }
7358
7359 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7360 return TypeReader.read(kind: *maybeClass);
7361}
7362
7363namespace clang {
7364
7365class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7366 ASTRecordReader &Reader;
7367
7368 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7369 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7370
7371 TypeSourceInfo *GetTypeSourceInfo() {
7372 return Reader.readTypeSourceInfo();
7373 }
7374
7375 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7376 return Reader.readNestedNameSpecifierLoc();
7377 }
7378
7379 Attr *ReadAttr() {
7380 return Reader.readAttr();
7381 }
7382
7383public:
7384 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7385
7386 // We want compile-time assurance that we've enumerated all of
7387 // these, so unfortunately we have to declare them first, then
7388 // define them out-of-line.
7389#define ABSTRACT_TYPELOC(CLASS, PARENT)
7390#define TYPELOC(CLASS, PARENT) \
7391 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7392#include "clang/AST/TypeLocNodes.def"
7393
7394 void VisitFunctionTypeLoc(FunctionTypeLoc);
7395 void VisitArrayTypeLoc(ArrayTypeLoc);
7396 void VisitTagTypeLoc(TagTypeLoc TL);
7397};
7398
7399} // namespace clang
7400
7401void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7402 // nothing to do
7403}
7404
7405void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7406 TL.setBuiltinLoc(readSourceLocation());
7407 if (TL.needsExtraLocalData()) {
7408 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7409 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7410 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7411 TL.setModeAttr(Reader.readInt());
7412 }
7413}
7414
7415void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7416 TL.setNameLoc(readSourceLocation());
7417}
7418
7419void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7420 TL.setStarLoc(readSourceLocation());
7421}
7422
7423void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7424 // nothing to do
7425}
7426
7427void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7428 // nothing to do
7429}
7430
7431void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7432 // nothing to do
7433}
7434
7435void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7436 TL.setExpansionLoc(readSourceLocation());
7437}
7438
7439void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7440 TL.setCaretLoc(readSourceLocation());
7441}
7442
7443void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7444 TL.setAmpLoc(readSourceLocation());
7445}
7446
7447void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7448 TL.setAmpAmpLoc(readSourceLocation());
7449}
7450
7451void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7452 TL.setStarLoc(readSourceLocation());
7453 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7454}
7455
7456void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7457 TL.setLBracketLoc(readSourceLocation());
7458 TL.setRBracketLoc(readSourceLocation());
7459 if (Reader.readBool())
7460 TL.setSizeExpr(Reader.readExpr());
7461 else
7462 TL.setSizeExpr(nullptr);
7463}
7464
7465void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7466 VisitArrayTypeLoc(TL);
7467}
7468
7469void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7470 VisitArrayTypeLoc(TL);
7471}
7472
7473void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7474 VisitArrayTypeLoc(TL);
7475}
7476
7477void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7478 DependentSizedArrayTypeLoc TL) {
7479 VisitArrayTypeLoc(TL);
7480}
7481
7482void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7483 DependentAddressSpaceTypeLoc TL) {
7484
7485 TL.setAttrNameLoc(readSourceLocation());
7486 TL.setAttrOperandParensRange(readSourceRange());
7487 TL.setAttrExprOperand(Reader.readExpr());
7488}
7489
7490void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7491 DependentSizedExtVectorTypeLoc TL) {
7492 TL.setNameLoc(readSourceLocation());
7493}
7494
7495void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7496 TL.setNameLoc(readSourceLocation());
7497}
7498
7499void TypeLocReader::VisitDependentVectorTypeLoc(
7500 DependentVectorTypeLoc TL) {
7501 TL.setNameLoc(readSourceLocation());
7502}
7503
7504void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7505 TL.setNameLoc(readSourceLocation());
7506}
7507
7508void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7509 TL.setAttrNameLoc(readSourceLocation());
7510 TL.setAttrOperandParensRange(readSourceRange());
7511 TL.setAttrRowOperand(Reader.readExpr());
7512 TL.setAttrColumnOperand(Reader.readExpr());
7513}
7514
7515void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7516 DependentSizedMatrixTypeLoc TL) {
7517 TL.setAttrNameLoc(readSourceLocation());
7518 TL.setAttrOperandParensRange(readSourceRange());
7519 TL.setAttrRowOperand(Reader.readExpr());
7520 TL.setAttrColumnOperand(Reader.readExpr());
7521}
7522
7523void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7524 TL.setLocalRangeBegin(readSourceLocation());
7525 TL.setLParenLoc(readSourceLocation());
7526 TL.setRParenLoc(readSourceLocation());
7527 TL.setExceptionSpecRange(readSourceRange());
7528 TL.setLocalRangeEnd(readSourceLocation());
7529 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7530 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7531 }
7532}
7533
7534void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7535 VisitFunctionTypeLoc(TL);
7536}
7537
7538void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7539 VisitFunctionTypeLoc(TL);
7540}
7541
7542void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7543 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7544 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7545 SourceLocation NameLoc = readSourceLocation();
7546 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7547}
7548
7549void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7550 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7551 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7552 SourceLocation NameLoc = readSourceLocation();
7553 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7554}
7555
7556void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7557 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7558 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7559 SourceLocation NameLoc = readSourceLocation();
7560 TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);
7561}
7562
7563void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7564 TL.setTypeofLoc(readSourceLocation());
7565 TL.setLParenLoc(readSourceLocation());
7566 TL.setRParenLoc(readSourceLocation());
7567}
7568
7569void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7570 TL.setTypeofLoc(readSourceLocation());
7571 TL.setLParenLoc(readSourceLocation());
7572 TL.setRParenLoc(readSourceLocation());
7573 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7574}
7575
7576void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7577 TL.setDecltypeLoc(readSourceLocation());
7578 TL.setRParenLoc(readSourceLocation());
7579}
7580
7581void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7582 TL.setEllipsisLoc(readSourceLocation());
7583}
7584
7585void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7586 TL.setKWLoc(readSourceLocation());
7587 TL.setLParenLoc(readSourceLocation());
7588 TL.setRParenLoc(readSourceLocation());
7589 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7590}
7591
7592ConceptReference *ASTRecordReader::readConceptReference() {
7593 auto NNS = readNestedNameSpecifierLoc();
7594 auto TemplateKWLoc = readSourceLocation();
7595 auto ConceptNameLoc = readDeclarationNameInfo();
7596 auto FoundDecl = readDeclAs<NamedDecl>();
7597 auto NamedConcept = readDeclAs<ConceptDecl>();
7598 auto *CR = ConceptReference::Create(
7599 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7600 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7601 return CR;
7602}
7603
7604void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7605 TL.setNameLoc(readSourceLocation());
7606 if (Reader.readBool())
7607 TL.setConceptReference(Reader.readConceptReference());
7608 if (Reader.readBool())
7609 TL.setRParenLoc(readSourceLocation());
7610}
7611
7612void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7613 DeducedTemplateSpecializationTypeLoc TL) {
7614 TL.setElaboratedKeywordLoc(readSourceLocation());
7615 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7616 TL.setTemplateNameLoc(readSourceLocation());
7617}
7618
7619void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {
7620 TL.setElaboratedKeywordLoc(readSourceLocation());
7621 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7622 TL.setNameLoc(readSourceLocation());
7623}
7624
7625void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7626 VisitTagTypeLoc(TL);
7627}
7628
7629void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7630 VisitTagTypeLoc(TL);
7631}
7632
7633void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }
7634
7635void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7636 TL.setAttr(ReadAttr());
7637}
7638
7639void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7640 // Nothing to do
7641}
7642
7643void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7644 // Nothing to do.
7645}
7646
7647void TypeLocReader::VisitOverflowBehaviorTypeLoc(OverflowBehaviorTypeLoc TL) {
7648 TL.setAttrLoc(readSourceLocation());
7649}
7650
7651void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7652 HLSLAttributedResourceTypeLoc TL) {
7653 // Nothing to do.
7654}
7655
7656void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7657 // Nothing to do.
7658}
7659
7660void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7661 TL.setNameLoc(readSourceLocation());
7662}
7663
7664void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7665 SubstTemplateTypeParmTypeLoc TL) {
7666 TL.setNameLoc(readSourceLocation());
7667}
7668
7669void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7670 SubstTemplateTypeParmPackTypeLoc TL) {
7671 TL.setNameLoc(readSourceLocation());
7672}
7673
7674void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(
7675 SubstBuiltinTemplatePackTypeLoc TL) {
7676 TL.setNameLoc(readSourceLocation());
7677}
7678
7679void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7680 TemplateSpecializationTypeLoc TL) {
7681 SourceLocation ElaboratedKeywordLoc = readSourceLocation();
7682 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();
7683 SourceLocation TemplateKeywordLoc = readSourceLocation();
7684 SourceLocation NameLoc = readSourceLocation();
7685 SourceLocation LAngleLoc = readSourceLocation();
7686 SourceLocation RAngleLoc = readSourceLocation();
7687 TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,
7688 LAngleLoc, RAngleLoc);
7689 MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();
7690 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7691 Args[I] = Reader.readTemplateArgumentLocInfo(
7692 Kind: TL.getTypePtr()->template_arguments()[I].getKind());
7693}
7694
7695void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7696 TL.setLParenLoc(readSourceLocation());
7697 TL.setRParenLoc(readSourceLocation());
7698}
7699
7700void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7701 TL.setElaboratedKeywordLoc(readSourceLocation());
7702 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7703 TL.setNameLoc(readSourceLocation());
7704}
7705
7706void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7707 TL.setEllipsisLoc(readSourceLocation());
7708}
7709
7710void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7711 TL.setNameLoc(readSourceLocation());
7712 TL.setNameEndLoc(readSourceLocation());
7713}
7714
7715void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7716 if (TL.getNumProtocols()) {
7717 TL.setProtocolLAngleLoc(readSourceLocation());
7718 TL.setProtocolRAngleLoc(readSourceLocation());
7719 }
7720 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7721 TL.setProtocolLoc(i, Loc: readSourceLocation());
7722}
7723
7724void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7725 TL.setHasBaseTypeAsWritten(Reader.readBool());
7726 TL.setTypeArgsLAngleLoc(readSourceLocation());
7727 TL.setTypeArgsRAngleLoc(readSourceLocation());
7728 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7729 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7730 TL.setProtocolLAngleLoc(readSourceLocation());
7731 TL.setProtocolRAngleLoc(readSourceLocation());
7732 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7733 TL.setProtocolLoc(i, Loc: readSourceLocation());
7734}
7735
7736void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7737 TL.setStarLoc(readSourceLocation());
7738}
7739
7740void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7741 TL.setKWLoc(readSourceLocation());
7742 TL.setLParenLoc(readSourceLocation());
7743 TL.setRParenLoc(readSourceLocation());
7744}
7745
7746void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7747 TL.setKWLoc(readSourceLocation());
7748}
7749
7750void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7751 TL.setNameLoc(readSourceLocation());
7752}
7753
7754void TypeLocReader::VisitDependentBitIntTypeLoc(
7755 clang::DependentBitIntTypeLoc TL) {
7756 TL.setNameLoc(readSourceLocation());
7757}
7758
7759void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {
7760 // Nothing to do.
7761}
7762
7763void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7764 TypeLocReader TLR(*this);
7765 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7766 TLR.Visit(TyLoc: TL);
7767}
7768
7769TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7770 QualType InfoTy = readType();
7771 if (InfoTy.isNull())
7772 return nullptr;
7773
7774 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7775 readTypeLoc(TL: TInfo->getTypeLoc());
7776 return TInfo;
7777}
7778
7779static unsigned getIndexForTypeID(serialization::TypeID ID) {
7780 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7781}
7782
7783static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7784 return ID >> 32;
7785}
7786
7787static bool isPredefinedType(serialization::TypeID ID) {
7788 // We don't need to erase the higher bits since if these bits are not 0,
7789 // it must be larger than NUM_PREDEF_TYPE_IDS.
7790 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7791}
7792
7793std::pair<ModuleFile *, unsigned>
7794ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7795 assert(!isPredefinedType(ID) &&
7796 "Predefined type shouldn't be in TypesLoaded");
7797 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7798 assert(ModuleFileIndex && "Untranslated Local Decl?");
7799
7800 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7801 assert(OwningModuleFile &&
7802 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7803
7804 return {OwningModuleFile,
7805 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7806}
7807
7808QualType ASTReader::GetType(TypeID ID) {
7809 assert(ContextObj && "reading type with no AST context");
7810 ASTContext &Context = *ContextObj;
7811
7812 unsigned FastQuals = ID & Qualifiers::FastMask;
7813
7814 if (isPredefinedType(ID)) {
7815 QualType T;
7816 unsigned Index = getIndexForTypeID(ID);
7817 switch ((PredefinedTypeIDs)Index) {
7818 case PREDEF_TYPE_LAST_ID:
7819 // We should never use this one.
7820 llvm_unreachable("Invalid predefined type");
7821 break;
7822 case PREDEF_TYPE_NULL_ID:
7823 return QualType();
7824 case PREDEF_TYPE_VOID_ID:
7825 T = Context.VoidTy;
7826 break;
7827 case PREDEF_TYPE_BOOL_ID:
7828 T = Context.BoolTy;
7829 break;
7830 case PREDEF_TYPE_CHAR_U_ID:
7831 case PREDEF_TYPE_CHAR_S_ID:
7832 // FIXME: Check that the signedness of CharTy is correct!
7833 T = Context.CharTy;
7834 break;
7835 case PREDEF_TYPE_UCHAR_ID:
7836 T = Context.UnsignedCharTy;
7837 break;
7838 case PREDEF_TYPE_USHORT_ID:
7839 T = Context.UnsignedShortTy;
7840 break;
7841 case PREDEF_TYPE_UINT_ID:
7842 T = Context.UnsignedIntTy;
7843 break;
7844 case PREDEF_TYPE_ULONG_ID:
7845 T = Context.UnsignedLongTy;
7846 break;
7847 case PREDEF_TYPE_ULONGLONG_ID:
7848 T = Context.UnsignedLongLongTy;
7849 break;
7850 case PREDEF_TYPE_UINT128_ID:
7851 T = Context.UnsignedInt128Ty;
7852 break;
7853 case PREDEF_TYPE_SCHAR_ID:
7854 T = Context.SignedCharTy;
7855 break;
7856 case PREDEF_TYPE_WCHAR_ID:
7857 T = Context.WCharTy;
7858 break;
7859 case PREDEF_TYPE_SHORT_ID:
7860 T = Context.ShortTy;
7861 break;
7862 case PREDEF_TYPE_INT_ID:
7863 T = Context.IntTy;
7864 break;
7865 case PREDEF_TYPE_LONG_ID:
7866 T = Context.LongTy;
7867 break;
7868 case PREDEF_TYPE_LONGLONG_ID:
7869 T = Context.LongLongTy;
7870 break;
7871 case PREDEF_TYPE_INT128_ID:
7872 T = Context.Int128Ty;
7873 break;
7874 case PREDEF_TYPE_BFLOAT16_ID:
7875 T = Context.BFloat16Ty;
7876 break;
7877 case PREDEF_TYPE_HALF_ID:
7878 T = Context.HalfTy;
7879 break;
7880 case PREDEF_TYPE_FLOAT_ID:
7881 T = Context.FloatTy;
7882 break;
7883 case PREDEF_TYPE_DOUBLE_ID:
7884 T = Context.DoubleTy;
7885 break;
7886 case PREDEF_TYPE_LONGDOUBLE_ID:
7887 T = Context.LongDoubleTy;
7888 break;
7889 case PREDEF_TYPE_SHORT_ACCUM_ID:
7890 T = Context.ShortAccumTy;
7891 break;
7892 case PREDEF_TYPE_ACCUM_ID:
7893 T = Context.AccumTy;
7894 break;
7895 case PREDEF_TYPE_LONG_ACCUM_ID:
7896 T = Context.LongAccumTy;
7897 break;
7898 case PREDEF_TYPE_USHORT_ACCUM_ID:
7899 T = Context.UnsignedShortAccumTy;
7900 break;
7901 case PREDEF_TYPE_UACCUM_ID:
7902 T = Context.UnsignedAccumTy;
7903 break;
7904 case PREDEF_TYPE_ULONG_ACCUM_ID:
7905 T = Context.UnsignedLongAccumTy;
7906 break;
7907 case PREDEF_TYPE_SHORT_FRACT_ID:
7908 T = Context.ShortFractTy;
7909 break;
7910 case PREDEF_TYPE_FRACT_ID:
7911 T = Context.FractTy;
7912 break;
7913 case PREDEF_TYPE_LONG_FRACT_ID:
7914 T = Context.LongFractTy;
7915 break;
7916 case PREDEF_TYPE_USHORT_FRACT_ID:
7917 T = Context.UnsignedShortFractTy;
7918 break;
7919 case PREDEF_TYPE_UFRACT_ID:
7920 T = Context.UnsignedFractTy;
7921 break;
7922 case PREDEF_TYPE_ULONG_FRACT_ID:
7923 T = Context.UnsignedLongFractTy;
7924 break;
7925 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7926 T = Context.SatShortAccumTy;
7927 break;
7928 case PREDEF_TYPE_SAT_ACCUM_ID:
7929 T = Context.SatAccumTy;
7930 break;
7931 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7932 T = Context.SatLongAccumTy;
7933 break;
7934 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7935 T = Context.SatUnsignedShortAccumTy;
7936 break;
7937 case PREDEF_TYPE_SAT_UACCUM_ID:
7938 T = Context.SatUnsignedAccumTy;
7939 break;
7940 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7941 T = Context.SatUnsignedLongAccumTy;
7942 break;
7943 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7944 T = Context.SatShortFractTy;
7945 break;
7946 case PREDEF_TYPE_SAT_FRACT_ID:
7947 T = Context.SatFractTy;
7948 break;
7949 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7950 T = Context.SatLongFractTy;
7951 break;
7952 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7953 T = Context.SatUnsignedShortFractTy;
7954 break;
7955 case PREDEF_TYPE_SAT_UFRACT_ID:
7956 T = Context.SatUnsignedFractTy;
7957 break;
7958 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7959 T = Context.SatUnsignedLongFractTy;
7960 break;
7961 case PREDEF_TYPE_FLOAT16_ID:
7962 T = Context.Float16Ty;
7963 break;
7964 case PREDEF_TYPE_FLOAT128_ID:
7965 T = Context.Float128Ty;
7966 break;
7967 case PREDEF_TYPE_IBM128_ID:
7968 T = Context.Ibm128Ty;
7969 break;
7970 case PREDEF_TYPE_OVERLOAD_ID:
7971 T = Context.OverloadTy;
7972 break;
7973 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
7974 T = Context.UnresolvedTemplateTy;
7975 break;
7976 case PREDEF_TYPE_BOUND_MEMBER:
7977 T = Context.BoundMemberTy;
7978 break;
7979 case PREDEF_TYPE_PSEUDO_OBJECT:
7980 T = Context.PseudoObjectTy;
7981 break;
7982 case PREDEF_TYPE_DEPENDENT_ID:
7983 T = Context.DependentTy;
7984 break;
7985 case PREDEF_TYPE_UNKNOWN_ANY:
7986 T = Context.UnknownAnyTy;
7987 break;
7988 case PREDEF_TYPE_NULLPTR_ID:
7989 T = Context.NullPtrTy;
7990 break;
7991 case PREDEF_TYPE_CHAR8_ID:
7992 T = Context.Char8Ty;
7993 break;
7994 case PREDEF_TYPE_CHAR16_ID:
7995 T = Context.Char16Ty;
7996 break;
7997 case PREDEF_TYPE_CHAR32_ID:
7998 T = Context.Char32Ty;
7999 break;
8000 case PREDEF_TYPE_OBJC_ID:
8001 T = Context.ObjCBuiltinIdTy;
8002 break;
8003 case PREDEF_TYPE_OBJC_CLASS:
8004 T = Context.ObjCBuiltinClassTy;
8005 break;
8006 case PREDEF_TYPE_OBJC_SEL:
8007 T = Context.ObjCBuiltinSelTy;
8008 break;
8009#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
8010 case PREDEF_TYPE_##Id##_ID: \
8011 T = Context.SingletonId; \
8012 break;
8013#include "clang/Basic/OpenCLImageTypes.def"
8014#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
8015 case PREDEF_TYPE_##Id##_ID: \
8016 T = Context.Id##Ty; \
8017 break;
8018#include "clang/Basic/OpenCLExtensionTypes.def"
8019 case PREDEF_TYPE_SAMPLER_ID:
8020 T = Context.OCLSamplerTy;
8021 break;
8022 case PREDEF_TYPE_EVENT_ID:
8023 T = Context.OCLEventTy;
8024 break;
8025 case PREDEF_TYPE_CLK_EVENT_ID:
8026 T = Context.OCLClkEventTy;
8027 break;
8028 case PREDEF_TYPE_QUEUE_ID:
8029 T = Context.OCLQueueTy;
8030 break;
8031 case PREDEF_TYPE_RESERVE_ID_ID:
8032 T = Context.OCLReserveIDTy;
8033 break;
8034 case PREDEF_TYPE_AUTO_DEDUCT:
8035 T = Context.getAutoDeductType();
8036 break;
8037 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
8038 T = Context.getAutoRRefDeductType();
8039 break;
8040 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
8041 T = Context.ARCUnbridgedCastTy;
8042 break;
8043 case PREDEF_TYPE_BUILTIN_FN:
8044 T = Context.BuiltinFnTy;
8045 break;
8046 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
8047 T = Context.IncompleteMatrixIdxTy;
8048 break;
8049 case PREDEF_TYPE_ARRAY_SECTION:
8050 T = Context.ArraySectionTy;
8051 break;
8052 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
8053 T = Context.OMPArrayShapingTy;
8054 break;
8055 case PREDEF_TYPE_OMP_ITERATOR:
8056 T = Context.OMPIteratorTy;
8057 break;
8058#define SVE_TYPE(Name, Id, SingletonId) \
8059 case PREDEF_TYPE_##Id##_ID: \
8060 T = Context.SingletonId; \
8061 break;
8062#include "clang/Basic/AArch64ACLETypes.def"
8063#define PPC_VECTOR_TYPE(Name, Id, Size) \
8064 case PREDEF_TYPE_##Id##_ID: \
8065 T = Context.Id##Ty; \
8066 break;
8067#include "clang/Basic/PPCTypes.def"
8068#define RVV_TYPE(Name, Id, SingletonId) \
8069 case PREDEF_TYPE_##Id##_ID: \
8070 T = Context.SingletonId; \
8071 break;
8072#include "clang/Basic/RISCVVTypes.def"
8073#define WASM_TYPE(Name, Id, SingletonId) \
8074 case PREDEF_TYPE_##Id##_ID: \
8075 T = Context.SingletonId; \
8076 break;
8077#include "clang/Basic/WebAssemblyReferenceTypes.def"
8078#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
8079 case PREDEF_TYPE_##Id##_ID: \
8080 T = Context.SingletonId; \
8081 break;
8082#include "clang/Basic/AMDGPUTypes.def"
8083#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
8084 case PREDEF_TYPE_##Id##_ID: \
8085 T = Context.SingletonId; \
8086 break;
8087#include "clang/Basic/HLSLIntangibleTypes.def"
8088 }
8089
8090 assert(!T.isNull() && "Unknown predefined type");
8091 return T.withFastQualifiers(TQs: FastQuals);
8092 }
8093
8094 unsigned Index = translateTypeIDToIndex(ID).second;
8095
8096 assert(Index < TypesLoaded.size() && "Type index out-of-range");
8097 if (TypesLoaded[Index].isNull()) {
8098 TypesLoaded[Index] = readTypeRecord(ID);
8099 if (TypesLoaded[Index].isNull())
8100 return QualType();
8101
8102 TypesLoaded[Index]->setFromAST();
8103 if (DeserializationListener)
8104 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
8105 T: TypesLoaded[Index]);
8106 }
8107
8108 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
8109}
8110
8111QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
8112 return GetType(ID: getGlobalTypeID(F, LocalID));
8113}
8114
8115serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
8116 LocalTypeID LocalID) const {
8117 if (isPredefinedType(ID: LocalID))
8118 return LocalID;
8119
8120 if (!F.ModuleOffsetMap.empty())
8121 ReadModuleOffsetMap(F);
8122
8123 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
8124 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
8125
8126 if (ModuleFileIndex == 0)
8127 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
8128
8129 ModuleFile &MF =
8130 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
8131 ModuleFileIndex = MF.Index + 1;
8132 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
8133}
8134
8135TemplateArgumentLocInfo
8136ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
8137 switch (Kind) {
8138 case TemplateArgument::Expression:
8139 return readExpr();
8140 case TemplateArgument::Type:
8141 return readTypeSourceInfo();
8142 case TemplateArgument::Template:
8143 case TemplateArgument::TemplateExpansion: {
8144 SourceLocation TemplateKWLoc = readSourceLocation();
8145 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
8146 SourceLocation TemplateNameLoc = readSourceLocation();
8147 SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion
8148 ? readSourceLocation()
8149 : SourceLocation();
8150 return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,
8151 TemplateNameLoc, EllipsisLoc);
8152 }
8153 case TemplateArgument::Null:
8154 case TemplateArgument::Integral:
8155 case TemplateArgument::Declaration:
8156 case TemplateArgument::NullPtr:
8157 case TemplateArgument::StructuralValue:
8158 case TemplateArgument::Pack:
8159 // FIXME: Is this right?
8160 return TemplateArgumentLocInfo();
8161 }
8162 llvm_unreachable("unexpected template argument loc");
8163}
8164
8165TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
8166 TemplateArgument Arg = readTemplateArgument();
8167
8168 if (Arg.getKind() == TemplateArgument::Expression) {
8169 if (readBool()) // bool InfoHasSameExpr.
8170 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
8171 }
8172 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
8173}
8174
8175void ASTRecordReader::readTemplateArgumentListInfo(
8176 TemplateArgumentListInfo &Result) {
8177 Result.setLAngleLoc(readSourceLocation());
8178 Result.setRAngleLoc(readSourceLocation());
8179 unsigned NumArgsAsWritten = readInt();
8180 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
8181 Result.addArgument(Loc: readTemplateArgumentLoc());
8182}
8183
8184const ASTTemplateArgumentListInfo *
8185ASTRecordReader::readASTTemplateArgumentListInfo() {
8186 TemplateArgumentListInfo Result;
8187 readTemplateArgumentListInfo(Result);
8188 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
8189}
8190
8191Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
8192
8193void ASTReader::CompleteRedeclChain(const Decl *D) {
8194 if (NumCurrentElementsDeserializing) {
8195 // We arrange to not care about the complete redeclaration chain while we're
8196 // deserializing. Just remember that the AST has marked this one as complete
8197 // but that it's not actually complete yet, so we know we still need to
8198 // complete it later.
8199 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
8200 return;
8201 }
8202
8203 if (!D->getDeclContext()) {
8204 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
8205 return;
8206 }
8207
8208 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
8209
8210 // If this is a named declaration, complete it by looking it up
8211 // within its context.
8212 //
8213 // FIXME: Merging a function definition should merge
8214 // all mergeable entities within it.
8215 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
8216 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
8217 if (!getContext().getLangOpts().CPlusPlus &&
8218 isa<TranslationUnitDecl>(Val: DC)) {
8219 // Outside of C++, we don't have a lookup table for the TU, so update
8220 // the identifier instead. (For C++ modules, we don't store decls
8221 // in the serialized identifier table, so we do the lookup in the TU.)
8222 auto *II = Name.getAsIdentifierInfo();
8223 assert(II && "non-identifier name in C?");
8224 if (II->isOutOfDate())
8225 updateOutOfDateIdentifier(II: *II);
8226 } else
8227 DC->lookup(Name);
8228 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
8229 // Find all declarations of this kind from the relevant context.
8230 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
8231 auto *DC = cast<DeclContext>(Val: DCDecl);
8232 SmallVector<Decl*, 8> Decls;
8233 FindExternalLexicalDecls(
8234 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
8235 }
8236 }
8237 }
8238
8239 RedeclarableTemplateDecl *Template = nullptr;
8240 ArrayRef<TemplateArgument> Args;
8241 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
8242 Template = CTSD->getSpecializedTemplate();
8243 Args = CTSD->getTemplateArgs().asArray();
8244 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
8245 Template = VTSD->getSpecializedTemplate();
8246 Args = VTSD->getTemplateArgs().asArray();
8247 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
8248 if (auto *Tmplt = FD->getPrimaryTemplate()) {
8249 Template = Tmplt;
8250 Args = FD->getTemplateSpecializationArgs()->asArray();
8251 }
8252 }
8253
8254 if (Template)
8255 Template->loadLazySpecializationsImpl(Args);
8256}
8257
8258CXXCtorInitializer **
8259ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
8260 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8261 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8262 SavedStreamPosition SavedPosition(Cursor);
8263 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8264 Error(Err: std::move(Err));
8265 return nullptr;
8266 }
8267 ReadingKindTracker ReadingKind(Read_Decl, *this);
8268 Deserializing D(this);
8269
8270 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8271 if (!MaybeCode) {
8272 Error(Err: MaybeCode.takeError());
8273 return nullptr;
8274 }
8275 unsigned Code = MaybeCode.get();
8276
8277 ASTRecordReader Record(*this, *Loc.F);
8278 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8279 if (!MaybeRecCode) {
8280 Error(Err: MaybeRecCode.takeError());
8281 return nullptr;
8282 }
8283 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
8284 Error(Msg: "malformed AST file: missing C++ ctor initializers");
8285 return nullptr;
8286 }
8287
8288 return Record.readCXXCtorInitializers();
8289}
8290
8291CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8292 assert(ContextObj && "reading base specifiers with no AST context");
8293 ASTContext &Context = *ContextObj;
8294
8295 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8296 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8297 SavedStreamPosition SavedPosition(Cursor);
8298 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8299 Error(Err: std::move(Err));
8300 return nullptr;
8301 }
8302 ReadingKindTracker ReadingKind(Read_Decl, *this);
8303 Deserializing D(this);
8304
8305 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8306 if (!MaybeCode) {
8307 Error(Err: MaybeCode.takeError());
8308 return nullptr;
8309 }
8310 unsigned Code = MaybeCode.get();
8311
8312 ASTRecordReader Record(*this, *Loc.F);
8313 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8314 if (!MaybeRecCode) {
8315 Error(Err: MaybeCode.takeError());
8316 return nullptr;
8317 }
8318 unsigned RecCode = MaybeRecCode.get();
8319
8320 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8321 Error(Msg: "malformed AST file: missing C++ base specifiers");
8322 return nullptr;
8323 }
8324
8325 unsigned NumBases = Record.readInt();
8326 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8327 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8328 for (unsigned I = 0; I != NumBases; ++I)
8329 Bases[I] = Record.readCXXBaseSpecifier();
8330 return Bases;
8331}
8332
8333GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8334 LocalDeclID LocalID) const {
8335 if (LocalID < NUM_PREDEF_DECL_IDS)
8336 return GlobalDeclID(LocalID.getRawValue());
8337
8338 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8339 DeclID ID = LocalID.getLocalDeclIndex();
8340
8341 if (!F.ModuleOffsetMap.empty())
8342 ReadModuleOffsetMap(F);
8343
8344 ModuleFile *OwningModuleFile =
8345 OwningModuleFileIndex == 0
8346 ? &F
8347 : F.TransitiveImports[OwningModuleFileIndex - 1];
8348
8349 if (OwningModuleFileIndex == 0)
8350 ID -= NUM_PREDEF_DECL_IDS;
8351
8352 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8353 return GlobalDeclID(NewModuleFileIndex, ID);
8354}
8355
8356bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8357 // Predefined decls aren't from any module.
8358 if (ID < NUM_PREDEF_DECL_IDS)
8359 return false;
8360
8361 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8362 return M.Index == ModuleFileIndex - 1;
8363}
8364
8365ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8366 // Predefined decls aren't from any module.
8367 if (ID < NUM_PREDEF_DECL_IDS)
8368 return nullptr;
8369
8370 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8371 assert(ModuleFileIndex && "Untranslated Local Decl?");
8372
8373 return &getModuleManager()[ModuleFileIndex - 1];
8374}
8375
8376ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8377 if (!D->isFromASTFile())
8378 return nullptr;
8379
8380 return getOwningModuleFile(ID: D->getGlobalID());
8381}
8382
8383SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8384 if (ID < NUM_PREDEF_DECL_IDS)
8385 return SourceLocation();
8386
8387 if (Decl *D = GetExistingDecl(ID))
8388 return D->getLocation();
8389
8390 SourceLocation Loc;
8391 DeclCursorForID(ID, Location&: Loc);
8392 return Loc;
8393}
8394
8395Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8396 assert(ContextObj && "reading predefined decl without AST context");
8397 ASTContext &Context = *ContextObj;
8398 Decl *NewLoaded = nullptr;
8399 switch (ID) {
8400 case PREDEF_DECL_NULL_ID:
8401 return nullptr;
8402
8403 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8404 return Context.getTranslationUnitDecl();
8405
8406 case PREDEF_DECL_OBJC_ID_ID:
8407 if (Context.ObjCIdDecl)
8408 return Context.ObjCIdDecl;
8409 NewLoaded = Context.getObjCIdDecl();
8410 break;
8411
8412 case PREDEF_DECL_OBJC_SEL_ID:
8413 if (Context.ObjCSelDecl)
8414 return Context.ObjCSelDecl;
8415 NewLoaded = Context.getObjCSelDecl();
8416 break;
8417
8418 case PREDEF_DECL_OBJC_CLASS_ID:
8419 if (Context.ObjCClassDecl)
8420 return Context.ObjCClassDecl;
8421 NewLoaded = Context.getObjCClassDecl();
8422 break;
8423
8424 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8425 if (Context.ObjCProtocolClassDecl)
8426 return Context.ObjCProtocolClassDecl;
8427 NewLoaded = Context.getObjCProtocolDecl();
8428 break;
8429
8430 case PREDEF_DECL_INT_128_ID:
8431 if (Context.Int128Decl)
8432 return Context.Int128Decl;
8433 NewLoaded = Context.getInt128Decl();
8434 break;
8435
8436 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8437 if (Context.UInt128Decl)
8438 return Context.UInt128Decl;
8439 NewLoaded = Context.getUInt128Decl();
8440 break;
8441
8442 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8443 if (Context.ObjCInstanceTypeDecl)
8444 return Context.ObjCInstanceTypeDecl;
8445 NewLoaded = Context.getObjCInstanceTypeDecl();
8446 break;
8447
8448 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8449 if (Context.BuiltinVaListDecl)
8450 return Context.BuiltinVaListDecl;
8451 NewLoaded = Context.getBuiltinVaListDecl();
8452 break;
8453
8454 case PREDEF_DECL_VA_LIST_TAG:
8455 if (Context.VaListTagDecl)
8456 return Context.VaListTagDecl;
8457 NewLoaded = Context.getVaListTagDecl();
8458 break;
8459
8460 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8461 if (Context.BuiltinMSVaListDecl)
8462 return Context.BuiltinMSVaListDecl;
8463 NewLoaded = Context.getBuiltinMSVaListDecl();
8464 break;
8465
8466 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8467 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8468 return Context.getMSGuidTagDecl();
8469
8470 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8471 if (Context.ExternCContext)
8472 return Context.ExternCContext;
8473 NewLoaded = Context.getExternCContextDecl();
8474 break;
8475
8476 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8477 if (Context.CFConstantStringTypeDecl)
8478 return Context.CFConstantStringTypeDecl;
8479 NewLoaded = Context.getCFConstantStringDecl();
8480 break;
8481
8482 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8483 if (Context.CFConstantStringTagDecl)
8484 return Context.CFConstantStringTagDecl;
8485 NewLoaded = Context.getCFConstantStringTagDecl();
8486 break;
8487
8488 case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:
8489 return Context.getMSTypeInfoTagDecl();
8490
8491#define BuiltinTemplate(BTName) \
8492 case PREDEF_DECL##BTName##_ID: \
8493 if (Context.Decl##BTName) \
8494 return Context.Decl##BTName; \
8495 NewLoaded = Context.get##BTName##Decl(); \
8496 break;
8497#include "clang/Basic/BuiltinTemplates.inc"
8498
8499 case NUM_PREDEF_DECL_IDS:
8500 llvm_unreachable("Invalid decl ID");
8501 break;
8502 }
8503
8504 assert(NewLoaded && "Failed to load predefined decl?");
8505
8506 if (DeserializationListener)
8507 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8508
8509 return NewLoaded;
8510}
8511
8512unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8513 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8514 if (!OwningModuleFile) {
8515 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8516 return GlobalID.getRawValue();
8517 }
8518
8519 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8520}
8521
8522Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8523 assert(ContextObj && "reading decl with no AST context");
8524
8525 if (ID < NUM_PREDEF_DECL_IDS) {
8526 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8527 if (D) {
8528 // Track that we have merged the declaration with ID \p ID into the
8529 // pre-existing predefined declaration \p D.
8530 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8531 if (Merged.empty())
8532 Merged.push_back(Elt: ID);
8533 }
8534 return D;
8535 }
8536
8537 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8538
8539 if (Index >= DeclsLoaded.size()) {
8540 assert(0 && "declaration ID out-of-range for AST file");
8541 Error(Msg: "declaration ID out-of-range for AST file");
8542 return nullptr;
8543 }
8544
8545 return DeclsLoaded[Index];
8546}
8547
8548Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8549 if (ID < NUM_PREDEF_DECL_IDS)
8550 return GetExistingDecl(ID);
8551
8552 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8553
8554 if (Index >= DeclsLoaded.size()) {
8555 assert(0 && "declaration ID out-of-range for AST file");
8556 Error(Msg: "declaration ID out-of-range for AST file");
8557 return nullptr;
8558 }
8559
8560 if (!DeclsLoaded[Index]) {
8561 ReadDeclRecord(ID);
8562 if (DeserializationListener)
8563 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8564 }
8565
8566 return DeclsLoaded[Index];
8567}
8568
8569LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8570 GlobalDeclID GlobalID) {
8571 if (GlobalID < NUM_PREDEF_DECL_IDS)
8572 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8573
8574 if (!M.ModuleOffsetMap.empty())
8575 ReadModuleOffsetMap(F&: M);
8576
8577 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8578 DeclID ID = GlobalID.getLocalDeclIndex();
8579
8580 if (Owner == &M) {
8581 ID += NUM_PREDEF_DECL_IDS;
8582 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8583 }
8584
8585 uint64_t OrignalModuleFileIndex = 0;
8586 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8587 if (M.TransitiveImports[I] == Owner) {
8588 OrignalModuleFileIndex = I + 1;
8589 break;
8590 }
8591
8592 if (!OrignalModuleFileIndex)
8593 return LocalDeclID();
8594
8595 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8596}
8597
8598GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8599 unsigned &Idx) {
8600 if (Idx >= Record.size()) {
8601 Error(Msg: "Corrupted AST file");
8602 return GlobalDeclID(0);
8603 }
8604
8605 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8606}
8607
8608/// Resolve the offset of a statement into a statement.
8609///
8610/// This operation will read a new statement from the external
8611/// source each time it is called, and is meant to be used via a
8612/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8613Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8614 // Switch case IDs are per Decl.
8615 ClearSwitchCaseIDs();
8616
8617 // Offset here is a global offset across the entire chain.
8618 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8619 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8620 Error(Err: std::move(Err));
8621 return nullptr;
8622 }
8623 assert(NumCurrentElementsDeserializing == 0 &&
8624 "should not be called while already deserializing");
8625 Deserializing D(this);
8626 return ReadStmtFromStream(F&: *Loc.F);
8627}
8628
8629bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8630 const Decl *D) {
8631 assert(D);
8632
8633 auto It = SpecLookups.find(Val: D);
8634 if (It == SpecLookups.end())
8635 return false;
8636
8637 // Get Decl may violate the iterator from SpecializationsLookups so we store
8638 // the DeclIDs in ahead.
8639 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8640 It->second.Table.findAll();
8641
8642 // Since we've loaded all the specializations, we can erase it from
8643 // the lookup table.
8644 SpecLookups.erase(I: It);
8645
8646 bool NewSpecsFound = false;
8647 Deserializing LookupResults(this);
8648 for (auto &Info : Infos) {
8649 if (GetExistingDecl(ID: Info))
8650 continue;
8651 NewSpecsFound = true;
8652 GetDecl(ID: Info);
8653 }
8654
8655 return NewSpecsFound;
8656}
8657
8658bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8659 assert(D);
8660
8661 CompleteRedeclChain(D);
8662 bool NewSpecsFound =
8663 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8664 if (OnlyPartial)
8665 return NewSpecsFound;
8666
8667 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8668 return NewSpecsFound;
8669}
8670
8671bool ASTReader::LoadExternalSpecializationsImpl(
8672 SpecLookupTableTy &SpecLookups, const Decl *D,
8673 ArrayRef<TemplateArgument> TemplateArgs) {
8674 assert(D);
8675
8676 reader::LazySpecializationInfoLookupTable *LookupTable = nullptr;
8677 if (auto It = SpecLookups.find(Val: D); It != SpecLookups.end())
8678 LookupTable = &It->getSecond();
8679 if (!LookupTable)
8680 return false;
8681
8682 // NOTE: The getNameForDiagnostic usage in the lambda may mutate the
8683 // `SpecLookups` object.
8684 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8685 std::string Name;
8686 llvm::raw_string_ostream OS(Name);
8687 auto *ND = cast<NamedDecl>(Val: D);
8688 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8689 /*Qualified=*/true);
8690 return Name;
8691 });
8692
8693 Deserializing LookupResults(this);
8694 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8695
8696 // Get Decl may violate the iterator from SpecLookups
8697 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8698 LookupTable->Table.find(EKey: HashValue);
8699
8700 bool NewSpecsFound = false;
8701 for (auto &Info : Infos) {
8702 if (GetExistingDecl(ID: Info))
8703 continue;
8704 NewSpecsFound = true;
8705 GetDecl(ID: Info);
8706 }
8707
8708 return NewSpecsFound;
8709}
8710
8711bool ASTReader::LoadExternalSpecializations(
8712 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8713 assert(D);
8714
8715 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8716 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8717 NewDeclsFound |=
8718 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8719
8720 return NewDeclsFound;
8721}
8722
8723void ASTReader::FindExternalLexicalDecls(
8724 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8725 SmallVectorImpl<Decl *> &Decls) {
8726 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8727
8728 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8729 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8730 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8731 auto K = (Decl::Kind)+LexicalDecls[I];
8732 if (!IsKindWeWant(K))
8733 continue;
8734
8735 auto ID = (DeclID) + LexicalDecls[I + 1];
8736
8737 // Don't add predefined declarations to the lexical context more
8738 // than once.
8739 if (ID < NUM_PREDEF_DECL_IDS) {
8740 if (PredefsVisited[ID])
8741 continue;
8742
8743 PredefsVisited[ID] = true;
8744 }
8745
8746 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8747 assert(D->getKind() == K && "wrong kind for lexical decl");
8748 if (!DC->isDeclInLexicalTraversal(D))
8749 Decls.push_back(Elt: D);
8750 }
8751 }
8752 };
8753
8754 if (isa<TranslationUnitDecl>(Val: DC)) {
8755 for (const auto &Lexical : TULexicalDecls)
8756 Visit(Lexical.first, Lexical.second);
8757 } else {
8758 auto I = LexicalDecls.find(Val: DC);
8759 if (I != LexicalDecls.end())
8760 Visit(I->second.first, I->second.second);
8761 }
8762
8763 ++NumLexicalDeclContextsRead;
8764}
8765
8766namespace {
8767
8768class UnalignedDeclIDComp {
8769 ASTReader &Reader;
8770 ModuleFile &Mod;
8771
8772public:
8773 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8774 : Reader(Reader), Mod(M) {}
8775
8776 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8777 SourceLocation LHS = getLocation(ID: L);
8778 SourceLocation RHS = getLocation(ID: R);
8779 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8780 }
8781
8782 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8783 SourceLocation RHS = getLocation(ID: R);
8784 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8785 }
8786
8787 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8788 SourceLocation LHS = getLocation(ID: L);
8789 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8790 }
8791
8792 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8793 return Reader.getSourceManager().getFileLoc(
8794 Loc: Reader.getSourceLocationForDeclID(
8795 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8796 }
8797};
8798
8799} // namespace
8800
8801void ASTReader::FindFileRegionDecls(FileID File,
8802 unsigned Offset, unsigned Length,
8803 SmallVectorImpl<Decl *> &Decls) {
8804 SourceManager &SM = getSourceManager();
8805
8806 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8807 if (I == FileDeclIDs.end())
8808 return;
8809
8810 FileDeclsInfo &DInfo = I->second;
8811 if (DInfo.Decls.empty())
8812 return;
8813
8814 SourceLocation
8815 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8816 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8817
8818 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8819 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8820 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8821 if (BeginIt != DInfo.Decls.begin())
8822 --BeginIt;
8823
8824 // If we are pointing at a top-level decl inside an objc container, we need
8825 // to backtrack until we find it otherwise we will fail to report that the
8826 // region overlaps with an objc container.
8827 while (BeginIt != DInfo.Decls.begin() &&
8828 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8829 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8830 ->isTopLevelDeclInObjCContainer())
8831 --BeginIt;
8832
8833 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8834 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8835 if (EndIt != DInfo.Decls.end())
8836 ++EndIt;
8837
8838 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8839 ++DIt)
8840 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8841 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8842}
8843
8844bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8845 DeclarationName Name,
8846 const DeclContext *OriginalDC) {
8847 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8848 "DeclContext has no visible decls in storage");
8849 if (!Name)
8850 return false;
8851
8852 // Load the list of declarations.
8853 DeclsSet DS;
8854
8855 auto Find = [&, this](auto &&Table, auto &&Key) {
8856 for (GlobalDeclID ID : Table.find(Key)) {
8857 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8858 if (ND->getDeclName() != Name)
8859 continue;
8860 // Special case for namespaces: There can be a lot of redeclarations of
8861 // some namespaces, and we import a "key declaration" per imported module.
8862 // Since all declarations of a namespace are essentially interchangeable,
8863 // we can optimize namespace look-up by only storing the key declaration
8864 // of the current TU, rather than storing N key declarations where N is
8865 // the # of imported modules that declare that namespace.
8866 // TODO: Try to generalize this optimization to other redeclarable decls.
8867 if (isa<NamespaceDecl>(Val: ND))
8868 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8869 DS.insert(ND);
8870 }
8871 };
8872
8873 Deserializing LookupResults(this);
8874
8875 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8876 // available.
8877 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8878 ++NumVisibleDeclContextsRead;
8879 Find(It->second.Table, Name);
8880 }
8881
8882 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8883 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8884 ++NumModuleLocalVisibleDeclContexts;
8885 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8886 }
8887 };
8888 if (auto *NamedModule =
8889 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8890 : nullptr)
8891 FindModuleLocalLookup(NamedModule);
8892 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8893 // We're going to find a decl but the decl context of the lookup is
8894 // unspecified. In this case, the OriginalDC may be the decl context in other
8895 // module.
8896 if (ContextObj && ContextObj->getCurrentNamedModule())
8897 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8898
8899 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8900 ++NumTULocalVisibleDeclContexts;
8901 Find(It->second.Table, Name);
8902 }
8903
8904 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8905 return !DS.empty();
8906}
8907
8908void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8909 if (!DC->hasExternalVisibleStorage())
8910 return;
8911
8912 DeclsMap Decls;
8913
8914 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8915 auto It = LookupTables.find(DC);
8916 if (It == LookupTables.end())
8917 return;
8918
8919 NumRead++;
8920
8921 for (GlobalDeclID ID : It->second.Table.findAll()) {
8922 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8923 // Special case for namespaces: There can be a lot of redeclarations of
8924 // some namespaces, and we import a "key declaration" per imported module.
8925 // Since all declarations of a namespace are essentially interchangeable,
8926 // we can optimize namespace look-up by only storing the key declaration
8927 // of the current TU, rather than storing N key declarations where N is
8928 // the # of imported modules that declare that namespace.
8929 // TODO: Try to generalize this optimization to other redeclarable decls.
8930 if (isa<NamespaceDecl>(Val: ND))
8931 ND = cast<NamedDecl>(Val: getKeyDeclaration(D: ND));
8932 Decls[ND->getDeclName()].insert(ND);
8933 }
8934
8935 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8936 };
8937
8938 findAll(Lookups, NumVisibleDeclContextsRead);
8939 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8940 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8941
8942 for (auto &[Name, DS] : Decls)
8943 SetExternalVisibleDeclsForName(DC, Name, Decls: DS);
8944
8945 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8946}
8947
8948const serialization::reader::DeclContextLookupTable *
8949ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8950 auto I = Lookups.find(Val: Primary);
8951 return I == Lookups.end() ? nullptr : &I->second;
8952}
8953
8954const serialization::reader::ModuleLocalLookupTable *
8955ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
8956 auto I = ModuleLocalLookups.find(Val: Primary);
8957 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
8958}
8959
8960const serialization::reader::DeclContextLookupTable *
8961ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
8962 auto I = TULocalLookups.find(Val: Primary);
8963 return I == TULocalLookups.end() ? nullptr : &I->second;
8964}
8965
8966serialization::reader::LazySpecializationInfoLookupTable *
8967ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
8968 assert(D->isCanonicalDecl());
8969 auto &LookupTable =
8970 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
8971 auto I = LookupTable.find(Val: D);
8972 return I == LookupTable.end() ? nullptr : &I->second;
8973}
8974
8975bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
8976 assert(D->isCanonicalDecl());
8977 return PartialSpecializationsLookups.contains(Val: D) ||
8978 SpecializationsLookups.contains(Val: D);
8979}
8980
8981/// Under non-PCH compilation the consumer receives the objc methods
8982/// before receiving the implementation, and codegen depends on this.
8983/// We simulate this by deserializing and passing to consumer the methods of the
8984/// implementation before passing the deserialized implementation decl.
8985static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8986 ASTConsumer *Consumer) {
8987 assert(ImplD && Consumer);
8988
8989 for (auto *I : ImplD->methods())
8990 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
8991
8992 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8993}
8994
8995void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8996 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8997 PassObjCImplDeclToConsumer(ImplD, Consumer);
8998 else
8999 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
9000}
9001
9002void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
9003 Consumer->HandleVTable(RD);
9004}
9005
9006void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
9007 this->Consumer = Consumer;
9008
9009 if (Consumer)
9010 PassInterestingDeclsToConsumer();
9011
9012 if (DeserializationListener)
9013 DeserializationListener->ReaderInitialized(Reader: this);
9014}
9015
9016void ASTReader::PrintStats() {
9017 std::fprintf(stderr, format: "*** AST File Statistics:\n");
9018
9019 unsigned NumTypesLoaded =
9020 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
9021 unsigned NumDeclsLoaded =
9022 DeclsLoaded.size() -
9023 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
9024 unsigned NumIdentifiersLoaded =
9025 IdentifiersLoaded.size() -
9026 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
9027 unsigned NumMacrosLoaded =
9028 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
9029 unsigned NumSelectorsLoaded =
9030 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
9031
9032 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
9033 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
9034 NumSLocEntriesRead, TotalNumSLocEntries,
9035 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
9036 if (!TypesLoaded.empty())
9037 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
9038 NumTypesLoaded, (unsigned)TypesLoaded.size(),
9039 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
9040 if (!DeclsLoaded.empty())
9041 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
9042 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
9043 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
9044 if (!IdentifiersLoaded.empty())
9045 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
9046 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
9047 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
9048 if (!MacrosLoaded.empty())
9049 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9050 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
9051 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
9052 if (!SelectorsLoaded.empty())
9053 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
9054 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
9055 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
9056 if (TotalNumStatements)
9057 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
9058 NumStatementsRead, TotalNumStatements,
9059 ((float)NumStatementsRead/TotalNumStatements * 100));
9060 if (TotalNumMacros)
9061 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
9062 NumMacrosRead, TotalNumMacros,
9063 ((float)NumMacrosRead/TotalNumMacros * 100));
9064 if (TotalLexicalDeclContexts)
9065 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
9066 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
9067 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
9068 * 100));
9069 if (TotalVisibleDeclContexts)
9070 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
9071 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
9072 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
9073 * 100));
9074 if (TotalModuleLocalVisibleDeclContexts)
9075 std::fprintf(
9076 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
9077 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
9078 ((float)NumModuleLocalVisibleDeclContexts /
9079 TotalModuleLocalVisibleDeclContexts * 100));
9080 if (TotalTULocalVisibleDeclContexts)
9081 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
9082 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
9083 ((float)NumTULocalVisibleDeclContexts /
9084 TotalTULocalVisibleDeclContexts * 100));
9085 if (TotalNumMethodPoolEntries)
9086 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
9087 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
9088 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
9089 * 100));
9090 if (NumMethodPoolLookups)
9091 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
9092 NumMethodPoolHits, NumMethodPoolLookups,
9093 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
9094 if (NumMethodPoolTableLookups)
9095 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
9096 NumMethodPoolTableHits, NumMethodPoolTableLookups,
9097 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
9098 * 100.0));
9099 if (NumIdentifierLookupHits)
9100 std::fprintf(stderr,
9101 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
9102 NumIdentifierLookupHits, NumIdentifierLookups,
9103 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
9104
9105 if (GlobalIndex) {
9106 std::fprintf(stderr, format: "\n");
9107 GlobalIndex->printStats();
9108 }
9109
9110 std::fprintf(stderr, format: "\n");
9111 dump();
9112 std::fprintf(stderr, format: "\n");
9113}
9114
9115template<typename Key, typename ModuleFile, unsigned InitialCapacity>
9116LLVM_DUMP_METHOD static void
9117dumpModuleIDMap(StringRef Name,
9118 const ContinuousRangeMap<Key, ModuleFile *,
9119 InitialCapacity> &Map) {
9120 if (Map.begin() == Map.end())
9121 return;
9122
9123 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
9124
9125 llvm::errs() << Name << ":\n";
9126 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
9127 I != IEnd; ++I)
9128 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
9129 << "\n";
9130}
9131
9132LLVM_DUMP_METHOD void ASTReader::dump() {
9133 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
9134 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
9135 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
9136 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
9137 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
9138 dumpModuleIDMap(Name: "Global preprocessed entity map",
9139 Map: GlobalPreprocessedEntityMap);
9140
9141 llvm::errs() << "\n*** PCH/Modules Loaded:";
9142 for (ModuleFile &M : ModuleMgr)
9143 M.dump();
9144}
9145
9146/// Return the amount of memory used by memory buffers, breaking down
9147/// by heap-backed versus mmap'ed memory.
9148void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
9149 for (ModuleFile &I : ModuleMgr) {
9150 if (llvm::MemoryBuffer *buf = I.Buffer) {
9151 size_t bytes = buf->getBufferSize();
9152 switch (buf->getBufferKind()) {
9153 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
9154 sizes.malloc_bytes += bytes;
9155 break;
9156 case llvm::MemoryBuffer::MemoryBuffer_MMap:
9157 sizes.mmap_bytes += bytes;
9158 break;
9159 }
9160 }
9161 }
9162}
9163
9164void ASTReader::InitializeSema(Sema &S) {
9165 SemaObj = &S;
9166 S.addExternalSource(E: this);
9167
9168 // Makes sure any declarations that were deserialized "too early"
9169 // still get added to the identifier's declaration chains.
9170 for (GlobalDeclID ID : PreloadedDeclIDs) {
9171 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
9172 pushExternalDeclIntoScope(D, Name: D->getDeclName());
9173 }
9174 PreloadedDeclIDs.clear();
9175
9176 // FIXME: What happens if these are changed by a module import?
9177 if (!FPPragmaOptions.empty()) {
9178 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
9179 FPOptionsOverride NewOverrides =
9180 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
9181 SemaObj->CurFPFeatures =
9182 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
9183 }
9184
9185 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
9186 Decl *D = GetDecl(ID);
9187 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
9188 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
9189 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
9190 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
9191 else
9192 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
9193 }
9194 DeclsWithEffectsToVerify.clear();
9195
9196 SemaObj->OpenCLFeatures = OpenCLExtensions;
9197
9198 UpdateSema();
9199}
9200
9201void ASTReader::UpdateSema() {
9202 assert(SemaObj && "no Sema to update");
9203
9204 // Load the offsets of the declarations that Sema references.
9205 // They will be lazily deserialized when needed.
9206 if (!SemaDeclRefs.empty()) {
9207 assert(SemaDeclRefs.size() % 3 == 0);
9208 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
9209 if (!SemaObj->StdNamespace)
9210 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
9211 if (!SemaObj->StdBadAlloc)
9212 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
9213 if (!SemaObj->StdAlignValT)
9214 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
9215 }
9216 SemaDeclRefs.clear();
9217 }
9218
9219 // Update the state of pragmas. Use the same API as if we had encountered the
9220 // pragma in the source.
9221 if(OptimizeOffPragmaLocation.isValid())
9222 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
9223 if (PragmaMSStructState != -1)
9224 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
9225 if (PointersToMembersPragmaLocation.isValid()) {
9226 SemaObj->ActOnPragmaMSPointersToMembers(
9227 Kind: (LangOptions::PragmaMSPointersToMembersKind)
9228 PragmaMSPointersToMembersState,
9229 PragmaLoc: PointersToMembersPragmaLocation);
9230 }
9231 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
9232 if (!RISCVVecIntrinsicPragma.empty()) {
9233 assert(RISCVVecIntrinsicPragma.size() == 3 &&
9234 "Wrong number of RISCVVecIntrinsicPragma");
9235 SemaObj->RISCV().DeclareRVVBuiltins = RISCVVecIntrinsicPragma[0];
9236 SemaObj->RISCV().DeclareSiFiveVectorBuiltins = RISCVVecIntrinsicPragma[1];
9237 SemaObj->RISCV().DeclareAndesVectorBuiltins = RISCVVecIntrinsicPragma[2];
9238 }
9239
9240 if (PragmaAlignPackCurrentValue) {
9241 // The bottom of the stack might have a default value. It must be adjusted
9242 // to the current value to ensure that the packing state is preserved after
9243 // popping entries that were included/imported from a PCH/module.
9244 bool DropFirst = false;
9245 if (!PragmaAlignPackStack.empty() &&
9246 PragmaAlignPackStack.front().Location.isInvalid()) {
9247 assert(PragmaAlignPackStack.front().Value ==
9248 SemaObj->AlignPackStack.DefaultValue &&
9249 "Expected a default alignment value");
9250 SemaObj->AlignPackStack.Stack.emplace_back(
9251 Args&: PragmaAlignPackStack.front().SlotLabel,
9252 Args&: SemaObj->AlignPackStack.CurrentValue,
9253 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
9254 Args&: PragmaAlignPackStack.front().PushLocation);
9255 DropFirst = true;
9256 }
9257 for (const auto &Entry :
9258 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
9259 SemaObj->AlignPackStack.Stack.emplace_back(
9260 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9261 }
9262 if (PragmaAlignPackCurrentLocation.isInvalid()) {
9263 assert(*PragmaAlignPackCurrentValue ==
9264 SemaObj->AlignPackStack.DefaultValue &&
9265 "Expected a default align and pack value");
9266 // Keep the current values.
9267 } else {
9268 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
9269 SemaObj->AlignPackStack.CurrentPragmaLocation =
9270 PragmaAlignPackCurrentLocation;
9271 }
9272 }
9273 if (FpPragmaCurrentValue) {
9274 // The bottom of the stack might have a default value. It must be adjusted
9275 // to the current value to ensure that fp-pragma state is preserved after
9276 // popping entries that were included/imported from a PCH/module.
9277 bool DropFirst = false;
9278 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
9279 assert(FpPragmaStack.front().Value ==
9280 SemaObj->FpPragmaStack.DefaultValue &&
9281 "Expected a default pragma float_control value");
9282 SemaObj->FpPragmaStack.Stack.emplace_back(
9283 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
9284 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
9285 Args&: FpPragmaStack.front().PushLocation);
9286 DropFirst = true;
9287 }
9288 for (const auto &Entry :
9289 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
9290 SemaObj->FpPragmaStack.Stack.emplace_back(
9291 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
9292 if (FpPragmaCurrentLocation.isInvalid()) {
9293 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
9294 "Expected a default pragma float_control value");
9295 // Keep the current values.
9296 } else {
9297 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
9298 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
9299 }
9300 }
9301
9302 // For non-modular AST files, restore visiblity of modules.
9303 for (auto &Import : PendingImportedModulesSema) {
9304 if (Import.ImportLoc.isInvalid())
9305 continue;
9306 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
9307 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
9308 }
9309 }
9310 PendingImportedModulesSema.clear();
9311}
9312
9313IdentifierInfo *ASTReader::get(StringRef Name) {
9314 // Note that we are loading an identifier.
9315 Deserializing AnIdentifier(this);
9316
9317 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
9318 NumIdentifierLookups,
9319 NumIdentifierLookupHits);
9320
9321 // We don't need to do identifier table lookups in C++ modules (we preload
9322 // all interesting declarations, and don't need to use the scope for name
9323 // lookups). Perform the lookup in PCH files, though, since we don't build
9324 // a complete initial identifier table if we're carrying on from a PCH.
9325 if (PP.getLangOpts().CPlusPlus) {
9326 for (auto *F : ModuleMgr.pch_modules())
9327 if (Visitor(*F))
9328 break;
9329 } else {
9330 // If there is a global index, look there first to determine which modules
9331 // provably do not have any results for this identifier.
9332 GlobalModuleIndex::HitSet Hits;
9333 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9334 if (!loadGlobalIndex()) {
9335 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9336 HitsPtr = &Hits;
9337 }
9338 }
9339
9340 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9341 }
9342
9343 IdentifierInfo *II = Visitor.getIdentifierInfo();
9344 markIdentifierUpToDate(II);
9345 return II;
9346}
9347
9348namespace clang {
9349
9350 /// An identifier-lookup iterator that enumerates all of the
9351 /// identifiers stored within a set of AST files.
9352 class ASTIdentifierIterator : public IdentifierIterator {
9353 /// The AST reader whose identifiers are being enumerated.
9354 const ASTReader &Reader;
9355
9356 /// The current index into the chain of AST files stored in
9357 /// the AST reader.
9358 unsigned Index;
9359
9360 /// The current position within the identifier lookup table
9361 /// of the current AST file.
9362 ASTIdentifierLookupTable::key_iterator Current;
9363
9364 /// The end position within the identifier lookup table of
9365 /// the current AST file.
9366 ASTIdentifierLookupTable::key_iterator End;
9367
9368 /// Whether to skip any modules in the ASTReader.
9369 bool SkipModules;
9370
9371 public:
9372 explicit ASTIdentifierIterator(const ASTReader &Reader,
9373 bool SkipModules = false);
9374
9375 StringRef Next() override;
9376 };
9377
9378} // namespace clang
9379
9380ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9381 bool SkipModules)
9382 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9383}
9384
9385StringRef ASTIdentifierIterator::Next() {
9386 while (Current == End) {
9387 // If we have exhausted all of our AST files, we're done.
9388 if (Index == 0)
9389 return StringRef();
9390
9391 --Index;
9392 ModuleFile &F = Reader.ModuleMgr[Index];
9393 if (SkipModules && F.isModule())
9394 continue;
9395
9396 ASTIdentifierLookupTable *IdTable =
9397 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9398 Current = IdTable->key_begin();
9399 End = IdTable->key_end();
9400 }
9401
9402 // We have any identifiers remaining in the current AST file; return
9403 // the next one.
9404 StringRef Result = *Current;
9405 ++Current;
9406 return Result;
9407}
9408
9409namespace {
9410
9411/// A utility for appending two IdentifierIterators.
9412class ChainedIdentifierIterator : public IdentifierIterator {
9413 std::unique_ptr<IdentifierIterator> Current;
9414 std::unique_ptr<IdentifierIterator> Queued;
9415
9416public:
9417 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9418 std::unique_ptr<IdentifierIterator> Second)
9419 : Current(std::move(First)), Queued(std::move(Second)) {}
9420
9421 StringRef Next() override {
9422 if (!Current)
9423 return StringRef();
9424
9425 StringRef result = Current->Next();
9426 if (!result.empty())
9427 return result;
9428
9429 // Try the queued iterator, which may itself be empty.
9430 Current.reset();
9431 std::swap(x&: Current, y&: Queued);
9432 return Next();
9433 }
9434};
9435
9436} // namespace
9437
9438IdentifierIterator *ASTReader::getIdentifiers() {
9439 if (!loadGlobalIndex()) {
9440 std::unique_ptr<IdentifierIterator> ReaderIter(
9441 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9442 std::unique_ptr<IdentifierIterator> ModulesIter(
9443 GlobalIndex->createIdentifierIterator());
9444 return new ChainedIdentifierIterator(std::move(ReaderIter),
9445 std::move(ModulesIter));
9446 }
9447
9448 return new ASTIdentifierIterator(*this);
9449}
9450
9451namespace clang {
9452namespace serialization {
9453
9454 class ReadMethodPoolVisitor {
9455 ASTReader &Reader;
9456 Selector Sel;
9457 unsigned PriorGeneration;
9458 unsigned InstanceBits = 0;
9459 unsigned FactoryBits = 0;
9460 bool InstanceHasMoreThanOneDecl = false;
9461 bool FactoryHasMoreThanOneDecl = false;
9462 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9463 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9464
9465 public:
9466 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9467 unsigned PriorGeneration)
9468 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9469
9470 bool operator()(ModuleFile &M) {
9471 if (!M.SelectorLookupTable)
9472 return false;
9473
9474 // If we've already searched this module file, skip it now.
9475 if (M.Generation <= PriorGeneration)
9476 return true;
9477
9478 ++Reader.NumMethodPoolTableLookups;
9479 ASTSelectorLookupTable *PoolTable
9480 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9481 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9482 if (Pos == PoolTable->end())
9483 return false;
9484
9485 ++Reader.NumMethodPoolTableHits;
9486 ++Reader.NumSelectorsRead;
9487 // FIXME: Not quite happy with the statistics here. We probably should
9488 // disable this tracking when called via LoadSelector.
9489 // Also, should entries without methods count as misses?
9490 ++Reader.NumMethodPoolEntriesRead;
9491 ASTSelectorLookupTrait::data_type Data = *Pos;
9492 if (Reader.DeserializationListener)
9493 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9494
9495 // Append methods in the reverse order, so that later we can process them
9496 // in the order they appear in the source code by iterating through
9497 // the vector in the reverse order.
9498 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9499 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9500 InstanceBits = Data.InstanceBits;
9501 FactoryBits = Data.FactoryBits;
9502 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9503 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9504 return false;
9505 }
9506
9507 /// Retrieve the instance methods found by this visitor.
9508 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9509 return InstanceMethods;
9510 }
9511
9512 /// Retrieve the instance methods found by this visitor.
9513 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9514 return FactoryMethods;
9515 }
9516
9517 unsigned getInstanceBits() const { return InstanceBits; }
9518 unsigned getFactoryBits() const { return FactoryBits; }
9519
9520 bool instanceHasMoreThanOneDecl() const {
9521 return InstanceHasMoreThanOneDecl;
9522 }
9523
9524 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9525 };
9526
9527} // namespace serialization
9528} // namespace clang
9529
9530/// Add the given set of methods to the method list.
9531static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9532 ObjCMethodList &List) {
9533 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9534 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9535}
9536
9537void ASTReader::ReadMethodPool(Selector Sel) {
9538 // Get the selector generation and update it to the current generation.
9539 unsigned &Generation = SelectorGeneration[Sel];
9540 unsigned PriorGeneration = Generation;
9541 Generation = getGeneration();
9542 SelectorOutOfDate[Sel] = false;
9543
9544 // Search for methods defined with this selector.
9545 ++NumMethodPoolLookups;
9546 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9547 ModuleMgr.visit(Visitor);
9548
9549 if (Visitor.getInstanceMethods().empty() &&
9550 Visitor.getFactoryMethods().empty())
9551 return;
9552
9553 ++NumMethodPoolHits;
9554
9555 if (!getSema())
9556 return;
9557
9558 Sema &S = *getSema();
9559 auto &Methods = S.ObjC().MethodPool[Sel];
9560
9561 Methods.first.setBits(Visitor.getInstanceBits());
9562 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9563 Methods.second.setBits(Visitor.getFactoryBits());
9564 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9565
9566 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9567 // when building a module we keep every method individually and may need to
9568 // update hasMoreThanOneDecl as we add the methods.
9569 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9570 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9571}
9572
9573void ASTReader::updateOutOfDateSelector(Selector Sel) {
9574 if (SelectorOutOfDate[Sel])
9575 ReadMethodPool(Sel);
9576}
9577
9578void ASTReader::ReadKnownNamespaces(
9579 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9580 Namespaces.clear();
9581
9582 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9583 if (NamespaceDecl *Namespace
9584 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9585 Namespaces.push_back(Elt: Namespace);
9586 }
9587}
9588
9589void ASTReader::ReadUndefinedButUsed(
9590 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9591 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9592 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9593 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9594 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9595 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9596 }
9597 UndefinedButUsed.clear();
9598}
9599
9600void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9601 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9602 Exprs) {
9603 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9604 FieldDecl *FD =
9605 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9606 uint64_t Count = DelayedDeleteExprs[Idx++];
9607 for (uint64_t C = 0; C < Count; ++C) {
9608 SourceLocation DeleteLoc =
9609 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9610 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9611 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9612 }
9613 }
9614}
9615
9616void ASTReader::ReadTentativeDefinitions(
9617 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9618 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9619 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9620 if (Var)
9621 TentativeDefs.push_back(Elt: Var);
9622 }
9623 TentativeDefinitions.clear();
9624}
9625
9626void ASTReader::ReadUnusedFileScopedDecls(
9627 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9628 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9629 DeclaratorDecl *D
9630 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9631 if (D)
9632 Decls.push_back(Elt: D);
9633 }
9634 UnusedFileScopedDecls.clear();
9635}
9636
9637void ASTReader::ReadDelegatingConstructors(
9638 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9639 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9640 CXXConstructorDecl *D
9641 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9642 if (D)
9643 Decls.push_back(Elt: D);
9644 }
9645 DelegatingCtorDecls.clear();
9646}
9647
9648void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9649 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9650 TypedefNameDecl *D
9651 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9652 if (D)
9653 Decls.push_back(Elt: D);
9654 }
9655 ExtVectorDecls.clear();
9656}
9657
9658void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9659 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9660 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9661 ++I) {
9662 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9663 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9664 if (D)
9665 Decls.insert(X: D);
9666 }
9667 UnusedLocalTypedefNameCandidates.clear();
9668}
9669
9670void ASTReader::ReadDeclsToCheckForDeferredDiags(
9671 llvm::SmallSetVector<Decl *, 4> &Decls) {
9672 for (auto I : DeclsToCheckForDeferredDiags) {
9673 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9674 if (D)
9675 Decls.insert(X: D);
9676 }
9677 DeclsToCheckForDeferredDiags.clear();
9678}
9679
9680void ASTReader::ReadReferencedSelectors(
9681 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9682 if (ReferencedSelectorsData.empty())
9683 return;
9684
9685 // If there are @selector references added them to its pool. This is for
9686 // implementation of -Wselector.
9687 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9688 unsigned I = 0;
9689 while (I < DataSize) {
9690 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9691 SourceLocation SelLoc
9692 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9693 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9694 }
9695 ReferencedSelectorsData.clear();
9696}
9697
9698void ASTReader::ReadWeakUndeclaredIdentifiers(
9699 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9700 if (WeakUndeclaredIdentifiers.empty())
9701 return;
9702
9703 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9704 IdentifierInfo *WeakId
9705 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9706 IdentifierInfo *AliasId
9707 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9708 SourceLocation Loc =
9709 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9710 WeakInfo WI(AliasId, Loc);
9711 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9712 }
9713 WeakUndeclaredIdentifiers.clear();
9714}
9715
9716void ASTReader::ReadExtnameUndeclaredIdentifiers(
9717 SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs) {
9718 if (ExtnameUndeclaredIdentifiers.empty())
9719 return;
9720
9721 for (unsigned I = 0, N = ExtnameUndeclaredIdentifiers.size(); I < N; I += 3) {
9722 IdentifierInfo *NameId =
9723 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I]);
9724 IdentifierInfo *ExtnameId =
9725 DecodeIdentifierInfo(ID: ExtnameUndeclaredIdentifiers[I+1]);
9726 SourceLocation Loc =
9727 SourceLocation::getFromRawEncoding(Encoding: ExtnameUndeclaredIdentifiers[I+2]);
9728 AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
9729 Ctx&: getContext(), Label: ExtnameId->getName(),
9730 CommonInfo: AttributeCommonInfo(ExtnameId, SourceRange(Loc),
9731 AttributeCommonInfo::Form::Pragma()));
9732 ExtnameIDs.push_back(Elt: std::make_pair(x&: NameId, y&: Attr));
9733 }
9734 ExtnameUndeclaredIdentifiers.clear();
9735}
9736
9737void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9738 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9739 ExternalVTableUse VT;
9740 VTableUse &TableInfo = VTableUses[Idx++];
9741 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9742 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9743 VT.DefinitionRequired = TableInfo.Used;
9744 VTables.push_back(Elt: VT);
9745 }
9746
9747 VTableUses.clear();
9748}
9749
9750void ASTReader::ReadPendingInstantiations(
9751 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9752 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9753 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9754 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9755 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9756
9757 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9758 }
9759 PendingInstantiations.clear();
9760}
9761
9762void ASTReader::ReadLateParsedTemplates(
9763 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9764 &LPTMap) {
9765 for (auto &LPT : LateParsedTemplates) {
9766 ModuleFile *FMod = LPT.first;
9767 RecordDataImpl &LateParsed = LPT.second;
9768 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9769 /* In loop */) {
9770 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9771
9772 auto LT = std::make_unique<LateParsedTemplate>();
9773 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9774 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9775
9776 ModuleFile *F = getOwningModuleFile(D: LT->D);
9777 assert(F && "No module");
9778
9779 unsigned TokN = LateParsed[Idx++];
9780 LT->Toks.reserve(N: TokN);
9781 for (unsigned T = 0; T < TokN; ++T)
9782 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9783
9784 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9785 }
9786 }
9787
9788 LateParsedTemplates.clear();
9789}
9790
9791void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9792 if (!Lambda->getLambdaContextDecl())
9793 return;
9794
9795 auto LambdaInfo =
9796 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9797 y: Lambda->getLambdaIndexInContext());
9798
9799 // Handle the import and then include case for lambdas.
9800 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9801 Iter != LambdaDeclarationsForMerging.end() &&
9802 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9803 CXXRecordDecl *Previous =
9804 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9805 Lambda->setPreviousDecl(Previous);
9806 return;
9807 }
9808
9809 // Keep track of this lambda so it can be merged with another lambda that
9810 // is loaded later.
9811 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9812}
9813
9814void ASTReader::LoadSelector(Selector Sel) {
9815 // It would be complicated to avoid reading the methods anyway. So don't.
9816 ReadMethodPool(Sel);
9817}
9818
9819void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9820 assert(ID && "Non-zero identifier ID required");
9821 unsigned Index = translateIdentifierIDToIndex(ID).second;
9822 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9823 IdentifiersLoaded[Index] = II;
9824 if (DeserializationListener)
9825 DeserializationListener->IdentifierRead(ID, II);
9826}
9827
9828/// Set the globally-visible declarations associated with the given
9829/// identifier.
9830///
9831/// If the AST reader is currently in a state where the given declaration IDs
9832/// cannot safely be resolved, they are queued until it is safe to resolve
9833/// them.
9834///
9835/// \param II an IdentifierInfo that refers to one or more globally-visible
9836/// declarations.
9837///
9838/// \param DeclIDs the set of declaration IDs with the name @p II that are
9839/// visible at global scope.
9840///
9841/// \param Decls if non-null, this vector will be populated with the set of
9842/// deserialized declarations. These declarations will not be pushed into
9843/// scope.
9844void ASTReader::SetGloballyVisibleDecls(
9845 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9846 SmallVectorImpl<Decl *> *Decls) {
9847 if (NumCurrentElementsDeserializing && !Decls) {
9848 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9849 return;
9850 }
9851
9852 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9853 if (!SemaObj) {
9854 // Queue this declaration so that it will be added to the
9855 // translation unit scope and identifier's declaration chain
9856 // once a Sema object is known.
9857 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9858 continue;
9859 }
9860
9861 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9862
9863 // If we're simply supposed to record the declarations, do so now.
9864 if (Decls) {
9865 Decls->push_back(Elt: D);
9866 continue;
9867 }
9868
9869 // Introduce this declaration into the translation-unit scope
9870 // and add it to the declaration chain for this identifier, so
9871 // that (unqualified) name lookup will find it.
9872 pushExternalDeclIntoScope(D, Name: II);
9873 }
9874}
9875
9876std::pair<ModuleFile *, unsigned>
9877ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9878 if (ID == 0)
9879 return {nullptr, 0};
9880
9881 unsigned ModuleFileIndex = ID >> 32;
9882 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9883
9884 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9885 assert(getModuleManager().size() > ModuleFileIndex - 1);
9886
9887 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9888 assert(LocalID < MF.LocalNumIdentifiers);
9889 return {&MF, MF.BaseIdentifierID + LocalID};
9890}
9891
9892IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9893 if (ID == 0)
9894 return nullptr;
9895
9896 if (IdentifiersLoaded.empty()) {
9897 Error(Msg: "no identifier table in AST file");
9898 return nullptr;
9899 }
9900
9901 auto [M, Index] = translateIdentifierIDToIndex(ID);
9902 if (!IdentifiersLoaded[Index]) {
9903 assert(M != nullptr && "Untranslated Identifier ID?");
9904 assert(Index >= M->BaseIdentifierID);
9905 unsigned LocalIndex = Index - M->BaseIdentifierID;
9906 const unsigned char *Data =
9907 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9908
9909 ASTIdentifierLookupTrait Trait(*this, *M);
9910 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9911 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9912 auto &II = PP.getIdentifierTable().get(Name: Key);
9913 IdentifiersLoaded[Index] = &II;
9914 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9915 markIdentifierFromAST(Reader&: *this, II, IsModule);
9916 if (DeserializationListener)
9917 DeserializationListener->IdentifierRead(ID, II: &II);
9918 }
9919
9920 return IdentifiersLoaded[Index];
9921}
9922
9923IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9924 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9925}
9926
9927IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9928 if (LocalID < NUM_PREDEF_IDENT_IDS)
9929 return LocalID;
9930
9931 if (!M.ModuleOffsetMap.empty())
9932 ReadModuleOffsetMap(F&: M);
9933
9934 unsigned ModuleFileIndex = LocalID >> 32;
9935 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9936 ModuleFile *MF =
9937 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9938 assert(MF && "malformed identifier ID encoding?");
9939
9940 if (!ModuleFileIndex)
9941 LocalID -= NUM_PREDEF_IDENT_IDS;
9942
9943 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
9944}
9945
9946std::pair<ModuleFile *, unsigned>
9947ASTReader::translateMacroIDToIndex(MacroID ID) const {
9948 if (ID == 0)
9949 return {nullptr, 0};
9950
9951 unsigned ModuleFileIndex = ID >> 32;
9952 assert(ModuleFileIndex && "not translating loaded MacroID?");
9953 assert(getModuleManager().size() > ModuleFileIndex - 1);
9954 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9955
9956 unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(N: 32);
9957 assert(LocalID < MF.LocalNumMacros);
9958 return {&MF, MF.BaseMacroID + LocalID};
9959}
9960
9961MacroInfo *ASTReader::getMacro(MacroID ID) {
9962 if (ID == 0)
9963 return nullptr;
9964
9965 if (MacrosLoaded.empty()) {
9966 Error(Msg: "no macro table in AST file");
9967 return nullptr;
9968 }
9969
9970 auto [M, Index] = translateMacroIDToIndex(ID);
9971 if (!MacrosLoaded[Index]) {
9972 assert(M != nullptr && "Untranslated Macro ID?");
9973 assert(Index >= M->BaseMacroID);
9974 unsigned LocalIndex = Index - M->BaseMacroID;
9975 uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];
9976 MacrosLoaded[Index] = ReadMacroRecord(F&: *M, Offset: DataOffset);
9977
9978 if (DeserializationListener)
9979 DeserializationListener->MacroRead(ID, MI: MacrosLoaded[Index]);
9980 }
9981
9982 return MacrosLoaded[Index];
9983}
9984
9985MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {
9986 if (LocalID < NUM_PREDEF_MACRO_IDS)
9987 return LocalID;
9988
9989 if (!M.ModuleOffsetMap.empty())
9990 ReadModuleOffsetMap(F&: M);
9991
9992 unsigned ModuleFileIndex = LocalID >> 32;
9993 LocalID &= llvm::maskTrailingOnes<MacroID>(N: 32);
9994 ModuleFile *MF =
9995 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9996 assert(MF && "malformed identifier ID encoding?");
9997
9998 if (!ModuleFileIndex) {
9999 assert(LocalID >= NUM_PREDEF_MACRO_IDS);
10000 LocalID -= NUM_PREDEF_MACRO_IDS;
10001 }
10002
10003 return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;
10004}
10005
10006serialization::SubmoduleID
10007ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
10008 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
10009 return LocalID;
10010
10011 if (!M.ModuleOffsetMap.empty())
10012 ReadModuleOffsetMap(F&: M);
10013
10014 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10015 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
10016 assert(I != M.SubmoduleRemap.end()
10017 && "Invalid index into submodule index remap");
10018
10019 return LocalID + I->second;
10020}
10021
10022Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
10023 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
10024 assert(GlobalID == 0 && "Unhandled global submodule ID");
10025 return nullptr;
10026 }
10027
10028 if (GlobalID > SubmodulesLoaded.size()) {
10029 Error(Msg: "submodule ID out of range in AST file");
10030 return nullptr;
10031 }
10032
10033 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
10034}
10035
10036Module *ASTReader::getModule(unsigned ID) {
10037 return getSubmodule(GlobalID: ID);
10038}
10039
10040ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
10041 if (ID & 1) {
10042 // It's a module, look it up by submodule ID.
10043 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
10044 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
10045 } else {
10046 // It's a prefix (preamble, PCH, ...). Look it up by index.
10047 int IndexFromEnd = static_cast<int>(ID >> 1);
10048 assert(IndexFromEnd && "got reference to unknown module file");
10049 return getModuleManager().pch_modules().end()[-IndexFromEnd];
10050 }
10051}
10052
10053unsigned ASTReader::getModuleFileID(ModuleFile *M) {
10054 if (!M)
10055 return 1;
10056
10057 // For a file representing a module, use the submodule ID of the top-level
10058 // module as the file ID. For any other kind of file, the number of such
10059 // files loaded beforehand will be the same on reload.
10060 // FIXME: Is this true even if we have an explicit module file and a PCH?
10061 if (M->isModule())
10062 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
10063
10064 auto PCHModules = getModuleManager().pch_modules();
10065 auto I = llvm::find(Range&: PCHModules, Val: M);
10066 assert(I != PCHModules.end() && "emitting reference to unknown file");
10067 return std::distance(first: I, last: PCHModules.end()) << 1;
10068}
10069
10070std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
10071 if (Module *M = getSubmodule(GlobalID: ID))
10072 return ASTSourceDescriptor(*M);
10073
10074 // If there is only a single PCH, return it instead.
10075 // Chained PCH are not supported.
10076 const auto &PCHChain = ModuleMgr.pch_modules();
10077 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
10078 ModuleFile &MF = ModuleMgr.getPrimaryModule();
10079 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
10080 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
10081 return ASTSourceDescriptor(ModuleName,
10082 llvm::sys::path::parent_path(path: MF.FileName),
10083 FileName, MF.Signature);
10084 }
10085 return std::nullopt;
10086}
10087
10088ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
10089 auto I = DefinitionSource.find(Val: FD);
10090 if (I == DefinitionSource.end())
10091 return EK_ReplyHazy;
10092 return I->second ? EK_Never : EK_Always;
10093}
10094
10095bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
10096 return ThisDeclarationWasADefinitionSet.contains(V: FD);
10097}
10098
10099Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
10100 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
10101}
10102
10103Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
10104 if (ID == 0)
10105 return Selector();
10106
10107 if (ID > SelectorsLoaded.size()) {
10108 Error(Msg: "selector ID out of range in AST file");
10109 return Selector();
10110 }
10111
10112 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
10113 // Load this selector from the selector table.
10114 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
10115 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
10116 ModuleFile &M = *I->second;
10117 ASTSelectorLookupTrait Trait(*this, M);
10118 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
10119 SelectorsLoaded[ID - 1] =
10120 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
10121 if (DeserializationListener)
10122 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
10123 }
10124
10125 return SelectorsLoaded[ID - 1];
10126}
10127
10128Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
10129 return DecodeSelector(ID);
10130}
10131
10132uint32_t ASTReader::GetNumExternalSelectors() {
10133 // ID 0 (the null selector) is considered an external selector.
10134 return getTotalNumSelectors() + 1;
10135}
10136
10137serialization::SelectorID
10138ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
10139 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
10140 return LocalID;
10141
10142 if (!M.ModuleOffsetMap.empty())
10143 ReadModuleOffsetMap(F&: M);
10144
10145 ContinuousRangeMap<uint32_t, int, 2>::iterator I
10146 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
10147 assert(I != M.SelectorRemap.end()
10148 && "Invalid index into selector index remap");
10149
10150 return LocalID + I->second;
10151}
10152
10153DeclarationNameLoc
10154ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
10155 switch (Name.getNameKind()) {
10156 case DeclarationName::CXXConstructorName:
10157 case DeclarationName::CXXDestructorName:
10158 case DeclarationName::CXXConversionFunctionName:
10159 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
10160
10161 case DeclarationName::CXXOperatorName:
10162 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
10163
10164 case DeclarationName::CXXLiteralOperatorName:
10165 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
10166 Loc: readSourceLocation());
10167
10168 case DeclarationName::Identifier:
10169 case DeclarationName::ObjCZeroArgSelector:
10170 case DeclarationName::ObjCOneArgSelector:
10171 case DeclarationName::ObjCMultiArgSelector:
10172 case DeclarationName::CXXUsingDirective:
10173 case DeclarationName::CXXDeductionGuideName:
10174 break;
10175 }
10176 return DeclarationNameLoc();
10177}
10178
10179DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
10180 DeclarationNameInfo NameInfo;
10181 NameInfo.setName(readDeclarationName());
10182 NameInfo.setLoc(readSourceLocation());
10183 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
10184 return NameInfo;
10185}
10186
10187TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
10188 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
10189}
10190
10191SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
10192 auto Kind = readInt();
10193 auto ResultType = readQualType();
10194 auto Value = readAPInt();
10195 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
10196 assert(Op.isValid());
10197 return Op;
10198}
10199
10200void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
10201 Info.QualifierLoc = readNestedNameSpecifierLoc();
10202 unsigned NumTPLists = readInt();
10203 Info.NumTemplParamLists = NumTPLists;
10204 if (NumTPLists) {
10205 Info.TemplParamLists =
10206 new (getContext()) TemplateParameterList *[NumTPLists];
10207 for (unsigned i = 0; i != NumTPLists; ++i)
10208 Info.TemplParamLists[i] = readTemplateParameterList();
10209 }
10210}
10211
10212TemplateParameterList *
10213ASTRecordReader::readTemplateParameterList() {
10214 SourceLocation TemplateLoc = readSourceLocation();
10215 SourceLocation LAngleLoc = readSourceLocation();
10216 SourceLocation RAngleLoc = readSourceLocation();
10217
10218 unsigned NumParams = readInt();
10219 SmallVector<NamedDecl *, 16> Params;
10220 Params.reserve(N: NumParams);
10221 while (NumParams--)
10222 Params.push_back(Elt: readDeclAs<NamedDecl>());
10223
10224 bool HasRequiresClause = readBool();
10225 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
10226
10227 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
10228 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
10229 return TemplateParams;
10230}
10231
10232void ASTRecordReader::readTemplateArgumentList(
10233 SmallVectorImpl<TemplateArgument> &TemplArgs,
10234 bool Canonicalize) {
10235 unsigned NumTemplateArgs = readInt();
10236 TemplArgs.reserve(N: NumTemplateArgs);
10237 while (NumTemplateArgs--)
10238 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
10239}
10240
10241/// Read a UnresolvedSet structure.
10242void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
10243 unsigned NumDecls = readInt();
10244 Set.reserve(C&: getContext(), N: NumDecls);
10245 while (NumDecls--) {
10246 GlobalDeclID ID = readDeclID();
10247 AccessSpecifier AS = (AccessSpecifier) readInt();
10248 Set.addLazyDecl(C&: getContext(), ID, AS);
10249 }
10250}
10251
10252CXXBaseSpecifier
10253ASTRecordReader::readCXXBaseSpecifier() {
10254 bool isVirtual = readBool();
10255 bool isBaseOfClass = readBool();
10256 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
10257 bool inheritConstructors = readBool();
10258 TypeSourceInfo *TInfo = readTypeSourceInfo();
10259 SourceRange Range = readSourceRange();
10260 SourceLocation EllipsisLoc = readSourceLocation();
10261 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
10262 EllipsisLoc);
10263 Result.setInheritConstructors(inheritConstructors);
10264 return Result;
10265}
10266
10267CXXCtorInitializer **
10268ASTRecordReader::readCXXCtorInitializers() {
10269 ASTContext &Context = getContext();
10270 unsigned NumInitializers = readInt();
10271 assert(NumInitializers && "wrote ctor initializers but have no inits");
10272 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
10273 for (unsigned i = 0; i != NumInitializers; ++i) {
10274 TypeSourceInfo *TInfo = nullptr;
10275 bool IsBaseVirtual = false;
10276 FieldDecl *Member = nullptr;
10277 IndirectFieldDecl *IndirectMember = nullptr;
10278
10279 CtorInitializerType Type = (CtorInitializerType) readInt();
10280 switch (Type) {
10281 case CTOR_INITIALIZER_BASE:
10282 TInfo = readTypeSourceInfo();
10283 IsBaseVirtual = readBool();
10284 break;
10285
10286 case CTOR_INITIALIZER_DELEGATING:
10287 TInfo = readTypeSourceInfo();
10288 break;
10289
10290 case CTOR_INITIALIZER_MEMBER:
10291 Member = readDeclAs<FieldDecl>();
10292 break;
10293
10294 case CTOR_INITIALIZER_INDIRECT_MEMBER:
10295 IndirectMember = readDeclAs<IndirectFieldDecl>();
10296 break;
10297 }
10298
10299 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
10300 Expr *Init = readExpr();
10301 SourceLocation LParenLoc = readSourceLocation();
10302 SourceLocation RParenLoc = readSourceLocation();
10303
10304 CXXCtorInitializer *BOMInit;
10305 if (Type == CTOR_INITIALIZER_BASE)
10306 BOMInit = new (Context)
10307 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
10308 RParenLoc, MemberOrEllipsisLoc);
10309 else if (Type == CTOR_INITIALIZER_DELEGATING)
10310 BOMInit = new (Context)
10311 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
10312 else if (Member)
10313 BOMInit = new (Context)
10314 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
10315 Init, RParenLoc);
10316 else
10317 BOMInit = new (Context)
10318 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
10319 LParenLoc, Init, RParenLoc);
10320
10321 if (/*IsWritten*/readBool()) {
10322 unsigned SourceOrder = readInt();
10323 BOMInit->setSourceOrder(SourceOrder);
10324 }
10325
10326 CtorInitializers[i] = BOMInit;
10327 }
10328
10329 return CtorInitializers;
10330}
10331
10332NestedNameSpecifierLoc
10333ASTRecordReader::readNestedNameSpecifierLoc() {
10334 ASTContext &Context = getContext();
10335 unsigned N = readInt();
10336 NestedNameSpecifierLocBuilder Builder;
10337 for (unsigned I = 0; I != N; ++I) {
10338 auto Kind = readNestedNameSpecifierKind();
10339 switch (Kind) {
10340 case NestedNameSpecifier::Kind::Namespace: {
10341 auto *NS = readDeclAs<NamespaceBaseDecl>();
10342 SourceRange Range = readSourceRange();
10343 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10344 break;
10345 }
10346
10347 case NestedNameSpecifier::Kind::Type: {
10348 TypeSourceInfo *T = readTypeSourceInfo();
10349 if (!T)
10350 return NestedNameSpecifierLoc();
10351 SourceLocation ColonColonLoc = readSourceLocation();
10352 Builder.Make(Context, TL: T->getTypeLoc(), ColonColonLoc);
10353 break;
10354 }
10355
10356 case NestedNameSpecifier::Kind::Global: {
10357 SourceLocation ColonColonLoc = readSourceLocation();
10358 Builder.MakeGlobal(Context, ColonColonLoc);
10359 break;
10360 }
10361
10362 case NestedNameSpecifier::Kind::MicrosoftSuper: {
10363 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10364 SourceRange Range = readSourceRange();
10365 Builder.MakeMicrosoftSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10366 break;
10367 }
10368
10369 case NestedNameSpecifier::Kind::Null:
10370 llvm_unreachable("unexpected null nested name specifier");
10371 }
10372 }
10373
10374 return Builder.getWithLocInContext(Context);
10375}
10376
10377SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10378 unsigned &Idx) {
10379 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10380 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10381 return SourceRange(beg, end);
10382}
10383
10384llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10385 const StringRef Blob) {
10386 unsigned Count = Record[0];
10387 const char *Byte = Blob.data();
10388 llvm::BitVector Ret = llvm::BitVector(Count, false);
10389 for (unsigned I = 0; I < Count; ++Byte)
10390 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10391 if (*Byte & (1 << Bit))
10392 Ret[I] = true;
10393 return Ret;
10394}
10395
10396/// Read a floating-point value
10397llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10398 return llvm::APFloat(Sem, readAPInt());
10399}
10400
10401// Read a string
10402std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10403 unsigned Len = Record[Idx++];
10404 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10405 Idx += Len;
10406 return Result;
10407}
10408
10409StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10410 StringRef &Blob) {
10411 unsigned Len = Record[Idx++];
10412 StringRef Result = Blob.substr(Start: 0, N: Len);
10413 Blob = Blob.substr(Start: Len);
10414 return Result;
10415}
10416
10417std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10418 unsigned &Idx) {
10419 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10420}
10421
10422std::string ASTReader::ReadPath(StringRef BaseDirectory,
10423 const RecordData &Record, unsigned &Idx) {
10424 std::string Filename = ReadString(Record, Idx);
10425 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10426}
10427
10428std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10429 const RecordData &Record, unsigned &Idx,
10430 StringRef &Blob) {
10431 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10432 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10433}
10434
10435VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10436 unsigned &Idx) {
10437 unsigned Major = Record[Idx++];
10438 unsigned Minor = Record[Idx++];
10439 unsigned Subminor = Record[Idx++];
10440 if (Minor == 0)
10441 return VersionTuple(Major);
10442 if (Subminor == 0)
10443 return VersionTuple(Major, Minor - 1);
10444 return VersionTuple(Major, Minor - 1, Subminor - 1);
10445}
10446
10447CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10448 const RecordData &Record,
10449 unsigned &Idx) {
10450 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10451 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10452}
10453
10454DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10455 return Diag(Loc: CurrentImportLoc, DiagID);
10456}
10457
10458DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10459 return Diags.Report(Loc, DiagID);
10460}
10461
10462void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10463 llvm::function_ref<void()> Fn) {
10464 // When Sema is available, avoid duplicate errors.
10465 if (SemaObj) {
10466 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10467 return;
10468 }
10469
10470 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10471}
10472
10473/// Retrieve the identifier table associated with the
10474/// preprocessor.
10475IdentifierTable &ASTReader::getIdentifierTable() {
10476 return PP.getIdentifierTable();
10477}
10478
10479/// Record that the given ID maps to the given switch-case
10480/// statement.
10481void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10482 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10483 "Already have a SwitchCase with this ID");
10484 (*CurrSwitchCaseStmts)[ID] = SC;
10485}
10486
10487/// Retrieve the switch-case statement with the given ID.
10488SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10489 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10490 return (*CurrSwitchCaseStmts)[ID];
10491}
10492
10493void ASTReader::ClearSwitchCaseIDs() {
10494 CurrSwitchCaseStmts->clear();
10495}
10496
10497void ASTReader::ReadComments() {
10498 ASTContext &Context = getContext();
10499 std::vector<RawComment *> Comments;
10500 for (SmallVectorImpl<std::pair<BitstreamCursor,
10501 serialization::ModuleFile *>>::iterator
10502 I = CommentsCursors.begin(),
10503 E = CommentsCursors.end();
10504 I != E; ++I) {
10505 Comments.clear();
10506 BitstreamCursor &Cursor = I->first;
10507 serialization::ModuleFile &F = *I->second;
10508 SavedStreamPosition SavedPosition(Cursor);
10509
10510 RecordData Record;
10511 while (true) {
10512 Expected<llvm::BitstreamEntry> MaybeEntry =
10513 Cursor.advanceSkippingSubblocks(
10514 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10515 if (!MaybeEntry) {
10516 Error(Err: MaybeEntry.takeError());
10517 return;
10518 }
10519 llvm::BitstreamEntry Entry = MaybeEntry.get();
10520
10521 switch (Entry.Kind) {
10522 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10523 case llvm::BitstreamEntry::Error:
10524 Error(Msg: "malformed block record in AST file");
10525 return;
10526 case llvm::BitstreamEntry::EndBlock:
10527 goto NextCursor;
10528 case llvm::BitstreamEntry::Record:
10529 // The interesting case.
10530 break;
10531 }
10532
10533 // Read a record.
10534 Record.clear();
10535 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10536 if (!MaybeComment) {
10537 Error(Err: MaybeComment.takeError());
10538 return;
10539 }
10540 switch ((CommentRecordTypes)MaybeComment.get()) {
10541 case COMMENTS_RAW_COMMENT: {
10542 unsigned Idx = 0;
10543 SourceRange SR = ReadSourceRange(F, Record, Idx);
10544 RawComment::CommentKind Kind =
10545 (RawComment::CommentKind) Record[Idx++];
10546 bool IsTrailingComment = Record[Idx++];
10547 bool IsAlmostTrailingComment = Record[Idx++];
10548 Comments.push_back(x: new (Context) RawComment(
10549 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10550 break;
10551 }
10552 }
10553 }
10554 NextCursor:
10555 for (RawComment *C : Comments) {
10556 SourceLocation CommentLoc = C->getBeginLoc();
10557 if (CommentLoc.isValid()) {
10558 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10559 if (Loc.first.isValid())
10560 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10561 }
10562 }
10563 }
10564}
10565
10566void ASTReader::visitInputFileInfos(
10567 serialization::ModuleFile &MF, bool IncludeSystem,
10568 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10569 bool IsSystem)>
10570 Visitor) {
10571 unsigned NumUserInputs = MF.NumUserInputFiles;
10572 unsigned NumInputs = MF.InputFilesLoaded.size();
10573 assert(NumUserInputs <= NumInputs);
10574 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10575 for (unsigned I = 0; I < N; ++I) {
10576 bool IsSystem = I >= NumUserInputs;
10577 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10578 Visitor(IFI, IsSystem);
10579 }
10580}
10581
10582void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10583 bool IncludeSystem, bool Complain,
10584 llvm::function_ref<void(const serialization::InputFile &IF,
10585 bool isSystem)> Visitor) {
10586 unsigned NumUserInputs = MF.NumUserInputFiles;
10587 unsigned NumInputs = MF.InputFilesLoaded.size();
10588 assert(NumUserInputs <= NumInputs);
10589 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10590 for (unsigned I = 0; I < N; ++I) {
10591 bool IsSystem = I >= NumUserInputs;
10592 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10593 Visitor(IF, IsSystem);
10594 }
10595}
10596
10597void ASTReader::visitTopLevelModuleMaps(
10598 serialization::ModuleFile &MF,
10599 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10600 unsigned NumInputs = MF.InputFilesLoaded.size();
10601 for (unsigned I = 0; I < NumInputs; ++I) {
10602 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10603 if (IFI.TopLevel && IFI.ModuleMap)
10604 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10605 Visitor(*FE);
10606 }
10607}
10608
10609void ASTReader::finishPendingActions() {
10610 while (!PendingIdentifierInfos.empty() ||
10611 !PendingDeducedFunctionTypes.empty() ||
10612 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10613 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10614 !PendingUpdateRecords.empty() ||
10615 !PendingObjCExtensionIvarRedeclarations.empty()) {
10616 // If any identifiers with corresponding top-level declarations have
10617 // been loaded, load those declarations now.
10618 using TopLevelDeclsMap =
10619 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10620 TopLevelDeclsMap TopLevelDecls;
10621
10622 while (!PendingIdentifierInfos.empty()) {
10623 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10624 SmallVector<GlobalDeclID, 4> DeclIDs =
10625 std::move(PendingIdentifierInfos.back().second);
10626 PendingIdentifierInfos.pop_back();
10627
10628 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10629 }
10630
10631 // Load each function type that we deferred loading because it was a
10632 // deduced type that might refer to a local type declared within itself.
10633 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10634 auto *FD = PendingDeducedFunctionTypes[I].first;
10635 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10636
10637 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10638 // If we gave a function a deduced return type, remember that we need to
10639 // propagate that along the redeclaration chain.
10640 if (DT->isDeduced()) {
10641 PendingDeducedTypeUpdates.insert(
10642 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10643 continue;
10644 }
10645
10646 // The function has undeduced DeduceType return type. We hope we can
10647 // find the deduced type by iterating the redecls in other modules
10648 // later.
10649 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10650 continue;
10651 }
10652 }
10653 PendingDeducedFunctionTypes.clear();
10654
10655 // Load each variable type that we deferred loading because it was a
10656 // deduced type that might refer to a local type declared within itself.
10657 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10658 auto *VD = PendingDeducedVarTypes[I].first;
10659 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10660 }
10661 PendingDeducedVarTypes.clear();
10662
10663 // Load pending declaration chains.
10664 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10665 loadPendingDeclChain(D: PendingDeclChains[I].first,
10666 LocalOffset: PendingDeclChains[I].second);
10667 PendingDeclChains.clear();
10668
10669 // Make the most recent of the top-level declarations visible.
10670 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10671 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10672 IdentifierInfo *II = TLD->first;
10673 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10674 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10675 }
10676 }
10677
10678 // Load any pending macro definitions.
10679 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10680 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10681 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10682 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10683 // Initialize the macro history from chained-PCHs ahead of module imports.
10684 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10685 ++IDIdx) {
10686 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10687 if (!Info.M->isModule())
10688 resolvePendingMacro(II, PMInfo: Info);
10689 }
10690 // Handle module imports.
10691 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10692 ++IDIdx) {
10693 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10694 if (Info.M->isModule())
10695 resolvePendingMacro(II, PMInfo: Info);
10696 }
10697 }
10698 PendingMacroIDs.clear();
10699
10700 // Wire up the DeclContexts for Decls that we delayed setting until
10701 // recursive loading is completed.
10702 while (!PendingDeclContextInfos.empty()) {
10703 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10704 PendingDeclContextInfos.pop_front();
10705 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10706 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10707 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10708 }
10709
10710 // Perform any pending declaration updates.
10711 while (!PendingUpdateRecords.empty()) {
10712 auto Update = PendingUpdateRecords.pop_back_val();
10713 ReadingKindTracker ReadingKind(Read_Decl, *this);
10714 loadDeclUpdateRecords(Record&: Update);
10715 }
10716
10717 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10718 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10719 auto DuplicateIvars =
10720 PendingObjCExtensionIvarRedeclarations.back().second;
10721 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10722 StructuralEquivalenceContext Ctx(
10723 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10724 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10725 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10726 /*Complain =*/false,
10727 /*ErrorOnTagTypeMismatch =*/true);
10728 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10729 // Merge redeclared ivars with their predecessors.
10730 for (auto IvarPair : DuplicateIvars) {
10731 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10732 // Change semantic DeclContext but keep the lexical one.
10733 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10734 LexicalDC: Ivar->getLexicalDeclContext(),
10735 Ctx&: getContext());
10736 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10737 }
10738 // Invalidate duplicate extension and the cached ivar list.
10739 ExtensionsPair.first->setInvalidDecl();
10740 ExtensionsPair.second->getClassInterface()
10741 ->getDefinition()
10742 ->setIvarList(nullptr);
10743 } else {
10744 for (auto IvarPair : DuplicateIvars) {
10745 Diag(Loc: IvarPair.first->getLocation(),
10746 DiagID: diag::err_duplicate_ivar_declaration)
10747 << IvarPair.first->getIdentifier();
10748 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10749 }
10750 }
10751 PendingObjCExtensionIvarRedeclarations.pop_back();
10752 }
10753 }
10754
10755 // At this point, all update records for loaded decls are in place, so any
10756 // fake class definitions should have become real.
10757 assert(PendingFakeDefinitionData.empty() &&
10758 "faked up a class definition but never saw the real one");
10759
10760 // If we deserialized any C++ or Objective-C class definitions, any
10761 // Objective-C protocol definitions, or any redeclarable templates, make sure
10762 // that all redeclarations point to the definitions. Note that this can only
10763 // happen now, after the redeclaration chains have been fully wired.
10764 for (Decl *D : PendingDefinitions) {
10765 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10766 if (auto *RD = dyn_cast<CXXRecordDecl>(Val: TD)) {
10767 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10768 R = R->getPreviousDecl()) {
10769 assert((R == D) ==
10770 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10771 "declaration thinks it's the definition but it isn't");
10772 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10773 }
10774 }
10775
10776 continue;
10777 }
10778
10779 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10780 // Make sure that the ObjCInterfaceType points at the definition.
10781 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10782 ->Decl = ID;
10783
10784 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10785 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10786
10787 continue;
10788 }
10789
10790 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10791 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10792 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10793
10794 continue;
10795 }
10796
10797 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10798 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10799 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10800 }
10801 PendingDefinitions.clear();
10802
10803 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10804 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10805 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10806 return VD->isThisDeclarationADefinition() ||
10807 VD->isThisDeclarationADemotedDefinition();
10808
10809 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10810 return TD->isThisDeclarationADefinition() ||
10811 TD->isThisDeclarationADemotedDefinition();
10812
10813 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10814 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10815
10816 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10817 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10818
10819 // Conservatively return false here.
10820 return false;
10821 };
10822
10823 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10824 return hasDefinitionImpl(D, hasDefinitionImpl);
10825 };
10826
10827 // It is not good to prevent multiple declarations since the forward
10828 // declaration is common. Let's try to avoid duplicated definitions
10829 // only.
10830 if (!hasDefinition(D) || !hasDefinition(Previous))
10831 continue;
10832
10833 Module *PM = Previous->getOwningModule();
10834 Module *DM = D->getOwningModule();
10835 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10836 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10837 << (DM ? DM->getTopLevelModuleName() : "global module");
10838 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10839 }
10840 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10841
10842 // Load the bodies of any functions or methods we've encountered. We do
10843 // this now (delayed) so that we can be sure that the declaration chains
10844 // have been fully wired up (hasBody relies on this).
10845 // FIXME: We shouldn't require complete redeclaration chains here.
10846 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10847 PBEnd = PendingBodies.end();
10848 PB != PBEnd; ++PB) {
10849 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10850 // FIXME: Check for =delete/=default?
10851 const FunctionDecl *Defn = nullptr;
10852 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10853 FD->setLazyBody(PB->second);
10854 } else {
10855 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10856 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10857
10858 if (!FD->isLateTemplateParsed() &&
10859 !NonConstDefn->isLateTemplateParsed() &&
10860 // We only perform ODR checks for decls not in the explicit
10861 // global module fragment.
10862 !shouldSkipCheckingODR(D: FD) &&
10863 !shouldSkipCheckingODR(D: NonConstDefn) &&
10864 FD->getODRHash() != NonConstDefn->getODRHash()) {
10865 if (!isa<CXXMethodDecl>(Val: FD)) {
10866 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10867 } else if (FD->getLexicalParent()->isFileContext() &&
10868 NonConstDefn->getLexicalParent()->isFileContext()) {
10869 // Only diagnose out-of-line method definitions. If they are
10870 // in class definitions, then an error will be generated when
10871 // processing the class bodies.
10872 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10873 }
10874 }
10875 }
10876 continue;
10877 }
10878
10879 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10880 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10881 MD->setLazyBody(PB->second);
10882 }
10883 PendingBodies.clear();
10884
10885 // Inform any classes that had members added that they now have more members.
10886 for (auto [RD, MD] : PendingAddedClassMembers) {
10887 RD->addedMember(D: MD);
10888 }
10889 PendingAddedClassMembers.clear();
10890
10891 // Do some cleanup.
10892 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10893 getContext().deduplicateMergedDefinitionsFor(ND);
10894 PendingMergedDefinitionsToDeduplicate.clear();
10895
10896 // For each decl chain that we wanted to complete while deserializing, mark
10897 // it as "still needs to be completed".
10898 for (Decl *D : PendingIncompleteDeclChains)
10899 markIncompleteDeclChain(D);
10900 PendingIncompleteDeclChains.clear();
10901
10902 assert(PendingIdentifierInfos.empty() &&
10903 "Should be empty at the end of finishPendingActions");
10904 assert(PendingDeducedFunctionTypes.empty() &&
10905 "Should be empty at the end of finishPendingActions");
10906 assert(PendingDeducedVarTypes.empty() &&
10907 "Should be empty at the end of finishPendingActions");
10908 assert(PendingDeclChains.empty() &&
10909 "Should be empty at the end of finishPendingActions");
10910 assert(PendingMacroIDs.empty() &&
10911 "Should be empty at the end of finishPendingActions");
10912 assert(PendingDeclContextInfos.empty() &&
10913 "Should be empty at the end of finishPendingActions");
10914 assert(PendingUpdateRecords.empty() &&
10915 "Should be empty at the end of finishPendingActions");
10916 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10917 "Should be empty at the end of finishPendingActions");
10918 assert(PendingFakeDefinitionData.empty() &&
10919 "Should be empty at the end of finishPendingActions");
10920 assert(PendingDefinitions.empty() &&
10921 "Should be empty at the end of finishPendingActions");
10922 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10923 "Should be empty at the end of finishPendingActions");
10924 assert(PendingBodies.empty() &&
10925 "Should be empty at the end of finishPendingActions");
10926 assert(PendingAddedClassMembers.empty() &&
10927 "Should be empty at the end of finishPendingActions");
10928 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10929 "Should be empty at the end of finishPendingActions");
10930 assert(PendingIncompleteDeclChains.empty() &&
10931 "Should be empty at the end of finishPendingActions");
10932}
10933
10934void ASTReader::diagnoseOdrViolations() {
10935 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10936 PendingRecordOdrMergeFailures.empty() &&
10937 PendingFunctionOdrMergeFailures.empty() &&
10938 PendingEnumOdrMergeFailures.empty() &&
10939 PendingObjCInterfaceOdrMergeFailures.empty() &&
10940 PendingObjCProtocolOdrMergeFailures.empty())
10941 return;
10942
10943 // Trigger the import of the full definition of each class that had any
10944 // odr-merging problems, so we can produce better diagnostics for them.
10945 // These updates may in turn find and diagnose some ODR failures, so take
10946 // ownership of the set first.
10947 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10948 PendingOdrMergeFailures.clear();
10949 for (auto &Merge : OdrMergeFailures) {
10950 Merge.first->buildLookup();
10951 Merge.first->decls_begin();
10952 Merge.first->bases_begin();
10953 Merge.first->vbases_begin();
10954 for (auto &RecordPair : Merge.second) {
10955 auto *RD = RecordPair.first;
10956 RD->decls_begin();
10957 RD->bases_begin();
10958 RD->vbases_begin();
10959 }
10960 }
10961
10962 // Trigger the import of the full definition of each record in C/ObjC.
10963 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
10964 PendingRecordOdrMergeFailures.clear();
10965 for (auto &Merge : RecordOdrMergeFailures) {
10966 Merge.first->decls_begin();
10967 for (auto &D : Merge.second)
10968 D->decls_begin();
10969 }
10970
10971 // Trigger the import of the full interface definition.
10972 auto ObjCInterfaceOdrMergeFailures =
10973 std::move(PendingObjCInterfaceOdrMergeFailures);
10974 PendingObjCInterfaceOdrMergeFailures.clear();
10975 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10976 Merge.first->decls_begin();
10977 for (auto &InterfacePair : Merge.second)
10978 InterfacePair.first->decls_begin();
10979 }
10980
10981 // Trigger the import of functions.
10982 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10983 PendingFunctionOdrMergeFailures.clear();
10984 for (auto &Merge : FunctionOdrMergeFailures) {
10985 Merge.first->buildLookup();
10986 Merge.first->decls_begin();
10987 Merge.first->getBody();
10988 for (auto &FD : Merge.second) {
10989 FD->buildLookup();
10990 FD->decls_begin();
10991 FD->getBody();
10992 }
10993 }
10994
10995 // Trigger the import of enums.
10996 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10997 PendingEnumOdrMergeFailures.clear();
10998 for (auto &Merge : EnumOdrMergeFailures) {
10999 Merge.first->decls_begin();
11000 for (auto &Enum : Merge.second) {
11001 Enum->decls_begin();
11002 }
11003 }
11004
11005 // Trigger the import of the full protocol definition.
11006 auto ObjCProtocolOdrMergeFailures =
11007 std::move(PendingObjCProtocolOdrMergeFailures);
11008 PendingObjCProtocolOdrMergeFailures.clear();
11009 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11010 Merge.first->decls_begin();
11011 for (auto &ProtocolPair : Merge.second)
11012 ProtocolPair.first->decls_begin();
11013 }
11014
11015 // For each declaration from a merged context, check that the canonical
11016 // definition of that context also contains a declaration of the same
11017 // entity.
11018 //
11019 // Caution: this loop does things that might invalidate iterators into
11020 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
11021 while (!PendingOdrMergeChecks.empty()) {
11022 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
11023
11024 // FIXME: Skip over implicit declarations for now. This matters for things
11025 // like implicitly-declared special member functions. This isn't entirely
11026 // correct; we can end up with multiple unmerged declarations of the same
11027 // implicit entity.
11028 if (D->isImplicit())
11029 continue;
11030
11031 DeclContext *CanonDef = D->getDeclContext();
11032
11033 bool Found = false;
11034 const Decl *DCanon = D->getCanonicalDecl();
11035
11036 for (auto *RI : D->redecls()) {
11037 if (RI->getLexicalDeclContext() == CanonDef) {
11038 Found = true;
11039 break;
11040 }
11041 }
11042 if (Found)
11043 continue;
11044
11045 // Quick check failed, time to do the slow thing. Note, we can't just
11046 // look up the name of D in CanonDef here, because the member that is
11047 // in CanonDef might not be found by name lookup (it might have been
11048 // replaced by a more recent declaration in the lookup table), and we
11049 // can't necessarily find it in the redeclaration chain because it might
11050 // be merely mergeable, not redeclarable.
11051 llvm::SmallVector<const NamedDecl*, 4> Candidates;
11052 for (auto *CanonMember : CanonDef->decls()) {
11053 if (CanonMember->getCanonicalDecl() == DCanon) {
11054 // This can happen if the declaration is merely mergeable and not
11055 // actually redeclarable (we looked for redeclarations earlier).
11056 //
11057 // FIXME: We should be able to detect this more efficiently, without
11058 // pulling in all of the members of CanonDef.
11059 Found = true;
11060 break;
11061 }
11062 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
11063 if (ND->getDeclName() == D->getDeclName())
11064 Candidates.push_back(Elt: ND);
11065 }
11066
11067 if (!Found) {
11068 // The AST doesn't like TagDecls becoming invalid after they've been
11069 // completed. We only really need to mark FieldDecls as invalid here.
11070 if (!isa<TagDecl>(Val: D))
11071 D->setInvalidDecl();
11072
11073 // Ensure we don't accidentally recursively enter deserialization while
11074 // we're producing our diagnostic.
11075 Deserializing RecursionGuard(this);
11076
11077 std::string CanonDefModule =
11078 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
11079 D: cast<Decl>(Val: CanonDef));
11080 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
11081 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
11082 << CanonDef << CanonDefModule.empty() << CanonDefModule;
11083
11084 if (Candidates.empty())
11085 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
11086 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
11087 else {
11088 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
11089 Diag(Loc: Candidates[I]->getLocation(),
11090 DiagID: diag::note_module_odr_violation_possible_decl)
11091 << Candidates[I];
11092 }
11093
11094 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
11095 }
11096 }
11097
11098 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
11099 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
11100 ObjCInterfaceOdrMergeFailures.empty() &&
11101 ObjCProtocolOdrMergeFailures.empty())
11102 return;
11103
11104 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
11105 getPreprocessor().getLangOpts());
11106
11107 // Issue any pending ODR-failure diagnostics.
11108 for (auto &Merge : OdrMergeFailures) {
11109 // If we've already pointed out a specific problem with this class, don't
11110 // bother issuing a general "something's different" diagnostic.
11111 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11112 continue;
11113
11114 bool Diagnosed = false;
11115 CXXRecordDecl *FirstRecord = Merge.first;
11116 for (auto &RecordPair : Merge.second) {
11117 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
11118 SecondDD: RecordPair.second)) {
11119 Diagnosed = true;
11120 break;
11121 }
11122 }
11123
11124 if (!Diagnosed) {
11125 // All definitions are updates to the same declaration. This happens if a
11126 // module instantiates the declaration of a class template specialization
11127 // and two or more other modules instantiate its definition.
11128 //
11129 // FIXME: Indicate which modules had instantiations of this definition.
11130 // FIXME: How can this even happen?
11131 Diag(Loc: Merge.first->getLocation(),
11132 DiagID: diag::err_module_odr_violation_different_instantiations)
11133 << Merge.first;
11134 }
11135 }
11136
11137 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
11138 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
11139 for (auto &Merge : RecordOdrMergeFailures) {
11140 // If we've already pointed out a specific problem with this class, don't
11141 // bother issuing a general "something's different" diagnostic.
11142 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11143 continue;
11144
11145 RecordDecl *FirstRecord = Merge.first;
11146 bool Diagnosed = false;
11147 for (auto *SecondRecord : Merge.second) {
11148 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
11149 Diagnosed = true;
11150 break;
11151 }
11152 }
11153 (void)Diagnosed;
11154 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11155 }
11156
11157 // Issue ODR failures diagnostics for functions.
11158 for (auto &Merge : FunctionOdrMergeFailures) {
11159 FunctionDecl *FirstFunction = Merge.first;
11160 bool Diagnosed = false;
11161 for (auto &SecondFunction : Merge.second) {
11162 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
11163 Diagnosed = true;
11164 break;
11165 }
11166 }
11167 (void)Diagnosed;
11168 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11169 }
11170
11171 // Issue ODR failures diagnostics for enums.
11172 for (auto &Merge : EnumOdrMergeFailures) {
11173 // If we've already pointed out a specific problem with this enum, don't
11174 // bother issuing a general "something's different" diagnostic.
11175 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11176 continue;
11177
11178 EnumDecl *FirstEnum = Merge.first;
11179 bool Diagnosed = false;
11180 for (auto &SecondEnum : Merge.second) {
11181 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
11182 Diagnosed = true;
11183 break;
11184 }
11185 }
11186 (void)Diagnosed;
11187 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11188 }
11189
11190 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
11191 // If we've already pointed out a specific problem with this interface,
11192 // don't bother issuing a general "something's different" diagnostic.
11193 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11194 continue;
11195
11196 bool Diagnosed = false;
11197 ObjCInterfaceDecl *FirstID = Merge.first;
11198 for (auto &InterfacePair : Merge.second) {
11199 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
11200 SecondDD: InterfacePair.second)) {
11201 Diagnosed = true;
11202 break;
11203 }
11204 }
11205 (void)Diagnosed;
11206 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11207 }
11208
11209 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
11210 // If we've already pointed out a specific problem with this protocol,
11211 // don't bother issuing a general "something's different" diagnostic.
11212 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
11213 continue;
11214
11215 ObjCProtocolDecl *FirstProtocol = Merge.first;
11216 bool Diagnosed = false;
11217 for (auto &ProtocolPair : Merge.second) {
11218 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
11219 SecondDD: ProtocolPair.second)) {
11220 Diagnosed = true;
11221 break;
11222 }
11223 }
11224 (void)Diagnosed;
11225 assert(Diagnosed && "Unable to emit ODR diagnostic.");
11226 }
11227}
11228
11229void ASTReader::StartedDeserializing() {
11230 if (llvm::Timer *T = ReadTimer.get();
11231 ++NumCurrentElementsDeserializing == 1 && T)
11232 ReadTimeRegion.emplace(args&: T);
11233}
11234
11235void ASTReader::FinishedDeserializing() {
11236 assert(NumCurrentElementsDeserializing &&
11237 "FinishedDeserializing not paired with StartedDeserializing");
11238 if (NumCurrentElementsDeserializing == 1) {
11239 // We decrease NumCurrentElementsDeserializing only after pending actions
11240 // are finished, to avoid recursively re-calling finishPendingActions().
11241 finishPendingActions();
11242 }
11243 --NumCurrentElementsDeserializing;
11244
11245 if (NumCurrentElementsDeserializing == 0) {
11246 {
11247 // Guard variable to avoid recursively entering the process of passing
11248 // decls to consumer.
11249 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
11250 /*NewValue=*/false);
11251
11252 // Propagate exception specification and deduced type updates along
11253 // redeclaration chains.
11254 //
11255 // We do this now rather than in finishPendingActions because we want to
11256 // be able to walk the complete redeclaration chains of the updated decls.
11257 while (!PendingExceptionSpecUpdates.empty() ||
11258 !PendingDeducedTypeUpdates.empty() ||
11259 !PendingUndeducedFunctionDecls.empty()) {
11260 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11261 PendingExceptionSpecUpdates.clear();
11262 for (auto Update : ESUpdates) {
11263 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11264 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11265 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11266 if (auto *Listener = getContext().getASTMutationListener())
11267 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
11268 for (auto *Redecl : Update.second->redecls())
11269 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
11270 }
11271
11272 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11273 PendingDeducedTypeUpdates.clear();
11274 for (auto Update : DTUpdates) {
11275 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11276 // FIXME: If the return type is already deduced, check that it
11277 // matches.
11278 getContext().adjustDeducedFunctionResultType(FD: Update.first,
11279 ResultType: Update.second);
11280 }
11281
11282 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
11283 PendingUndeducedFunctionDecls.clear();
11284 // We hope we can find the deduced type for the functions by iterating
11285 // redeclarations in other modules.
11286 for (FunctionDecl *UndeducedFD : UDTUpdates)
11287 (void)UndeducedFD->getMostRecentDecl();
11288 }
11289
11290 ReadTimeRegion.reset();
11291
11292 diagnoseOdrViolations();
11293 }
11294
11295 // We are not in recursive loading, so it's safe to pass the "interesting"
11296 // decls to the consumer.
11297 if (Consumer)
11298 PassInterestingDeclsToConsumer();
11299 }
11300}
11301
11302void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11303 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11304 // Remove any fake results before adding any real ones.
11305 auto It = PendingFakeLookupResults.find(Key: II);
11306 if (It != PendingFakeLookupResults.end()) {
11307 for (auto *ND : It->second)
11308 SemaObj->IdResolver.RemoveDecl(D: ND);
11309 // FIXME: this works around module+PCH performance issue.
11310 // Rather than erase the result from the map, which is O(n), just clear
11311 // the vector of NamedDecls.
11312 It->second.clear();
11313 }
11314 }
11315
11316 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11317 SemaObj->TUScope->AddDecl(D);
11318 } else if (SemaObj->TUScope) {
11319 // Adding the decl to IdResolver may have failed because it was already in
11320 // (even though it was not added in scope). If it is already in, make sure
11321 // it gets in the scope as well.
11322 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
11323 SemaObj->TUScope->AddDecl(D);
11324 }
11325}
11326
11327ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
11328 ASTContext *Context,
11329 const PCHContainerReader &PCHContainerRdr,
11330 const CodeGenOptions &CodeGenOpts,
11331 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11332 StringRef isysroot,
11333 DisableValidationForModuleKind DisableValidationKind,
11334 bool AllowASTWithCompilerErrors,
11335 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11336 bool ForceValidateUserInputs,
11337 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11338 std::unique_ptr<llvm::Timer> ReadTimer)
11339 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11340 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11341 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11342 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11343 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11344 StackHandler(Diags), PP(PP), ContextObj(Context),
11345 CodeGenOpts(CodeGenOpts),
11346 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11347 PP.getHeaderSearchInfo()),
11348 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11349 DisableValidationKind(DisableValidationKind),
11350 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11351 AllowConfigurationMismatch(AllowConfigurationMismatch),
11352 ValidateSystemInputs(ValidateSystemInputs),
11353 ForceValidateUserInputs(ForceValidateUserInputs),
11354 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11355 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11356 SourceMgr.setExternalSLocEntrySource(this);
11357
11358 PathBuf.reserve(N: 256);
11359
11360 for (const auto &Ext : Extensions) {
11361 auto BlockName = Ext->getExtensionMetadata().BlockName;
11362 auto Known = ModuleFileExtensions.find(Key: BlockName);
11363 if (Known != ModuleFileExtensions.end()) {
11364 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11365 << BlockName;
11366 continue;
11367 }
11368
11369 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11370 }
11371}
11372
11373ASTReader::~ASTReader() {
11374 if (OwnsDeserializationListener)
11375 delete DeserializationListener;
11376}
11377
11378IdentifierResolver &ASTReader::getIdResolver() {
11379 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11380}
11381
11382Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11383 unsigned AbbrevID) {
11384 Idx = 0;
11385 Record.clear();
11386 return Cursor.readRecord(AbbrevID, Vals&: Record);
11387}
11388//===----------------------------------------------------------------------===//
11389//// OMPClauseReader implementation
11390////===----------------------------------------------------------------------===//
11391
11392// This has to be in namespace clang because it's friended by all
11393// of the OMP clauses.
11394namespace clang {
11395
11396class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11397 ASTRecordReader &Record;
11398 ASTContext &Context;
11399
11400public:
11401 OMPClauseReader(ASTRecordReader &Record)
11402 : Record(Record), Context(Record.getContext()) {}
11403#define GEN_CLANG_CLAUSE_CLASS
11404#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11405#include "llvm/Frontend/OpenMP/OMP.inc"
11406 OMPClause *readClause();
11407 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11408 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11409};
11410
11411} // end namespace clang
11412
11413OMPClause *ASTRecordReader::readOMPClause() {
11414 return OMPClauseReader(*this).readClause();
11415}
11416
11417OMPClause *OMPClauseReader::readClause() {
11418 OMPClause *C = nullptr;
11419 switch (llvm::omp::Clause(Record.readInt())) {
11420 case llvm::omp::OMPC_if:
11421 C = new (Context) OMPIfClause();
11422 break;
11423 case llvm::omp::OMPC_final:
11424 C = new (Context) OMPFinalClause();
11425 break;
11426 case llvm::omp::OMPC_num_threads:
11427 C = new (Context) OMPNumThreadsClause();
11428 break;
11429 case llvm::omp::OMPC_safelen:
11430 C = new (Context) OMPSafelenClause();
11431 break;
11432 case llvm::omp::OMPC_simdlen:
11433 C = new (Context) OMPSimdlenClause();
11434 break;
11435 case llvm::omp::OMPC_sizes: {
11436 unsigned NumSizes = Record.readInt();
11437 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11438 break;
11439 }
11440 case llvm::omp::OMPC_permutation: {
11441 unsigned NumLoops = Record.readInt();
11442 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11443 break;
11444 }
11445 case llvm::omp::OMPC_full:
11446 C = OMPFullClause::CreateEmpty(C: Context);
11447 break;
11448 case llvm::omp::OMPC_partial:
11449 C = OMPPartialClause::CreateEmpty(C: Context);
11450 break;
11451 case llvm::omp::OMPC_looprange:
11452 C = OMPLoopRangeClause::CreateEmpty(C: Context);
11453 break;
11454 case llvm::omp::OMPC_allocator:
11455 C = new (Context) OMPAllocatorClause();
11456 break;
11457 case llvm::omp::OMPC_collapse:
11458 C = new (Context) OMPCollapseClause();
11459 break;
11460 case llvm::omp::OMPC_default:
11461 C = new (Context) OMPDefaultClause();
11462 break;
11463 case llvm::omp::OMPC_proc_bind:
11464 C = new (Context) OMPProcBindClause();
11465 break;
11466 case llvm::omp::OMPC_schedule:
11467 C = new (Context) OMPScheduleClause();
11468 break;
11469 case llvm::omp::OMPC_ordered:
11470 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11471 break;
11472 case llvm::omp::OMPC_nowait:
11473 C = new (Context) OMPNowaitClause();
11474 break;
11475 case llvm::omp::OMPC_untied:
11476 C = new (Context) OMPUntiedClause();
11477 break;
11478 case llvm::omp::OMPC_mergeable:
11479 C = new (Context) OMPMergeableClause();
11480 break;
11481 case llvm::omp::OMPC_threadset:
11482 C = new (Context) OMPThreadsetClause();
11483 break;
11484 case llvm::omp::OMPC_transparent:
11485 C = new (Context) OMPTransparentClause();
11486 break;
11487 case llvm::omp::OMPC_read:
11488 C = new (Context) OMPReadClause();
11489 break;
11490 case llvm::omp::OMPC_write:
11491 C = new (Context) OMPWriteClause();
11492 break;
11493 case llvm::omp::OMPC_update:
11494 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11495 break;
11496 case llvm::omp::OMPC_capture:
11497 C = new (Context) OMPCaptureClause();
11498 break;
11499 case llvm::omp::OMPC_compare:
11500 C = new (Context) OMPCompareClause();
11501 break;
11502 case llvm::omp::OMPC_fail:
11503 C = new (Context) OMPFailClause();
11504 break;
11505 case llvm::omp::OMPC_seq_cst:
11506 C = new (Context) OMPSeqCstClause();
11507 break;
11508 case llvm::omp::OMPC_acq_rel:
11509 C = new (Context) OMPAcqRelClause();
11510 break;
11511 case llvm::omp::OMPC_absent: {
11512 unsigned NumKinds = Record.readInt();
11513 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11514 break;
11515 }
11516 case llvm::omp::OMPC_holds:
11517 C = new (Context) OMPHoldsClause();
11518 break;
11519 case llvm::omp::OMPC_contains: {
11520 unsigned NumKinds = Record.readInt();
11521 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11522 break;
11523 }
11524 case llvm::omp::OMPC_no_openmp:
11525 C = new (Context) OMPNoOpenMPClause();
11526 break;
11527 case llvm::omp::OMPC_no_openmp_routines:
11528 C = new (Context) OMPNoOpenMPRoutinesClause();
11529 break;
11530 case llvm::omp::OMPC_no_openmp_constructs:
11531 C = new (Context) OMPNoOpenMPConstructsClause();
11532 break;
11533 case llvm::omp::OMPC_no_parallelism:
11534 C = new (Context) OMPNoParallelismClause();
11535 break;
11536 case llvm::omp::OMPC_acquire:
11537 C = new (Context) OMPAcquireClause();
11538 break;
11539 case llvm::omp::OMPC_release:
11540 C = new (Context) OMPReleaseClause();
11541 break;
11542 case llvm::omp::OMPC_relaxed:
11543 C = new (Context) OMPRelaxedClause();
11544 break;
11545 case llvm::omp::OMPC_weak:
11546 C = new (Context) OMPWeakClause();
11547 break;
11548 case llvm::omp::OMPC_threads:
11549 C = new (Context) OMPThreadsClause();
11550 break;
11551 case llvm::omp::OMPC_simd:
11552 C = new (Context) OMPSIMDClause();
11553 break;
11554 case llvm::omp::OMPC_nogroup:
11555 C = new (Context) OMPNogroupClause();
11556 break;
11557 case llvm::omp::OMPC_unified_address:
11558 C = new (Context) OMPUnifiedAddressClause();
11559 break;
11560 case llvm::omp::OMPC_unified_shared_memory:
11561 C = new (Context) OMPUnifiedSharedMemoryClause();
11562 break;
11563 case llvm::omp::OMPC_reverse_offload:
11564 C = new (Context) OMPReverseOffloadClause();
11565 break;
11566 case llvm::omp::OMPC_dynamic_allocators:
11567 C = new (Context) OMPDynamicAllocatorsClause();
11568 break;
11569 case llvm::omp::OMPC_atomic_default_mem_order:
11570 C = new (Context) OMPAtomicDefaultMemOrderClause();
11571 break;
11572 case llvm::omp::OMPC_self_maps:
11573 C = new (Context) OMPSelfMapsClause();
11574 break;
11575 case llvm::omp::OMPC_at:
11576 C = new (Context) OMPAtClause();
11577 break;
11578 case llvm::omp::OMPC_severity:
11579 C = new (Context) OMPSeverityClause();
11580 break;
11581 case llvm::omp::OMPC_message:
11582 C = new (Context) OMPMessageClause();
11583 break;
11584 case llvm::omp::OMPC_private:
11585 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11586 break;
11587 case llvm::omp::OMPC_firstprivate:
11588 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11589 break;
11590 case llvm::omp::OMPC_lastprivate:
11591 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11592 break;
11593 case llvm::omp::OMPC_shared:
11594 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11595 break;
11596 case llvm::omp::OMPC_reduction: {
11597 unsigned N = Record.readInt();
11598 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11599 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11600 break;
11601 }
11602 case llvm::omp::OMPC_task_reduction:
11603 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11604 break;
11605 case llvm::omp::OMPC_in_reduction:
11606 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11607 break;
11608 case llvm::omp::OMPC_linear:
11609 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11610 break;
11611 case llvm::omp::OMPC_aligned:
11612 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11613 break;
11614 case llvm::omp::OMPC_copyin:
11615 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11616 break;
11617 case llvm::omp::OMPC_copyprivate:
11618 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11619 break;
11620 case llvm::omp::OMPC_flush:
11621 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11622 break;
11623 case llvm::omp::OMPC_depobj:
11624 C = OMPDepobjClause::CreateEmpty(C: Context);
11625 break;
11626 case llvm::omp::OMPC_depend: {
11627 unsigned NumVars = Record.readInt();
11628 unsigned NumLoops = Record.readInt();
11629 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11630 break;
11631 }
11632 case llvm::omp::OMPC_device:
11633 C = new (Context) OMPDeviceClause();
11634 break;
11635 case llvm::omp::OMPC_map: {
11636 OMPMappableExprListSizeTy Sizes;
11637 Sizes.NumVars = Record.readInt();
11638 Sizes.NumUniqueDeclarations = Record.readInt();
11639 Sizes.NumComponentLists = Record.readInt();
11640 Sizes.NumComponents = Record.readInt();
11641 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11642 break;
11643 }
11644 case llvm::omp::OMPC_num_teams:
11645 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11646 break;
11647 case llvm::omp::OMPC_thread_limit:
11648 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11649 break;
11650 case llvm::omp::OMPC_priority:
11651 C = new (Context) OMPPriorityClause();
11652 break;
11653 case llvm::omp::OMPC_grainsize:
11654 C = new (Context) OMPGrainsizeClause();
11655 break;
11656 case llvm::omp::OMPC_num_tasks:
11657 C = new (Context) OMPNumTasksClause();
11658 break;
11659 case llvm::omp::OMPC_hint:
11660 C = new (Context) OMPHintClause();
11661 break;
11662 case llvm::omp::OMPC_dist_schedule:
11663 C = new (Context) OMPDistScheduleClause();
11664 break;
11665 case llvm::omp::OMPC_defaultmap:
11666 C = new (Context) OMPDefaultmapClause();
11667 break;
11668 case llvm::omp::OMPC_to: {
11669 OMPMappableExprListSizeTy Sizes;
11670 Sizes.NumVars = Record.readInt();
11671 Sizes.NumUniqueDeclarations = Record.readInt();
11672 Sizes.NumComponentLists = Record.readInt();
11673 Sizes.NumComponents = Record.readInt();
11674 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11675 break;
11676 }
11677 case llvm::omp::OMPC_from: {
11678 OMPMappableExprListSizeTy Sizes;
11679 Sizes.NumVars = Record.readInt();
11680 Sizes.NumUniqueDeclarations = Record.readInt();
11681 Sizes.NumComponentLists = Record.readInt();
11682 Sizes.NumComponents = Record.readInt();
11683 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11684 break;
11685 }
11686 case llvm::omp::OMPC_use_device_ptr: {
11687 OMPMappableExprListSizeTy Sizes;
11688 Sizes.NumVars = Record.readInt();
11689 Sizes.NumUniqueDeclarations = Record.readInt();
11690 Sizes.NumComponentLists = Record.readInt();
11691 Sizes.NumComponents = Record.readInt();
11692 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11693 break;
11694 }
11695 case llvm::omp::OMPC_use_device_addr: {
11696 OMPMappableExprListSizeTy Sizes;
11697 Sizes.NumVars = Record.readInt();
11698 Sizes.NumUniqueDeclarations = Record.readInt();
11699 Sizes.NumComponentLists = Record.readInt();
11700 Sizes.NumComponents = Record.readInt();
11701 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11702 break;
11703 }
11704 case llvm::omp::OMPC_is_device_ptr: {
11705 OMPMappableExprListSizeTy Sizes;
11706 Sizes.NumVars = Record.readInt();
11707 Sizes.NumUniqueDeclarations = Record.readInt();
11708 Sizes.NumComponentLists = Record.readInt();
11709 Sizes.NumComponents = Record.readInt();
11710 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11711 break;
11712 }
11713 case llvm::omp::OMPC_has_device_addr: {
11714 OMPMappableExprListSizeTy Sizes;
11715 Sizes.NumVars = Record.readInt();
11716 Sizes.NumUniqueDeclarations = Record.readInt();
11717 Sizes.NumComponentLists = Record.readInt();
11718 Sizes.NumComponents = Record.readInt();
11719 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11720 break;
11721 }
11722 case llvm::omp::OMPC_allocate:
11723 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11724 break;
11725 case llvm::omp::OMPC_nontemporal:
11726 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11727 break;
11728 case llvm::omp::OMPC_inclusive:
11729 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11730 break;
11731 case llvm::omp::OMPC_exclusive:
11732 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11733 break;
11734 case llvm::omp::OMPC_order:
11735 C = new (Context) OMPOrderClause();
11736 break;
11737 case llvm::omp::OMPC_init:
11738 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
11739 break;
11740 case llvm::omp::OMPC_use:
11741 C = new (Context) OMPUseClause();
11742 break;
11743 case llvm::omp::OMPC_destroy:
11744 C = new (Context) OMPDestroyClause();
11745 break;
11746 case llvm::omp::OMPC_novariants:
11747 C = new (Context) OMPNovariantsClause();
11748 break;
11749 case llvm::omp::OMPC_nocontext:
11750 C = new (Context) OMPNocontextClause();
11751 break;
11752 case llvm::omp::OMPC_detach:
11753 C = new (Context) OMPDetachClause();
11754 break;
11755 case llvm::omp::OMPC_uses_allocators:
11756 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11757 break;
11758 case llvm::omp::OMPC_affinity:
11759 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11760 break;
11761 case llvm::omp::OMPC_filter:
11762 C = new (Context) OMPFilterClause();
11763 break;
11764 case llvm::omp::OMPC_bind:
11765 C = OMPBindClause::CreateEmpty(C: Context);
11766 break;
11767 case llvm::omp::OMPC_align:
11768 C = new (Context) OMPAlignClause();
11769 break;
11770 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11771 C = new (Context) OMPXDynCGroupMemClause();
11772 break;
11773 case llvm::omp::OMPC_dyn_groupprivate:
11774 C = new (Context) OMPDynGroupprivateClause();
11775 break;
11776 case llvm::omp::OMPC_doacross: {
11777 unsigned NumVars = Record.readInt();
11778 unsigned NumLoops = Record.readInt();
11779 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11780 break;
11781 }
11782 case llvm::omp::OMPC_ompx_attribute:
11783 C = new (Context) OMPXAttributeClause();
11784 break;
11785 case llvm::omp::OMPC_ompx_bare:
11786 C = new (Context) OMPXBareClause();
11787 break;
11788#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11789 case llvm::omp::Enum: \
11790 break;
11791#include "llvm/Frontend/OpenMP/OMPKinds.def"
11792 default:
11793 break;
11794 }
11795 assert(C && "Unknown OMPClause type");
11796
11797 Visit(S: C);
11798 C->setLocStart(Record.readSourceLocation());
11799 C->setLocEnd(Record.readSourceLocation());
11800
11801 return C;
11802}
11803
11804void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11805 C->setPreInitStmt(S: Record.readSubStmt(),
11806 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11807}
11808
11809void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11810 VisitOMPClauseWithPreInit(C);
11811 C->setPostUpdateExpr(Record.readSubExpr());
11812}
11813
11814void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11815 VisitOMPClauseWithPreInit(C);
11816 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11817 C->setNameModifierLoc(Record.readSourceLocation());
11818 C->setColonLoc(Record.readSourceLocation());
11819 C->setCondition(Record.readSubExpr());
11820 C->setLParenLoc(Record.readSourceLocation());
11821}
11822
11823void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11824 VisitOMPClauseWithPreInit(C);
11825 C->setCondition(Record.readSubExpr());
11826 C->setLParenLoc(Record.readSourceLocation());
11827}
11828
11829void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11830 VisitOMPClauseWithPreInit(C);
11831 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11832 C->setNumThreads(Record.readSubExpr());
11833 C->setModifierLoc(Record.readSourceLocation());
11834 C->setLParenLoc(Record.readSourceLocation());
11835}
11836
11837void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11838 C->setSafelen(Record.readSubExpr());
11839 C->setLParenLoc(Record.readSourceLocation());
11840}
11841
11842void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11843 C->setSimdlen(Record.readSubExpr());
11844 C->setLParenLoc(Record.readSourceLocation());
11845}
11846
11847void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11848 for (Expr *&E : C->getSizesRefs())
11849 E = Record.readSubExpr();
11850 C->setLParenLoc(Record.readSourceLocation());
11851}
11852
11853void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11854 for (Expr *&E : C->getArgsRefs())
11855 E = Record.readSubExpr();
11856 C->setLParenLoc(Record.readSourceLocation());
11857}
11858
11859void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11860
11861void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11862 C->setFactor(Record.readSubExpr());
11863 C->setLParenLoc(Record.readSourceLocation());
11864}
11865
11866void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {
11867 C->setFirst(Record.readSubExpr());
11868 C->setCount(Record.readSubExpr());
11869 C->setLParenLoc(Record.readSourceLocation());
11870 C->setFirstLoc(Record.readSourceLocation());
11871 C->setCountLoc(Record.readSourceLocation());
11872}
11873
11874void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11875 C->setAllocator(Record.readExpr());
11876 C->setLParenLoc(Record.readSourceLocation());
11877}
11878
11879void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11880 C->setNumForLoops(Record.readSubExpr());
11881 C->setLParenLoc(Record.readSourceLocation());
11882}
11883
11884void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11885 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11886 C->setLParenLoc(Record.readSourceLocation());
11887 C->setDefaultKindKwLoc(Record.readSourceLocation());
11888 C->setDefaultVariableCategory(
11889 Record.readEnum<OpenMPDefaultClauseVariableCategory>());
11890 C->setDefaultVariableCategoryLocation(Record.readSourceLocation());
11891}
11892
11893// Read the parameter of threadset clause. This will have been saved when
11894// OMPClauseWriter is called.
11895void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {
11896 C->setLParenLoc(Record.readSourceLocation());
11897 SourceLocation ThreadsetKindLoc = Record.readSourceLocation();
11898 C->setThreadsetKindLoc(ThreadsetKindLoc);
11899 OpenMPThreadsetKind TKind =
11900 static_cast<OpenMPThreadsetKind>(Record.readInt());
11901 C->setThreadsetKind(TKind);
11902}
11903
11904void OMPClauseReader::VisitOMPTransparentClause(OMPTransparentClause *C) {
11905 C->setLParenLoc(Record.readSourceLocation());
11906 C->setImpexTypeKind(Record.readSubExpr());
11907}
11908
11909void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11910 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11911 C->setLParenLoc(Record.readSourceLocation());
11912 C->setProcBindKindKwLoc(Record.readSourceLocation());
11913}
11914
11915void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11916 VisitOMPClauseWithPreInit(C);
11917 C->setScheduleKind(
11918 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11919 C->setFirstScheduleModifier(
11920 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11921 C->setSecondScheduleModifier(
11922 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11923 C->setChunkSize(Record.readSubExpr());
11924 C->setLParenLoc(Record.readSourceLocation());
11925 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11926 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11927 C->setScheduleKindLoc(Record.readSourceLocation());
11928 C->setCommaLoc(Record.readSourceLocation());
11929}
11930
11931void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11932 C->setNumForLoops(Record.readSubExpr());
11933 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11934 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11935 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11936 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11937 C->setLParenLoc(Record.readSourceLocation());
11938}
11939
11940void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11941 C->setEventHandler(Record.readSubExpr());
11942 C->setLParenLoc(Record.readSourceLocation());
11943}
11944
11945void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {
11946 C->setCondition(Record.readSubExpr());
11947 C->setLParenLoc(Record.readSourceLocation());
11948}
11949
11950void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11951
11952void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11953
11954void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11955
11956void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11957
11958void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11959 if (C->isExtended()) {
11960 C->setLParenLoc(Record.readSourceLocation());
11961 C->setArgumentLoc(Record.readSourceLocation());
11962 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11963 }
11964}
11965
11966void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11967
11968void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11969
11970// Read the parameter of fail clause. This will have been saved when
11971// OMPClauseWriter is called.
11972void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
11973 C->setLParenLoc(Record.readSourceLocation());
11974 SourceLocation FailParameterLoc = Record.readSourceLocation();
11975 C->setFailParameterLoc(FailParameterLoc);
11976 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
11977 C->setFailParameter(CKind);
11978}
11979
11980void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
11981 unsigned Count = C->getDirectiveKinds().size();
11982 C->setLParenLoc(Record.readSourceLocation());
11983 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11984 DKVec.reserve(N: Count);
11985 for (unsigned I = 0; I < Count; I++) {
11986 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11987 }
11988 C->setDirectiveKinds(DKVec);
11989}
11990
11991void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
11992 C->setExpr(Record.readExpr());
11993 C->setLParenLoc(Record.readSourceLocation());
11994}
11995
11996void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
11997 unsigned Count = C->getDirectiveKinds().size();
11998 C->setLParenLoc(Record.readSourceLocation());
11999 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
12000 DKVec.reserve(N: Count);
12001 for (unsigned I = 0; I < Count; I++) {
12002 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
12003 }
12004 C->setDirectiveKinds(DKVec);
12005}
12006
12007void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
12008
12009void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
12010 OMPNoOpenMPRoutinesClause *) {}
12011
12012void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
12013 OMPNoOpenMPConstructsClause *) {}
12014
12015void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
12016
12017void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12018
12019void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12020
12021void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12022
12023void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12024
12025void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12026
12027void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
12028
12029void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12030
12031void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12032
12033void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12034
12035void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12036 unsigned NumVars = C->varlist_size();
12037 SmallVector<Expr *, 16> Vars;
12038 Vars.reserve(N: NumVars);
12039 for (unsigned I = 0; I != NumVars; ++I)
12040 Vars.push_back(Elt: Record.readSubExpr());
12041 C->setVarRefs(Vars);
12042 C->setIsTarget(Record.readBool());
12043 C->setIsTargetSync(Record.readBool());
12044 C->setLParenLoc(Record.readSourceLocation());
12045 C->setVarLoc(Record.readSourceLocation());
12046}
12047
12048void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12049 C->setInteropVar(Record.readSubExpr());
12050 C->setLParenLoc(Record.readSourceLocation());
12051 C->setVarLoc(Record.readSourceLocation());
12052}
12053
12054void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12055 C->setInteropVar(Record.readSubExpr());
12056 C->setLParenLoc(Record.readSourceLocation());
12057 C->setVarLoc(Record.readSourceLocation());
12058}
12059
12060void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12061 VisitOMPClauseWithPreInit(C);
12062 C->setCondition(Record.readSubExpr());
12063 C->setLParenLoc(Record.readSourceLocation());
12064}
12065
12066void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12067 VisitOMPClauseWithPreInit(C);
12068 C->setCondition(Record.readSubExpr());
12069 C->setLParenLoc(Record.readSourceLocation());
12070}
12071
12072void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12073
12074void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12075 OMPUnifiedSharedMemoryClause *) {}
12076
12077void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12078
12079void
12080OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12081}
12082
12083void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12084 OMPAtomicDefaultMemOrderClause *C) {
12085 C->setAtomicDefaultMemOrderKind(
12086 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12087 C->setLParenLoc(Record.readSourceLocation());
12088 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12089}
12090
12091void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
12092
12093void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
12094 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
12095 C->setLParenLoc(Record.readSourceLocation());
12096 C->setAtKindKwLoc(Record.readSourceLocation());
12097}
12098
12099void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
12100 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
12101 C->setLParenLoc(Record.readSourceLocation());
12102 C->setSeverityKindKwLoc(Record.readSourceLocation());
12103}
12104
12105void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
12106 VisitOMPClauseWithPreInit(C);
12107 C->setMessageString(Record.readSubExpr());
12108 C->setLParenLoc(Record.readSourceLocation());
12109}
12110
12111void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12112 C->setLParenLoc(Record.readSourceLocation());
12113 unsigned NumVars = C->varlist_size();
12114 SmallVector<Expr *, 16> Vars;
12115 Vars.reserve(N: NumVars);
12116 for (unsigned i = 0; i != NumVars; ++i)
12117 Vars.push_back(Elt: Record.readSubExpr());
12118 C->setVarRefs(Vars);
12119 Vars.clear();
12120 for (unsigned i = 0; i != NumVars; ++i)
12121 Vars.push_back(Elt: Record.readSubExpr());
12122 C->setPrivateCopies(Vars);
12123}
12124
12125void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12126 VisitOMPClauseWithPreInit(C);
12127 C->setLParenLoc(Record.readSourceLocation());
12128 unsigned NumVars = C->varlist_size();
12129 SmallVector<Expr *, 16> Vars;
12130 Vars.reserve(N: NumVars);
12131 for (unsigned i = 0; i != NumVars; ++i)
12132 Vars.push_back(Elt: Record.readSubExpr());
12133 C->setVarRefs(Vars);
12134 Vars.clear();
12135 for (unsigned i = 0; i != NumVars; ++i)
12136 Vars.push_back(Elt: Record.readSubExpr());
12137 C->setPrivateCopies(Vars);
12138 Vars.clear();
12139 for (unsigned i = 0; i != NumVars; ++i)
12140 Vars.push_back(Elt: Record.readSubExpr());
12141 C->setInits(Vars);
12142}
12143
12144void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12145 VisitOMPClauseWithPostUpdate(C);
12146 C->setLParenLoc(Record.readSourceLocation());
12147 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12148 C->setKindLoc(Record.readSourceLocation());
12149 C->setColonLoc(Record.readSourceLocation());
12150 unsigned NumVars = C->varlist_size();
12151 SmallVector<Expr *, 16> Vars;
12152 Vars.reserve(N: NumVars);
12153 for (unsigned i = 0; i != NumVars; ++i)
12154 Vars.push_back(Elt: Record.readSubExpr());
12155 C->setVarRefs(Vars);
12156 Vars.clear();
12157 for (unsigned i = 0; i != NumVars; ++i)
12158 Vars.push_back(Elt: Record.readSubExpr());
12159 C->setPrivateCopies(Vars);
12160 Vars.clear();
12161 for (unsigned i = 0; i != NumVars; ++i)
12162 Vars.push_back(Elt: Record.readSubExpr());
12163 C->setSourceExprs(Vars);
12164 Vars.clear();
12165 for (unsigned i = 0; i != NumVars; ++i)
12166 Vars.push_back(Elt: Record.readSubExpr());
12167 C->setDestinationExprs(Vars);
12168 Vars.clear();
12169 for (unsigned i = 0; i != NumVars; ++i)
12170 Vars.push_back(Elt: Record.readSubExpr());
12171 C->setAssignmentOps(Vars);
12172}
12173
12174void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12175 C->setLParenLoc(Record.readSourceLocation());
12176 unsigned NumVars = C->varlist_size();
12177 SmallVector<Expr *, 16> Vars;
12178 Vars.reserve(N: NumVars);
12179 for (unsigned i = 0; i != NumVars; ++i)
12180 Vars.push_back(Elt: Record.readSubExpr());
12181 C->setVarRefs(Vars);
12182}
12183
12184void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12185 VisitOMPClauseWithPostUpdate(C);
12186 C->setLParenLoc(Record.readSourceLocation());
12187 C->setModifierLoc(Record.readSourceLocation());
12188 C->setColonLoc(Record.readSourceLocation());
12189 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12190 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12191 C->setQualifierLoc(NNSL);
12192 C->setNameInfo(DNI);
12193
12194 unsigned NumVars = C->varlist_size();
12195 SmallVector<Expr *, 16> Vars;
12196 Vars.reserve(N: NumVars);
12197 for (unsigned i = 0; i != NumVars; ++i)
12198 Vars.push_back(Elt: Record.readSubExpr());
12199 C->setVarRefs(Vars);
12200 Vars.clear();
12201 for (unsigned i = 0; i != NumVars; ++i)
12202 Vars.push_back(Elt: Record.readSubExpr());
12203 C->setPrivates(Vars);
12204 Vars.clear();
12205 for (unsigned i = 0; i != NumVars; ++i)
12206 Vars.push_back(Elt: Record.readSubExpr());
12207 C->setLHSExprs(Vars);
12208 Vars.clear();
12209 for (unsigned i = 0; i != NumVars; ++i)
12210 Vars.push_back(Elt: Record.readSubExpr());
12211 C->setRHSExprs(Vars);
12212 Vars.clear();
12213 for (unsigned i = 0; i != NumVars; ++i)
12214 Vars.push_back(Elt: Record.readSubExpr());
12215 C->setReductionOps(Vars);
12216 if (C->getModifier() == OMPC_REDUCTION_inscan) {
12217 Vars.clear();
12218 for (unsigned i = 0; i != NumVars; ++i)
12219 Vars.push_back(Elt: Record.readSubExpr());
12220 C->setInscanCopyOps(Vars);
12221 Vars.clear();
12222 for (unsigned i = 0; i != NumVars; ++i)
12223 Vars.push_back(Elt: Record.readSubExpr());
12224 C->setInscanCopyArrayTemps(Vars);
12225 Vars.clear();
12226 for (unsigned i = 0; i != NumVars; ++i)
12227 Vars.push_back(Elt: Record.readSubExpr());
12228 C->setInscanCopyArrayElems(Vars);
12229 }
12230 unsigned NumFlags = Record.readInt();
12231 SmallVector<bool, 16> Flags;
12232 Flags.reserve(N: NumFlags);
12233 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
12234 Flags.push_back(Elt: Record.readInt());
12235 C->setPrivateVariableReductionFlags(Flags);
12236}
12237
12238void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12239 VisitOMPClauseWithPostUpdate(C);
12240 C->setLParenLoc(Record.readSourceLocation());
12241 C->setColonLoc(Record.readSourceLocation());
12242 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12243 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12244 C->setQualifierLoc(NNSL);
12245 C->setNameInfo(DNI);
12246
12247 unsigned NumVars = C->varlist_size();
12248 SmallVector<Expr *, 16> Vars;
12249 Vars.reserve(N: NumVars);
12250 for (unsigned I = 0; I != NumVars; ++I)
12251 Vars.push_back(Elt: Record.readSubExpr());
12252 C->setVarRefs(Vars);
12253 Vars.clear();
12254 for (unsigned I = 0; I != NumVars; ++I)
12255 Vars.push_back(Elt: Record.readSubExpr());
12256 C->setPrivates(Vars);
12257 Vars.clear();
12258 for (unsigned I = 0; I != NumVars; ++I)
12259 Vars.push_back(Elt: Record.readSubExpr());
12260 C->setLHSExprs(Vars);
12261 Vars.clear();
12262 for (unsigned I = 0; I != NumVars; ++I)
12263 Vars.push_back(Elt: Record.readSubExpr());
12264 C->setRHSExprs(Vars);
12265 Vars.clear();
12266 for (unsigned I = 0; I != NumVars; ++I)
12267 Vars.push_back(Elt: Record.readSubExpr());
12268 C->setReductionOps(Vars);
12269}
12270
12271void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12272 VisitOMPClauseWithPostUpdate(C);
12273 C->setLParenLoc(Record.readSourceLocation());
12274 C->setColonLoc(Record.readSourceLocation());
12275 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12276 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12277 C->setQualifierLoc(NNSL);
12278 C->setNameInfo(DNI);
12279
12280 unsigned NumVars = C->varlist_size();
12281 SmallVector<Expr *, 16> Vars;
12282 Vars.reserve(N: NumVars);
12283 for (unsigned I = 0; I != NumVars; ++I)
12284 Vars.push_back(Elt: Record.readSubExpr());
12285 C->setVarRefs(Vars);
12286 Vars.clear();
12287 for (unsigned I = 0; I != NumVars; ++I)
12288 Vars.push_back(Elt: Record.readSubExpr());
12289 C->setPrivates(Vars);
12290 Vars.clear();
12291 for (unsigned I = 0; I != NumVars; ++I)
12292 Vars.push_back(Elt: Record.readSubExpr());
12293 C->setLHSExprs(Vars);
12294 Vars.clear();
12295 for (unsigned I = 0; I != NumVars; ++I)
12296 Vars.push_back(Elt: Record.readSubExpr());
12297 C->setRHSExprs(Vars);
12298 Vars.clear();
12299 for (unsigned I = 0; I != NumVars; ++I)
12300 Vars.push_back(Elt: Record.readSubExpr());
12301 C->setReductionOps(Vars);
12302 Vars.clear();
12303 for (unsigned I = 0; I != NumVars; ++I)
12304 Vars.push_back(Elt: Record.readSubExpr());
12305 C->setTaskgroupDescriptors(Vars);
12306}
12307
12308void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12309 VisitOMPClauseWithPostUpdate(C);
12310 C->setLParenLoc(Record.readSourceLocation());
12311 C->setColonLoc(Record.readSourceLocation());
12312 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12313 C->setModifierLoc(Record.readSourceLocation());
12314 unsigned NumVars = C->varlist_size();
12315 SmallVector<Expr *, 16> Vars;
12316 Vars.reserve(N: NumVars);
12317 for (unsigned i = 0; i != NumVars; ++i)
12318 Vars.push_back(Elt: Record.readSubExpr());
12319 C->setVarRefs(Vars);
12320 Vars.clear();
12321 for (unsigned i = 0; i != NumVars; ++i)
12322 Vars.push_back(Elt: Record.readSubExpr());
12323 C->setPrivates(Vars);
12324 Vars.clear();
12325 for (unsigned i = 0; i != NumVars; ++i)
12326 Vars.push_back(Elt: Record.readSubExpr());
12327 C->setInits(Vars);
12328 Vars.clear();
12329 for (unsigned i = 0; i != NumVars; ++i)
12330 Vars.push_back(Elt: Record.readSubExpr());
12331 C->setUpdates(Vars);
12332 Vars.clear();
12333 for (unsigned i = 0; i != NumVars; ++i)
12334 Vars.push_back(Elt: Record.readSubExpr());
12335 C->setFinals(Vars);
12336 C->setStep(Record.readSubExpr());
12337 C->setCalcStep(Record.readSubExpr());
12338 Vars.clear();
12339 for (unsigned I = 0; I != NumVars + 1; ++I)
12340 Vars.push_back(Elt: Record.readSubExpr());
12341 C->setUsedExprs(Vars);
12342}
12343
12344void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12345 C->setLParenLoc(Record.readSourceLocation());
12346 C->setColonLoc(Record.readSourceLocation());
12347 unsigned NumVars = C->varlist_size();
12348 SmallVector<Expr *, 16> Vars;
12349 Vars.reserve(N: NumVars);
12350 for (unsigned i = 0; i != NumVars; ++i)
12351 Vars.push_back(Elt: Record.readSubExpr());
12352 C->setVarRefs(Vars);
12353 C->setAlignment(Record.readSubExpr());
12354}
12355
12356void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12357 C->setLParenLoc(Record.readSourceLocation());
12358 unsigned NumVars = C->varlist_size();
12359 SmallVector<Expr *, 16> Exprs;
12360 Exprs.reserve(N: NumVars);
12361 for (unsigned i = 0; i != NumVars; ++i)
12362 Exprs.push_back(Elt: Record.readSubExpr());
12363 C->setVarRefs(Exprs);
12364 Exprs.clear();
12365 for (unsigned i = 0; i != NumVars; ++i)
12366 Exprs.push_back(Elt: Record.readSubExpr());
12367 C->setSourceExprs(Exprs);
12368 Exprs.clear();
12369 for (unsigned i = 0; i != NumVars; ++i)
12370 Exprs.push_back(Elt: Record.readSubExpr());
12371 C->setDestinationExprs(Exprs);
12372 Exprs.clear();
12373 for (unsigned i = 0; i != NumVars; ++i)
12374 Exprs.push_back(Elt: Record.readSubExpr());
12375 C->setAssignmentOps(Exprs);
12376}
12377
12378void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12379 C->setLParenLoc(Record.readSourceLocation());
12380 unsigned NumVars = C->varlist_size();
12381 SmallVector<Expr *, 16> Exprs;
12382 Exprs.reserve(N: NumVars);
12383 for (unsigned i = 0; i != NumVars; ++i)
12384 Exprs.push_back(Elt: Record.readSubExpr());
12385 C->setVarRefs(Exprs);
12386 Exprs.clear();
12387 for (unsigned i = 0; i != NumVars; ++i)
12388 Exprs.push_back(Elt: Record.readSubExpr());
12389 C->setSourceExprs(Exprs);
12390 Exprs.clear();
12391 for (unsigned i = 0; i != NumVars; ++i)
12392 Exprs.push_back(Elt: Record.readSubExpr());
12393 C->setDestinationExprs(Exprs);
12394 Exprs.clear();
12395 for (unsigned i = 0; i != NumVars; ++i)
12396 Exprs.push_back(Elt: Record.readSubExpr());
12397 C->setAssignmentOps(Exprs);
12398}
12399
12400void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12401 C->setLParenLoc(Record.readSourceLocation());
12402 unsigned NumVars = C->varlist_size();
12403 SmallVector<Expr *, 16> Vars;
12404 Vars.reserve(N: NumVars);
12405 for (unsigned i = 0; i != NumVars; ++i)
12406 Vars.push_back(Elt: Record.readSubExpr());
12407 C->setVarRefs(Vars);
12408}
12409
12410void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12411 C->setDepobj(Record.readSubExpr());
12412 C->setLParenLoc(Record.readSourceLocation());
12413}
12414
12415void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12416 C->setLParenLoc(Record.readSourceLocation());
12417 C->setModifier(Record.readSubExpr());
12418 C->setDependencyKind(
12419 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12420 C->setDependencyLoc(Record.readSourceLocation());
12421 C->setColonLoc(Record.readSourceLocation());
12422 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12423 unsigned NumVars = C->varlist_size();
12424 SmallVector<Expr *, 16> Vars;
12425 Vars.reserve(N: NumVars);
12426 for (unsigned I = 0; I != NumVars; ++I)
12427 Vars.push_back(Elt: Record.readSubExpr());
12428 C->setVarRefs(Vars);
12429 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12430 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12431}
12432
12433void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12434 VisitOMPClauseWithPreInit(C);
12435 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12436 C->setDevice(Record.readSubExpr());
12437 C->setModifierLoc(Record.readSourceLocation());
12438 C->setLParenLoc(Record.readSourceLocation());
12439}
12440
12441void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12442 C->setLParenLoc(Record.readSourceLocation());
12443 bool HasIteratorModifier = false;
12444 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12445 C->setMapTypeModifier(
12446 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12447 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12448 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12449 HasIteratorModifier = true;
12450 }
12451 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12452 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12453 C->setMapType(
12454 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12455 C->setMapLoc(Record.readSourceLocation());
12456 C->setColonLoc(Record.readSourceLocation());
12457 auto NumVars = C->varlist_size();
12458 auto UniqueDecls = C->getUniqueDeclarationsNum();
12459 auto TotalLists = C->getTotalComponentListNum();
12460 auto TotalComponents = C->getTotalComponentsNum();
12461
12462 SmallVector<Expr *, 16> Vars;
12463 Vars.reserve(N: NumVars);
12464 for (unsigned i = 0; i != NumVars; ++i)
12465 Vars.push_back(Elt: Record.readExpr());
12466 C->setVarRefs(Vars);
12467
12468 SmallVector<Expr *, 16> UDMappers;
12469 UDMappers.reserve(N: NumVars);
12470 for (unsigned I = 0; I < NumVars; ++I)
12471 UDMappers.push_back(Elt: Record.readExpr());
12472 C->setUDMapperRefs(UDMappers);
12473
12474 if (HasIteratorModifier)
12475 C->setIteratorModifier(Record.readExpr());
12476
12477 SmallVector<ValueDecl *, 16> Decls;
12478 Decls.reserve(N: UniqueDecls);
12479 for (unsigned i = 0; i < UniqueDecls; ++i)
12480 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12481 C->setUniqueDecls(Decls);
12482
12483 SmallVector<unsigned, 16> ListsPerDecl;
12484 ListsPerDecl.reserve(N: UniqueDecls);
12485 for (unsigned i = 0; i < UniqueDecls; ++i)
12486 ListsPerDecl.push_back(Elt: Record.readInt());
12487 C->setDeclNumLists(ListsPerDecl);
12488
12489 SmallVector<unsigned, 32> ListSizes;
12490 ListSizes.reserve(N: TotalLists);
12491 for (unsigned i = 0; i < TotalLists; ++i)
12492 ListSizes.push_back(Elt: Record.readInt());
12493 C->setComponentListSizes(ListSizes);
12494
12495 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12496 Components.reserve(N: TotalComponents);
12497 for (unsigned i = 0; i < TotalComponents; ++i) {
12498 Expr *AssociatedExprPr = Record.readExpr();
12499 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12500 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12501 /*IsNonContiguous=*/Args: false);
12502 }
12503 C->setComponents(Components, CLSs: ListSizes);
12504}
12505
12506void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12507 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12508 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12509 C->setLParenLoc(Record.readSourceLocation());
12510 C->setColonLoc(Record.readSourceLocation());
12511 C->setAllocator(Record.readSubExpr());
12512 C->setAlignment(Record.readSubExpr());
12513 unsigned NumVars = C->varlist_size();
12514 SmallVector<Expr *, 16> Vars;
12515 Vars.reserve(N: NumVars);
12516 for (unsigned i = 0; i != NumVars; ++i)
12517 Vars.push_back(Elt: Record.readSubExpr());
12518 C->setVarRefs(Vars);
12519}
12520
12521void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12522 VisitOMPClauseWithPreInit(C);
12523 C->setLParenLoc(Record.readSourceLocation());
12524 unsigned NumVars = C->varlist_size();
12525 SmallVector<Expr *, 16> Vars;
12526 Vars.reserve(N: NumVars);
12527 for (unsigned I = 0; I != NumVars; ++I)
12528 Vars.push_back(Elt: Record.readSubExpr());
12529 C->setVarRefs(Vars);
12530}
12531
12532void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12533 VisitOMPClauseWithPreInit(C);
12534 C->setLParenLoc(Record.readSourceLocation());
12535 unsigned NumVars = C->varlist_size();
12536 SmallVector<Expr *, 16> Vars;
12537 Vars.reserve(N: NumVars);
12538 for (unsigned I = 0; I != NumVars; ++I)
12539 Vars.push_back(Elt: Record.readSubExpr());
12540 C->setVarRefs(Vars);
12541}
12542
12543void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12544 VisitOMPClauseWithPreInit(C);
12545 C->setPriority(Record.readSubExpr());
12546 C->setLParenLoc(Record.readSourceLocation());
12547}
12548
12549void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12550 VisitOMPClauseWithPreInit(C);
12551 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12552 C->setGrainsize(Record.readSubExpr());
12553 C->setModifierLoc(Record.readSourceLocation());
12554 C->setLParenLoc(Record.readSourceLocation());
12555}
12556
12557void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12558 VisitOMPClauseWithPreInit(C);
12559 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12560 C->setNumTasks(Record.readSubExpr());
12561 C->setModifierLoc(Record.readSourceLocation());
12562 C->setLParenLoc(Record.readSourceLocation());
12563}
12564
12565void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12566 C->setHint(Record.readSubExpr());
12567 C->setLParenLoc(Record.readSourceLocation());
12568}
12569
12570void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12571 VisitOMPClauseWithPreInit(C);
12572 C->setDistScheduleKind(
12573 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12574 C->setChunkSize(Record.readSubExpr());
12575 C->setLParenLoc(Record.readSourceLocation());
12576 C->setDistScheduleKindLoc(Record.readSourceLocation());
12577 C->setCommaLoc(Record.readSourceLocation());
12578}
12579
12580void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12581 C->setDefaultmapKind(
12582 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12583 C->setDefaultmapModifier(
12584 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12585 C->setLParenLoc(Record.readSourceLocation());
12586 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12587 C->setDefaultmapKindLoc(Record.readSourceLocation());
12588}
12589
12590void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12591 C->setLParenLoc(Record.readSourceLocation());
12592 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12593 C->setMotionModifier(
12594 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12595 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12596 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12597 C->setIteratorModifier(Record.readExpr());
12598 }
12599 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12600 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12601 C->setColonLoc(Record.readSourceLocation());
12602 auto NumVars = C->varlist_size();
12603 auto UniqueDecls = C->getUniqueDeclarationsNum();
12604 auto TotalLists = C->getTotalComponentListNum();
12605 auto TotalComponents = C->getTotalComponentsNum();
12606
12607 SmallVector<Expr *, 16> Vars;
12608 Vars.reserve(N: NumVars);
12609 for (unsigned i = 0; i != NumVars; ++i)
12610 Vars.push_back(Elt: Record.readSubExpr());
12611 C->setVarRefs(Vars);
12612
12613 SmallVector<Expr *, 16> UDMappers;
12614 UDMappers.reserve(N: NumVars);
12615 for (unsigned I = 0; I < NumVars; ++I)
12616 UDMappers.push_back(Elt: Record.readSubExpr());
12617 C->setUDMapperRefs(UDMappers);
12618
12619 SmallVector<ValueDecl *, 16> Decls;
12620 Decls.reserve(N: UniqueDecls);
12621 for (unsigned i = 0; i < UniqueDecls; ++i)
12622 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12623 C->setUniqueDecls(Decls);
12624
12625 SmallVector<unsigned, 16> ListsPerDecl;
12626 ListsPerDecl.reserve(N: UniqueDecls);
12627 for (unsigned i = 0; i < UniqueDecls; ++i)
12628 ListsPerDecl.push_back(Elt: Record.readInt());
12629 C->setDeclNumLists(ListsPerDecl);
12630
12631 SmallVector<unsigned, 32> ListSizes;
12632 ListSizes.reserve(N: TotalLists);
12633 for (unsigned i = 0; i < TotalLists; ++i)
12634 ListSizes.push_back(Elt: Record.readInt());
12635 C->setComponentListSizes(ListSizes);
12636
12637 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12638 Components.reserve(N: TotalComponents);
12639 for (unsigned i = 0; i < TotalComponents; ++i) {
12640 Expr *AssociatedExprPr = Record.readSubExpr();
12641 bool IsNonContiguous = Record.readBool();
12642 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12643 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12644 }
12645 C->setComponents(Components, CLSs: ListSizes);
12646}
12647
12648void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12649 C->setLParenLoc(Record.readSourceLocation());
12650 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12651 C->setMotionModifier(
12652 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12653 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12654 if (C->getMotionModifier(Cnt: I) == OMPC_MOTION_MODIFIER_iterator)
12655 C->setIteratorModifier(Record.readExpr());
12656 }
12657 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12658 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12659 C->setColonLoc(Record.readSourceLocation());
12660 auto NumVars = C->varlist_size();
12661 auto UniqueDecls = C->getUniqueDeclarationsNum();
12662 auto TotalLists = C->getTotalComponentListNum();
12663 auto TotalComponents = C->getTotalComponentsNum();
12664
12665 SmallVector<Expr *, 16> Vars;
12666 Vars.reserve(N: NumVars);
12667 for (unsigned i = 0; i != NumVars; ++i)
12668 Vars.push_back(Elt: Record.readSubExpr());
12669 C->setVarRefs(Vars);
12670
12671 SmallVector<Expr *, 16> UDMappers;
12672 UDMappers.reserve(N: NumVars);
12673 for (unsigned I = 0; I < NumVars; ++I)
12674 UDMappers.push_back(Elt: Record.readSubExpr());
12675 C->setUDMapperRefs(UDMappers);
12676
12677 SmallVector<ValueDecl *, 16> Decls;
12678 Decls.reserve(N: UniqueDecls);
12679 for (unsigned i = 0; i < UniqueDecls; ++i)
12680 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12681 C->setUniqueDecls(Decls);
12682
12683 SmallVector<unsigned, 16> ListsPerDecl;
12684 ListsPerDecl.reserve(N: UniqueDecls);
12685 for (unsigned i = 0; i < UniqueDecls; ++i)
12686 ListsPerDecl.push_back(Elt: Record.readInt());
12687 C->setDeclNumLists(ListsPerDecl);
12688
12689 SmallVector<unsigned, 32> ListSizes;
12690 ListSizes.reserve(N: TotalLists);
12691 for (unsigned i = 0; i < TotalLists; ++i)
12692 ListSizes.push_back(Elt: Record.readInt());
12693 C->setComponentListSizes(ListSizes);
12694
12695 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12696 Components.reserve(N: TotalComponents);
12697 for (unsigned i = 0; i < TotalComponents; ++i) {
12698 Expr *AssociatedExprPr = Record.readSubExpr();
12699 bool IsNonContiguous = Record.readBool();
12700 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12701 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12702 }
12703 C->setComponents(Components, CLSs: ListSizes);
12704}
12705
12706void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12707 C->setLParenLoc(Record.readSourceLocation());
12708 C->setFallbackModifier(Record.readEnum<OpenMPUseDevicePtrFallbackModifier>());
12709 C->setFallbackModifierLoc(Record.readSourceLocation());
12710 auto NumVars = C->varlist_size();
12711 auto UniqueDecls = C->getUniqueDeclarationsNum();
12712 auto TotalLists = C->getTotalComponentListNum();
12713 auto TotalComponents = C->getTotalComponentsNum();
12714
12715 SmallVector<Expr *, 16> Vars;
12716 Vars.reserve(N: NumVars);
12717 for (unsigned i = 0; i != NumVars; ++i)
12718 Vars.push_back(Elt: Record.readSubExpr());
12719 C->setVarRefs(Vars);
12720 Vars.clear();
12721 for (unsigned i = 0; i != NumVars; ++i)
12722 Vars.push_back(Elt: Record.readSubExpr());
12723 C->setPrivateCopies(Vars);
12724 Vars.clear();
12725 for (unsigned i = 0; i != NumVars; ++i)
12726 Vars.push_back(Elt: Record.readSubExpr());
12727 C->setInits(Vars);
12728
12729 SmallVector<ValueDecl *, 16> Decls;
12730 Decls.reserve(N: UniqueDecls);
12731 for (unsigned i = 0; i < UniqueDecls; ++i)
12732 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12733 C->setUniqueDecls(Decls);
12734
12735 SmallVector<unsigned, 16> ListsPerDecl;
12736 ListsPerDecl.reserve(N: UniqueDecls);
12737 for (unsigned i = 0; i < UniqueDecls; ++i)
12738 ListsPerDecl.push_back(Elt: Record.readInt());
12739 C->setDeclNumLists(ListsPerDecl);
12740
12741 SmallVector<unsigned, 32> ListSizes;
12742 ListSizes.reserve(N: TotalLists);
12743 for (unsigned i = 0; i < TotalLists; ++i)
12744 ListSizes.push_back(Elt: Record.readInt());
12745 C->setComponentListSizes(ListSizes);
12746
12747 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12748 Components.reserve(N: TotalComponents);
12749 for (unsigned i = 0; i < TotalComponents; ++i) {
12750 auto *AssociatedExprPr = Record.readSubExpr();
12751 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12752 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12753 /*IsNonContiguous=*/Args: false);
12754 }
12755 C->setComponents(Components, CLSs: ListSizes);
12756}
12757
12758void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12759 C->setLParenLoc(Record.readSourceLocation());
12760 auto NumVars = C->varlist_size();
12761 auto UniqueDecls = C->getUniqueDeclarationsNum();
12762 auto TotalLists = C->getTotalComponentListNum();
12763 auto TotalComponents = C->getTotalComponentsNum();
12764
12765 SmallVector<Expr *, 16> Vars;
12766 Vars.reserve(N: NumVars);
12767 for (unsigned i = 0; i != NumVars; ++i)
12768 Vars.push_back(Elt: Record.readSubExpr());
12769 C->setVarRefs(Vars);
12770
12771 SmallVector<ValueDecl *, 16> Decls;
12772 Decls.reserve(N: UniqueDecls);
12773 for (unsigned i = 0; i < UniqueDecls; ++i)
12774 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12775 C->setUniqueDecls(Decls);
12776
12777 SmallVector<unsigned, 16> ListsPerDecl;
12778 ListsPerDecl.reserve(N: UniqueDecls);
12779 for (unsigned i = 0; i < UniqueDecls; ++i)
12780 ListsPerDecl.push_back(Elt: Record.readInt());
12781 C->setDeclNumLists(ListsPerDecl);
12782
12783 SmallVector<unsigned, 32> ListSizes;
12784 ListSizes.reserve(N: TotalLists);
12785 for (unsigned i = 0; i < TotalLists; ++i)
12786 ListSizes.push_back(Elt: Record.readInt());
12787 C->setComponentListSizes(ListSizes);
12788
12789 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12790 Components.reserve(N: TotalComponents);
12791 for (unsigned i = 0; i < TotalComponents; ++i) {
12792 Expr *AssociatedExpr = Record.readSubExpr();
12793 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12794 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12795 /*IsNonContiguous*/ Args: false);
12796 }
12797 C->setComponents(Components, CLSs: ListSizes);
12798}
12799
12800void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12801 C->setLParenLoc(Record.readSourceLocation());
12802 auto NumVars = C->varlist_size();
12803 auto UniqueDecls = C->getUniqueDeclarationsNum();
12804 auto TotalLists = C->getTotalComponentListNum();
12805 auto TotalComponents = C->getTotalComponentsNum();
12806
12807 SmallVector<Expr *, 16> Vars;
12808 Vars.reserve(N: NumVars);
12809 for (unsigned i = 0; i != NumVars; ++i)
12810 Vars.push_back(Elt: Record.readSubExpr());
12811 C->setVarRefs(Vars);
12812 Vars.clear();
12813
12814 SmallVector<ValueDecl *, 16> Decls;
12815 Decls.reserve(N: UniqueDecls);
12816 for (unsigned i = 0; i < UniqueDecls; ++i)
12817 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12818 C->setUniqueDecls(Decls);
12819
12820 SmallVector<unsigned, 16> ListsPerDecl;
12821 ListsPerDecl.reserve(N: UniqueDecls);
12822 for (unsigned i = 0; i < UniqueDecls; ++i)
12823 ListsPerDecl.push_back(Elt: Record.readInt());
12824 C->setDeclNumLists(ListsPerDecl);
12825
12826 SmallVector<unsigned, 32> ListSizes;
12827 ListSizes.reserve(N: TotalLists);
12828 for (unsigned i = 0; i < TotalLists; ++i)
12829 ListSizes.push_back(Elt: Record.readInt());
12830 C->setComponentListSizes(ListSizes);
12831
12832 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12833 Components.reserve(N: TotalComponents);
12834 for (unsigned i = 0; i < TotalComponents; ++i) {
12835 Expr *AssociatedExpr = Record.readSubExpr();
12836 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12837 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12838 /*IsNonContiguous=*/Args: false);
12839 }
12840 C->setComponents(Components, CLSs: ListSizes);
12841}
12842
12843void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12844 C->setLParenLoc(Record.readSourceLocation());
12845 auto NumVars = C->varlist_size();
12846 auto UniqueDecls = C->getUniqueDeclarationsNum();
12847 auto TotalLists = C->getTotalComponentListNum();
12848 auto TotalComponents = C->getTotalComponentsNum();
12849
12850 SmallVector<Expr *, 16> Vars;
12851 Vars.reserve(N: NumVars);
12852 for (unsigned I = 0; I != NumVars; ++I)
12853 Vars.push_back(Elt: Record.readSubExpr());
12854 C->setVarRefs(Vars);
12855 Vars.clear();
12856
12857 SmallVector<ValueDecl *, 16> Decls;
12858 Decls.reserve(N: UniqueDecls);
12859 for (unsigned I = 0; I < UniqueDecls; ++I)
12860 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12861 C->setUniqueDecls(Decls);
12862
12863 SmallVector<unsigned, 16> ListsPerDecl;
12864 ListsPerDecl.reserve(N: UniqueDecls);
12865 for (unsigned I = 0; I < UniqueDecls; ++I)
12866 ListsPerDecl.push_back(Elt: Record.readInt());
12867 C->setDeclNumLists(ListsPerDecl);
12868
12869 SmallVector<unsigned, 32> ListSizes;
12870 ListSizes.reserve(N: TotalLists);
12871 for (unsigned i = 0; i < TotalLists; ++i)
12872 ListSizes.push_back(Elt: Record.readInt());
12873 C->setComponentListSizes(ListSizes);
12874
12875 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12876 Components.reserve(N: TotalComponents);
12877 for (unsigned I = 0; I < TotalComponents; ++I) {
12878 Expr *AssociatedExpr = Record.readSubExpr();
12879 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12880 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12881 /*IsNonContiguous=*/Args: false);
12882 }
12883 C->setComponents(Components, CLSs: ListSizes);
12884}
12885
12886void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12887 C->setLParenLoc(Record.readSourceLocation());
12888 unsigned NumVars = C->varlist_size();
12889 SmallVector<Expr *, 16> Vars;
12890 Vars.reserve(N: NumVars);
12891 for (unsigned i = 0; i != NumVars; ++i)
12892 Vars.push_back(Elt: Record.readSubExpr());
12893 C->setVarRefs(Vars);
12894 Vars.clear();
12895 Vars.reserve(N: NumVars);
12896 for (unsigned i = 0; i != NumVars; ++i)
12897 Vars.push_back(Elt: Record.readSubExpr());
12898 C->setPrivateRefs(Vars);
12899}
12900
12901void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12902 C->setLParenLoc(Record.readSourceLocation());
12903 unsigned NumVars = C->varlist_size();
12904 SmallVector<Expr *, 16> Vars;
12905 Vars.reserve(N: NumVars);
12906 for (unsigned i = 0; i != NumVars; ++i)
12907 Vars.push_back(Elt: Record.readSubExpr());
12908 C->setVarRefs(Vars);
12909}
12910
12911void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12912 C->setLParenLoc(Record.readSourceLocation());
12913 unsigned NumVars = C->varlist_size();
12914 SmallVector<Expr *, 16> Vars;
12915 Vars.reserve(N: NumVars);
12916 for (unsigned i = 0; i != NumVars; ++i)
12917 Vars.push_back(Elt: Record.readSubExpr());
12918 C->setVarRefs(Vars);
12919}
12920
12921void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12922 C->setLParenLoc(Record.readSourceLocation());
12923 unsigned NumOfAllocators = C->getNumberOfAllocators();
12924 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12925 Data.reserve(N: NumOfAllocators);
12926 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12927 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12928 D.Allocator = Record.readSubExpr();
12929 D.AllocatorTraits = Record.readSubExpr();
12930 D.LParenLoc = Record.readSourceLocation();
12931 D.RParenLoc = Record.readSourceLocation();
12932 }
12933 C->setAllocatorsData(Data);
12934}
12935
12936void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12937 C->setLParenLoc(Record.readSourceLocation());
12938 C->setModifier(Record.readSubExpr());
12939 C->setColonLoc(Record.readSourceLocation());
12940 unsigned NumOfLocators = C->varlist_size();
12941 SmallVector<Expr *, 4> Locators;
12942 Locators.reserve(N: NumOfLocators);
12943 for (unsigned I = 0; I != NumOfLocators; ++I)
12944 Locators.push_back(Elt: Record.readSubExpr());
12945 C->setVarRefs(Locators);
12946}
12947
12948void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12949 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12950 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
12951 C->setLParenLoc(Record.readSourceLocation());
12952 C->setKindKwLoc(Record.readSourceLocation());
12953 C->setModifierKwLoc(Record.readSourceLocation());
12954}
12955
12956void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12957 VisitOMPClauseWithPreInit(C);
12958 C->setThreadID(Record.readSubExpr());
12959 C->setLParenLoc(Record.readSourceLocation());
12960}
12961
12962void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12963 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12964 C->setLParenLoc(Record.readSourceLocation());
12965 C->setBindKindLoc(Record.readSourceLocation());
12966}
12967
12968void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12969 C->setAlignment(Record.readExpr());
12970 C->setLParenLoc(Record.readSourceLocation());
12971}
12972
12973void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
12974 VisitOMPClauseWithPreInit(C);
12975 C->setSize(Record.readSubExpr());
12976 C->setLParenLoc(Record.readSourceLocation());
12977}
12978
12979void OMPClauseReader::VisitOMPDynGroupprivateClause(
12980 OMPDynGroupprivateClause *C) {
12981 VisitOMPClauseWithPreInit(C);
12982 C->setDynGroupprivateModifier(
12983 Record.readEnum<OpenMPDynGroupprivateClauseModifier>());
12984 C->setDynGroupprivateFallbackModifier(
12985 Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());
12986 C->setSize(Record.readSubExpr());
12987 C->setLParenLoc(Record.readSourceLocation());
12988 C->setDynGroupprivateModifierLoc(Record.readSourceLocation());
12989 C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());
12990}
12991
12992void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
12993 C->setLParenLoc(Record.readSourceLocation());
12994 C->setDependenceType(
12995 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
12996 C->setDependenceLoc(Record.readSourceLocation());
12997 C->setColonLoc(Record.readSourceLocation());
12998 unsigned NumVars = C->varlist_size();
12999 SmallVector<Expr *, 16> Vars;
13000 Vars.reserve(N: NumVars);
13001 for (unsigned I = 0; I != NumVars; ++I)
13002 Vars.push_back(Elt: Record.readSubExpr());
13003 C->setVarRefs(Vars);
13004 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
13005 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
13006}
13007
13008void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
13009 AttrVec Attrs;
13010 Record.readAttributes(Attrs);
13011 C->setAttrs(Attrs);
13012 C->setLocStart(Record.readSourceLocation());
13013 C->setLParenLoc(Record.readSourceLocation());
13014 C->setLocEnd(Record.readSourceLocation());
13015}
13016
13017void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
13018
13019OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
13020 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
13021 TI.Sets.resize(N: readUInt32());
13022 for (auto &Set : TI.Sets) {
13023 Set.Kind = readEnum<llvm::omp::TraitSet>();
13024 Set.Selectors.resize(N: readUInt32());
13025 for (auto &Selector : Set.Selectors) {
13026 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
13027 Selector.ScoreOrCondition = nullptr;
13028 if (readBool())
13029 Selector.ScoreOrCondition = readExprRef();
13030 Selector.Properties.resize(N: readUInt32());
13031 for (auto &Property : Selector.Properties)
13032 Property.Kind = readEnum<llvm::omp::TraitProperty>();
13033 }
13034 }
13035 return &TI;
13036}
13037
13038void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13039 if (!Data)
13040 return;
13041 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13042 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13043 skipInts(N: 3);
13044 }
13045 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13046 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13047 Clauses[I] = readOMPClause();
13048 Data->setClauses(Clauses);
13049 if (Data->hasAssociatedStmt())
13050 Data->setAssociatedStmt(readStmt());
13051 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13052 Data->getChildren()[I] = readStmt();
13053}
13054
13055SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
13056 unsigned NumVars = readInt();
13057 llvm::SmallVector<Expr *> VarList;
13058 for (unsigned I = 0; I < NumVars; ++I)
13059 VarList.push_back(Elt: readExpr());
13060 return VarList;
13061}
13062
13063SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
13064 unsigned NumExprs = readInt();
13065 llvm::SmallVector<Expr *> ExprList;
13066 for (unsigned I = 0; I < NumExprs; ++I)
13067 ExprList.push_back(Elt: readSubExpr());
13068 return ExprList;
13069}
13070
13071OpenACCClause *ASTRecordReader::readOpenACCClause() {
13072 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
13073 SourceLocation BeginLoc = readSourceLocation();
13074 SourceLocation EndLoc = readSourceLocation();
13075
13076 switch (ClauseKind) {
13077 case OpenACCClauseKind::Default: {
13078 SourceLocation LParenLoc = readSourceLocation();
13079 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
13080 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
13081 EndLoc);
13082 }
13083 case OpenACCClauseKind::If: {
13084 SourceLocation LParenLoc = readSourceLocation();
13085 Expr *CondExpr = readSubExpr();
13086 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
13087 EndLoc);
13088 }
13089 case OpenACCClauseKind::Self: {
13090 SourceLocation LParenLoc = readSourceLocation();
13091 bool isConditionExprClause = readBool();
13092 if (isConditionExprClause) {
13093 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
13094 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
13095 ConditionExpr: CondExpr, EndLoc);
13096 }
13097 unsigned NumVars = readInt();
13098 llvm::SmallVector<Expr *> VarList;
13099 for (unsigned I = 0; I < NumVars; ++I)
13100 VarList.push_back(Elt: readSubExpr());
13101 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
13102 EndLoc);
13103 }
13104 case OpenACCClauseKind::NumGangs: {
13105 SourceLocation LParenLoc = readSourceLocation();
13106 unsigned NumClauses = readInt();
13107 llvm::SmallVector<Expr *> IntExprs;
13108 for (unsigned I = 0; I < NumClauses; ++I)
13109 IntExprs.push_back(Elt: readSubExpr());
13110 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
13111 IntExprs, EndLoc);
13112 }
13113 case OpenACCClauseKind::NumWorkers: {
13114 SourceLocation LParenLoc = readSourceLocation();
13115 Expr *IntExpr = readSubExpr();
13116 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
13117 IntExpr, EndLoc);
13118 }
13119 case OpenACCClauseKind::DeviceNum: {
13120 SourceLocation LParenLoc = readSourceLocation();
13121 Expr *IntExpr = readSubExpr();
13122 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
13123 IntExpr, EndLoc);
13124 }
13125 case OpenACCClauseKind::DefaultAsync: {
13126 SourceLocation LParenLoc = readSourceLocation();
13127 Expr *IntExpr = readSubExpr();
13128 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13129 IntExpr, EndLoc);
13130 }
13131 case OpenACCClauseKind::VectorLength: {
13132 SourceLocation LParenLoc = readSourceLocation();
13133 Expr *IntExpr = readSubExpr();
13134 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
13135 IntExpr, EndLoc);
13136 }
13137 case OpenACCClauseKind::Private: {
13138 SourceLocation LParenLoc = readSourceLocation();
13139 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13140
13141 llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
13142 for (unsigned I = 0; I < VarList.size(); ++I) {
13143 static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
13144 VarDecl *Alloca = readDeclAs<VarDecl>();
13145 RecipeList.push_back(Elt: {Alloca});
13146 }
13147
13148 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13149 VarList, InitRecipes: RecipeList, EndLoc);
13150 }
13151 case OpenACCClauseKind::Host: {
13152 SourceLocation LParenLoc = readSourceLocation();
13153 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13154 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13155 EndLoc);
13156 }
13157 case OpenACCClauseKind::Device: {
13158 SourceLocation LParenLoc = readSourceLocation();
13159 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13160 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13161 VarList, EndLoc);
13162 }
13163 case OpenACCClauseKind::FirstPrivate: {
13164 SourceLocation LParenLoc = readSourceLocation();
13165 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13166 llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
13167 for (unsigned I = 0; I < VarList.size(); ++I) {
13168 static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
13169 VarDecl *Recipe = readDeclAs<VarDecl>();
13170 VarDecl *RecipeTemp = readDeclAs<VarDecl>();
13171 RecipeList.push_back(Elt: {Recipe, RecipeTemp});
13172 }
13173
13174 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13175 VarList, InitRecipes: RecipeList, EndLoc);
13176 }
13177 case OpenACCClauseKind::Attach: {
13178 SourceLocation LParenLoc = readSourceLocation();
13179 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13180 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13181 VarList, EndLoc);
13182 }
13183 case OpenACCClauseKind::Detach: {
13184 SourceLocation LParenLoc = readSourceLocation();
13185 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13186 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
13187 VarList, EndLoc);
13188 }
13189 case OpenACCClauseKind::Delete: {
13190 SourceLocation LParenLoc = readSourceLocation();
13191 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13192 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
13193 VarList, EndLoc);
13194 }
13195 case OpenACCClauseKind::UseDevice: {
13196 SourceLocation LParenLoc = readSourceLocation();
13197 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13198 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
13199 VarList, EndLoc);
13200 }
13201 case OpenACCClauseKind::DevicePtr: {
13202 SourceLocation LParenLoc = readSourceLocation();
13203 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13204 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
13205 VarList, EndLoc);
13206 }
13207 case OpenACCClauseKind::NoCreate: {
13208 SourceLocation LParenLoc = readSourceLocation();
13209 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13210 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
13211 VarList, EndLoc);
13212 }
13213 case OpenACCClauseKind::Present: {
13214 SourceLocation LParenLoc = readSourceLocation();
13215 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13216 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
13217 VarList, EndLoc);
13218 }
13219 case OpenACCClauseKind::PCopy:
13220 case OpenACCClauseKind::PresentOrCopy:
13221 case OpenACCClauseKind::Copy: {
13222 SourceLocation LParenLoc = readSourceLocation();
13223 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13224 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13225 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13226 LParenLoc, Mods: ModList, VarList, EndLoc);
13227 }
13228 case OpenACCClauseKind::CopyIn:
13229 case OpenACCClauseKind::PCopyIn:
13230 case OpenACCClauseKind::PresentOrCopyIn: {
13231 SourceLocation LParenLoc = readSourceLocation();
13232 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13233 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13234 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13235 LParenLoc, Mods: ModList, VarList, EndLoc);
13236 }
13237 case OpenACCClauseKind::CopyOut:
13238 case OpenACCClauseKind::PCopyOut:
13239 case OpenACCClauseKind::PresentOrCopyOut: {
13240 SourceLocation LParenLoc = readSourceLocation();
13241 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13242 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13243 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13244 LParenLoc, Mods: ModList, VarList, EndLoc);
13245 }
13246 case OpenACCClauseKind::Create:
13247 case OpenACCClauseKind::PCreate:
13248 case OpenACCClauseKind::PresentOrCreate: {
13249 SourceLocation LParenLoc = readSourceLocation();
13250 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
13251 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13252 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
13253 LParenLoc, Mods: ModList, VarList, EndLoc);
13254 }
13255 case OpenACCClauseKind::Async: {
13256 SourceLocation LParenLoc = readSourceLocation();
13257 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
13258 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
13259 IntExpr: AsyncExpr, EndLoc);
13260 }
13261 case OpenACCClauseKind::Wait: {
13262 SourceLocation LParenLoc = readSourceLocation();
13263 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
13264 SourceLocation QueuesLoc = readSourceLocation();
13265 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
13266 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
13267 DevNumExpr, QueuesLoc, QueueIdExprs,
13268 EndLoc);
13269 }
13270 case OpenACCClauseKind::DeviceType:
13271 case OpenACCClauseKind::DType: {
13272 SourceLocation LParenLoc = readSourceLocation();
13273 llvm::SmallVector<DeviceTypeArgument> Archs;
13274 unsigned NumArchs = readInt();
13275
13276 for (unsigned I = 0; I < NumArchs; ++I) {
13277 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
13278 SourceLocation Loc = readSourceLocation();
13279 Archs.emplace_back(Args&: Loc, Args&: Ident);
13280 }
13281
13282 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
13283 LParenLoc, Archs, EndLoc);
13284 }
13285 case OpenACCClauseKind::Reduction: {
13286 SourceLocation LParenLoc = readSourceLocation();
13287 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
13288 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13289 llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
13290
13291 for (unsigned I = 0; I < VarList.size(); ++I) {
13292 VarDecl *Recipe = readDeclAs<VarDecl>();
13293
13294 static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==
13295 3 * sizeof(int *));
13296
13297 llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;
13298 unsigned NumCombiners = readInt();
13299 for (unsigned I = 0; I < NumCombiners; ++I) {
13300 VarDecl *LHS = readDeclAs<VarDecl>();
13301 VarDecl *RHS = readDeclAs<VarDecl>();
13302 Expr *Op = readExpr();
13303
13304 Combiners.push_back(Elt: {.LHS: LHS, .RHS: RHS, .Op: Op});
13305 }
13306
13307 RecipeList.push_back(Elt: {Recipe, Combiners});
13308 }
13309
13310 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
13311 VarList, Recipes: RecipeList, EndLoc);
13312 }
13313 case OpenACCClauseKind::Seq:
13314 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13315 case OpenACCClauseKind::NoHost:
13316 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13317 case OpenACCClauseKind::Finalize:
13318 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13319 case OpenACCClauseKind::IfPresent:
13320 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13321 case OpenACCClauseKind::Independent:
13322 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13323 case OpenACCClauseKind::Auto:
13324 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
13325 case OpenACCClauseKind::Collapse: {
13326 SourceLocation LParenLoc = readSourceLocation();
13327 bool HasForce = readBool();
13328 Expr *LoopCount = readSubExpr();
13329 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
13330 HasForce, LoopCount, EndLoc);
13331 }
13332 case OpenACCClauseKind::Tile: {
13333 SourceLocation LParenLoc = readSourceLocation();
13334 unsigned NumClauses = readInt();
13335 llvm::SmallVector<Expr *> SizeExprs;
13336 for (unsigned I = 0; I < NumClauses; ++I)
13337 SizeExprs.push_back(Elt: readSubExpr());
13338 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
13339 SizeExprs, EndLoc);
13340 }
13341 case OpenACCClauseKind::Gang: {
13342 SourceLocation LParenLoc = readSourceLocation();
13343 unsigned NumExprs = readInt();
13344 llvm::SmallVector<OpenACCGangKind> GangKinds;
13345 llvm::SmallVector<Expr *> Exprs;
13346 for (unsigned I = 0; I < NumExprs; ++I) {
13347 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
13348 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
13349 Exprs.push_back(Elt: readExpr());
13350 }
13351 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13352 GangKinds, IntExprs: Exprs, EndLoc);
13353 }
13354 case OpenACCClauseKind::Worker: {
13355 SourceLocation LParenLoc = readSourceLocation();
13356 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
13357 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13358 IntExpr: WorkerExpr, EndLoc);
13359 }
13360 case OpenACCClauseKind::Vector: {
13361 SourceLocation LParenLoc = readSourceLocation();
13362 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
13363 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
13364 IntExpr: VectorExpr, EndLoc);
13365 }
13366 case OpenACCClauseKind::Link: {
13367 SourceLocation LParenLoc = readSourceLocation();
13368 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13369 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
13370 EndLoc);
13371 }
13372 case OpenACCClauseKind::DeviceResident: {
13373 SourceLocation LParenLoc = readSourceLocation();
13374 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13375 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
13376 LParenLoc, VarList, EndLoc);
13377 }
13378
13379 case OpenACCClauseKind::Bind: {
13380 SourceLocation LParenLoc = readSourceLocation();
13381 bool IsString = readBool();
13382 if (IsString)
13383 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13384 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
13385 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
13386 ID: readIdentifier(), EndLoc);
13387 }
13388 case OpenACCClauseKind::Shortloop:
13389 case OpenACCClauseKind::Invalid:
13390 llvm_unreachable("Clause serialization not yet implemented");
13391 }
13392 llvm_unreachable("Invalid Clause Kind");
13393}
13394
13395void ASTRecordReader::readOpenACCClauseList(
13396 MutableArrayRef<const OpenACCClause *> Clauses) {
13397 for (unsigned I = 0; I < Clauses.size(); ++I)
13398 Clauses[I] = readOpenACCClause();
13399}
13400
13401void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
13402 unsigned NumVars = readInt();
13403 A->Clauses.resize(N: NumVars);
13404 readOpenACCClauseList(Clauses: A->Clauses);
13405}
13406
13407static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
13408 // TODO: Maybe it is better to check PrimaryModuleName is a valid
13409 // module name?
13410 llvm::FoldingSetNodeID ID;
13411 ID.AddString(String: PrimaryModuleName);
13412 return ID.computeStableHash();
13413}
13414
13415UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
13416 if (!M)
13417 return std::nullopt;
13418
13419 if (M->isHeaderLikeModule())
13420 return std::nullopt;
13421
13422 if (M->isGlobalModule())
13423 return std::nullopt;
13424
13425 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
13426 return getStableHashForModuleName(PrimaryModuleName);
13427}
13428