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

source code of clang/lib/Serialization/ASTReader.cpp