1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ASTCommon.h"
14#include "ASTReaderInternals.h"
15#include "TemplateArgumentHasher.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/ASTMutationListener.h"
19#include "clang/AST/ASTStructuralEquivalence.h"
20#include "clang/AST/ASTUnresolvedSet.h"
21#include "clang/AST/AbstractTypeReader.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclBase.h"
24#include "clang/AST/DeclCXX.h"
25#include "clang/AST/DeclFriend.h"
26#include "clang/AST/DeclGroup.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/DeclTemplate.h"
29#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExternalASTSource.h"
33#include "clang/AST/NestedNameSpecifier.h"
34#include "clang/AST/ODRDiagsEmitter.h"
35#include "clang/AST/OpenACCClause.h"
36#include "clang/AST/OpenMPClause.h"
37#include "clang/AST/RawCommentList.h"
38#include "clang/AST/TemplateBase.h"
39#include "clang/AST/TemplateName.h"
40#include "clang/AST/Type.h"
41#include "clang/AST/TypeLoc.h"
42#include "clang/AST/TypeLocVisitor.h"
43#include "clang/AST/UnresolvedSet.h"
44#include "clang/Basic/ASTSourceDescriptor.h"
45#include "clang/Basic/CommentOptions.h"
46#include "clang/Basic/Diagnostic.h"
47#include "clang/Basic/DiagnosticIDs.h"
48#include "clang/Basic/DiagnosticOptions.h"
49#include "clang/Basic/DiagnosticSema.h"
50#include "clang/Basic/FileManager.h"
51#include "clang/Basic/FileSystemOptions.h"
52#include "clang/Basic/IdentifierTable.h"
53#include "clang/Basic/LLVM.h"
54#include "clang/Basic/LangOptions.h"
55#include "clang/Basic/Module.h"
56#include "clang/Basic/ObjCRuntime.h"
57#include "clang/Basic/OpenACCKinds.h"
58#include "clang/Basic/OpenMPKinds.h"
59#include "clang/Basic/OperatorKinds.h"
60#include "clang/Basic/PragmaKinds.h"
61#include "clang/Basic/Sanitizers.h"
62#include "clang/Basic/SourceLocation.h"
63#include "clang/Basic/SourceManager.h"
64#include "clang/Basic/SourceManagerInternals.h"
65#include "clang/Basic/Specifiers.h"
66#include "clang/Basic/TargetInfo.h"
67#include "clang/Basic/TargetOptions.h"
68#include "clang/Basic/TokenKinds.h"
69#include "clang/Basic/Version.h"
70#include "clang/Lex/HeaderSearch.h"
71#include "clang/Lex/HeaderSearchOptions.h"
72#include "clang/Lex/MacroInfo.h"
73#include "clang/Lex/ModuleMap.h"
74#include "clang/Lex/PreprocessingRecord.h"
75#include "clang/Lex/Preprocessor.h"
76#include "clang/Lex/PreprocessorOptions.h"
77#include "clang/Lex/Token.h"
78#include "clang/Sema/ObjCMethodList.h"
79#include "clang/Sema/Scope.h"
80#include "clang/Sema/Sema.h"
81#include "clang/Sema/SemaCUDA.h"
82#include "clang/Sema/SemaObjC.h"
83#include "clang/Sema/Weak.h"
84#include "clang/Serialization/ASTBitCodes.h"
85#include "clang/Serialization/ASTDeserializationListener.h"
86#include "clang/Serialization/ASTRecordReader.h"
87#include "clang/Serialization/ContinuousRangeMap.h"
88#include "clang/Serialization/GlobalModuleIndex.h"
89#include "clang/Serialization/InMemoryModuleCache.h"
90#include "clang/Serialization/ModuleCache.h"
91#include "clang/Serialization/ModuleFile.h"
92#include "clang/Serialization/ModuleFileExtension.h"
93#include "clang/Serialization/ModuleManager.h"
94#include "clang/Serialization/PCHContainerOperations.h"
95#include "clang/Serialization/SerializationDiagnostic.h"
96#include "llvm/ADT/APFloat.h"
97#include "llvm/ADT/APInt.h"
98#include "llvm/ADT/ArrayRef.h"
99#include "llvm/ADT/DenseMap.h"
100#include "llvm/ADT/FoldingSet.h"
101#include "llvm/ADT/IntrusiveRefCntPtr.h"
102#include "llvm/ADT/STLExtras.h"
103#include "llvm/ADT/ScopeExit.h"
104#include "llvm/ADT/Sequence.h"
105#include "llvm/ADT/SmallPtrSet.h"
106#include "llvm/ADT/SmallVector.h"
107#include "llvm/ADT/StringExtras.h"
108#include "llvm/ADT/StringMap.h"
109#include "llvm/ADT/StringRef.h"
110#include "llvm/ADT/iterator_range.h"
111#include "llvm/Bitstream/BitstreamReader.h"
112#include "llvm/Support/Compiler.h"
113#include "llvm/Support/Compression.h"
114#include "llvm/Support/DJB.h"
115#include "llvm/Support/Endian.h"
116#include "llvm/Support/Error.h"
117#include "llvm/Support/ErrorHandling.h"
118#include "llvm/Support/LEB128.h"
119#include "llvm/Support/MemoryBuffer.h"
120#include "llvm/Support/Path.h"
121#include "llvm/Support/SaveAndRestore.h"
122#include "llvm/Support/TimeProfiler.h"
123#include "llvm/Support/Timer.h"
124#include "llvm/Support/VersionTuple.h"
125#include "llvm/Support/raw_ostream.h"
126#include "llvm/TargetParser/Triple.h"
127#include <algorithm>
128#include <cassert>
129#include <cstddef>
130#include <cstdint>
131#include <cstdio>
132#include <ctime>
133#include <iterator>
134#include <limits>
135#include <map>
136#include <memory>
137#include <optional>
138#include <string>
139#include <system_error>
140#include <tuple>
141#include <utility>
142#include <vector>
143
144using namespace clang;
145using namespace clang::serialization;
146using namespace clang::serialization::reader;
147using llvm::BitstreamCursor;
148
149//===----------------------------------------------------------------------===//
150// ChainedASTReaderListener implementation
151//===----------------------------------------------------------------------===//
152
153bool
154ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
155 return First->ReadFullVersionInformation(FullVersion) ||
156 Second->ReadFullVersionInformation(FullVersion);
157}
158
159void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
160 First->ReadModuleName(ModuleName);
161 Second->ReadModuleName(ModuleName);
162}
163
164void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
165 First->ReadModuleMapFile(ModuleMapPath);
166 Second->ReadModuleMapFile(ModuleMapPath);
167}
168
169bool ChainedASTReaderListener::ReadLanguageOptions(
170 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
171 bool AllowCompatibleDifferences) {
172 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
173 AllowCompatibleDifferences) ||
174 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
175 AllowCompatibleDifferences);
176}
177
178bool ChainedASTReaderListener::ReadTargetOptions(
179 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
180 bool AllowCompatibleDifferences) {
181 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
182 AllowCompatibleDifferences) ||
183 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
184 AllowCompatibleDifferences);
185}
186
187bool ChainedASTReaderListener::ReadDiagnosticOptions(
188 DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {
189 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
190 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
191}
192
193bool
194ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
195 bool Complain) {
196 return First->ReadFileSystemOptions(FSOpts, Complain) ||
197 Second->ReadFileSystemOptions(FSOpts, Complain);
198}
199
200bool ChainedASTReaderListener::ReadHeaderSearchOptions(
201 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
202 StringRef SpecificModuleCachePath, bool Complain) {
203 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
204 SpecificModuleCachePath, Complain) ||
205 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
206 SpecificModuleCachePath, Complain);
207}
208
209bool ChainedASTReaderListener::ReadPreprocessorOptions(
210 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
211 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
212 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
213 Complain, SuggestedPredefines) ||
214 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
215 Complain, SuggestedPredefines);
216}
217
218void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
219 unsigned Value) {
220 First->ReadCounter(M, Value);
221 Second->ReadCounter(M, Value);
222}
223
224bool ChainedASTReaderListener::needsInputFileVisitation() {
225 return First->needsInputFileVisitation() ||
226 Second->needsInputFileVisitation();
227}
228
229bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
230 return First->needsSystemInputFileVisitation() ||
231 Second->needsSystemInputFileVisitation();
232}
233
234void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
235 ModuleKind Kind) {
236 First->visitModuleFile(Filename, Kind);
237 Second->visitModuleFile(Filename, Kind);
238}
239
240bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
241 bool isSystem,
242 bool isOverridden,
243 bool isExplicitModule) {
244 bool Continue = false;
245 if (First->needsInputFileVisitation() &&
246 (!isSystem || First->needsSystemInputFileVisitation()))
247 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
248 isExplicitModule);
249 if (Second->needsInputFileVisitation() &&
250 (!isSystem || Second->needsSystemInputFileVisitation()))
251 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
252 isExplicitModule);
253 return Continue;
254}
255
256void ChainedASTReaderListener::readModuleFileExtension(
257 const ModuleFileExtensionMetadata &Metadata) {
258 First->readModuleFileExtension(Metadata);
259 Second->readModuleFileExtension(Metadata);
260}
261
262//===----------------------------------------------------------------------===//
263// PCH validator implementation
264//===----------------------------------------------------------------------===//
265
266ASTReaderListener::~ASTReaderListener() = default;
267
268/// Compare the given set of language options against an existing set of
269/// language options.
270///
271/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
272/// \param AllowCompatibleDifferences If true, differences between compatible
273/// language options will be permitted.
274///
275/// \returns true if the languagae options mis-match, false otherwise.
276static bool checkLanguageOptions(const LangOptions &LangOpts,
277 const LangOptions &ExistingLangOpts,
278 StringRef ModuleFilename,
279 DiagnosticsEngine *Diags,
280 bool AllowCompatibleDifferences = true) {
281#define LANGOPT(Name, Bits, Default, Description) \
282 if (ExistingLangOpts.Name != LangOpts.Name) { \
283 if (Diags) { \
284 if (Bits == 1) \
285 Diags->Report(diag::err_ast_file_langopt_mismatch) \
286 << Description << LangOpts.Name << ExistingLangOpts.Name \
287 << ModuleFilename; \
288 else \
289 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
290 << Description << ModuleFilename; \
291 } \
292 return true; \
293 }
294
295#define VALUE_LANGOPT(Name, Bits, Default, Description) \
296 if (ExistingLangOpts.Name != LangOpts.Name) { \
297 if (Diags) \
298 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
299 << Description << ModuleFilename; \
300 return true; \
301 }
302
303#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
304 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
305 if (Diags) \
306 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
307 << Description << ModuleFilename; \
308 return true; \
309 }
310
311#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
312 if (!AllowCompatibleDifferences) \
313 LANGOPT(Name, Bits, Default, Description)
314
315#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
316 if (!AllowCompatibleDifferences) \
317 ENUM_LANGOPT(Name, Bits, Default, Description)
318
319#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
320 if (!AllowCompatibleDifferences) \
321 VALUE_LANGOPT(Name, Bits, Default, Description)
322
323#define BENIGN_LANGOPT(Name, Bits, Default, Description)
324#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
325#define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
326#include "clang/Basic/LangOptions.def"
327
328 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
329 if (Diags)
330 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
331 << "module features" << ModuleFilename;
332 return true;
333 }
334
335 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
336 if (Diags)
337 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
338 << "target Objective-C runtime" << ModuleFilename;
339 return true;
340 }
341
342 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
343 LangOpts.CommentOpts.BlockCommandNames) {
344 if (Diags)
345 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
346 << "block command names" << ModuleFilename;
347 return true;
348 }
349
350 // Sanitizer feature mismatches are treated as compatible differences. If
351 // compatible differences aren't allowed, we still only want to check for
352 // mismatches of non-modular sanitizers (the only ones which can affect AST
353 // generation).
354 if (!AllowCompatibleDifferences) {
355 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
356 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
357 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
358 ExistingSanitizers.clear(K: ModularSanitizers);
359 ImportedSanitizers.clear(K: ModularSanitizers);
360 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
361 const std::string Flag = "-fsanitize=";
362 if (Diags) {
363#define SANITIZER(NAME, ID) \
364 { \
365 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
366 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
367 if (InExistingModule != InImportedModule) \
368 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
369 << InExistingModule << ModuleFilename << (Flag + NAME); \
370 }
371#include "clang/Basic/Sanitizers.def"
372 }
373 return true;
374 }
375 }
376
377 return false;
378}
379
380/// Compare the given set of target options against an existing set of
381/// target options.
382///
383/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
384///
385/// \returns true if the target options mis-match, false otherwise.
386static bool checkTargetOptions(const TargetOptions &TargetOpts,
387 const TargetOptions &ExistingTargetOpts,
388 StringRef ModuleFilename,
389 DiagnosticsEngine *Diags,
390 bool AllowCompatibleDifferences = true) {
391#define CHECK_TARGET_OPT(Field, Name) \
392 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
393 if (Diags) \
394 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
395 << ModuleFilename << Name << TargetOpts.Field \
396 << ExistingTargetOpts.Field; \
397 return true; \
398 }
399
400 // The triple and ABI must match exactly.
401 CHECK_TARGET_OPT(Triple, "target");
402 CHECK_TARGET_OPT(ABI, "target ABI");
403
404 // We can tolerate different CPUs in many cases, notably when one CPU
405 // supports a strict superset of another. When allowing compatible
406 // differences skip this check.
407 if (!AllowCompatibleDifferences) {
408 CHECK_TARGET_OPT(CPU, "target CPU");
409 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
410 }
411
412#undef CHECK_TARGET_OPT
413
414 // Compare feature sets.
415 SmallVector<StringRef, 4> ExistingFeatures(
416 ExistingTargetOpts.FeaturesAsWritten.begin(),
417 ExistingTargetOpts.FeaturesAsWritten.end());
418 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
419 TargetOpts.FeaturesAsWritten.end());
420 llvm::sort(C&: ExistingFeatures);
421 llvm::sort(C&: ReadFeatures);
422
423 // We compute the set difference in both directions explicitly so that we can
424 // diagnose the differences differently.
425 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
426 std::set_difference(
427 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
428 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
429 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
430 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
431 result: std::back_inserter(x&: UnmatchedReadFeatures));
432
433 // If we are allowing compatible differences and the read feature set is
434 // a strict subset of the existing feature set, there is nothing to diagnose.
435 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
436 return false;
437
438 if (Diags) {
439 for (StringRef Feature : UnmatchedReadFeatures)
440 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
441 << /* is-existing-feature */ false << ModuleFilename << Feature;
442 for (StringRef Feature : UnmatchedExistingFeatures)
443 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
444 << /* is-existing-feature */ true << ModuleFilename << Feature;
445 }
446
447 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
448}
449
450bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
451 StringRef ModuleFilename, bool Complain,
452 bool AllowCompatibleDifferences) {
453 const LangOptions &ExistingLangOpts = PP.getLangOpts();
454 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
455 Diags: Complain ? &Reader.Diags : nullptr,
456 AllowCompatibleDifferences);
457}
458
459bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
460 StringRef ModuleFilename, bool Complain,
461 bool AllowCompatibleDifferences) {
462 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
463 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
464 Diags: Complain ? &Reader.Diags : nullptr,
465 AllowCompatibleDifferences);
466}
467
468namespace {
469
470using MacroDefinitionsMap =
471 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
472using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
473
474} // namespace
475
476static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
477 DiagnosticsEngine &Diags,
478 StringRef ModuleFilename,
479 bool Complain) {
480 using Level = DiagnosticsEngine::Level;
481
482 // Check current mappings for new -Werror mappings, and the stored mappings
483 // for cases that were explicitly mapped to *not* be errors that are now
484 // errors because of options like -Werror.
485 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
486
487 for (DiagnosticsEngine *MappingSource : MappingSources) {
488 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
489 diag::kind DiagID = DiagIDMappingPair.first;
490 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
491 if (CurLevel < DiagnosticsEngine::Error)
492 continue; // not significant
493 Level StoredLevel =
494 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
495 if (StoredLevel < DiagnosticsEngine::Error) {
496 if (Complain)
497 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
498 << "-Werror=" + Diags.getDiagnosticIDs()
499 ->getWarningOptionForDiag(DiagID)
500 .str()
501 << ModuleFilename;
502 return true;
503 }
504 }
505 }
506
507 return false;
508}
509
510static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
511 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
512 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
513 return true;
514 return Ext >= diag::Severity::Error;
515}
516
517static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
518 DiagnosticsEngine &Diags,
519 StringRef ModuleFilename, bool IsSystem,
520 bool SystemHeaderWarningsInModule,
521 bool Complain) {
522 // Top-level options
523 if (IsSystem) {
524 if (Diags.getSuppressSystemWarnings())
525 return false;
526 // If -Wsystem-headers was not enabled before, and it was not explicit,
527 // be conservative
528 if (StoredDiags.getSuppressSystemWarnings() &&
529 !SystemHeaderWarningsInModule) {
530 if (Complain)
531 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
532 << "-Wsystem-headers" << ModuleFilename;
533 return true;
534 }
535 }
536
537 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
538 if (Complain)
539 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
540 << "-Werror" << ModuleFilename;
541 return true;
542 }
543
544 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
545 !StoredDiags.getEnableAllWarnings()) {
546 if (Complain)
547 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
548 << "-Weverything -Werror" << ModuleFilename;
549 return true;
550 }
551
552 if (isExtHandlingFromDiagsError(Diags) &&
553 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
554 if (Complain)
555 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
556 << "-pedantic-errors" << ModuleFilename;
557 return true;
558 }
559
560 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
561 Complain);
562}
563
564/// Return the top import module if it is implicit, nullptr otherwise.
565static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
566 Preprocessor &PP) {
567 // If the original import came from a file explicitly generated by the user,
568 // don't check the diagnostic mappings.
569 // FIXME: currently this is approximated by checking whether this is not a
570 // module import of an implicitly-loaded module file.
571 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
572 // the transitive closure of its imports, since unrelated modules cannot be
573 // imported until after this module finishes validation.
574 ModuleFile *TopImport = &*ModuleMgr.rbegin();
575 while (!TopImport->ImportedBy.empty())
576 TopImport = TopImport->ImportedBy[0];
577 if (TopImport->Kind != MK_ImplicitModule)
578 return nullptr;
579
580 StringRef ModuleName = TopImport->ModuleName;
581 assert(!ModuleName.empty() && "diagnostic options read before module name");
582
583 Module *M =
584 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
585 assert(M && "missing module");
586 return M;
587}
588
589bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
590 StringRef ModuleFilename,
591 bool Complain) {
592 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
593 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
594 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
595 new DiagnosticsEngine(DiagIDs, DiagOpts));
596 // This should never fail, because we would have processed these options
597 // before writing them to an ASTFile.
598 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
599 VFS&: PP.getFileManager().getVirtualFileSystem(),
600 /*Report*/ ReportDiags: false);
601
602 ModuleManager &ModuleMgr = Reader.getModuleManager();
603 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
604
605 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
606 if (!TopM)
607 return false;
608
609 Module *Importer = PP.getCurrentModule();
610
611 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
612 bool SystemHeaderWarningsInModule =
613 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
614 Element: Importer->Name);
615
616 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
617 // contains the union of their flags.
618 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
619 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
620 Complain);
621}
622
623/// Collect the macro definitions provided by the given preprocessor
624/// options.
625static void
626collectMacroDefinitions(const PreprocessorOptions &PPOpts,
627 MacroDefinitionsMap &Macros,
628 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
629 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
630 StringRef Macro = PPOpts.Macros[I].first;
631 bool IsUndef = PPOpts.Macros[I].second;
632
633 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
634 StringRef MacroName = MacroPair.first;
635 StringRef MacroBody = MacroPair.second;
636
637 // For an #undef'd macro, we only care about the name.
638 if (IsUndef) {
639 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
640 if (MacroNames && Inserted)
641 MacroNames->push_back(Elt: MacroName);
642
643 It->second = std::make_pair(x: "", y: true);
644 continue;
645 }
646
647 // For a #define'd macro, figure out the actual definition.
648 if (MacroName.size() == Macro.size())
649 MacroBody = "1";
650 else {
651 // Note: GCC drops anything following an end-of-line character.
652 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
653 MacroBody = MacroBody.substr(Start: 0, N: End);
654 }
655
656 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
657 if (MacroNames && Inserted)
658 MacroNames->push_back(Elt: MacroName);
659 It->second = std::make_pair(x&: MacroBody, y: false);
660 }
661}
662
663enum OptionValidation {
664 OptionValidateNone,
665 OptionValidateContradictions,
666 OptionValidateStrictMatches,
667};
668
669/// Check the preprocessor options deserialized from the control block
670/// against the preprocessor options in an existing preprocessor.
671///
672/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
673/// \param Validation If set to OptionValidateNone, ignore differences in
674/// preprocessor options. If set to OptionValidateContradictions,
675/// require that options passed both in the AST file and on the command
676/// line (-D or -U) match, but tolerate options missing in one or the
677/// other. If set to OptionValidateContradictions, require that there
678/// are no differences in the options between the two.
679static bool checkPreprocessorOptions(
680 const PreprocessorOptions &PPOpts,
681 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
682 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
683 std::string &SuggestedPredefines, const LangOptions &LangOpts,
684 OptionValidation Validation = OptionValidateContradictions) {
685 if (ReadMacros) {
686 // Check macro definitions.
687 MacroDefinitionsMap ASTFileMacros;
688 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
689 MacroDefinitionsMap ExistingMacros;
690 SmallVector<StringRef, 4> ExistingMacroNames;
691 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
692 MacroNames: &ExistingMacroNames);
693
694 // Use a line marker to enter the <command line> file, as the defines and
695 // undefines here will have come from the command line.
696 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
697
698 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
699 // Dig out the macro definition in the existing preprocessor options.
700 StringRef MacroName = ExistingMacroNames[I];
701 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
702
703 // Check whether we know anything about this macro name or not.
704 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
705 ASTFileMacros.find(Key: MacroName);
706 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
707 if (Validation == OptionValidateStrictMatches) {
708 // If strict matches are requested, don't tolerate any extra defines
709 // on the command line that are missing in the AST file.
710 if (Diags) {
711 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
712 << MacroName << true << ModuleFilename;
713 }
714 return true;
715 }
716 // FIXME: Check whether this identifier was referenced anywhere in the
717 // AST file. If so, we should reject the AST file. Unfortunately, this
718 // information isn't in the control block. What shall we do about it?
719
720 if (Existing.second) {
721 SuggestedPredefines += "#undef ";
722 SuggestedPredefines += MacroName.str();
723 SuggestedPredefines += '\n';
724 } else {
725 SuggestedPredefines += "#define ";
726 SuggestedPredefines += MacroName.str();
727 SuggestedPredefines += ' ';
728 SuggestedPredefines += Existing.first.str();
729 SuggestedPredefines += '\n';
730 }
731 continue;
732 }
733
734 // If the macro was defined in one but undef'd in the other, we have a
735 // conflict.
736 if (Existing.second != Known->second.second) {
737 if (Diags) {
738 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
739 << MacroName << Known->second.second << ModuleFilename;
740 }
741 return true;
742 }
743
744 // If the macro was #undef'd in both, or if the macro bodies are
745 // identical, it's fine.
746 if (Existing.second || Existing.first == Known->second.first) {
747 ASTFileMacros.erase(I: Known);
748 continue;
749 }
750
751 // The macro bodies differ; complain.
752 if (Diags) {
753 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
754 << MacroName << Known->second.first << Existing.first
755 << ModuleFilename;
756 }
757 return true;
758 }
759
760 // Leave the <command line> file and return to <built-in>.
761 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
762
763 if (Validation == OptionValidateStrictMatches) {
764 // If strict matches are requested, don't tolerate any extra defines in
765 // the AST file that are missing on the command line.
766 for (const auto &MacroName : ASTFileMacros.keys()) {
767 if (Diags) {
768 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
769 << MacroName << false << ModuleFilename;
770 }
771 return true;
772 }
773 }
774 }
775
776 // Check whether we're using predefines.
777 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
778 Validation != OptionValidateNone) {
779 if (Diags) {
780 Diags->Report(DiagID: diag::err_ast_file_undef)
781 << ExistingPPOpts.UsePredefines << ModuleFilename;
782 }
783 return true;
784 }
785
786 // Detailed record is important since it is used for the module cache hash.
787 if (LangOpts.Modules &&
788 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
789 Validation != OptionValidateNone) {
790 if (Diags) {
791 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
792 << PPOpts.DetailedRecord << ModuleFilename;
793 }
794 return true;
795 }
796
797 // Compute the #include and #include_macros lines we need.
798 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
799 StringRef File = ExistingPPOpts.Includes[I];
800
801 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
802 !ExistingPPOpts.PCHThroughHeader.empty()) {
803 // In case the through header is an include, we must add all the includes
804 // to the predefines so the start point can be determined.
805 SuggestedPredefines += "#include \"";
806 SuggestedPredefines += File;
807 SuggestedPredefines += "\"\n";
808 continue;
809 }
810
811 if (File == ExistingPPOpts.ImplicitPCHInclude)
812 continue;
813
814 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
815 continue;
816
817 SuggestedPredefines += "#include \"";
818 SuggestedPredefines += File;
819 SuggestedPredefines += "\"\n";
820 }
821
822 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
823 StringRef File = ExistingPPOpts.MacroIncludes[I];
824 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
825 continue;
826
827 SuggestedPredefines += "#__include_macros \"";
828 SuggestedPredefines += File;
829 SuggestedPredefines += "\"\n##\n";
830 }
831
832 return false;
833}
834
835bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
836 StringRef ModuleFilename,
837 bool ReadMacros, bool Complain,
838 std::string &SuggestedPredefines) {
839 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
840
841 return checkPreprocessorOptions(
842 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
843 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
844 SuggestedPredefines, LangOpts: PP.getLangOpts());
845}
846
847bool SimpleASTReaderListener::ReadPreprocessorOptions(
848 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
849 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
850 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
851 ModuleFilename, ReadMacros, Diags: nullptr,
852 FileMgr&: PP.getFileManager(), SuggestedPredefines,
853 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
854}
855
856/// Check that the specified and the existing module cache paths are equivalent.
857///
858/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
859/// \returns true when the module cache paths differ.
860static bool checkModuleCachePath(llvm::vfs::FileSystem &VFS,
861 StringRef SpecificModuleCachePath,
862 StringRef ExistingModuleCachePath,
863 StringRef ModuleFilename,
864 DiagnosticsEngine *Diags,
865 const LangOptions &LangOpts,
866 const PreprocessorOptions &PPOpts) {
867 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
868 SpecificModuleCachePath == ExistingModuleCachePath)
869 return false;
870 auto EqualOrErr =
871 VFS.equivalent(A: SpecificModuleCachePath, B: ExistingModuleCachePath);
872 if (EqualOrErr && *EqualOrErr)
873 return false;
874 if (Diags)
875 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
876 << SpecificModuleCachePath << ExistingModuleCachePath << ModuleFilename;
877 return true;
878}
879
880bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
881 StringRef ModuleFilename,
882 StringRef SpecificModuleCachePath,
883 bool Complain) {
884 return checkModuleCachePath(
885 VFS&: Reader.getFileManager().getVirtualFileSystem(), SpecificModuleCachePath,
886 ExistingModuleCachePath: PP.getHeaderSearchInfo().getModuleCachePath(), ModuleFilename,
887 Diags: Complain ? &Reader.Diags : nullptr, LangOpts: PP.getLangOpts(),
888 PPOpts: PP.getPreprocessorOpts());
889}
890
891void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
892 PP.setCounterValue(Value);
893}
894
895//===----------------------------------------------------------------------===//
896// AST reader implementation
897//===----------------------------------------------------------------------===//
898
899static uint64_t readULEB(const unsigned char *&P) {
900 unsigned Length = 0;
901 const char *Error = nullptr;
902
903 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
904 if (Error)
905 llvm::report_fatal_error(reason: Error);
906 P += Length;
907 return Val;
908}
909
910/// Read ULEB-encoded key length and data length.
911static std::pair<unsigned, unsigned>
912readULEBKeyDataLength(const unsigned char *&P) {
913 unsigned KeyLen = readULEB(P);
914 if ((unsigned)KeyLen != KeyLen)
915 llvm::report_fatal_error(reason: "key too large");
916
917 unsigned DataLen = readULEB(P);
918 if ((unsigned)DataLen != DataLen)
919 llvm::report_fatal_error(reason: "data too large");
920
921 return std::make_pair(x&: KeyLen, y&: DataLen);
922}
923
924void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
925 bool TakeOwnership) {
926 DeserializationListener = Listener;
927 OwnsDeserializationListener = TakeOwnership;
928}
929
930unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
931 return serialization::ComputeHash(Sel);
932}
933
934LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
935 LocalDeclID ID(Value);
936#ifndef NDEBUG
937 if (!MF.ModuleOffsetMap.empty())
938 Reader.ReadModuleOffsetMap(MF);
939
940 unsigned ModuleFileIndex = ID.getModuleFileIndex();
941 unsigned LocalDeclID = ID.getLocalDeclIndex();
942
943 assert(ModuleFileIndex <= MF.TransitiveImports.size());
944
945 ModuleFile *OwningModuleFile =
946 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
947 assert(OwningModuleFile);
948
949 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
950
951 if (!ModuleFileIndex)
952 LocalNumDecls += NUM_PREDEF_DECL_IDS;
953
954 assert(LocalDeclID < LocalNumDecls);
955#endif
956 (void)Reader;
957 (void)MF;
958 return ID;
959}
960
961LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
962 unsigned ModuleFileIndex, unsigned LocalDeclID) {
963 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
964 return LocalDeclID::get(Reader, MF, Value);
965}
966
967std::pair<unsigned, unsigned>
968ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
969 return readULEBKeyDataLength(P&: d);
970}
971
972ASTSelectorLookupTrait::internal_key_type
973ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
974 using namespace llvm::support;
975
976 SelectorTable &SelTable = Reader.getContext().Selectors;
977 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
978 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
979 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
980 if (N == 0)
981 return SelTable.getNullarySelector(ID: FirstII);
982 else if (N == 1)
983 return SelTable.getUnarySelector(ID: FirstII);
984
985 SmallVector<const IdentifierInfo *, 16> Args;
986 Args.push_back(Elt: FirstII);
987 for (unsigned I = 1; I != N; ++I)
988 Args.push_back(Elt: Reader.getLocalIdentifier(
989 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
990
991 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
992}
993
994ASTSelectorLookupTrait::data_type
995ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
996 unsigned DataLen) {
997 using namespace llvm::support;
998
999 data_type Result;
1000
1001 Result.ID = Reader.getGlobalSelectorID(
1002 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1003 unsigned FullInstanceBits =
1004 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1005 unsigned FullFactoryBits =
1006 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1007 Result.InstanceBits = FullInstanceBits & 0x3;
1008 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1009 Result.FactoryBits = FullFactoryBits & 0x3;
1010 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1011 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1012 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1013
1014 // Load instance methods
1015 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1016 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1017 F, LocalID: LocalDeclID::get(
1018 Reader, MF&: F,
1019 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1020 Result.Instance.push_back(Elt: Method);
1021 }
1022
1023 // Load factory methods
1024 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1025 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1026 F, LocalID: LocalDeclID::get(
1027 Reader, MF&: F,
1028 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1029 Result.Factory.push_back(Elt: Method);
1030 }
1031
1032 return Result;
1033}
1034
1035unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1036 return llvm::djbHash(Buffer: a);
1037}
1038
1039std::pair<unsigned, unsigned>
1040ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1041 return readULEBKeyDataLength(P&: d);
1042}
1043
1044ASTIdentifierLookupTraitBase::internal_key_type
1045ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1046 assert(n >= 2 && d[n-1] == '\0');
1047 return StringRef((const char*) d, n-1);
1048}
1049
1050/// Whether the given identifier is "interesting".
1051static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1052 bool IsModule) {
1053 bool IsInteresting =
1054 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1055 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1056 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1057 return II.hadMacroDefinition() || II.isPoisoned() ||
1058 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1059 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1060 II.getFETokenInfo());
1061}
1062
1063static bool readBit(unsigned &Bits) {
1064 bool Value = Bits & 0x1;
1065 Bits >>= 1;
1066 return Value;
1067}
1068
1069IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1070 using namespace llvm::support;
1071
1072 IdentifierID RawID =
1073 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1074 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1075}
1076
1077static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1078 bool IsModule) {
1079 if (!II.isFromAST()) {
1080 II.setIsFromAST();
1081 if (isInterestingIdentifier(Reader, II, IsModule))
1082 II.setChangedSinceDeserialization();
1083 }
1084}
1085
1086IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1087 const unsigned char* d,
1088 unsigned DataLen) {
1089 using namespace llvm::support;
1090
1091 IdentifierID RawID =
1092 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1093 bool IsInteresting = RawID & 0x01;
1094
1095 DataLen -= sizeof(IdentifierID);
1096
1097 // Wipe out the "is interesting" bit.
1098 RawID = RawID >> 1;
1099
1100 // Build the IdentifierInfo and link the identifier ID with it.
1101 IdentifierInfo *II = KnownII;
1102 if (!II) {
1103 II = &Reader.getIdentifierTable().getOwn(Name: k);
1104 KnownII = II;
1105 }
1106 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1107 markIdentifierFromAST(Reader, II&: *II, IsModule);
1108 Reader.markIdentifierUpToDate(II);
1109
1110 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1111 if (!IsInteresting) {
1112 // For uninteresting identifiers, there's nothing else to do. Just notify
1113 // the reader that we've finished loading this identifier.
1114 Reader.SetIdentifierInfo(ID, II);
1115 return II;
1116 }
1117
1118 unsigned ObjCOrBuiltinID =
1119 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1120 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1121 bool CPlusPlusOperatorKeyword = readBit(Bits);
1122 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1123 bool Poisoned = readBit(Bits);
1124 bool ExtensionToken = readBit(Bits);
1125 bool HasMacroDefinition = readBit(Bits);
1126
1127 assert(Bits == 0 && "Extra bits in the identifier?");
1128 DataLen -= sizeof(uint16_t) * 2;
1129
1130 // Set or check the various bits in the IdentifierInfo structure.
1131 // Token IDs are read-only.
1132 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1133 II->revertTokenIDToIdentifier();
1134 if (!F.isModule())
1135 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1136 assert(II->isExtensionToken() == ExtensionToken &&
1137 "Incorrect extension token flag");
1138 (void)ExtensionToken;
1139 if (Poisoned)
1140 II->setIsPoisoned(true);
1141 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1142 "Incorrect C++ operator keyword flag");
1143 (void)CPlusPlusOperatorKeyword;
1144
1145 // If this identifier has a macro definition, deserialize it or notify the
1146 // visitor the actual definition is in a different module.
1147 if (HasMacroDefinition) {
1148 uint32_t MacroDirectivesOffset =
1149 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1150 DataLen -= 4;
1151
1152 if (MacroDirectivesOffset)
1153 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1154 else
1155 hasMacroDefinitionInDependencies = true;
1156 }
1157
1158 Reader.SetIdentifierInfo(ID, II);
1159
1160 // Read all of the declarations visible at global scope with this
1161 // name.
1162 if (DataLen > 0) {
1163 SmallVector<GlobalDeclID, 4> DeclIDs;
1164 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1165 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1166 F, LocalID: LocalDeclID::get(
1167 Reader, MF&: F,
1168 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1169 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1170 }
1171
1172 return II;
1173}
1174
1175DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1176 : Kind(Name.getNameKind()) {
1177 switch (Kind) {
1178 case DeclarationName::Identifier:
1179 Data = (uint64_t)Name.getAsIdentifierInfo();
1180 break;
1181 case DeclarationName::ObjCZeroArgSelector:
1182 case DeclarationName::ObjCOneArgSelector:
1183 case DeclarationName::ObjCMultiArgSelector:
1184 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1185 break;
1186 case DeclarationName::CXXOperatorName:
1187 Data = Name.getCXXOverloadedOperator();
1188 break;
1189 case DeclarationName::CXXLiteralOperatorName:
1190 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1191 break;
1192 case DeclarationName::CXXDeductionGuideName:
1193 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1194 ->getDeclName().getAsIdentifierInfo();
1195 break;
1196 case DeclarationName::CXXConstructorName:
1197 case DeclarationName::CXXDestructorName:
1198 case DeclarationName::CXXConversionFunctionName:
1199 case DeclarationName::CXXUsingDirective:
1200 Data = 0;
1201 break;
1202 }
1203}
1204
1205unsigned DeclarationNameKey::getHash() const {
1206 llvm::FoldingSetNodeID ID;
1207 ID.AddInteger(I: Kind);
1208
1209 switch (Kind) {
1210 case DeclarationName::Identifier:
1211 case DeclarationName::CXXLiteralOperatorName:
1212 case DeclarationName::CXXDeductionGuideName:
1213 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1214 break;
1215 case DeclarationName::ObjCZeroArgSelector:
1216 case DeclarationName::ObjCOneArgSelector:
1217 case DeclarationName::ObjCMultiArgSelector:
1218 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1219 break;
1220 case DeclarationName::CXXOperatorName:
1221 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1222 break;
1223 case DeclarationName::CXXConstructorName:
1224 case DeclarationName::CXXDestructorName:
1225 case DeclarationName::CXXConversionFunctionName:
1226 case DeclarationName::CXXUsingDirective:
1227 break;
1228 }
1229
1230 return ID.computeStableHash();
1231}
1232
1233ModuleFile *
1234ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1235 using namespace llvm::support;
1236
1237 uint32_t ModuleFileID =
1238 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1239 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1240}
1241
1242std::pair<unsigned, unsigned>
1243ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1244 return readULEBKeyDataLength(P&: d);
1245}
1246
1247DeclarationNameKey
1248ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1249 using namespace llvm::support;
1250
1251 auto Kind = (DeclarationName::NameKind)*d++;
1252 uint64_t Data;
1253 switch (Kind) {
1254 case DeclarationName::Identifier:
1255 case DeclarationName::CXXLiteralOperatorName:
1256 case DeclarationName::CXXDeductionGuideName:
1257 Data = (uint64_t)Reader.getLocalIdentifier(
1258 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1259 break;
1260 case DeclarationName::ObjCZeroArgSelector:
1261 case DeclarationName::ObjCOneArgSelector:
1262 case DeclarationName::ObjCMultiArgSelector:
1263 Data = (uint64_t)Reader
1264 .getLocalSelector(
1265 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1266 .getAsOpaquePtr();
1267 break;
1268 case DeclarationName::CXXOperatorName:
1269 Data = *d++; // OverloadedOperatorKind
1270 break;
1271 case DeclarationName::CXXConstructorName:
1272 case DeclarationName::CXXDestructorName:
1273 case DeclarationName::CXXConversionFunctionName:
1274 case DeclarationName::CXXUsingDirective:
1275 Data = 0;
1276 break;
1277 }
1278
1279 return DeclarationNameKey(Kind, Data);
1280}
1281
1282ASTDeclContextNameLookupTrait::internal_key_type
1283ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1284 return ReadKeyBase(d);
1285}
1286
1287void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1288 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1289 using namespace llvm::support;
1290
1291 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1292 LocalDeclID ID = LocalDeclID::get(
1293 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1294 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1295 }
1296}
1297
1298void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1299 const unsigned char *d,
1300 unsigned DataLen,
1301 data_type_builder &Val) {
1302 ReadDataIntoImpl(d, DataLen, Val);
1303}
1304
1305ModuleLocalNameLookupTrait::hash_value_type
1306ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1307 llvm::FoldingSetNodeID ID;
1308 ID.AddInteger(I: Key.first.getHash());
1309 ID.AddInteger(I: Key.second);
1310 return ID.computeStableHash();
1311}
1312
1313ModuleLocalNameLookupTrait::internal_key_type
1314ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1315 DeclarationNameKey Name(Key.first);
1316
1317 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1318 if (!ModuleHash)
1319 return {Name, 0};
1320
1321 return {Name, *ModuleHash};
1322}
1323
1324ModuleLocalNameLookupTrait::internal_key_type
1325ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1326 DeclarationNameKey Name = ReadKeyBase(d);
1327 unsigned PrimaryModuleHash =
1328 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1329 return {Name, PrimaryModuleHash};
1330}
1331
1332void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1333 const unsigned char *d,
1334 unsigned DataLen,
1335 data_type_builder &Val) {
1336 ReadDataIntoImpl(d, DataLen, Val);
1337}
1338
1339ModuleFile *
1340LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1341 using namespace llvm::support;
1342
1343 uint32_t ModuleFileID =
1344 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1345 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1346}
1347
1348LazySpecializationInfoLookupTrait::internal_key_type
1349LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1350 using namespace llvm::support;
1351 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1352}
1353
1354std::pair<unsigned, unsigned>
1355LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1356 return readULEBKeyDataLength(P&: d);
1357}
1358
1359void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1360 const unsigned char *d,
1361 unsigned DataLen,
1362 data_type_builder &Val) {
1363 using namespace llvm::support;
1364
1365 for (unsigned NumDecls =
1366 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1367 NumDecls; --NumDecls) {
1368 LocalDeclID LocalID = LocalDeclID::get(
1369 Reader, MF&: F,
1370 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1371 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1372 }
1373}
1374
1375bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1376 BitstreamCursor &Cursor,
1377 uint64_t Offset,
1378 DeclContext *DC) {
1379 assert(Offset != 0);
1380
1381 SavedStreamPosition SavedPosition(Cursor);
1382 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1383 Error(Err: std::move(Err));
1384 return true;
1385 }
1386
1387 RecordData Record;
1388 StringRef Blob;
1389 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1390 if (!MaybeCode) {
1391 Error(Err: MaybeCode.takeError());
1392 return true;
1393 }
1394 unsigned Code = MaybeCode.get();
1395
1396 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1397 if (!MaybeRecCode) {
1398 Error(Err: MaybeRecCode.takeError());
1399 return true;
1400 }
1401 unsigned RecCode = MaybeRecCode.get();
1402 if (RecCode != DECL_CONTEXT_LEXICAL) {
1403 Error(Msg: "Expected lexical block");
1404 return true;
1405 }
1406
1407 assert(!isa<TranslationUnitDecl>(DC) &&
1408 "expected a TU_UPDATE_LEXICAL record for TU");
1409 // If we are handling a C++ class template instantiation, we can see multiple
1410 // lexical updates for the same record. It's important that we select only one
1411 // of them, so that field numbering works properly. Just pick the first one we
1412 // see.
1413 auto &Lex = LexicalDecls[DC];
1414 if (!Lex.first) {
1415 Lex = std::make_pair(
1416 x: &M, y: llvm::ArrayRef(
1417 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1418 Blob.size() / sizeof(DeclID)));
1419 }
1420 DC->setHasExternalLexicalStorage(true);
1421 return false;
1422}
1423
1424bool ASTReader::ReadVisibleDeclContextStorage(
1425 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1426 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1427 assert(Offset != 0);
1428
1429 SavedStreamPosition SavedPosition(Cursor);
1430 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1431 Error(Err: std::move(Err));
1432 return true;
1433 }
1434
1435 RecordData Record;
1436 StringRef Blob;
1437 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1438 if (!MaybeCode) {
1439 Error(Err: MaybeCode.takeError());
1440 return true;
1441 }
1442 unsigned Code = MaybeCode.get();
1443
1444 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1445 if (!MaybeRecCode) {
1446 Error(Err: MaybeRecCode.takeError());
1447 return true;
1448 }
1449 unsigned RecCode = MaybeRecCode.get();
1450 switch (VisibleKind) {
1451 case VisibleDeclContextStorageKind::GenerallyVisible:
1452 if (RecCode != DECL_CONTEXT_VISIBLE) {
1453 Error(Msg: "Expected visible lookup table block");
1454 return true;
1455 }
1456 break;
1457 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1458 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1459 Error(Msg: "Expected module local visible lookup table block");
1460 return true;
1461 }
1462 break;
1463 case VisibleDeclContextStorageKind::TULocalVisible:
1464 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1465 Error(Msg: "Expected TU local lookup table block");
1466 return true;
1467 }
1468 break;
1469 }
1470
1471 // We can't safely determine the primary context yet, so delay attaching the
1472 // lookup table until we're done with recursive deserialization.
1473 auto *Data = (const unsigned char*)Blob.data();
1474 switch (VisibleKind) {
1475 case VisibleDeclContextStorageKind::GenerallyVisible:
1476 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1477 break;
1478 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1479 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1480 break;
1481 case VisibleDeclContextStorageKind::TULocalVisible:
1482 if (M.Kind == MK_MainFile)
1483 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1484 break;
1485 }
1486 return false;
1487}
1488
1489void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1490 ModuleFile &M, bool IsPartial) {
1491 D = D->getCanonicalDecl();
1492 auto &SpecLookups =
1493 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1494 SpecLookups[D].Table.add(File: &M, Data,
1495 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1496}
1497
1498bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1499 uint64_t Offset, Decl *D, bool IsPartial) {
1500 assert(Offset != 0);
1501
1502 SavedStreamPosition SavedPosition(Cursor);
1503 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1504 Error(Err: std::move(Err));
1505 return true;
1506 }
1507
1508 RecordData Record;
1509 StringRef Blob;
1510 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1511 if (!MaybeCode) {
1512 Error(Err: MaybeCode.takeError());
1513 return true;
1514 }
1515 unsigned Code = MaybeCode.get();
1516
1517 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1518 if (!MaybeRecCode) {
1519 Error(Err: MaybeRecCode.takeError());
1520 return true;
1521 }
1522 unsigned RecCode = MaybeRecCode.get();
1523 if (RecCode != DECL_SPECIALIZATIONS &&
1524 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1525 Error(Msg: "Expected decl specs block");
1526 return true;
1527 }
1528
1529 auto *Data = (const unsigned char *)Blob.data();
1530 AddSpecializations(D, Data, M, IsPartial);
1531 return false;
1532}
1533
1534void ASTReader::Error(StringRef Msg) const {
1535 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1536 if (PP.getLangOpts().Modules &&
1537 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1538 Diag(DiagID: diag::note_module_cache_path)
1539 << PP.getHeaderSearchInfo().getModuleCachePath();
1540 }
1541}
1542
1543void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1544 StringRef Arg3) const {
1545 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1546}
1547
1548namespace {
1549struct AlreadyReportedDiagnosticError
1550 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1551 static char ID;
1552
1553 void log(raw_ostream &OS) const override {
1554 llvm_unreachable("reporting an already-reported diagnostic error");
1555 }
1556
1557 std::error_code convertToErrorCode() const override {
1558 return llvm::inconvertibleErrorCode();
1559 }
1560};
1561
1562char AlreadyReportedDiagnosticError::ID = 0;
1563} // namespace
1564
1565void ASTReader::Error(llvm::Error &&Err) const {
1566 handleAllErrors(
1567 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1568 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1569}
1570
1571//===----------------------------------------------------------------------===//
1572// Source Manager Deserialization
1573//===----------------------------------------------------------------------===//
1574
1575/// Read the line table in the source manager block.
1576void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1577 unsigned Idx = 0;
1578 LineTableInfo &LineTable = SourceMgr.getLineTable();
1579
1580 // Parse the file names
1581 std::map<int, int> FileIDs;
1582 FileIDs[-1] = -1; // For unspecified filenames.
1583 for (unsigned I = 0; Record[Idx]; ++I) {
1584 // Extract the file name
1585 auto Filename = ReadPath(F, Record, Idx);
1586 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1587 }
1588 ++Idx;
1589
1590 // Parse the line entries
1591 std::vector<LineEntry> Entries;
1592 while (Idx < Record.size()) {
1593 FileID FID = ReadFileID(F, Record, Idx);
1594
1595 // Extract the line entries
1596 unsigned NumEntries = Record[Idx++];
1597 assert(NumEntries && "no line entries for file ID");
1598 Entries.clear();
1599 Entries.reserve(n: NumEntries);
1600 for (unsigned I = 0; I != NumEntries; ++I) {
1601 unsigned FileOffset = Record[Idx++];
1602 unsigned LineNo = Record[Idx++];
1603 int FilenameID = FileIDs[Record[Idx++]];
1604 SrcMgr::CharacteristicKind FileKind
1605 = (SrcMgr::CharacteristicKind)Record[Idx++];
1606 unsigned IncludeOffset = Record[Idx++];
1607 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1608 FileKind, IncludeOffset));
1609 }
1610 LineTable.AddEntry(FID, Entries);
1611 }
1612}
1613
1614/// Read a source manager block
1615llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1616 using namespace SrcMgr;
1617
1618 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1619
1620 // Set the source-location entry cursor to the current position in
1621 // the stream. This cursor will be used to read the contents of the
1622 // source manager block initially, and then lazily read
1623 // source-location entries as needed.
1624 SLocEntryCursor = F.Stream;
1625
1626 // The stream itself is going to skip over the source manager block.
1627 if (llvm::Error Err = F.Stream.SkipBlock())
1628 return Err;
1629
1630 // Enter the source manager block.
1631 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1632 return Err;
1633 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1634
1635 RecordData Record;
1636 while (true) {
1637 Expected<llvm::BitstreamEntry> MaybeE =
1638 SLocEntryCursor.advanceSkippingSubblocks();
1639 if (!MaybeE)
1640 return MaybeE.takeError();
1641 llvm::BitstreamEntry E = MaybeE.get();
1642
1643 switch (E.Kind) {
1644 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1645 case llvm::BitstreamEntry::Error:
1646 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1647 Fmt: "malformed block record in AST file");
1648 case llvm::BitstreamEntry::EndBlock:
1649 return llvm::Error::success();
1650 case llvm::BitstreamEntry::Record:
1651 // The interesting case.
1652 break;
1653 }
1654
1655 // Read a record.
1656 Record.clear();
1657 StringRef Blob;
1658 Expected<unsigned> MaybeRecord =
1659 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1660 if (!MaybeRecord)
1661 return MaybeRecord.takeError();
1662 switch (MaybeRecord.get()) {
1663 default: // Default behavior: ignore.
1664 break;
1665
1666 case SM_SLOC_FILE_ENTRY:
1667 case SM_SLOC_BUFFER_ENTRY:
1668 case SM_SLOC_EXPANSION_ENTRY:
1669 // Once we hit one of the source location entries, we're done.
1670 return llvm::Error::success();
1671 }
1672 }
1673}
1674
1675llvm::Expected<SourceLocation::UIntTy>
1676ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1677 BitstreamCursor &Cursor = F->SLocEntryCursor;
1678 SavedStreamPosition SavedPosition(Cursor);
1679 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1680 F->SLocEntryOffsets[Index]))
1681 return std::move(Err);
1682
1683 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1684 if (!MaybeEntry)
1685 return MaybeEntry.takeError();
1686
1687 llvm::BitstreamEntry Entry = MaybeEntry.get();
1688 if (Entry.Kind != llvm::BitstreamEntry::Record)
1689 return llvm::createStringError(
1690 EC: std::errc::illegal_byte_sequence,
1691 Fmt: "incorrectly-formatted source location entry in AST file");
1692
1693 RecordData Record;
1694 StringRef Blob;
1695 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1696 if (!MaybeSLOC)
1697 return MaybeSLOC.takeError();
1698
1699 switch (MaybeSLOC.get()) {
1700 default:
1701 return llvm::createStringError(
1702 EC: std::errc::illegal_byte_sequence,
1703 Fmt: "incorrectly-formatted source location entry in AST file");
1704 case SM_SLOC_FILE_ENTRY:
1705 case SM_SLOC_BUFFER_ENTRY:
1706 case SM_SLOC_EXPANSION_ENTRY:
1707 return F->SLocEntryBaseOffset + Record[0];
1708 }
1709}
1710
1711int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1712 auto SLocMapI =
1713 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1714 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1715 "Corrupted global sloc offset map");
1716 ModuleFile *F = SLocMapI->second;
1717
1718 bool Invalid = false;
1719
1720 auto It = llvm::upper_bound(
1721 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1722 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1723 int ID = F->SLocEntryBaseID + LocalIndex;
1724 std::size_t Index = -ID - 2;
1725 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1726 assert(!SourceMgr.SLocEntryLoaded[Index]);
1727 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1728 if (!MaybeEntryOffset) {
1729 Error(Err: MaybeEntryOffset.takeError());
1730 Invalid = true;
1731 return true;
1732 }
1733 SourceMgr.LoadedSLocEntryTable[Index] =
1734 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1735 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1736 }
1737 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1738 });
1739
1740 if (Invalid)
1741 return 0;
1742
1743 // The iterator points to the first entry with start offset greater than the
1744 // offset of interest. The previous entry must contain the offset of interest.
1745 return F->SLocEntryBaseID + *std::prev(x: It);
1746}
1747
1748bool ASTReader::ReadSLocEntry(int ID) {
1749 if (ID == 0)
1750 return false;
1751
1752 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1753 Error(Msg: "source location entry ID out-of-range for AST file");
1754 return true;
1755 }
1756
1757 // Local helper to read the (possibly-compressed) buffer data following the
1758 // entry record.
1759 auto ReadBuffer = [this](
1760 BitstreamCursor &SLocEntryCursor,
1761 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1762 RecordData Record;
1763 StringRef Blob;
1764 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1765 if (!MaybeCode) {
1766 Error(Err: MaybeCode.takeError());
1767 return nullptr;
1768 }
1769 unsigned Code = MaybeCode.get();
1770
1771 Expected<unsigned> MaybeRecCode =
1772 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1773 if (!MaybeRecCode) {
1774 Error(Err: MaybeRecCode.takeError());
1775 return nullptr;
1776 }
1777 unsigned RecCode = MaybeRecCode.get();
1778
1779 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1780 // Inspect the first byte to differentiate zlib (\x78) and zstd
1781 // (little-endian 0xFD2FB528).
1782 const llvm::compression::Format F =
1783 Blob.size() > 0 && Blob.data()[0] == 0x78
1784 ? llvm::compression::Format::Zlib
1785 : llvm::compression::Format::Zstd;
1786 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1787 Error(Msg: Reason);
1788 return nullptr;
1789 }
1790 SmallVector<uint8_t, 0> Decompressed;
1791 if (llvm::Error E = llvm::compression::decompress(
1792 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1793 Error(Msg: "could not decompress embedded file contents: " +
1794 llvm::toString(E: std::move(E)));
1795 return nullptr;
1796 }
1797 return llvm::MemoryBuffer::getMemBufferCopy(
1798 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1799 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1800 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1801 } else {
1802 Error(Msg: "AST record has invalid code");
1803 return nullptr;
1804 }
1805 };
1806
1807 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1808 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1809 BitNo: F->SLocEntryOffsetsBase +
1810 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1811 Error(Err: std::move(Err));
1812 return true;
1813 }
1814
1815 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1816 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1817
1818 ++NumSLocEntriesRead;
1819 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1820 if (!MaybeEntry) {
1821 Error(Err: MaybeEntry.takeError());
1822 return true;
1823 }
1824 llvm::BitstreamEntry Entry = MaybeEntry.get();
1825
1826 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1827 Error(Msg: "incorrectly-formatted source location entry in AST file");
1828 return true;
1829 }
1830
1831 RecordData Record;
1832 StringRef Blob;
1833 Expected<unsigned> MaybeSLOC =
1834 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1835 if (!MaybeSLOC) {
1836 Error(Err: MaybeSLOC.takeError());
1837 return true;
1838 }
1839 switch (MaybeSLOC.get()) {
1840 default:
1841 Error(Msg: "incorrectly-formatted source location entry in AST file");
1842 return true;
1843
1844 case SM_SLOC_FILE_ENTRY: {
1845 // We will detect whether a file changed and return 'Failure' for it, but
1846 // we will also try to fail gracefully by setting up the SLocEntry.
1847 unsigned InputID = Record[4];
1848 InputFile IF = getInputFile(F&: *F, ID: InputID);
1849 OptionalFileEntryRef File = IF.getFile();
1850 bool OverriddenBuffer = IF.isOverridden();
1851
1852 // Note that we only check if a File was returned. If it was out-of-date
1853 // we have complained but we will continue creating a FileID to recover
1854 // gracefully.
1855 if (!File)
1856 return true;
1857
1858 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1859 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1860 // This is the module's main file.
1861 IncludeLoc = getImportLocation(F);
1862 }
1863 SrcMgr::CharacteristicKind
1864 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1865 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1866 LoadedOffset: BaseOffset + Record[0]);
1867 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1868 FileInfo.NumCreatedFIDs = Record[5];
1869 if (Record[3])
1870 FileInfo.setHasLineDirectives();
1871
1872 unsigned NumFileDecls = Record[7];
1873 if (NumFileDecls && ContextObj) {
1874 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1875 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1876 FileDeclIDs[FID] =
1877 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1878 }
1879
1880 const SrcMgr::ContentCache &ContentCache =
1881 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
1882 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1883 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1884 !ContentCache.getBufferIfLoaded()) {
1885 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1886 if (!Buffer)
1887 return true;
1888 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
1889 }
1890
1891 break;
1892 }
1893
1894 case SM_SLOC_BUFFER_ENTRY: {
1895 const char *Name = Blob.data();
1896 unsigned Offset = Record[0];
1897 SrcMgr::CharacteristicKind
1898 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1899 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1900 if (IncludeLoc.isInvalid() && F->isModule()) {
1901 IncludeLoc = getImportLocation(F);
1902 }
1903
1904 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1905 if (!Buffer)
1906 return true;
1907 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
1908 LoadedOffset: BaseOffset + Offset, IncludeLoc);
1909 if (Record[3]) {
1910 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1911 FileInfo.setHasLineDirectives();
1912 }
1913 break;
1914 }
1915
1916 case SM_SLOC_EXPANSION_ENTRY: {
1917 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1918 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
1919 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
1920 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
1921 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
1922 LoadedOffset: BaseOffset + Record[0]);
1923 break;
1924 }
1925 }
1926
1927 return false;
1928}
1929
1930std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1931 if (ID == 0)
1932 return std::make_pair(x: SourceLocation(), y: "");
1933
1934 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1935 Error(Msg: "source location entry ID out-of-range for AST file");
1936 return std::make_pair(x: SourceLocation(), y: "");
1937 }
1938
1939 // Find which module file this entry lands in.
1940 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
1941 if (!M->isModule())
1942 return std::make_pair(x: SourceLocation(), y: "");
1943
1944 // FIXME: Can we map this down to a particular submodule? That would be
1945 // ideal.
1946 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
1947}
1948
1949/// Find the location where the module F is imported.
1950SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1951 if (F->ImportLoc.isValid())
1952 return F->ImportLoc;
1953
1954 // Otherwise we have a PCH. It's considered to be "imported" at the first
1955 // location of its includer.
1956 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1957 // Main file is the importer.
1958 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1959 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
1960 }
1961 return F->ImportedBy[0]->FirstLoc;
1962}
1963
1964/// Enter a subblock of the specified BlockID with the specified cursor. Read
1965/// the abbreviations that are at the top of the block and then leave the cursor
1966/// pointing into the block.
1967llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1968 unsigned BlockID,
1969 uint64_t *StartOfBlockOffset) {
1970 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1971 return Err;
1972
1973 if (StartOfBlockOffset)
1974 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1975
1976 while (true) {
1977 uint64_t Offset = Cursor.GetCurrentBitNo();
1978 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1979 if (!MaybeCode)
1980 return MaybeCode.takeError();
1981 unsigned Code = MaybeCode.get();
1982
1983 // We expect all abbrevs to be at the start of the block.
1984 if (Code != llvm::bitc::DEFINE_ABBREV) {
1985 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
1986 return Err;
1987 return llvm::Error::success();
1988 }
1989 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1990 return Err;
1991 }
1992}
1993
1994Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
1995 unsigned &Idx) {
1996 Token Tok;
1997 Tok.startToken();
1998 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
1999 Tok.setKind((tok::TokenKind)Record[Idx++]);
2000 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2001
2002 if (Tok.isAnnotation()) {
2003 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2004 switch (Tok.getKind()) {
2005 case tok::annot_pragma_loop_hint: {
2006 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2007 Info->PragmaName = ReadToken(M, Record, Idx);
2008 Info->Option = ReadToken(M, Record, Idx);
2009 unsigned NumTokens = Record[Idx++];
2010 SmallVector<Token, 4> Toks;
2011 Toks.reserve(N: NumTokens);
2012 for (unsigned I = 0; I < NumTokens; ++I)
2013 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2014 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2015 Tok.setAnnotationValue(static_cast<void *>(Info));
2016 break;
2017 }
2018 case tok::annot_pragma_pack: {
2019 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2020 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2021 auto SlotLabel = ReadString(Record, Idx);
2022 Info->SlotLabel =
2023 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2024 Info->Alignment = ReadToken(M, Record, Idx);
2025 Tok.setAnnotationValue(static_cast<void *>(Info));
2026 break;
2027 }
2028 // Some annotation tokens do not use the PtrData field.
2029 case tok::annot_pragma_openmp:
2030 case tok::annot_pragma_openmp_end:
2031 case tok::annot_pragma_unused:
2032 case tok::annot_pragma_openacc:
2033 case tok::annot_pragma_openacc_end:
2034 case tok::annot_repl_input_end:
2035 break;
2036 default:
2037 llvm_unreachable("missing deserialization code for annotation token");
2038 }
2039 } else {
2040 Tok.setLength(Record[Idx++]);
2041 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2042 Tok.setIdentifierInfo(II);
2043 }
2044 return Tok;
2045}
2046
2047MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2048 BitstreamCursor &Stream = F.MacroCursor;
2049
2050 // Keep track of where we are in the stream, then jump back there
2051 // after reading this macro.
2052 SavedStreamPosition SavedPosition(Stream);
2053
2054 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2055 // FIXME this drops errors on the floor.
2056 consumeError(Err: std::move(Err));
2057 return nullptr;
2058 }
2059 RecordData Record;
2060 SmallVector<IdentifierInfo*, 16> MacroParams;
2061 MacroInfo *Macro = nullptr;
2062 llvm::MutableArrayRef<Token> MacroTokens;
2063
2064 while (true) {
2065 // Advance to the next record, but if we get to the end of the block, don't
2066 // pop it (removing all the abbreviations from the cursor) since we want to
2067 // be able to reseek within the block and read entries.
2068 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2069 Expected<llvm::BitstreamEntry> MaybeEntry =
2070 Stream.advanceSkippingSubblocks(Flags);
2071 if (!MaybeEntry) {
2072 Error(Err: MaybeEntry.takeError());
2073 return Macro;
2074 }
2075 llvm::BitstreamEntry Entry = MaybeEntry.get();
2076
2077 switch (Entry.Kind) {
2078 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2079 case llvm::BitstreamEntry::Error:
2080 Error(Msg: "malformed block record in AST file");
2081 return Macro;
2082 case llvm::BitstreamEntry::EndBlock:
2083 return Macro;
2084 case llvm::BitstreamEntry::Record:
2085 // The interesting case.
2086 break;
2087 }
2088
2089 // Read a record.
2090 Record.clear();
2091 PreprocessorRecordTypes RecType;
2092 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2093 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2094 else {
2095 Error(Err: MaybeRecType.takeError());
2096 return Macro;
2097 }
2098 switch (RecType) {
2099 case PP_MODULE_MACRO:
2100 case PP_MACRO_DIRECTIVE_HISTORY:
2101 return Macro;
2102
2103 case PP_MACRO_OBJECT_LIKE:
2104 case PP_MACRO_FUNCTION_LIKE: {
2105 // If we already have a macro, that means that we've hit the end
2106 // of the definition of the macro we were looking for. We're
2107 // done.
2108 if (Macro)
2109 return Macro;
2110
2111 unsigned NextIndex = 1; // Skip identifier ID.
2112 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2113 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2114 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2115 MI->setIsUsed(Record[NextIndex++]);
2116 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2117 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2118 PPAllocator&: PP.getPreprocessorAllocator());
2119 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2120 // Decode function-like macro info.
2121 bool isC99VarArgs = Record[NextIndex++];
2122 bool isGNUVarArgs = Record[NextIndex++];
2123 bool hasCommaPasting = Record[NextIndex++];
2124 MacroParams.clear();
2125 unsigned NumArgs = Record[NextIndex++];
2126 for (unsigned i = 0; i != NumArgs; ++i)
2127 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2128
2129 // Install function-like macro info.
2130 MI->setIsFunctionLike();
2131 if (isC99VarArgs) MI->setIsC99Varargs();
2132 if (isGNUVarArgs) MI->setIsGNUVarargs();
2133 if (hasCommaPasting) MI->setHasCommaPasting();
2134 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2135 }
2136
2137 // Remember that we saw this macro last so that we add the tokens that
2138 // form its body to it.
2139 Macro = MI;
2140
2141 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2142 Record[NextIndex]) {
2143 // We have a macro definition. Register the association
2144 PreprocessedEntityID
2145 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2146 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2147 PreprocessingRecord::PPEntityID PPID =
2148 PPRec.getPPEntityID(Index: GlobalID - 1, /*isLoaded=*/true);
2149 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2150 Val: PPRec.getPreprocessedEntity(PPID));
2151 if (PPDef)
2152 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2153 }
2154
2155 ++NumMacrosRead;
2156 break;
2157 }
2158
2159 case PP_TOKEN: {
2160 // If we see a TOKEN before a PP_MACRO_*, then the file is
2161 // erroneous, just pretend we didn't see this.
2162 if (!Macro) break;
2163 if (MacroTokens.empty()) {
2164 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2165 return Macro;
2166 }
2167
2168 unsigned Idx = 0;
2169 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2170 MacroTokens = MacroTokens.drop_front();
2171 break;
2172 }
2173 }
2174 }
2175}
2176
2177PreprocessedEntityID
2178ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2179 unsigned LocalID) const {
2180 if (!M.ModuleOffsetMap.empty())
2181 ReadModuleOffsetMap(F&: M);
2182
2183 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
2184 I = M.PreprocessedEntityRemap.find(K: LocalID - NUM_PREDEF_PP_ENTITY_IDS);
2185 assert(I != M.PreprocessedEntityRemap.end()
2186 && "Invalid index into preprocessed entity index remap");
2187
2188 return LocalID + I->second;
2189}
2190
2191OptionalFileEntryRef
2192HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2193 FileManager &FileMgr = Reader.getFileManager();
2194 if (!Key.Imported)
2195 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2196
2197 auto Resolved =
2198 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2199 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2200}
2201
2202unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2203 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2204 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2205 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2206 return llvm::xxh3_64bits(data: buf);
2207}
2208
2209HeaderFileInfoTrait::internal_key_type
2210HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2211 internal_key_type ikey = {.Size: ekey.getSize(),
2212 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2213 .Filename: ekey.getName(), /*Imported*/ false};
2214 return ikey;
2215}
2216
2217bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2218 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2219 return false;
2220
2221 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2222 return true;
2223
2224 // Determine whether the actual files are equivalent.
2225 OptionalFileEntryRef FEA = getFile(Key: a);
2226 OptionalFileEntryRef FEB = getFile(Key: b);
2227 return FEA && FEA == FEB;
2228}
2229
2230std::pair<unsigned, unsigned>
2231HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2232 return readULEBKeyDataLength(P&: d);
2233}
2234
2235HeaderFileInfoTrait::internal_key_type
2236HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2237 using namespace llvm::support;
2238
2239 internal_key_type ikey;
2240 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2241 ikey.ModTime =
2242 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2243 ikey.Filename = (const char *)d;
2244 ikey.Imported = true;
2245 return ikey;
2246}
2247
2248HeaderFileInfoTrait::data_type
2249HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2250 unsigned DataLen) {
2251 using namespace llvm::support;
2252
2253 const unsigned char *End = d + DataLen;
2254 HeaderFileInfo HFI;
2255 unsigned Flags = *d++;
2256
2257 OptionalFileEntryRef FE;
2258 bool Included = (Flags >> 6) & 0x01;
2259 if (Included)
2260 if ((FE = getFile(Key: key)))
2261 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2262 // deserialize this header file info again.
2263 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2264
2265 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2266 HFI.isImport |= (Flags >> 5) & 0x01;
2267 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2268 HFI.DirInfo = (Flags >> 1) & 0x07;
2269 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2270 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2271
2272 assert((End - d) % 4 == 0 &&
2273 "Wrong data length in HeaderFileInfo deserialization");
2274 while (d != End) {
2275 uint32_t LocalSMID =
2276 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2277 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2278 LocalSMID >>= 3;
2279
2280 // This header is part of a module. Associate it with the module to enable
2281 // implicit module import.
2282 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2283 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2284 ModuleMap &ModMap =
2285 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2286
2287 if (FE || (FE = getFile(Key: key))) {
2288 // FIXME: NameAsWritten
2289 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2290 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2291 }
2292 HFI.mergeModuleMembership(Role: HeaderRole);
2293 }
2294
2295 // This HeaderFileInfo was externally loaded.
2296 HFI.External = true;
2297 HFI.IsValid = true;
2298 return HFI;
2299}
2300
2301void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2302 uint32_t MacroDirectivesOffset) {
2303 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2304 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2305}
2306
2307void ASTReader::ReadDefinedMacros() {
2308 // Note that we are loading defined macros.
2309 Deserializing Macros(this);
2310
2311 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2312 BitstreamCursor &MacroCursor = I.MacroCursor;
2313
2314 // If there was no preprocessor block, skip this file.
2315 if (MacroCursor.getBitcodeBytes().empty())
2316 continue;
2317
2318 BitstreamCursor Cursor = MacroCursor;
2319 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2320 Error(Err: std::move(Err));
2321 return;
2322 }
2323
2324 RecordData Record;
2325 while (true) {
2326 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2327 if (!MaybeE) {
2328 Error(Err: MaybeE.takeError());
2329 return;
2330 }
2331 llvm::BitstreamEntry E = MaybeE.get();
2332
2333 switch (E.Kind) {
2334 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2335 case llvm::BitstreamEntry::Error:
2336 Error(Msg: "malformed block record in AST file");
2337 return;
2338 case llvm::BitstreamEntry::EndBlock:
2339 goto NextCursor;
2340
2341 case llvm::BitstreamEntry::Record: {
2342 Record.clear();
2343 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2344 if (!MaybeRecord) {
2345 Error(Err: MaybeRecord.takeError());
2346 return;
2347 }
2348 switch (MaybeRecord.get()) {
2349 default: // Default behavior: ignore.
2350 break;
2351
2352 case PP_MACRO_OBJECT_LIKE:
2353 case PP_MACRO_FUNCTION_LIKE: {
2354 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2355 if (II->isOutOfDate())
2356 updateOutOfDateIdentifier(II: *II);
2357 break;
2358 }
2359
2360 case PP_TOKEN:
2361 // Ignore tokens.
2362 break;
2363 }
2364 break;
2365 }
2366 }
2367 }
2368 NextCursor: ;
2369 }
2370}
2371
2372namespace {
2373
2374 /// Visitor class used to look up identifirs in an AST file.
2375 class IdentifierLookupVisitor {
2376 StringRef Name;
2377 unsigned NameHash;
2378 unsigned PriorGeneration;
2379 unsigned &NumIdentifierLookups;
2380 unsigned &NumIdentifierLookupHits;
2381 IdentifierInfo *Found = nullptr;
2382
2383 public:
2384 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2385 unsigned &NumIdentifierLookups,
2386 unsigned &NumIdentifierLookupHits)
2387 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2388 PriorGeneration(PriorGeneration),
2389 NumIdentifierLookups(NumIdentifierLookups),
2390 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2391
2392 bool operator()(ModuleFile &M) {
2393 // If we've already searched this module file, skip it now.
2394 if (M.Generation <= PriorGeneration)
2395 return true;
2396
2397 ASTIdentifierLookupTable *IdTable
2398 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2399 if (!IdTable)
2400 return false;
2401
2402 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2403 Found);
2404 ++NumIdentifierLookups;
2405 ASTIdentifierLookupTable::iterator Pos =
2406 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2407 if (Pos == IdTable->end())
2408 return false;
2409
2410 // Dereferencing the iterator has the effect of building the
2411 // IdentifierInfo node and populating it with the various
2412 // declarations it needs.
2413 ++NumIdentifierLookupHits;
2414 Found = *Pos;
2415 if (Trait.hasMoreInformationInDependencies()) {
2416 // Look for the identifier in extra modules as they contain more info.
2417 return false;
2418 }
2419 return true;
2420 }
2421
2422 // Retrieve the identifier info found within the module
2423 // files.
2424 IdentifierInfo *getIdentifierInfo() const { return Found; }
2425 };
2426
2427} // namespace
2428
2429void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2430 // Note that we are loading an identifier.
2431 Deserializing AnIdentifier(this);
2432
2433 unsigned PriorGeneration = 0;
2434 if (getContext().getLangOpts().Modules)
2435 PriorGeneration = IdentifierGeneration[&II];
2436
2437 // If there is a global index, look there first to determine which modules
2438 // provably do not have any results for this identifier.
2439 GlobalModuleIndex::HitSet Hits;
2440 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2441 if (!loadGlobalIndex()) {
2442 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2443 HitsPtr = &Hits;
2444 }
2445 }
2446
2447 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2448 NumIdentifierLookups,
2449 NumIdentifierLookupHits);
2450 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2451 markIdentifierUpToDate(II: &II);
2452}
2453
2454void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2455 if (!II)
2456 return;
2457
2458 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2459
2460 // Update the generation for this identifier.
2461 if (getContext().getLangOpts().Modules)
2462 IdentifierGeneration[II] = getGeneration();
2463}
2464
2465void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2466 const PendingMacroInfo &PMInfo) {
2467 ModuleFile &M = *PMInfo.M;
2468
2469 BitstreamCursor &Cursor = M.MacroCursor;
2470 SavedStreamPosition SavedPosition(Cursor);
2471 if (llvm::Error Err =
2472 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2473 Error(Err: std::move(Err));
2474 return;
2475 }
2476
2477 struct ModuleMacroRecord {
2478 SubmoduleID SubModID;
2479 MacroInfo *MI;
2480 SmallVector<SubmoduleID, 8> Overrides;
2481 };
2482 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2483
2484 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2485 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2486 // macro histroy.
2487 RecordData Record;
2488 while (true) {
2489 Expected<llvm::BitstreamEntry> MaybeEntry =
2490 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2491 if (!MaybeEntry) {
2492 Error(Err: MaybeEntry.takeError());
2493 return;
2494 }
2495 llvm::BitstreamEntry Entry = MaybeEntry.get();
2496
2497 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2498 Error(Msg: "malformed block record in AST file");
2499 return;
2500 }
2501
2502 Record.clear();
2503 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2504 if (!MaybePP) {
2505 Error(Err: MaybePP.takeError());
2506 return;
2507 }
2508 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2509 case PP_MACRO_DIRECTIVE_HISTORY:
2510 break;
2511
2512 case PP_MODULE_MACRO: {
2513 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2514 auto &Info = ModuleMacros.back();
2515 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[0]);
2516 Info.MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[1]));
2517 for (int I = 2, N = Record.size(); I != N; ++I)
2518 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2519 continue;
2520 }
2521
2522 default:
2523 Error(Msg: "malformed block record in AST file");
2524 return;
2525 }
2526
2527 // We found the macro directive history; that's the last record
2528 // for this macro.
2529 break;
2530 }
2531
2532 // Module macros are listed in reverse dependency order.
2533 {
2534 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2535 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2536 for (auto &MMR : ModuleMacros) {
2537 Overrides.clear();
2538 for (unsigned ModID : MMR.Overrides) {
2539 Module *Mod = getSubmodule(GlobalID: ModID);
2540 auto *Macro = PP.getModuleMacro(Mod, II);
2541 assert(Macro && "missing definition for overridden macro");
2542 Overrides.push_back(Elt: Macro);
2543 }
2544
2545 bool Inserted = false;
2546 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2547 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2548 }
2549 }
2550
2551 // Don't read the directive history for a module; we don't have anywhere
2552 // to put it.
2553 if (M.isModule())
2554 return;
2555
2556 // Deserialize the macro directives history in reverse source-order.
2557 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2558 unsigned Idx = 0, N = Record.size();
2559 while (Idx < N) {
2560 MacroDirective *MD = nullptr;
2561 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2562 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2563 switch (K) {
2564 case MacroDirective::MD_Define: {
2565 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2566 MD = PP.AllocateDefMacroDirective(MI, Loc);
2567 break;
2568 }
2569 case MacroDirective::MD_Undefine:
2570 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2571 break;
2572 case MacroDirective::MD_Visibility:
2573 bool isPublic = Record[Idx++];
2574 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2575 break;
2576 }
2577
2578 if (!Latest)
2579 Latest = MD;
2580 if (Earliest)
2581 Earliest->setPrevious(MD);
2582 Earliest = MD;
2583 }
2584
2585 if (Latest)
2586 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2587}
2588
2589bool ASTReader::shouldDisableValidationForFile(
2590 const serialization::ModuleFile &M) const {
2591 if (DisableValidationKind == DisableValidationForModuleKind::None)
2592 return false;
2593
2594 // If a PCH is loaded and validation is disabled for PCH then disable
2595 // validation for the PCH and the modules it loads.
2596 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2597
2598 switch (K) {
2599 case MK_MainFile:
2600 case MK_Preamble:
2601 case MK_PCH:
2602 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2603 case MK_ImplicitModule:
2604 case MK_ExplicitModule:
2605 case MK_PrebuiltModule:
2606 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2607 }
2608
2609 return false;
2610}
2611
2612static std::pair<StringRef, StringRef>
2613getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2614 const StringRef InputBlob) {
2615 uint16_t AsRequestedLength = Record[7];
2616 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2617 InputBlob.substr(Start: AsRequestedLength)};
2618}
2619
2620InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2621 // If this ID is bogus, just return an empty input file.
2622 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2623 return InputFileInfo();
2624
2625 // If we've already loaded this input file, return it.
2626 if (F.InputFileInfosLoaded[ID - 1].isValid())
2627 return F.InputFileInfosLoaded[ID - 1];
2628
2629 // Go find this input file.
2630 BitstreamCursor &Cursor = F.InputFilesCursor;
2631 SavedStreamPosition SavedPosition(Cursor);
2632 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2633 F.InputFileOffsets[ID - 1])) {
2634 // FIXME this drops errors on the floor.
2635 consumeError(Err: std::move(Err));
2636 }
2637
2638 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2639 if (!MaybeCode) {
2640 // FIXME this drops errors on the floor.
2641 consumeError(Err: MaybeCode.takeError());
2642 }
2643 unsigned Code = MaybeCode.get();
2644 RecordData Record;
2645 StringRef Blob;
2646
2647 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2648 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2649 "invalid record type for input file");
2650 else {
2651 // FIXME this drops errors on the floor.
2652 consumeError(Err: Maybe.takeError());
2653 }
2654
2655 assert(Record[0] == ID && "Bogus stored ID or offset");
2656 InputFileInfo R;
2657 R.StoredSize = static_cast<off_t>(Record[1]);
2658 R.StoredTime = static_cast<time_t>(Record[2]);
2659 R.Overridden = static_cast<bool>(Record[3]);
2660 R.Transient = static_cast<bool>(Record[4]);
2661 R.TopLevel = static_cast<bool>(Record[5]);
2662 R.ModuleMap = static_cast<bool>(Record[6]);
2663 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2664 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2665 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2666 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2667 ? UnresolvedFilenameAsRequested
2668 : UnresolvedFilename;
2669
2670 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2671 if (!MaybeEntry) // FIXME this drops errors on the floor.
2672 consumeError(Err: MaybeEntry.takeError());
2673 llvm::BitstreamEntry Entry = MaybeEntry.get();
2674 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2675 "expected record type for input file hash");
2676
2677 Record.clear();
2678 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2679 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2680 "invalid record type for input file hash");
2681 else {
2682 // FIXME this drops errors on the floor.
2683 consumeError(Err: Maybe.takeError());
2684 }
2685 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2686 static_cast<uint64_t>(Record[0]);
2687
2688 // Note that we've loaded this input file info.
2689 F.InputFileInfosLoaded[ID - 1] = R;
2690 return R;
2691}
2692
2693static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2694InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2695 // If this ID is bogus, just return an empty input file.
2696 if (ID == 0 || ID > F.InputFilesLoaded.size())
2697 return InputFile();
2698
2699 // If we've already loaded this input file, return it.
2700 if (F.InputFilesLoaded[ID-1].getFile())
2701 return F.InputFilesLoaded[ID-1];
2702
2703 if (F.InputFilesLoaded[ID-1].isNotFound())
2704 return InputFile();
2705
2706 // Go find this input file.
2707 BitstreamCursor &Cursor = F.InputFilesCursor;
2708 SavedStreamPosition SavedPosition(Cursor);
2709 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2710 F.InputFileOffsets[ID - 1])) {
2711 // FIXME this drops errors on the floor.
2712 consumeError(Err: std::move(Err));
2713 }
2714
2715 InputFileInfo FI = getInputFileInfo(F, ID);
2716 off_t StoredSize = FI.StoredSize;
2717 time_t StoredTime = FI.StoredTime;
2718 bool Overridden = FI.Overridden;
2719 bool Transient = FI.Transient;
2720 auto Filename =
2721 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2722 uint64_t StoredContentHash = FI.ContentHash;
2723
2724 // For standard C++ modules, we don't need to check the inputs.
2725 bool SkipChecks = F.StandardCXXModule;
2726
2727 const HeaderSearchOptions &HSOpts =
2728 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2729
2730 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2731 // modules.
2732 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2733 SkipChecks = false;
2734 Overridden = false;
2735 }
2736
2737 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2738
2739 // For an overridden file, create a virtual file with the stored
2740 // size/timestamp.
2741 if ((Overridden || Transient || SkipChecks) && !File)
2742 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2743
2744 if (!File) {
2745 if (Complain) {
2746 std::string ErrorStr = "could not find file '";
2747 ErrorStr += *Filename;
2748 ErrorStr += "' referenced by AST file '";
2749 ErrorStr += F.FileName;
2750 ErrorStr += "'";
2751 Error(Msg: ErrorStr);
2752 }
2753 // Record that we didn't find the file.
2754 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2755 return InputFile();
2756 }
2757
2758 // Check if there was a request to override the contents of the file
2759 // that was part of the precompiled header. Overriding such a file
2760 // can lead to problems when lexing using the source locations from the
2761 // PCH.
2762 SourceManager &SM = getSourceManager();
2763 // FIXME: Reject if the overrides are different.
2764 if ((!Overridden && !Transient) && !SkipChecks &&
2765 SM.isFileOverridden(File: *File)) {
2766 if (Complain)
2767 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2768
2769 // After emitting the diagnostic, bypass the overriding file to recover
2770 // (this creates a separate FileEntry).
2771 File = SM.bypassFileContentsOverride(File: *File);
2772 if (!File) {
2773 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2774 return InputFile();
2775 }
2776 }
2777
2778 struct Change {
2779 enum ModificationKind {
2780 Size,
2781 ModTime,
2782 Content,
2783 None,
2784 } Kind;
2785 std::optional<int64_t> Old = std::nullopt;
2786 std::optional<int64_t> New = std::nullopt;
2787 };
2788 auto HasInputContentChanged = [&](Change OriginalChange) {
2789 assert(ValidateASTInputFilesContent &&
2790 "We should only check the content of the inputs with "
2791 "ValidateASTInputFilesContent enabled.");
2792
2793 if (StoredContentHash == 0)
2794 return OriginalChange;
2795
2796 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2797 if (!MemBuffOrError) {
2798 if (!Complain)
2799 return OriginalChange;
2800 std::string ErrorStr = "could not get buffer for file '";
2801 ErrorStr += File->getName();
2802 ErrorStr += "'";
2803 Error(Msg: ErrorStr);
2804 return OriginalChange;
2805 }
2806
2807 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2808 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2809 return Change{.Kind: Change::None};
2810
2811 return Change{.Kind: Change::Content};
2812 };
2813 auto HasInputFileChanged = [&]() {
2814 if (StoredSize != File->getSize())
2815 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2816 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2817 StoredTime != File->getModificationTime()) {
2818 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2819 .New: File->getModificationTime()};
2820
2821 // In case the modification time changes but not the content,
2822 // accept the cached file as legit.
2823 if (ValidateASTInputFilesContent)
2824 return HasInputContentChanged(MTimeChange);
2825
2826 return MTimeChange;
2827 }
2828 return Change{.Kind: Change::None};
2829 };
2830
2831 bool IsOutOfDate = false;
2832 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2833 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2834 // enabled, it is better to check the contents of the inputs. Since we can't
2835 // get correct modified time information for inputs from overriden inputs.
2836 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2837 F.StandardCXXModule && FileChange.Kind == Change::None)
2838 FileChange = HasInputContentChanged(FileChange);
2839
2840 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2841 // it is better to check the content of the input files because we cannot rely
2842 // on the file modification time, which will be the same (zero) for these
2843 // files.
2844 if (!StoredTime && ValidateASTInputFilesContent &&
2845 FileChange.Kind == Change::None)
2846 FileChange = HasInputContentChanged(FileChange);
2847
2848 // For an overridden file, there is nothing to validate.
2849 if (!Overridden && FileChange.Kind != Change::None) {
2850 if (Complain) {
2851 // Build a list of the PCH imports that got us here (in reverse).
2852 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2853 while (!ImportStack.back()->ImportedBy.empty())
2854 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2855
2856 // The top-level AST file is stale.
2857 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
2858 Diag(DiagID: diag::err_fe_ast_file_modified)
2859 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
2860 << TopLevelASTFileName << FileChange.Kind
2861 << (FileChange.Old && FileChange.New)
2862 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
2863 << llvm::itostr(X: FileChange.New.value_or(u: 0));
2864
2865 // Print the import stack.
2866 if (ImportStack.size() > 1) {
2867 Diag(DiagID: diag::note_ast_file_required_by)
2868 << *Filename << ImportStack[0]->FileName;
2869 for (unsigned I = 1; I < ImportStack.size(); ++I)
2870 Diag(DiagID: diag::note_ast_file_required_by)
2871 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
2872 }
2873
2874 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
2875 }
2876
2877 IsOutOfDate = true;
2878 }
2879 // FIXME: If the file is overridden and we've already opened it,
2880 // issue an error (or split it into a separate FileEntry).
2881
2882 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2883
2884 // Note that we've loaded this input file.
2885 F.InputFilesLoaded[ID-1] = IF;
2886 return IF;
2887}
2888
2889ASTReader::TemporarilyOwnedStringRef
2890ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
2891 ModuleFile &ModF) {
2892 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
2893}
2894
2895ASTReader::TemporarilyOwnedStringRef
2896ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
2897 StringRef Prefix) {
2898 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
2899
2900 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
2901 Path == "<built-in>" || Path == "<command line>")
2902 return {Path, Buf};
2903
2904 Buf.clear();
2905 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
2906 StringRef ResolvedPath{Buf.data(), Buf.size()};
2907 return {ResolvedPath, Buf};
2908}
2909
2910std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
2911 StringRef P,
2912 ModuleFile &ModF) {
2913 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
2914}
2915
2916std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
2917 StringRef P,
2918 StringRef Prefix) {
2919 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
2920 return ResolvedPath->str();
2921}
2922
2923static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2924 switch (ARR) {
2925 case ASTReader::Failure: return true;
2926 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2927 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2928 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2929 case ASTReader::ConfigurationMismatch:
2930 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2931 case ASTReader::HadErrors: return true;
2932 case ASTReader::Success: return false;
2933 }
2934
2935 llvm_unreachable("unknown ASTReadResult");
2936}
2937
2938ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2939 BitstreamCursor &Stream, StringRef Filename,
2940 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
2941 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
2942 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
2943 // FIXME this drops errors on the floor.
2944 consumeError(Err: std::move(Err));
2945 return Failure;
2946 }
2947
2948 // Read all of the records in the options block.
2949 RecordData Record;
2950 ASTReadResult Result = Success;
2951 while (true) {
2952 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2953 if (!MaybeEntry) {
2954 // FIXME this drops errors on the floor.
2955 consumeError(Err: MaybeEntry.takeError());
2956 return Failure;
2957 }
2958 llvm::BitstreamEntry Entry = MaybeEntry.get();
2959
2960 switch (Entry.Kind) {
2961 case llvm::BitstreamEntry::Error:
2962 case llvm::BitstreamEntry::SubBlock:
2963 return Failure;
2964
2965 case llvm::BitstreamEntry::EndBlock:
2966 return Result;
2967
2968 case llvm::BitstreamEntry::Record:
2969 // The interesting case.
2970 break;
2971 }
2972
2973 // Read and process a record.
2974 Record.clear();
2975 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2976 if (!MaybeRecordType) {
2977 // FIXME this drops errors on the floor.
2978 consumeError(Err: MaybeRecordType.takeError());
2979 return Failure;
2980 }
2981 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2982 case LANGUAGE_OPTIONS: {
2983 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2984 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
2985 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
2986 Result = ConfigurationMismatch;
2987 break;
2988 }
2989
2990 case TARGET_OPTIONS: {
2991 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2992 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
2993 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
2994 Result = ConfigurationMismatch;
2995 break;
2996 }
2997
2998 case FILE_SYSTEM_OPTIONS: {
2999 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3000 if (!AllowCompatibleConfigurationMismatch &&
3001 ParseFileSystemOptions(Record, Complain, Listener))
3002 Result = ConfigurationMismatch;
3003 break;
3004 }
3005
3006 case HEADER_SEARCH_OPTIONS: {
3007 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3008 if (!AllowCompatibleConfigurationMismatch &&
3009 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3010 Result = ConfigurationMismatch;
3011 break;
3012 }
3013
3014 case PREPROCESSOR_OPTIONS:
3015 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3016 if (!AllowCompatibleConfigurationMismatch &&
3017 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3018 SuggestedPredefines))
3019 Result = ConfigurationMismatch;
3020 break;
3021 }
3022 }
3023}
3024
3025ASTReader::ASTReadResult
3026ASTReader::ReadControlBlock(ModuleFile &F,
3027 SmallVectorImpl<ImportedModule> &Loaded,
3028 const ModuleFile *ImportedBy,
3029 unsigned ClientLoadCapabilities) {
3030 BitstreamCursor &Stream = F.Stream;
3031
3032 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3033 Error(Err: std::move(Err));
3034 return Failure;
3035 }
3036
3037 // Lambda to read the unhashed control block the first time it's called.
3038 //
3039 // For PCM files, the unhashed control block cannot be read until after the
3040 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3041 // need to look ahead before reading the IMPORTS record. For consistency,
3042 // this block is always read somehow (see BitstreamEntry::EndBlock).
3043 bool HasReadUnhashedControlBlock = false;
3044 auto readUnhashedControlBlockOnce = [&]() {
3045 if (!HasReadUnhashedControlBlock) {
3046 HasReadUnhashedControlBlock = true;
3047 if (ASTReadResult Result =
3048 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3049 return Result;
3050 }
3051 return Success;
3052 };
3053
3054 bool DisableValidation = shouldDisableValidationForFile(M: F);
3055
3056 // Read all of the records and blocks in the control block.
3057 RecordData Record;
3058 unsigned NumInputs = 0;
3059 unsigned NumUserInputs = 0;
3060 StringRef BaseDirectoryAsWritten;
3061 while (true) {
3062 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3063 if (!MaybeEntry) {
3064 Error(Err: MaybeEntry.takeError());
3065 return Failure;
3066 }
3067 llvm::BitstreamEntry Entry = MaybeEntry.get();
3068
3069 switch (Entry.Kind) {
3070 case llvm::BitstreamEntry::Error:
3071 Error(Msg: "malformed block record in AST file");
3072 return Failure;
3073 case llvm::BitstreamEntry::EndBlock: {
3074 // Validate the module before returning. This call catches an AST with
3075 // no module name and no imports.
3076 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3077 return Result;
3078
3079 // Validate input files.
3080 const HeaderSearchOptions &HSOpts =
3081 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3082
3083 // All user input files reside at the index range [0, NumUserInputs), and
3084 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3085 // loaded module files, ignore missing inputs.
3086 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3087 F.Kind != MK_PrebuiltModule) {
3088 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3089
3090 // If we are reading a module, we will create a verification timestamp,
3091 // so we verify all input files. Otherwise, verify only user input
3092 // files.
3093
3094 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3095 if (HSOpts.ModulesValidateOncePerBuildSession &&
3096 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3097 F.Kind == MK_ImplicitModule)
3098 N = ForceValidateUserInputs ? NumUserInputs : 0;
3099
3100 for (unsigned I = 0; I < N; ++I) {
3101 InputFile IF = getInputFile(F, ID: I+1, Complain);
3102 if (!IF.getFile() || IF.isOutOfDate())
3103 return OutOfDate;
3104 }
3105 }
3106
3107 if (Listener)
3108 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
3109
3110 if (Listener && Listener->needsInputFileVisitation()) {
3111 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3112 : NumUserInputs;
3113 for (unsigned I = 0; I < N; ++I) {
3114 bool IsSystem = I >= NumUserInputs;
3115 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3116 auto FilenameAsRequested = ResolveImportedPath(
3117 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3118 Listener->visitInputFile(
3119 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3120 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3121 }
3122 }
3123
3124 return Success;
3125 }
3126
3127 case llvm::BitstreamEntry::SubBlock:
3128 switch (Entry.ID) {
3129 case INPUT_FILES_BLOCK_ID:
3130 F.InputFilesCursor = Stream;
3131 if (llvm::Error Err = Stream.SkipBlock()) {
3132 Error(Err: std::move(Err));
3133 return Failure;
3134 }
3135 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3136 Error(Msg: "malformed block record in AST file");
3137 return Failure;
3138 }
3139 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3140 continue;
3141
3142 case OPTIONS_BLOCK_ID:
3143 // If we're reading the first module for this group, check its options
3144 // are compatible with ours. For modules it imports, no further checking
3145 // is required, because we checked them when we built it.
3146 if (Listener && !ImportedBy) {
3147 // Should we allow the configuration of the module file to differ from
3148 // the configuration of the current translation unit in a compatible
3149 // way?
3150 //
3151 // FIXME: Allow this for files explicitly specified with -include-pch.
3152 bool AllowCompatibleConfigurationMismatch =
3153 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3154
3155 ASTReadResult Result =
3156 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3157 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3158 SuggestedPredefines);
3159 if (Result == Failure) {
3160 Error(Msg: "malformed block record in AST file");
3161 return Result;
3162 }
3163
3164 if (DisableValidation ||
3165 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3166 Result = Success;
3167
3168 // If we can't load the module, exit early since we likely
3169 // will rebuild the module anyway. The stream may be in the
3170 // middle of a block.
3171 if (Result != Success)
3172 return Result;
3173 } else if (llvm::Error Err = Stream.SkipBlock()) {
3174 Error(Err: std::move(Err));
3175 return Failure;
3176 }
3177 continue;
3178
3179 default:
3180 if (llvm::Error Err = Stream.SkipBlock()) {
3181 Error(Err: std::move(Err));
3182 return Failure;
3183 }
3184 continue;
3185 }
3186
3187 case llvm::BitstreamEntry::Record:
3188 // The interesting case.
3189 break;
3190 }
3191
3192 // Read and process a record.
3193 Record.clear();
3194 StringRef Blob;
3195 Expected<unsigned> MaybeRecordType =
3196 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3197 if (!MaybeRecordType) {
3198 Error(Err: MaybeRecordType.takeError());
3199 return Failure;
3200 }
3201 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3202 case METADATA: {
3203 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3204 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3205 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3206 : diag::err_ast_file_version_too_new)
3207 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3208 return VersionMismatch;
3209 }
3210
3211 bool hasErrors = Record[7];
3212 if (hasErrors && !DisableValidation) {
3213 // If requested by the caller and the module hasn't already been read
3214 // or compiled, mark modules on error as out-of-date.
3215 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3216 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3217 return OutOfDate;
3218
3219 if (!AllowASTWithCompilerErrors) {
3220 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3221 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3222 return HadErrors;
3223 }
3224 }
3225 if (hasErrors) {
3226 Diags.ErrorOccurred = true;
3227 Diags.UncompilableErrorOccurred = true;
3228 Diags.UnrecoverableErrorOccurred = true;
3229 }
3230
3231 F.RelocatablePCH = Record[4];
3232 // Relative paths in a relocatable PCH are relative to our sysroot.
3233 if (F.RelocatablePCH)
3234 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3235
3236 F.StandardCXXModule = Record[5];
3237
3238 F.HasTimestamps = Record[6];
3239
3240 const std::string &CurBranch = getClangFullRepositoryVersion();
3241 StringRef ASTBranch = Blob;
3242 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3243 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3244 Diag(DiagID: diag::err_ast_file_different_branch)
3245 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3246 << CurBranch;
3247 return VersionMismatch;
3248 }
3249 break;
3250 }
3251
3252 case IMPORT: {
3253 // Validate the AST before processing any imports (otherwise, untangling
3254 // them can be error-prone and expensive). A module will have a name and
3255 // will already have been validated, but this catches the PCH case.
3256 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3257 return Result;
3258
3259 unsigned Idx = 0;
3260 // Read information about the AST file.
3261 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3262
3263 // The import location will be the local one for now; we will adjust
3264 // all import locations of module imports after the global source
3265 // location info are setup, in ReadAST.
3266 auto [ImportLoc, ImportModuleFileIndex] =
3267 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3268 // The import location must belong to the current module file itself.
3269 assert(ImportModuleFileIndex == 0);
3270
3271 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3272
3273 bool IsImportingStdCXXModule = Record[Idx++];
3274
3275 off_t StoredSize = 0;
3276 time_t StoredModTime = 0;
3277 ASTFileSignature StoredSignature;
3278 std::string ImportedFile;
3279 std::string StoredFile;
3280 bool IgnoreImportedByNote = false;
3281
3282 // For prebuilt and explicit modules first consult the file map for
3283 // an override. Note that here we don't search prebuilt module
3284 // directories if we're not importing standard c++ module, only the
3285 // explicit name to file mappings. Also, we will still verify the
3286 // size/signature making sure it is essentially the same file but
3287 // perhaps in a different location.
3288 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3289 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3290 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3291
3292 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3293 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3294 return Missing;
3295 }
3296
3297 if (!IsImportingStdCXXModule) {
3298 StoredSize = (off_t)Record[Idx++];
3299 StoredModTime = (time_t)Record[Idx++];
3300
3301 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3302 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3303 Last: SignatureBytes.end());
3304 Blob = Blob.substr(Start: ASTFileSignature::size);
3305
3306 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3307 // ModuleCache as when writing.
3308 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3309 if (ImportedFile.empty()) {
3310 ImportedFile = StoredFile;
3311 } else if (!getDiags().isIgnored(
3312 DiagID: diag::warn_module_file_mapping_mismatch,
3313 Loc: CurrentImportLoc)) {
3314 auto ImportedFileRef =
3315 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3316 auto StoredFileRef =
3317 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3318 if ((ImportedFileRef && StoredFileRef) &&
3319 (*ImportedFileRef != *StoredFileRef)) {
3320 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3321 << ImportedFile << StoredFile;
3322 Diag(DiagID: diag::note_module_file_imported_by)
3323 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3324 IgnoreImportedByNote = true;
3325 }
3326 }
3327 }
3328
3329 // If our client can't cope with us being out of date, we can't cope with
3330 // our dependency being missing.
3331 unsigned Capabilities = ClientLoadCapabilities;
3332 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3333 Capabilities &= ~ARR_Missing;
3334
3335 // Load the AST file.
3336 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3337 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3338 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3339
3340 // Check the AST we just read from ImportedFile contains a different
3341 // module than we expected (ImportedName). This can occur for C++20
3342 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3343 if (IsImportingStdCXXModule) {
3344 if (const auto *Imported =
3345 getModuleManager().lookupByFileName(FileName: ImportedFile);
3346 Imported != nullptr && Imported->ModuleName != ImportedName) {
3347 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3348 Result = Missing;
3349 }
3350 }
3351
3352 // If we diagnosed a problem, produce a backtrace.
3353 bool recompilingFinalized = Result == OutOfDate &&
3354 (Capabilities & ARR_OutOfDate) &&
3355 getModuleManager()
3356 .getModuleCache()
3357 .getInMemoryModuleCache()
3358 .isPCMFinal(Filename: F.FileName);
3359 if (!IgnoreImportedByNote &&
3360 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3361 Diag(DiagID: diag::note_module_file_imported_by)
3362 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3363
3364 switch (Result) {
3365 case Failure: return Failure;
3366 // If we have to ignore the dependency, we'll have to ignore this too.
3367 case Missing:
3368 case OutOfDate: return OutOfDate;
3369 case VersionMismatch: return VersionMismatch;
3370 case ConfigurationMismatch: return ConfigurationMismatch;
3371 case HadErrors: return HadErrors;
3372 case Success: break;
3373 }
3374 break;
3375 }
3376
3377 case ORIGINAL_FILE:
3378 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3379 F.ActualOriginalSourceFileName = std::string(Blob);
3380 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3381 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3382 break;
3383
3384 case ORIGINAL_FILE_ID:
3385 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3386 break;
3387
3388 case MODULE_NAME:
3389 F.ModuleName = std::string(Blob);
3390 Diag(DiagID: diag::remark_module_import)
3391 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3392 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3393 if (Listener)
3394 Listener->ReadModuleName(ModuleName: F.ModuleName);
3395
3396 // Validate the AST as soon as we have a name so we can exit early on
3397 // failure.
3398 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3399 return Result;
3400
3401 break;
3402
3403 case MODULE_DIRECTORY: {
3404 // Save the BaseDirectory as written in the PCM for computing the module
3405 // filename for the ModuleCache.
3406 BaseDirectoryAsWritten = Blob;
3407 assert(!F.ModuleName.empty() &&
3408 "MODULE_DIRECTORY found before MODULE_NAME");
3409 F.BaseDirectory = std::string(Blob);
3410 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3411 break;
3412 // If we've already loaded a module map file covering this module, we may
3413 // have a better path for it (relative to the current build).
3414 Module *M = PP.getHeaderSearchInfo().lookupModule(
3415 ModuleName: F.ModuleName, ImportLoc: SourceLocation(), /*AllowSearch*/ true,
3416 /*AllowExtraModuleMapSearch*/ true);
3417 if (M && M->Directory) {
3418 // If we're implicitly loading a module, the base directory can't
3419 // change between the build and use.
3420 // Don't emit module relocation error if we have -fno-validate-pch
3421 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3422 DisableValidationForModuleKind::Module) &&
3423 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3424 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3425 if (!BuildDir || *BuildDir != M->Directory) {
3426 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3427 Diag(DiagID: diag::err_imported_module_relocated)
3428 << F.ModuleName << Blob << M->Directory->getName();
3429 return OutOfDate;
3430 }
3431 }
3432 F.BaseDirectory = std::string(M->Directory->getName());
3433 }
3434 break;
3435 }
3436
3437 case MODULE_MAP_FILE:
3438 if (ASTReadResult Result =
3439 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3440 return Result;
3441 break;
3442
3443 case INPUT_FILE_OFFSETS:
3444 NumInputs = Record[0];
3445 NumUserInputs = Record[1];
3446 F.InputFileOffsets =
3447 (const llvm::support::unaligned_uint64_t *)Blob.data();
3448 F.InputFilesLoaded.resize(new_size: NumInputs);
3449 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3450 F.NumUserInputFiles = NumUserInputs;
3451 break;
3452 }
3453 }
3454}
3455
3456llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3457 unsigned ClientLoadCapabilities) {
3458 BitstreamCursor &Stream = F.Stream;
3459
3460 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3461 return Err;
3462 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3463
3464 // Read all of the records and blocks for the AST file.
3465 RecordData Record;
3466 while (true) {
3467 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3468 if (!MaybeEntry)
3469 return MaybeEntry.takeError();
3470 llvm::BitstreamEntry Entry = MaybeEntry.get();
3471
3472 switch (Entry.Kind) {
3473 case llvm::BitstreamEntry::Error:
3474 return llvm::createStringError(
3475 EC: std::errc::illegal_byte_sequence,
3476 Fmt: "error at end of module block in AST file");
3477 case llvm::BitstreamEntry::EndBlock:
3478 // Outside of C++, we do not store a lookup map for the translation unit.
3479 // Instead, mark it as needing a lookup map to be built if this module
3480 // contains any declarations lexically within it (which it always does!).
3481 // This usually has no cost, since we very rarely need the lookup map for
3482 // the translation unit outside C++.
3483 if (ASTContext *Ctx = ContextObj) {
3484 DeclContext *DC = Ctx->getTranslationUnitDecl();
3485 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3486 DC->setMustBuildLookupTable();
3487 }
3488
3489 return llvm::Error::success();
3490 case llvm::BitstreamEntry::SubBlock:
3491 switch (Entry.ID) {
3492 case DECLTYPES_BLOCK_ID:
3493 // We lazily load the decls block, but we want to set up the
3494 // DeclsCursor cursor to point into it. Clone our current bitcode
3495 // cursor to it, enter the block and read the abbrevs in that block.
3496 // With the main cursor, we just skip over it.
3497 F.DeclsCursor = Stream;
3498 if (llvm::Error Err = Stream.SkipBlock())
3499 return Err;
3500 if (llvm::Error Err = ReadBlockAbbrevs(
3501 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3502 return Err;
3503 break;
3504
3505 case PREPROCESSOR_BLOCK_ID:
3506 F.MacroCursor = Stream;
3507 if (!PP.getExternalSource())
3508 PP.setExternalSource(this);
3509
3510 if (llvm::Error Err = Stream.SkipBlock())
3511 return Err;
3512 if (llvm::Error Err =
3513 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3514 return Err;
3515 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3516 break;
3517
3518 case PREPROCESSOR_DETAIL_BLOCK_ID:
3519 F.PreprocessorDetailCursor = Stream;
3520
3521 if (llvm::Error Err = Stream.SkipBlock()) {
3522 return Err;
3523 }
3524 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3525 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3526 return Err;
3527 F.PreprocessorDetailStartOffset
3528 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3529
3530 if (!PP.getPreprocessingRecord())
3531 PP.createPreprocessingRecord();
3532 if (!PP.getPreprocessingRecord()->getExternalSource())
3533 PP.getPreprocessingRecord()->SetExternalSource(*this);
3534 break;
3535
3536 case SOURCE_MANAGER_BLOCK_ID:
3537 if (llvm::Error Err = ReadSourceManagerBlock(F))
3538 return Err;
3539 break;
3540
3541 case SUBMODULE_BLOCK_ID:
3542 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3543 return Err;
3544 break;
3545
3546 case COMMENTS_BLOCK_ID: {
3547 BitstreamCursor C = Stream;
3548
3549 if (llvm::Error Err = Stream.SkipBlock())
3550 return Err;
3551 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3552 return Err;
3553 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3554 break;
3555 }
3556
3557 default:
3558 if (llvm::Error Err = Stream.SkipBlock())
3559 return Err;
3560 break;
3561 }
3562 continue;
3563
3564 case llvm::BitstreamEntry::Record:
3565 // The interesting case.
3566 break;
3567 }
3568
3569 // Read and process a record.
3570 Record.clear();
3571 StringRef Blob;
3572 Expected<unsigned> MaybeRecordType =
3573 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3574 if (!MaybeRecordType)
3575 return MaybeRecordType.takeError();
3576 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3577
3578 // If we're not loading an AST context, we don't care about most records.
3579 if (!ContextObj) {
3580 switch (RecordType) {
3581 case IDENTIFIER_TABLE:
3582 case IDENTIFIER_OFFSET:
3583 case INTERESTING_IDENTIFIERS:
3584 case STATISTICS:
3585 case PP_ASSUME_NONNULL_LOC:
3586 case PP_CONDITIONAL_STACK:
3587 case PP_COUNTER_VALUE:
3588 case SOURCE_LOCATION_OFFSETS:
3589 case MODULE_OFFSET_MAP:
3590 case SOURCE_MANAGER_LINE_TABLE:
3591 case PPD_ENTITIES_OFFSETS:
3592 case HEADER_SEARCH_TABLE:
3593 case IMPORTED_MODULES:
3594 case MACRO_OFFSET:
3595 break;
3596 default:
3597 continue;
3598 }
3599 }
3600
3601 switch (RecordType) {
3602 default: // Default behavior: ignore.
3603 break;
3604
3605 case TYPE_OFFSET: {
3606 if (F.LocalNumTypes != 0)
3607 return llvm::createStringError(
3608 EC: std::errc::illegal_byte_sequence,
3609 Fmt: "duplicate TYPE_OFFSET record in AST file");
3610 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3611 F.LocalNumTypes = Record[0];
3612 F.BaseTypeIndex = getTotalNumTypes();
3613
3614 if (F.LocalNumTypes > 0)
3615 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3616
3617 break;
3618 }
3619
3620 case DECL_OFFSET: {
3621 if (F.LocalNumDecls != 0)
3622 return llvm::createStringError(
3623 EC: std::errc::illegal_byte_sequence,
3624 Fmt: "duplicate DECL_OFFSET record in AST file");
3625 F.DeclOffsets = (const DeclOffset *)Blob.data();
3626 F.LocalNumDecls = Record[0];
3627 F.BaseDeclIndex = getTotalNumDecls();
3628
3629 if (F.LocalNumDecls > 0)
3630 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3631
3632 break;
3633 }
3634
3635 case TU_UPDATE_LEXICAL: {
3636 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3637 LexicalContents Contents(
3638 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3639 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3640 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3641 TU->setHasExternalLexicalStorage(true);
3642 break;
3643 }
3644
3645 case UPDATE_VISIBLE: {
3646 unsigned Idx = 0;
3647 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3648 auto *Data = (const unsigned char*)Blob.data();
3649 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3650 // If we've already loaded the decl, perform the updates when we finish
3651 // loading this block.
3652 if (Decl *D = GetExistingDecl(ID))
3653 PendingUpdateRecords.push_back(
3654 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3655 break;
3656 }
3657
3658 case UPDATE_MODULE_LOCAL_VISIBLE: {
3659 unsigned Idx = 0;
3660 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3661 auto *Data = (const unsigned char *)Blob.data();
3662 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3663 // If we've already loaded the decl, perform the updates when we finish
3664 // loading this block.
3665 if (Decl *D = GetExistingDecl(ID))
3666 PendingUpdateRecords.push_back(
3667 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3668 break;
3669 }
3670
3671 case UPDATE_TU_LOCAL_VISIBLE: {
3672 if (F.Kind != MK_MainFile)
3673 break;
3674 unsigned Idx = 0;
3675 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3676 auto *Data = (const unsigned char *)Blob.data();
3677 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3678 // If we've already loaded the decl, perform the updates when we finish
3679 // loading this block.
3680 if (Decl *D = GetExistingDecl(ID))
3681 PendingUpdateRecords.push_back(
3682 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3683 break;
3684 }
3685
3686 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3687 unsigned Idx = 0;
3688 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3689 auto *Data = (const unsigned char *)Blob.data();
3690 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3691 // If we've already loaded the decl, perform the updates when we finish
3692 // loading this block.
3693 if (Decl *D = GetExistingDecl(ID))
3694 PendingUpdateRecords.push_back(
3695 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3696 break;
3697 }
3698
3699 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3700 unsigned Idx = 0;
3701 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3702 auto *Data = (const unsigned char *)Blob.data();
3703 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3704 // If we've already loaded the decl, perform the updates when we finish
3705 // loading this block.
3706 if (Decl *D = GetExistingDecl(ID))
3707 PendingUpdateRecords.push_back(
3708 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3709 break;
3710 }
3711
3712 case IDENTIFIER_TABLE:
3713 F.IdentifierTableData =
3714 reinterpret_cast<const unsigned char *>(Blob.data());
3715 if (Record[0]) {
3716 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3717 Buckets: F.IdentifierTableData + Record[0],
3718 Payload: F.IdentifierTableData + sizeof(uint32_t),
3719 Base: F.IdentifierTableData,
3720 InfoObj: ASTIdentifierLookupTrait(*this, F));
3721
3722 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3723 }
3724 break;
3725
3726 case IDENTIFIER_OFFSET: {
3727 if (F.LocalNumIdentifiers != 0)
3728 return llvm::createStringError(
3729 EC: std::errc::illegal_byte_sequence,
3730 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3731 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3732 F.LocalNumIdentifiers = Record[0];
3733 F.BaseIdentifierID = getTotalNumIdentifiers();
3734
3735 if (F.LocalNumIdentifiers > 0)
3736 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3737 + F.LocalNumIdentifiers);
3738 break;
3739 }
3740
3741 case INTERESTING_IDENTIFIERS:
3742 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3743 break;
3744
3745 case EAGERLY_DESERIALIZED_DECLS:
3746 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3747 // about "interesting" decls (for instance, if we're building a module).
3748 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3749 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3750 break;
3751
3752 case MODULAR_CODEGEN_DECLS:
3753 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3754 // them (ie: if we're not codegenerating this module).
3755 if (F.Kind == MK_MainFile ||
3756 getContext().getLangOpts().BuildingPCHWithObjectFile)
3757 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3758 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3759 break;
3760
3761 case SPECIAL_TYPES:
3762 if (SpecialTypes.empty()) {
3763 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3764 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3765 break;
3766 }
3767
3768 if (Record.empty())
3769 break;
3770
3771 if (SpecialTypes.size() != Record.size())
3772 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3773 Fmt: "invalid special-types record");
3774
3775 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3776 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3777 if (!SpecialTypes[I])
3778 SpecialTypes[I] = ID;
3779 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3780 // merge step?
3781 }
3782 break;
3783
3784 case STATISTICS:
3785 TotalNumStatements += Record[0];
3786 TotalNumMacros += Record[1];
3787 TotalLexicalDeclContexts += Record[2];
3788 TotalVisibleDeclContexts += Record[3];
3789 TotalModuleLocalVisibleDeclContexts += Record[4];
3790 TotalTULocalVisibleDeclContexts += Record[5];
3791 break;
3792
3793 case UNUSED_FILESCOPED_DECLS:
3794 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3795 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3796 break;
3797
3798 case DELEGATING_CTORS:
3799 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3800 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3801 break;
3802
3803 case WEAK_UNDECLARED_IDENTIFIERS:
3804 if (Record.size() % 3 != 0)
3805 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3806 Fmt: "invalid weak identifiers record");
3807
3808 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3809 // files. This isn't the way to do it :)
3810 WeakUndeclaredIdentifiers.clear();
3811
3812 // Translate the weak, undeclared identifiers into global IDs.
3813 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3814 WeakUndeclaredIdentifiers.push_back(
3815 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3816 WeakUndeclaredIdentifiers.push_back(
3817 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3818 WeakUndeclaredIdentifiers.push_back(
3819 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3820 }
3821 break;
3822
3823 case SELECTOR_OFFSETS: {
3824 F.SelectorOffsets = (const uint32_t *)Blob.data();
3825 F.LocalNumSelectors = Record[0];
3826 unsigned LocalBaseSelectorID = Record[1];
3827 F.BaseSelectorID = getTotalNumSelectors();
3828
3829 if (F.LocalNumSelectors > 0) {
3830 // Introduce the global -> local mapping for selectors within this
3831 // module.
3832 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
3833
3834 // Introduce the local -> global mapping for selectors within this
3835 // module.
3836 F.SelectorRemap.insertOrReplace(
3837 Val: std::make_pair(x&: LocalBaseSelectorID,
3838 y: F.BaseSelectorID - LocalBaseSelectorID));
3839
3840 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
3841 }
3842 break;
3843 }
3844
3845 case METHOD_POOL:
3846 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3847 if (Record[0])
3848 F.SelectorLookupTable
3849 = ASTSelectorLookupTable::Create(
3850 Buckets: F.SelectorLookupTableData + Record[0],
3851 Base: F.SelectorLookupTableData,
3852 InfoObj: ASTSelectorLookupTrait(*this, F));
3853 TotalNumMethodPoolEntries += Record[1];
3854 break;
3855
3856 case REFERENCED_SELECTOR_POOL:
3857 if (!Record.empty()) {
3858 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3859 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
3860 LocalID: Record[Idx++]));
3861 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
3862 getRawEncoding());
3863 }
3864 }
3865 break;
3866
3867 case PP_ASSUME_NONNULL_LOC: {
3868 unsigned Idx = 0;
3869 if (!Record.empty())
3870 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3871 ReadSourceLocation(ModuleFile&: F, Record, Idx));
3872 break;
3873 }
3874
3875 case PP_UNSAFE_BUFFER_USAGE: {
3876 if (!Record.empty()) {
3877 SmallVector<SourceLocation, 64> SrcLocs;
3878 unsigned Idx = 0;
3879 while (Idx < Record.size())
3880 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
3881 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
3882 }
3883 break;
3884 }
3885
3886 case PP_CONDITIONAL_STACK:
3887 if (!Record.empty()) {
3888 unsigned Idx = 0, End = Record.size() - 1;
3889 bool ReachedEOFWhileSkipping = Record[Idx++];
3890 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3891 if (ReachedEOFWhileSkipping) {
3892 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3893 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3894 bool FoundNonSkipPortion = Record[Idx++];
3895 bool FoundElse = Record[Idx++];
3896 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3897 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
3898 args&: FoundElse, args&: ElseLoc);
3899 }
3900 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3901 while (Idx < End) {
3902 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3903 bool WasSkipping = Record[Idx++];
3904 bool FoundNonSkip = Record[Idx++];
3905 bool FoundElse = Record[Idx++];
3906 ConditionalStack.push_back(
3907 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
3908 }
3909 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
3910 }
3911 break;
3912
3913 case PP_COUNTER_VALUE:
3914 if (!Record.empty() && Listener)
3915 Listener->ReadCounter(M: F, Value: Record[0]);
3916 break;
3917
3918 case FILE_SORTED_DECLS:
3919 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
3920 F.NumFileSortedDecls = Record[0];
3921 break;
3922
3923 case SOURCE_LOCATION_OFFSETS: {
3924 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3925 F.LocalNumSLocEntries = Record[0];
3926 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3927 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3928 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
3929 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
3930 TotalSize: SLocSpaceSize);
3931 if (!F.SLocEntryBaseID) {
3932 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
3933 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
3934 return llvm::createStringError(EC: std::errc::invalid_argument,
3935 Fmt: "ran out of source locations");
3936 }
3937 // Make our entry in the range map. BaseID is negative and growing, so
3938 // we invert it. Because we invert it, though, we need the other end of
3939 // the range.
3940 unsigned RangeStart =
3941 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3942 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
3943 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
3944
3945 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3946 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3947 GlobalSLocOffsetMap.insert(
3948 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3949 - SLocSpaceSize,y: &F));
3950
3951 TotalNumSLocEntries += F.LocalNumSLocEntries;
3952 break;
3953 }
3954
3955 case MODULE_OFFSET_MAP:
3956 F.ModuleOffsetMap = Blob;
3957 break;
3958
3959 case SOURCE_MANAGER_LINE_TABLE:
3960 ParseLineTable(F, Record);
3961 break;
3962
3963 case EXT_VECTOR_DECLS:
3964 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3965 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3966 break;
3967
3968 case VTABLE_USES:
3969 if (Record.size() % 3 != 0)
3970 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3971 Fmt: "Invalid VTABLE_USES record");
3972
3973 // Later tables overwrite earlier ones.
3974 // FIXME: Modules will have some trouble with this. This is clearly not
3975 // the right way to do this.
3976 VTableUses.clear();
3977
3978 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3979 VTableUses.push_back(
3980 Elt: {.ID: ReadDeclID(F, Record, Idx),
3981 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
3982 .Used: (bool)Record[Idx++]});
3983 }
3984 break;
3985
3986 case PENDING_IMPLICIT_INSTANTIATIONS:
3987
3988 if (Record.size() % 2 != 0)
3989 return llvm::createStringError(
3990 EC: std::errc::illegal_byte_sequence,
3991 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3992
3993 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3994 PendingInstantiations.push_back(
3995 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
3996 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
3997 }
3998 break;
3999
4000 case SEMA_DECL_REFS:
4001 if (Record.size() != 3)
4002 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4003 Fmt: "Invalid SEMA_DECL_REFS block");
4004 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4005 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4006 break;
4007
4008 case PPD_ENTITIES_OFFSETS: {
4009 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4010 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4011 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4012
4013 unsigned LocalBasePreprocessedEntityID = Record[0];
4014
4015 unsigned StartingID;
4016 if (!PP.getPreprocessingRecord())
4017 PP.createPreprocessingRecord();
4018 if (!PP.getPreprocessingRecord()->getExternalSource())
4019 PP.getPreprocessingRecord()->SetExternalSource(*this);
4020 StartingID
4021 = PP.getPreprocessingRecord()
4022 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4023 F.BasePreprocessedEntityID = StartingID;
4024
4025 if (F.NumPreprocessedEntities > 0) {
4026 // Introduce the global -> local mapping for preprocessed entities in
4027 // this module.
4028 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4029
4030 // Introduce the local -> global mapping for preprocessed entities in
4031 // this module.
4032 F.PreprocessedEntityRemap.insertOrReplace(
4033 Val: std::make_pair(x&: LocalBasePreprocessedEntityID,
4034 y: F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
4035 }
4036
4037 break;
4038 }
4039
4040 case PPD_SKIPPED_RANGES: {
4041 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4042 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4043 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4044
4045 if (!PP.getPreprocessingRecord())
4046 PP.createPreprocessingRecord();
4047 if (!PP.getPreprocessingRecord()->getExternalSource())
4048 PP.getPreprocessingRecord()->SetExternalSource(*this);
4049 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4050 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4051
4052 if (F.NumPreprocessedSkippedRanges > 0)
4053 GlobalSkippedRangeMap.insert(
4054 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4055 break;
4056 }
4057
4058 case DECL_UPDATE_OFFSETS:
4059 if (Record.size() % 2 != 0)
4060 return llvm::createStringError(
4061 EC: std::errc::illegal_byte_sequence,
4062 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4063 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4064 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4065 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4066
4067 // If we've already loaded the decl, perform the updates when we finish
4068 // loading this block.
4069 if (Decl *D = GetExistingDecl(ID))
4070 PendingUpdateRecords.push_back(
4071 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4072 }
4073 break;
4074
4075 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4076 if (Record.size() % 5 != 0)
4077 return llvm::createStringError(
4078 EC: std::errc::illegal_byte_sequence,
4079 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4080 "file");
4081 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4082 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4083
4084 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4085 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4086 uint64_t LocalLexicalOffset = Record[I++];
4087 uint64_t LexicalOffset =
4088 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4089 uint64_t LocalVisibleOffset = Record[I++];
4090 uint64_t VisibleOffset =
4091 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4092 uint64_t LocalModuleLocalOffset = Record[I++];
4093 uint64_t ModuleLocalOffset =
4094 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4095 uint64_t TULocalLocalOffset = Record[I++];
4096 uint64_t TULocalOffset =
4097 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4098
4099 DelayedNamespaceOffsetMap[ID] = {
4100 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: TULocalOffset, .TULocalOffset: ModuleLocalOffset}, .LexicalOffset: LexicalOffset};
4101
4102 assert(!GetExistingDecl(ID) &&
4103 "We shouldn't load the namespace in the front of delayed "
4104 "namespace lexical and visible block");
4105 }
4106 break;
4107 }
4108
4109 case RELATED_DECLS_MAP:
4110 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4111 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4112 auto &RelatedDecls = RelatedDeclsMap[ID];
4113 unsigned NN = Record[I++];
4114 RelatedDecls.reserve(N: NN);
4115 for (unsigned II = 0; II < NN; II++)
4116 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4117 }
4118 break;
4119
4120 case OBJC_CATEGORIES_MAP:
4121 if (F.LocalNumObjCCategoriesInMap != 0)
4122 return llvm::createStringError(
4123 EC: std::errc::illegal_byte_sequence,
4124 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4125
4126 F.LocalNumObjCCategoriesInMap = Record[0];
4127 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4128 break;
4129
4130 case OBJC_CATEGORIES:
4131 F.ObjCCategories.swap(RHS&: Record);
4132 break;
4133
4134 case CUDA_SPECIAL_DECL_REFS:
4135 // Later tables overwrite earlier ones.
4136 // FIXME: Modules will have trouble with this.
4137 CUDASpecialDeclRefs.clear();
4138 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4139 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4140 break;
4141
4142 case HEADER_SEARCH_TABLE:
4143 F.HeaderFileInfoTableData = Blob.data();
4144 F.LocalNumHeaderFileInfos = Record[1];
4145 if (Record[0]) {
4146 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4147 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4148 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4149 InfoObj: HeaderFileInfoTrait(*this, F));
4150
4151 PP.getHeaderSearchInfo().SetExternalSource(this);
4152 if (!PP.getHeaderSearchInfo().getExternalLookup())
4153 PP.getHeaderSearchInfo().SetExternalLookup(this);
4154 }
4155 break;
4156
4157 case FP_PRAGMA_OPTIONS:
4158 // Later tables overwrite earlier ones.
4159 FPPragmaOptions.swap(RHS&: Record);
4160 break;
4161
4162 case DECLS_WITH_EFFECTS_TO_VERIFY:
4163 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4164 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4165 break;
4166
4167 case OPENCL_EXTENSIONS:
4168 for (unsigned I = 0, E = Record.size(); I != E; ) {
4169 auto Name = ReadString(Record, Idx&: I);
4170 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4171 OptInfo.Supported = Record[I++] != 0;
4172 OptInfo.Enabled = Record[I++] != 0;
4173 OptInfo.WithPragma = Record[I++] != 0;
4174 OptInfo.Avail = Record[I++];
4175 OptInfo.Core = Record[I++];
4176 OptInfo.Opt = Record[I++];
4177 }
4178 break;
4179
4180 case TENTATIVE_DEFINITIONS:
4181 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4182 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4183 break;
4184
4185 case KNOWN_NAMESPACES:
4186 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4187 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4188 break;
4189
4190 case UNDEFINED_BUT_USED:
4191 if (Record.size() % 2 != 0)
4192 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4193 Fmt: "invalid undefined-but-used record");
4194 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4195 UndefinedButUsed.push_back(
4196 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4197 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4198 }
4199 break;
4200
4201 case DELETE_EXPRS_TO_ANALYZE:
4202 for (unsigned I = 0, N = Record.size(); I != N;) {
4203 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4204 const uint64_t Count = Record[I++];
4205 DelayedDeleteExprs.push_back(Elt: Count);
4206 for (uint64_t C = 0; C < Count; ++C) {
4207 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4208 bool IsArrayForm = Record[I++] == 1;
4209 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4210 }
4211 }
4212 break;
4213
4214 case VTABLES_TO_EMIT:
4215 if (F.Kind == MK_MainFile ||
4216 getContext().getLangOpts().BuildingPCHWithObjectFile)
4217 for (unsigned I = 0, N = Record.size(); I != N;)
4218 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4219 break;
4220
4221 case IMPORTED_MODULES:
4222 if (!F.isModule()) {
4223 // If we aren't loading a module (which has its own exports), make
4224 // all of the imported modules visible.
4225 // FIXME: Deal with macros-only imports.
4226 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4227 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4228 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4229 if (GlobalID) {
4230 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4231 if (DeserializationListener)
4232 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4233 }
4234 }
4235 }
4236 break;
4237
4238 case MACRO_OFFSET: {
4239 if (F.LocalNumMacros != 0)
4240 return llvm::createStringError(
4241 EC: std::errc::illegal_byte_sequence,
4242 Fmt: "duplicate MACRO_OFFSET record in AST file");
4243 F.MacroOffsets = (const uint32_t *)Blob.data();
4244 F.LocalNumMacros = Record[0];
4245 unsigned LocalBaseMacroID = Record[1];
4246 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
4247 F.BaseMacroID = getTotalNumMacros();
4248
4249 if (F.LocalNumMacros > 0) {
4250 // Introduce the global -> local mapping for macros within this module.
4251 GlobalMacroMap.insert(Val: std::make_pair(x: getTotalNumMacros() + 1, y: &F));
4252
4253 // Introduce the local -> global mapping for macros within this module.
4254 F.MacroRemap.insertOrReplace(
4255 Val: std::make_pair(x&: LocalBaseMacroID,
4256 y: F.BaseMacroID - LocalBaseMacroID));
4257
4258 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4259 }
4260 break;
4261 }
4262
4263 case LATE_PARSED_TEMPLATE:
4264 LateParsedTemplates.emplace_back(
4265 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4266 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4267 break;
4268
4269 case OPTIMIZE_PRAGMA_OPTIONS:
4270 if (Record.size() != 1)
4271 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4272 Fmt: "invalid pragma optimize record");
4273 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4274 break;
4275
4276 case MSSTRUCT_PRAGMA_OPTIONS:
4277 if (Record.size() != 1)
4278 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4279 Fmt: "invalid pragma ms_struct record");
4280 PragmaMSStructState = Record[0];
4281 break;
4282
4283 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4284 if (Record.size() != 2)
4285 return llvm::createStringError(
4286 EC: std::errc::illegal_byte_sequence,
4287 Fmt: "invalid pragma pointers to members record");
4288 PragmaMSPointersToMembersState = Record[0];
4289 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4290 break;
4291
4292 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4293 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4294 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4295 break;
4296
4297 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4298 if (Record.size() != 1)
4299 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4300 Fmt: "invalid cuda pragma options record");
4301 ForceHostDeviceDepth = Record[0];
4302 break;
4303
4304 case ALIGN_PACK_PRAGMA_OPTIONS: {
4305 if (Record.size() < 3)
4306 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4307 Fmt: "invalid pragma pack record");
4308 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4309 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4310 unsigned NumStackEntries = Record[2];
4311 unsigned Idx = 3;
4312 // Reset the stack when importing a new module.
4313 PragmaAlignPackStack.clear();
4314 for (unsigned I = 0; I < NumStackEntries; ++I) {
4315 PragmaAlignPackStackEntry Entry;
4316 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4317 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4318 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4319 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4320 Entry.SlotLabel = PragmaAlignPackStrings.back();
4321 PragmaAlignPackStack.push_back(Elt: Entry);
4322 }
4323 break;
4324 }
4325
4326 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4327 if (Record.size() < 3)
4328 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4329 Fmt: "invalid pragma float control record");
4330 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4331 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4332 unsigned NumStackEntries = Record[2];
4333 unsigned Idx = 3;
4334 // Reset the stack when importing a new module.
4335 FpPragmaStack.clear();
4336 for (unsigned I = 0; I < NumStackEntries; ++I) {
4337 FpPragmaStackEntry Entry;
4338 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4339 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4340 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4341 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4342 Entry.SlotLabel = FpPragmaStrings.back();
4343 FpPragmaStack.push_back(Elt: Entry);
4344 }
4345 break;
4346 }
4347
4348 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4349 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4350 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4351 break;
4352 }
4353 }
4354}
4355
4356void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4357 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4358
4359 // Additional remapping information.
4360 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4361 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4362 F.ModuleOffsetMap = StringRef();
4363
4364 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4365 RemapBuilder MacroRemap(F.MacroRemap);
4366 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4367 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4368 RemapBuilder SelectorRemap(F.SelectorRemap);
4369
4370 auto &ImportedModuleVector = F.TransitiveImports;
4371 assert(ImportedModuleVector.empty());
4372
4373 while (Data < DataEnd) {
4374 // FIXME: Looking up dependency modules by filename is horrible. Let's
4375 // start fixing this with prebuilt, explicit and implicit modules and see
4376 // how it goes...
4377 using namespace llvm::support;
4378 ModuleKind Kind = static_cast<ModuleKind>(
4379 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4380 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4381 StringRef Name = StringRef((const char*)Data, Len);
4382 Data += Len;
4383 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4384 Kind == MK_ImplicitModule
4385 ? ModuleMgr.lookupByModuleName(ModName: Name)
4386 : ModuleMgr.lookupByFileName(FileName: Name));
4387 if (!OM) {
4388 std::string Msg = "refers to unknown module, cannot find ";
4389 Msg.append(str: std::string(Name));
4390 Error(Msg);
4391 return;
4392 }
4393
4394 ImportedModuleVector.push_back(Elt: OM);
4395
4396 uint32_t MacroIDOffset =
4397 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4398 uint32_t PreprocessedEntityIDOffset =
4399 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4400 uint32_t SubmoduleIDOffset =
4401 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4402 uint32_t SelectorIDOffset =
4403 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4404
4405 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4406 RemapBuilder &Remap) {
4407 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4408 if (Offset != None)
4409 Remap.insert(Val: std::make_pair(x&: Offset,
4410 y: static_cast<int>(BaseOffset - Offset)));
4411 };
4412
4413 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4414 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4415 PreprocessedEntityRemap);
4416 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4417 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4418 }
4419}
4420
4421ASTReader::ASTReadResult
4422ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4423 const ModuleFile *ImportedBy,
4424 unsigned ClientLoadCapabilities) {
4425 unsigned Idx = 0;
4426 F.ModuleMapPath = ReadPath(F, Record, Idx);
4427
4428 // Try to resolve ModuleName in the current header search context and
4429 // verify that it is found in the same module map file as we saved. If the
4430 // top-level AST file is a main file, skip this check because there is no
4431 // usable header search context.
4432 assert(!F.ModuleName.empty() &&
4433 "MODULE_NAME should come before MODULE_MAP_FILE");
4434 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4435 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4436 // An implicitly-loaded module file should have its module listed in some
4437 // module map file that we've already loaded.
4438 Module *M =
4439 PP.getHeaderSearchInfo().lookupModule(ModuleName: F.ModuleName, ImportLoc: F.ImportLoc);
4440 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4441 OptionalFileEntryRef ModMap =
4442 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4443 // Don't emit module relocation error if we have -fno-validate-pch
4444 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4445 DisableValidationForModuleKind::Module) &&
4446 !ModMap) {
4447 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4448 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4449 // This module was defined by an imported (explicit) module.
4450 Diag(DiagID: diag::err_module_file_conflict) << F.ModuleName << F.FileName
4451 << ASTFE->getName();
4452 // TODO: Add a note with the module map paths if they differ.
4453 } else {
4454 // This module was built with a different module map.
4455 Diag(DiagID: diag::err_imported_module_not_found)
4456 << F.ModuleName << F.FileName
4457 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4458 << !ImportedBy;
4459 // In case it was imported by a PCH, there's a chance the user is
4460 // just missing to include the search path to the directory containing
4461 // the modulemap.
4462 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4463 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4464 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4465 }
4466 }
4467 return OutOfDate;
4468 }
4469
4470 assert(M && M->Name == F.ModuleName && "found module with different name");
4471
4472 // Check the primary module map file.
4473 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4474 if (!StoredModMap || *StoredModMap != ModMap) {
4475 assert(ModMap && "found module is missing module map file");
4476 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4477 "top-level import should be verified");
4478 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4479 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4480 Diag(DiagID: diag::err_imported_module_modmap_changed)
4481 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4482 << ModMap->getName() << F.ModuleMapPath << NotImported;
4483 return OutOfDate;
4484 }
4485
4486 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4487 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4488 // FIXME: we should use input files rather than storing names.
4489 std::string Filename = ReadPath(F, Record, Idx);
4490 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4491 if (!SF) {
4492 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4493 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4494 return OutOfDate;
4495 }
4496 AdditionalStoredMaps.insert(V: *SF);
4497 }
4498
4499 // Check any additional module map files (e.g. module.private.modulemap)
4500 // that are not in the pcm.
4501 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4502 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4503 // Remove files that match
4504 // Note: SmallPtrSet::erase is really remove
4505 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4506 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4507 Diag(DiagID: diag::err_module_different_modmap)
4508 << F.ModuleName << /*new*/0 << ModMap.getName();
4509 return OutOfDate;
4510 }
4511 }
4512 }
4513
4514 // Check any additional module map files that are in the pcm, but not
4515 // found in header search. Cases that match are already removed.
4516 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4517 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4518 Diag(DiagID: diag::err_module_different_modmap)
4519 << F.ModuleName << /*not new*/1 << ModMap.getName();
4520 return OutOfDate;
4521 }
4522 }
4523
4524 if (Listener)
4525 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4526 return Success;
4527}
4528
4529/// Move the given method to the back of the global list of methods.
4530static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4531 // Find the entry for this selector in the method pool.
4532 SemaObjC::GlobalMethodPool::iterator Known =
4533 S.ObjC().MethodPool.find(Val: Method->getSelector());
4534 if (Known == S.ObjC().MethodPool.end())
4535 return;
4536
4537 // Retrieve the appropriate method list.
4538 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4539 : Known->second.second;
4540 bool Found = false;
4541 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4542 if (!Found) {
4543 if (List->getMethod() == Method) {
4544 Found = true;
4545 } else {
4546 // Keep searching.
4547 continue;
4548 }
4549 }
4550
4551 if (List->getNext())
4552 List->setMethod(List->getNext()->getMethod());
4553 else
4554 List->setMethod(Method);
4555 }
4556}
4557
4558void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4559 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4560 for (Decl *D : Names) {
4561 bool wasHidden = !D->isUnconditionallyVisible();
4562 D->setVisibleDespiteOwningModule();
4563
4564 if (wasHidden && SemaObj) {
4565 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4566 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4567 }
4568 }
4569 }
4570}
4571
4572void ASTReader::makeModuleVisible(Module *Mod,
4573 Module::NameVisibilityKind NameVisibility,
4574 SourceLocation ImportLoc) {
4575 llvm::SmallPtrSet<Module *, 4> Visited;
4576 SmallVector<Module *, 4> Stack;
4577 Stack.push_back(Elt: Mod);
4578 while (!Stack.empty()) {
4579 Mod = Stack.pop_back_val();
4580
4581 if (NameVisibility <= Mod->NameVisibility) {
4582 // This module already has this level of visibility (or greater), so
4583 // there is nothing more to do.
4584 continue;
4585 }
4586
4587 if (Mod->isUnimportable()) {
4588 // Modules that aren't importable cannot be made visible.
4589 continue;
4590 }
4591
4592 // Update the module's name visibility.
4593 Mod->NameVisibility = NameVisibility;
4594
4595 // If we've already deserialized any names from this module,
4596 // mark them as visible.
4597 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4598 if (Hidden != HiddenNamesMap.end()) {
4599 auto HiddenNames = std::move(*Hidden);
4600 HiddenNamesMap.erase(I: Hidden);
4601 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4602 assert(!HiddenNamesMap.contains(Mod) &&
4603 "making names visible added hidden names");
4604 }
4605
4606 // Push any exported modules onto the stack to be marked as visible.
4607 SmallVector<Module *, 16> Exports;
4608 Mod->getExportedModules(Exported&: Exports);
4609 for (SmallVectorImpl<Module *>::iterator
4610 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4611 Module *Exported = *I;
4612 if (Visited.insert(Ptr: Exported).second)
4613 Stack.push_back(Elt: Exported);
4614 }
4615 }
4616}
4617
4618/// We've merged the definition \p MergedDef into the existing definition
4619/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4620/// visible.
4621void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4622 NamedDecl *MergedDef) {
4623 if (!Def->isUnconditionallyVisible()) {
4624 // If MergedDef is visible or becomes visible, make the definition visible.
4625 if (MergedDef->isUnconditionallyVisible())
4626 Def->setVisibleDespiteOwningModule();
4627 else {
4628 getContext().mergeDefinitionIntoModule(
4629 ND: Def, M: MergedDef->getImportedOwningModule(),
4630 /*NotifyListeners*/ false);
4631 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4632 }
4633 }
4634}
4635
4636bool ASTReader::loadGlobalIndex() {
4637 if (GlobalIndex)
4638 return false;
4639
4640 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4641 !PP.getLangOpts().Modules)
4642 return true;
4643
4644 // Try to load the global index.
4645 TriedLoadingGlobalIndex = true;
4646 StringRef ModuleCachePath
4647 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4648 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4649 GlobalModuleIndex::readIndex(Path: ModuleCachePath);
4650 if (llvm::Error Err = std::move(Result.second)) {
4651 assert(!Result.first);
4652 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4653 return true;
4654 }
4655
4656 GlobalIndex.reset(p: Result.first);
4657 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4658 return false;
4659}
4660
4661bool ASTReader::isGlobalIndexUnavailable() const {
4662 return PP.getLangOpts().Modules && UseGlobalIndex &&
4663 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4664}
4665
4666/// Given a cursor at the start of an AST file, scan ahead and drop the
4667/// cursor into the start of the given block ID, returning false on success and
4668/// true on failure.
4669static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4670 while (true) {
4671 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4672 if (!MaybeEntry) {
4673 // FIXME this drops errors on the floor.
4674 consumeError(Err: MaybeEntry.takeError());
4675 return true;
4676 }
4677 llvm::BitstreamEntry Entry = MaybeEntry.get();
4678
4679 switch (Entry.Kind) {
4680 case llvm::BitstreamEntry::Error:
4681 case llvm::BitstreamEntry::EndBlock:
4682 return true;
4683
4684 case llvm::BitstreamEntry::Record:
4685 // Ignore top-level records.
4686 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4687 break;
4688 else {
4689 // FIXME this drops errors on the floor.
4690 consumeError(Err: Skipped.takeError());
4691 return true;
4692 }
4693
4694 case llvm::BitstreamEntry::SubBlock:
4695 if (Entry.ID == BlockID) {
4696 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4697 // FIXME this drops the error on the floor.
4698 consumeError(Err: std::move(Err));
4699 return true;
4700 }
4701 // Found it!
4702 return false;
4703 }
4704
4705 if (llvm::Error Err = Cursor.SkipBlock()) {
4706 // FIXME this drops the error on the floor.
4707 consumeError(Err: std::move(Err));
4708 return true;
4709 }
4710 }
4711 }
4712}
4713
4714ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4715 SourceLocation ImportLoc,
4716 unsigned ClientLoadCapabilities,
4717 ModuleFile **NewLoadedModuleFile) {
4718 llvm::TimeTraceScope scope("ReadAST", FileName);
4719
4720 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4721 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4722 CurrentDeserializingModuleKind, Type);
4723
4724 // Defer any pending actions until we get to the end of reading the AST file.
4725 Deserializing AnASTFile(this);
4726
4727 // Bump the generation number.
4728 unsigned PreviousGeneration = 0;
4729 if (ContextObj)
4730 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4731
4732 unsigned NumModules = ModuleMgr.size();
4733 SmallVector<ImportedModule, 4> Loaded;
4734 if (ASTReadResult ReadResult =
4735 ReadASTCore(FileName, Type, ImportLoc,
4736 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4737 ClientLoadCapabilities)) {
4738 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4739
4740 // If we find that any modules are unusable, the global index is going
4741 // to be out-of-date. Just remove it.
4742 GlobalIndex.reset();
4743 ModuleMgr.setGlobalIndex(nullptr);
4744 return ReadResult;
4745 }
4746
4747 if (NewLoadedModuleFile && !Loaded.empty())
4748 *NewLoadedModuleFile = Loaded.back().Mod;
4749
4750 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4751 // remove modules from this point. Various fields are updated during reading
4752 // the AST block and removing the modules would result in dangling pointers.
4753 // They are generally only incidentally dereferenced, ie. a binary search
4754 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4755 // be dereferenced but it wouldn't actually be used.
4756
4757 // Load the AST blocks of all of the modules that we loaded. We can still
4758 // hit errors parsing the ASTs at this point.
4759 for (ImportedModule &M : Loaded) {
4760 ModuleFile &F = *M.Mod;
4761 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4762
4763 // Read the AST block.
4764 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4765 Error(Err: std::move(Err));
4766 return Failure;
4767 }
4768
4769 // The AST block should always have a definition for the main module.
4770 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4771 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
4772 return Failure;
4773 }
4774
4775 // Read the extension blocks.
4776 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
4777 if (llvm::Error Err = ReadExtensionBlock(F)) {
4778 Error(Err: std::move(Err));
4779 return Failure;
4780 }
4781 }
4782
4783 // Once read, set the ModuleFile bit base offset and update the size in
4784 // bits of all files we've seen.
4785 F.GlobalBitOffset = TotalModulesSizeInBits;
4786 TotalModulesSizeInBits += F.SizeInBits;
4787 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
4788 }
4789
4790 // Preload source locations and interesting indentifiers.
4791 for (ImportedModule &M : Loaded) {
4792 ModuleFile &F = *M.Mod;
4793
4794 // Map the original source file ID into the ID space of the current
4795 // compilation.
4796 if (F.OriginalSourceFileID.isValid())
4797 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
4798
4799 for (auto Offset : F.PreloadIdentifierOffsets) {
4800 const unsigned char *Data = F.IdentifierTableData + Offset;
4801
4802 ASTIdentifierLookupTrait Trait(*this, F);
4803 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
4804 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
4805
4806 IdentifierInfo *II;
4807 if (!PP.getLangOpts().CPlusPlus) {
4808 // Identifiers present in both the module file and the importing
4809 // instance are marked out-of-date so that they can be deserialized
4810 // on next use via ASTReader::updateOutOfDateIdentifier().
4811 // Identifiers present in the module file but not in the importing
4812 // instance are ignored for now, preventing growth of the identifier
4813 // table. They will be deserialized on first use via ASTReader::get().
4814 auto It = PP.getIdentifierTable().find(Name: Key);
4815 if (It == PP.getIdentifierTable().end())
4816 continue;
4817 II = It->second;
4818 } else {
4819 // With C++ modules, not many identifiers are considered interesting.
4820 // All identifiers in the module file can be placed into the identifier
4821 // table of the importing instance and marked as out-of-date. This makes
4822 // ASTReader::get() a no-op, and deserialization will take place on
4823 // first/next use via ASTReader::updateOutOfDateIdentifier().
4824 II = &PP.getIdentifierTable().getOwn(Name: Key);
4825 }
4826
4827 II->setOutOfDate(true);
4828
4829 // Mark this identifier as being from an AST file so that we can track
4830 // whether we need to serialize it.
4831 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
4832
4833 // Associate the ID with the identifier so that the writer can reuse it.
4834 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
4835 SetIdentifierInfo(ID, II);
4836 }
4837 }
4838
4839 // Builtins and library builtins have already been initialized. Mark all
4840 // identifiers as out-of-date, so that they are deserialized on first use.
4841 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4842 for (auto &Id : PP.getIdentifierTable())
4843 Id.second->setOutOfDate(true);
4844
4845 // Mark selectors as out of date.
4846 for (const auto &Sel : SelectorGeneration)
4847 SelectorOutOfDate[Sel.first] = true;
4848
4849 // Setup the import locations and notify the module manager that we've
4850 // committed to these module files.
4851 for (ImportedModule &M : Loaded) {
4852 ModuleFile &F = *M.Mod;
4853
4854 ModuleMgr.moduleFileAccepted(MF: &F);
4855
4856 // Set the import location.
4857 F.DirectImportLoc = ImportLoc;
4858 // FIXME: We assume that locations from PCH / preamble do not need
4859 // any translation.
4860 if (!M.ImportedBy)
4861 F.ImportLoc = M.ImportLoc;
4862 else
4863 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
4864 }
4865
4866 // Resolve any unresolved module exports.
4867 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4868 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4869 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
4870 Module *ResolvedMod = getSubmodule(GlobalID);
4871
4872 switch (Unresolved.Kind) {
4873 case UnresolvedModuleRef::Conflict:
4874 if (ResolvedMod) {
4875 Module::Conflict Conflict;
4876 Conflict.Other = ResolvedMod;
4877 Conflict.Message = Unresolved.String.str();
4878 Unresolved.Mod->Conflicts.push_back(x: Conflict);
4879 }
4880 continue;
4881
4882 case UnresolvedModuleRef::Import:
4883 if (ResolvedMod)
4884 Unresolved.Mod->Imports.insert(X: ResolvedMod);
4885 continue;
4886
4887 case UnresolvedModuleRef::Affecting:
4888 if (ResolvedMod)
4889 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
4890 continue;
4891
4892 case UnresolvedModuleRef::Export:
4893 if (ResolvedMod || Unresolved.IsWildcard)
4894 Unresolved.Mod->Exports.push_back(
4895 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4896 continue;
4897 }
4898 }
4899 UnresolvedModuleRefs.clear();
4900
4901 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4902 // Might be unnecessary as use declarations are only used to build the
4903 // module itself.
4904
4905 if (ContextObj)
4906 InitializeContext();
4907
4908 if (SemaObj)
4909 UpdateSema();
4910
4911 if (DeserializationListener)
4912 DeserializationListener->ReaderInitialized(Reader: this);
4913
4914 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4915 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4916 // If this AST file is a precompiled preamble, then set the
4917 // preamble file ID of the source manager to the file source file
4918 // from which the preamble was built.
4919 if (Type == MK_Preamble) {
4920 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4921 } else if (Type == MK_MainFile) {
4922 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4923 }
4924 }
4925
4926 // For any Objective-C class definitions we have already loaded, make sure
4927 // that we load any additional categories.
4928 if (ContextObj) {
4929 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4930 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
4931 D: ObjCClassesLoaded[I], PreviousGeneration);
4932 }
4933 }
4934
4935 const HeaderSearchOptions &HSOpts =
4936 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4937 if (HSOpts.ModulesValidateOncePerBuildSession) {
4938 // Now we are certain that the module and all modules it depends on are
4939 // up-to-date. For implicitly-built module files, ensure the corresponding
4940 // timestamp files are up-to-date in this build session.
4941 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4942 ImportedModule &M = Loaded[I];
4943 if (M.Mod->Kind == MK_ImplicitModule &&
4944 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4945 getModuleManager().getModuleCache().updateModuleTimestamp(
4946 ModuleFilename: M.Mod->FileName);
4947 }
4948 }
4949
4950 return Success;
4951}
4952
4953static ASTFileSignature readASTFileSignature(StringRef PCH);
4954
4955/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
4956static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4957 // FIXME checking magic headers is done in other places such as
4958 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4959 // always done the same. Unify it all with a helper.
4960 if (!Stream.canSkipToPos(pos: 4))
4961 return llvm::createStringError(
4962 EC: std::errc::illegal_byte_sequence,
4963 Fmt: "file too small to contain precompiled file magic");
4964 for (unsigned C : {'C', 'P', 'C', 'H'})
4965 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
4966 if (Res.get() != C)
4967 return llvm::createStringError(
4968 EC: std::errc::illegal_byte_sequence,
4969 Fmt: "file doesn't start with precompiled file magic");
4970 } else
4971 return Res.takeError();
4972 return llvm::Error::success();
4973}
4974
4975static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4976 switch (Kind) {
4977 case MK_PCH:
4978 return 0; // PCH
4979 case MK_ImplicitModule:
4980 case MK_ExplicitModule:
4981 case MK_PrebuiltModule:
4982 return 1; // module
4983 case MK_MainFile:
4984 case MK_Preamble:
4985 return 2; // main source file
4986 }
4987 llvm_unreachable("unknown module kind");
4988}
4989
4990ASTReader::ASTReadResult
4991ASTReader::ReadASTCore(StringRef FileName,
4992 ModuleKind Type,
4993 SourceLocation ImportLoc,
4994 ModuleFile *ImportedBy,
4995 SmallVectorImpl<ImportedModule> &Loaded,
4996 off_t ExpectedSize, time_t ExpectedModTime,
4997 ASTFileSignature ExpectedSignature,
4998 unsigned ClientLoadCapabilities) {
4999 ModuleFile *M;
5000 std::string ErrorStr;
5001 ModuleManager::AddModuleResult AddResult
5002 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
5003 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
5004 ExpectedSignature, ReadSignature: readASTFileSignature,
5005 Module&: M, ErrorStr);
5006
5007 switch (AddResult) {
5008 case ModuleManager::AlreadyLoaded:
5009 Diag(DiagID: diag::remark_module_import)
5010 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5011 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5012 return Success;
5013
5014 case ModuleManager::NewlyLoaded:
5015 // Load module file below.
5016 break;
5017
5018 case ModuleManager::Missing:
5019 // The module file was missing; if the client can handle that, return
5020 // it.
5021 if (ClientLoadCapabilities & ARR_Missing)
5022 return Missing;
5023
5024 // Otherwise, return an error.
5025 Diag(DiagID: diag::err_ast_file_not_found)
5026 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5027 << ErrorStr;
5028 return Failure;
5029
5030 case ModuleManager::OutOfDate:
5031 // We couldn't load the module file because it is out-of-date. If the
5032 // client can handle out-of-date, return it.
5033 if (ClientLoadCapabilities & ARR_OutOfDate)
5034 return OutOfDate;
5035
5036 // Otherwise, return an error.
5037 Diag(DiagID: diag::err_ast_file_out_of_date)
5038 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5039 << ErrorStr;
5040 return Failure;
5041 }
5042
5043 assert(M && "Missing module file");
5044
5045 bool ShouldFinalizePCM = false;
5046 auto FinalizeOrDropPCM = llvm::make_scope_exit(F: [&]() {
5047 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5048 if (ShouldFinalizePCM)
5049 MC.finalizePCM(Filename: FileName);
5050 else
5051 MC.tryToDropPCM(Filename: FileName);
5052 });
5053 ModuleFile &F = *M;
5054 BitstreamCursor &Stream = F.Stream;
5055 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5056 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5057
5058 // Sniff for the signature.
5059 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5060 Diag(DiagID: diag::err_ast_file_invalid)
5061 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5062 return Failure;
5063 }
5064
5065 // This is used for compatibility with older PCH formats.
5066 bool HaveReadControlBlock = false;
5067 while (true) {
5068 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5069 if (!MaybeEntry) {
5070 Error(Err: MaybeEntry.takeError());
5071 return Failure;
5072 }
5073 llvm::BitstreamEntry Entry = MaybeEntry.get();
5074
5075 switch (Entry.Kind) {
5076 case llvm::BitstreamEntry::Error:
5077 case llvm::BitstreamEntry::Record:
5078 case llvm::BitstreamEntry::EndBlock:
5079 Error(Msg: "invalid record at top-level of AST file");
5080 return Failure;
5081
5082 case llvm::BitstreamEntry::SubBlock:
5083 break;
5084 }
5085
5086 switch (Entry.ID) {
5087 case CONTROL_BLOCK_ID:
5088 HaveReadControlBlock = true;
5089 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5090 case Success:
5091 // Check that we didn't try to load a non-module AST file as a module.
5092 //
5093 // FIXME: Should we also perform the converse check? Loading a module as
5094 // a PCH file sort of works, but it's a bit wonky.
5095 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5096 Type == MK_PrebuiltModule) &&
5097 F.ModuleName.empty()) {
5098 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5099 if (Result != OutOfDate ||
5100 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5101 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5102 return Result;
5103 }
5104 break;
5105
5106 case Failure: return Failure;
5107 case Missing: return Missing;
5108 case OutOfDate: return OutOfDate;
5109 case VersionMismatch: return VersionMismatch;
5110 case ConfigurationMismatch: return ConfigurationMismatch;
5111 case HadErrors: return HadErrors;
5112 }
5113 break;
5114
5115 case AST_BLOCK_ID:
5116 if (!HaveReadControlBlock) {
5117 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5118 Diag(DiagID: diag::err_ast_file_version_too_old)
5119 << moduleKindForDiagnostic(Kind: Type) << FileName;
5120 return VersionMismatch;
5121 }
5122
5123 // Record that we've loaded this module.
5124 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5125 ShouldFinalizePCM = true;
5126 return Success;
5127
5128 default:
5129 if (llvm::Error Err = Stream.SkipBlock()) {
5130 Error(Err: std::move(Err));
5131 return Failure;
5132 }
5133 break;
5134 }
5135 }
5136
5137 llvm_unreachable("unexpected break; expected return");
5138}
5139
5140ASTReader::ASTReadResult
5141ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5142 unsigned ClientLoadCapabilities) {
5143 const HeaderSearchOptions &HSOpts =
5144 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5145 bool AllowCompatibleConfigurationMismatch =
5146 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5147 bool DisableValidation = shouldDisableValidationForFile(M: F);
5148
5149 ASTReadResult Result = readUnhashedControlBlockImpl(
5150 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5151 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5152 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5153
5154 // If F was directly imported by another module, it's implicitly validated by
5155 // the importing module.
5156 if (DisableValidation || WasImportedBy ||
5157 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5158 return Success;
5159
5160 if (Result == Failure) {
5161 Error(Msg: "malformed block record in AST file");
5162 return Failure;
5163 }
5164
5165 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5166 // If this module has already been finalized in the ModuleCache, we're stuck
5167 // with it; we can only load a single version of each module.
5168 //
5169 // This can happen when a module is imported in two contexts: in one, as a
5170 // user module; in another, as a system module (due to an import from
5171 // another module marked with the [system] flag). It usually indicates a
5172 // bug in the module map: this module should also be marked with [system].
5173 //
5174 // If -Wno-system-headers (the default), and the first import is as a
5175 // system module, then validation will fail during the as-user import,
5176 // since -Werror flags won't have been validated. However, it's reasonable
5177 // to treat this consistently as a system module.
5178 //
5179 // If -Wsystem-headers, the PCM on disk was built with
5180 // -Wno-system-headers, and the first import is as a user module, then
5181 // validation will fail during the as-system import since the PCM on disk
5182 // doesn't guarantee that -Werror was respected. However, the -Werror
5183 // flags were checked during the initial as-user import.
5184 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5185 Filename: F.FileName)) {
5186 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5187 return Success;
5188 }
5189 }
5190
5191 return Result;
5192}
5193
5194ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5195 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5196 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5197 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5198 // Initialize a stream.
5199 BitstreamCursor Stream(StreamData);
5200
5201 // Sniff for the signature.
5202 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5203 // FIXME this drops the error on the floor.
5204 consumeError(Err: std::move(Err));
5205 return Failure;
5206 }
5207
5208 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5209 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5210 return Failure;
5211
5212 // Read all of the records in the options block.
5213 RecordData Record;
5214 ASTReadResult Result = Success;
5215 while (true) {
5216 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5217 if (!MaybeEntry) {
5218 // FIXME this drops the error on the floor.
5219 consumeError(Err: MaybeEntry.takeError());
5220 return Failure;
5221 }
5222 llvm::BitstreamEntry Entry = MaybeEntry.get();
5223
5224 switch (Entry.Kind) {
5225 case llvm::BitstreamEntry::Error:
5226 case llvm::BitstreamEntry::SubBlock:
5227 return Failure;
5228
5229 case llvm::BitstreamEntry::EndBlock:
5230 return Result;
5231
5232 case llvm::BitstreamEntry::Record:
5233 // The interesting case.
5234 break;
5235 }
5236
5237 // Read and process a record.
5238 Record.clear();
5239 StringRef Blob;
5240 Expected<unsigned> MaybeRecordType =
5241 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5242 if (!MaybeRecordType) {
5243 // FIXME this drops the error.
5244 return Failure;
5245 }
5246 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5247 case SIGNATURE:
5248 if (F) {
5249 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5250 assert(F->Signature != ASTFileSignature::createDummy() &&
5251 "Dummy AST file signature not backpatched in ASTWriter.");
5252 }
5253 break;
5254 case AST_BLOCK_HASH:
5255 if (F) {
5256 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5257 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5258 "Dummy AST block hash not backpatched in ASTWriter.");
5259 }
5260 break;
5261 case DIAGNOSTIC_OPTIONS: {
5262 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5263 if (Listener && ValidateDiagnosticOptions &&
5264 !AllowCompatibleConfigurationMismatch &&
5265 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5266 Result = OutOfDate; // Don't return early. Read the signature.
5267 break;
5268 }
5269 case HEADER_SEARCH_PATHS: {
5270 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5271 if (Listener && !AllowCompatibleConfigurationMismatch &&
5272 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5273 Result = ConfigurationMismatch;
5274 break;
5275 }
5276 case DIAG_PRAGMA_MAPPINGS:
5277 if (!F)
5278 break;
5279 if (F->PragmaDiagMappings.empty())
5280 F->PragmaDiagMappings.swap(RHS&: Record);
5281 else
5282 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5283 From: Record.begin(), To: Record.end());
5284 break;
5285 case HEADER_SEARCH_ENTRY_USAGE:
5286 if (F)
5287 F->SearchPathUsage = ReadBitVector(Record, Blob);
5288 break;
5289 case VFS_USAGE:
5290 if (F)
5291 F->VFSUsage = ReadBitVector(Record, Blob);
5292 break;
5293 }
5294 }
5295}
5296
5297/// Parse a record and blob containing module file extension metadata.
5298static bool parseModuleFileExtensionMetadata(
5299 const SmallVectorImpl<uint64_t> &Record,
5300 StringRef Blob,
5301 ModuleFileExtensionMetadata &Metadata) {
5302 if (Record.size() < 4) return true;
5303
5304 Metadata.MajorVersion = Record[0];
5305 Metadata.MinorVersion = Record[1];
5306
5307 unsigned BlockNameLen = Record[2];
5308 unsigned UserInfoLen = Record[3];
5309
5310 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5311
5312 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5313 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5314 Blob.data() + BlockNameLen + UserInfoLen);
5315 return false;
5316}
5317
5318llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5319 BitstreamCursor &Stream = F.Stream;
5320
5321 RecordData Record;
5322 while (true) {
5323 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5324 if (!MaybeEntry)
5325 return MaybeEntry.takeError();
5326 llvm::BitstreamEntry Entry = MaybeEntry.get();
5327
5328 switch (Entry.Kind) {
5329 case llvm::BitstreamEntry::SubBlock:
5330 if (llvm::Error Err = Stream.SkipBlock())
5331 return Err;
5332 continue;
5333 case llvm::BitstreamEntry::EndBlock:
5334 return llvm::Error::success();
5335 case llvm::BitstreamEntry::Error:
5336 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5337 Fmt: "malformed block record in AST file");
5338 case llvm::BitstreamEntry::Record:
5339 break;
5340 }
5341
5342 Record.clear();
5343 StringRef Blob;
5344 Expected<unsigned> MaybeRecCode =
5345 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5346 if (!MaybeRecCode)
5347 return MaybeRecCode.takeError();
5348 switch (MaybeRecCode.get()) {
5349 case EXTENSION_METADATA: {
5350 ModuleFileExtensionMetadata Metadata;
5351 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5352 return llvm::createStringError(
5353 EC: std::errc::illegal_byte_sequence,
5354 Fmt: "malformed EXTENSION_METADATA in AST file");
5355
5356 // Find a module file extension with this block name.
5357 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5358 if (Known == ModuleFileExtensions.end()) break;
5359
5360 // Form a reader.
5361 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5362 Mod&: F, Stream)) {
5363 F.ExtensionReaders.push_back(x: std::move(Reader));
5364 }
5365
5366 break;
5367 }
5368 }
5369 }
5370
5371 llvm_unreachable("ReadExtensionBlock should return from while loop");
5372}
5373
5374void ASTReader::InitializeContext() {
5375 assert(ContextObj && "no context to initialize");
5376 ASTContext &Context = *ContextObj;
5377
5378 // If there's a listener, notify them that we "read" the translation unit.
5379 if (DeserializationListener)
5380 DeserializationListener->DeclRead(
5381 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5382 D: Context.getTranslationUnitDecl());
5383
5384 // FIXME: Find a better way to deal with collisions between these
5385 // built-in types. Right now, we just ignore the problem.
5386
5387 // Load the special types.
5388 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5389 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5390 if (!Context.CFConstantStringTypeDecl)
5391 Context.setCFConstantStringType(GetType(ID: String));
5392 }
5393
5394 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5395 QualType FileType = GetType(ID: File);
5396 if (FileType.isNull()) {
5397 Error(Msg: "FILE type is NULL");
5398 return;
5399 }
5400
5401 if (!Context.FILEDecl) {
5402 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5403 Context.setFILEDecl(Typedef->getDecl());
5404 else {
5405 const TagType *Tag = FileType->getAs<TagType>();
5406 if (!Tag) {
5407 Error(Msg: "Invalid FILE type in AST file");
5408 return;
5409 }
5410 Context.setFILEDecl(Tag->getDecl());
5411 }
5412 }
5413 }
5414
5415 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5416 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5417 if (Jmp_bufType.isNull()) {
5418 Error(Msg: "jmp_buf type is NULL");
5419 return;
5420 }
5421
5422 if (!Context.jmp_bufDecl) {
5423 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5424 Context.setjmp_bufDecl(Typedef->getDecl());
5425 else {
5426 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5427 if (!Tag) {
5428 Error(Msg: "Invalid jmp_buf type in AST file");
5429 return;
5430 }
5431 Context.setjmp_bufDecl(Tag->getDecl());
5432 }
5433 }
5434 }
5435
5436 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5437 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5438 if (Sigjmp_bufType.isNull()) {
5439 Error(Msg: "sigjmp_buf type is NULL");
5440 return;
5441 }
5442
5443 if (!Context.sigjmp_bufDecl) {
5444 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5445 Context.setsigjmp_bufDecl(Typedef->getDecl());
5446 else {
5447 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5448 assert(Tag && "Invalid sigjmp_buf type in AST file");
5449 Context.setsigjmp_bufDecl(Tag->getDecl());
5450 }
5451 }
5452 }
5453
5454 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5455 if (Context.ObjCIdRedefinitionType.isNull())
5456 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5457 }
5458
5459 if (TypeID ObjCClassRedef =
5460 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5461 if (Context.ObjCClassRedefinitionType.isNull())
5462 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5463 }
5464
5465 if (TypeID ObjCSelRedef =
5466 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5467 if (Context.ObjCSelRedefinitionType.isNull())
5468 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5469 }
5470
5471 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5472 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5473 if (Ucontext_tType.isNull()) {
5474 Error(Msg: "ucontext_t type is NULL");
5475 return;
5476 }
5477
5478 if (!Context.ucontext_tDecl) {
5479 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5480 Context.setucontext_tDecl(Typedef->getDecl());
5481 else {
5482 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5483 assert(Tag && "Invalid ucontext_t type in AST file");
5484 Context.setucontext_tDecl(Tag->getDecl());
5485 }
5486 }
5487 }
5488 }
5489
5490 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5491
5492 // If there were any CUDA special declarations, deserialize them.
5493 if (!CUDASpecialDeclRefs.empty()) {
5494 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5495 Context.setcudaConfigureCallDecl(
5496 cast<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5497 }
5498
5499 // Re-export any modules that were imported by a non-module AST file.
5500 // FIXME: This does not make macro-only imports visible again.
5501 for (auto &Import : PendingImportedModules) {
5502 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5503 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5504 /*ImportLoc=*/Import.ImportLoc);
5505 if (Import.ImportLoc.isValid())
5506 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5507 // This updates visibility for Preprocessor only. For Sema, which can be
5508 // nullptr here, we do the same later, in UpdateSema().
5509 }
5510 }
5511
5512 // Hand off these modules to Sema.
5513 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5514 PendingImportedModules.clear();
5515}
5516
5517void ASTReader::finalizeForWriting() {
5518 // Nothing to do for now.
5519}
5520
5521/// Reads and return the signature record from \p PCH's control block, or
5522/// else returns 0.
5523static ASTFileSignature readASTFileSignature(StringRef PCH) {
5524 BitstreamCursor Stream(PCH);
5525 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5526 // FIXME this drops the error on the floor.
5527 consumeError(Err: std::move(Err));
5528 return ASTFileSignature();
5529 }
5530
5531 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5532 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5533 return ASTFileSignature();
5534
5535 // Scan for SIGNATURE inside the diagnostic options block.
5536 ASTReader::RecordData Record;
5537 while (true) {
5538 Expected<llvm::BitstreamEntry> MaybeEntry =
5539 Stream.advanceSkippingSubblocks();
5540 if (!MaybeEntry) {
5541 // FIXME this drops the error on the floor.
5542 consumeError(Err: MaybeEntry.takeError());
5543 return ASTFileSignature();
5544 }
5545 llvm::BitstreamEntry Entry = MaybeEntry.get();
5546
5547 if (Entry.Kind != llvm::BitstreamEntry::Record)
5548 return ASTFileSignature();
5549
5550 Record.clear();
5551 StringRef Blob;
5552 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5553 if (!MaybeRecord) {
5554 // FIXME this drops the error on the floor.
5555 consumeError(Err: MaybeRecord.takeError());
5556 return ASTFileSignature();
5557 }
5558 if (SIGNATURE == MaybeRecord.get()) {
5559 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5560 assert(Signature != ASTFileSignature::createDummy() &&
5561 "Dummy AST file signature not backpatched in ASTWriter.");
5562 return Signature;
5563 }
5564 }
5565}
5566
5567/// Retrieve the name of the original source file name
5568/// directly from the AST file, without actually loading the AST
5569/// file.
5570std::string ASTReader::getOriginalSourceFile(
5571 const std::string &ASTFileName, FileManager &FileMgr,
5572 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5573 // Open the AST file.
5574 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5575 /*RequiresNullTerminator=*/false,
5576 /*MaybeLimit=*/std::nullopt,
5577 /*IsText=*/false);
5578 if (!Buffer) {
5579 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5580 << ASTFileName << Buffer.getError().message();
5581 return std::string();
5582 }
5583
5584 // Initialize the stream
5585 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5586
5587 // Sniff for the signature.
5588 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5589 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5590 return std::string();
5591 }
5592
5593 // Scan for the CONTROL_BLOCK_ID block.
5594 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5595 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5596 return std::string();
5597 }
5598
5599 // Scan for ORIGINAL_FILE inside the control block.
5600 RecordData Record;
5601 while (true) {
5602 Expected<llvm::BitstreamEntry> MaybeEntry =
5603 Stream.advanceSkippingSubblocks();
5604 if (!MaybeEntry) {
5605 // FIXME this drops errors on the floor.
5606 consumeError(Err: MaybeEntry.takeError());
5607 return std::string();
5608 }
5609 llvm::BitstreamEntry Entry = MaybeEntry.get();
5610
5611 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5612 return std::string();
5613
5614 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5615 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5616 return std::string();
5617 }
5618
5619 Record.clear();
5620 StringRef Blob;
5621 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5622 if (!MaybeRecord) {
5623 // FIXME this drops the errors on the floor.
5624 consumeError(Err: MaybeRecord.takeError());
5625 return std::string();
5626 }
5627 if (ORIGINAL_FILE == MaybeRecord.get())
5628 return Blob.str();
5629 }
5630}
5631
5632namespace {
5633
5634 class SimplePCHValidator : public ASTReaderListener {
5635 const LangOptions &ExistingLangOpts;
5636 const TargetOptions &ExistingTargetOpts;
5637 const PreprocessorOptions &ExistingPPOpts;
5638 std::string ExistingModuleCachePath;
5639 FileManager &FileMgr;
5640 bool StrictOptionMatches;
5641
5642 public:
5643 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5644 const TargetOptions &ExistingTargetOpts,
5645 const PreprocessorOptions &ExistingPPOpts,
5646 StringRef ExistingModuleCachePath, FileManager &FileMgr,
5647 bool StrictOptionMatches)
5648 : ExistingLangOpts(ExistingLangOpts),
5649 ExistingTargetOpts(ExistingTargetOpts),
5650 ExistingPPOpts(ExistingPPOpts),
5651 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5652 StrictOptionMatches(StrictOptionMatches) {}
5653
5654 bool ReadLanguageOptions(const LangOptions &LangOpts,
5655 StringRef ModuleFilename, bool Complain,
5656 bool AllowCompatibleDifferences) override {
5657 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5658 Diags: nullptr, AllowCompatibleDifferences);
5659 }
5660
5661 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5662 StringRef ModuleFilename, bool Complain,
5663 bool AllowCompatibleDifferences) override {
5664 return checkTargetOptions(TargetOpts: ExistingTargetOpts, ExistingTargetOpts: TargetOpts, ModuleFilename,
5665 Diags: nullptr, AllowCompatibleDifferences);
5666 }
5667
5668 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5669 StringRef ModuleFilename,
5670 StringRef SpecificModuleCachePath,
5671 bool Complain) override {
5672 return checkModuleCachePath(VFS&: FileMgr.getVirtualFileSystem(),
5673 SpecificModuleCachePath,
5674 ExistingModuleCachePath, ModuleFilename,
5675 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts);
5676 }
5677
5678 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5679 StringRef ModuleFilename, bool ReadMacros,
5680 bool Complain,
5681 std::string &SuggestedPredefines) override {
5682 return checkPreprocessorOptions(
5683 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5684 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5685 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5686 : OptionValidateContradictions);
5687 }
5688 };
5689
5690} // namespace
5691
5692bool ASTReader::readASTFileControlBlock(
5693 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5694 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5695 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5696 unsigned ClientLoadCapabilities) {
5697 // Open the AST file.
5698 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5699 llvm::MemoryBuffer *Buffer =
5700 ModCache.getInMemoryModuleCache().lookupPCM(Filename);
5701 if (!Buffer) {
5702 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5703 // read again later, but we do not have the context here to determine if it
5704 // is safe to change the result of InMemoryModuleCache::getPCMState().
5705
5706 // FIXME: This allows use of the VFS; we do not allow use of the
5707 // VFS when actually loading a module.
5708 auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5709 if (!BufferOrErr)
5710 return true;
5711 OwnedBuffer = std::move(*BufferOrErr);
5712 Buffer = OwnedBuffer.get();
5713 }
5714
5715 // Initialize the stream
5716 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5717 BitstreamCursor Stream(Bytes);
5718
5719 // Sniff for the signature.
5720 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5721 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5722 return true;
5723 }
5724
5725 // Scan for the CONTROL_BLOCK_ID block.
5726 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5727 return true;
5728
5729 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5730 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5731 bool NeedsImports = Listener.needsImportVisitation();
5732 BitstreamCursor InputFilesCursor;
5733 uint64_t InputFilesOffsetBase = 0;
5734
5735 RecordData Record;
5736 std::string ModuleDir;
5737 bool DoneWithControlBlock = false;
5738 SmallString<0> PathBuf;
5739 PathBuf.reserve(N: 256);
5740 // Additional path buffer to use when multiple paths need to be resolved.
5741 // For example, when deserializing input files that contains a path that was
5742 // resolved from a vfs overlay and an external location.
5743 SmallString<0> AdditionalPathBuf;
5744 AdditionalPathBuf.reserve(N: 256);
5745 while (!DoneWithControlBlock) {
5746 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5747 if (!MaybeEntry) {
5748 // FIXME this drops the error on the floor.
5749 consumeError(Err: MaybeEntry.takeError());
5750 return true;
5751 }
5752 llvm::BitstreamEntry Entry = MaybeEntry.get();
5753
5754 switch (Entry.Kind) {
5755 case llvm::BitstreamEntry::SubBlock: {
5756 switch (Entry.ID) {
5757 case OPTIONS_BLOCK_ID: {
5758 std::string IgnoredSuggestedPredefines;
5759 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
5760 /*AllowCompatibleConfigurationMismatch*/ false,
5761 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
5762 return true;
5763 break;
5764 }
5765
5766 case INPUT_FILES_BLOCK_ID:
5767 InputFilesCursor = Stream;
5768 if (llvm::Error Err = Stream.SkipBlock()) {
5769 // FIXME this drops the error on the floor.
5770 consumeError(Err: std::move(Err));
5771 return true;
5772 }
5773 if (NeedsInputFiles &&
5774 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
5775 return true;
5776 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5777 break;
5778
5779 default:
5780 if (llvm::Error Err = Stream.SkipBlock()) {
5781 // FIXME this drops the error on the floor.
5782 consumeError(Err: std::move(Err));
5783 return true;
5784 }
5785 break;
5786 }
5787
5788 continue;
5789 }
5790
5791 case llvm::BitstreamEntry::EndBlock:
5792 DoneWithControlBlock = true;
5793 break;
5794
5795 case llvm::BitstreamEntry::Error:
5796 return true;
5797
5798 case llvm::BitstreamEntry::Record:
5799 break;
5800 }
5801
5802 if (DoneWithControlBlock) break;
5803
5804 Record.clear();
5805 StringRef Blob;
5806 Expected<unsigned> MaybeRecCode =
5807 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5808 if (!MaybeRecCode) {
5809 // FIXME this drops the error.
5810 return Failure;
5811 }
5812 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5813 case METADATA:
5814 if (Record[0] != VERSION_MAJOR)
5815 return true;
5816 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
5817 return true;
5818 break;
5819 case MODULE_NAME:
5820 Listener.ReadModuleName(ModuleName: Blob);
5821 break;
5822 case MODULE_DIRECTORY:
5823 ModuleDir = std::string(Blob);
5824 break;
5825 case MODULE_MAP_FILE: {
5826 unsigned Idx = 0;
5827 std::string PathStr = ReadString(Record, Idx);
5828 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
5829 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
5830 break;
5831 }
5832 case INPUT_FILE_OFFSETS: {
5833 if (!NeedsInputFiles)
5834 break;
5835
5836 unsigned NumInputFiles = Record[0];
5837 unsigned NumUserFiles = Record[1];
5838 const llvm::support::unaligned_uint64_t *InputFileOffs =
5839 (const llvm::support::unaligned_uint64_t *)Blob.data();
5840 for (unsigned I = 0; I != NumInputFiles; ++I) {
5841 // Go find this input file.
5842 bool isSystemFile = I >= NumUserFiles;
5843
5844 if (isSystemFile && !NeedsSystemInputFiles)
5845 break; // the rest are system input files
5846
5847 BitstreamCursor &Cursor = InputFilesCursor;
5848 SavedStreamPosition SavedPosition(Cursor);
5849 if (llvm::Error Err =
5850 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
5851 // FIXME this drops errors on the floor.
5852 consumeError(Err: std::move(Err));
5853 }
5854
5855 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5856 if (!MaybeCode) {
5857 // FIXME this drops errors on the floor.
5858 consumeError(Err: MaybeCode.takeError());
5859 }
5860 unsigned Code = MaybeCode.get();
5861
5862 RecordData Record;
5863 StringRef Blob;
5864 bool shouldContinue = false;
5865 Expected<unsigned> MaybeRecordType =
5866 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
5867 if (!MaybeRecordType) {
5868 // FIXME this drops errors on the floor.
5869 consumeError(Err: MaybeRecordType.takeError());
5870 }
5871 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5872 case INPUT_FILE_HASH:
5873 break;
5874 case INPUT_FILE:
5875 bool Overridden = static_cast<bool>(Record[3]);
5876 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
5877 getUnresolvedInputFilenames(Record, InputBlob: Blob);
5878 auto FilenameAsRequestedBuf = ResolveImportedPath(
5879 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
5880 StringRef Filename;
5881 if (UnresolvedFilename.empty())
5882 Filename = *FilenameAsRequestedBuf;
5883 else {
5884 auto FilenameBuf = ResolveImportedPath(
5885 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
5886 Filename = *FilenameBuf;
5887 }
5888 shouldContinue = Listener.visitInputFile(
5889 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
5890 /*IsExplicitModule=*/isExplicitModule: false);
5891 break;
5892 }
5893 if (!shouldContinue)
5894 break;
5895 }
5896 break;
5897 }
5898
5899 case IMPORT: {
5900 if (!NeedsImports)
5901 break;
5902
5903 unsigned Idx = 0;
5904 // Read information about the AST file.
5905
5906 // Skip Kind
5907 Idx++;
5908
5909 // Skip ImportLoc
5910 Idx++;
5911
5912 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
5913
5914 bool IsStandardCXXModule = Record[Idx++];
5915
5916 // In C++20 Modules, we don't record the path to imported
5917 // modules in the BMI files.
5918 if (IsStandardCXXModule) {
5919 Listener.visitImport(ModuleName, /*Filename=*/"");
5920 continue;
5921 }
5922
5923 // Skip Size and ModTime.
5924 Idx += 1 + 1;
5925 // Skip signature.
5926 Blob = Blob.substr(Start: ASTFileSignature::size);
5927
5928 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
5929 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
5930 Listener.visitImport(ModuleName, Filename: *Filename);
5931 break;
5932 }
5933
5934 default:
5935 // No other validation to perform.
5936 break;
5937 }
5938 }
5939
5940 // Look for module file extension blocks, if requested.
5941 if (FindModuleFileExtensions) {
5942 BitstreamCursor SavedStream = Stream;
5943 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
5944 bool DoneWithExtensionBlock = false;
5945 while (!DoneWithExtensionBlock) {
5946 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5947 if (!MaybeEntry) {
5948 // FIXME this drops the error.
5949 return true;
5950 }
5951 llvm::BitstreamEntry Entry = MaybeEntry.get();
5952
5953 switch (Entry.Kind) {
5954 case llvm::BitstreamEntry::SubBlock:
5955 if (llvm::Error Err = Stream.SkipBlock()) {
5956 // FIXME this drops the error on the floor.
5957 consumeError(Err: std::move(Err));
5958 return true;
5959 }
5960 continue;
5961
5962 case llvm::BitstreamEntry::EndBlock:
5963 DoneWithExtensionBlock = true;
5964 continue;
5965
5966 case llvm::BitstreamEntry::Error:
5967 return true;
5968
5969 case llvm::BitstreamEntry::Record:
5970 break;
5971 }
5972
5973 Record.clear();
5974 StringRef Blob;
5975 Expected<unsigned> MaybeRecCode =
5976 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5977 if (!MaybeRecCode) {
5978 // FIXME this drops the error.
5979 return true;
5980 }
5981 switch (MaybeRecCode.get()) {
5982 case EXTENSION_METADATA: {
5983 ModuleFileExtensionMetadata Metadata;
5984 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5985 return true;
5986
5987 Listener.readModuleFileExtension(Metadata);
5988 break;
5989 }
5990 }
5991 }
5992 }
5993 Stream = std::move(SavedStream);
5994 }
5995
5996 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5997 if (readUnhashedControlBlockImpl(
5998 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
5999 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6000 ValidateDiagnosticOptions) != Success)
6001 return true;
6002
6003 return false;
6004}
6005
6006bool ASTReader::isAcceptableASTFile(
6007 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6008 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6009 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
6010 StringRef ExistingModuleCachePath, bool RequireStrictOptionMatches) {
6011 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
6012 ExistingModuleCachePath, FileMgr,
6013 RequireStrictOptionMatches);
6014 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6015 /*FindModuleFileExtensions=*/false, Listener&: validator,
6016 /*ValidateDiagnosticOptions=*/true);
6017}
6018
6019llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
6020 unsigned ClientLoadCapabilities) {
6021 // Enter the submodule block.
6022 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
6023 return Err;
6024
6025 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6026 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6027 // If we don't know the top-level module, there's no point in doing qualified
6028 // lookup of its submodules; it won't find anything anywhere within this tree.
6029 // Let's skip that and avoid some string lookups.
6030 auto CreateModule = !KnowsTopLevelModule
6031 ? &ModuleMap::createModule
6032 : &ModuleMap::findOrCreateModuleFirst;
6033
6034 bool First = true;
6035 Module *CurrentModule = nullptr;
6036 RecordData Record;
6037 while (true) {
6038 Expected<llvm::BitstreamEntry> MaybeEntry =
6039 F.Stream.advanceSkippingSubblocks();
6040 if (!MaybeEntry)
6041 return MaybeEntry.takeError();
6042 llvm::BitstreamEntry Entry = MaybeEntry.get();
6043
6044 switch (Entry.Kind) {
6045 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6046 case llvm::BitstreamEntry::Error:
6047 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6048 Fmt: "malformed block record in AST file");
6049 case llvm::BitstreamEntry::EndBlock:
6050 return llvm::Error::success();
6051 case llvm::BitstreamEntry::Record:
6052 // The interesting case.
6053 break;
6054 }
6055
6056 // Read a record.
6057 StringRef Blob;
6058 Record.clear();
6059 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6060 if (!MaybeKind)
6061 return MaybeKind.takeError();
6062 unsigned Kind = MaybeKind.get();
6063
6064 if ((Kind == SUBMODULE_METADATA) != First)
6065 return llvm::createStringError(
6066 EC: std::errc::illegal_byte_sequence,
6067 Fmt: "submodule metadata record should be at beginning of block");
6068 First = false;
6069
6070 // Submodule information is only valid if we have a current module.
6071 // FIXME: Should we error on these cases?
6072 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
6073 Kind != SUBMODULE_DEFINITION)
6074 continue;
6075
6076 switch (Kind) {
6077 default: // Default behavior: ignore.
6078 break;
6079
6080 case SUBMODULE_DEFINITION: {
6081 if (Record.size() < 13)
6082 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6083 Fmt: "malformed module definition");
6084
6085 StringRef Name = Blob;
6086 unsigned Idx = 0;
6087 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6088 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6089 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6090 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6091 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6092 bool IsFramework = Record[Idx++];
6093 bool IsExplicit = Record[Idx++];
6094 bool IsSystem = Record[Idx++];
6095 bool IsExternC = Record[Idx++];
6096 bool InferSubmodules = Record[Idx++];
6097 bool InferExplicitSubmodules = Record[Idx++];
6098 bool InferExportWildcard = Record[Idx++];
6099 bool ConfigMacrosExhaustive = Record[Idx++];
6100 bool ModuleMapIsPrivate = Record[Idx++];
6101 bool NamedModuleHasInit = Record[Idx++];
6102
6103 Module *ParentModule = nullptr;
6104 if (Parent)
6105 ParentModule = getSubmodule(GlobalID: Parent);
6106
6107 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6108 args&: IsFramework, args&: IsExplicit);
6109
6110 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6111 if (GlobalIndex >= SubmodulesLoaded.size() ||
6112 SubmodulesLoaded[GlobalIndex])
6113 return llvm::createStringError(EC: std::errc::invalid_argument,
6114 Fmt: "too many submodules");
6115
6116 if (!ParentModule) {
6117 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
6118 // Don't emit module relocation error if we have -fno-validate-pch
6119 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6120 DisableValidationForModuleKind::Module)) {
6121 assert(CurFile != F.File && "ModuleManager did not de-duplicate");
6122
6123 Diag(DiagID: diag::err_module_file_conflict)
6124 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
6125 << F.File.getName();
6126
6127 auto CurModMapFile =
6128 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6129 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6130 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6131 Diag(DiagID: diag::note_module_file_conflict)
6132 << CurModMapFile->getName() << ModMapFile->getName();
6133
6134 return llvm::make_error<AlreadyReportedDiagnosticError>();
6135 }
6136 }
6137
6138 F.DidReadTopLevelSubmodule = true;
6139 CurrentModule->setASTFile(F.File);
6140 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6141 }
6142
6143 CurrentModule->Kind = Kind;
6144 // Note that we may be rewriting an existing location and it is important
6145 // to keep doing that. In particular, we would like to prefer a
6146 // `DefinitionLoc` loaded from the module file instead of the location
6147 // created in the current source manager, because it allows the new
6148 // location to be marked as "unaffecting" when writing and avoid creating
6149 // duplicate locations for the same module map file.
6150 CurrentModule->DefinitionLoc = DefinitionLoc;
6151 CurrentModule->Signature = F.Signature;
6152 CurrentModule->IsFromModuleFile = true;
6153 if (InferredAllowedBy.isValid())
6154 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6155 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6156 CurrentModule->IsExternC = IsExternC;
6157 CurrentModule->InferSubmodules = InferSubmodules;
6158 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6159 CurrentModule->InferExportWildcard = InferExportWildcard;
6160 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6161 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6162 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6163 if (DeserializationListener)
6164 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6165
6166 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6167
6168 // Clear out data that will be replaced by what is in the module file.
6169 CurrentModule->LinkLibraries.clear();
6170 CurrentModule->ConfigMacros.clear();
6171 CurrentModule->UnresolvedConflicts.clear();
6172 CurrentModule->Conflicts.clear();
6173
6174 // The module is available unless it's missing a requirement; relevant
6175 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6176 // Missing headers that were present when the module was built do not
6177 // make it unavailable -- if we got this far, this must be an explicitly
6178 // imported module file.
6179 CurrentModule->Requirements.clear();
6180 CurrentModule->MissingHeaders.clear();
6181 CurrentModule->IsUnimportable =
6182 ParentModule && ParentModule->IsUnimportable;
6183 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6184 break;
6185 }
6186
6187 case SUBMODULE_UMBRELLA_HEADER: {
6188 // FIXME: This doesn't work for framework modules as `Filename` is the
6189 // name as written in the module file and does not include
6190 // `Headers/`, so this path will never exist.
6191 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6192 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename: *Filename)) {
6193 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6194 // FIXME: NameAsWritten
6195 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6196 }
6197 // Note that it's too late at this point to return out of date if the
6198 // name from the PCM doesn't match up with the one in the module map,
6199 // but also quite unlikely since we will have already checked the
6200 // modification time and size of the module map file itself.
6201 }
6202 break;
6203 }
6204
6205 case SUBMODULE_HEADER:
6206 case SUBMODULE_EXCLUDED_HEADER:
6207 case SUBMODULE_PRIVATE_HEADER:
6208 // We lazily associate headers with their modules via the HeaderInfo table.
6209 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6210 // of complete filenames or remove it entirely.
6211 break;
6212
6213 case SUBMODULE_TEXTUAL_HEADER:
6214 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6215 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6216 // them here.
6217 break;
6218
6219 case SUBMODULE_TOPHEADER: {
6220 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6221 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6222 break;
6223 }
6224
6225 case SUBMODULE_UMBRELLA_DIR: {
6226 // See comments in SUBMODULE_UMBRELLA_HEADER
6227 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6228 if (auto Umbrella =
6229 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6230 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6231 // FIXME: NameAsWritten
6232 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6233 }
6234 }
6235 break;
6236 }
6237
6238 case SUBMODULE_METADATA: {
6239 F.BaseSubmoduleID = getTotalNumSubmodules();
6240 F.LocalNumSubmodules = Record[0];
6241 unsigned LocalBaseSubmoduleID = Record[1];
6242 if (F.LocalNumSubmodules > 0) {
6243 // Introduce the global -> local mapping for submodules within this
6244 // module.
6245 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
6246
6247 // Introduce the local -> global mapping for submodules within this
6248 // module.
6249 F.SubmoduleRemap.insertOrReplace(
6250 Val: std::make_pair(x&: LocalBaseSubmoduleID,
6251 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
6252
6253 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
6254 }
6255 break;
6256 }
6257
6258 case SUBMODULE_IMPORTS:
6259 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6260 UnresolvedModuleRef Unresolved;
6261 Unresolved.File = &F;
6262 Unresolved.Mod = CurrentModule;
6263 Unresolved.ID = Record[Idx];
6264 Unresolved.Kind = UnresolvedModuleRef::Import;
6265 Unresolved.IsWildcard = false;
6266 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6267 }
6268 break;
6269
6270 case SUBMODULE_AFFECTING_MODULES:
6271 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6272 UnresolvedModuleRef Unresolved;
6273 Unresolved.File = &F;
6274 Unresolved.Mod = CurrentModule;
6275 Unresolved.ID = Record[Idx];
6276 Unresolved.Kind = UnresolvedModuleRef::Affecting;
6277 Unresolved.IsWildcard = false;
6278 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6279 }
6280 break;
6281
6282 case SUBMODULE_EXPORTS:
6283 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6284 UnresolvedModuleRef Unresolved;
6285 Unresolved.File = &F;
6286 Unresolved.Mod = CurrentModule;
6287 Unresolved.ID = Record[Idx];
6288 Unresolved.Kind = UnresolvedModuleRef::Export;
6289 Unresolved.IsWildcard = Record[Idx + 1];
6290 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6291 }
6292
6293 // Once we've loaded the set of exports, there's no reason to keep
6294 // the parsed, unresolved exports around.
6295 CurrentModule->UnresolvedExports.clear();
6296 break;
6297
6298 case SUBMODULE_REQUIRES:
6299 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6300 Target: PP.getTargetInfo());
6301 break;
6302
6303 case SUBMODULE_LINK_LIBRARY:
6304 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6305 CurrentModule->LinkLibraries.push_back(
6306 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6307 break;
6308
6309 case SUBMODULE_CONFIG_MACRO:
6310 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6311 break;
6312
6313 case SUBMODULE_CONFLICT: {
6314 UnresolvedModuleRef Unresolved;
6315 Unresolved.File = &F;
6316 Unresolved.Mod = CurrentModule;
6317 Unresolved.ID = Record[0];
6318 Unresolved.Kind = UnresolvedModuleRef::Conflict;
6319 Unresolved.IsWildcard = false;
6320 Unresolved.String = Blob;
6321 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6322 break;
6323 }
6324
6325 case SUBMODULE_INITIALIZERS: {
6326 if (!ContextObj)
6327 break;
6328 SmallVector<GlobalDeclID, 16> Inits;
6329 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6330 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6331 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6332 break;
6333 }
6334
6335 case SUBMODULE_EXPORT_AS:
6336 CurrentModule->ExportAsModule = Blob.str();
6337 ModMap.addLinkAsDependency(Mod: CurrentModule);
6338 break;
6339 }
6340 }
6341}
6342
6343/// Parse the record that corresponds to a LangOptions data
6344/// structure.
6345///
6346/// This routine parses the language options from the AST file and then gives
6347/// them to the AST listener if one is set.
6348///
6349/// \returns true if the listener deems the file unacceptable, false otherwise.
6350bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6351 StringRef ModuleFilename, bool Complain,
6352 ASTReaderListener &Listener,
6353 bool AllowCompatibleDifferences) {
6354 LangOptions LangOpts;
6355 unsigned Idx = 0;
6356#define LANGOPT(Name, Bits, Default, Description) \
6357 LangOpts.Name = Record[Idx++];
6358#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
6359 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6360#include "clang/Basic/LangOptions.def"
6361#define SANITIZER(NAME, ID) \
6362 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6363#include "clang/Basic/Sanitizers.def"
6364
6365 for (unsigned N = Record[Idx++]; N; --N)
6366 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6367
6368 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6369 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6370 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6371
6372 LangOpts.CurrentModule = ReadString(Record, Idx);
6373
6374 // Comment options.
6375 for (unsigned N = Record[Idx++]; N; --N) {
6376 LangOpts.CommentOpts.BlockCommandNames.push_back(
6377 x: ReadString(Record, Idx));
6378 }
6379 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6380
6381 // OpenMP offloading options.
6382 for (unsigned N = Record[Idx++]; N; --N) {
6383 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6384 }
6385
6386 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6387
6388 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6389 AllowCompatibleDifferences);
6390}
6391
6392bool ASTReader::ParseTargetOptions(const RecordData &Record,
6393 StringRef ModuleFilename, bool Complain,
6394 ASTReaderListener &Listener,
6395 bool AllowCompatibleDifferences) {
6396 unsigned Idx = 0;
6397 TargetOptions TargetOpts;
6398 TargetOpts.Triple = ReadString(Record, Idx);
6399 TargetOpts.CPU = ReadString(Record, Idx);
6400 TargetOpts.TuneCPU = ReadString(Record, Idx);
6401 TargetOpts.ABI = ReadString(Record, Idx);
6402 for (unsigned N = Record[Idx++]; N; --N) {
6403 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6404 }
6405 for (unsigned N = Record[Idx++]; N; --N) {
6406 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6407 }
6408
6409 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6410 AllowCompatibleDifferences);
6411}
6412
6413bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6414 StringRef ModuleFilename, bool Complain,
6415 ASTReaderListener &Listener) {
6416 DiagnosticOptions DiagOpts;
6417 unsigned Idx = 0;
6418#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6419#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6420 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6421#include "clang/Basic/DiagnosticOptions.def"
6422
6423 for (unsigned N = Record[Idx++]; N; --N)
6424 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6425 for (unsigned N = Record[Idx++]; N; --N)
6426 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6427
6428 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6429}
6430
6431bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6432 ASTReaderListener &Listener) {
6433 FileSystemOptions FSOpts;
6434 unsigned Idx = 0;
6435 FSOpts.WorkingDir = ReadString(Record, Idx);
6436 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6437}
6438
6439bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6440 StringRef ModuleFilename,
6441 bool Complain,
6442 ASTReaderListener &Listener) {
6443 HeaderSearchOptions HSOpts;
6444 unsigned Idx = 0;
6445 HSOpts.Sysroot = ReadString(Record, Idx);
6446
6447 HSOpts.ResourceDir = ReadString(Record, Idx);
6448 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6449 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6450 HSOpts.DisableModuleHash = Record[Idx++];
6451 HSOpts.ImplicitModuleMaps = Record[Idx++];
6452 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6453 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6454 HSOpts.UseBuiltinIncludes = Record[Idx++];
6455 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6456 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6457 HSOpts.UseLibcxx = Record[Idx++];
6458 std::string SpecificModuleCachePath = ReadString(Record, Idx);
6459
6460 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename,
6461 SpecificModuleCachePath, Complain);
6462}
6463
6464bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6465 ASTReaderListener &Listener) {
6466 HeaderSearchOptions HSOpts;
6467 unsigned Idx = 0;
6468
6469 // Include entries.
6470 for (unsigned N = Record[Idx++]; N; --N) {
6471 std::string Path = ReadString(Record, Idx);
6472 frontend::IncludeDirGroup Group
6473 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6474 bool IsFramework = Record[Idx++];
6475 bool IgnoreSysRoot = Record[Idx++];
6476 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6477 args&: IgnoreSysRoot);
6478 }
6479
6480 // System header prefixes.
6481 for (unsigned N = Record[Idx++]; N; --N) {
6482 std::string Prefix = ReadString(Record, Idx);
6483 bool IsSystemHeader = Record[Idx++];
6484 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6485 }
6486
6487 // VFS overlay files.
6488 for (unsigned N = Record[Idx++]; N; --N) {
6489 std::string VFSOverlayFile = ReadString(Record, Idx);
6490 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6491 }
6492
6493 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6494}
6495
6496bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6497 StringRef ModuleFilename,
6498 bool Complain,
6499 ASTReaderListener &Listener,
6500 std::string &SuggestedPredefines) {
6501 PreprocessorOptions PPOpts;
6502 unsigned Idx = 0;
6503
6504 // Macro definitions/undefs
6505 bool ReadMacros = Record[Idx++];
6506 if (ReadMacros) {
6507 for (unsigned N = Record[Idx++]; N; --N) {
6508 std::string Macro = ReadString(Record, Idx);
6509 bool IsUndef = Record[Idx++];
6510 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6511 }
6512 }
6513
6514 // Includes
6515 for (unsigned N = Record[Idx++]; N; --N) {
6516 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6517 }
6518
6519 // Macro Includes
6520 for (unsigned N = Record[Idx++]; N; --N) {
6521 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6522 }
6523
6524 PPOpts.UsePredefines = Record[Idx++];
6525 PPOpts.DetailedRecord = Record[Idx++];
6526 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6527 PPOpts.ObjCXXARCStandardLibrary =
6528 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6529 SuggestedPredefines.clear();
6530 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6531 Complain, SuggestedPredefines);
6532}
6533
6534std::pair<ModuleFile *, unsigned>
6535ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6536 GlobalPreprocessedEntityMapType::iterator
6537 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6538 assert(I != GlobalPreprocessedEntityMap.end() &&
6539 "Corrupted global preprocessed entity map");
6540 ModuleFile *M = I->second;
6541 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6542 return std::make_pair(x&: M, y&: LocalIndex);
6543}
6544
6545llvm::iterator_range<PreprocessingRecord::iterator>
6546ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6547 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6548 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6549 count: Mod.NumPreprocessedEntities);
6550
6551 return llvm::make_range(x: PreprocessingRecord::iterator(),
6552 y: PreprocessingRecord::iterator());
6553}
6554
6555bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6556 unsigned int ClientLoadCapabilities) {
6557 return ClientLoadCapabilities & ARR_OutOfDate &&
6558 !getModuleManager()
6559 .getModuleCache()
6560 .getInMemoryModuleCache()
6561 .isPCMFinal(Filename: ModuleFileName);
6562}
6563
6564llvm::iterator_range<ASTReader::ModuleDeclIterator>
6565ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6566 return llvm::make_range(
6567 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6568 y: ModuleDeclIterator(this, &Mod,
6569 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6570}
6571
6572SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6573 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6574 assert(I != GlobalSkippedRangeMap.end() &&
6575 "Corrupted global skipped range map");
6576 ModuleFile *M = I->second;
6577 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6578 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6579 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6580 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6581 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6582 assert(Range.isValid());
6583 return Range;
6584}
6585
6586PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6587 PreprocessedEntityID PPID = Index+1;
6588 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6589 ModuleFile &M = *PPInfo.first;
6590 unsigned LocalIndex = PPInfo.second;
6591 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6592
6593 if (!PP.getPreprocessingRecord()) {
6594 Error(Msg: "no preprocessing record");
6595 return nullptr;
6596 }
6597
6598 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6599 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6600 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6601 Error(Err: std::move(Err));
6602 return nullptr;
6603 }
6604
6605 Expected<llvm::BitstreamEntry> MaybeEntry =
6606 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6607 if (!MaybeEntry) {
6608 Error(Err: MaybeEntry.takeError());
6609 return nullptr;
6610 }
6611 llvm::BitstreamEntry Entry = MaybeEntry.get();
6612
6613 if (Entry.Kind != llvm::BitstreamEntry::Record)
6614 return nullptr;
6615
6616 // Read the record.
6617 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6618 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6619 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6620 StringRef Blob;
6621 RecordData Record;
6622 Expected<unsigned> MaybeRecType =
6623 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6624 if (!MaybeRecType) {
6625 Error(Err: MaybeRecType.takeError());
6626 return nullptr;
6627 }
6628 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6629 case PPD_MACRO_EXPANSION: {
6630 bool isBuiltin = Record[0];
6631 IdentifierInfo *Name = nullptr;
6632 MacroDefinitionRecord *Def = nullptr;
6633 if (isBuiltin)
6634 Name = getLocalIdentifier(M, LocalID: Record[1]);
6635 else {
6636 PreprocessedEntityID GlobalID =
6637 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6638 Def = cast<MacroDefinitionRecord>(
6639 Val: PPRec.getLoadedPreprocessedEntity(Index: GlobalID - 1));
6640 }
6641
6642 MacroExpansion *ME;
6643 if (isBuiltin)
6644 ME = new (PPRec) MacroExpansion(Name, Range);
6645 else
6646 ME = new (PPRec) MacroExpansion(Def, Range);
6647
6648 return ME;
6649 }
6650
6651 case PPD_MACRO_DEFINITION: {
6652 // Decode the identifier info and then check again; if the macro is
6653 // still defined and associated with the identifier,
6654 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6655 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6656
6657 if (DeserializationListener)
6658 DeserializationListener->MacroDefinitionRead(PPID, MD);
6659
6660 return MD;
6661 }
6662
6663 case PPD_INCLUSION_DIRECTIVE: {
6664 const char *FullFileNameStart = Blob.data() + Record[0];
6665 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6666 OptionalFileEntryRef File;
6667 if (!FullFileName.empty())
6668 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6669
6670 // FIXME: Stable encoding
6671 InclusionDirective::InclusionKind Kind
6672 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6673 InclusionDirective *ID
6674 = new (PPRec) InclusionDirective(PPRec, Kind,
6675 StringRef(Blob.data(), Record[0]),
6676 Record[1], Record[3],
6677 File,
6678 Range);
6679 return ID;
6680 }
6681 }
6682
6683 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6684}
6685
6686/// Find the next module that contains entities and return the ID
6687/// of the first entry.
6688///
6689/// \param SLocMapI points at a chunk of a module that contains no
6690/// preprocessed entities or the entities it contains are not the ones we are
6691/// looking for.
6692PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6693 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6694 ++SLocMapI;
6695 for (GlobalSLocOffsetMapType::const_iterator
6696 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6697 ModuleFile &M = *SLocMapI->second;
6698 if (M.NumPreprocessedEntities)
6699 return M.BasePreprocessedEntityID;
6700 }
6701
6702 return getTotalNumPreprocessedEntities();
6703}
6704
6705namespace {
6706
6707struct PPEntityComp {
6708 const ASTReader &Reader;
6709 ModuleFile &M;
6710
6711 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6712
6713 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6714 SourceLocation LHS = getLoc(PPE: L);
6715 SourceLocation RHS = getLoc(PPE: R);
6716 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6717 }
6718
6719 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6720 SourceLocation LHS = getLoc(PPE: L);
6721 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6722 }
6723
6724 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6725 SourceLocation RHS = getLoc(PPE: R);
6726 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6727 }
6728
6729 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6730 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
6731 }
6732};
6733
6734} // namespace
6735
6736PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6737 bool EndsAfter) const {
6738 if (SourceMgr.isLocalSourceLocation(Loc))
6739 return getTotalNumPreprocessedEntities();
6740
6741 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6742 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6743 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6744 "Corrupted global sloc offset map");
6745
6746 if (SLocMapI->second->NumPreprocessedEntities == 0)
6747 return findNextPreprocessedEntity(SLocMapI);
6748
6749 ModuleFile &M = *SLocMapI->second;
6750
6751 using pp_iterator = const PPEntityOffset *;
6752
6753 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6754 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6755
6756 size_t Count = M.NumPreprocessedEntities;
6757 size_t Half;
6758 pp_iterator First = pp_begin;
6759 pp_iterator PPI;
6760
6761 if (EndsAfter) {
6762 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
6763 comp: PPEntityComp(*this, M));
6764 } else {
6765 // Do a binary search manually instead of using std::lower_bound because
6766 // The end locations of entities may be unordered (when a macro expansion
6767 // is inside another macro argument), but for this case it is not important
6768 // whether we get the first macro expansion or its containing macro.
6769 while (Count > 0) {
6770 Half = Count / 2;
6771 PPI = First;
6772 std::advance(i&: PPI, n: Half);
6773 if (SourceMgr.isBeforeInTranslationUnit(
6774 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
6775 First = PPI;
6776 ++First;
6777 Count = Count - Half - 1;
6778 } else
6779 Count = Half;
6780 }
6781 }
6782
6783 if (PPI == pp_end)
6784 return findNextPreprocessedEntity(SLocMapI);
6785
6786 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6787}
6788
6789/// Returns a pair of [Begin, End) indices of preallocated
6790/// preprocessed entities that \arg Range encompasses.
6791std::pair<unsigned, unsigned>
6792 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6793 if (Range.isInvalid())
6794 return std::make_pair(x: 0,y: 0);
6795 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6796
6797 PreprocessedEntityID BeginID =
6798 findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
6799 PreprocessedEntityID EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
6800 return std::make_pair(x&: BeginID, y&: EndID);
6801}
6802
6803/// Optionally returns true or false if the preallocated preprocessed
6804/// entity with index \arg Index came from file \arg FID.
6805std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6806 FileID FID) {
6807 if (FID.isInvalid())
6808 return false;
6809
6810 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6811 ModuleFile &M = *PPInfo.first;
6812 unsigned LocalIndex = PPInfo.second;
6813 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6814
6815 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
6816 if (Loc.isInvalid())
6817 return false;
6818
6819 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
6820 return true;
6821 else
6822 return false;
6823}
6824
6825namespace {
6826
6827 /// Visitor used to search for information about a header file.
6828 class HeaderFileInfoVisitor {
6829 FileEntryRef FE;
6830 std::optional<HeaderFileInfo> HFI;
6831
6832 public:
6833 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6834
6835 bool operator()(ModuleFile &M) {
6836 HeaderFileInfoLookupTable *Table
6837 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6838 if (!Table)
6839 return false;
6840
6841 // Look in the on-disk hash table for an entry for this file name.
6842 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
6843 if (Pos == Table->end())
6844 return false;
6845
6846 HFI = *Pos;
6847 return true;
6848 }
6849
6850 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6851 };
6852
6853} // namespace
6854
6855HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6856 HeaderFileInfoVisitor Visitor(FE);
6857 ModuleMgr.visit(Visitor);
6858 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6859 return *HFI;
6860
6861 return HeaderFileInfo();
6862}
6863
6864void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6865 using DiagState = DiagnosticsEngine::DiagState;
6866 SmallVector<DiagState *, 32> DiagStates;
6867
6868 for (ModuleFile &F : ModuleMgr) {
6869 unsigned Idx = 0;
6870 auto &Record = F.PragmaDiagMappings;
6871 if (Record.empty())
6872 continue;
6873
6874 DiagStates.clear();
6875
6876 auto ReadDiagState = [&](const DiagState &BasedOn,
6877 bool IncludeNonPragmaStates) {
6878 unsigned BackrefID = Record[Idx++];
6879 if (BackrefID != 0)
6880 return DiagStates[BackrefID - 1];
6881
6882 // A new DiagState was created here.
6883 Diag.DiagStates.push_back(x: BasedOn);
6884 DiagState *NewState = &Diag.DiagStates.back();
6885 DiagStates.push_back(Elt: NewState);
6886 unsigned Size = Record[Idx++];
6887 assert(Idx + Size * 2 <= Record.size() &&
6888 "Invalid data, not enough diag/map pairs");
6889 while (Size--) {
6890 unsigned DiagID = Record[Idx++];
6891 DiagnosticMapping NewMapping =
6892 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
6893 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6894 continue;
6895
6896 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
6897
6898 // If this mapping was specified as a warning but the severity was
6899 // upgraded due to diagnostic settings, simulate the current diagnostic
6900 // settings (and use a warning).
6901 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6902 NewMapping.setSeverity(diag::Severity::Warning);
6903 NewMapping.setUpgradedFromWarning(false);
6904 }
6905
6906 Mapping = NewMapping;
6907 }
6908 return NewState;
6909 };
6910
6911 // Read the first state.
6912 DiagState *FirstState;
6913 if (F.Kind == MK_ImplicitModule) {
6914 // Implicitly-built modules are reused with different diagnostic
6915 // settings. Use the initial diagnostic state from Diag to simulate this
6916 // compilation's diagnostic settings.
6917 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6918 DiagStates.push_back(Elt: FirstState);
6919
6920 // Skip the initial diagnostic state from the serialized module.
6921 assert(Record[1] == 0 &&
6922 "Invalid data, unexpected backref in initial state");
6923 Idx = 3 + Record[2] * 2;
6924 assert(Idx < Record.size() &&
6925 "Invalid data, not enough state change pairs in initial state");
6926 } else if (F.isModule()) {
6927 // For an explicit module, preserve the flags from the module build
6928 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6929 // -Wblah flags.
6930 unsigned Flags = Record[Idx++];
6931 DiagState Initial(*Diag.getDiagnosticIDs());
6932 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6933 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6934 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6935 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6936 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6937 Initial.ExtBehavior = (diag::Severity)Flags;
6938 FirstState = ReadDiagState(Initial, true);
6939
6940 assert(F.OriginalSourceFileID.isValid());
6941
6942 // Set up the root buffer of the module to start with the initial
6943 // diagnostic state of the module itself, to cover files that contain no
6944 // explicit transitions (for which we did not serialize anything).
6945 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6946 .StateTransitions.push_back(Elt: {FirstState, 0});
6947 } else {
6948 // For prefix ASTs, start with whatever the user configured on the
6949 // command line.
6950 Idx++; // Skip flags.
6951 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6952 }
6953
6954 // Read the state transitions.
6955 unsigned NumLocations = Record[Idx++];
6956 while (NumLocations--) {
6957 assert(Idx < Record.size() &&
6958 "Invalid data, missing pragma diagnostic states");
6959 FileID FID = ReadFileID(F, Record, Idx);
6960 assert(FID.isValid() && "invalid FileID for transition");
6961 unsigned Transitions = Record[Idx++];
6962
6963 // Note that we don't need to set up Parent/ParentOffset here, because
6964 // we won't be changing the diagnostic state within imported FileIDs
6965 // (other than perhaps appending to the main source file, which has no
6966 // parent).
6967 auto &F = Diag.DiagStatesByLoc.Files[FID];
6968 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
6969 for (unsigned I = 0; I != Transitions; ++I) {
6970 unsigned Offset = Record[Idx++];
6971 auto *State = ReadDiagState(*FirstState, false);
6972 F.StateTransitions.push_back(Elt: {State, Offset});
6973 }
6974 }
6975
6976 // Read the final state.
6977 assert(Idx < Record.size() &&
6978 "Invalid data, missing final pragma diagnostic state");
6979 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6980 auto *CurState = ReadDiagState(*FirstState, false);
6981
6982 if (!F.isModule()) {
6983 Diag.DiagStatesByLoc.CurDiagState = CurState;
6984 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6985
6986 // Preserve the property that the imaginary root file describes the
6987 // current state.
6988 FileID NullFile;
6989 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6990 if (T.empty())
6991 T.push_back(Elt: {CurState, 0});
6992 else
6993 T[0].State = CurState;
6994 }
6995
6996 // Don't try to read these mappings again.
6997 Record.clear();
6998 }
6999}
7000
7001/// Get the correct cursor and offset for loading a type.
7002ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7003 auto [M, Index] = translateTypeIDToIndex(ID);
7004 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7005 M->DeclsBlockStartOffset);
7006}
7007
7008static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7009 switch (code) {
7010#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7011 case TYPE_##CODE_ID: return Type::CLASS_ID;
7012#include "clang/Serialization/TypeBitCodes.def"
7013 default:
7014 return std::nullopt;
7015 }
7016}
7017
7018/// Read and return the type with the given index..
7019///
7020/// The index is the type ID, shifted and minus the number of predefs. This
7021/// routine actually reads the record corresponding to the type at the given
7022/// location. It is a helper routine for GetType, which deals with reading type
7023/// IDs.
7024QualType ASTReader::readTypeRecord(TypeID ID) {
7025 assert(ContextObj && "reading type with no AST context");
7026 ASTContext &Context = *ContextObj;
7027 RecordLocation Loc = TypeCursorForIndex(ID);
7028 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7029
7030 // Keep track of where we are in the stream, then jump back there
7031 // after reading this type.
7032 SavedStreamPosition SavedPosition(DeclsCursor);
7033
7034 ReadingKindTracker ReadingKind(Read_Type, *this);
7035
7036 // Note that we are loading a type record.
7037 Deserializing AType(this);
7038
7039 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7040 Error(Err: std::move(Err));
7041 return QualType();
7042 }
7043 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7044 if (!RawCode) {
7045 Error(Err: RawCode.takeError());
7046 return QualType();
7047 }
7048
7049 ASTRecordReader Record(*this, *Loc.F);
7050 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7051 if (!Code) {
7052 Error(Err: Code.takeError());
7053 return QualType();
7054 }
7055 if (Code.get() == TYPE_EXT_QUAL) {
7056 QualType baseType = Record.readQualType();
7057 Qualifiers quals = Record.readQualifiers();
7058 return Context.getQualifiedType(T: baseType, Qs: quals);
7059 }
7060
7061 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7062 if (!maybeClass) {
7063 Error(Msg: "Unexpected code for type");
7064 return QualType();
7065 }
7066
7067 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7068 return TypeReader.read(kind: *maybeClass);
7069}
7070
7071namespace clang {
7072
7073class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7074 ASTRecordReader &Reader;
7075
7076 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7077 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7078
7079 TypeSourceInfo *GetTypeSourceInfo() {
7080 return Reader.readTypeSourceInfo();
7081 }
7082
7083 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7084 return Reader.readNestedNameSpecifierLoc();
7085 }
7086
7087 Attr *ReadAttr() {
7088 return Reader.readAttr();
7089 }
7090
7091public:
7092 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7093
7094 // We want compile-time assurance that we've enumerated all of
7095 // these, so unfortunately we have to declare them first, then
7096 // define them out-of-line.
7097#define ABSTRACT_TYPELOC(CLASS, PARENT)
7098#define TYPELOC(CLASS, PARENT) \
7099 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7100#include "clang/AST/TypeLocNodes.def"
7101
7102 void VisitFunctionTypeLoc(FunctionTypeLoc);
7103 void VisitArrayTypeLoc(ArrayTypeLoc);
7104};
7105
7106} // namespace clang
7107
7108void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7109 // nothing to do
7110}
7111
7112void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7113 TL.setBuiltinLoc(readSourceLocation());
7114 if (TL.needsExtraLocalData()) {
7115 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7116 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7117 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7118 TL.setModeAttr(Reader.readInt());
7119 }
7120}
7121
7122void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7123 TL.setNameLoc(readSourceLocation());
7124}
7125
7126void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7127 TL.setStarLoc(readSourceLocation());
7128}
7129
7130void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7131 // nothing to do
7132}
7133
7134void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7135 // nothing to do
7136}
7137
7138void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7139 // nothing to do
7140}
7141
7142void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7143 TL.setExpansionLoc(readSourceLocation());
7144}
7145
7146void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7147 TL.setCaretLoc(readSourceLocation());
7148}
7149
7150void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7151 TL.setAmpLoc(readSourceLocation());
7152}
7153
7154void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7155 TL.setAmpAmpLoc(readSourceLocation());
7156}
7157
7158void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7159 TL.setStarLoc(readSourceLocation());
7160 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7161}
7162
7163void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7164 TL.setLBracketLoc(readSourceLocation());
7165 TL.setRBracketLoc(readSourceLocation());
7166 if (Reader.readBool())
7167 TL.setSizeExpr(Reader.readExpr());
7168 else
7169 TL.setSizeExpr(nullptr);
7170}
7171
7172void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7173 VisitArrayTypeLoc(TL);
7174}
7175
7176void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7177 VisitArrayTypeLoc(TL);
7178}
7179
7180void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7181 VisitArrayTypeLoc(TL);
7182}
7183
7184void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7185 DependentSizedArrayTypeLoc TL) {
7186 VisitArrayTypeLoc(TL);
7187}
7188
7189void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7190 DependentAddressSpaceTypeLoc TL) {
7191
7192 TL.setAttrNameLoc(readSourceLocation());
7193 TL.setAttrOperandParensRange(readSourceRange());
7194 TL.setAttrExprOperand(Reader.readExpr());
7195}
7196
7197void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7198 DependentSizedExtVectorTypeLoc TL) {
7199 TL.setNameLoc(readSourceLocation());
7200}
7201
7202void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7203 TL.setNameLoc(readSourceLocation());
7204}
7205
7206void TypeLocReader::VisitDependentVectorTypeLoc(
7207 DependentVectorTypeLoc TL) {
7208 TL.setNameLoc(readSourceLocation());
7209}
7210
7211void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7212 TL.setNameLoc(readSourceLocation());
7213}
7214
7215void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7216 TL.setAttrNameLoc(readSourceLocation());
7217 TL.setAttrOperandParensRange(readSourceRange());
7218 TL.setAttrRowOperand(Reader.readExpr());
7219 TL.setAttrColumnOperand(Reader.readExpr());
7220}
7221
7222void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7223 DependentSizedMatrixTypeLoc TL) {
7224 TL.setAttrNameLoc(readSourceLocation());
7225 TL.setAttrOperandParensRange(readSourceRange());
7226 TL.setAttrRowOperand(Reader.readExpr());
7227 TL.setAttrColumnOperand(Reader.readExpr());
7228}
7229
7230void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7231 TL.setLocalRangeBegin(readSourceLocation());
7232 TL.setLParenLoc(readSourceLocation());
7233 TL.setRParenLoc(readSourceLocation());
7234 TL.setExceptionSpecRange(readSourceRange());
7235 TL.setLocalRangeEnd(readSourceLocation());
7236 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7237 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7238 }
7239}
7240
7241void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7242 VisitFunctionTypeLoc(TL);
7243}
7244
7245void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7246 VisitFunctionTypeLoc(TL);
7247}
7248
7249void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7250 TL.setNameLoc(readSourceLocation());
7251}
7252
7253void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7254 TL.setNameLoc(readSourceLocation());
7255}
7256
7257void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7258 TL.setNameLoc(readSourceLocation());
7259}
7260
7261void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7262 TL.setTypeofLoc(readSourceLocation());
7263 TL.setLParenLoc(readSourceLocation());
7264 TL.setRParenLoc(readSourceLocation());
7265}
7266
7267void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7268 TL.setTypeofLoc(readSourceLocation());
7269 TL.setLParenLoc(readSourceLocation());
7270 TL.setRParenLoc(readSourceLocation());
7271 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7272}
7273
7274void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7275 TL.setDecltypeLoc(readSourceLocation());
7276 TL.setRParenLoc(readSourceLocation());
7277}
7278
7279void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7280 TL.setEllipsisLoc(readSourceLocation());
7281}
7282
7283void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7284 TL.setKWLoc(readSourceLocation());
7285 TL.setLParenLoc(readSourceLocation());
7286 TL.setRParenLoc(readSourceLocation());
7287 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7288}
7289
7290ConceptReference *ASTRecordReader::readConceptReference() {
7291 auto NNS = readNestedNameSpecifierLoc();
7292 auto TemplateKWLoc = readSourceLocation();
7293 auto ConceptNameLoc = readDeclarationNameInfo();
7294 auto FoundDecl = readDeclAs<NamedDecl>();
7295 auto NamedConcept = readDeclAs<ConceptDecl>();
7296 auto *CR = ConceptReference::Create(
7297 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7298 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7299 return CR;
7300}
7301
7302void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7303 TL.setNameLoc(readSourceLocation());
7304 if (Reader.readBool())
7305 TL.setConceptReference(Reader.readConceptReference());
7306 if (Reader.readBool())
7307 TL.setRParenLoc(readSourceLocation());
7308}
7309
7310void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7311 DeducedTemplateSpecializationTypeLoc TL) {
7312 TL.setTemplateNameLoc(readSourceLocation());
7313}
7314
7315void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7316 TL.setNameLoc(readSourceLocation());
7317}
7318
7319void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7320 TL.setNameLoc(readSourceLocation());
7321}
7322
7323void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7324 TL.setAttr(ReadAttr());
7325}
7326
7327void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7328 // Nothing to do
7329}
7330
7331void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7332 // Nothing to do.
7333}
7334
7335void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7336 HLSLAttributedResourceTypeLoc TL) {
7337 // Nothing to do.
7338}
7339
7340void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7341 // Nothing to do.
7342}
7343
7344void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7345 TL.setNameLoc(readSourceLocation());
7346}
7347
7348void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7349 SubstTemplateTypeParmTypeLoc TL) {
7350 TL.setNameLoc(readSourceLocation());
7351}
7352
7353void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7354 SubstTemplateTypeParmPackTypeLoc TL) {
7355 TL.setNameLoc(readSourceLocation());
7356}
7357
7358void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7359 TemplateSpecializationTypeLoc TL) {
7360 TL.setTemplateKeywordLoc(readSourceLocation());
7361 TL.setTemplateNameLoc(readSourceLocation());
7362 TL.setLAngleLoc(readSourceLocation());
7363 TL.setRAngleLoc(readSourceLocation());
7364 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7365 TL.setArgLocInfo(i,
7366 AI: Reader.readTemplateArgumentLocInfo(
7367 Kind: TL.getTypePtr()->template_arguments()[i].getKind()));
7368}
7369
7370void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7371 TL.setLParenLoc(readSourceLocation());
7372 TL.setRParenLoc(readSourceLocation());
7373}
7374
7375void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7376 TL.setElaboratedKeywordLoc(readSourceLocation());
7377 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7378}
7379
7380void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7381 TL.setNameLoc(readSourceLocation());
7382}
7383
7384void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7385 TL.setElaboratedKeywordLoc(readSourceLocation());
7386 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7387 TL.setNameLoc(readSourceLocation());
7388}
7389
7390void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7391 DependentTemplateSpecializationTypeLoc TL) {
7392 TL.setElaboratedKeywordLoc(readSourceLocation());
7393 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7394 TL.setTemplateKeywordLoc(readSourceLocation());
7395 TL.setTemplateNameLoc(readSourceLocation());
7396 TL.setLAngleLoc(readSourceLocation());
7397 TL.setRAngleLoc(readSourceLocation());
7398 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7399 TL.setArgLocInfo(i: I,
7400 AI: Reader.readTemplateArgumentLocInfo(
7401 Kind: TL.getTypePtr()->template_arguments()[I].getKind()));
7402}
7403
7404void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7405 TL.setEllipsisLoc(readSourceLocation());
7406}
7407
7408void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7409 TL.setNameLoc(readSourceLocation());
7410 TL.setNameEndLoc(readSourceLocation());
7411}
7412
7413void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7414 if (TL.getNumProtocols()) {
7415 TL.setProtocolLAngleLoc(readSourceLocation());
7416 TL.setProtocolRAngleLoc(readSourceLocation());
7417 }
7418 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7419 TL.setProtocolLoc(i, Loc: readSourceLocation());
7420}
7421
7422void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7423 TL.setHasBaseTypeAsWritten(Reader.readBool());
7424 TL.setTypeArgsLAngleLoc(readSourceLocation());
7425 TL.setTypeArgsRAngleLoc(readSourceLocation());
7426 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7427 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7428 TL.setProtocolLAngleLoc(readSourceLocation());
7429 TL.setProtocolRAngleLoc(readSourceLocation());
7430 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7431 TL.setProtocolLoc(i, Loc: readSourceLocation());
7432}
7433
7434void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7435 TL.setStarLoc(readSourceLocation());
7436}
7437
7438void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7439 TL.setKWLoc(readSourceLocation());
7440 TL.setLParenLoc(readSourceLocation());
7441 TL.setRParenLoc(readSourceLocation());
7442}
7443
7444void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7445 TL.setKWLoc(readSourceLocation());
7446}
7447
7448void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7449 TL.setNameLoc(readSourceLocation());
7450}
7451void TypeLocReader::VisitDependentBitIntTypeLoc(
7452 clang::DependentBitIntTypeLoc TL) {
7453 TL.setNameLoc(readSourceLocation());
7454}
7455
7456void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7457 TypeLocReader TLR(*this);
7458 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7459 TLR.Visit(TyLoc: TL);
7460}
7461
7462TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7463 QualType InfoTy = readType();
7464 if (InfoTy.isNull())
7465 return nullptr;
7466
7467 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7468 readTypeLoc(TL: TInfo->getTypeLoc());
7469 return TInfo;
7470}
7471
7472static unsigned getIndexForTypeID(serialization::TypeID ID) {
7473 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7474}
7475
7476static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7477 return ID >> 32;
7478}
7479
7480static bool isPredefinedType(serialization::TypeID ID) {
7481 // We don't need to erase the higher bits since if these bits are not 0,
7482 // it must be larger than NUM_PREDEF_TYPE_IDS.
7483 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7484}
7485
7486std::pair<ModuleFile *, unsigned>
7487ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7488 assert(!isPredefinedType(ID) &&
7489 "Predefined type shouldn't be in TypesLoaded");
7490 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7491 assert(ModuleFileIndex && "Untranslated Local Decl?");
7492
7493 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7494 assert(OwningModuleFile &&
7495 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7496
7497 return {OwningModuleFile,
7498 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7499}
7500
7501QualType ASTReader::GetType(TypeID ID) {
7502 assert(ContextObj && "reading type with no AST context");
7503 ASTContext &Context = *ContextObj;
7504
7505 unsigned FastQuals = ID & Qualifiers::FastMask;
7506
7507 if (isPredefinedType(ID)) {
7508 QualType T;
7509 unsigned Index = getIndexForTypeID(ID);
7510 switch ((PredefinedTypeIDs)Index) {
7511 case PREDEF_TYPE_LAST_ID:
7512 // We should never use this one.
7513 llvm_unreachable("Invalid predefined type");
7514 break;
7515 case PREDEF_TYPE_NULL_ID:
7516 return QualType();
7517 case PREDEF_TYPE_VOID_ID:
7518 T = Context.VoidTy;
7519 break;
7520 case PREDEF_TYPE_BOOL_ID:
7521 T = Context.BoolTy;
7522 break;
7523 case PREDEF_TYPE_CHAR_U_ID:
7524 case PREDEF_TYPE_CHAR_S_ID:
7525 // FIXME: Check that the signedness of CharTy is correct!
7526 T = Context.CharTy;
7527 break;
7528 case PREDEF_TYPE_UCHAR_ID:
7529 T = Context.UnsignedCharTy;
7530 break;
7531 case PREDEF_TYPE_USHORT_ID:
7532 T = Context.UnsignedShortTy;
7533 break;
7534 case PREDEF_TYPE_UINT_ID:
7535 T = Context.UnsignedIntTy;
7536 break;
7537 case PREDEF_TYPE_ULONG_ID:
7538 T = Context.UnsignedLongTy;
7539 break;
7540 case PREDEF_TYPE_ULONGLONG_ID:
7541 T = Context.UnsignedLongLongTy;
7542 break;
7543 case PREDEF_TYPE_UINT128_ID:
7544 T = Context.UnsignedInt128Ty;
7545 break;
7546 case PREDEF_TYPE_SCHAR_ID:
7547 T = Context.SignedCharTy;
7548 break;
7549 case PREDEF_TYPE_WCHAR_ID:
7550 T = Context.WCharTy;
7551 break;
7552 case PREDEF_TYPE_SHORT_ID:
7553 T = Context.ShortTy;
7554 break;
7555 case PREDEF_TYPE_INT_ID:
7556 T = Context.IntTy;
7557 break;
7558 case PREDEF_TYPE_LONG_ID:
7559 T = Context.LongTy;
7560 break;
7561 case PREDEF_TYPE_LONGLONG_ID:
7562 T = Context.LongLongTy;
7563 break;
7564 case PREDEF_TYPE_INT128_ID:
7565 T = Context.Int128Ty;
7566 break;
7567 case PREDEF_TYPE_BFLOAT16_ID:
7568 T = Context.BFloat16Ty;
7569 break;
7570 case PREDEF_TYPE_HALF_ID:
7571 T = Context.HalfTy;
7572 break;
7573 case PREDEF_TYPE_FLOAT_ID:
7574 T = Context.FloatTy;
7575 break;
7576 case PREDEF_TYPE_DOUBLE_ID:
7577 T = Context.DoubleTy;
7578 break;
7579 case PREDEF_TYPE_LONGDOUBLE_ID:
7580 T = Context.LongDoubleTy;
7581 break;
7582 case PREDEF_TYPE_SHORT_ACCUM_ID:
7583 T = Context.ShortAccumTy;
7584 break;
7585 case PREDEF_TYPE_ACCUM_ID:
7586 T = Context.AccumTy;
7587 break;
7588 case PREDEF_TYPE_LONG_ACCUM_ID:
7589 T = Context.LongAccumTy;
7590 break;
7591 case PREDEF_TYPE_USHORT_ACCUM_ID:
7592 T = Context.UnsignedShortAccumTy;
7593 break;
7594 case PREDEF_TYPE_UACCUM_ID:
7595 T = Context.UnsignedAccumTy;
7596 break;
7597 case PREDEF_TYPE_ULONG_ACCUM_ID:
7598 T = Context.UnsignedLongAccumTy;
7599 break;
7600 case PREDEF_TYPE_SHORT_FRACT_ID:
7601 T = Context.ShortFractTy;
7602 break;
7603 case PREDEF_TYPE_FRACT_ID:
7604 T = Context.FractTy;
7605 break;
7606 case PREDEF_TYPE_LONG_FRACT_ID:
7607 T = Context.LongFractTy;
7608 break;
7609 case PREDEF_TYPE_USHORT_FRACT_ID:
7610 T = Context.UnsignedShortFractTy;
7611 break;
7612 case PREDEF_TYPE_UFRACT_ID:
7613 T = Context.UnsignedFractTy;
7614 break;
7615 case PREDEF_TYPE_ULONG_FRACT_ID:
7616 T = Context.UnsignedLongFractTy;
7617 break;
7618 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7619 T = Context.SatShortAccumTy;
7620 break;
7621 case PREDEF_TYPE_SAT_ACCUM_ID:
7622 T = Context.SatAccumTy;
7623 break;
7624 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7625 T = Context.SatLongAccumTy;
7626 break;
7627 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7628 T = Context.SatUnsignedShortAccumTy;
7629 break;
7630 case PREDEF_TYPE_SAT_UACCUM_ID:
7631 T = Context.SatUnsignedAccumTy;
7632 break;
7633 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7634 T = Context.SatUnsignedLongAccumTy;
7635 break;
7636 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7637 T = Context.SatShortFractTy;
7638 break;
7639 case PREDEF_TYPE_SAT_FRACT_ID:
7640 T = Context.SatFractTy;
7641 break;
7642 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7643 T = Context.SatLongFractTy;
7644 break;
7645 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7646 T = Context.SatUnsignedShortFractTy;
7647 break;
7648 case PREDEF_TYPE_SAT_UFRACT_ID:
7649 T = Context.SatUnsignedFractTy;
7650 break;
7651 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7652 T = Context.SatUnsignedLongFractTy;
7653 break;
7654 case PREDEF_TYPE_FLOAT16_ID:
7655 T = Context.Float16Ty;
7656 break;
7657 case PREDEF_TYPE_FLOAT128_ID:
7658 T = Context.Float128Ty;
7659 break;
7660 case PREDEF_TYPE_IBM128_ID:
7661 T = Context.Ibm128Ty;
7662 break;
7663 case PREDEF_TYPE_OVERLOAD_ID:
7664 T = Context.OverloadTy;
7665 break;
7666 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
7667 T = Context.UnresolvedTemplateTy;
7668 break;
7669 case PREDEF_TYPE_BOUND_MEMBER:
7670 T = Context.BoundMemberTy;
7671 break;
7672 case PREDEF_TYPE_PSEUDO_OBJECT:
7673 T = Context.PseudoObjectTy;
7674 break;
7675 case PREDEF_TYPE_DEPENDENT_ID:
7676 T = Context.DependentTy;
7677 break;
7678 case PREDEF_TYPE_UNKNOWN_ANY:
7679 T = Context.UnknownAnyTy;
7680 break;
7681 case PREDEF_TYPE_NULLPTR_ID:
7682 T = Context.NullPtrTy;
7683 break;
7684 case PREDEF_TYPE_CHAR8_ID:
7685 T = Context.Char8Ty;
7686 break;
7687 case PREDEF_TYPE_CHAR16_ID:
7688 T = Context.Char16Ty;
7689 break;
7690 case PREDEF_TYPE_CHAR32_ID:
7691 T = Context.Char32Ty;
7692 break;
7693 case PREDEF_TYPE_OBJC_ID:
7694 T = Context.ObjCBuiltinIdTy;
7695 break;
7696 case PREDEF_TYPE_OBJC_CLASS:
7697 T = Context.ObjCBuiltinClassTy;
7698 break;
7699 case PREDEF_TYPE_OBJC_SEL:
7700 T = Context.ObjCBuiltinSelTy;
7701 break;
7702#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7703 case PREDEF_TYPE_##Id##_ID: \
7704 T = Context.SingletonId; \
7705 break;
7706#include "clang/Basic/OpenCLImageTypes.def"
7707#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7708 case PREDEF_TYPE_##Id##_ID: \
7709 T = Context.Id##Ty; \
7710 break;
7711#include "clang/Basic/OpenCLExtensionTypes.def"
7712 case PREDEF_TYPE_SAMPLER_ID:
7713 T = Context.OCLSamplerTy;
7714 break;
7715 case PREDEF_TYPE_EVENT_ID:
7716 T = Context.OCLEventTy;
7717 break;
7718 case PREDEF_TYPE_CLK_EVENT_ID:
7719 T = Context.OCLClkEventTy;
7720 break;
7721 case PREDEF_TYPE_QUEUE_ID:
7722 T = Context.OCLQueueTy;
7723 break;
7724 case PREDEF_TYPE_RESERVE_ID_ID:
7725 T = Context.OCLReserveIDTy;
7726 break;
7727 case PREDEF_TYPE_AUTO_DEDUCT:
7728 T = Context.getAutoDeductType();
7729 break;
7730 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7731 T = Context.getAutoRRefDeductType();
7732 break;
7733 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7734 T = Context.ARCUnbridgedCastTy;
7735 break;
7736 case PREDEF_TYPE_BUILTIN_FN:
7737 T = Context.BuiltinFnTy;
7738 break;
7739 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7740 T = Context.IncompleteMatrixIdxTy;
7741 break;
7742 case PREDEF_TYPE_ARRAY_SECTION:
7743 T = Context.ArraySectionTy;
7744 break;
7745 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7746 T = Context.OMPArrayShapingTy;
7747 break;
7748 case PREDEF_TYPE_OMP_ITERATOR:
7749 T = Context.OMPIteratorTy;
7750 break;
7751#define SVE_TYPE(Name, Id, SingletonId) \
7752 case PREDEF_TYPE_##Id##_ID: \
7753 T = Context.SingletonId; \
7754 break;
7755#include "clang/Basic/AArch64ACLETypes.def"
7756#define PPC_VECTOR_TYPE(Name, Id, Size) \
7757 case PREDEF_TYPE_##Id##_ID: \
7758 T = Context.Id##Ty; \
7759 break;
7760#include "clang/Basic/PPCTypes.def"
7761#define RVV_TYPE(Name, Id, SingletonId) \
7762 case PREDEF_TYPE_##Id##_ID: \
7763 T = Context.SingletonId; \
7764 break;
7765#include "clang/Basic/RISCVVTypes.def"
7766#define WASM_TYPE(Name, Id, SingletonId) \
7767 case PREDEF_TYPE_##Id##_ID: \
7768 T = Context.SingletonId; \
7769 break;
7770#include "clang/Basic/WebAssemblyReferenceTypes.def"
7771#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
7772 case PREDEF_TYPE_##Id##_ID: \
7773 T = Context.SingletonId; \
7774 break;
7775#include "clang/Basic/AMDGPUTypes.def"
7776#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
7777 case PREDEF_TYPE_##Id##_ID: \
7778 T = Context.SingletonId; \
7779 break;
7780#include "clang/Basic/HLSLIntangibleTypes.def"
7781 }
7782
7783 assert(!T.isNull() && "Unknown predefined type");
7784 return T.withFastQualifiers(TQs: FastQuals);
7785 }
7786
7787 unsigned Index = translateTypeIDToIndex(ID).second;
7788
7789 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7790 if (TypesLoaded[Index].isNull()) {
7791 TypesLoaded[Index] = readTypeRecord(ID);
7792 if (TypesLoaded[Index].isNull())
7793 return QualType();
7794
7795 TypesLoaded[Index]->setFromAST();
7796 if (DeserializationListener)
7797 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
7798 T: TypesLoaded[Index]);
7799 }
7800
7801 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
7802}
7803
7804QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
7805 return GetType(ID: getGlobalTypeID(F, LocalID));
7806}
7807
7808serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
7809 LocalTypeID LocalID) const {
7810 if (isPredefinedType(ID: LocalID))
7811 return LocalID;
7812
7813 if (!F.ModuleOffsetMap.empty())
7814 ReadModuleOffsetMap(F);
7815
7816 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
7817 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
7818
7819 if (ModuleFileIndex == 0)
7820 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
7821
7822 ModuleFile &MF =
7823 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
7824 ModuleFileIndex = MF.Index + 1;
7825 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
7826}
7827
7828TemplateArgumentLocInfo
7829ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7830 switch (Kind) {
7831 case TemplateArgument::Expression:
7832 return readExpr();
7833 case TemplateArgument::Type:
7834 return readTypeSourceInfo();
7835 case TemplateArgument::Template: {
7836 NestedNameSpecifierLoc QualifierLoc =
7837 readNestedNameSpecifierLoc();
7838 SourceLocation TemplateNameLoc = readSourceLocation();
7839 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7840 TemplateNameLoc, SourceLocation());
7841 }
7842 case TemplateArgument::TemplateExpansion: {
7843 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7844 SourceLocation TemplateNameLoc = readSourceLocation();
7845 SourceLocation EllipsisLoc = readSourceLocation();
7846 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7847 TemplateNameLoc, EllipsisLoc);
7848 }
7849 case TemplateArgument::Null:
7850 case TemplateArgument::Integral:
7851 case TemplateArgument::Declaration:
7852 case TemplateArgument::NullPtr:
7853 case TemplateArgument::StructuralValue:
7854 case TemplateArgument::Pack:
7855 // FIXME: Is this right?
7856 return TemplateArgumentLocInfo();
7857 }
7858 llvm_unreachable("unexpected template argument loc");
7859}
7860
7861TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7862 TemplateArgument Arg = readTemplateArgument();
7863
7864 if (Arg.getKind() == TemplateArgument::Expression) {
7865 if (readBool()) // bool InfoHasSameExpr.
7866 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7867 }
7868 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
7869}
7870
7871void ASTRecordReader::readTemplateArgumentListInfo(
7872 TemplateArgumentListInfo &Result) {
7873 Result.setLAngleLoc(readSourceLocation());
7874 Result.setRAngleLoc(readSourceLocation());
7875 unsigned NumArgsAsWritten = readInt();
7876 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7877 Result.addArgument(Loc: readTemplateArgumentLoc());
7878}
7879
7880const ASTTemplateArgumentListInfo *
7881ASTRecordReader::readASTTemplateArgumentListInfo() {
7882 TemplateArgumentListInfo Result;
7883 readTemplateArgumentListInfo(Result);
7884 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
7885}
7886
7887Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
7888
7889void ASTReader::CompleteRedeclChain(const Decl *D) {
7890 if (NumCurrentElementsDeserializing) {
7891 // We arrange to not care about the complete redeclaration chain while we're
7892 // deserializing. Just remember that the AST has marked this one as complete
7893 // but that it's not actually complete yet, so we know we still need to
7894 // complete it later.
7895 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
7896 return;
7897 }
7898
7899 if (!D->getDeclContext()) {
7900 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7901 return;
7902 }
7903
7904 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7905
7906 // If this is a named declaration, complete it by looking it up
7907 // within its context.
7908 //
7909 // FIXME: Merging a function definition should merge
7910 // all mergeable entities within it.
7911 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
7912 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
7913 if (!getContext().getLangOpts().CPlusPlus &&
7914 isa<TranslationUnitDecl>(Val: DC)) {
7915 // Outside of C++, we don't have a lookup table for the TU, so update
7916 // the identifier instead. (For C++ modules, we don't store decls
7917 // in the serialized identifier table, so we do the lookup in the TU.)
7918 auto *II = Name.getAsIdentifierInfo();
7919 assert(II && "non-identifier name in C?");
7920 if (II->isOutOfDate())
7921 updateOutOfDateIdentifier(II: *II);
7922 } else
7923 DC->lookup(Name);
7924 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
7925 // Find all declarations of this kind from the relevant context.
7926 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
7927 auto *DC = cast<DeclContext>(Val: DCDecl);
7928 SmallVector<Decl*, 8> Decls;
7929 FindExternalLexicalDecls(
7930 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7931 }
7932 }
7933 }
7934
7935 RedeclarableTemplateDecl *Template = nullptr;
7936 ArrayRef<TemplateArgument> Args;
7937 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
7938 Template = CTSD->getSpecializedTemplate();
7939 Args = CTSD->getTemplateArgs().asArray();
7940 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
7941 Template = VTSD->getSpecializedTemplate();
7942 Args = VTSD->getTemplateArgs().asArray();
7943 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
7944 if (auto *Tmplt = FD->getPrimaryTemplate()) {
7945 Template = Tmplt;
7946 Args = FD->getTemplateSpecializationArgs()->asArray();
7947 }
7948 }
7949
7950 if (Template) {
7951 // For partitial specialization, load all the specializations for safety.
7952 if (isa<ClassTemplatePartialSpecializationDecl,
7953 VarTemplatePartialSpecializationDecl>(Val: D))
7954 Template->loadLazySpecializationsImpl();
7955 else
7956 Template->loadLazySpecializationsImpl(Args);
7957 }
7958}
7959
7960CXXCtorInitializer **
7961ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7962 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7963 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7964 SavedStreamPosition SavedPosition(Cursor);
7965 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
7966 Error(Err: std::move(Err));
7967 return nullptr;
7968 }
7969 ReadingKindTracker ReadingKind(Read_Decl, *this);
7970 Deserializing D(this);
7971
7972 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7973 if (!MaybeCode) {
7974 Error(Err: MaybeCode.takeError());
7975 return nullptr;
7976 }
7977 unsigned Code = MaybeCode.get();
7978
7979 ASTRecordReader Record(*this, *Loc.F);
7980 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
7981 if (!MaybeRecCode) {
7982 Error(Err: MaybeRecCode.takeError());
7983 return nullptr;
7984 }
7985 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7986 Error(Msg: "malformed AST file: missing C++ ctor initializers");
7987 return nullptr;
7988 }
7989
7990 return Record.readCXXCtorInitializers();
7991}
7992
7993CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7994 assert(ContextObj && "reading base specifiers with no AST context");
7995 ASTContext &Context = *ContextObj;
7996
7997 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7998 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7999 SavedStreamPosition SavedPosition(Cursor);
8000 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8001 Error(Err: std::move(Err));
8002 return nullptr;
8003 }
8004 ReadingKindTracker ReadingKind(Read_Decl, *this);
8005 Deserializing D(this);
8006
8007 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8008 if (!MaybeCode) {
8009 Error(Err: MaybeCode.takeError());
8010 return nullptr;
8011 }
8012 unsigned Code = MaybeCode.get();
8013
8014 ASTRecordReader Record(*this, *Loc.F);
8015 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8016 if (!MaybeRecCode) {
8017 Error(Err: MaybeCode.takeError());
8018 return nullptr;
8019 }
8020 unsigned RecCode = MaybeRecCode.get();
8021
8022 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8023 Error(Msg: "malformed AST file: missing C++ base specifiers");
8024 return nullptr;
8025 }
8026
8027 unsigned NumBases = Record.readInt();
8028 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8029 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8030 for (unsigned I = 0; I != NumBases; ++I)
8031 Bases[I] = Record.readCXXBaseSpecifier();
8032 return Bases;
8033}
8034
8035GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8036 LocalDeclID LocalID) const {
8037 if (LocalID < NUM_PREDEF_DECL_IDS)
8038 return GlobalDeclID(LocalID.getRawValue());
8039
8040 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8041 DeclID ID = LocalID.getLocalDeclIndex();
8042
8043 if (!F.ModuleOffsetMap.empty())
8044 ReadModuleOffsetMap(F);
8045
8046 ModuleFile *OwningModuleFile =
8047 OwningModuleFileIndex == 0
8048 ? &F
8049 : F.TransitiveImports[OwningModuleFileIndex - 1];
8050
8051 if (OwningModuleFileIndex == 0)
8052 ID -= NUM_PREDEF_DECL_IDS;
8053
8054 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8055 return GlobalDeclID(NewModuleFileIndex, ID);
8056}
8057
8058bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8059 // Predefined decls aren't from any module.
8060 if (ID < NUM_PREDEF_DECL_IDS)
8061 return false;
8062
8063 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8064 return M.Index == ModuleFileIndex - 1;
8065}
8066
8067ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8068 // Predefined decls aren't from any module.
8069 if (ID < NUM_PREDEF_DECL_IDS)
8070 return nullptr;
8071
8072 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8073 assert(ModuleFileIndex && "Untranslated Local Decl?");
8074
8075 return &getModuleManager()[ModuleFileIndex - 1];
8076}
8077
8078ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8079 if (!D->isFromASTFile())
8080 return nullptr;
8081
8082 return getOwningModuleFile(ID: D->getGlobalID());
8083}
8084
8085SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8086 if (ID < NUM_PREDEF_DECL_IDS)
8087 return SourceLocation();
8088
8089 if (Decl *D = GetExistingDecl(ID))
8090 return D->getLocation();
8091
8092 SourceLocation Loc;
8093 DeclCursorForID(ID, Location&: Loc);
8094 return Loc;
8095}
8096
8097Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8098 assert(ContextObj && "reading predefined decl without AST context");
8099 ASTContext &Context = *ContextObj;
8100 Decl *NewLoaded = nullptr;
8101 switch (ID) {
8102 case PREDEF_DECL_NULL_ID:
8103 return nullptr;
8104
8105 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8106 return Context.getTranslationUnitDecl();
8107
8108 case PREDEF_DECL_OBJC_ID_ID:
8109 if (Context.ObjCIdDecl)
8110 return Context.ObjCIdDecl;
8111 NewLoaded = Context.getObjCIdDecl();
8112 break;
8113
8114 case PREDEF_DECL_OBJC_SEL_ID:
8115 if (Context.ObjCSelDecl)
8116 return Context.ObjCSelDecl;
8117 NewLoaded = Context.getObjCSelDecl();
8118 break;
8119
8120 case PREDEF_DECL_OBJC_CLASS_ID:
8121 if (Context.ObjCClassDecl)
8122 return Context.ObjCClassDecl;
8123 NewLoaded = Context.getObjCClassDecl();
8124 break;
8125
8126 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8127 if (Context.ObjCProtocolClassDecl)
8128 return Context.ObjCProtocolClassDecl;
8129 NewLoaded = Context.getObjCProtocolDecl();
8130 break;
8131
8132 case PREDEF_DECL_INT_128_ID:
8133 if (Context.Int128Decl)
8134 return Context.Int128Decl;
8135 NewLoaded = Context.getInt128Decl();
8136 break;
8137
8138 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8139 if (Context.UInt128Decl)
8140 return Context.UInt128Decl;
8141 NewLoaded = Context.getUInt128Decl();
8142 break;
8143
8144 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8145 if (Context.ObjCInstanceTypeDecl)
8146 return Context.ObjCInstanceTypeDecl;
8147 NewLoaded = Context.getObjCInstanceTypeDecl();
8148 break;
8149
8150 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8151 if (Context.BuiltinVaListDecl)
8152 return Context.BuiltinVaListDecl;
8153 NewLoaded = Context.getBuiltinVaListDecl();
8154 break;
8155
8156 case PREDEF_DECL_VA_LIST_TAG:
8157 if (Context.VaListTagDecl)
8158 return Context.VaListTagDecl;
8159 NewLoaded = Context.getVaListTagDecl();
8160 break;
8161
8162 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8163 if (Context.BuiltinMSVaListDecl)
8164 return Context.BuiltinMSVaListDecl;
8165 NewLoaded = Context.getBuiltinMSVaListDecl();
8166 break;
8167
8168 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8169 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8170 return Context.getMSGuidTagDecl();
8171
8172 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8173 if (Context.ExternCContext)
8174 return Context.ExternCContext;
8175 NewLoaded = Context.getExternCContextDecl();
8176 break;
8177
8178 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8179 if (Context.CFConstantStringTypeDecl)
8180 return Context.CFConstantStringTypeDecl;
8181 NewLoaded = Context.getCFConstantStringDecl();
8182 break;
8183
8184 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8185 if (Context.CFConstantStringTagDecl)
8186 return Context.CFConstantStringTagDecl;
8187 NewLoaded = Context.getCFConstantStringTagDecl();
8188 break;
8189
8190#define BuiltinTemplate(BTName) \
8191 case PREDEF_DECL##BTName##_ID: \
8192 if (Context.Decl##BTName) \
8193 return Context.Decl##BTName; \
8194 NewLoaded = Context.get##BTName##Decl(); \
8195 break;
8196#include "clang/Basic/BuiltinTemplates.inc"
8197
8198 case NUM_PREDEF_DECL_IDS:
8199 llvm_unreachable("Invalid decl ID");
8200 break;
8201 }
8202
8203 assert(NewLoaded && "Failed to load predefined decl?");
8204
8205 if (DeserializationListener)
8206 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8207
8208 return NewLoaded;
8209}
8210
8211unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8212 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8213 if (!OwningModuleFile) {
8214 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8215 return GlobalID.getRawValue();
8216 }
8217
8218 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8219}
8220
8221Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8222 assert(ContextObj && "reading decl with no AST context");
8223
8224 if (ID < NUM_PREDEF_DECL_IDS) {
8225 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8226 if (D) {
8227 // Track that we have merged the declaration with ID \p ID into the
8228 // pre-existing predefined declaration \p D.
8229 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8230 if (Merged.empty())
8231 Merged.push_back(Elt: ID);
8232 }
8233 return D;
8234 }
8235
8236 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8237
8238 if (Index >= DeclsLoaded.size()) {
8239 assert(0 && "declaration ID out-of-range for AST file");
8240 Error(Msg: "declaration ID out-of-range for AST file");
8241 return nullptr;
8242 }
8243
8244 return DeclsLoaded[Index];
8245}
8246
8247Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8248 if (ID < NUM_PREDEF_DECL_IDS)
8249 return GetExistingDecl(ID);
8250
8251 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8252
8253 if (Index >= DeclsLoaded.size()) {
8254 assert(0 && "declaration ID out-of-range for AST file");
8255 Error(Msg: "declaration ID out-of-range for AST file");
8256 return nullptr;
8257 }
8258
8259 if (!DeclsLoaded[Index]) {
8260 ReadDeclRecord(ID);
8261 if (DeserializationListener)
8262 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8263 }
8264
8265 return DeclsLoaded[Index];
8266}
8267
8268LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8269 GlobalDeclID GlobalID) {
8270 if (GlobalID < NUM_PREDEF_DECL_IDS)
8271 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8272
8273 if (!M.ModuleOffsetMap.empty())
8274 ReadModuleOffsetMap(F&: M);
8275
8276 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8277 DeclID ID = GlobalID.getLocalDeclIndex();
8278
8279 if (Owner == &M) {
8280 ID += NUM_PREDEF_DECL_IDS;
8281 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8282 }
8283
8284 uint64_t OrignalModuleFileIndex = 0;
8285 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8286 if (M.TransitiveImports[I] == Owner) {
8287 OrignalModuleFileIndex = I + 1;
8288 break;
8289 }
8290
8291 if (!OrignalModuleFileIndex)
8292 return LocalDeclID();
8293
8294 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8295}
8296
8297GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8298 unsigned &Idx) {
8299 if (Idx >= Record.size()) {
8300 Error(Msg: "Corrupted AST file");
8301 return GlobalDeclID(0);
8302 }
8303
8304 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8305}
8306
8307/// Resolve the offset of a statement into a statement.
8308///
8309/// This operation will read a new statement from the external
8310/// source each time it is called, and is meant to be used via a
8311/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8312Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8313 // Switch case IDs are per Decl.
8314 ClearSwitchCaseIDs();
8315
8316 // Offset here is a global offset across the entire chain.
8317 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8318 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8319 Error(Err: std::move(Err));
8320 return nullptr;
8321 }
8322 assert(NumCurrentElementsDeserializing == 0 &&
8323 "should not be called while already deserializing");
8324 Deserializing D(this);
8325 return ReadStmtFromStream(F&: *Loc.F);
8326}
8327
8328bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8329 const Decl *D) {
8330 assert(D);
8331
8332 auto It = SpecLookups.find(Val: D);
8333 if (It == SpecLookups.end())
8334 return false;
8335
8336 // Get Decl may violate the iterator from SpecializationsLookups so we store
8337 // the DeclIDs in ahead.
8338 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8339 It->second.Table.findAll();
8340
8341 // Since we've loaded all the specializations, we can erase it from
8342 // the lookup table.
8343 SpecLookups.erase(I: It);
8344
8345 bool NewSpecsFound = false;
8346 Deserializing LookupResults(this);
8347 for (auto &Info : Infos) {
8348 if (GetExistingDecl(ID: Info))
8349 continue;
8350 NewSpecsFound = true;
8351 GetDecl(ID: Info);
8352 }
8353
8354 return NewSpecsFound;
8355}
8356
8357bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8358 assert(D);
8359
8360 bool NewSpecsFound =
8361 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8362 if (OnlyPartial)
8363 return NewSpecsFound;
8364
8365 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8366 return NewSpecsFound;
8367}
8368
8369bool ASTReader::LoadExternalSpecializationsImpl(
8370 SpecLookupTableTy &SpecLookups, const Decl *D,
8371 ArrayRef<TemplateArgument> TemplateArgs) {
8372 assert(D);
8373
8374 auto It = SpecLookups.find(Val: D);
8375 if (It == SpecLookups.end())
8376 return false;
8377
8378 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8379 std::string Name;
8380 llvm::raw_string_ostream OS(Name);
8381 auto *ND = cast<NamedDecl>(Val: D);
8382 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8383 /*Qualified=*/true);
8384 return Name;
8385 });
8386
8387 Deserializing LookupResults(this);
8388 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8389
8390 // Get Decl may violate the iterator from SpecLookups
8391 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8392 It->second.Table.find(EKey: HashValue);
8393
8394 bool NewSpecsFound = false;
8395 for (auto &Info : Infos) {
8396 if (GetExistingDecl(ID: Info))
8397 continue;
8398 NewSpecsFound = true;
8399 GetDecl(ID: Info);
8400 }
8401
8402 return NewSpecsFound;
8403}
8404
8405bool ASTReader::LoadExternalSpecializations(
8406 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8407 assert(D);
8408
8409 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8410 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8411 NewDeclsFound |=
8412 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8413
8414 return NewDeclsFound;
8415}
8416
8417void ASTReader::FindExternalLexicalDecls(
8418 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8419 SmallVectorImpl<Decl *> &Decls) {
8420 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8421
8422 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8423 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8424 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8425 auto K = (Decl::Kind)+LexicalDecls[I];
8426 if (!IsKindWeWant(K))
8427 continue;
8428
8429 auto ID = (DeclID) + LexicalDecls[I + 1];
8430
8431 // Don't add predefined declarations to the lexical context more
8432 // than once.
8433 if (ID < NUM_PREDEF_DECL_IDS) {
8434 if (PredefsVisited[ID])
8435 continue;
8436
8437 PredefsVisited[ID] = true;
8438 }
8439
8440 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8441 assert(D->getKind() == K && "wrong kind for lexical decl");
8442 if (!DC->isDeclInLexicalTraversal(D))
8443 Decls.push_back(Elt: D);
8444 }
8445 }
8446 };
8447
8448 if (isa<TranslationUnitDecl>(Val: DC)) {
8449 for (const auto &Lexical : TULexicalDecls)
8450 Visit(Lexical.first, Lexical.second);
8451 } else {
8452 auto I = LexicalDecls.find(Val: DC);
8453 if (I != LexicalDecls.end())
8454 Visit(I->second.first, I->second.second);
8455 }
8456
8457 ++NumLexicalDeclContextsRead;
8458}
8459
8460namespace {
8461
8462class UnalignedDeclIDComp {
8463 ASTReader &Reader;
8464 ModuleFile &Mod;
8465
8466public:
8467 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8468 : Reader(Reader), Mod(M) {}
8469
8470 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8471 SourceLocation LHS = getLocation(ID: L);
8472 SourceLocation RHS = getLocation(ID: R);
8473 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8474 }
8475
8476 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8477 SourceLocation RHS = getLocation(ID: R);
8478 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8479 }
8480
8481 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8482 SourceLocation LHS = getLocation(ID: L);
8483 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8484 }
8485
8486 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8487 return Reader.getSourceManager().getFileLoc(
8488 Loc: Reader.getSourceLocationForDeclID(
8489 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8490 }
8491};
8492
8493} // namespace
8494
8495void ASTReader::FindFileRegionDecls(FileID File,
8496 unsigned Offset, unsigned Length,
8497 SmallVectorImpl<Decl *> &Decls) {
8498 SourceManager &SM = getSourceManager();
8499
8500 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8501 if (I == FileDeclIDs.end())
8502 return;
8503
8504 FileDeclsInfo &DInfo = I->second;
8505 if (DInfo.Decls.empty())
8506 return;
8507
8508 SourceLocation
8509 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8510 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8511
8512 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8513 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8514 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8515 if (BeginIt != DInfo.Decls.begin())
8516 --BeginIt;
8517
8518 // If we are pointing at a top-level decl inside an objc container, we need
8519 // to backtrack until we find it otherwise we will fail to report that the
8520 // region overlaps with an objc container.
8521 while (BeginIt != DInfo.Decls.begin() &&
8522 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8523 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8524 ->isTopLevelDeclInObjCContainer())
8525 --BeginIt;
8526
8527 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8528 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8529 if (EndIt != DInfo.Decls.end())
8530 ++EndIt;
8531
8532 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8533 ++DIt)
8534 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8535 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8536}
8537
8538bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8539 DeclarationName Name,
8540 const DeclContext *OriginalDC) {
8541 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8542 "DeclContext has no visible decls in storage");
8543 if (!Name)
8544 return false;
8545
8546 // Load the list of declarations.
8547 SmallVector<NamedDecl *, 64> Decls;
8548 llvm::SmallPtrSet<NamedDecl *, 8> Found;
8549
8550 auto Find = [&, this](auto &&Table, auto &&Key) {
8551 for (GlobalDeclID ID : Table.find(Key)) {
8552 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8553 if (ND->getDeclName() == Name && Found.insert(Ptr: ND).second)
8554 Decls.push_back(Elt: ND);
8555 }
8556 };
8557
8558 Deserializing LookupResults(this);
8559
8560 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8561 // available.
8562 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8563 ++NumVisibleDeclContextsRead;
8564 Find(It->second.Table, Name);
8565 }
8566
8567 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8568 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8569 ++NumModuleLocalVisibleDeclContexts;
8570 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8571 }
8572 };
8573 if (auto *NamedModule =
8574 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8575 : nullptr)
8576 FindModuleLocalLookup(NamedModule);
8577 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8578 // We're going to find a decl but the decl context of the lookup is
8579 // unspecified. In this case, the OriginalDC may be the decl context in other
8580 // module.
8581 if (ContextObj && ContextObj->getCurrentNamedModule())
8582 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8583
8584 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8585 ++NumTULocalVisibleDeclContexts;
8586 Find(It->second.Table, Name);
8587 }
8588
8589 SetExternalVisibleDeclsForName(DC, Name, Decls);
8590 return !Decls.empty();
8591}
8592
8593void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8594 if (!DC->hasExternalVisibleStorage())
8595 return;
8596
8597 DeclsMap Decls;
8598
8599 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8600 auto It = LookupTables.find(DC);
8601 if (It == LookupTables.end())
8602 return;
8603
8604 NumRead++;
8605
8606 for (GlobalDeclID ID : It->second.Table.findAll()) {
8607 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8608 Decls[ND->getDeclName()].push_back(Elt: ND);
8609 }
8610
8611 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8612 };
8613
8614 findAll(Lookups, NumVisibleDeclContextsRead);
8615 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8616 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8617
8618 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8619 SetExternalVisibleDeclsForName(DC, Name: I->first, Decls: I->second);
8620 }
8621 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8622}
8623
8624const serialization::reader::DeclContextLookupTable *
8625ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8626 auto I = Lookups.find(Val: Primary);
8627 return I == Lookups.end() ? nullptr : &I->second;
8628}
8629
8630const serialization::reader::ModuleLocalLookupTable *
8631ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
8632 auto I = ModuleLocalLookups.find(Val: Primary);
8633 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
8634}
8635
8636const serialization::reader::DeclContextLookupTable *
8637ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
8638 auto I = TULocalLookups.find(Val: Primary);
8639 return I == TULocalLookups.end() ? nullptr : &I->second;
8640}
8641
8642serialization::reader::LazySpecializationInfoLookupTable *
8643ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
8644 assert(D->isCanonicalDecl());
8645 auto &LookupTable =
8646 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
8647 auto I = LookupTable.find(Val: D);
8648 return I == LookupTable.end() ? nullptr : &I->second;
8649}
8650
8651bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
8652 assert(D->isCanonicalDecl());
8653 return PartialSpecializationsLookups.contains(Val: D) ||
8654 SpecializationsLookups.contains(Val: D);
8655}
8656
8657/// Under non-PCH compilation the consumer receives the objc methods
8658/// before receiving the implementation, and codegen depends on this.
8659/// We simulate this by deserializing and passing to consumer the methods of the
8660/// implementation before passing the deserialized implementation decl.
8661static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8662 ASTConsumer *Consumer) {
8663 assert(ImplD && Consumer);
8664
8665 for (auto *I : ImplD->methods())
8666 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
8667
8668 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8669}
8670
8671void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8672 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8673 PassObjCImplDeclToConsumer(ImplD, Consumer);
8674 else
8675 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
8676}
8677
8678void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
8679 Consumer->HandleVTable(RD);
8680}
8681
8682void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8683 this->Consumer = Consumer;
8684
8685 if (Consumer)
8686 PassInterestingDeclsToConsumer();
8687
8688 if (DeserializationListener)
8689 DeserializationListener->ReaderInitialized(Reader: this);
8690}
8691
8692void ASTReader::PrintStats() {
8693 std::fprintf(stderr, format: "*** AST File Statistics:\n");
8694
8695 unsigned NumTypesLoaded =
8696 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
8697 unsigned NumDeclsLoaded =
8698 DeclsLoaded.size() -
8699 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
8700 unsigned NumIdentifiersLoaded =
8701 IdentifiersLoaded.size() -
8702 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
8703 unsigned NumMacrosLoaded =
8704 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
8705 unsigned NumSelectorsLoaded =
8706 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
8707
8708 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8709 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
8710 NumSLocEntriesRead, TotalNumSLocEntries,
8711 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8712 if (!TypesLoaded.empty())
8713 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
8714 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8715 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8716 if (!DeclsLoaded.empty())
8717 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
8718 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8719 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8720 if (!IdentifiersLoaded.empty())
8721 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
8722 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8723 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8724 if (!MacrosLoaded.empty())
8725 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8726 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8727 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8728 if (!SelectorsLoaded.empty())
8729 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
8730 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8731 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8732 if (TotalNumStatements)
8733 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
8734 NumStatementsRead, TotalNumStatements,
8735 ((float)NumStatementsRead/TotalNumStatements * 100));
8736 if (TotalNumMacros)
8737 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8738 NumMacrosRead, TotalNumMacros,
8739 ((float)NumMacrosRead/TotalNumMacros * 100));
8740 if (TotalLexicalDeclContexts)
8741 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
8742 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8743 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8744 * 100));
8745 if (TotalVisibleDeclContexts)
8746 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
8747 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8748 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8749 * 100));
8750 if (TotalModuleLocalVisibleDeclContexts)
8751 std::fprintf(
8752 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
8753 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
8754 ((float)NumModuleLocalVisibleDeclContexts /
8755 TotalModuleLocalVisibleDeclContexts * 100));
8756 if (TotalTULocalVisibleDeclContexts)
8757 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
8758 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
8759 ((float)NumTULocalVisibleDeclContexts /
8760 TotalTULocalVisibleDeclContexts * 100));
8761 if (TotalNumMethodPoolEntries)
8762 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
8763 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8764 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8765 * 100));
8766 if (NumMethodPoolLookups)
8767 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
8768 NumMethodPoolHits, NumMethodPoolLookups,
8769 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8770 if (NumMethodPoolTableLookups)
8771 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
8772 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8773 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8774 * 100.0));
8775 if (NumIdentifierLookupHits)
8776 std::fprintf(stderr,
8777 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
8778 NumIdentifierLookupHits, NumIdentifierLookups,
8779 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8780
8781 if (GlobalIndex) {
8782 std::fprintf(stderr, format: "\n");
8783 GlobalIndex->printStats();
8784 }
8785
8786 std::fprintf(stderr, format: "\n");
8787 dump();
8788 std::fprintf(stderr, format: "\n");
8789}
8790
8791template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8792LLVM_DUMP_METHOD static void
8793dumpModuleIDMap(StringRef Name,
8794 const ContinuousRangeMap<Key, ModuleFile *,
8795 InitialCapacity> &Map) {
8796 if (Map.begin() == Map.end())
8797 return;
8798
8799 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8800
8801 llvm::errs() << Name << ":\n";
8802 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8803 I != IEnd; ++I)
8804 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
8805 << "\n";
8806}
8807
8808LLVM_DUMP_METHOD void ASTReader::dump() {
8809 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8810 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
8811 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
8812 dumpModuleIDMap(Name: "Global macro map", Map: GlobalMacroMap);
8813 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
8814 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
8815 dumpModuleIDMap(Name: "Global preprocessed entity map",
8816 Map: GlobalPreprocessedEntityMap);
8817
8818 llvm::errs() << "\n*** PCH/Modules Loaded:";
8819 for (ModuleFile &M : ModuleMgr)
8820 M.dump();
8821}
8822
8823/// Return the amount of memory used by memory buffers, breaking down
8824/// by heap-backed versus mmap'ed memory.
8825void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8826 for (ModuleFile &I : ModuleMgr) {
8827 if (llvm::MemoryBuffer *buf = I.Buffer) {
8828 size_t bytes = buf->getBufferSize();
8829 switch (buf->getBufferKind()) {
8830 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8831 sizes.malloc_bytes += bytes;
8832 break;
8833 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8834 sizes.mmap_bytes += bytes;
8835 break;
8836 }
8837 }
8838 }
8839}
8840
8841void ASTReader::InitializeSema(Sema &S) {
8842 SemaObj = &S;
8843 S.addExternalSource(E: this);
8844
8845 // Makes sure any declarations that were deserialized "too early"
8846 // still get added to the identifier's declaration chains.
8847 for (GlobalDeclID ID : PreloadedDeclIDs) {
8848 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
8849 pushExternalDeclIntoScope(D, Name: D->getDeclName());
8850 }
8851 PreloadedDeclIDs.clear();
8852
8853 // FIXME: What happens if these are changed by a module import?
8854 if (!FPPragmaOptions.empty()) {
8855 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8856 FPOptionsOverride NewOverrides =
8857 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
8858 SemaObj->CurFPFeatures =
8859 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
8860 }
8861
8862 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
8863 Decl *D = GetDecl(ID);
8864 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
8865 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
8866 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
8867 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
8868 else
8869 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
8870 }
8871 DeclsWithEffectsToVerify.clear();
8872
8873 SemaObj->OpenCLFeatures = OpenCLExtensions;
8874
8875 UpdateSema();
8876}
8877
8878void ASTReader::UpdateSema() {
8879 assert(SemaObj && "no Sema to update");
8880
8881 // Load the offsets of the declarations that Sema references.
8882 // They will be lazily deserialized when needed.
8883 if (!SemaDeclRefs.empty()) {
8884 assert(SemaDeclRefs.size() % 3 == 0);
8885 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8886 if (!SemaObj->StdNamespace)
8887 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
8888 if (!SemaObj->StdBadAlloc)
8889 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
8890 if (!SemaObj->StdAlignValT)
8891 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
8892 }
8893 SemaDeclRefs.clear();
8894 }
8895
8896 // Update the state of pragmas. Use the same API as if we had encountered the
8897 // pragma in the source.
8898 if(OptimizeOffPragmaLocation.isValid())
8899 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
8900 if (PragmaMSStructState != -1)
8901 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
8902 if (PointersToMembersPragmaLocation.isValid()) {
8903 SemaObj->ActOnPragmaMSPointersToMembers(
8904 Kind: (LangOptions::PragmaMSPointersToMembersKind)
8905 PragmaMSPointersToMembersState,
8906 PragmaLoc: PointersToMembersPragmaLocation);
8907 }
8908 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
8909
8910 if (PragmaAlignPackCurrentValue) {
8911 // The bottom of the stack might have a default value. It must be adjusted
8912 // to the current value to ensure that the packing state is preserved after
8913 // popping entries that were included/imported from a PCH/module.
8914 bool DropFirst = false;
8915 if (!PragmaAlignPackStack.empty() &&
8916 PragmaAlignPackStack.front().Location.isInvalid()) {
8917 assert(PragmaAlignPackStack.front().Value ==
8918 SemaObj->AlignPackStack.DefaultValue &&
8919 "Expected a default alignment value");
8920 SemaObj->AlignPackStack.Stack.emplace_back(
8921 Args&: PragmaAlignPackStack.front().SlotLabel,
8922 Args&: SemaObj->AlignPackStack.CurrentValue,
8923 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
8924 Args&: PragmaAlignPackStack.front().PushLocation);
8925 DropFirst = true;
8926 }
8927 for (const auto &Entry :
8928 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
8929 SemaObj->AlignPackStack.Stack.emplace_back(
8930 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8931 }
8932 if (PragmaAlignPackCurrentLocation.isInvalid()) {
8933 assert(*PragmaAlignPackCurrentValue ==
8934 SemaObj->AlignPackStack.DefaultValue &&
8935 "Expected a default align and pack value");
8936 // Keep the current values.
8937 } else {
8938 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8939 SemaObj->AlignPackStack.CurrentPragmaLocation =
8940 PragmaAlignPackCurrentLocation;
8941 }
8942 }
8943 if (FpPragmaCurrentValue) {
8944 // The bottom of the stack might have a default value. It must be adjusted
8945 // to the current value to ensure that fp-pragma state is preserved after
8946 // popping entries that were included/imported from a PCH/module.
8947 bool DropFirst = false;
8948 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8949 assert(FpPragmaStack.front().Value ==
8950 SemaObj->FpPragmaStack.DefaultValue &&
8951 "Expected a default pragma float_control value");
8952 SemaObj->FpPragmaStack.Stack.emplace_back(
8953 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
8954 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
8955 Args&: FpPragmaStack.front().PushLocation);
8956 DropFirst = true;
8957 }
8958 for (const auto &Entry :
8959 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
8960 SemaObj->FpPragmaStack.Stack.emplace_back(
8961 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8962 if (FpPragmaCurrentLocation.isInvalid()) {
8963 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8964 "Expected a default pragma float_control value");
8965 // Keep the current values.
8966 } else {
8967 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8968 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8969 }
8970 }
8971
8972 // For non-modular AST files, restore visiblity of modules.
8973 for (auto &Import : PendingImportedModulesSema) {
8974 if (Import.ImportLoc.isInvalid())
8975 continue;
8976 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
8977 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
8978 }
8979 }
8980 PendingImportedModulesSema.clear();
8981}
8982
8983IdentifierInfo *ASTReader::get(StringRef Name) {
8984 // Note that we are loading an identifier.
8985 Deserializing AnIdentifier(this);
8986
8987 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8988 NumIdentifierLookups,
8989 NumIdentifierLookupHits);
8990
8991 // We don't need to do identifier table lookups in C++ modules (we preload
8992 // all interesting declarations, and don't need to use the scope for name
8993 // lookups). Perform the lookup in PCH files, though, since we don't build
8994 // a complete initial identifier table if we're carrying on from a PCH.
8995 if (PP.getLangOpts().CPlusPlus) {
8996 for (auto *F : ModuleMgr.pch_modules())
8997 if (Visitor(*F))
8998 break;
8999 } else {
9000 // If there is a global index, look there first to determine which modules
9001 // provably do not have any results for this identifier.
9002 GlobalModuleIndex::HitSet Hits;
9003 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9004 if (!loadGlobalIndex()) {
9005 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9006 HitsPtr = &Hits;
9007 }
9008 }
9009
9010 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9011 }
9012
9013 IdentifierInfo *II = Visitor.getIdentifierInfo();
9014 markIdentifierUpToDate(II);
9015 return II;
9016}
9017
9018namespace clang {
9019
9020 /// An identifier-lookup iterator that enumerates all of the
9021 /// identifiers stored within a set of AST files.
9022 class ASTIdentifierIterator : public IdentifierIterator {
9023 /// The AST reader whose identifiers are being enumerated.
9024 const ASTReader &Reader;
9025
9026 /// The current index into the chain of AST files stored in
9027 /// the AST reader.
9028 unsigned Index;
9029
9030 /// The current position within the identifier lookup table
9031 /// of the current AST file.
9032 ASTIdentifierLookupTable::key_iterator Current;
9033
9034 /// The end position within the identifier lookup table of
9035 /// the current AST file.
9036 ASTIdentifierLookupTable::key_iterator End;
9037
9038 /// Whether to skip any modules in the ASTReader.
9039 bool SkipModules;
9040
9041 public:
9042 explicit ASTIdentifierIterator(const ASTReader &Reader,
9043 bool SkipModules = false);
9044
9045 StringRef Next() override;
9046 };
9047
9048} // namespace clang
9049
9050ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9051 bool SkipModules)
9052 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9053}
9054
9055StringRef ASTIdentifierIterator::Next() {
9056 while (Current == End) {
9057 // If we have exhausted all of our AST files, we're done.
9058 if (Index == 0)
9059 return StringRef();
9060
9061 --Index;
9062 ModuleFile &F = Reader.ModuleMgr[Index];
9063 if (SkipModules && F.isModule())
9064 continue;
9065
9066 ASTIdentifierLookupTable *IdTable =
9067 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9068 Current = IdTable->key_begin();
9069 End = IdTable->key_end();
9070 }
9071
9072 // We have any identifiers remaining in the current AST file; return
9073 // the next one.
9074 StringRef Result = *Current;
9075 ++Current;
9076 return Result;
9077}
9078
9079namespace {
9080
9081/// A utility for appending two IdentifierIterators.
9082class ChainedIdentifierIterator : public IdentifierIterator {
9083 std::unique_ptr<IdentifierIterator> Current;
9084 std::unique_ptr<IdentifierIterator> Queued;
9085
9086public:
9087 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9088 std::unique_ptr<IdentifierIterator> Second)
9089 : Current(std::move(First)), Queued(std::move(Second)) {}
9090
9091 StringRef Next() override {
9092 if (!Current)
9093 return StringRef();
9094
9095 StringRef result = Current->Next();
9096 if (!result.empty())
9097 return result;
9098
9099 // Try the queued iterator, which may itself be empty.
9100 Current.reset();
9101 std::swap(x&: Current, y&: Queued);
9102 return Next();
9103 }
9104};
9105
9106} // namespace
9107
9108IdentifierIterator *ASTReader::getIdentifiers() {
9109 if (!loadGlobalIndex()) {
9110 std::unique_ptr<IdentifierIterator> ReaderIter(
9111 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9112 std::unique_ptr<IdentifierIterator> ModulesIter(
9113 GlobalIndex->createIdentifierIterator());
9114 return new ChainedIdentifierIterator(std::move(ReaderIter),
9115 std::move(ModulesIter));
9116 }
9117
9118 return new ASTIdentifierIterator(*this);
9119}
9120
9121namespace clang {
9122namespace serialization {
9123
9124 class ReadMethodPoolVisitor {
9125 ASTReader &Reader;
9126 Selector Sel;
9127 unsigned PriorGeneration;
9128 unsigned InstanceBits = 0;
9129 unsigned FactoryBits = 0;
9130 bool InstanceHasMoreThanOneDecl = false;
9131 bool FactoryHasMoreThanOneDecl = false;
9132 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9133 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9134
9135 public:
9136 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9137 unsigned PriorGeneration)
9138 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9139
9140 bool operator()(ModuleFile &M) {
9141 if (!M.SelectorLookupTable)
9142 return false;
9143
9144 // If we've already searched this module file, skip it now.
9145 if (M.Generation <= PriorGeneration)
9146 return true;
9147
9148 ++Reader.NumMethodPoolTableLookups;
9149 ASTSelectorLookupTable *PoolTable
9150 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9151 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9152 if (Pos == PoolTable->end())
9153 return false;
9154
9155 ++Reader.NumMethodPoolTableHits;
9156 ++Reader.NumSelectorsRead;
9157 // FIXME: Not quite happy with the statistics here. We probably should
9158 // disable this tracking when called via LoadSelector.
9159 // Also, should entries without methods count as misses?
9160 ++Reader.NumMethodPoolEntriesRead;
9161 ASTSelectorLookupTrait::data_type Data = *Pos;
9162 if (Reader.DeserializationListener)
9163 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9164
9165 // Append methods in the reverse order, so that later we can process them
9166 // in the order they appear in the source code by iterating through
9167 // the vector in the reverse order.
9168 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9169 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9170 InstanceBits = Data.InstanceBits;
9171 FactoryBits = Data.FactoryBits;
9172 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9173 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9174 return false;
9175 }
9176
9177 /// Retrieve the instance methods found by this visitor.
9178 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9179 return InstanceMethods;
9180 }
9181
9182 /// Retrieve the instance methods found by this visitor.
9183 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9184 return FactoryMethods;
9185 }
9186
9187 unsigned getInstanceBits() const { return InstanceBits; }
9188 unsigned getFactoryBits() const { return FactoryBits; }
9189
9190 bool instanceHasMoreThanOneDecl() const {
9191 return InstanceHasMoreThanOneDecl;
9192 }
9193
9194 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9195 };
9196
9197} // namespace serialization
9198} // namespace clang
9199
9200/// Add the given set of methods to the method list.
9201static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9202 ObjCMethodList &List) {
9203 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9204 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9205}
9206
9207void ASTReader::ReadMethodPool(Selector Sel) {
9208 // Get the selector generation and update it to the current generation.
9209 unsigned &Generation = SelectorGeneration[Sel];
9210 unsigned PriorGeneration = Generation;
9211 Generation = getGeneration();
9212 SelectorOutOfDate[Sel] = false;
9213
9214 // Search for methods defined with this selector.
9215 ++NumMethodPoolLookups;
9216 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9217 ModuleMgr.visit(Visitor);
9218
9219 if (Visitor.getInstanceMethods().empty() &&
9220 Visitor.getFactoryMethods().empty())
9221 return;
9222
9223 ++NumMethodPoolHits;
9224
9225 if (!getSema())
9226 return;
9227
9228 Sema &S = *getSema();
9229 auto &Methods = S.ObjC().MethodPool[Sel];
9230
9231 Methods.first.setBits(Visitor.getInstanceBits());
9232 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9233 Methods.second.setBits(Visitor.getFactoryBits());
9234 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9235
9236 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9237 // when building a module we keep every method individually and may need to
9238 // update hasMoreThanOneDecl as we add the methods.
9239 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9240 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9241}
9242
9243void ASTReader::updateOutOfDateSelector(Selector Sel) {
9244 if (SelectorOutOfDate[Sel])
9245 ReadMethodPool(Sel);
9246}
9247
9248void ASTReader::ReadKnownNamespaces(
9249 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9250 Namespaces.clear();
9251
9252 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9253 if (NamespaceDecl *Namespace
9254 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9255 Namespaces.push_back(Elt: Namespace);
9256 }
9257}
9258
9259void ASTReader::ReadUndefinedButUsed(
9260 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9261 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9262 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9263 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9264 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9265 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9266 }
9267 UndefinedButUsed.clear();
9268}
9269
9270void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9271 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9272 Exprs) {
9273 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9274 FieldDecl *FD =
9275 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9276 uint64_t Count = DelayedDeleteExprs[Idx++];
9277 for (uint64_t C = 0; C < Count; ++C) {
9278 SourceLocation DeleteLoc =
9279 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9280 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9281 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9282 }
9283 }
9284}
9285
9286void ASTReader::ReadTentativeDefinitions(
9287 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9288 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9289 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9290 if (Var)
9291 TentativeDefs.push_back(Elt: Var);
9292 }
9293 TentativeDefinitions.clear();
9294}
9295
9296void ASTReader::ReadUnusedFileScopedDecls(
9297 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9298 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9299 DeclaratorDecl *D
9300 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9301 if (D)
9302 Decls.push_back(Elt: D);
9303 }
9304 UnusedFileScopedDecls.clear();
9305}
9306
9307void ASTReader::ReadDelegatingConstructors(
9308 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9309 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9310 CXXConstructorDecl *D
9311 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9312 if (D)
9313 Decls.push_back(Elt: D);
9314 }
9315 DelegatingCtorDecls.clear();
9316}
9317
9318void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9319 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9320 TypedefNameDecl *D
9321 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9322 if (D)
9323 Decls.push_back(Elt: D);
9324 }
9325 ExtVectorDecls.clear();
9326}
9327
9328void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9329 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9330 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9331 ++I) {
9332 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9333 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9334 if (D)
9335 Decls.insert(X: D);
9336 }
9337 UnusedLocalTypedefNameCandidates.clear();
9338}
9339
9340void ASTReader::ReadDeclsToCheckForDeferredDiags(
9341 llvm::SmallSetVector<Decl *, 4> &Decls) {
9342 for (auto I : DeclsToCheckForDeferredDiags) {
9343 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9344 if (D)
9345 Decls.insert(X: D);
9346 }
9347 DeclsToCheckForDeferredDiags.clear();
9348}
9349
9350void ASTReader::ReadReferencedSelectors(
9351 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9352 if (ReferencedSelectorsData.empty())
9353 return;
9354
9355 // If there are @selector references added them to its pool. This is for
9356 // implementation of -Wselector.
9357 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9358 unsigned I = 0;
9359 while (I < DataSize) {
9360 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9361 SourceLocation SelLoc
9362 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9363 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9364 }
9365 ReferencedSelectorsData.clear();
9366}
9367
9368void ASTReader::ReadWeakUndeclaredIdentifiers(
9369 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9370 if (WeakUndeclaredIdentifiers.empty())
9371 return;
9372
9373 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9374 IdentifierInfo *WeakId
9375 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9376 IdentifierInfo *AliasId
9377 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9378 SourceLocation Loc =
9379 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9380 WeakInfo WI(AliasId, Loc);
9381 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9382 }
9383 WeakUndeclaredIdentifiers.clear();
9384}
9385
9386void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9387 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9388 ExternalVTableUse VT;
9389 VTableUse &TableInfo = VTableUses[Idx++];
9390 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9391 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9392 VT.DefinitionRequired = TableInfo.Used;
9393 VTables.push_back(Elt: VT);
9394 }
9395
9396 VTableUses.clear();
9397}
9398
9399void ASTReader::ReadPendingInstantiations(
9400 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9401 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9402 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9403 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9404 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9405
9406 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9407 }
9408 PendingInstantiations.clear();
9409}
9410
9411void ASTReader::ReadLateParsedTemplates(
9412 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9413 &LPTMap) {
9414 for (auto &LPT : LateParsedTemplates) {
9415 ModuleFile *FMod = LPT.first;
9416 RecordDataImpl &LateParsed = LPT.second;
9417 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9418 /* In loop */) {
9419 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9420
9421 auto LT = std::make_unique<LateParsedTemplate>();
9422 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9423 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9424
9425 ModuleFile *F = getOwningModuleFile(D: LT->D);
9426 assert(F && "No module");
9427
9428 unsigned TokN = LateParsed[Idx++];
9429 LT->Toks.reserve(N: TokN);
9430 for (unsigned T = 0; T < TokN; ++T)
9431 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9432
9433 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9434 }
9435 }
9436
9437 LateParsedTemplates.clear();
9438}
9439
9440void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9441 if (!Lambda->getLambdaContextDecl())
9442 return;
9443
9444 auto LambdaInfo =
9445 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9446 y: Lambda->getLambdaIndexInContext());
9447
9448 // Handle the import and then include case for lambdas.
9449 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9450 Iter != LambdaDeclarationsForMerging.end() &&
9451 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9452 CXXRecordDecl *Previous =
9453 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9454 Lambda->setPreviousDecl(Previous);
9455 // FIXME: It will be best to use the Previous type when we creating the
9456 // lambda directly. But that requires us to get the lambda context decl and
9457 // lambda index before creating the lambda, which needs a drastic change in
9458 // the parser.
9459 const_cast<QualType &>(Lambda->TypeForDecl->CanonicalType) =
9460 Previous->TypeForDecl->CanonicalType;
9461 return;
9462 }
9463
9464 // Keep track of this lambda so it can be merged with another lambda that
9465 // is loaded later.
9466 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9467}
9468
9469void ASTReader::LoadSelector(Selector Sel) {
9470 // It would be complicated to avoid reading the methods anyway. So don't.
9471 ReadMethodPool(Sel);
9472}
9473
9474void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9475 assert(ID && "Non-zero identifier ID required");
9476 unsigned Index = translateIdentifierIDToIndex(ID).second;
9477 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9478 IdentifiersLoaded[Index] = II;
9479 if (DeserializationListener)
9480 DeserializationListener->IdentifierRead(ID, II);
9481}
9482
9483/// Set the globally-visible declarations associated with the given
9484/// identifier.
9485///
9486/// If the AST reader is currently in a state where the given declaration IDs
9487/// cannot safely be resolved, they are queued until it is safe to resolve
9488/// them.
9489///
9490/// \param II an IdentifierInfo that refers to one or more globally-visible
9491/// declarations.
9492///
9493/// \param DeclIDs the set of declaration IDs with the name @p II that are
9494/// visible at global scope.
9495///
9496/// \param Decls if non-null, this vector will be populated with the set of
9497/// deserialized declarations. These declarations will not be pushed into
9498/// scope.
9499void ASTReader::SetGloballyVisibleDecls(
9500 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9501 SmallVectorImpl<Decl *> *Decls) {
9502 if (NumCurrentElementsDeserializing && !Decls) {
9503 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9504 return;
9505 }
9506
9507 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9508 if (!SemaObj) {
9509 // Queue this declaration so that it will be added to the
9510 // translation unit scope and identifier's declaration chain
9511 // once a Sema object is known.
9512 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9513 continue;
9514 }
9515
9516 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9517
9518 // If we're simply supposed to record the declarations, do so now.
9519 if (Decls) {
9520 Decls->push_back(Elt: D);
9521 continue;
9522 }
9523
9524 // Introduce this declaration into the translation-unit scope
9525 // and add it to the declaration chain for this identifier, so
9526 // that (unqualified) name lookup will find it.
9527 pushExternalDeclIntoScope(D, Name: II);
9528 }
9529}
9530
9531std::pair<ModuleFile *, unsigned>
9532ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9533 if (ID == 0)
9534 return {nullptr, 0};
9535
9536 unsigned ModuleFileIndex = ID >> 32;
9537 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9538
9539 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9540 assert(getModuleManager().size() > ModuleFileIndex - 1);
9541
9542 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9543 assert(LocalID < MF.LocalNumIdentifiers);
9544 return {&MF, MF.BaseIdentifierID + LocalID};
9545}
9546
9547IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9548 if (ID == 0)
9549 return nullptr;
9550
9551 if (IdentifiersLoaded.empty()) {
9552 Error(Msg: "no identifier table in AST file");
9553 return nullptr;
9554 }
9555
9556 auto [M, Index] = translateIdentifierIDToIndex(ID);
9557 if (!IdentifiersLoaded[Index]) {
9558 assert(M != nullptr && "Untranslated Identifier ID?");
9559 assert(Index >= M->BaseIdentifierID);
9560 unsigned LocalIndex = Index - M->BaseIdentifierID;
9561 const unsigned char *Data =
9562 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9563
9564 ASTIdentifierLookupTrait Trait(*this, *M);
9565 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9566 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9567 auto &II = PP.getIdentifierTable().get(Name: Key);
9568 IdentifiersLoaded[Index] = &II;
9569 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9570 markIdentifierFromAST(Reader&: *this, II, IsModule);
9571 if (DeserializationListener)
9572 DeserializationListener->IdentifierRead(ID, II: &II);
9573 }
9574
9575 return IdentifiersLoaded[Index];
9576}
9577
9578IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9579 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9580}
9581
9582IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9583 if (LocalID < NUM_PREDEF_IDENT_IDS)
9584 return LocalID;
9585
9586 if (!M.ModuleOffsetMap.empty())
9587 ReadModuleOffsetMap(F&: M);
9588
9589 unsigned ModuleFileIndex = LocalID >> 32;
9590 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9591 ModuleFile *MF =
9592 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9593 assert(MF && "malformed identifier ID encoding?");
9594
9595 if (!ModuleFileIndex)
9596 LocalID -= NUM_PREDEF_IDENT_IDS;
9597
9598 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
9599}
9600
9601MacroInfo *ASTReader::getMacro(MacroID ID) {
9602 if (ID == 0)
9603 return nullptr;
9604
9605 if (MacrosLoaded.empty()) {
9606 Error(Msg: "no macro table in AST file");
9607 return nullptr;
9608 }
9609
9610 ID -= NUM_PREDEF_MACRO_IDS;
9611 if (!MacrosLoaded[ID]) {
9612 GlobalMacroMapType::iterator I
9613 = GlobalMacroMap.find(K: ID + NUM_PREDEF_MACRO_IDS);
9614 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
9615 ModuleFile *M = I->second;
9616 unsigned Index = ID - M->BaseMacroID;
9617 MacrosLoaded[ID] =
9618 ReadMacroRecord(F&: *M, Offset: M->MacroOffsetsBase + M->MacroOffsets[Index]);
9619
9620 if (DeserializationListener)
9621 DeserializationListener->MacroRead(ID: ID + NUM_PREDEF_MACRO_IDS,
9622 MI: MacrosLoaded[ID]);
9623 }
9624
9625 return MacrosLoaded[ID];
9626}
9627
9628MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
9629 if (LocalID < NUM_PREDEF_MACRO_IDS)
9630 return LocalID;
9631
9632 if (!M.ModuleOffsetMap.empty())
9633 ReadModuleOffsetMap(F&: M);
9634
9635 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9636 = M.MacroRemap.find(K: LocalID - NUM_PREDEF_MACRO_IDS);
9637 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
9638
9639 return LocalID + I->second;
9640}
9641
9642serialization::SubmoduleID
9643ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
9644 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
9645 return LocalID;
9646
9647 if (!M.ModuleOffsetMap.empty())
9648 ReadModuleOffsetMap(F&: M);
9649
9650 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9651 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
9652 assert(I != M.SubmoduleRemap.end()
9653 && "Invalid index into submodule index remap");
9654
9655 return LocalID + I->second;
9656}
9657
9658Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
9659 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
9660 assert(GlobalID == 0 && "Unhandled global submodule ID");
9661 return nullptr;
9662 }
9663
9664 if (GlobalID > SubmodulesLoaded.size()) {
9665 Error(Msg: "submodule ID out of range in AST file");
9666 return nullptr;
9667 }
9668
9669 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
9670}
9671
9672Module *ASTReader::getModule(unsigned ID) {
9673 return getSubmodule(GlobalID: ID);
9674}
9675
9676ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
9677 if (ID & 1) {
9678 // It's a module, look it up by submodule ID.
9679 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
9680 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
9681 } else {
9682 // It's a prefix (preamble, PCH, ...). Look it up by index.
9683 int IndexFromEnd = static_cast<int>(ID >> 1);
9684 assert(IndexFromEnd && "got reference to unknown module file");
9685 return getModuleManager().pch_modules().end()[-static_cast<int>(IndexFromEnd)];
9686 }
9687}
9688
9689unsigned ASTReader::getModuleFileID(ModuleFile *M) {
9690 if (!M)
9691 return 1;
9692
9693 // For a file representing a module, use the submodule ID of the top-level
9694 // module as the file ID. For any other kind of file, the number of such
9695 // files loaded beforehand will be the same on reload.
9696 // FIXME: Is this true even if we have an explicit module file and a PCH?
9697 if (M->isModule())
9698 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
9699
9700 auto PCHModules = getModuleManager().pch_modules();
9701 auto I = llvm::find(Range&: PCHModules, Val: M);
9702 assert(I != PCHModules.end() && "emitting reference to unknown file");
9703 return std::distance(first: I, last: PCHModules.end()) << 1;
9704}
9705
9706std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
9707 if (Module *M = getSubmodule(GlobalID: ID))
9708 return ASTSourceDescriptor(*M);
9709
9710 // If there is only a single PCH, return it instead.
9711 // Chained PCH are not supported.
9712 const auto &PCHChain = ModuleMgr.pch_modules();
9713 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
9714 ModuleFile &MF = ModuleMgr.getPrimaryModule();
9715 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
9716 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
9717 return ASTSourceDescriptor(ModuleName,
9718 llvm::sys::path::parent_path(path: MF.FileName),
9719 FileName, MF.Signature);
9720 }
9721 return std::nullopt;
9722}
9723
9724ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9725 auto I = DefinitionSource.find(Val: FD);
9726 if (I == DefinitionSource.end())
9727 return EK_ReplyHazy;
9728 return I->second ? EK_Never : EK_Always;
9729}
9730
9731bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
9732 return ThisDeclarationWasADefinitionSet.contains(V: FD);
9733}
9734
9735Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9736 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
9737}
9738
9739Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9740 if (ID == 0)
9741 return Selector();
9742
9743 if (ID > SelectorsLoaded.size()) {
9744 Error(Msg: "selector ID out of range in AST file");
9745 return Selector();
9746 }
9747
9748 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9749 // Load this selector from the selector table.
9750 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
9751 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9752 ModuleFile &M = *I->second;
9753 ASTSelectorLookupTrait Trait(*this, M);
9754 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9755 SelectorsLoaded[ID - 1] =
9756 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9757 if (DeserializationListener)
9758 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
9759 }
9760
9761 return SelectorsLoaded[ID - 1];
9762}
9763
9764Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9765 return DecodeSelector(ID);
9766}
9767
9768uint32_t ASTReader::GetNumExternalSelectors() {
9769 // ID 0 (the null selector) is considered an external selector.
9770 return getTotalNumSelectors() + 1;
9771}
9772
9773serialization::SelectorID
9774ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9775 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9776 return LocalID;
9777
9778 if (!M.ModuleOffsetMap.empty())
9779 ReadModuleOffsetMap(F&: M);
9780
9781 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9782 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
9783 assert(I != M.SelectorRemap.end()
9784 && "Invalid index into selector index remap");
9785
9786 return LocalID + I->second;
9787}
9788
9789DeclarationNameLoc
9790ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9791 switch (Name.getNameKind()) {
9792 case DeclarationName::CXXConstructorName:
9793 case DeclarationName::CXXDestructorName:
9794 case DeclarationName::CXXConversionFunctionName:
9795 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
9796
9797 case DeclarationName::CXXOperatorName:
9798 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
9799
9800 case DeclarationName::CXXLiteralOperatorName:
9801 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9802 Loc: readSourceLocation());
9803
9804 case DeclarationName::Identifier:
9805 case DeclarationName::ObjCZeroArgSelector:
9806 case DeclarationName::ObjCOneArgSelector:
9807 case DeclarationName::ObjCMultiArgSelector:
9808 case DeclarationName::CXXUsingDirective:
9809 case DeclarationName::CXXDeductionGuideName:
9810 break;
9811 }
9812 return DeclarationNameLoc();
9813}
9814
9815DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9816 DeclarationNameInfo NameInfo;
9817 NameInfo.setName(readDeclarationName());
9818 NameInfo.setLoc(readSourceLocation());
9819 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
9820 return NameInfo;
9821}
9822
9823TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
9824 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
9825}
9826
9827SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
9828 auto Kind = readInt();
9829 auto ResultType = readQualType();
9830 auto Value = readAPInt();
9831 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
9832 assert(Op.isValid());
9833 return Op;
9834}
9835
9836void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9837 Info.QualifierLoc = readNestedNameSpecifierLoc();
9838 unsigned NumTPLists = readInt();
9839 Info.NumTemplParamLists = NumTPLists;
9840 if (NumTPLists) {
9841 Info.TemplParamLists =
9842 new (getContext()) TemplateParameterList *[NumTPLists];
9843 for (unsigned i = 0; i != NumTPLists; ++i)
9844 Info.TemplParamLists[i] = readTemplateParameterList();
9845 }
9846}
9847
9848TemplateParameterList *
9849ASTRecordReader::readTemplateParameterList() {
9850 SourceLocation TemplateLoc = readSourceLocation();
9851 SourceLocation LAngleLoc = readSourceLocation();
9852 SourceLocation RAngleLoc = readSourceLocation();
9853
9854 unsigned NumParams = readInt();
9855 SmallVector<NamedDecl *, 16> Params;
9856 Params.reserve(N: NumParams);
9857 while (NumParams--)
9858 Params.push_back(Elt: readDeclAs<NamedDecl>());
9859
9860 bool HasRequiresClause = readBool();
9861 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9862
9863 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9864 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9865 return TemplateParams;
9866}
9867
9868void ASTRecordReader::readTemplateArgumentList(
9869 SmallVectorImpl<TemplateArgument> &TemplArgs,
9870 bool Canonicalize) {
9871 unsigned NumTemplateArgs = readInt();
9872 TemplArgs.reserve(N: NumTemplateArgs);
9873 while (NumTemplateArgs--)
9874 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
9875}
9876
9877/// Read a UnresolvedSet structure.
9878void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9879 unsigned NumDecls = readInt();
9880 Set.reserve(C&: getContext(), N: NumDecls);
9881 while (NumDecls--) {
9882 GlobalDeclID ID = readDeclID();
9883 AccessSpecifier AS = (AccessSpecifier) readInt();
9884 Set.addLazyDecl(C&: getContext(), ID, AS);
9885 }
9886}
9887
9888CXXBaseSpecifier
9889ASTRecordReader::readCXXBaseSpecifier() {
9890 bool isVirtual = readBool();
9891 bool isBaseOfClass = readBool();
9892 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9893 bool inheritConstructors = readBool();
9894 TypeSourceInfo *TInfo = readTypeSourceInfo();
9895 SourceRange Range = readSourceRange();
9896 SourceLocation EllipsisLoc = readSourceLocation();
9897 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9898 EllipsisLoc);
9899 Result.setInheritConstructors(inheritConstructors);
9900 return Result;
9901}
9902
9903CXXCtorInitializer **
9904ASTRecordReader::readCXXCtorInitializers() {
9905 ASTContext &Context = getContext();
9906 unsigned NumInitializers = readInt();
9907 assert(NumInitializers && "wrote ctor initializers but have no inits");
9908 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9909 for (unsigned i = 0; i != NumInitializers; ++i) {
9910 TypeSourceInfo *TInfo = nullptr;
9911 bool IsBaseVirtual = false;
9912 FieldDecl *Member = nullptr;
9913 IndirectFieldDecl *IndirectMember = nullptr;
9914
9915 CtorInitializerType Type = (CtorInitializerType) readInt();
9916 switch (Type) {
9917 case CTOR_INITIALIZER_BASE:
9918 TInfo = readTypeSourceInfo();
9919 IsBaseVirtual = readBool();
9920 break;
9921
9922 case CTOR_INITIALIZER_DELEGATING:
9923 TInfo = readTypeSourceInfo();
9924 break;
9925
9926 case CTOR_INITIALIZER_MEMBER:
9927 Member = readDeclAs<FieldDecl>();
9928 break;
9929
9930 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9931 IndirectMember = readDeclAs<IndirectFieldDecl>();
9932 break;
9933 }
9934
9935 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9936 Expr *Init = readExpr();
9937 SourceLocation LParenLoc = readSourceLocation();
9938 SourceLocation RParenLoc = readSourceLocation();
9939
9940 CXXCtorInitializer *BOMInit;
9941 if (Type == CTOR_INITIALIZER_BASE)
9942 BOMInit = new (Context)
9943 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9944 RParenLoc, MemberOrEllipsisLoc);
9945 else if (Type == CTOR_INITIALIZER_DELEGATING)
9946 BOMInit = new (Context)
9947 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9948 else if (Member)
9949 BOMInit = new (Context)
9950 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9951 Init, RParenLoc);
9952 else
9953 BOMInit = new (Context)
9954 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9955 LParenLoc, Init, RParenLoc);
9956
9957 if (/*IsWritten*/readBool()) {
9958 unsigned SourceOrder = readInt();
9959 BOMInit->setSourceOrder(SourceOrder);
9960 }
9961
9962 CtorInitializers[i] = BOMInit;
9963 }
9964
9965 return CtorInitializers;
9966}
9967
9968NestedNameSpecifierLoc
9969ASTRecordReader::readNestedNameSpecifierLoc() {
9970 ASTContext &Context = getContext();
9971 unsigned N = readInt();
9972 NestedNameSpecifierLocBuilder Builder;
9973 for (unsigned I = 0; I != N; ++I) {
9974 auto Kind = readNestedNameSpecifierKind();
9975 switch (Kind) {
9976 case NestedNameSpecifier::Identifier: {
9977 IdentifierInfo *II = readIdentifier();
9978 SourceRange Range = readSourceRange();
9979 Builder.Extend(Context, Identifier: II, IdentifierLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9980 break;
9981 }
9982
9983 case NestedNameSpecifier::Namespace: {
9984 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9985 SourceRange Range = readSourceRange();
9986 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9987 break;
9988 }
9989
9990 case NestedNameSpecifier::NamespaceAlias: {
9991 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
9992 SourceRange Range = readSourceRange();
9993 Builder.Extend(Context, Alias, AliasLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9994 break;
9995 }
9996
9997 case NestedNameSpecifier::TypeSpec: {
9998 TypeSourceInfo *T = readTypeSourceInfo();
9999 if (!T)
10000 return NestedNameSpecifierLoc();
10001 SourceLocation ColonColonLoc = readSourceLocation();
10002 Builder.Extend(Context, TL: T->getTypeLoc(), ColonColonLoc);
10003 break;
10004 }
10005
10006 case NestedNameSpecifier::Global: {
10007 SourceLocation ColonColonLoc = readSourceLocation();
10008 Builder.MakeGlobal(Context, ColonColonLoc);
10009 break;
10010 }
10011
10012 case NestedNameSpecifier::Super: {
10013 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10014 SourceRange Range = readSourceRange();
10015 Builder.MakeSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10016 break;
10017 }
10018 }
10019 }
10020
10021 return Builder.getWithLocInContext(Context);
10022}
10023
10024SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10025 unsigned &Idx) {
10026 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10027 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10028 return SourceRange(beg, end);
10029}
10030
10031llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10032 const StringRef Blob) {
10033 unsigned Count = Record[0];
10034 const char *Byte = Blob.data();
10035 llvm::BitVector Ret = llvm::BitVector(Count, false);
10036 for (unsigned I = 0; I < Count; ++Byte)
10037 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10038 if (*Byte & (1 << Bit))
10039 Ret[I] = true;
10040 return Ret;
10041}
10042
10043/// Read a floating-point value
10044llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10045 return llvm::APFloat(Sem, readAPInt());
10046}
10047
10048// Read a string
10049std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10050 unsigned Len = Record[Idx++];
10051 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10052 Idx += Len;
10053 return Result;
10054}
10055
10056StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10057 StringRef &Blob) {
10058 unsigned Len = Record[Idx++];
10059 StringRef Result = Blob.substr(Start: 0, N: Len);
10060 Blob = Blob.substr(Start: Len);
10061 return Result;
10062}
10063
10064std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10065 unsigned &Idx) {
10066 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10067}
10068
10069std::string ASTReader::ReadPath(StringRef BaseDirectory,
10070 const RecordData &Record, unsigned &Idx) {
10071 std::string Filename = ReadString(Record, Idx);
10072 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10073}
10074
10075std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10076 const RecordData &Record, unsigned &Idx,
10077 StringRef &Blob) {
10078 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10079 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10080}
10081
10082VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10083 unsigned &Idx) {
10084 unsigned Major = Record[Idx++];
10085 unsigned Minor = Record[Idx++];
10086 unsigned Subminor = Record[Idx++];
10087 if (Minor == 0)
10088 return VersionTuple(Major);
10089 if (Subminor == 0)
10090 return VersionTuple(Major, Minor - 1);
10091 return VersionTuple(Major, Minor - 1, Subminor - 1);
10092}
10093
10094CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10095 const RecordData &Record,
10096 unsigned &Idx) {
10097 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10098 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10099}
10100
10101DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10102 return Diag(Loc: CurrentImportLoc, DiagID);
10103}
10104
10105DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10106 return Diags.Report(Loc, DiagID);
10107}
10108
10109void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10110 llvm::function_ref<void()> Fn) {
10111 // When Sema is available, avoid duplicate errors.
10112 if (SemaObj) {
10113 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10114 return;
10115 }
10116
10117 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10118}
10119
10120/// Retrieve the identifier table associated with the
10121/// preprocessor.
10122IdentifierTable &ASTReader::getIdentifierTable() {
10123 return PP.getIdentifierTable();
10124}
10125
10126/// Record that the given ID maps to the given switch-case
10127/// statement.
10128void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10129 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10130 "Already have a SwitchCase with this ID");
10131 (*CurrSwitchCaseStmts)[ID] = SC;
10132}
10133
10134/// Retrieve the switch-case statement with the given ID.
10135SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10136 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10137 return (*CurrSwitchCaseStmts)[ID];
10138}
10139
10140void ASTReader::ClearSwitchCaseIDs() {
10141 CurrSwitchCaseStmts->clear();
10142}
10143
10144void ASTReader::ReadComments() {
10145 ASTContext &Context = getContext();
10146 std::vector<RawComment *> Comments;
10147 for (SmallVectorImpl<std::pair<BitstreamCursor,
10148 serialization::ModuleFile *>>::iterator
10149 I = CommentsCursors.begin(),
10150 E = CommentsCursors.end();
10151 I != E; ++I) {
10152 Comments.clear();
10153 BitstreamCursor &Cursor = I->first;
10154 serialization::ModuleFile &F = *I->second;
10155 SavedStreamPosition SavedPosition(Cursor);
10156
10157 RecordData Record;
10158 while (true) {
10159 Expected<llvm::BitstreamEntry> MaybeEntry =
10160 Cursor.advanceSkippingSubblocks(
10161 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10162 if (!MaybeEntry) {
10163 Error(Err: MaybeEntry.takeError());
10164 return;
10165 }
10166 llvm::BitstreamEntry Entry = MaybeEntry.get();
10167
10168 switch (Entry.Kind) {
10169 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10170 case llvm::BitstreamEntry::Error:
10171 Error(Msg: "malformed block record in AST file");
10172 return;
10173 case llvm::BitstreamEntry::EndBlock:
10174 goto NextCursor;
10175 case llvm::BitstreamEntry::Record:
10176 // The interesting case.
10177 break;
10178 }
10179
10180 // Read a record.
10181 Record.clear();
10182 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10183 if (!MaybeComment) {
10184 Error(Err: MaybeComment.takeError());
10185 return;
10186 }
10187 switch ((CommentRecordTypes)MaybeComment.get()) {
10188 case COMMENTS_RAW_COMMENT: {
10189 unsigned Idx = 0;
10190 SourceRange SR = ReadSourceRange(F, Record, Idx);
10191 RawComment::CommentKind Kind =
10192 (RawComment::CommentKind) Record[Idx++];
10193 bool IsTrailingComment = Record[Idx++];
10194 bool IsAlmostTrailingComment = Record[Idx++];
10195 Comments.push_back(x: new (Context) RawComment(
10196 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10197 break;
10198 }
10199 }
10200 }
10201 NextCursor:
10202 for (RawComment *C : Comments) {
10203 SourceLocation CommentLoc = C->getBeginLoc();
10204 if (CommentLoc.isValid()) {
10205 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10206 if (Loc.first.isValid())
10207 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10208 }
10209 }
10210 }
10211}
10212
10213void ASTReader::visitInputFileInfos(
10214 serialization::ModuleFile &MF, bool IncludeSystem,
10215 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10216 bool IsSystem)>
10217 Visitor) {
10218 unsigned NumUserInputs = MF.NumUserInputFiles;
10219 unsigned NumInputs = MF.InputFilesLoaded.size();
10220 assert(NumUserInputs <= NumInputs);
10221 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10222 for (unsigned I = 0; I < N; ++I) {
10223 bool IsSystem = I >= NumUserInputs;
10224 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10225 Visitor(IFI, IsSystem);
10226 }
10227}
10228
10229void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10230 bool IncludeSystem, bool Complain,
10231 llvm::function_ref<void(const serialization::InputFile &IF,
10232 bool isSystem)> Visitor) {
10233 unsigned NumUserInputs = MF.NumUserInputFiles;
10234 unsigned NumInputs = MF.InputFilesLoaded.size();
10235 assert(NumUserInputs <= NumInputs);
10236 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10237 for (unsigned I = 0; I < N; ++I) {
10238 bool IsSystem = I >= NumUserInputs;
10239 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10240 Visitor(IF, IsSystem);
10241 }
10242}
10243
10244void ASTReader::visitTopLevelModuleMaps(
10245 serialization::ModuleFile &MF,
10246 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10247 unsigned NumInputs = MF.InputFilesLoaded.size();
10248 for (unsigned I = 0; I < NumInputs; ++I) {
10249 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10250 if (IFI.TopLevel && IFI.ModuleMap)
10251 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10252 Visitor(*FE);
10253 }
10254}
10255
10256void ASTReader::finishPendingActions() {
10257 while (!PendingIdentifierInfos.empty() ||
10258 !PendingDeducedFunctionTypes.empty() ||
10259 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10260 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10261 !PendingUpdateRecords.empty() ||
10262 !PendingObjCExtensionIvarRedeclarations.empty()) {
10263 // If any identifiers with corresponding top-level declarations have
10264 // been loaded, load those declarations now.
10265 using TopLevelDeclsMap =
10266 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10267 TopLevelDeclsMap TopLevelDecls;
10268
10269 while (!PendingIdentifierInfos.empty()) {
10270 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10271 SmallVector<GlobalDeclID, 4> DeclIDs =
10272 std::move(PendingIdentifierInfos.back().second);
10273 PendingIdentifierInfos.pop_back();
10274
10275 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10276 }
10277
10278 // Load each function type that we deferred loading because it was a
10279 // deduced type that might refer to a local type declared within itself.
10280 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10281 auto *FD = PendingDeducedFunctionTypes[I].first;
10282 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10283
10284 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10285 // If we gave a function a deduced return type, remember that we need to
10286 // propagate that along the redeclaration chain.
10287 if (DT->isDeduced()) {
10288 PendingDeducedTypeUpdates.insert(
10289 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10290 continue;
10291 }
10292
10293 // The function has undeduced DeduceType return type. We hope we can
10294 // find the deduced type by iterating the redecls in other modules
10295 // later.
10296 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10297 continue;
10298 }
10299 }
10300 PendingDeducedFunctionTypes.clear();
10301
10302 // Load each variable type that we deferred loading because it was a
10303 // deduced type that might refer to a local type declared within itself.
10304 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10305 auto *VD = PendingDeducedVarTypes[I].first;
10306 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10307 }
10308 PendingDeducedVarTypes.clear();
10309
10310 // Load pending declaration chains.
10311 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10312 loadPendingDeclChain(D: PendingDeclChains[I].first,
10313 LocalOffset: PendingDeclChains[I].second);
10314 PendingDeclChains.clear();
10315
10316 // Make the most recent of the top-level declarations visible.
10317 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10318 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10319 IdentifierInfo *II = TLD->first;
10320 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10321 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10322 }
10323 }
10324
10325 // Load any pending macro definitions.
10326 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10327 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10328 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10329 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10330 // Initialize the macro history from chained-PCHs ahead of module imports.
10331 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10332 ++IDIdx) {
10333 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10334 if (!Info.M->isModule())
10335 resolvePendingMacro(II, PMInfo: Info);
10336 }
10337 // Handle module imports.
10338 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10339 ++IDIdx) {
10340 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10341 if (Info.M->isModule())
10342 resolvePendingMacro(II, PMInfo: Info);
10343 }
10344 }
10345 PendingMacroIDs.clear();
10346
10347 // Wire up the DeclContexts for Decls that we delayed setting until
10348 // recursive loading is completed.
10349 while (!PendingDeclContextInfos.empty()) {
10350 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10351 PendingDeclContextInfos.pop_front();
10352 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10353 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10354 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10355 }
10356
10357 // Perform any pending declaration updates.
10358 while (!PendingUpdateRecords.empty()) {
10359 auto Update = PendingUpdateRecords.pop_back_val();
10360 ReadingKindTracker ReadingKind(Read_Decl, *this);
10361 loadDeclUpdateRecords(Record&: Update);
10362 }
10363
10364 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10365 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10366 auto DuplicateIvars =
10367 PendingObjCExtensionIvarRedeclarations.back().second;
10368 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10369 StructuralEquivalenceContext Ctx(
10370 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10371 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10372 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10373 /*Complain =*/false,
10374 /*ErrorOnTagTypeMismatch =*/true);
10375 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10376 // Merge redeclared ivars with their predecessors.
10377 for (auto IvarPair : DuplicateIvars) {
10378 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10379 // Change semantic DeclContext but keep the lexical one.
10380 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10381 LexicalDC: Ivar->getLexicalDeclContext(),
10382 Ctx&: getContext());
10383 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10384 }
10385 // Invalidate duplicate extension and the cached ivar list.
10386 ExtensionsPair.first->setInvalidDecl();
10387 ExtensionsPair.second->getClassInterface()
10388 ->getDefinition()
10389 ->setIvarList(nullptr);
10390 } else {
10391 for (auto IvarPair : DuplicateIvars) {
10392 Diag(Loc: IvarPair.first->getLocation(),
10393 DiagID: diag::err_duplicate_ivar_declaration)
10394 << IvarPair.first->getIdentifier();
10395 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10396 }
10397 }
10398 PendingObjCExtensionIvarRedeclarations.pop_back();
10399 }
10400 }
10401
10402 // At this point, all update records for loaded decls are in place, so any
10403 // fake class definitions should have become real.
10404 assert(PendingFakeDefinitionData.empty() &&
10405 "faked up a class definition but never saw the real one");
10406
10407 // If we deserialized any C++ or Objective-C class definitions, any
10408 // Objective-C protocol definitions, or any redeclarable templates, make sure
10409 // that all redeclarations point to the definitions. Note that this can only
10410 // happen now, after the redeclaration chains have been fully wired.
10411 for (Decl *D : PendingDefinitions) {
10412 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10413 if (const TagType *TagT = dyn_cast<TagType>(Val: TD->getTypeForDecl())) {
10414 // Make sure that the TagType points at the definition.
10415 const_cast<TagType*>(TagT)->decl = TD;
10416 }
10417
10418 if (auto RD = dyn_cast<CXXRecordDecl>(Val: D)) {
10419 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10420 R = R->getPreviousDecl()) {
10421 assert((R == D) ==
10422 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10423 "declaration thinks it's the definition but it isn't");
10424 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10425 }
10426 }
10427
10428 continue;
10429 }
10430
10431 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10432 // Make sure that the ObjCInterfaceType points at the definition.
10433 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10434 ->Decl = ID;
10435
10436 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10437 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10438
10439 continue;
10440 }
10441
10442 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10443 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10444 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10445
10446 continue;
10447 }
10448
10449 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10450 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10451 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10452 }
10453 PendingDefinitions.clear();
10454
10455 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10456 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10457 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10458 return VD->isThisDeclarationADefinition() ||
10459 VD->isThisDeclarationADemotedDefinition();
10460
10461 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10462 return TD->isThisDeclarationADefinition() ||
10463 TD->isThisDeclarationADemotedDefinition();
10464
10465 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10466 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10467
10468 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10469 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10470
10471 // Conservatively return false here.
10472 return false;
10473 };
10474
10475 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10476 return hasDefinitionImpl(D, hasDefinitionImpl);
10477 };
10478
10479 // It is not good to prevent multiple declarations since the forward
10480 // declaration is common. Let's try to avoid duplicated definitions
10481 // only.
10482 if (!hasDefinition(D) || !hasDefinition(Previous))
10483 continue;
10484
10485 Module *PM = Previous->getOwningModule();
10486 Module *DM = D->getOwningModule();
10487 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10488 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10489 << (DM ? DM->getTopLevelModuleName() : "global module");
10490 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10491 }
10492 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10493
10494 // Load the bodies of any functions or methods we've encountered. We do
10495 // this now (delayed) so that we can be sure that the declaration chains
10496 // have been fully wired up (hasBody relies on this).
10497 // FIXME: We shouldn't require complete redeclaration chains here.
10498 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10499 PBEnd = PendingBodies.end();
10500 PB != PBEnd; ++PB) {
10501 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10502 // FIXME: Check for =delete/=default?
10503 const FunctionDecl *Defn = nullptr;
10504 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10505 FD->setLazyBody(PB->second);
10506 } else {
10507 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10508 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10509
10510 if (!FD->isLateTemplateParsed() &&
10511 !NonConstDefn->isLateTemplateParsed() &&
10512 // We only perform ODR checks for decls not in the explicit
10513 // global module fragment.
10514 !shouldSkipCheckingODR(D: FD) &&
10515 !shouldSkipCheckingODR(D: NonConstDefn) &&
10516 FD->getODRHash() != NonConstDefn->getODRHash()) {
10517 if (!isa<CXXMethodDecl>(Val: FD)) {
10518 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10519 } else if (FD->getLexicalParent()->isFileContext() &&
10520 NonConstDefn->getLexicalParent()->isFileContext()) {
10521 // Only diagnose out-of-line method definitions. If they are
10522 // in class definitions, then an error will be generated when
10523 // processing the class bodies.
10524 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10525 }
10526 }
10527 }
10528 continue;
10529 }
10530
10531 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10532 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10533 MD->setLazyBody(PB->second);
10534 }
10535 PendingBodies.clear();
10536
10537 // Inform any classes that had members added that they now have more members.
10538 for (auto [RD, MD] : PendingAddedClassMembers) {
10539 RD->addedMember(D: MD);
10540 }
10541 PendingAddedClassMembers.clear();
10542
10543 // Do some cleanup.
10544 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10545 getContext().deduplicateMergedDefinitionsFor(ND);
10546 PendingMergedDefinitionsToDeduplicate.clear();
10547
10548 // For each decl chain that we wanted to complete while deserializing, mark
10549 // it as "still needs to be completed".
10550 for (Decl *D : PendingIncompleteDeclChains)
10551 markIncompleteDeclChain(D);
10552 PendingIncompleteDeclChains.clear();
10553
10554 assert(PendingIdentifierInfos.empty() &&
10555 "Should be empty at the end of finishPendingActions");
10556 assert(PendingDeducedFunctionTypes.empty() &&
10557 "Should be empty at the end of finishPendingActions");
10558 assert(PendingDeducedVarTypes.empty() &&
10559 "Should be empty at the end of finishPendingActions");
10560 assert(PendingDeclChains.empty() &&
10561 "Should be empty at the end of finishPendingActions");
10562 assert(PendingMacroIDs.empty() &&
10563 "Should be empty at the end of finishPendingActions");
10564 assert(PendingDeclContextInfos.empty() &&
10565 "Should be empty at the end of finishPendingActions");
10566 assert(PendingUpdateRecords.empty() &&
10567 "Should be empty at the end of finishPendingActions");
10568 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10569 "Should be empty at the end of finishPendingActions");
10570 assert(PendingFakeDefinitionData.empty() &&
10571 "Should be empty at the end of finishPendingActions");
10572 assert(PendingDefinitions.empty() &&
10573 "Should be empty at the end of finishPendingActions");
10574 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10575 "Should be empty at the end of finishPendingActions");
10576 assert(PendingBodies.empty() &&
10577 "Should be empty at the end of finishPendingActions");
10578 assert(PendingAddedClassMembers.empty() &&
10579 "Should be empty at the end of finishPendingActions");
10580 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10581 "Should be empty at the end of finishPendingActions");
10582 assert(PendingIncompleteDeclChains.empty() &&
10583 "Should be empty at the end of finishPendingActions");
10584}
10585
10586void ASTReader::diagnoseOdrViolations() {
10587 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10588 PendingRecordOdrMergeFailures.empty() &&
10589 PendingFunctionOdrMergeFailures.empty() &&
10590 PendingEnumOdrMergeFailures.empty() &&
10591 PendingObjCInterfaceOdrMergeFailures.empty() &&
10592 PendingObjCProtocolOdrMergeFailures.empty())
10593 return;
10594
10595 // Trigger the import of the full definition of each class that had any
10596 // odr-merging problems, so we can produce better diagnostics for them.
10597 // These updates may in turn find and diagnose some ODR failures, so take
10598 // ownership of the set first.
10599 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10600 PendingOdrMergeFailures.clear();
10601 for (auto &Merge : OdrMergeFailures) {
10602 Merge.first->buildLookup();
10603 Merge.first->decls_begin();
10604 Merge.first->bases_begin();
10605 Merge.first->vbases_begin();
10606 for (auto &RecordPair : Merge.second) {
10607 auto *RD = RecordPair.first;
10608 RD->decls_begin();
10609 RD->bases_begin();
10610 RD->vbases_begin();
10611 }
10612 }
10613
10614 // Trigger the import of the full definition of each record in C/ObjC.
10615 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
10616 PendingRecordOdrMergeFailures.clear();
10617 for (auto &Merge : RecordOdrMergeFailures) {
10618 Merge.first->decls_begin();
10619 for (auto &D : Merge.second)
10620 D->decls_begin();
10621 }
10622
10623 // Trigger the import of the full interface definition.
10624 auto ObjCInterfaceOdrMergeFailures =
10625 std::move(PendingObjCInterfaceOdrMergeFailures);
10626 PendingObjCInterfaceOdrMergeFailures.clear();
10627 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10628 Merge.first->decls_begin();
10629 for (auto &InterfacePair : Merge.second)
10630 InterfacePair.first->decls_begin();
10631 }
10632
10633 // Trigger the import of functions.
10634 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10635 PendingFunctionOdrMergeFailures.clear();
10636 for (auto &Merge : FunctionOdrMergeFailures) {
10637 Merge.first->buildLookup();
10638 Merge.first->decls_begin();
10639 Merge.first->getBody();
10640 for (auto &FD : Merge.second) {
10641 FD->buildLookup();
10642 FD->decls_begin();
10643 FD->getBody();
10644 }
10645 }
10646
10647 // Trigger the import of enums.
10648 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10649 PendingEnumOdrMergeFailures.clear();
10650 for (auto &Merge : EnumOdrMergeFailures) {
10651 Merge.first->decls_begin();
10652 for (auto &Enum : Merge.second) {
10653 Enum->decls_begin();
10654 }
10655 }
10656
10657 // Trigger the import of the full protocol definition.
10658 auto ObjCProtocolOdrMergeFailures =
10659 std::move(PendingObjCProtocolOdrMergeFailures);
10660 PendingObjCProtocolOdrMergeFailures.clear();
10661 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10662 Merge.first->decls_begin();
10663 for (auto &ProtocolPair : Merge.second)
10664 ProtocolPair.first->decls_begin();
10665 }
10666
10667 // For each declaration from a merged context, check that the canonical
10668 // definition of that context also contains a declaration of the same
10669 // entity.
10670 //
10671 // Caution: this loop does things that might invalidate iterators into
10672 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10673 while (!PendingOdrMergeChecks.empty()) {
10674 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10675
10676 // FIXME: Skip over implicit declarations for now. This matters for things
10677 // like implicitly-declared special member functions. This isn't entirely
10678 // correct; we can end up with multiple unmerged declarations of the same
10679 // implicit entity.
10680 if (D->isImplicit())
10681 continue;
10682
10683 DeclContext *CanonDef = D->getDeclContext();
10684
10685 bool Found = false;
10686 const Decl *DCanon = D->getCanonicalDecl();
10687
10688 for (auto *RI : D->redecls()) {
10689 if (RI->getLexicalDeclContext() == CanonDef) {
10690 Found = true;
10691 break;
10692 }
10693 }
10694 if (Found)
10695 continue;
10696
10697 // Quick check failed, time to do the slow thing. Note, we can't just
10698 // look up the name of D in CanonDef here, because the member that is
10699 // in CanonDef might not be found by name lookup (it might have been
10700 // replaced by a more recent declaration in the lookup table), and we
10701 // can't necessarily find it in the redeclaration chain because it might
10702 // be merely mergeable, not redeclarable.
10703 llvm::SmallVector<const NamedDecl*, 4> Candidates;
10704 for (auto *CanonMember : CanonDef->decls()) {
10705 if (CanonMember->getCanonicalDecl() == DCanon) {
10706 // This can happen if the declaration is merely mergeable and not
10707 // actually redeclarable (we looked for redeclarations earlier).
10708 //
10709 // FIXME: We should be able to detect this more efficiently, without
10710 // pulling in all of the members of CanonDef.
10711 Found = true;
10712 break;
10713 }
10714 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
10715 if (ND->getDeclName() == D->getDeclName())
10716 Candidates.push_back(Elt: ND);
10717 }
10718
10719 if (!Found) {
10720 // The AST doesn't like TagDecls becoming invalid after they've been
10721 // completed. We only really need to mark FieldDecls as invalid here.
10722 if (!isa<TagDecl>(Val: D))
10723 D->setInvalidDecl();
10724
10725 // Ensure we don't accidentally recursively enter deserialization while
10726 // we're producing our diagnostic.
10727 Deserializing RecursionGuard(this);
10728
10729 std::string CanonDefModule =
10730 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
10731 D: cast<Decl>(Val: CanonDef));
10732 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
10733 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
10734 << CanonDef << CanonDefModule.empty() << CanonDefModule;
10735
10736 if (Candidates.empty())
10737 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
10738 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
10739 else {
10740 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10741 Diag(Loc: Candidates[I]->getLocation(),
10742 DiagID: diag::note_module_odr_violation_possible_decl)
10743 << Candidates[I];
10744 }
10745
10746 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
10747 }
10748 }
10749
10750 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
10751 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
10752 ObjCInterfaceOdrMergeFailures.empty() &&
10753 ObjCProtocolOdrMergeFailures.empty())
10754 return;
10755
10756 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
10757 getPreprocessor().getLangOpts());
10758
10759 // Issue any pending ODR-failure diagnostics.
10760 for (auto &Merge : OdrMergeFailures) {
10761 // If we've already pointed out a specific problem with this class, don't
10762 // bother issuing a general "something's different" diagnostic.
10763 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10764 continue;
10765
10766 bool Diagnosed = false;
10767 CXXRecordDecl *FirstRecord = Merge.first;
10768 for (auto &RecordPair : Merge.second) {
10769 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
10770 SecondDD: RecordPair.second)) {
10771 Diagnosed = true;
10772 break;
10773 }
10774 }
10775
10776 if (!Diagnosed) {
10777 // All definitions are updates to the same declaration. This happens if a
10778 // module instantiates the declaration of a class template specialization
10779 // and two or more other modules instantiate its definition.
10780 //
10781 // FIXME: Indicate which modules had instantiations of this definition.
10782 // FIXME: How can this even happen?
10783 Diag(Loc: Merge.first->getLocation(),
10784 DiagID: diag::err_module_odr_violation_different_instantiations)
10785 << Merge.first;
10786 }
10787 }
10788
10789 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
10790 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
10791 for (auto &Merge : RecordOdrMergeFailures) {
10792 // If we've already pointed out a specific problem with this class, don't
10793 // bother issuing a general "something's different" diagnostic.
10794 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10795 continue;
10796
10797 RecordDecl *FirstRecord = Merge.first;
10798 bool Diagnosed = false;
10799 for (auto *SecondRecord : Merge.second) {
10800 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
10801 Diagnosed = true;
10802 break;
10803 }
10804 }
10805 (void)Diagnosed;
10806 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10807 }
10808
10809 // Issue ODR failures diagnostics for functions.
10810 for (auto &Merge : FunctionOdrMergeFailures) {
10811 FunctionDecl *FirstFunction = Merge.first;
10812 bool Diagnosed = false;
10813 for (auto &SecondFunction : Merge.second) {
10814 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
10815 Diagnosed = true;
10816 break;
10817 }
10818 }
10819 (void)Diagnosed;
10820 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10821 }
10822
10823 // Issue ODR failures diagnostics for enums.
10824 for (auto &Merge : EnumOdrMergeFailures) {
10825 // If we've already pointed out a specific problem with this enum, don't
10826 // bother issuing a general "something's different" diagnostic.
10827 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10828 continue;
10829
10830 EnumDecl *FirstEnum = Merge.first;
10831 bool Diagnosed = false;
10832 for (auto &SecondEnum : Merge.second) {
10833 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
10834 Diagnosed = true;
10835 break;
10836 }
10837 }
10838 (void)Diagnosed;
10839 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10840 }
10841
10842 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10843 // If we've already pointed out a specific problem with this interface,
10844 // don't bother issuing a general "something's different" diagnostic.
10845 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10846 continue;
10847
10848 bool Diagnosed = false;
10849 ObjCInterfaceDecl *FirstID = Merge.first;
10850 for (auto &InterfacePair : Merge.second) {
10851 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
10852 SecondDD: InterfacePair.second)) {
10853 Diagnosed = true;
10854 break;
10855 }
10856 }
10857 (void)Diagnosed;
10858 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10859 }
10860
10861 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10862 // If we've already pointed out a specific problem with this protocol,
10863 // don't bother issuing a general "something's different" diagnostic.
10864 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10865 continue;
10866
10867 ObjCProtocolDecl *FirstProtocol = Merge.first;
10868 bool Diagnosed = false;
10869 for (auto &ProtocolPair : Merge.second) {
10870 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
10871 SecondDD: ProtocolPair.second)) {
10872 Diagnosed = true;
10873 break;
10874 }
10875 }
10876 (void)Diagnosed;
10877 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10878 }
10879}
10880
10881void ASTReader::StartedDeserializing() {
10882 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10883 ReadTimer->startTimer();
10884}
10885
10886void ASTReader::FinishedDeserializing() {
10887 assert(NumCurrentElementsDeserializing &&
10888 "FinishedDeserializing not paired with StartedDeserializing");
10889 if (NumCurrentElementsDeserializing == 1) {
10890 // We decrease NumCurrentElementsDeserializing only after pending actions
10891 // are finished, to avoid recursively re-calling finishPendingActions().
10892 finishPendingActions();
10893 }
10894 --NumCurrentElementsDeserializing;
10895
10896 if (NumCurrentElementsDeserializing == 0) {
10897 {
10898 // Guard variable to avoid recursively entering the process of passing
10899 // decls to consumer.
10900 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
10901 /*NewValue=*/false);
10902
10903 // Propagate exception specification and deduced type updates along
10904 // redeclaration chains.
10905 //
10906 // We do this now rather than in finishPendingActions because we want to
10907 // be able to walk the complete redeclaration chains of the updated decls.
10908 while (!PendingExceptionSpecUpdates.empty() ||
10909 !PendingDeducedTypeUpdates.empty() ||
10910 !PendingUndeducedFunctionDecls.empty()) {
10911 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10912 PendingExceptionSpecUpdates.clear();
10913 for (auto Update : ESUpdates) {
10914 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10915 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10916 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10917 if (auto *Listener = getContext().getASTMutationListener())
10918 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
10919 for (auto *Redecl : Update.second->redecls())
10920 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
10921 }
10922
10923 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10924 PendingDeducedTypeUpdates.clear();
10925 for (auto Update : DTUpdates) {
10926 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10927 // FIXME: If the return type is already deduced, check that it
10928 // matches.
10929 getContext().adjustDeducedFunctionResultType(FD: Update.first,
10930 ResultType: Update.second);
10931 }
10932
10933 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10934 PendingUndeducedFunctionDecls.clear();
10935 // We hope we can find the deduced type for the functions by iterating
10936 // redeclarations in other modules.
10937 for (FunctionDecl *UndeducedFD : UDTUpdates)
10938 (void)UndeducedFD->getMostRecentDecl();
10939 }
10940
10941 if (ReadTimer)
10942 ReadTimer->stopTimer();
10943
10944 diagnoseOdrViolations();
10945 }
10946
10947 // We are not in recursive loading, so it's safe to pass the "interesting"
10948 // decls to the consumer.
10949 if (Consumer)
10950 PassInterestingDeclsToConsumer();
10951 }
10952}
10953
10954void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10955 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10956 // Remove any fake results before adding any real ones.
10957 auto It = PendingFakeLookupResults.find(Key: II);
10958 if (It != PendingFakeLookupResults.end()) {
10959 for (auto *ND : It->second)
10960 SemaObj->IdResolver.RemoveDecl(D: ND);
10961 // FIXME: this works around module+PCH performance issue.
10962 // Rather than erase the result from the map, which is O(n), just clear
10963 // the vector of NamedDecls.
10964 It->second.clear();
10965 }
10966 }
10967
10968 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10969 SemaObj->TUScope->AddDecl(D);
10970 } else if (SemaObj->TUScope) {
10971 // Adding the decl to IdResolver may have failed because it was already in
10972 // (even though it was not added in scope). If it is already in, make sure
10973 // it gets in the scope as well.
10974 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
10975 SemaObj->TUScope->AddDecl(D);
10976 }
10977}
10978
10979ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
10980 ASTContext *Context,
10981 const PCHContainerReader &PCHContainerRdr,
10982 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10983 StringRef isysroot,
10984 DisableValidationForModuleKind DisableValidationKind,
10985 bool AllowASTWithCompilerErrors,
10986 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10987 bool ForceValidateUserInputs,
10988 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10989 std::unique_ptr<llvm::Timer> ReadTimer)
10990 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
10991 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
10992 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
10993 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
10994 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
10995 StackHandler(Diags), PP(PP), ContextObj(Context),
10996 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
10997 PP.getHeaderSearchInfo()),
10998 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
10999 DisableValidationKind(DisableValidationKind),
11000 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11001 AllowConfigurationMismatch(AllowConfigurationMismatch),
11002 ValidateSystemInputs(ValidateSystemInputs),
11003 ForceValidateUserInputs(ForceValidateUserInputs),
11004 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11005 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11006 SourceMgr.setExternalSLocEntrySource(this);
11007
11008 PathBuf.reserve(N: 256);
11009
11010 for (const auto &Ext : Extensions) {
11011 auto BlockName = Ext->getExtensionMetadata().BlockName;
11012 auto Known = ModuleFileExtensions.find(Key: BlockName);
11013 if (Known != ModuleFileExtensions.end()) {
11014 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11015 << BlockName;
11016 continue;
11017 }
11018
11019 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11020 }
11021}
11022
11023ASTReader::~ASTReader() {
11024 if (OwnsDeserializationListener)
11025 delete DeserializationListener;
11026}
11027
11028IdentifierResolver &ASTReader::getIdResolver() {
11029 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11030}
11031
11032Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11033 unsigned AbbrevID) {
11034 Idx = 0;
11035 Record.clear();
11036 return Cursor.readRecord(AbbrevID, Vals&: Record);
11037}
11038//===----------------------------------------------------------------------===//
11039//// OMPClauseReader implementation
11040////===----------------------------------------------------------------------===//
11041
11042// This has to be in namespace clang because it's friended by all
11043// of the OMP clauses.
11044namespace clang {
11045
11046class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11047 ASTRecordReader &Record;
11048 ASTContext &Context;
11049
11050public:
11051 OMPClauseReader(ASTRecordReader &Record)
11052 : Record(Record), Context(Record.getContext()) {}
11053#define GEN_CLANG_CLAUSE_CLASS
11054#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11055#include "llvm/Frontend/OpenMP/OMP.inc"
11056 OMPClause *readClause();
11057 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11058 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11059};
11060
11061} // end namespace clang
11062
11063OMPClause *ASTRecordReader::readOMPClause() {
11064 return OMPClauseReader(*this).readClause();
11065}
11066
11067OMPClause *OMPClauseReader::readClause() {
11068 OMPClause *C = nullptr;
11069 switch (llvm::omp::Clause(Record.readInt())) {
11070 case llvm::omp::OMPC_if:
11071 C = new (Context) OMPIfClause();
11072 break;
11073 case llvm::omp::OMPC_final:
11074 C = new (Context) OMPFinalClause();
11075 break;
11076 case llvm::omp::OMPC_num_threads:
11077 C = new (Context) OMPNumThreadsClause();
11078 break;
11079 case llvm::omp::OMPC_safelen:
11080 C = new (Context) OMPSafelenClause();
11081 break;
11082 case llvm::omp::OMPC_simdlen:
11083 C = new (Context) OMPSimdlenClause();
11084 break;
11085 case llvm::omp::OMPC_sizes: {
11086 unsigned NumSizes = Record.readInt();
11087 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11088 break;
11089 }
11090 case llvm::omp::OMPC_permutation: {
11091 unsigned NumLoops = Record.readInt();
11092 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11093 break;
11094 }
11095 case llvm::omp::OMPC_full:
11096 C = OMPFullClause::CreateEmpty(C: Context);
11097 break;
11098 case llvm::omp::OMPC_partial:
11099 C = OMPPartialClause::CreateEmpty(C: Context);
11100 break;
11101 case llvm::omp::OMPC_allocator:
11102 C = new (Context) OMPAllocatorClause();
11103 break;
11104 case llvm::omp::OMPC_collapse:
11105 C = new (Context) OMPCollapseClause();
11106 break;
11107 case llvm::omp::OMPC_default:
11108 C = new (Context) OMPDefaultClause();
11109 break;
11110 case llvm::omp::OMPC_proc_bind:
11111 C = new (Context) OMPProcBindClause();
11112 break;
11113 case llvm::omp::OMPC_schedule:
11114 C = new (Context) OMPScheduleClause();
11115 break;
11116 case llvm::omp::OMPC_ordered:
11117 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11118 break;
11119 case llvm::omp::OMPC_nowait:
11120 C = new (Context) OMPNowaitClause();
11121 break;
11122 case llvm::omp::OMPC_untied:
11123 C = new (Context) OMPUntiedClause();
11124 break;
11125 case llvm::omp::OMPC_mergeable:
11126 C = new (Context) OMPMergeableClause();
11127 break;
11128 case llvm::omp::OMPC_read:
11129 C = new (Context) OMPReadClause();
11130 break;
11131 case llvm::omp::OMPC_write:
11132 C = new (Context) OMPWriteClause();
11133 break;
11134 case llvm::omp::OMPC_update:
11135 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11136 break;
11137 case llvm::omp::OMPC_capture:
11138 C = new (Context) OMPCaptureClause();
11139 break;
11140 case llvm::omp::OMPC_compare:
11141 C = new (Context) OMPCompareClause();
11142 break;
11143 case llvm::omp::OMPC_fail:
11144 C = new (Context) OMPFailClause();
11145 break;
11146 case llvm::omp::OMPC_seq_cst:
11147 C = new (Context) OMPSeqCstClause();
11148 break;
11149 case llvm::omp::OMPC_acq_rel:
11150 C = new (Context) OMPAcqRelClause();
11151 break;
11152 case llvm::omp::OMPC_absent: {
11153 unsigned NumKinds = Record.readInt();
11154 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11155 break;
11156 }
11157 case llvm::omp::OMPC_holds:
11158 C = new (Context) OMPHoldsClause();
11159 break;
11160 case llvm::omp::OMPC_contains: {
11161 unsigned NumKinds = Record.readInt();
11162 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11163 break;
11164 }
11165 case llvm::omp::OMPC_no_openmp:
11166 C = new (Context) OMPNoOpenMPClause();
11167 break;
11168 case llvm::omp::OMPC_no_openmp_routines:
11169 C = new (Context) OMPNoOpenMPRoutinesClause();
11170 break;
11171 case llvm::omp::OMPC_no_openmp_constructs:
11172 C = new (Context) OMPNoOpenMPConstructsClause();
11173 break;
11174 case llvm::omp::OMPC_no_parallelism:
11175 C = new (Context) OMPNoParallelismClause();
11176 break;
11177 case llvm::omp::OMPC_acquire:
11178 C = new (Context) OMPAcquireClause();
11179 break;
11180 case llvm::omp::OMPC_release:
11181 C = new (Context) OMPReleaseClause();
11182 break;
11183 case llvm::omp::OMPC_relaxed:
11184 C = new (Context) OMPRelaxedClause();
11185 break;
11186 case llvm::omp::OMPC_weak:
11187 C = new (Context) OMPWeakClause();
11188 break;
11189 case llvm::omp::OMPC_threads:
11190 C = new (Context) OMPThreadsClause();
11191 break;
11192 case llvm::omp::OMPC_simd:
11193 C = new (Context) OMPSIMDClause();
11194 break;
11195 case llvm::omp::OMPC_nogroup:
11196 C = new (Context) OMPNogroupClause();
11197 break;
11198 case llvm::omp::OMPC_unified_address:
11199 C = new (Context) OMPUnifiedAddressClause();
11200 break;
11201 case llvm::omp::OMPC_unified_shared_memory:
11202 C = new (Context) OMPUnifiedSharedMemoryClause();
11203 break;
11204 case llvm::omp::OMPC_reverse_offload:
11205 C = new (Context) OMPReverseOffloadClause();
11206 break;
11207 case llvm::omp::OMPC_dynamic_allocators:
11208 C = new (Context) OMPDynamicAllocatorsClause();
11209 break;
11210 case llvm::omp::OMPC_atomic_default_mem_order:
11211 C = new (Context) OMPAtomicDefaultMemOrderClause();
11212 break;
11213 case llvm::omp::OMPC_self_maps:
11214 C = new (Context) OMPSelfMapsClause();
11215 break;
11216 case llvm::omp::OMPC_at:
11217 C = new (Context) OMPAtClause();
11218 break;
11219 case llvm::omp::OMPC_severity:
11220 C = new (Context) OMPSeverityClause();
11221 break;
11222 case llvm::omp::OMPC_message:
11223 C = new (Context) OMPMessageClause();
11224 break;
11225 case llvm::omp::OMPC_private:
11226 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11227 break;
11228 case llvm::omp::OMPC_firstprivate:
11229 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11230 break;
11231 case llvm::omp::OMPC_lastprivate:
11232 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11233 break;
11234 case llvm::omp::OMPC_shared:
11235 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11236 break;
11237 case llvm::omp::OMPC_reduction: {
11238 unsigned N = Record.readInt();
11239 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11240 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11241 break;
11242 }
11243 case llvm::omp::OMPC_task_reduction:
11244 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11245 break;
11246 case llvm::omp::OMPC_in_reduction:
11247 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11248 break;
11249 case llvm::omp::OMPC_linear:
11250 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11251 break;
11252 case llvm::omp::OMPC_aligned:
11253 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11254 break;
11255 case llvm::omp::OMPC_copyin:
11256 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11257 break;
11258 case llvm::omp::OMPC_copyprivate:
11259 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11260 break;
11261 case llvm::omp::OMPC_flush:
11262 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11263 break;
11264 case llvm::omp::OMPC_depobj:
11265 C = OMPDepobjClause::CreateEmpty(C: Context);
11266 break;
11267 case llvm::omp::OMPC_depend: {
11268 unsigned NumVars = Record.readInt();
11269 unsigned NumLoops = Record.readInt();
11270 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11271 break;
11272 }
11273 case llvm::omp::OMPC_device:
11274 C = new (Context) OMPDeviceClause();
11275 break;
11276 case llvm::omp::OMPC_map: {
11277 OMPMappableExprListSizeTy Sizes;
11278 Sizes.NumVars = Record.readInt();
11279 Sizes.NumUniqueDeclarations = Record.readInt();
11280 Sizes.NumComponentLists = Record.readInt();
11281 Sizes.NumComponents = Record.readInt();
11282 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11283 break;
11284 }
11285 case llvm::omp::OMPC_num_teams:
11286 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11287 break;
11288 case llvm::omp::OMPC_thread_limit:
11289 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11290 break;
11291 case llvm::omp::OMPC_priority:
11292 C = new (Context) OMPPriorityClause();
11293 break;
11294 case llvm::omp::OMPC_grainsize:
11295 C = new (Context) OMPGrainsizeClause();
11296 break;
11297 case llvm::omp::OMPC_num_tasks:
11298 C = new (Context) OMPNumTasksClause();
11299 break;
11300 case llvm::omp::OMPC_hint:
11301 C = new (Context) OMPHintClause();
11302 break;
11303 case llvm::omp::OMPC_dist_schedule:
11304 C = new (Context) OMPDistScheduleClause();
11305 break;
11306 case llvm::omp::OMPC_defaultmap:
11307 C = new (Context) OMPDefaultmapClause();
11308 break;
11309 case llvm::omp::OMPC_to: {
11310 OMPMappableExprListSizeTy Sizes;
11311 Sizes.NumVars = Record.readInt();
11312 Sizes.NumUniqueDeclarations = Record.readInt();
11313 Sizes.NumComponentLists = Record.readInt();
11314 Sizes.NumComponents = Record.readInt();
11315 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11316 break;
11317 }
11318 case llvm::omp::OMPC_from: {
11319 OMPMappableExprListSizeTy Sizes;
11320 Sizes.NumVars = Record.readInt();
11321 Sizes.NumUniqueDeclarations = Record.readInt();
11322 Sizes.NumComponentLists = Record.readInt();
11323 Sizes.NumComponents = Record.readInt();
11324 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11325 break;
11326 }
11327 case llvm::omp::OMPC_use_device_ptr: {
11328 OMPMappableExprListSizeTy Sizes;
11329 Sizes.NumVars = Record.readInt();
11330 Sizes.NumUniqueDeclarations = Record.readInt();
11331 Sizes.NumComponentLists = Record.readInt();
11332 Sizes.NumComponents = Record.readInt();
11333 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11334 break;
11335 }
11336 case llvm::omp::OMPC_use_device_addr: {
11337 OMPMappableExprListSizeTy Sizes;
11338 Sizes.NumVars = Record.readInt();
11339 Sizes.NumUniqueDeclarations = Record.readInt();
11340 Sizes.NumComponentLists = Record.readInt();
11341 Sizes.NumComponents = Record.readInt();
11342 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11343 break;
11344 }
11345 case llvm::omp::OMPC_is_device_ptr: {
11346 OMPMappableExprListSizeTy Sizes;
11347 Sizes.NumVars = Record.readInt();
11348 Sizes.NumUniqueDeclarations = Record.readInt();
11349 Sizes.NumComponentLists = Record.readInt();
11350 Sizes.NumComponents = Record.readInt();
11351 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11352 break;
11353 }
11354 case llvm::omp::OMPC_has_device_addr: {
11355 OMPMappableExprListSizeTy Sizes;
11356 Sizes.NumVars = Record.readInt();
11357 Sizes.NumUniqueDeclarations = Record.readInt();
11358 Sizes.NumComponentLists = Record.readInt();
11359 Sizes.NumComponents = Record.readInt();
11360 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11361 break;
11362 }
11363 case llvm::omp::OMPC_allocate:
11364 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11365 break;
11366 case llvm::omp::OMPC_nontemporal:
11367 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11368 break;
11369 case llvm::omp::OMPC_inclusive:
11370 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11371 break;
11372 case llvm::omp::OMPC_exclusive:
11373 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11374 break;
11375 case llvm::omp::OMPC_order:
11376 C = new (Context) OMPOrderClause();
11377 break;
11378 case llvm::omp::OMPC_init:
11379 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
11380 break;
11381 case llvm::omp::OMPC_use:
11382 C = new (Context) OMPUseClause();
11383 break;
11384 case llvm::omp::OMPC_destroy:
11385 C = new (Context) OMPDestroyClause();
11386 break;
11387 case llvm::omp::OMPC_novariants:
11388 C = new (Context) OMPNovariantsClause();
11389 break;
11390 case llvm::omp::OMPC_nocontext:
11391 C = new (Context) OMPNocontextClause();
11392 break;
11393 case llvm::omp::OMPC_detach:
11394 C = new (Context) OMPDetachClause();
11395 break;
11396 case llvm::omp::OMPC_uses_allocators:
11397 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11398 break;
11399 case llvm::omp::OMPC_affinity:
11400 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11401 break;
11402 case llvm::omp::OMPC_filter:
11403 C = new (Context) OMPFilterClause();
11404 break;
11405 case llvm::omp::OMPC_bind:
11406 C = OMPBindClause::CreateEmpty(C: Context);
11407 break;
11408 case llvm::omp::OMPC_align:
11409 C = new (Context) OMPAlignClause();
11410 break;
11411 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11412 C = new (Context) OMPXDynCGroupMemClause();
11413 break;
11414 case llvm::omp::OMPC_doacross: {
11415 unsigned NumVars = Record.readInt();
11416 unsigned NumLoops = Record.readInt();
11417 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11418 break;
11419 }
11420 case llvm::omp::OMPC_ompx_attribute:
11421 C = new (Context) OMPXAttributeClause();
11422 break;
11423 case llvm::omp::OMPC_ompx_bare:
11424 C = new (Context) OMPXBareClause();
11425 break;
11426#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11427 case llvm::omp::Enum: \
11428 break;
11429#include "llvm/Frontend/OpenMP/OMPKinds.def"
11430 default:
11431 break;
11432 }
11433 assert(C && "Unknown OMPClause type");
11434
11435 Visit(S: C);
11436 C->setLocStart(Record.readSourceLocation());
11437 C->setLocEnd(Record.readSourceLocation());
11438
11439 return C;
11440}
11441
11442void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11443 C->setPreInitStmt(S: Record.readSubStmt(),
11444 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11445}
11446
11447void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11448 VisitOMPClauseWithPreInit(C);
11449 C->setPostUpdateExpr(Record.readSubExpr());
11450}
11451
11452void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11453 VisitOMPClauseWithPreInit(C);
11454 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11455 C->setNameModifierLoc(Record.readSourceLocation());
11456 C->setColonLoc(Record.readSourceLocation());
11457 C->setCondition(Record.readSubExpr());
11458 C->setLParenLoc(Record.readSourceLocation());
11459}
11460
11461void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11462 VisitOMPClauseWithPreInit(C);
11463 C->setCondition(Record.readSubExpr());
11464 C->setLParenLoc(Record.readSourceLocation());
11465}
11466
11467void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11468 VisitOMPClauseWithPreInit(C);
11469 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11470 C->setNumThreads(Record.readSubExpr());
11471 C->setModifierLoc(Record.readSourceLocation());
11472 C->setLParenLoc(Record.readSourceLocation());
11473}
11474
11475void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11476 C->setSafelen(Record.readSubExpr());
11477 C->setLParenLoc(Record.readSourceLocation());
11478}
11479
11480void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11481 C->setSimdlen(Record.readSubExpr());
11482 C->setLParenLoc(Record.readSourceLocation());
11483}
11484
11485void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11486 for (Expr *&E : C->getSizesRefs())
11487 E = Record.readSubExpr();
11488 C->setLParenLoc(Record.readSourceLocation());
11489}
11490
11491void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11492 for (Expr *&E : C->getArgsRefs())
11493 E = Record.readSubExpr();
11494 C->setLParenLoc(Record.readSourceLocation());
11495}
11496
11497void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11498
11499void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11500 C->setFactor(Record.readSubExpr());
11501 C->setLParenLoc(Record.readSourceLocation());
11502}
11503
11504void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11505 C->setAllocator(Record.readExpr());
11506 C->setLParenLoc(Record.readSourceLocation());
11507}
11508
11509void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11510 C->setNumForLoops(Record.readSubExpr());
11511 C->setLParenLoc(Record.readSourceLocation());
11512}
11513
11514void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11515 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11516 C->setLParenLoc(Record.readSourceLocation());
11517 C->setDefaultKindKwLoc(Record.readSourceLocation());
11518}
11519
11520void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11521 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11522 C->setLParenLoc(Record.readSourceLocation());
11523 C->setProcBindKindKwLoc(Record.readSourceLocation());
11524}
11525
11526void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11527 VisitOMPClauseWithPreInit(C);
11528 C->setScheduleKind(
11529 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11530 C->setFirstScheduleModifier(
11531 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11532 C->setSecondScheduleModifier(
11533 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11534 C->setChunkSize(Record.readSubExpr());
11535 C->setLParenLoc(Record.readSourceLocation());
11536 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11537 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11538 C->setScheduleKindLoc(Record.readSourceLocation());
11539 C->setCommaLoc(Record.readSourceLocation());
11540}
11541
11542void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11543 C->setNumForLoops(Record.readSubExpr());
11544 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11545 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11546 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11547 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11548 C->setLParenLoc(Record.readSourceLocation());
11549}
11550
11551void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11552 C->setEventHandler(Record.readSubExpr());
11553 C->setLParenLoc(Record.readSourceLocation());
11554}
11555
11556void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11557
11558void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11559
11560void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11561
11562void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11563
11564void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11565
11566void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11567 if (C->isExtended()) {
11568 C->setLParenLoc(Record.readSourceLocation());
11569 C->setArgumentLoc(Record.readSourceLocation());
11570 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11571 }
11572}
11573
11574void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11575
11576void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11577
11578// Read the parameter of fail clause. This will have been saved when
11579// OMPClauseWriter is called.
11580void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
11581 C->setLParenLoc(Record.readSourceLocation());
11582 SourceLocation FailParameterLoc = Record.readSourceLocation();
11583 C->setFailParameterLoc(FailParameterLoc);
11584 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
11585 C->setFailParameter(CKind);
11586}
11587
11588void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
11589 unsigned Count = C->getDirectiveKinds().size();
11590 C->setLParenLoc(Record.readSourceLocation());
11591 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11592 DKVec.reserve(N: Count);
11593 for (unsigned I = 0; I < Count; I++) {
11594 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11595 }
11596 C->setDirectiveKinds(DKVec);
11597}
11598
11599void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
11600 C->setExpr(Record.readExpr());
11601 C->setLParenLoc(Record.readSourceLocation());
11602}
11603
11604void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
11605 unsigned Count = C->getDirectiveKinds().size();
11606 C->setLParenLoc(Record.readSourceLocation());
11607 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11608 DKVec.reserve(N: Count);
11609 for (unsigned I = 0; I < Count; I++) {
11610 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11611 }
11612 C->setDirectiveKinds(DKVec);
11613}
11614
11615void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
11616
11617void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
11618 OMPNoOpenMPRoutinesClause *) {}
11619
11620void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
11621 OMPNoOpenMPConstructsClause *) {}
11622
11623void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
11624
11625void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11626
11627void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11628
11629void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11630
11631void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11632
11633void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11634
11635void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
11636
11637void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11638
11639void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11640
11641void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11642
11643void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
11644 unsigned NumVars = C->varlist_size();
11645 SmallVector<Expr *, 16> Vars;
11646 Vars.reserve(N: NumVars);
11647 for (unsigned I = 0; I != NumVars; ++I)
11648 Vars.push_back(Elt: Record.readSubExpr());
11649 C->setVarRefs(Vars);
11650 C->setIsTarget(Record.readBool());
11651 C->setIsTargetSync(Record.readBool());
11652 C->setLParenLoc(Record.readSourceLocation());
11653 C->setVarLoc(Record.readSourceLocation());
11654}
11655
11656void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
11657 C->setInteropVar(Record.readSubExpr());
11658 C->setLParenLoc(Record.readSourceLocation());
11659 C->setVarLoc(Record.readSourceLocation());
11660}
11661
11662void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
11663 C->setInteropVar(Record.readSubExpr());
11664 C->setLParenLoc(Record.readSourceLocation());
11665 C->setVarLoc(Record.readSourceLocation());
11666}
11667
11668void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
11669 VisitOMPClauseWithPreInit(C);
11670 C->setCondition(Record.readSubExpr());
11671 C->setLParenLoc(Record.readSourceLocation());
11672}
11673
11674void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
11675 VisitOMPClauseWithPreInit(C);
11676 C->setCondition(Record.readSubExpr());
11677 C->setLParenLoc(Record.readSourceLocation());
11678}
11679
11680void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11681
11682void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11683 OMPUnifiedSharedMemoryClause *) {}
11684
11685void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11686
11687void
11688OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11689}
11690
11691void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11692 OMPAtomicDefaultMemOrderClause *C) {
11693 C->setAtomicDefaultMemOrderKind(
11694 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11695 C->setLParenLoc(Record.readSourceLocation());
11696 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11697}
11698
11699void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
11700
11701void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
11702 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
11703 C->setLParenLoc(Record.readSourceLocation());
11704 C->setAtKindKwLoc(Record.readSourceLocation());
11705}
11706
11707void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
11708 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
11709 C->setLParenLoc(Record.readSourceLocation());
11710 C->setSeverityKindKwLoc(Record.readSourceLocation());
11711}
11712
11713void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
11714 C->setMessageString(Record.readSubExpr());
11715 C->setLParenLoc(Record.readSourceLocation());
11716}
11717
11718void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11719 C->setLParenLoc(Record.readSourceLocation());
11720 unsigned NumVars = C->varlist_size();
11721 SmallVector<Expr *, 16> Vars;
11722 Vars.reserve(N: NumVars);
11723 for (unsigned i = 0; i != NumVars; ++i)
11724 Vars.push_back(Elt: Record.readSubExpr());
11725 C->setVarRefs(Vars);
11726 Vars.clear();
11727 for (unsigned i = 0; i != NumVars; ++i)
11728 Vars.push_back(Elt: Record.readSubExpr());
11729 C->setPrivateCopies(Vars);
11730}
11731
11732void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
11733 VisitOMPClauseWithPreInit(C);
11734 C->setLParenLoc(Record.readSourceLocation());
11735 unsigned NumVars = C->varlist_size();
11736 SmallVector<Expr *, 16> Vars;
11737 Vars.reserve(N: NumVars);
11738 for (unsigned i = 0; i != NumVars; ++i)
11739 Vars.push_back(Elt: Record.readSubExpr());
11740 C->setVarRefs(Vars);
11741 Vars.clear();
11742 for (unsigned i = 0; i != NumVars; ++i)
11743 Vars.push_back(Elt: Record.readSubExpr());
11744 C->setPrivateCopies(Vars);
11745 Vars.clear();
11746 for (unsigned i = 0; i != NumVars; ++i)
11747 Vars.push_back(Elt: Record.readSubExpr());
11748 C->setInits(Vars);
11749}
11750
11751void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
11752 VisitOMPClauseWithPostUpdate(C);
11753 C->setLParenLoc(Record.readSourceLocation());
11754 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
11755 C->setKindLoc(Record.readSourceLocation());
11756 C->setColonLoc(Record.readSourceLocation());
11757 unsigned NumVars = C->varlist_size();
11758 SmallVector<Expr *, 16> Vars;
11759 Vars.reserve(N: NumVars);
11760 for (unsigned i = 0; i != NumVars; ++i)
11761 Vars.push_back(Elt: Record.readSubExpr());
11762 C->setVarRefs(Vars);
11763 Vars.clear();
11764 for (unsigned i = 0; i != NumVars; ++i)
11765 Vars.push_back(Elt: Record.readSubExpr());
11766 C->setPrivateCopies(Vars);
11767 Vars.clear();
11768 for (unsigned i = 0; i != NumVars; ++i)
11769 Vars.push_back(Elt: Record.readSubExpr());
11770 C->setSourceExprs(Vars);
11771 Vars.clear();
11772 for (unsigned i = 0; i != NumVars; ++i)
11773 Vars.push_back(Elt: Record.readSubExpr());
11774 C->setDestinationExprs(Vars);
11775 Vars.clear();
11776 for (unsigned i = 0; i != NumVars; ++i)
11777 Vars.push_back(Elt: Record.readSubExpr());
11778 C->setAssignmentOps(Vars);
11779}
11780
11781void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
11782 C->setLParenLoc(Record.readSourceLocation());
11783 unsigned NumVars = C->varlist_size();
11784 SmallVector<Expr *, 16> Vars;
11785 Vars.reserve(N: NumVars);
11786 for (unsigned i = 0; i != NumVars; ++i)
11787 Vars.push_back(Elt: Record.readSubExpr());
11788 C->setVarRefs(Vars);
11789}
11790
11791void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
11792 VisitOMPClauseWithPostUpdate(C);
11793 C->setLParenLoc(Record.readSourceLocation());
11794 C->setModifierLoc(Record.readSourceLocation());
11795 C->setColonLoc(Record.readSourceLocation());
11796 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11797 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11798 C->setQualifierLoc(NNSL);
11799 C->setNameInfo(DNI);
11800
11801 unsigned NumVars = C->varlist_size();
11802 SmallVector<Expr *, 16> Vars;
11803 Vars.reserve(N: NumVars);
11804 for (unsigned i = 0; i != NumVars; ++i)
11805 Vars.push_back(Elt: Record.readSubExpr());
11806 C->setVarRefs(Vars);
11807 Vars.clear();
11808 for (unsigned i = 0; i != NumVars; ++i)
11809 Vars.push_back(Elt: Record.readSubExpr());
11810 C->setPrivates(Vars);
11811 Vars.clear();
11812 for (unsigned i = 0; i != NumVars; ++i)
11813 Vars.push_back(Elt: Record.readSubExpr());
11814 C->setLHSExprs(Vars);
11815 Vars.clear();
11816 for (unsigned i = 0; i != NumVars; ++i)
11817 Vars.push_back(Elt: Record.readSubExpr());
11818 C->setRHSExprs(Vars);
11819 Vars.clear();
11820 for (unsigned i = 0; i != NumVars; ++i)
11821 Vars.push_back(Elt: Record.readSubExpr());
11822 C->setReductionOps(Vars);
11823 if (C->getModifier() == OMPC_REDUCTION_inscan) {
11824 Vars.clear();
11825 for (unsigned i = 0; i != NumVars; ++i)
11826 Vars.push_back(Elt: Record.readSubExpr());
11827 C->setInscanCopyOps(Vars);
11828 Vars.clear();
11829 for (unsigned i = 0; i != NumVars; ++i)
11830 Vars.push_back(Elt: Record.readSubExpr());
11831 C->setInscanCopyArrayTemps(Vars);
11832 Vars.clear();
11833 for (unsigned i = 0; i != NumVars; ++i)
11834 Vars.push_back(Elt: Record.readSubExpr());
11835 C->setInscanCopyArrayElems(Vars);
11836 }
11837 unsigned NumFlags = Record.readInt();
11838 SmallVector<bool, 16> Flags;
11839 Flags.reserve(N: NumFlags);
11840 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
11841 Flags.push_back(Elt: Record.readInt());
11842 C->setPrivateVariableReductionFlags(Flags);
11843}
11844
11845void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
11846 VisitOMPClauseWithPostUpdate(C);
11847 C->setLParenLoc(Record.readSourceLocation());
11848 C->setColonLoc(Record.readSourceLocation());
11849 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11850 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11851 C->setQualifierLoc(NNSL);
11852 C->setNameInfo(DNI);
11853
11854 unsigned NumVars = C->varlist_size();
11855 SmallVector<Expr *, 16> Vars;
11856 Vars.reserve(N: NumVars);
11857 for (unsigned I = 0; I != NumVars; ++I)
11858 Vars.push_back(Elt: Record.readSubExpr());
11859 C->setVarRefs(Vars);
11860 Vars.clear();
11861 for (unsigned I = 0; I != NumVars; ++I)
11862 Vars.push_back(Elt: Record.readSubExpr());
11863 C->setPrivates(Vars);
11864 Vars.clear();
11865 for (unsigned I = 0; I != NumVars; ++I)
11866 Vars.push_back(Elt: Record.readSubExpr());
11867 C->setLHSExprs(Vars);
11868 Vars.clear();
11869 for (unsigned I = 0; I != NumVars; ++I)
11870 Vars.push_back(Elt: Record.readSubExpr());
11871 C->setRHSExprs(Vars);
11872 Vars.clear();
11873 for (unsigned I = 0; I != NumVars; ++I)
11874 Vars.push_back(Elt: Record.readSubExpr());
11875 C->setReductionOps(Vars);
11876}
11877
11878void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
11879 VisitOMPClauseWithPostUpdate(C);
11880 C->setLParenLoc(Record.readSourceLocation());
11881 C->setColonLoc(Record.readSourceLocation());
11882 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11883 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11884 C->setQualifierLoc(NNSL);
11885 C->setNameInfo(DNI);
11886
11887 unsigned NumVars = C->varlist_size();
11888 SmallVector<Expr *, 16> Vars;
11889 Vars.reserve(N: NumVars);
11890 for (unsigned I = 0; I != NumVars; ++I)
11891 Vars.push_back(Elt: Record.readSubExpr());
11892 C->setVarRefs(Vars);
11893 Vars.clear();
11894 for (unsigned I = 0; I != NumVars; ++I)
11895 Vars.push_back(Elt: Record.readSubExpr());
11896 C->setPrivates(Vars);
11897 Vars.clear();
11898 for (unsigned I = 0; I != NumVars; ++I)
11899 Vars.push_back(Elt: Record.readSubExpr());
11900 C->setLHSExprs(Vars);
11901 Vars.clear();
11902 for (unsigned I = 0; I != NumVars; ++I)
11903 Vars.push_back(Elt: Record.readSubExpr());
11904 C->setRHSExprs(Vars);
11905 Vars.clear();
11906 for (unsigned I = 0; I != NumVars; ++I)
11907 Vars.push_back(Elt: Record.readSubExpr());
11908 C->setReductionOps(Vars);
11909 Vars.clear();
11910 for (unsigned I = 0; I != NumVars; ++I)
11911 Vars.push_back(Elt: Record.readSubExpr());
11912 C->setTaskgroupDescriptors(Vars);
11913}
11914
11915void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
11916 VisitOMPClauseWithPostUpdate(C);
11917 C->setLParenLoc(Record.readSourceLocation());
11918 C->setColonLoc(Record.readSourceLocation());
11919 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
11920 C->setModifierLoc(Record.readSourceLocation());
11921 unsigned NumVars = C->varlist_size();
11922 SmallVector<Expr *, 16> Vars;
11923 Vars.reserve(N: NumVars);
11924 for (unsigned i = 0; i != NumVars; ++i)
11925 Vars.push_back(Elt: Record.readSubExpr());
11926 C->setVarRefs(Vars);
11927 Vars.clear();
11928 for (unsigned i = 0; i != NumVars; ++i)
11929 Vars.push_back(Elt: Record.readSubExpr());
11930 C->setPrivates(Vars);
11931 Vars.clear();
11932 for (unsigned i = 0; i != NumVars; ++i)
11933 Vars.push_back(Elt: Record.readSubExpr());
11934 C->setInits(Vars);
11935 Vars.clear();
11936 for (unsigned i = 0; i != NumVars; ++i)
11937 Vars.push_back(Elt: Record.readSubExpr());
11938 C->setUpdates(Vars);
11939 Vars.clear();
11940 for (unsigned i = 0; i != NumVars; ++i)
11941 Vars.push_back(Elt: Record.readSubExpr());
11942 C->setFinals(Vars);
11943 C->setStep(Record.readSubExpr());
11944 C->setCalcStep(Record.readSubExpr());
11945 Vars.clear();
11946 for (unsigned I = 0; I != NumVars + 1; ++I)
11947 Vars.push_back(Elt: Record.readSubExpr());
11948 C->setUsedExprs(Vars);
11949}
11950
11951void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
11952 C->setLParenLoc(Record.readSourceLocation());
11953 C->setColonLoc(Record.readSourceLocation());
11954 unsigned NumVars = C->varlist_size();
11955 SmallVector<Expr *, 16> Vars;
11956 Vars.reserve(N: NumVars);
11957 for (unsigned i = 0; i != NumVars; ++i)
11958 Vars.push_back(Elt: Record.readSubExpr());
11959 C->setVarRefs(Vars);
11960 C->setAlignment(Record.readSubExpr());
11961}
11962
11963void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
11964 C->setLParenLoc(Record.readSourceLocation());
11965 unsigned NumVars = C->varlist_size();
11966 SmallVector<Expr *, 16> Exprs;
11967 Exprs.reserve(N: NumVars);
11968 for (unsigned i = 0; i != NumVars; ++i)
11969 Exprs.push_back(Elt: Record.readSubExpr());
11970 C->setVarRefs(Exprs);
11971 Exprs.clear();
11972 for (unsigned i = 0; i != NumVars; ++i)
11973 Exprs.push_back(Elt: Record.readSubExpr());
11974 C->setSourceExprs(Exprs);
11975 Exprs.clear();
11976 for (unsigned i = 0; i != NumVars; ++i)
11977 Exprs.push_back(Elt: Record.readSubExpr());
11978 C->setDestinationExprs(Exprs);
11979 Exprs.clear();
11980 for (unsigned i = 0; i != NumVars; ++i)
11981 Exprs.push_back(Elt: Record.readSubExpr());
11982 C->setAssignmentOps(Exprs);
11983}
11984
11985void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11986 C->setLParenLoc(Record.readSourceLocation());
11987 unsigned NumVars = C->varlist_size();
11988 SmallVector<Expr *, 16> Exprs;
11989 Exprs.reserve(N: NumVars);
11990 for (unsigned i = 0; i != NumVars; ++i)
11991 Exprs.push_back(Elt: Record.readSubExpr());
11992 C->setVarRefs(Exprs);
11993 Exprs.clear();
11994 for (unsigned i = 0; i != NumVars; ++i)
11995 Exprs.push_back(Elt: Record.readSubExpr());
11996 C->setSourceExprs(Exprs);
11997 Exprs.clear();
11998 for (unsigned i = 0; i != NumVars; ++i)
11999 Exprs.push_back(Elt: Record.readSubExpr());
12000 C->setDestinationExprs(Exprs);
12001 Exprs.clear();
12002 for (unsigned i = 0; i != NumVars; ++i)
12003 Exprs.push_back(Elt: Record.readSubExpr());
12004 C->setAssignmentOps(Exprs);
12005}
12006
12007void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12008 C->setLParenLoc(Record.readSourceLocation());
12009 unsigned NumVars = C->varlist_size();
12010 SmallVector<Expr *, 16> Vars;
12011 Vars.reserve(N: NumVars);
12012 for (unsigned i = 0; i != NumVars; ++i)
12013 Vars.push_back(Elt: Record.readSubExpr());
12014 C->setVarRefs(Vars);
12015}
12016
12017void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12018 C->setDepobj(Record.readSubExpr());
12019 C->setLParenLoc(Record.readSourceLocation());
12020}
12021
12022void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12023 C->setLParenLoc(Record.readSourceLocation());
12024 C->setModifier(Record.readSubExpr());
12025 C->setDependencyKind(
12026 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12027 C->setDependencyLoc(Record.readSourceLocation());
12028 C->setColonLoc(Record.readSourceLocation());
12029 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12030 unsigned NumVars = C->varlist_size();
12031 SmallVector<Expr *, 16> Vars;
12032 Vars.reserve(N: NumVars);
12033 for (unsigned I = 0; I != NumVars; ++I)
12034 Vars.push_back(Elt: Record.readSubExpr());
12035 C->setVarRefs(Vars);
12036 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12037 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12038}
12039
12040void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12041 VisitOMPClauseWithPreInit(C);
12042 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12043 C->setDevice(Record.readSubExpr());
12044 C->setModifierLoc(Record.readSourceLocation());
12045 C->setLParenLoc(Record.readSourceLocation());
12046}
12047
12048void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12049 C->setLParenLoc(Record.readSourceLocation());
12050 bool HasIteratorModifier = false;
12051 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12052 C->setMapTypeModifier(
12053 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12054 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12055 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12056 HasIteratorModifier = true;
12057 }
12058 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12059 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12060 C->setMapType(
12061 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12062 C->setMapLoc(Record.readSourceLocation());
12063 C->setColonLoc(Record.readSourceLocation());
12064 auto NumVars = C->varlist_size();
12065 auto UniqueDecls = C->getUniqueDeclarationsNum();
12066 auto TotalLists = C->getTotalComponentListNum();
12067 auto TotalComponents = C->getTotalComponentsNum();
12068
12069 SmallVector<Expr *, 16> Vars;
12070 Vars.reserve(N: NumVars);
12071 for (unsigned i = 0; i != NumVars; ++i)
12072 Vars.push_back(Elt: Record.readExpr());
12073 C->setVarRefs(Vars);
12074
12075 SmallVector<Expr *, 16> UDMappers;
12076 UDMappers.reserve(N: NumVars);
12077 for (unsigned I = 0; I < NumVars; ++I)
12078 UDMappers.push_back(Elt: Record.readExpr());
12079 C->setUDMapperRefs(UDMappers);
12080
12081 if (HasIteratorModifier)
12082 C->setIteratorModifier(Record.readExpr());
12083
12084 SmallVector<ValueDecl *, 16> Decls;
12085 Decls.reserve(N: UniqueDecls);
12086 for (unsigned i = 0; i < UniqueDecls; ++i)
12087 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12088 C->setUniqueDecls(Decls);
12089
12090 SmallVector<unsigned, 16> ListsPerDecl;
12091 ListsPerDecl.reserve(N: UniqueDecls);
12092 for (unsigned i = 0; i < UniqueDecls; ++i)
12093 ListsPerDecl.push_back(Elt: Record.readInt());
12094 C->setDeclNumLists(ListsPerDecl);
12095
12096 SmallVector<unsigned, 32> ListSizes;
12097 ListSizes.reserve(N: TotalLists);
12098 for (unsigned i = 0; i < TotalLists; ++i)
12099 ListSizes.push_back(Elt: Record.readInt());
12100 C->setComponentListSizes(ListSizes);
12101
12102 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12103 Components.reserve(N: TotalComponents);
12104 for (unsigned i = 0; i < TotalComponents; ++i) {
12105 Expr *AssociatedExprPr = Record.readExpr();
12106 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12107 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12108 /*IsNonContiguous=*/Args: false);
12109 }
12110 C->setComponents(Components, CLSs: ListSizes);
12111}
12112
12113void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12114 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12115 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12116 C->setLParenLoc(Record.readSourceLocation());
12117 C->setColonLoc(Record.readSourceLocation());
12118 C->setAllocator(Record.readSubExpr());
12119 C->setAlignment(Record.readSubExpr());
12120 unsigned NumVars = C->varlist_size();
12121 SmallVector<Expr *, 16> Vars;
12122 Vars.reserve(N: NumVars);
12123 for (unsigned i = 0; i != NumVars; ++i)
12124 Vars.push_back(Elt: Record.readSubExpr());
12125 C->setVarRefs(Vars);
12126}
12127
12128void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12129 VisitOMPClauseWithPreInit(C);
12130 C->setLParenLoc(Record.readSourceLocation());
12131 unsigned NumVars = C->varlist_size();
12132 SmallVector<Expr *, 16> Vars;
12133 Vars.reserve(N: NumVars);
12134 for (unsigned I = 0; I != NumVars; ++I)
12135 Vars.push_back(Elt: Record.readSubExpr());
12136 C->setVarRefs(Vars);
12137}
12138
12139void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12140 VisitOMPClauseWithPreInit(C);
12141 C->setLParenLoc(Record.readSourceLocation());
12142 unsigned NumVars = C->varlist_size();
12143 SmallVector<Expr *, 16> Vars;
12144 Vars.reserve(N: NumVars);
12145 for (unsigned I = 0; I != NumVars; ++I)
12146 Vars.push_back(Elt: Record.readSubExpr());
12147 C->setVarRefs(Vars);
12148}
12149
12150void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12151 VisitOMPClauseWithPreInit(C);
12152 C->setPriority(Record.readSubExpr());
12153 C->setLParenLoc(Record.readSourceLocation());
12154}
12155
12156void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12157 VisitOMPClauseWithPreInit(C);
12158 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12159 C->setGrainsize(Record.readSubExpr());
12160 C->setModifierLoc(Record.readSourceLocation());
12161 C->setLParenLoc(Record.readSourceLocation());
12162}
12163
12164void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12165 VisitOMPClauseWithPreInit(C);
12166 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12167 C->setNumTasks(Record.readSubExpr());
12168 C->setModifierLoc(Record.readSourceLocation());
12169 C->setLParenLoc(Record.readSourceLocation());
12170}
12171
12172void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12173 C->setHint(Record.readSubExpr());
12174 C->setLParenLoc(Record.readSourceLocation());
12175}
12176
12177void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12178 VisitOMPClauseWithPreInit(C);
12179 C->setDistScheduleKind(
12180 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12181 C->setChunkSize(Record.readSubExpr());
12182 C->setLParenLoc(Record.readSourceLocation());
12183 C->setDistScheduleKindLoc(Record.readSourceLocation());
12184 C->setCommaLoc(Record.readSourceLocation());
12185}
12186
12187void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12188 C->setDefaultmapKind(
12189 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12190 C->setDefaultmapModifier(
12191 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12192 C->setLParenLoc(Record.readSourceLocation());
12193 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12194 C->setDefaultmapKindLoc(Record.readSourceLocation());
12195}
12196
12197void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12198 C->setLParenLoc(Record.readSourceLocation());
12199 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12200 C->setMotionModifier(
12201 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12202 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12203 }
12204 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12205 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12206 C->setColonLoc(Record.readSourceLocation());
12207 auto NumVars = C->varlist_size();
12208 auto UniqueDecls = C->getUniqueDeclarationsNum();
12209 auto TotalLists = C->getTotalComponentListNum();
12210 auto TotalComponents = C->getTotalComponentsNum();
12211
12212 SmallVector<Expr *, 16> Vars;
12213 Vars.reserve(N: NumVars);
12214 for (unsigned i = 0; i != NumVars; ++i)
12215 Vars.push_back(Elt: Record.readSubExpr());
12216 C->setVarRefs(Vars);
12217
12218 SmallVector<Expr *, 16> UDMappers;
12219 UDMappers.reserve(N: NumVars);
12220 for (unsigned I = 0; I < NumVars; ++I)
12221 UDMappers.push_back(Elt: Record.readSubExpr());
12222 C->setUDMapperRefs(UDMappers);
12223
12224 SmallVector<ValueDecl *, 16> Decls;
12225 Decls.reserve(N: UniqueDecls);
12226 for (unsigned i = 0; i < UniqueDecls; ++i)
12227 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12228 C->setUniqueDecls(Decls);
12229
12230 SmallVector<unsigned, 16> ListsPerDecl;
12231 ListsPerDecl.reserve(N: UniqueDecls);
12232 for (unsigned i = 0; i < UniqueDecls; ++i)
12233 ListsPerDecl.push_back(Elt: Record.readInt());
12234 C->setDeclNumLists(ListsPerDecl);
12235
12236 SmallVector<unsigned, 32> ListSizes;
12237 ListSizes.reserve(N: TotalLists);
12238 for (unsigned i = 0; i < TotalLists; ++i)
12239 ListSizes.push_back(Elt: Record.readInt());
12240 C->setComponentListSizes(ListSizes);
12241
12242 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12243 Components.reserve(N: TotalComponents);
12244 for (unsigned i = 0; i < TotalComponents; ++i) {
12245 Expr *AssociatedExprPr = Record.readSubExpr();
12246 bool IsNonContiguous = Record.readBool();
12247 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12248 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12249 }
12250 C->setComponents(Components, CLSs: ListSizes);
12251}
12252
12253void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12254 C->setLParenLoc(Record.readSourceLocation());
12255 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12256 C->setMotionModifier(
12257 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12258 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12259 }
12260 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12261 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12262 C->setColonLoc(Record.readSourceLocation());
12263 auto NumVars = C->varlist_size();
12264 auto UniqueDecls = C->getUniqueDeclarationsNum();
12265 auto TotalLists = C->getTotalComponentListNum();
12266 auto TotalComponents = C->getTotalComponentsNum();
12267
12268 SmallVector<Expr *, 16> Vars;
12269 Vars.reserve(N: NumVars);
12270 for (unsigned i = 0; i != NumVars; ++i)
12271 Vars.push_back(Elt: Record.readSubExpr());
12272 C->setVarRefs(Vars);
12273
12274 SmallVector<Expr *, 16> UDMappers;
12275 UDMappers.reserve(N: NumVars);
12276 for (unsigned I = 0; I < NumVars; ++I)
12277 UDMappers.push_back(Elt: Record.readSubExpr());
12278 C->setUDMapperRefs(UDMappers);
12279
12280 SmallVector<ValueDecl *, 16> Decls;
12281 Decls.reserve(N: UniqueDecls);
12282 for (unsigned i = 0; i < UniqueDecls; ++i)
12283 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12284 C->setUniqueDecls(Decls);
12285
12286 SmallVector<unsigned, 16> ListsPerDecl;
12287 ListsPerDecl.reserve(N: UniqueDecls);
12288 for (unsigned i = 0; i < UniqueDecls; ++i)
12289 ListsPerDecl.push_back(Elt: Record.readInt());
12290 C->setDeclNumLists(ListsPerDecl);
12291
12292 SmallVector<unsigned, 32> ListSizes;
12293 ListSizes.reserve(N: TotalLists);
12294 for (unsigned i = 0; i < TotalLists; ++i)
12295 ListSizes.push_back(Elt: Record.readInt());
12296 C->setComponentListSizes(ListSizes);
12297
12298 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12299 Components.reserve(N: TotalComponents);
12300 for (unsigned i = 0; i < TotalComponents; ++i) {
12301 Expr *AssociatedExprPr = Record.readSubExpr();
12302 bool IsNonContiguous = Record.readBool();
12303 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12304 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12305 }
12306 C->setComponents(Components, CLSs: ListSizes);
12307}
12308
12309void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12310 C->setLParenLoc(Record.readSourceLocation());
12311 auto NumVars = C->varlist_size();
12312 auto UniqueDecls = C->getUniqueDeclarationsNum();
12313 auto TotalLists = C->getTotalComponentListNum();
12314 auto TotalComponents = C->getTotalComponentsNum();
12315
12316 SmallVector<Expr *, 16> Vars;
12317 Vars.reserve(N: NumVars);
12318 for (unsigned i = 0; i != NumVars; ++i)
12319 Vars.push_back(Elt: Record.readSubExpr());
12320 C->setVarRefs(Vars);
12321 Vars.clear();
12322 for (unsigned i = 0; i != NumVars; ++i)
12323 Vars.push_back(Elt: Record.readSubExpr());
12324 C->setPrivateCopies(Vars);
12325 Vars.clear();
12326 for (unsigned i = 0; i != NumVars; ++i)
12327 Vars.push_back(Elt: Record.readSubExpr());
12328 C->setInits(Vars);
12329
12330 SmallVector<ValueDecl *, 16> Decls;
12331 Decls.reserve(N: UniqueDecls);
12332 for (unsigned i = 0; i < UniqueDecls; ++i)
12333 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12334 C->setUniqueDecls(Decls);
12335
12336 SmallVector<unsigned, 16> ListsPerDecl;
12337 ListsPerDecl.reserve(N: UniqueDecls);
12338 for (unsigned i = 0; i < UniqueDecls; ++i)
12339 ListsPerDecl.push_back(Elt: Record.readInt());
12340 C->setDeclNumLists(ListsPerDecl);
12341
12342 SmallVector<unsigned, 32> ListSizes;
12343 ListSizes.reserve(N: TotalLists);
12344 for (unsigned i = 0; i < TotalLists; ++i)
12345 ListSizes.push_back(Elt: Record.readInt());
12346 C->setComponentListSizes(ListSizes);
12347
12348 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12349 Components.reserve(N: TotalComponents);
12350 for (unsigned i = 0; i < TotalComponents; ++i) {
12351 auto *AssociatedExprPr = Record.readSubExpr();
12352 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12353 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12354 /*IsNonContiguous=*/Args: false);
12355 }
12356 C->setComponents(Components, CLSs: ListSizes);
12357}
12358
12359void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12360 C->setLParenLoc(Record.readSourceLocation());
12361 auto NumVars = C->varlist_size();
12362 auto UniqueDecls = C->getUniqueDeclarationsNum();
12363 auto TotalLists = C->getTotalComponentListNum();
12364 auto TotalComponents = C->getTotalComponentsNum();
12365
12366 SmallVector<Expr *, 16> Vars;
12367 Vars.reserve(N: NumVars);
12368 for (unsigned i = 0; i != NumVars; ++i)
12369 Vars.push_back(Elt: Record.readSubExpr());
12370 C->setVarRefs(Vars);
12371
12372 SmallVector<ValueDecl *, 16> Decls;
12373 Decls.reserve(N: UniqueDecls);
12374 for (unsigned i = 0; i < UniqueDecls; ++i)
12375 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12376 C->setUniqueDecls(Decls);
12377
12378 SmallVector<unsigned, 16> ListsPerDecl;
12379 ListsPerDecl.reserve(N: UniqueDecls);
12380 for (unsigned i = 0; i < UniqueDecls; ++i)
12381 ListsPerDecl.push_back(Elt: Record.readInt());
12382 C->setDeclNumLists(ListsPerDecl);
12383
12384 SmallVector<unsigned, 32> ListSizes;
12385 ListSizes.reserve(N: TotalLists);
12386 for (unsigned i = 0; i < TotalLists; ++i)
12387 ListSizes.push_back(Elt: Record.readInt());
12388 C->setComponentListSizes(ListSizes);
12389
12390 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12391 Components.reserve(N: TotalComponents);
12392 for (unsigned i = 0; i < TotalComponents; ++i) {
12393 Expr *AssociatedExpr = Record.readSubExpr();
12394 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12395 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12396 /*IsNonContiguous*/ Args: false);
12397 }
12398 C->setComponents(Components, CLSs: ListSizes);
12399}
12400
12401void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12402 C->setLParenLoc(Record.readSourceLocation());
12403 auto NumVars = C->varlist_size();
12404 auto UniqueDecls = C->getUniqueDeclarationsNum();
12405 auto TotalLists = C->getTotalComponentListNum();
12406 auto TotalComponents = C->getTotalComponentsNum();
12407
12408 SmallVector<Expr *, 16> Vars;
12409 Vars.reserve(N: NumVars);
12410 for (unsigned i = 0; i != NumVars; ++i)
12411 Vars.push_back(Elt: Record.readSubExpr());
12412 C->setVarRefs(Vars);
12413 Vars.clear();
12414
12415 SmallVector<ValueDecl *, 16> Decls;
12416 Decls.reserve(N: UniqueDecls);
12417 for (unsigned i = 0; i < UniqueDecls; ++i)
12418 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12419 C->setUniqueDecls(Decls);
12420
12421 SmallVector<unsigned, 16> ListsPerDecl;
12422 ListsPerDecl.reserve(N: UniqueDecls);
12423 for (unsigned i = 0; i < UniqueDecls; ++i)
12424 ListsPerDecl.push_back(Elt: Record.readInt());
12425 C->setDeclNumLists(ListsPerDecl);
12426
12427 SmallVector<unsigned, 32> ListSizes;
12428 ListSizes.reserve(N: TotalLists);
12429 for (unsigned i = 0; i < TotalLists; ++i)
12430 ListSizes.push_back(Elt: Record.readInt());
12431 C->setComponentListSizes(ListSizes);
12432
12433 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12434 Components.reserve(N: TotalComponents);
12435 for (unsigned i = 0; i < TotalComponents; ++i) {
12436 Expr *AssociatedExpr = Record.readSubExpr();
12437 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12438 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12439 /*IsNonContiguous=*/Args: false);
12440 }
12441 C->setComponents(Components, CLSs: ListSizes);
12442}
12443
12444void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12445 C->setLParenLoc(Record.readSourceLocation());
12446 auto NumVars = C->varlist_size();
12447 auto UniqueDecls = C->getUniqueDeclarationsNum();
12448 auto TotalLists = C->getTotalComponentListNum();
12449 auto TotalComponents = C->getTotalComponentsNum();
12450
12451 SmallVector<Expr *, 16> Vars;
12452 Vars.reserve(N: NumVars);
12453 for (unsigned I = 0; I != NumVars; ++I)
12454 Vars.push_back(Elt: Record.readSubExpr());
12455 C->setVarRefs(Vars);
12456 Vars.clear();
12457
12458 SmallVector<ValueDecl *, 16> Decls;
12459 Decls.reserve(N: UniqueDecls);
12460 for (unsigned I = 0; I < UniqueDecls; ++I)
12461 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12462 C->setUniqueDecls(Decls);
12463
12464 SmallVector<unsigned, 16> ListsPerDecl;
12465 ListsPerDecl.reserve(N: UniqueDecls);
12466 for (unsigned I = 0; I < UniqueDecls; ++I)
12467 ListsPerDecl.push_back(Elt: Record.readInt());
12468 C->setDeclNumLists(ListsPerDecl);
12469
12470 SmallVector<unsigned, 32> ListSizes;
12471 ListSizes.reserve(N: TotalLists);
12472 for (unsigned i = 0; i < TotalLists; ++i)
12473 ListSizes.push_back(Elt: Record.readInt());
12474 C->setComponentListSizes(ListSizes);
12475
12476 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12477 Components.reserve(N: TotalComponents);
12478 for (unsigned I = 0; I < TotalComponents; ++I) {
12479 Expr *AssociatedExpr = Record.readSubExpr();
12480 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12481 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12482 /*IsNonContiguous=*/Args: false);
12483 }
12484 C->setComponents(Components, CLSs: ListSizes);
12485}
12486
12487void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12488 C->setLParenLoc(Record.readSourceLocation());
12489 unsigned NumVars = C->varlist_size();
12490 SmallVector<Expr *, 16> Vars;
12491 Vars.reserve(N: NumVars);
12492 for (unsigned i = 0; i != NumVars; ++i)
12493 Vars.push_back(Elt: Record.readSubExpr());
12494 C->setVarRefs(Vars);
12495 Vars.clear();
12496 Vars.reserve(N: NumVars);
12497 for (unsigned i = 0; i != NumVars; ++i)
12498 Vars.push_back(Elt: Record.readSubExpr());
12499 C->setPrivateRefs(Vars);
12500}
12501
12502void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12503 C->setLParenLoc(Record.readSourceLocation());
12504 unsigned NumVars = C->varlist_size();
12505 SmallVector<Expr *, 16> Vars;
12506 Vars.reserve(N: NumVars);
12507 for (unsigned i = 0; i != NumVars; ++i)
12508 Vars.push_back(Elt: Record.readSubExpr());
12509 C->setVarRefs(Vars);
12510}
12511
12512void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12513 C->setLParenLoc(Record.readSourceLocation());
12514 unsigned NumVars = C->varlist_size();
12515 SmallVector<Expr *, 16> Vars;
12516 Vars.reserve(N: NumVars);
12517 for (unsigned i = 0; i != NumVars; ++i)
12518 Vars.push_back(Elt: Record.readSubExpr());
12519 C->setVarRefs(Vars);
12520}
12521
12522void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12523 C->setLParenLoc(Record.readSourceLocation());
12524 unsigned NumOfAllocators = C->getNumberOfAllocators();
12525 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12526 Data.reserve(N: NumOfAllocators);
12527 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12528 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12529 D.Allocator = Record.readSubExpr();
12530 D.AllocatorTraits = Record.readSubExpr();
12531 D.LParenLoc = Record.readSourceLocation();
12532 D.RParenLoc = Record.readSourceLocation();
12533 }
12534 C->setAllocatorsData(Data);
12535}
12536
12537void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12538 C->setLParenLoc(Record.readSourceLocation());
12539 C->setModifier(Record.readSubExpr());
12540 C->setColonLoc(Record.readSourceLocation());
12541 unsigned NumOfLocators = C->varlist_size();
12542 SmallVector<Expr *, 4> Locators;
12543 Locators.reserve(N: NumOfLocators);
12544 for (unsigned I = 0; I != NumOfLocators; ++I)
12545 Locators.push_back(Elt: Record.readSubExpr());
12546 C->setVarRefs(Locators);
12547}
12548
12549void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12550 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12551 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
12552 C->setLParenLoc(Record.readSourceLocation());
12553 C->setKindKwLoc(Record.readSourceLocation());
12554 C->setModifierKwLoc(Record.readSourceLocation());
12555}
12556
12557void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12558 VisitOMPClauseWithPreInit(C);
12559 C->setThreadID(Record.readSubExpr());
12560 C->setLParenLoc(Record.readSourceLocation());
12561}
12562
12563void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12564 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12565 C->setLParenLoc(Record.readSourceLocation());
12566 C->setBindKindLoc(Record.readSourceLocation());
12567}
12568
12569void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12570 C->setAlignment(Record.readExpr());
12571 C->setLParenLoc(Record.readSourceLocation());
12572}
12573
12574void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
12575 VisitOMPClauseWithPreInit(C);
12576 C->setSize(Record.readSubExpr());
12577 C->setLParenLoc(Record.readSourceLocation());
12578}
12579
12580void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
12581 C->setLParenLoc(Record.readSourceLocation());
12582 C->setDependenceType(
12583 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
12584 C->setDependenceLoc(Record.readSourceLocation());
12585 C->setColonLoc(Record.readSourceLocation());
12586 unsigned NumVars = C->varlist_size();
12587 SmallVector<Expr *, 16> Vars;
12588 Vars.reserve(N: NumVars);
12589 for (unsigned I = 0; I != NumVars; ++I)
12590 Vars.push_back(Elt: Record.readSubExpr());
12591 C->setVarRefs(Vars);
12592 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12593 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12594}
12595
12596void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
12597 AttrVec Attrs;
12598 Record.readAttributes(Attrs);
12599 C->setAttrs(Attrs);
12600 C->setLocStart(Record.readSourceLocation());
12601 C->setLParenLoc(Record.readSourceLocation());
12602 C->setLocEnd(Record.readSourceLocation());
12603}
12604
12605void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
12606
12607OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12608 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12609 TI.Sets.resize(N: readUInt32());
12610 for (auto &Set : TI.Sets) {
12611 Set.Kind = readEnum<llvm::omp::TraitSet>();
12612 Set.Selectors.resize(N: readUInt32());
12613 for (auto &Selector : Set.Selectors) {
12614 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12615 Selector.ScoreOrCondition = nullptr;
12616 if (readBool())
12617 Selector.ScoreOrCondition = readExprRef();
12618 Selector.Properties.resize(N: readUInt32());
12619 for (auto &Property : Selector.Properties)
12620 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12621 }
12622 }
12623 return &TI;
12624}
12625
12626void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12627 if (!Data)
12628 return;
12629 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12630 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12631 skipInts(N: 3);
12632 }
12633 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12634 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12635 Clauses[I] = readOMPClause();
12636 Data->setClauses(Clauses);
12637 if (Data->hasAssociatedStmt())
12638 Data->setAssociatedStmt(readStmt());
12639 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12640 Data->getChildren()[I] = readStmt();
12641}
12642
12643SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
12644 unsigned NumVars = readInt();
12645 llvm::SmallVector<Expr *> VarList;
12646 for (unsigned I = 0; I < NumVars; ++I)
12647 VarList.push_back(Elt: readExpr());
12648 return VarList;
12649}
12650
12651SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
12652 unsigned NumExprs = readInt();
12653 llvm::SmallVector<Expr *> ExprList;
12654 for (unsigned I = 0; I < NumExprs; ++I)
12655 ExprList.push_back(Elt: readSubExpr());
12656 return ExprList;
12657}
12658
12659OpenACCClause *ASTRecordReader::readOpenACCClause() {
12660 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
12661 SourceLocation BeginLoc = readSourceLocation();
12662 SourceLocation EndLoc = readSourceLocation();
12663
12664 switch (ClauseKind) {
12665 case OpenACCClauseKind::Default: {
12666 SourceLocation LParenLoc = readSourceLocation();
12667 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
12668 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
12669 EndLoc);
12670 }
12671 case OpenACCClauseKind::If: {
12672 SourceLocation LParenLoc = readSourceLocation();
12673 Expr *CondExpr = readSubExpr();
12674 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
12675 EndLoc);
12676 }
12677 case OpenACCClauseKind::Self: {
12678 SourceLocation LParenLoc = readSourceLocation();
12679 bool isConditionExprClause = readBool();
12680 if (isConditionExprClause) {
12681 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
12682 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
12683 ConditionExpr: CondExpr, EndLoc);
12684 }
12685 unsigned NumVars = readInt();
12686 llvm::SmallVector<Expr *> VarList;
12687 for (unsigned I = 0; I < NumVars; ++I)
12688 VarList.push_back(Elt: readSubExpr());
12689 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
12690 EndLoc);
12691 }
12692 case OpenACCClauseKind::NumGangs: {
12693 SourceLocation LParenLoc = readSourceLocation();
12694 unsigned NumClauses = readInt();
12695 llvm::SmallVector<Expr *> IntExprs;
12696 for (unsigned I = 0; I < NumClauses; ++I)
12697 IntExprs.push_back(Elt: readSubExpr());
12698 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
12699 IntExprs, EndLoc);
12700 }
12701 case OpenACCClauseKind::NumWorkers: {
12702 SourceLocation LParenLoc = readSourceLocation();
12703 Expr *IntExpr = readSubExpr();
12704 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
12705 IntExpr, EndLoc);
12706 }
12707 case OpenACCClauseKind::DeviceNum: {
12708 SourceLocation LParenLoc = readSourceLocation();
12709 Expr *IntExpr = readSubExpr();
12710 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
12711 IntExpr, EndLoc);
12712 }
12713 case OpenACCClauseKind::DefaultAsync: {
12714 SourceLocation LParenLoc = readSourceLocation();
12715 Expr *IntExpr = readSubExpr();
12716 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
12717 IntExpr, EndLoc);
12718 }
12719 case OpenACCClauseKind::VectorLength: {
12720 SourceLocation LParenLoc = readSourceLocation();
12721 Expr *IntExpr = readSubExpr();
12722 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
12723 IntExpr, EndLoc);
12724 }
12725 case OpenACCClauseKind::Private: {
12726 SourceLocation LParenLoc = readSourceLocation();
12727 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12728 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12729 VarList, EndLoc);
12730 }
12731 case OpenACCClauseKind::Host: {
12732 SourceLocation LParenLoc = readSourceLocation();
12733 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12734 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
12735 EndLoc);
12736 }
12737 case OpenACCClauseKind::Device: {
12738 SourceLocation LParenLoc = readSourceLocation();
12739 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12740 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
12741 VarList, EndLoc);
12742 }
12743 case OpenACCClauseKind::FirstPrivate: {
12744 SourceLocation LParenLoc = readSourceLocation();
12745 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12746 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12747 VarList, EndLoc);
12748 }
12749 case OpenACCClauseKind::Attach: {
12750 SourceLocation LParenLoc = readSourceLocation();
12751 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12752 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
12753 VarList, EndLoc);
12754 }
12755 case OpenACCClauseKind::Detach: {
12756 SourceLocation LParenLoc = readSourceLocation();
12757 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12758 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
12759 VarList, EndLoc);
12760 }
12761 case OpenACCClauseKind::Delete: {
12762 SourceLocation LParenLoc = readSourceLocation();
12763 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12764 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
12765 VarList, EndLoc);
12766 }
12767 case OpenACCClauseKind::UseDevice: {
12768 SourceLocation LParenLoc = readSourceLocation();
12769 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12770 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
12771 VarList, EndLoc);
12772 }
12773 case OpenACCClauseKind::DevicePtr: {
12774 SourceLocation LParenLoc = readSourceLocation();
12775 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12776 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
12777 VarList, EndLoc);
12778 }
12779 case OpenACCClauseKind::NoCreate: {
12780 SourceLocation LParenLoc = readSourceLocation();
12781 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12782 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12783 VarList, EndLoc);
12784 }
12785 case OpenACCClauseKind::Present: {
12786 SourceLocation LParenLoc = readSourceLocation();
12787 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12788 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
12789 VarList, EndLoc);
12790 }
12791 case OpenACCClauseKind::PCopy:
12792 case OpenACCClauseKind::PresentOrCopy:
12793 case OpenACCClauseKind::Copy: {
12794 SourceLocation LParenLoc = readSourceLocation();
12795 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12796 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12797 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12798 LParenLoc, Mods: ModList, VarList, EndLoc);
12799 }
12800 case OpenACCClauseKind::CopyIn:
12801 case OpenACCClauseKind::PCopyIn:
12802 case OpenACCClauseKind::PresentOrCopyIn: {
12803 SourceLocation LParenLoc = readSourceLocation();
12804 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12805 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12806 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12807 LParenLoc, Mods: ModList, VarList, EndLoc);
12808 }
12809 case OpenACCClauseKind::CopyOut:
12810 case OpenACCClauseKind::PCopyOut:
12811 case OpenACCClauseKind::PresentOrCopyOut: {
12812 SourceLocation LParenLoc = readSourceLocation();
12813 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12814 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12815 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12816 LParenLoc, Mods: ModList, VarList, EndLoc);
12817 }
12818 case OpenACCClauseKind::Create:
12819 case OpenACCClauseKind::PCreate:
12820 case OpenACCClauseKind::PresentOrCreate: {
12821 SourceLocation LParenLoc = readSourceLocation();
12822 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12823 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12824 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12825 LParenLoc, Mods: ModList, VarList, EndLoc);
12826 }
12827 case OpenACCClauseKind::Async: {
12828 SourceLocation LParenLoc = readSourceLocation();
12829 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
12830 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
12831 IntExpr: AsyncExpr, EndLoc);
12832 }
12833 case OpenACCClauseKind::Wait: {
12834 SourceLocation LParenLoc = readSourceLocation();
12835 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
12836 SourceLocation QueuesLoc = readSourceLocation();
12837 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
12838 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
12839 DevNumExpr, QueuesLoc, QueueIdExprs,
12840 EndLoc);
12841 }
12842 case OpenACCClauseKind::DeviceType:
12843 case OpenACCClauseKind::DType: {
12844 SourceLocation LParenLoc = readSourceLocation();
12845 llvm::SmallVector<DeviceTypeArgument> Archs;
12846 unsigned NumArchs = readInt();
12847
12848 for (unsigned I = 0; I < NumArchs; ++I) {
12849 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
12850 SourceLocation Loc = readSourceLocation();
12851 Archs.emplace_back(Args&: Loc, Args&: Ident);
12852 }
12853
12854 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
12855 LParenLoc, Archs, EndLoc);
12856 }
12857 case OpenACCClauseKind::Reduction: {
12858 SourceLocation LParenLoc = readSourceLocation();
12859 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
12860 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12861 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
12862 VarList, EndLoc);
12863 }
12864 case OpenACCClauseKind::Seq:
12865 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12866 case OpenACCClauseKind::NoHost:
12867 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12868 case OpenACCClauseKind::Finalize:
12869 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12870 case OpenACCClauseKind::IfPresent:
12871 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12872 case OpenACCClauseKind::Independent:
12873 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12874 case OpenACCClauseKind::Auto:
12875 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12876 case OpenACCClauseKind::Collapse: {
12877 SourceLocation LParenLoc = readSourceLocation();
12878 bool HasForce = readBool();
12879 Expr *LoopCount = readSubExpr();
12880 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
12881 HasForce, LoopCount, EndLoc);
12882 }
12883 case OpenACCClauseKind::Tile: {
12884 SourceLocation LParenLoc = readSourceLocation();
12885 unsigned NumClauses = readInt();
12886 llvm::SmallVector<Expr *> SizeExprs;
12887 for (unsigned I = 0; I < NumClauses; ++I)
12888 SizeExprs.push_back(Elt: readSubExpr());
12889 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
12890 SizeExprs, EndLoc);
12891 }
12892 case OpenACCClauseKind::Gang: {
12893 SourceLocation LParenLoc = readSourceLocation();
12894 unsigned NumExprs = readInt();
12895 llvm::SmallVector<OpenACCGangKind> GangKinds;
12896 llvm::SmallVector<Expr *> Exprs;
12897 for (unsigned I = 0; I < NumExprs; ++I) {
12898 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
12899 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
12900 Exprs.push_back(Elt: readExpr());
12901 }
12902 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12903 GangKinds, IntExprs: Exprs, EndLoc);
12904 }
12905 case OpenACCClauseKind::Worker: {
12906 SourceLocation LParenLoc = readSourceLocation();
12907 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
12908 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12909 IntExpr: WorkerExpr, EndLoc);
12910 }
12911 case OpenACCClauseKind::Vector: {
12912 SourceLocation LParenLoc = readSourceLocation();
12913 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
12914 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12915 IntExpr: VectorExpr, EndLoc);
12916 }
12917 case OpenACCClauseKind::Link: {
12918 SourceLocation LParenLoc = readSourceLocation();
12919 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12920 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
12921 EndLoc);
12922 }
12923 case OpenACCClauseKind::DeviceResident: {
12924 SourceLocation LParenLoc = readSourceLocation();
12925 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12926 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
12927 LParenLoc, VarList, EndLoc);
12928 }
12929
12930 case OpenACCClauseKind::Bind: {
12931 SourceLocation LParenLoc = readSourceLocation();
12932 bool IsString = readBool();
12933 if (IsString)
12934 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
12935 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
12936 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
12937 ID: readIdentifier(), EndLoc);
12938 }
12939 case OpenACCClauseKind::Shortloop:
12940 case OpenACCClauseKind::Invalid:
12941 llvm_unreachable("Clause serialization not yet implemented");
12942 }
12943 llvm_unreachable("Invalid Clause Kind");
12944}
12945
12946void ASTRecordReader::readOpenACCClauseList(
12947 MutableArrayRef<const OpenACCClause *> Clauses) {
12948 for (unsigned I = 0; I < Clauses.size(); ++I)
12949 Clauses[I] = readOpenACCClause();
12950}
12951
12952void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
12953 unsigned NumVars = readInt();
12954 A->Clauses.resize(N: NumVars);
12955 readOpenACCClauseList(Clauses: A->Clauses);
12956}
12957
12958static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
12959 // TODO: Maybe it is better to check PrimaryModuleName is a valid
12960 // module name?
12961 llvm::FoldingSetNodeID ID;
12962 ID.AddString(String: PrimaryModuleName);
12963 return ID.computeStableHash();
12964}
12965
12966UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
12967 if (!M)
12968 return std::nullopt;
12969
12970 if (M->isHeaderLikeModule())
12971 return std::nullopt;
12972
12973 if (M->isGlobalModule())
12974 return std::nullopt;
12975
12976 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
12977 return getStableHashForModuleName(PrimaryModuleName);
12978}
12979