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 // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.
282 using CK = LangOptions::CompatibilityKind;
283
284#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
285 if constexpr (CK::Compatibility != CK::Benign) { \
286 if ((CK::Compatibility == CK::NotCompatible) || \
287 (CK::Compatibility == CK::Compatible && \
288 !AllowCompatibleDifferences)) { \
289 if (ExistingLangOpts.Name != LangOpts.Name) { \
290 if (Diags) { \
291 if (Bits == 1) \
292 Diags->Report(diag::err_ast_file_langopt_mismatch) \
293 << Description << LangOpts.Name << ExistingLangOpts.Name \
294 << ModuleFilename; \
295 else \
296 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
297 << Description << ModuleFilename; \
298 } \
299 return true; \
300 } \
301 } \
302 }
303
304#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \
305 if constexpr (CK::Compatibility != CK::Benign) { \
306 if ((CK::Compatibility == CK::NotCompatible) || \
307 (CK::Compatibility == CK::Compatible && \
308 !AllowCompatibleDifferences)) { \
309 if (ExistingLangOpts.Name != LangOpts.Name) { \
310 if (Diags) \
311 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
312 << Description << ModuleFilename; \
313 return true; \
314 } \
315 } \
316 }
317
318#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
319 if constexpr (CK::Compatibility != CK::Benign) { \
320 if ((CK::Compatibility == CK::NotCompatible) || \
321 (CK::Compatibility == CK::Compatible && \
322 !AllowCompatibleDifferences)) { \
323 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
324 if (Diags) \
325 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
326 << Description << ModuleFilename; \
327 return true; \
328 } \
329 } \
330 }
331
332#include "clang/Basic/LangOptions.def"
333
334 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
335 if (Diags)
336 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
337 << "module features" << ModuleFilename;
338 return true;
339 }
340
341 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
342 if (Diags)
343 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
344 << "target Objective-C runtime" << ModuleFilename;
345 return true;
346 }
347
348 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
349 LangOpts.CommentOpts.BlockCommandNames) {
350 if (Diags)
351 Diags->Report(DiagID: diag::err_ast_file_langopt_value_mismatch)
352 << "block command names" << ModuleFilename;
353 return true;
354 }
355
356 // Sanitizer feature mismatches are treated as compatible differences. If
357 // compatible differences aren't allowed, we still only want to check for
358 // mismatches of non-modular sanitizers (the only ones which can affect AST
359 // generation).
360 if (!AllowCompatibleDifferences) {
361 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
362 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
363 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
364 ExistingSanitizers.clear(K: ModularSanitizers);
365 ImportedSanitizers.clear(K: ModularSanitizers);
366 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
367 const std::string Flag = "-fsanitize=";
368 if (Diags) {
369#define SANITIZER(NAME, ID) \
370 { \
371 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
372 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
373 if (InExistingModule != InImportedModule) \
374 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
375 << InExistingModule << ModuleFilename << (Flag + NAME); \
376 }
377#include "clang/Basic/Sanitizers.def"
378 }
379 return true;
380 }
381 }
382
383 return false;
384}
385
386/// Compare the given set of target options against an existing set of
387/// target options.
388///
389/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
390///
391/// \returns true if the target options mis-match, false otherwise.
392static bool checkTargetOptions(const TargetOptions &TargetOpts,
393 const TargetOptions &ExistingTargetOpts,
394 StringRef ModuleFilename,
395 DiagnosticsEngine *Diags,
396 bool AllowCompatibleDifferences = true) {
397#define CHECK_TARGET_OPT(Field, Name) \
398 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
399 if (Diags) \
400 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
401 << ModuleFilename << Name << TargetOpts.Field \
402 << ExistingTargetOpts.Field; \
403 return true; \
404 }
405
406 // The triple and ABI must match exactly.
407 CHECK_TARGET_OPT(Triple, "target");
408 CHECK_TARGET_OPT(ABI, "target ABI");
409
410 // We can tolerate different CPUs in many cases, notably when one CPU
411 // supports a strict superset of another. When allowing compatible
412 // differences skip this check.
413 if (!AllowCompatibleDifferences) {
414 CHECK_TARGET_OPT(CPU, "target CPU");
415 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
416 }
417
418#undef CHECK_TARGET_OPT
419
420 // Compare feature sets.
421 SmallVector<StringRef, 4> ExistingFeatures(
422 ExistingTargetOpts.FeaturesAsWritten.begin(),
423 ExistingTargetOpts.FeaturesAsWritten.end());
424 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
425 TargetOpts.FeaturesAsWritten.end());
426 llvm::sort(C&: ExistingFeatures);
427 llvm::sort(C&: ReadFeatures);
428
429 // We compute the set difference in both directions explicitly so that we can
430 // diagnose the differences differently.
431 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
432 std::set_difference(
433 first1: ExistingFeatures.begin(), last1: ExistingFeatures.end(), first2: ReadFeatures.begin(),
434 last2: ReadFeatures.end(), result: std::back_inserter(x&: UnmatchedExistingFeatures));
435 std::set_difference(first1: ReadFeatures.begin(), last1: ReadFeatures.end(),
436 first2: ExistingFeatures.begin(), last2: ExistingFeatures.end(),
437 result: std::back_inserter(x&: UnmatchedReadFeatures));
438
439 // If we are allowing compatible differences and the read feature set is
440 // a strict subset of the existing feature set, there is nothing to diagnose.
441 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
442 return false;
443
444 if (Diags) {
445 for (StringRef Feature : UnmatchedReadFeatures)
446 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
447 << /* is-existing-feature */ false << ModuleFilename << Feature;
448 for (StringRef Feature : UnmatchedExistingFeatures)
449 Diags->Report(DiagID: diag::err_ast_file_targetopt_feature_mismatch)
450 << /* is-existing-feature */ true << ModuleFilename << Feature;
451 }
452
453 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
454}
455
456bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
457 StringRef ModuleFilename, bool Complain,
458 bool AllowCompatibleDifferences) {
459 const LangOptions &ExistingLangOpts = PP.getLangOpts();
460 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
461 Diags: Complain ? &Reader.Diags : nullptr,
462 AllowCompatibleDifferences);
463}
464
465bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
466 StringRef ModuleFilename, bool Complain,
467 bool AllowCompatibleDifferences) {
468 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
469 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
470 Diags: Complain ? &Reader.Diags : nullptr,
471 AllowCompatibleDifferences);
472}
473
474namespace {
475
476using MacroDefinitionsMap =
477 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
478using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
479
480} // namespace
481
482static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
483 DiagnosticsEngine &Diags,
484 StringRef ModuleFilename,
485 bool Complain) {
486 using Level = DiagnosticsEngine::Level;
487
488 // Check current mappings for new -Werror mappings, and the stored mappings
489 // for cases that were explicitly mapped to *not* be errors that are now
490 // errors because of options like -Werror.
491 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
492
493 for (DiagnosticsEngine *MappingSource : MappingSources) {
494 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
495 diag::kind DiagID = DiagIDMappingPair.first;
496 Level CurLevel = Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
497 if (CurLevel < DiagnosticsEngine::Error)
498 continue; // not significant
499 Level StoredLevel =
500 StoredDiags.getDiagnosticLevel(DiagID, Loc: SourceLocation());
501 if (StoredLevel < DiagnosticsEngine::Error) {
502 if (Complain)
503 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
504 << "-Werror=" + Diags.getDiagnosticIDs()
505 ->getWarningOptionForDiag(DiagID)
506 .str()
507 << ModuleFilename;
508 return true;
509 }
510 }
511 }
512
513 return false;
514}
515
516static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
517 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
518 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
519 return true;
520 return Ext >= diag::Severity::Error;
521}
522
523static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
524 DiagnosticsEngine &Diags,
525 StringRef ModuleFilename, bool IsSystem,
526 bool SystemHeaderWarningsInModule,
527 bool Complain) {
528 // Top-level options
529 if (IsSystem) {
530 if (Diags.getSuppressSystemWarnings())
531 return false;
532 // If -Wsystem-headers was not enabled before, and it was not explicit,
533 // be conservative
534 if (StoredDiags.getSuppressSystemWarnings() &&
535 !SystemHeaderWarningsInModule) {
536 if (Complain)
537 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
538 << "-Wsystem-headers" << ModuleFilename;
539 return true;
540 }
541 }
542
543 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
544 if (Complain)
545 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
546 << "-Werror" << ModuleFilename;
547 return true;
548 }
549
550 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
551 !StoredDiags.getEnableAllWarnings()) {
552 if (Complain)
553 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
554 << "-Weverything -Werror" << ModuleFilename;
555 return true;
556 }
557
558 if (isExtHandlingFromDiagsError(Diags) &&
559 !isExtHandlingFromDiagsError(Diags&: StoredDiags)) {
560 if (Complain)
561 Diags.Report(DiagID: diag::err_ast_file_diagopt_mismatch)
562 << "-pedantic-errors" << ModuleFilename;
563 return true;
564 }
565
566 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
567 Complain);
568}
569
570/// Return the top import module if it is implicit, nullptr otherwise.
571static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
572 Preprocessor &PP) {
573 // If the original import came from a file explicitly generated by the user,
574 // don't check the diagnostic mappings.
575 // FIXME: currently this is approximated by checking whether this is not a
576 // module import of an implicitly-loaded module file.
577 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
578 // the transitive closure of its imports, since unrelated modules cannot be
579 // imported until after this module finishes validation.
580 ModuleFile *TopImport = &*ModuleMgr.rbegin();
581 while (!TopImport->ImportedBy.empty())
582 TopImport = TopImport->ImportedBy[0];
583 if (TopImport->Kind != MK_ImplicitModule)
584 return nullptr;
585
586 StringRef ModuleName = TopImport->ModuleName;
587 assert(!ModuleName.empty() && "diagnostic options read before module name");
588
589 Module *M =
590 PP.getHeaderSearchInfo().lookupModule(ModuleName, ImportLoc: TopImport->ImportLoc);
591 assert(M && "missing module");
592 return M;
593}
594
595bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,
596 StringRef ModuleFilename,
597 bool Complain) {
598 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
599 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
600 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
601 new DiagnosticsEngine(DiagIDs, DiagOpts));
602 // This should never fail, because we would have processed these options
603 // before writing them to an ASTFile.
604 ProcessWarningOptions(Diags&: *Diags, Opts: DiagOpts,
605 VFS&: PP.getFileManager().getVirtualFileSystem(),
606 /*Report*/ ReportDiags: false);
607
608 ModuleManager &ModuleMgr = Reader.getModuleManager();
609 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
610
611 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
612 if (!TopM)
613 return false;
614
615 Module *Importer = PP.getCurrentModule();
616
617 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
618 bool SystemHeaderWarningsInModule =
619 Importer && llvm::is_contained(Range&: ExistingOpts.SystemHeaderWarningsModules,
620 Element: Importer->Name);
621
622 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
623 // contains the union of their flags.
624 return checkDiagnosticMappings(StoredDiags&: *Diags, Diags&: ExistingDiags, ModuleFilename,
625 IsSystem: TopM->IsSystem, SystemHeaderWarningsInModule,
626 Complain);
627}
628
629/// Collect the macro definitions provided by the given preprocessor
630/// options.
631static void
632collectMacroDefinitions(const PreprocessorOptions &PPOpts,
633 MacroDefinitionsMap &Macros,
634 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
635 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
636 StringRef Macro = PPOpts.Macros[I].first;
637 bool IsUndef = PPOpts.Macros[I].second;
638
639 std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '=');
640 StringRef MacroName = MacroPair.first;
641 StringRef MacroBody = MacroPair.second;
642
643 // For an #undef'd macro, we only care about the name.
644 if (IsUndef) {
645 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
646 if (MacroNames && Inserted)
647 MacroNames->push_back(Elt: MacroName);
648
649 It->second = std::make_pair(x: "", y: true);
650 continue;
651 }
652
653 // For a #define'd macro, figure out the actual definition.
654 if (MacroName.size() == Macro.size())
655 MacroBody = "1";
656 else {
657 // Note: GCC drops anything following an end-of-line character.
658 StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r");
659 MacroBody = MacroBody.substr(Start: 0, N: End);
660 }
661
662 auto [It, Inserted] = Macros.try_emplace(Key: MacroName);
663 if (MacroNames && Inserted)
664 MacroNames->push_back(Elt: MacroName);
665 It->second = std::make_pair(x&: MacroBody, y: false);
666 }
667}
668
669enum OptionValidation {
670 OptionValidateNone,
671 OptionValidateContradictions,
672 OptionValidateStrictMatches,
673};
674
675/// Check the preprocessor options deserialized from the control block
676/// against the preprocessor options in an existing preprocessor.
677///
678/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
679/// \param Validation If set to OptionValidateNone, ignore differences in
680/// preprocessor options. If set to OptionValidateContradictions,
681/// require that options passed both in the AST file and on the command
682/// line (-D or -U) match, but tolerate options missing in one or the
683/// other. If set to OptionValidateContradictions, require that there
684/// are no differences in the options between the two.
685static bool checkPreprocessorOptions(
686 const PreprocessorOptions &PPOpts,
687 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
688 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
689 std::string &SuggestedPredefines, const LangOptions &LangOpts,
690 OptionValidation Validation = OptionValidateContradictions) {
691 if (ReadMacros) {
692 // Check macro definitions.
693 MacroDefinitionsMap ASTFileMacros;
694 collectMacroDefinitions(PPOpts, Macros&: ASTFileMacros);
695 MacroDefinitionsMap ExistingMacros;
696 SmallVector<StringRef, 4> ExistingMacroNames;
697 collectMacroDefinitions(PPOpts: ExistingPPOpts, Macros&: ExistingMacros,
698 MacroNames: &ExistingMacroNames);
699
700 // Use a line marker to enter the <command line> file, as the defines and
701 // undefines here will have come from the command line.
702 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
703
704 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
705 // Dig out the macro definition in the existing preprocessor options.
706 StringRef MacroName = ExistingMacroNames[I];
707 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
708
709 // Check whether we know anything about this macro name or not.
710 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
711 ASTFileMacros.find(Key: MacroName);
712 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
713 if (Validation == OptionValidateStrictMatches) {
714 // If strict matches are requested, don't tolerate any extra defines
715 // on the command line that are missing in the AST file.
716 if (Diags) {
717 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
718 << MacroName << true << ModuleFilename;
719 }
720 return true;
721 }
722 // FIXME: Check whether this identifier was referenced anywhere in the
723 // AST file. If so, we should reject the AST file. Unfortunately, this
724 // information isn't in the control block. What shall we do about it?
725
726 if (Existing.second) {
727 SuggestedPredefines += "#undef ";
728 SuggestedPredefines += MacroName.str();
729 SuggestedPredefines += '\n';
730 } else {
731 SuggestedPredefines += "#define ";
732 SuggestedPredefines += MacroName.str();
733 SuggestedPredefines += ' ';
734 SuggestedPredefines += Existing.first.str();
735 SuggestedPredefines += '\n';
736 }
737 continue;
738 }
739
740 // If the macro was defined in one but undef'd in the other, we have a
741 // conflict.
742 if (Existing.second != Known->second.second) {
743 if (Diags) {
744 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
745 << MacroName << Known->second.second << ModuleFilename;
746 }
747 return true;
748 }
749
750 // If the macro was #undef'd in both, or if the macro bodies are
751 // identical, it's fine.
752 if (Existing.second || Existing.first == Known->second.first) {
753 ASTFileMacros.erase(I: Known);
754 continue;
755 }
756
757 // The macro bodies differ; complain.
758 if (Diags) {
759 Diags->Report(DiagID: diag::err_ast_file_macro_def_conflict)
760 << MacroName << Known->second.first << Existing.first
761 << ModuleFilename;
762 }
763 return true;
764 }
765
766 // Leave the <command line> file and return to <built-in>.
767 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
768
769 if (Validation == OptionValidateStrictMatches) {
770 // If strict matches are requested, don't tolerate any extra defines in
771 // the AST file that are missing on the command line.
772 for (const auto &MacroName : ASTFileMacros.keys()) {
773 if (Diags) {
774 Diags->Report(DiagID: diag::err_ast_file_macro_def_undef)
775 << MacroName << false << ModuleFilename;
776 }
777 return true;
778 }
779 }
780 }
781
782 // Check whether we're using predefines.
783 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
784 Validation != OptionValidateNone) {
785 if (Diags) {
786 Diags->Report(DiagID: diag::err_ast_file_undef)
787 << ExistingPPOpts.UsePredefines << ModuleFilename;
788 }
789 return true;
790 }
791
792 // Detailed record is important since it is used for the module cache hash.
793 if (LangOpts.Modules &&
794 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
795 Validation != OptionValidateNone) {
796 if (Diags) {
797 Diags->Report(DiagID: diag::err_ast_file_pp_detailed_record)
798 << PPOpts.DetailedRecord << ModuleFilename;
799 }
800 return true;
801 }
802
803 // Compute the #include and #include_macros lines we need.
804 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
805 StringRef File = ExistingPPOpts.Includes[I];
806
807 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
808 !ExistingPPOpts.PCHThroughHeader.empty()) {
809 // In case the through header is an include, we must add all the includes
810 // to the predefines so the start point can be determined.
811 SuggestedPredefines += "#include \"";
812 SuggestedPredefines += File;
813 SuggestedPredefines += "\"\n";
814 continue;
815 }
816
817 if (File == ExistingPPOpts.ImplicitPCHInclude)
818 continue;
819
820 if (llvm::is_contained(Range: PPOpts.Includes, Element: File))
821 continue;
822
823 SuggestedPredefines += "#include \"";
824 SuggestedPredefines += File;
825 SuggestedPredefines += "\"\n";
826 }
827
828 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
829 StringRef File = ExistingPPOpts.MacroIncludes[I];
830 if (llvm::is_contained(Range: PPOpts.MacroIncludes, Element: File))
831 continue;
832
833 SuggestedPredefines += "#__include_macros \"";
834 SuggestedPredefines += File;
835 SuggestedPredefines += "\"\n##\n";
836 }
837
838 return false;
839}
840
841bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
842 StringRef ModuleFilename,
843 bool ReadMacros, bool Complain,
844 std::string &SuggestedPredefines) {
845 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
846
847 return checkPreprocessorOptions(
848 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
849 Diags: Complain ? &Reader.Diags : nullptr, FileMgr&: PP.getFileManager(),
850 SuggestedPredefines, LangOpts: PP.getLangOpts());
851}
852
853bool SimpleASTReaderListener::ReadPreprocessorOptions(
854 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
855 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
856 return checkPreprocessorOptions(PPOpts, ExistingPPOpts: PP.getPreprocessorOpts(),
857 ModuleFilename, ReadMacros, Diags: nullptr,
858 FileMgr&: PP.getFileManager(), SuggestedPredefines,
859 LangOpts: PP.getLangOpts(), Validation: OptionValidateNone);
860}
861
862/// Check that the specified and the existing module cache paths are equivalent.
863///
864/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
865/// \returns true when the module cache paths differ.
866static bool checkModuleCachePath(llvm::vfs::FileSystem &VFS,
867 StringRef SpecificModuleCachePath,
868 StringRef ExistingModuleCachePath,
869 StringRef ModuleFilename,
870 DiagnosticsEngine *Diags,
871 const LangOptions &LangOpts,
872 const PreprocessorOptions &PPOpts) {
873 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
874 SpecificModuleCachePath == ExistingModuleCachePath)
875 return false;
876 auto EqualOrErr =
877 VFS.equivalent(A: SpecificModuleCachePath, B: ExistingModuleCachePath);
878 if (EqualOrErr && *EqualOrErr)
879 return false;
880 if (Diags)
881 Diags->Report(DiagID: diag::err_ast_file_modulecache_mismatch)
882 << SpecificModuleCachePath << ExistingModuleCachePath << ModuleFilename;
883 return true;
884}
885
886bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
887 StringRef ModuleFilename,
888 StringRef SpecificModuleCachePath,
889 bool Complain) {
890 return checkModuleCachePath(
891 VFS&: Reader.getFileManager().getVirtualFileSystem(), SpecificModuleCachePath,
892 ExistingModuleCachePath: PP.getHeaderSearchInfo().getModuleCachePath(), ModuleFilename,
893 Diags: Complain ? &Reader.Diags : nullptr, LangOpts: PP.getLangOpts(),
894 PPOpts: PP.getPreprocessorOpts());
895}
896
897void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
898 PP.setCounterValue(Value);
899}
900
901//===----------------------------------------------------------------------===//
902// AST reader implementation
903//===----------------------------------------------------------------------===//
904
905static uint64_t readULEB(const unsigned char *&P) {
906 unsigned Length = 0;
907 const char *Error = nullptr;
908
909 uint64_t Val = llvm::decodeULEB128(p: P, n: &Length, end: nullptr, error: &Error);
910 if (Error)
911 llvm::report_fatal_error(reason: Error);
912 P += Length;
913 return Val;
914}
915
916/// Read ULEB-encoded key length and data length.
917static std::pair<unsigned, unsigned>
918readULEBKeyDataLength(const unsigned char *&P) {
919 unsigned KeyLen = readULEB(P);
920 if ((unsigned)KeyLen != KeyLen)
921 llvm::report_fatal_error(reason: "key too large");
922
923 unsigned DataLen = readULEB(P);
924 if ((unsigned)DataLen != DataLen)
925 llvm::report_fatal_error(reason: "data too large");
926
927 return std::make_pair(x&: KeyLen, y&: DataLen);
928}
929
930void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
931 bool TakeOwnership) {
932 DeserializationListener = Listener;
933 OwnsDeserializationListener = TakeOwnership;
934}
935
936unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
937 return serialization::ComputeHash(Sel);
938}
939
940LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {
941 LocalDeclID ID(Value);
942#ifndef NDEBUG
943 if (!MF.ModuleOffsetMap.empty())
944 Reader.ReadModuleOffsetMap(MF);
945
946 unsigned ModuleFileIndex = ID.getModuleFileIndex();
947 unsigned LocalDeclID = ID.getLocalDeclIndex();
948
949 assert(ModuleFileIndex <= MF.TransitiveImports.size());
950
951 ModuleFile *OwningModuleFile =
952 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
953 assert(OwningModuleFile);
954
955 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
956
957 if (!ModuleFileIndex)
958 LocalNumDecls += NUM_PREDEF_DECL_IDS;
959
960 assert(LocalDeclID < LocalNumDecls);
961#endif
962 (void)Reader;
963 (void)MF;
964 return ID;
965}
966
967LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,
968 unsigned ModuleFileIndex, unsigned LocalDeclID) {
969 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
970 return LocalDeclID::get(Reader, MF, Value);
971}
972
973std::pair<unsigned, unsigned>
974ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
975 return readULEBKeyDataLength(P&: d);
976}
977
978ASTSelectorLookupTrait::internal_key_type
979ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
980 using namespace llvm::support;
981
982 SelectorTable &SelTable = Reader.getContext().Selectors;
983 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
984 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
985 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
986 if (N == 0)
987 return SelTable.getNullarySelector(ID: FirstII);
988 else if (N == 1)
989 return SelTable.getUnarySelector(ID: FirstII);
990
991 SmallVector<const IdentifierInfo *, 16> Args;
992 Args.push_back(Elt: FirstII);
993 for (unsigned I = 1; I != N; ++I)
994 Args.push_back(Elt: Reader.getLocalIdentifier(
995 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d)));
996
997 return SelTable.getSelector(NumArgs: N, IIV: Args.data());
998}
999
1000ASTSelectorLookupTrait::data_type
1001ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
1002 unsigned DataLen) {
1003 using namespace llvm::support;
1004
1005 data_type Result;
1006
1007 Result.ID = Reader.getGlobalSelectorID(
1008 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d));
1009 unsigned FullInstanceBits =
1010 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1011 unsigned FullFactoryBits =
1012 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1013 Result.InstanceBits = FullInstanceBits & 0x3;
1014 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1015 Result.FactoryBits = FullFactoryBits & 0x3;
1016 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1017 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1018 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1019
1020 // Load instance methods
1021 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1022 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1023 F, LocalID: LocalDeclID::get(
1024 Reader, MF&: F,
1025 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1026 Result.Instance.push_back(Elt: Method);
1027 }
1028
1029 // Load factory methods
1030 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1031 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1032 F, LocalID: LocalDeclID::get(
1033 Reader, MF&: F,
1034 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))))
1035 Result.Factory.push_back(Elt: Method);
1036 }
1037
1038 return Result;
1039}
1040
1041unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
1042 return llvm::djbHash(Buffer: a);
1043}
1044
1045std::pair<unsigned, unsigned>
1046ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
1047 return readULEBKeyDataLength(P&: d);
1048}
1049
1050ASTIdentifierLookupTraitBase::internal_key_type
1051ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1052 assert(n >= 2 && d[n-1] == '\0');
1053 return StringRef((const char*) d, n-1);
1054}
1055
1056/// Whether the given identifier is "interesting".
1057static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1058 bool IsModule) {
1059 bool IsInteresting =
1060 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1061 II.getBuiltinID() != Builtin::ID::NotBuiltin ||
1062 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1063 return II.hadMacroDefinition() || II.isPoisoned() ||
1064 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1065 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1066 II.getFETokenInfo());
1067}
1068
1069static bool readBit(unsigned &Bits) {
1070 bool Value = Bits & 0x1;
1071 Bits >>= 1;
1072 return Value;
1073}
1074
1075IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
1076 using namespace llvm::support;
1077
1078 IdentifierID RawID =
1079 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1080 return Reader.getGlobalIdentifierID(M&: F, LocalID: RawID >> 1);
1081}
1082
1083static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1084 bool IsModule) {
1085 if (!II.isFromAST()) {
1086 II.setIsFromAST();
1087 if (isInterestingIdentifier(Reader, II, IsModule))
1088 II.setChangedSinceDeserialization();
1089 }
1090}
1091
1092IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
1093 const unsigned char* d,
1094 unsigned DataLen) {
1095 using namespace llvm::support;
1096
1097 IdentifierID RawID =
1098 endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d);
1099 bool IsInteresting = RawID & 0x01;
1100
1101 DataLen -= sizeof(IdentifierID);
1102
1103 // Wipe out the "is interesting" bit.
1104 RawID = RawID >> 1;
1105
1106 // Build the IdentifierInfo and link the identifier ID with it.
1107 IdentifierInfo *II = KnownII;
1108 if (!II) {
1109 II = &Reader.getIdentifierTable().getOwn(Name: k);
1110 KnownII = II;
1111 }
1112 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1113 markIdentifierFromAST(Reader, II&: *II, IsModule);
1114 Reader.markIdentifierUpToDate(II);
1115
1116 IdentifierID ID = Reader.getGlobalIdentifierID(M&: F, LocalID: RawID);
1117 if (!IsInteresting) {
1118 // For uninteresting identifiers, there's nothing else to do. Just notify
1119 // the reader that we've finished loading this identifier.
1120 Reader.SetIdentifierInfo(ID, II);
1121 return II;
1122 }
1123
1124 unsigned ObjCOrBuiltinID =
1125 endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1126 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(memory&: d);
1127 bool CPlusPlusOperatorKeyword = readBit(Bits);
1128 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1129 bool Poisoned = readBit(Bits);
1130 bool ExtensionToken = readBit(Bits);
1131 bool HasMacroDefinition = readBit(Bits);
1132
1133 assert(Bits == 0 && "Extra bits in the identifier?");
1134 DataLen -= sizeof(uint16_t) * 2;
1135
1136 // Set or check the various bits in the IdentifierInfo structure.
1137 // Token IDs are read-only.
1138 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1139 II->revertTokenIDToIdentifier();
1140 if (!F.isModule())
1141 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1142 assert(II->isExtensionToken() == ExtensionToken &&
1143 "Incorrect extension token flag");
1144 (void)ExtensionToken;
1145 if (Poisoned)
1146 II->setIsPoisoned(true);
1147 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1148 "Incorrect C++ operator keyword flag");
1149 (void)CPlusPlusOperatorKeyword;
1150
1151 // If this identifier has a macro definition, deserialize it or notify the
1152 // visitor the actual definition is in a different module.
1153 if (HasMacroDefinition) {
1154 uint32_t MacroDirectivesOffset =
1155 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1156 DataLen -= 4;
1157
1158 if (MacroDirectivesOffset)
1159 Reader.addPendingMacro(II, M: &F, MacroDirectivesOffset);
1160 else
1161 hasMacroDefinitionInDependencies = true;
1162 }
1163
1164 Reader.SetIdentifierInfo(ID, II);
1165
1166 // Read all of the declarations visible at global scope with this
1167 // name.
1168 if (DataLen > 0) {
1169 SmallVector<GlobalDeclID, 4> DeclIDs;
1170 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1171 DeclIDs.push_back(Elt: Reader.getGlobalDeclID(
1172 F, LocalID: LocalDeclID::get(
1173 Reader, MF&: F,
1174 Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d))));
1175 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1176 }
1177
1178 return II;
1179}
1180
1181DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1182 : Kind(Name.getNameKind()) {
1183 switch (Kind) {
1184 case DeclarationName::Identifier:
1185 Data = (uint64_t)Name.getAsIdentifierInfo();
1186 break;
1187 case DeclarationName::ObjCZeroArgSelector:
1188 case DeclarationName::ObjCOneArgSelector:
1189 case DeclarationName::ObjCMultiArgSelector:
1190 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1191 break;
1192 case DeclarationName::CXXOperatorName:
1193 Data = Name.getCXXOverloadedOperator();
1194 break;
1195 case DeclarationName::CXXLiteralOperatorName:
1196 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1197 break;
1198 case DeclarationName::CXXDeductionGuideName:
1199 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1200 ->getDeclName().getAsIdentifierInfo();
1201 break;
1202 case DeclarationName::CXXConstructorName:
1203 case DeclarationName::CXXDestructorName:
1204 case DeclarationName::CXXConversionFunctionName:
1205 case DeclarationName::CXXUsingDirective:
1206 Data = 0;
1207 break;
1208 }
1209}
1210
1211unsigned DeclarationNameKey::getHash() const {
1212 llvm::FoldingSetNodeID ID;
1213 ID.AddInteger(I: Kind);
1214
1215 switch (Kind) {
1216 case DeclarationName::Identifier:
1217 case DeclarationName::CXXLiteralOperatorName:
1218 case DeclarationName::CXXDeductionGuideName:
1219 ID.AddString(String: ((IdentifierInfo*)Data)->getName());
1220 break;
1221 case DeclarationName::ObjCZeroArgSelector:
1222 case DeclarationName::ObjCOneArgSelector:
1223 case DeclarationName::ObjCMultiArgSelector:
1224 ID.AddInteger(I: serialization::ComputeHash(Sel: Selector(Data)));
1225 break;
1226 case DeclarationName::CXXOperatorName:
1227 ID.AddInteger(I: (OverloadedOperatorKind)Data);
1228 break;
1229 case DeclarationName::CXXConstructorName:
1230 case DeclarationName::CXXDestructorName:
1231 case DeclarationName::CXXConversionFunctionName:
1232 case DeclarationName::CXXUsingDirective:
1233 break;
1234 }
1235
1236 return ID.computeStableHash();
1237}
1238
1239ModuleFile *
1240ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {
1241 using namespace llvm::support;
1242
1243 uint32_t ModuleFileID =
1244 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1245 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1246}
1247
1248std::pair<unsigned, unsigned>
1249ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {
1250 return readULEBKeyDataLength(P&: d);
1251}
1252
1253DeclarationNameKey
1254ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {
1255 using namespace llvm::support;
1256
1257 auto Kind = (DeclarationName::NameKind)*d++;
1258 uint64_t Data;
1259 switch (Kind) {
1260 case DeclarationName::Identifier:
1261 case DeclarationName::CXXLiteralOperatorName:
1262 case DeclarationName::CXXDeductionGuideName:
1263 Data = (uint64_t)Reader.getLocalIdentifier(
1264 M&: F, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
1265 break;
1266 case DeclarationName::ObjCZeroArgSelector:
1267 case DeclarationName::ObjCOneArgSelector:
1268 case DeclarationName::ObjCMultiArgSelector:
1269 Data = (uint64_t)Reader
1270 .getLocalSelector(
1271 M&: F, LocalID: endian::readNext<uint32_t, llvm::endianness::little>(memory&: d))
1272 .getAsOpaquePtr();
1273 break;
1274 case DeclarationName::CXXOperatorName:
1275 Data = *d++; // OverloadedOperatorKind
1276 break;
1277 case DeclarationName::CXXConstructorName:
1278 case DeclarationName::CXXDestructorName:
1279 case DeclarationName::CXXConversionFunctionName:
1280 case DeclarationName::CXXUsingDirective:
1281 Data = 0;
1282 break;
1283 }
1284
1285 return DeclarationNameKey(Kind, Data);
1286}
1287
1288ASTDeclContextNameLookupTrait::internal_key_type
1289ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1290 return ReadKeyBase(d);
1291}
1292
1293void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(
1294 const unsigned char *d, unsigned DataLen, data_type_builder &Val) {
1295 using namespace llvm::support;
1296
1297 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1298 LocalDeclID ID = LocalDeclID::get(
1299 Reader, MF&: F, Value: endian::readNext<DeclID, llvm::endianness::little>(memory&: d));
1300 Val.insert(ID: Reader.getGlobalDeclID(F, LocalID: ID));
1301 }
1302}
1303
1304void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1305 const unsigned char *d,
1306 unsigned DataLen,
1307 data_type_builder &Val) {
1308 ReadDataIntoImpl(d, DataLen, Val);
1309}
1310
1311ModuleLocalNameLookupTrait::hash_value_type
1312ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {
1313 llvm::FoldingSetNodeID ID;
1314 ID.AddInteger(I: Key.first.getHash());
1315 ID.AddInteger(I: Key.second);
1316 return ID.computeStableHash();
1317}
1318
1319ModuleLocalNameLookupTrait::internal_key_type
1320ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {
1321 DeclarationNameKey Name(Key.first);
1322
1323 UnsignedOrNone ModuleHash = getPrimaryModuleHash(M: Key.second);
1324 if (!ModuleHash)
1325 return {Name, 0};
1326
1327 return {Name, *ModuleHash};
1328}
1329
1330ModuleLocalNameLookupTrait::internal_key_type
1331ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1332 DeclarationNameKey Name = ReadKeyBase(d);
1333 unsigned PrimaryModuleHash =
1334 llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
1335 return {Name, PrimaryModuleHash};
1336}
1337
1338void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,
1339 const unsigned char *d,
1340 unsigned DataLen,
1341 data_type_builder &Val) {
1342 ReadDataIntoImpl(d, DataLen, Val);
1343}
1344
1345ModuleFile *
1346LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {
1347 using namespace llvm::support;
1348
1349 uint32_t ModuleFileID =
1350 endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1351 return Reader.getLocalModuleFile(M&: F, ID: ModuleFileID);
1352}
1353
1354LazySpecializationInfoLookupTrait::internal_key_type
1355LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1356 using namespace llvm::support;
1357 return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(memory&: d);
1358}
1359
1360std::pair<unsigned, unsigned>
1361LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1362 return readULEBKeyDataLength(P&: d);
1363}
1364
1365void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,
1366 const unsigned char *d,
1367 unsigned DataLen,
1368 data_type_builder &Val) {
1369 using namespace llvm::support;
1370
1371 for (unsigned NumDecls =
1372 DataLen / sizeof(serialization::reader::LazySpecializationInfo);
1373 NumDecls; --NumDecls) {
1374 LocalDeclID LocalID = LocalDeclID::get(
1375 Reader, MF&: F,
1376 Value: endian::readNext<DeclID, llvm::endianness::little, unaligned>(memory&: d));
1377 Val.insert(Info: Reader.getGlobalDeclID(F, LocalID));
1378 }
1379}
1380
1381bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1382 BitstreamCursor &Cursor,
1383 uint64_t Offset,
1384 DeclContext *DC) {
1385 assert(Offset != 0);
1386
1387 SavedStreamPosition SavedPosition(Cursor);
1388 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1389 Error(Err: std::move(Err));
1390 return true;
1391 }
1392
1393 RecordData Record;
1394 StringRef Blob;
1395 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1396 if (!MaybeCode) {
1397 Error(Err: MaybeCode.takeError());
1398 return true;
1399 }
1400 unsigned Code = MaybeCode.get();
1401
1402 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1403 if (!MaybeRecCode) {
1404 Error(Err: MaybeRecCode.takeError());
1405 return true;
1406 }
1407 unsigned RecCode = MaybeRecCode.get();
1408 if (RecCode != DECL_CONTEXT_LEXICAL) {
1409 Error(Msg: "Expected lexical block");
1410 return true;
1411 }
1412
1413 assert(!isa<TranslationUnitDecl>(DC) &&
1414 "expected a TU_UPDATE_LEXICAL record for TU");
1415 // If we are handling a C++ class template instantiation, we can see multiple
1416 // lexical updates for the same record. It's important that we select only one
1417 // of them, so that field numbering works properly. Just pick the first one we
1418 // see.
1419 auto &Lex = LexicalDecls[DC];
1420 if (!Lex.first) {
1421 Lex = std::make_pair(
1422 x: &M, y: llvm::ArrayRef(
1423 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1424 Blob.size() / sizeof(DeclID)));
1425 }
1426 DC->setHasExternalLexicalStorage(true);
1427 return false;
1428}
1429
1430bool ASTReader::ReadVisibleDeclContextStorage(
1431 ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,
1432 ASTReader::VisibleDeclContextStorageKind VisibleKind) {
1433 assert(Offset != 0);
1434
1435 SavedStreamPosition SavedPosition(Cursor);
1436 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1437 Error(Err: std::move(Err));
1438 return true;
1439 }
1440
1441 RecordData Record;
1442 StringRef Blob;
1443 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1444 if (!MaybeCode) {
1445 Error(Err: MaybeCode.takeError());
1446 return true;
1447 }
1448 unsigned Code = MaybeCode.get();
1449
1450 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1451 if (!MaybeRecCode) {
1452 Error(Err: MaybeRecCode.takeError());
1453 return true;
1454 }
1455 unsigned RecCode = MaybeRecCode.get();
1456 switch (VisibleKind) {
1457 case VisibleDeclContextStorageKind::GenerallyVisible:
1458 if (RecCode != DECL_CONTEXT_VISIBLE) {
1459 Error(Msg: "Expected visible lookup table block");
1460 return true;
1461 }
1462 break;
1463 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1464 if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {
1465 Error(Msg: "Expected module local visible lookup table block");
1466 return true;
1467 }
1468 break;
1469 case VisibleDeclContextStorageKind::TULocalVisible:
1470 if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {
1471 Error(Msg: "Expected TU local lookup table block");
1472 return true;
1473 }
1474 break;
1475 }
1476
1477 // We can't safely determine the primary context yet, so delay attaching the
1478 // lookup table until we're done with recursive deserialization.
1479 auto *Data = (const unsigned char*)Blob.data();
1480 switch (VisibleKind) {
1481 case VisibleDeclContextStorageKind::GenerallyVisible:
1482 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1483 break;
1484 case VisibleDeclContextStorageKind::ModuleLocalVisible:
1485 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1486 break;
1487 case VisibleDeclContextStorageKind::TULocalVisible:
1488 if (M.Kind == MK_MainFile)
1489 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &M, .Data: Data});
1490 break;
1491 }
1492 return false;
1493}
1494
1495void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,
1496 ModuleFile &M, bool IsPartial) {
1497 D = D->getCanonicalDecl();
1498 auto &SpecLookups =
1499 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
1500 SpecLookups[D].Table.add(File: &M, Data,
1501 InfoObj: reader::LazySpecializationInfoLookupTrait(*this, M));
1502}
1503
1504bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,
1505 uint64_t Offset, Decl *D, bool IsPartial) {
1506 assert(Offset != 0);
1507
1508 SavedStreamPosition SavedPosition(Cursor);
1509 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset)) {
1510 Error(Err: std::move(Err));
1511 return true;
1512 }
1513
1514 RecordData Record;
1515 StringRef Blob;
1516 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1517 if (!MaybeCode) {
1518 Error(Err: MaybeCode.takeError());
1519 return true;
1520 }
1521 unsigned Code = MaybeCode.get();
1522
1523 Expected<unsigned> MaybeRecCode = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1524 if (!MaybeRecCode) {
1525 Error(Err: MaybeRecCode.takeError());
1526 return true;
1527 }
1528 unsigned RecCode = MaybeRecCode.get();
1529 if (RecCode != DECL_SPECIALIZATIONS &&
1530 RecCode != DECL_PARTIAL_SPECIALIZATIONS) {
1531 Error(Msg: "Expected decl specs block");
1532 return true;
1533 }
1534
1535 auto *Data = (const unsigned char *)Blob.data();
1536 AddSpecializations(D, Data, M, IsPartial);
1537 return false;
1538}
1539
1540void ASTReader::Error(StringRef Msg) const {
1541 Error(DiagID: diag::err_fe_ast_file_malformed, Arg1: Msg);
1542 if (PP.getLangOpts().Modules &&
1543 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1544 Diag(DiagID: diag::note_module_cache_path)
1545 << PP.getHeaderSearchInfo().getModuleCachePath();
1546 }
1547}
1548
1549void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1550 StringRef Arg3) const {
1551 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1552}
1553
1554namespace {
1555struct AlreadyReportedDiagnosticError
1556 : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {
1557 static char ID;
1558
1559 void log(raw_ostream &OS) const override {
1560 llvm_unreachable("reporting an already-reported diagnostic error");
1561 }
1562
1563 std::error_code convertToErrorCode() const override {
1564 return llvm::inconvertibleErrorCode();
1565 }
1566};
1567
1568char AlreadyReportedDiagnosticError::ID = 0;
1569} // namespace
1570
1571void ASTReader::Error(llvm::Error &&Err) const {
1572 handleAllErrors(
1573 E: std::move(Err), Handlers: [](AlreadyReportedDiagnosticError &) {},
1574 Handlers: [&](llvm::ErrorInfoBase &E) { return Error(Msg: E.message()); });
1575}
1576
1577//===----------------------------------------------------------------------===//
1578// Source Manager Deserialization
1579//===----------------------------------------------------------------------===//
1580
1581/// Read the line table in the source manager block.
1582void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1583 unsigned Idx = 0;
1584 LineTableInfo &LineTable = SourceMgr.getLineTable();
1585
1586 // Parse the file names
1587 std::map<int, int> FileIDs;
1588 FileIDs[-1] = -1; // For unspecified filenames.
1589 for (unsigned I = 0; Record[Idx]; ++I) {
1590 // Extract the file name
1591 auto Filename = ReadPath(F, Record, Idx);
1592 FileIDs[I] = LineTable.getLineTableFilenameID(Str: Filename);
1593 }
1594 ++Idx;
1595
1596 // Parse the line entries
1597 std::vector<LineEntry> Entries;
1598 while (Idx < Record.size()) {
1599 FileID FID = ReadFileID(F, Record, Idx);
1600
1601 // Extract the line entries
1602 unsigned NumEntries = Record[Idx++];
1603 assert(NumEntries && "no line entries for file ID");
1604 Entries.clear();
1605 Entries.reserve(n: NumEntries);
1606 for (unsigned I = 0; I != NumEntries; ++I) {
1607 unsigned FileOffset = Record[Idx++];
1608 unsigned LineNo = Record[Idx++];
1609 int FilenameID = FileIDs[Record[Idx++]];
1610 SrcMgr::CharacteristicKind FileKind
1611 = (SrcMgr::CharacteristicKind)Record[Idx++];
1612 unsigned IncludeOffset = Record[Idx++];
1613 Entries.push_back(x: LineEntry::get(Offs: FileOffset, Line: LineNo, Filename: FilenameID,
1614 FileKind, IncludeOffset));
1615 }
1616 LineTable.AddEntry(FID, Entries);
1617 }
1618}
1619
1620/// Read a source manager block
1621llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1622 using namespace SrcMgr;
1623
1624 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1625
1626 // Set the source-location entry cursor to the current position in
1627 // the stream. This cursor will be used to read the contents of the
1628 // source manager block initially, and then lazily read
1629 // source-location entries as needed.
1630 SLocEntryCursor = F.Stream;
1631
1632 // The stream itself is going to skip over the source manager block.
1633 if (llvm::Error Err = F.Stream.SkipBlock())
1634 return Err;
1635
1636 // Enter the source manager block.
1637 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(BlockID: SOURCE_MANAGER_BLOCK_ID))
1638 return Err;
1639 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1640
1641 RecordData Record;
1642 while (true) {
1643 Expected<llvm::BitstreamEntry> MaybeE =
1644 SLocEntryCursor.advanceSkippingSubblocks();
1645 if (!MaybeE)
1646 return MaybeE.takeError();
1647 llvm::BitstreamEntry E = MaybeE.get();
1648
1649 switch (E.Kind) {
1650 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1651 case llvm::BitstreamEntry::Error:
1652 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
1653 Fmt: "malformed block record in AST file");
1654 case llvm::BitstreamEntry::EndBlock:
1655 return llvm::Error::success();
1656 case llvm::BitstreamEntry::Record:
1657 // The interesting case.
1658 break;
1659 }
1660
1661 // Read a record.
1662 Record.clear();
1663 StringRef Blob;
1664 Expected<unsigned> MaybeRecord =
1665 SLocEntryCursor.readRecord(AbbrevID: E.ID, Vals&: Record, Blob: &Blob);
1666 if (!MaybeRecord)
1667 return MaybeRecord.takeError();
1668 switch (MaybeRecord.get()) {
1669 default: // Default behavior: ignore.
1670 break;
1671
1672 case SM_SLOC_FILE_ENTRY:
1673 case SM_SLOC_BUFFER_ENTRY:
1674 case SM_SLOC_EXPANSION_ENTRY:
1675 // Once we hit one of the source location entries, we're done.
1676 return llvm::Error::success();
1677 }
1678 }
1679}
1680
1681llvm::Expected<SourceLocation::UIntTy>
1682ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {
1683 BitstreamCursor &Cursor = F->SLocEntryCursor;
1684 SavedStreamPosition SavedPosition(Cursor);
1685 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F->SLocEntryOffsetsBase +
1686 F->SLocEntryOffsets[Index]))
1687 return std::move(Err);
1688
1689 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1690 if (!MaybeEntry)
1691 return MaybeEntry.takeError();
1692
1693 llvm::BitstreamEntry Entry = MaybeEntry.get();
1694 if (Entry.Kind != llvm::BitstreamEntry::Record)
1695 return llvm::createStringError(
1696 EC: std::errc::illegal_byte_sequence,
1697 Fmt: "incorrectly-formatted source location entry in AST file");
1698
1699 RecordData Record;
1700 StringRef Blob;
1701 Expected<unsigned> MaybeSLOC = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1702 if (!MaybeSLOC)
1703 return MaybeSLOC.takeError();
1704
1705 switch (MaybeSLOC.get()) {
1706 default:
1707 return llvm::createStringError(
1708 EC: std::errc::illegal_byte_sequence,
1709 Fmt: "incorrectly-formatted source location entry in AST file");
1710 case SM_SLOC_FILE_ENTRY:
1711 case SM_SLOC_BUFFER_ENTRY:
1712 case SM_SLOC_EXPANSION_ENTRY:
1713 return F->SLocEntryBaseOffset + Record[0];
1714 }
1715}
1716
1717int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
1718 auto SLocMapI =
1719 GlobalSLocOffsetMap.find(K: SourceManager::MaxLoadedOffset - SLocOffset - 1);
1720 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1721 "Corrupted global sloc offset map");
1722 ModuleFile *F = SLocMapI->second;
1723
1724 bool Invalid = false;
1725
1726 auto It = llvm::upper_bound(
1727 Range: llvm::index_range(0, F->LocalNumSLocEntries), Value&: SLocOffset,
1728 C: [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1729 int ID = F->SLocEntryBaseID + LocalIndex;
1730 std::size_t Index = -ID - 2;
1731 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1732 assert(!SourceMgr.SLocEntryLoaded[Index]);
1733 auto MaybeEntryOffset = readSLocOffset(F, Index: LocalIndex);
1734 if (!MaybeEntryOffset) {
1735 Error(Err: MaybeEntryOffset.takeError());
1736 Invalid = true;
1737 return true;
1738 }
1739 SourceMgr.LoadedSLocEntryTable[Index] =
1740 SrcMgr::SLocEntry::getOffsetOnly(Offset: *MaybeEntryOffset);
1741 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1742 }
1743 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1744 });
1745
1746 if (Invalid)
1747 return 0;
1748
1749 // The iterator points to the first entry with start offset greater than the
1750 // offset of interest. The previous entry must contain the offset of interest.
1751 return F->SLocEntryBaseID + *std::prev(x: It);
1752}
1753
1754bool ASTReader::ReadSLocEntry(int ID) {
1755 if (ID == 0)
1756 return false;
1757
1758 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1759 Error(Msg: "source location entry ID out-of-range for AST file");
1760 return true;
1761 }
1762
1763 // Local helper to read the (possibly-compressed) buffer data following the
1764 // entry record.
1765 auto ReadBuffer = [this](
1766 BitstreamCursor &SLocEntryCursor,
1767 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1768 RecordData Record;
1769 StringRef Blob;
1770 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1771 if (!MaybeCode) {
1772 Error(Err: MaybeCode.takeError());
1773 return nullptr;
1774 }
1775 unsigned Code = MaybeCode.get();
1776
1777 Expected<unsigned> MaybeRecCode =
1778 SLocEntryCursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
1779 if (!MaybeRecCode) {
1780 Error(Err: MaybeRecCode.takeError());
1781 return nullptr;
1782 }
1783 unsigned RecCode = MaybeRecCode.get();
1784
1785 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1786 // Inspect the first byte to differentiate zlib (\x78) and zstd
1787 // (little-endian 0xFD2FB528).
1788 const llvm::compression::Format F =
1789 Blob.size() > 0 && Blob.data()[0] == 0x78
1790 ? llvm::compression::Format::Zlib
1791 : llvm::compression::Format::Zstd;
1792 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1793 Error(Msg: Reason);
1794 return nullptr;
1795 }
1796 SmallVector<uint8_t, 0> Decompressed;
1797 if (llvm::Error E = llvm::compression::decompress(
1798 F, Input: llvm::arrayRefFromStringRef(Input: Blob), Output&: Decompressed, UncompressedSize: Record[0])) {
1799 Error(Msg: "could not decompress embedded file contents: " +
1800 llvm::toString(E: std::move(E)));
1801 return nullptr;
1802 }
1803 return llvm::MemoryBuffer::getMemBufferCopy(
1804 InputData: llvm::toStringRef(Input: Decompressed), BufferName: Name);
1805 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1806 return llvm::MemoryBuffer::getMemBuffer(InputData: Blob.drop_back(N: 1), BufferName: Name, RequiresNullTerminator: true);
1807 } else {
1808 Error(Msg: "AST record has invalid code");
1809 return nullptr;
1810 }
1811 };
1812
1813 ModuleFile *F = GlobalSLocEntryMap.find(K: -ID)->second;
1814 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1815 BitNo: F->SLocEntryOffsetsBase +
1816 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1817 Error(Err: std::move(Err));
1818 return true;
1819 }
1820
1821 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1822 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1823
1824 ++NumSLocEntriesRead;
1825 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1826 if (!MaybeEntry) {
1827 Error(Err: MaybeEntry.takeError());
1828 return true;
1829 }
1830 llvm::BitstreamEntry Entry = MaybeEntry.get();
1831
1832 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1833 Error(Msg: "incorrectly-formatted source location entry in AST file");
1834 return true;
1835 }
1836
1837 RecordData Record;
1838 StringRef Blob;
1839 Expected<unsigned> MaybeSLOC =
1840 SLocEntryCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
1841 if (!MaybeSLOC) {
1842 Error(Err: MaybeSLOC.takeError());
1843 return true;
1844 }
1845 switch (MaybeSLOC.get()) {
1846 default:
1847 Error(Msg: "incorrectly-formatted source location entry in AST file");
1848 return true;
1849
1850 case SM_SLOC_FILE_ENTRY: {
1851 // We will detect whether a file changed and return 'Failure' for it, but
1852 // we will also try to fail gracefully by setting up the SLocEntry.
1853 unsigned InputID = Record[4];
1854 InputFile IF = getInputFile(F&: *F, ID: InputID);
1855 OptionalFileEntryRef File = IF.getFile();
1856 bool OverriddenBuffer = IF.isOverridden();
1857
1858 // Note that we only check if a File was returned. If it was out-of-date
1859 // we have complained but we will continue creating a FileID to recover
1860 // gracefully.
1861 if (!File)
1862 return true;
1863
1864 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1865 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1866 // This is the module's main file.
1867 IncludeLoc = getImportLocation(F);
1868 }
1869 SrcMgr::CharacteristicKind
1870 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1871 FileID FID = SourceMgr.createFileID(SourceFile: *File, IncludePos: IncludeLoc, FileCharacter, LoadedID: ID,
1872 LoadedOffset: BaseOffset + Record[0]);
1873 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1874 FileInfo.NumCreatedFIDs = Record[5];
1875 if (Record[3])
1876 FileInfo.setHasLineDirectives();
1877
1878 unsigned NumFileDecls = Record[7];
1879 if (NumFileDecls && ContextObj) {
1880 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1881 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1882 FileDeclIDs[FID] =
1883 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1884 }
1885
1886 const SrcMgr::ContentCache &ContentCache =
1887 SourceMgr.getOrCreateContentCache(SourceFile: *File, isSystemFile: isSystem(CK: FileCharacter));
1888 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1889 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1890 !ContentCache.getBufferIfLoaded()) {
1891 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1892 if (!Buffer)
1893 return true;
1894 SourceMgr.overrideFileContents(SourceFile: *File, Buffer: std::move(Buffer));
1895 }
1896
1897 break;
1898 }
1899
1900 case SM_SLOC_BUFFER_ENTRY: {
1901 const char *Name = Blob.data();
1902 unsigned Offset = Record[0];
1903 SrcMgr::CharacteristicKind
1904 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1905 SourceLocation IncludeLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1906 if (IncludeLoc.isInvalid() && F->isModule()) {
1907 IncludeLoc = getImportLocation(F);
1908 }
1909
1910 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1911 if (!Buffer)
1912 return true;
1913 FileID FID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter, LoadedID: ID,
1914 LoadedOffset: BaseOffset + Offset, IncludeLoc);
1915 if (Record[3]) {
1916 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1917 FileInfo.setHasLineDirectives();
1918 }
1919 break;
1920 }
1921
1922 case SM_SLOC_EXPANSION_ENTRY: {
1923 SourceLocation SpellingLoc = ReadSourceLocation(MF&: *F, Raw: Record[1]);
1924 SourceLocation ExpansionBegin = ReadSourceLocation(MF&: *F, Raw: Record[2]);
1925 SourceLocation ExpansionEnd = ReadSourceLocation(MF&: *F, Raw: Record[3]);
1926 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionLocStart: ExpansionBegin, ExpansionLocEnd: ExpansionEnd,
1927 Length: Record[5], ExpansionIsTokenRange: Record[4], LoadedID: ID,
1928 LoadedOffset: BaseOffset + Record[0]);
1929 break;
1930 }
1931 }
1932
1933 return false;
1934}
1935
1936std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1937 if (ID == 0)
1938 return std::make_pair(x: SourceLocation(), y: "");
1939
1940 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1941 Error(Msg: "source location entry ID out-of-range for AST file");
1942 return std::make_pair(x: SourceLocation(), y: "");
1943 }
1944
1945 // Find which module file this entry lands in.
1946 ModuleFile *M = GlobalSLocEntryMap.find(K: -ID)->second;
1947 if (!M->isModule())
1948 return std::make_pair(x: SourceLocation(), y: "");
1949
1950 // FIXME: Can we map this down to a particular submodule? That would be
1951 // ideal.
1952 return std::make_pair(x&: M->ImportLoc, y: StringRef(M->ModuleName));
1953}
1954
1955/// Find the location where the module F is imported.
1956SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1957 if (F->ImportLoc.isValid())
1958 return F->ImportLoc;
1959
1960 // Otherwise we have a PCH. It's considered to be "imported" at the first
1961 // location of its includer.
1962 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1963 // Main file is the importer.
1964 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1965 return SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
1966 }
1967 return F->ImportedBy[0]->FirstLoc;
1968}
1969
1970/// Enter a subblock of the specified BlockID with the specified cursor. Read
1971/// the abbreviations that are at the top of the block and then leave the cursor
1972/// pointing into the block.
1973llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1974 unsigned BlockID,
1975 uint64_t *StartOfBlockOffset) {
1976 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1977 return Err;
1978
1979 if (StartOfBlockOffset)
1980 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1981
1982 while (true) {
1983 uint64_t Offset = Cursor.GetCurrentBitNo();
1984 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1985 if (!MaybeCode)
1986 return MaybeCode.takeError();
1987 unsigned Code = MaybeCode.get();
1988
1989 // We expect all abbrevs to be at the start of the block.
1990 if (Code != llvm::bitc::DEFINE_ABBREV) {
1991 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Offset))
1992 return Err;
1993 return llvm::Error::success();
1994 }
1995 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1996 return Err;
1997 }
1998}
1999
2000Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,
2001 unsigned &Idx) {
2002 Token Tok;
2003 Tok.startToken();
2004 Tok.setLocation(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2005 Tok.setKind((tok::TokenKind)Record[Idx++]);
2006 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
2007
2008 if (Tok.isAnnotation()) {
2009 Tok.setAnnotationEndLoc(ReadSourceLocation(ModuleFile&: M, Record, Idx));
2010 switch (Tok.getKind()) {
2011 case tok::annot_pragma_loop_hint: {
2012 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2013 Info->PragmaName = ReadToken(M, Record, Idx);
2014 Info->Option = ReadToken(M, Record, Idx);
2015 unsigned NumTokens = Record[Idx++];
2016 SmallVector<Token, 4> Toks;
2017 Toks.reserve(N: NumTokens);
2018 for (unsigned I = 0; I < NumTokens; ++I)
2019 Toks.push_back(Elt: ReadToken(M, Record, Idx));
2020 Info->Toks = llvm::ArrayRef(Toks).copy(A&: PP.getPreprocessorAllocator());
2021 Tok.setAnnotationValue(static_cast<void *>(Info));
2022 break;
2023 }
2024 case tok::annot_pragma_pack: {
2025 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
2026 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
2027 auto SlotLabel = ReadString(Record, Idx);
2028 Info->SlotLabel =
2029 llvm::StringRef(SlotLabel).copy(A&: PP.getPreprocessorAllocator());
2030 Info->Alignment = ReadToken(M, Record, Idx);
2031 Tok.setAnnotationValue(static_cast<void *>(Info));
2032 break;
2033 }
2034 // Some annotation tokens do not use the PtrData field.
2035 case tok::annot_pragma_openmp:
2036 case tok::annot_pragma_openmp_end:
2037 case tok::annot_pragma_unused:
2038 case tok::annot_pragma_openacc:
2039 case tok::annot_pragma_openacc_end:
2040 case tok::annot_repl_input_end:
2041 break;
2042 default:
2043 llvm_unreachable("missing deserialization code for annotation token");
2044 }
2045 } else {
2046 Tok.setLength(Record[Idx++]);
2047 if (IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[Idx++]))
2048 Tok.setIdentifierInfo(II);
2049 }
2050 return Tok;
2051}
2052
2053MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
2054 BitstreamCursor &Stream = F.MacroCursor;
2055
2056 // Keep track of where we are in the stream, then jump back there
2057 // after reading this macro.
2058 SavedStreamPosition SavedPosition(Stream);
2059
2060 if (llvm::Error Err = Stream.JumpToBit(BitNo: Offset)) {
2061 // FIXME this drops errors on the floor.
2062 consumeError(Err: std::move(Err));
2063 return nullptr;
2064 }
2065 RecordData Record;
2066 SmallVector<IdentifierInfo*, 16> MacroParams;
2067 MacroInfo *Macro = nullptr;
2068 llvm::MutableArrayRef<Token> MacroTokens;
2069
2070 while (true) {
2071 // Advance to the next record, but if we get to the end of the block, don't
2072 // pop it (removing all the abbreviations from the cursor) since we want to
2073 // be able to reseek within the block and read entries.
2074 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
2075 Expected<llvm::BitstreamEntry> MaybeEntry =
2076 Stream.advanceSkippingSubblocks(Flags);
2077 if (!MaybeEntry) {
2078 Error(Err: MaybeEntry.takeError());
2079 return Macro;
2080 }
2081 llvm::BitstreamEntry Entry = MaybeEntry.get();
2082
2083 switch (Entry.Kind) {
2084 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2085 case llvm::BitstreamEntry::Error:
2086 Error(Msg: "malformed block record in AST file");
2087 return Macro;
2088 case llvm::BitstreamEntry::EndBlock:
2089 return Macro;
2090 case llvm::BitstreamEntry::Record:
2091 // The interesting case.
2092 break;
2093 }
2094
2095 // Read a record.
2096 Record.clear();
2097 PreprocessorRecordTypes RecType;
2098 if (Expected<unsigned> MaybeRecType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2099 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
2100 else {
2101 Error(Err: MaybeRecType.takeError());
2102 return Macro;
2103 }
2104 switch (RecType) {
2105 case PP_MODULE_MACRO:
2106 case PP_MACRO_DIRECTIVE_HISTORY:
2107 return Macro;
2108
2109 case PP_MACRO_OBJECT_LIKE:
2110 case PP_MACRO_FUNCTION_LIKE: {
2111 // If we already have a macro, that means that we've hit the end
2112 // of the definition of the macro we were looking for. We're
2113 // done.
2114 if (Macro)
2115 return Macro;
2116
2117 unsigned NextIndex = 1; // Skip identifier ID.
2118 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex);
2119 MacroInfo *MI = PP.AllocateMacroInfo(L: Loc);
2120 MI->setDefinitionEndLoc(ReadSourceLocation(ModuleFile&: F, Record, Idx&: NextIndex));
2121 MI->setIsUsed(Record[NextIndex++]);
2122 MI->setUsedForHeaderGuard(Record[NextIndex++]);
2123 MacroTokens = MI->allocateTokens(NumTokens: Record[NextIndex++],
2124 PPAllocator&: PP.getPreprocessorAllocator());
2125 if (RecType == PP_MACRO_FUNCTION_LIKE) {
2126 // Decode function-like macro info.
2127 bool isC99VarArgs = Record[NextIndex++];
2128 bool isGNUVarArgs = Record[NextIndex++];
2129 bool hasCommaPasting = Record[NextIndex++];
2130 MacroParams.clear();
2131 unsigned NumArgs = Record[NextIndex++];
2132 for (unsigned i = 0; i != NumArgs; ++i)
2133 MacroParams.push_back(Elt: getLocalIdentifier(M&: F, LocalID: Record[NextIndex++]));
2134
2135 // Install function-like macro info.
2136 MI->setIsFunctionLike();
2137 if (isC99VarArgs) MI->setIsC99Varargs();
2138 if (isGNUVarArgs) MI->setIsGNUVarargs();
2139 if (hasCommaPasting) MI->setHasCommaPasting();
2140 MI->setParameterList(List: MacroParams, PPAllocator&: PP.getPreprocessorAllocator());
2141 }
2142
2143 // Remember that we saw this macro last so that we add the tokens that
2144 // form its body to it.
2145 Macro = MI;
2146
2147 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
2148 Record[NextIndex]) {
2149 // We have a macro definition. Register the association
2150 PreprocessedEntityID
2151 GlobalID = getGlobalPreprocessedEntityID(M&: F, LocalID: Record[NextIndex]);
2152 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2153 PreprocessingRecord::PPEntityID PPID =
2154 PPRec.getPPEntityID(Index: GlobalID - 1, /*isLoaded=*/true);
2155 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2156 Val: PPRec.getPreprocessedEntity(PPID));
2157 if (PPDef)
2158 PPRec.RegisterMacroDefinition(Macro, Def: PPDef);
2159 }
2160
2161 ++NumMacrosRead;
2162 break;
2163 }
2164
2165 case PP_TOKEN: {
2166 // If we see a TOKEN before a PP_MACRO_*, then the file is
2167 // erroneous, just pretend we didn't see this.
2168 if (!Macro) break;
2169 if (MacroTokens.empty()) {
2170 Error(Msg: "unexpected number of macro tokens for a macro in AST file");
2171 return Macro;
2172 }
2173
2174 unsigned Idx = 0;
2175 MacroTokens[0] = ReadToken(M&: F, Record, Idx);
2176 MacroTokens = MacroTokens.drop_front();
2177 break;
2178 }
2179 }
2180 }
2181}
2182
2183PreprocessedEntityID
2184ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
2185 unsigned LocalID) const {
2186 if (!M.ModuleOffsetMap.empty())
2187 ReadModuleOffsetMap(F&: M);
2188
2189 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
2190 I = M.PreprocessedEntityRemap.find(K: LocalID - NUM_PREDEF_PP_ENTITY_IDS);
2191 assert(I != M.PreprocessedEntityRemap.end()
2192 && "Invalid index into preprocessed entity index remap");
2193
2194 return LocalID + I->second;
2195}
2196
2197OptionalFileEntryRef
2198HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2199 FileManager &FileMgr = Reader.getFileManager();
2200 if (!Key.Imported)
2201 return FileMgr.getOptionalFileRef(Filename: Key.Filename);
2202
2203 auto Resolved =
2204 ASTReader::ResolveImportedPath(Buf&: Reader.getPathBuf(), Path: Key.Filename, ModF&: M);
2205 return FileMgr.getOptionalFileRef(Filename: *Resolved);
2206}
2207
2208unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2209 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2210 memcpy(dest: buf, src: &ikey.Size, n: sizeof(ikey.Size));
2211 memcpy(dest: buf + sizeof(ikey.Size), src: &ikey.ModTime, n: sizeof(ikey.ModTime));
2212 return llvm::xxh3_64bits(data: buf);
2213}
2214
2215HeaderFileInfoTrait::internal_key_type
2216HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2217 internal_key_type ikey = {.Size: ekey.getSize(),
2218 .ModTime: M.HasTimestamps ? ekey.getModificationTime() : 0,
2219 .Filename: ekey.getName(), /*Imported*/ false};
2220 return ikey;
2221}
2222
2223bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2224 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2225 return false;
2226
2227 if (llvm::sys::path::is_absolute(path: a.Filename) && a.Filename == b.Filename)
2228 return true;
2229
2230 // Determine whether the actual files are equivalent.
2231 OptionalFileEntryRef FEA = getFile(Key: a);
2232 OptionalFileEntryRef FEB = getFile(Key: b);
2233 return FEA && FEA == FEB;
2234}
2235
2236std::pair<unsigned, unsigned>
2237HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2238 return readULEBKeyDataLength(P&: d);
2239}
2240
2241HeaderFileInfoTrait::internal_key_type
2242HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2243 using namespace llvm::support;
2244
2245 internal_key_type ikey;
2246 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2247 ikey.ModTime =
2248 time_t(endian::readNext<uint64_t, llvm::endianness::little>(memory&: d));
2249 ikey.Filename = (const char *)d;
2250 ikey.Imported = true;
2251 return ikey;
2252}
2253
2254HeaderFileInfoTrait::data_type
2255HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2256 unsigned DataLen) {
2257 using namespace llvm::support;
2258
2259 const unsigned char *End = d + DataLen;
2260 HeaderFileInfo HFI;
2261 unsigned Flags = *d++;
2262
2263 OptionalFileEntryRef FE;
2264 bool Included = (Flags >> 6) & 0x01;
2265 if (Included)
2266 if ((FE = getFile(Key: key)))
2267 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2268 // deserialize this header file info again.
2269 Reader.getPreprocessor().getIncludedFiles().insert(V: *FE);
2270
2271 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2272 HFI.isImport |= (Flags >> 5) & 0x01;
2273 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2274 HFI.DirInfo = (Flags >> 1) & 0x07;
2275 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2276 M, LocalID: endian::readNext<IdentifierID, llvm::endianness::little>(memory&: d));
2277
2278 assert((End - d) % 4 == 0 &&
2279 "Wrong data length in HeaderFileInfo deserialization");
2280 while (d != End) {
2281 uint32_t LocalSMID =
2282 endian::readNext<uint32_t, llvm::endianness::little>(memory&: d);
2283 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2284 LocalSMID >>= 3;
2285
2286 // This header is part of a module. Associate it with the module to enable
2287 // implicit module import.
2288 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalID: LocalSMID);
2289 Module *Mod = Reader.getSubmodule(GlobalID: GlobalSMID);
2290 ModuleMap &ModMap =
2291 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2292
2293 if (FE || (FE = getFile(Key: key))) {
2294 // FIXME: NameAsWritten
2295 Module::Header H = {.NameAsWritten: std::string(key.Filename), .PathRelativeToRootModuleDirectory: "", .Entry: *FE};
2296 ModMap.addHeader(Mod, Header: H, Role: HeaderRole, /*Imported=*/true);
2297 }
2298 HFI.mergeModuleMembership(Role: HeaderRole);
2299 }
2300
2301 // This HeaderFileInfo was externally loaded.
2302 HFI.External = true;
2303 HFI.IsValid = true;
2304 return HFI;
2305}
2306
2307void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
2308 uint32_t MacroDirectivesOffset) {
2309 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2310 PendingMacroIDs[II].push_back(Elt: PendingMacroInfo(M, MacroDirectivesOffset));
2311}
2312
2313void ASTReader::ReadDefinedMacros() {
2314 // Note that we are loading defined macros.
2315 Deserializing Macros(this);
2316
2317 for (ModuleFile &I : llvm::reverse(C&: ModuleMgr)) {
2318 BitstreamCursor &MacroCursor = I.MacroCursor;
2319
2320 // If there was no preprocessor block, skip this file.
2321 if (MacroCursor.getBitcodeBytes().empty())
2322 continue;
2323
2324 BitstreamCursor Cursor = MacroCursor;
2325 if (llvm::Error Err = Cursor.JumpToBit(BitNo: I.MacroStartOffset)) {
2326 Error(Err: std::move(Err));
2327 return;
2328 }
2329
2330 RecordData Record;
2331 while (true) {
2332 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2333 if (!MaybeE) {
2334 Error(Err: MaybeE.takeError());
2335 return;
2336 }
2337 llvm::BitstreamEntry E = MaybeE.get();
2338
2339 switch (E.Kind) {
2340 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2341 case llvm::BitstreamEntry::Error:
2342 Error(Msg: "malformed block record in AST file");
2343 return;
2344 case llvm::BitstreamEntry::EndBlock:
2345 goto NextCursor;
2346
2347 case llvm::BitstreamEntry::Record: {
2348 Record.clear();
2349 Expected<unsigned> MaybeRecord = Cursor.readRecord(AbbrevID: E.ID, Vals&: Record);
2350 if (!MaybeRecord) {
2351 Error(Err: MaybeRecord.takeError());
2352 return;
2353 }
2354 switch (MaybeRecord.get()) {
2355 default: // Default behavior: ignore.
2356 break;
2357
2358 case PP_MACRO_OBJECT_LIKE:
2359 case PP_MACRO_FUNCTION_LIKE: {
2360 IdentifierInfo *II = getLocalIdentifier(M&: I, LocalID: Record[0]);
2361 if (II->isOutOfDate())
2362 updateOutOfDateIdentifier(II: *II);
2363 break;
2364 }
2365
2366 case PP_TOKEN:
2367 // Ignore tokens.
2368 break;
2369 }
2370 break;
2371 }
2372 }
2373 }
2374 NextCursor: ;
2375 }
2376}
2377
2378namespace {
2379
2380 /// Visitor class used to look up identifirs in an AST file.
2381 class IdentifierLookupVisitor {
2382 StringRef Name;
2383 unsigned NameHash;
2384 unsigned PriorGeneration;
2385 unsigned &NumIdentifierLookups;
2386 unsigned &NumIdentifierLookupHits;
2387 IdentifierInfo *Found = nullptr;
2388
2389 public:
2390 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2391 unsigned &NumIdentifierLookups,
2392 unsigned &NumIdentifierLookupHits)
2393 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(a: Name)),
2394 PriorGeneration(PriorGeneration),
2395 NumIdentifierLookups(NumIdentifierLookups),
2396 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2397
2398 bool operator()(ModuleFile &M) {
2399 // If we've already searched this module file, skip it now.
2400 if (M.Generation <= PriorGeneration)
2401 return true;
2402
2403 ASTIdentifierLookupTable *IdTable
2404 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2405 if (!IdTable)
2406 return false;
2407
2408 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2409 Found);
2410 ++NumIdentifierLookups;
2411 ASTIdentifierLookupTable::iterator Pos =
2412 IdTable->find_hashed(IKey: Name, KeyHash: NameHash, InfoPtr: &Trait);
2413 if (Pos == IdTable->end())
2414 return false;
2415
2416 // Dereferencing the iterator has the effect of building the
2417 // IdentifierInfo node and populating it with the various
2418 // declarations it needs.
2419 ++NumIdentifierLookupHits;
2420 Found = *Pos;
2421 if (Trait.hasMoreInformationInDependencies()) {
2422 // Look for the identifier in extra modules as they contain more info.
2423 return false;
2424 }
2425 return true;
2426 }
2427
2428 // Retrieve the identifier info found within the module
2429 // files.
2430 IdentifierInfo *getIdentifierInfo() const { return Found; }
2431 };
2432
2433} // namespace
2434
2435void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {
2436 // Note that we are loading an identifier.
2437 Deserializing AnIdentifier(this);
2438
2439 unsigned PriorGeneration = 0;
2440 if (getContext().getLangOpts().Modules)
2441 PriorGeneration = IdentifierGeneration[&II];
2442
2443 // If there is a global index, look there first to determine which modules
2444 // provably do not have any results for this identifier.
2445 GlobalModuleIndex::HitSet Hits;
2446 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2447 if (!loadGlobalIndex()) {
2448 if (GlobalIndex->lookupIdentifier(Name: II.getName(), Hits)) {
2449 HitsPtr = &Hits;
2450 }
2451 }
2452
2453 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2454 NumIdentifierLookups,
2455 NumIdentifierLookupHits);
2456 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
2457 markIdentifierUpToDate(II: &II);
2458}
2459
2460void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {
2461 if (!II)
2462 return;
2463
2464 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2465
2466 // Update the generation for this identifier.
2467 if (getContext().getLangOpts().Modules)
2468 IdentifierGeneration[II] = getGeneration();
2469}
2470
2471void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2472 const PendingMacroInfo &PMInfo) {
2473 ModuleFile &M = *PMInfo.M;
2474
2475 BitstreamCursor &Cursor = M.MacroCursor;
2476 SavedStreamPosition SavedPosition(Cursor);
2477 if (llvm::Error Err =
2478 Cursor.JumpToBit(BitNo: M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2479 Error(Err: std::move(Err));
2480 return;
2481 }
2482
2483 struct ModuleMacroRecord {
2484 SubmoduleID SubModID;
2485 MacroInfo *MI;
2486 SmallVector<SubmoduleID, 8> Overrides;
2487 };
2488 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2489
2490 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2491 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2492 // macro histroy.
2493 RecordData Record;
2494 while (true) {
2495 Expected<llvm::BitstreamEntry> MaybeEntry =
2496 Cursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
2497 if (!MaybeEntry) {
2498 Error(Err: MaybeEntry.takeError());
2499 return;
2500 }
2501 llvm::BitstreamEntry Entry = MaybeEntry.get();
2502
2503 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2504 Error(Msg: "malformed block record in AST file");
2505 return;
2506 }
2507
2508 Record.clear();
2509 Expected<unsigned> MaybePP = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2510 if (!MaybePP) {
2511 Error(Err: MaybePP.takeError());
2512 return;
2513 }
2514 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2515 case PP_MACRO_DIRECTIVE_HISTORY:
2516 break;
2517
2518 case PP_MODULE_MACRO: {
2519 ModuleMacros.push_back(Elt: ModuleMacroRecord());
2520 auto &Info = ModuleMacros.back();
2521 Info.SubModID = getGlobalSubmoduleID(M, LocalID: Record[0]);
2522 Info.MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[1]));
2523 for (int I = 2, N = Record.size(); I != N; ++I)
2524 Info.Overrides.push_back(Elt: getGlobalSubmoduleID(M, LocalID: Record[I]));
2525 continue;
2526 }
2527
2528 default:
2529 Error(Msg: "malformed block record in AST file");
2530 return;
2531 }
2532
2533 // We found the macro directive history; that's the last record
2534 // for this macro.
2535 break;
2536 }
2537
2538 // Module macros are listed in reverse dependency order.
2539 {
2540 std::reverse(first: ModuleMacros.begin(), last: ModuleMacros.end());
2541 llvm::SmallVector<ModuleMacro*, 8> Overrides;
2542 for (auto &MMR : ModuleMacros) {
2543 Overrides.clear();
2544 for (unsigned ModID : MMR.Overrides) {
2545 Module *Mod = getSubmodule(GlobalID: ModID);
2546 auto *Macro = PP.getModuleMacro(Mod, II);
2547 assert(Macro && "missing definition for overridden macro");
2548 Overrides.push_back(Elt: Macro);
2549 }
2550
2551 bool Inserted = false;
2552 Module *Owner = getSubmodule(GlobalID: MMR.SubModID);
2553 PP.addModuleMacro(Mod: Owner, II, Macro: MMR.MI, Overrides, IsNew&: Inserted);
2554 }
2555 }
2556
2557 // Don't read the directive history for a module; we don't have anywhere
2558 // to put it.
2559 if (M.isModule())
2560 return;
2561
2562 // Deserialize the macro directives history in reverse source-order.
2563 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2564 unsigned Idx = 0, N = Record.size();
2565 while (Idx < N) {
2566 MacroDirective *MD = nullptr;
2567 SourceLocation Loc = ReadSourceLocation(ModuleFile&: M, Record, Idx);
2568 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2569 switch (K) {
2570 case MacroDirective::MD_Define: {
2571 MacroInfo *MI = getMacro(ID: getGlobalMacroID(M, LocalID: Record[Idx++]));
2572 MD = PP.AllocateDefMacroDirective(MI, Loc);
2573 break;
2574 }
2575 case MacroDirective::MD_Undefine:
2576 MD = PP.AllocateUndefMacroDirective(UndefLoc: Loc);
2577 break;
2578 case MacroDirective::MD_Visibility:
2579 bool isPublic = Record[Idx++];
2580 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2581 break;
2582 }
2583
2584 if (!Latest)
2585 Latest = MD;
2586 if (Earliest)
2587 Earliest->setPrevious(MD);
2588 Earliest = MD;
2589 }
2590
2591 if (Latest)
2592 PP.setLoadedMacroDirective(II, ED: Earliest, MD: Latest);
2593}
2594
2595bool ASTReader::shouldDisableValidationForFile(
2596 const serialization::ModuleFile &M) const {
2597 if (DisableValidationKind == DisableValidationForModuleKind::None)
2598 return false;
2599
2600 // If a PCH is loaded and validation is disabled for PCH then disable
2601 // validation for the PCH and the modules it loads.
2602 ModuleKind K = CurrentDeserializingModuleKind.value_or(u: M.Kind);
2603
2604 switch (K) {
2605 case MK_MainFile:
2606 case MK_Preamble:
2607 case MK_PCH:
2608 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2609 case MK_ImplicitModule:
2610 case MK_ExplicitModule:
2611 case MK_PrebuiltModule:
2612 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2613 }
2614
2615 return false;
2616}
2617
2618static std::pair<StringRef, StringRef>
2619getUnresolvedInputFilenames(const ASTReader::RecordData &Record,
2620 const StringRef InputBlob) {
2621 uint16_t AsRequestedLength = Record[7];
2622 return {InputBlob.substr(Start: 0, N: AsRequestedLength),
2623 InputBlob.substr(Start: AsRequestedLength)};
2624}
2625
2626InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2627 // If this ID is bogus, just return an empty input file.
2628 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2629 return InputFileInfo();
2630
2631 // If we've already loaded this input file, return it.
2632 if (F.InputFileInfosLoaded[ID - 1].isValid())
2633 return F.InputFileInfosLoaded[ID - 1];
2634
2635 // Go find this input file.
2636 BitstreamCursor &Cursor = F.InputFilesCursor;
2637 SavedStreamPosition SavedPosition(Cursor);
2638 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2639 F.InputFileOffsets[ID - 1])) {
2640 // FIXME this drops errors on the floor.
2641 consumeError(Err: std::move(Err));
2642 }
2643
2644 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2645 if (!MaybeCode) {
2646 // FIXME this drops errors on the floor.
2647 consumeError(Err: MaybeCode.takeError());
2648 }
2649 unsigned Code = MaybeCode.get();
2650 RecordData Record;
2651 StringRef Blob;
2652
2653 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob))
2654 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2655 "invalid record type for input file");
2656 else {
2657 // FIXME this drops errors on the floor.
2658 consumeError(Err: Maybe.takeError());
2659 }
2660
2661 assert(Record[0] == ID && "Bogus stored ID or offset");
2662 InputFileInfo R;
2663 R.StoredSize = static_cast<off_t>(Record[1]);
2664 R.StoredTime = static_cast<time_t>(Record[2]);
2665 R.Overridden = static_cast<bool>(Record[3]);
2666 R.Transient = static_cast<bool>(Record[4]);
2667 R.TopLevel = static_cast<bool>(Record[5]);
2668 R.ModuleMap = static_cast<bool>(Record[6]);
2669 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
2670 getUnresolvedInputFilenames(Record, InputBlob: Blob);
2671 R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;
2672 R.UnresolvedImportedFilename = UnresolvedFilename.empty()
2673 ? UnresolvedFilenameAsRequested
2674 : UnresolvedFilename;
2675
2676 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2677 if (!MaybeEntry) // FIXME this drops errors on the floor.
2678 consumeError(Err: MaybeEntry.takeError());
2679 llvm::BitstreamEntry Entry = MaybeEntry.get();
2680 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2681 "expected record type for input file hash");
2682
2683 Record.clear();
2684 if (Expected<unsigned> Maybe = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record))
2685 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2686 "invalid record type for input file hash");
2687 else {
2688 // FIXME this drops errors on the floor.
2689 consumeError(Err: Maybe.takeError());
2690 }
2691 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2692 static_cast<uint64_t>(Record[0]);
2693
2694 // Note that we've loaded this input file info.
2695 F.InputFileInfosLoaded[ID - 1] = R;
2696 return R;
2697}
2698
2699static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2700InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2701 // If this ID is bogus, just return an empty input file.
2702 if (ID == 0 || ID > F.InputFilesLoaded.size())
2703 return InputFile();
2704
2705 // If we've already loaded this input file, return it.
2706 if (F.InputFilesLoaded[ID-1].getFile())
2707 return F.InputFilesLoaded[ID-1];
2708
2709 if (F.InputFilesLoaded[ID-1].isNotFound())
2710 return InputFile();
2711
2712 // Go find this input file.
2713 BitstreamCursor &Cursor = F.InputFilesCursor;
2714 SavedStreamPosition SavedPosition(Cursor);
2715 if (llvm::Error Err = Cursor.JumpToBit(BitNo: F.InputFilesOffsetBase +
2716 F.InputFileOffsets[ID - 1])) {
2717 // FIXME this drops errors on the floor.
2718 consumeError(Err: std::move(Err));
2719 }
2720
2721 InputFileInfo FI = getInputFileInfo(F, ID);
2722 off_t StoredSize = FI.StoredSize;
2723 time_t StoredTime = FI.StoredTime;
2724 bool Overridden = FI.Overridden;
2725 bool Transient = FI.Transient;
2726 auto Filename =
2727 ResolveImportedPath(Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
2728 uint64_t StoredContentHash = FI.ContentHash;
2729
2730 // For standard C++ modules, we don't need to check the inputs.
2731 bool SkipChecks = F.StandardCXXModule;
2732
2733 const HeaderSearchOptions &HSOpts =
2734 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2735
2736 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2737 // modules.
2738 if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {
2739 SkipChecks = false;
2740 Overridden = false;
2741 }
2742
2743 auto File = FileMgr.getOptionalFileRef(Filename: *Filename, /*OpenFile=*/false);
2744
2745 // For an overridden file, create a virtual file with the stored
2746 // size/timestamp.
2747 if ((Overridden || Transient || SkipChecks) && !File)
2748 File = FileMgr.getVirtualFileRef(Filename: *Filename, Size: StoredSize, ModificationTime: StoredTime);
2749
2750 if (!File) {
2751 if (Complain) {
2752 std::string ErrorStr = "could not find file '";
2753 ErrorStr += *Filename;
2754 ErrorStr += "' referenced by AST file '";
2755 ErrorStr += F.FileName;
2756 ErrorStr += "'";
2757 Error(Msg: ErrorStr);
2758 }
2759 // Record that we didn't find the file.
2760 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2761 return InputFile();
2762 }
2763
2764 // Check if there was a request to override the contents of the file
2765 // that was part of the precompiled header. Overriding such a file
2766 // can lead to problems when lexing using the source locations from the
2767 // PCH.
2768 SourceManager &SM = getSourceManager();
2769 // FIXME: Reject if the overrides are different.
2770 if ((!Overridden && !Transient) && !SkipChecks &&
2771 SM.isFileOverridden(File: *File)) {
2772 if (Complain)
2773 Error(DiagID: diag::err_fe_pch_file_overridden, Arg1: *Filename);
2774
2775 // After emitting the diagnostic, bypass the overriding file to recover
2776 // (this creates a separate FileEntry).
2777 File = SM.bypassFileContentsOverride(File: *File);
2778 if (!File) {
2779 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2780 return InputFile();
2781 }
2782 }
2783
2784 struct Change {
2785 enum ModificationKind {
2786 Size,
2787 ModTime,
2788 Content,
2789 None,
2790 } Kind;
2791 std::optional<int64_t> Old = std::nullopt;
2792 std::optional<int64_t> New = std::nullopt;
2793 };
2794 auto HasInputContentChanged = [&](Change OriginalChange) {
2795 assert(ValidateASTInputFilesContent &&
2796 "We should only check the content of the inputs with "
2797 "ValidateASTInputFilesContent enabled.");
2798
2799 if (StoredContentHash == 0)
2800 return OriginalChange;
2801
2802 auto MemBuffOrError = FileMgr.getBufferForFile(Entry: *File);
2803 if (!MemBuffOrError) {
2804 if (!Complain)
2805 return OriginalChange;
2806 std::string ErrorStr = "could not get buffer for file '";
2807 ErrorStr += File->getName();
2808 ErrorStr += "'";
2809 Error(Msg: ErrorStr);
2810 return OriginalChange;
2811 }
2812
2813 auto ContentHash = xxh3_64bits(data: MemBuffOrError.get()->getBuffer());
2814 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2815 return Change{.Kind: Change::None};
2816
2817 return Change{.Kind: Change::Content};
2818 };
2819 auto HasInputFileChanged = [&]() {
2820 if (StoredSize != File->getSize())
2821 return Change{.Kind: Change::Size, .Old: StoredSize, .New: File->getSize()};
2822 if (!shouldDisableValidationForFile(M: F) && StoredTime &&
2823 StoredTime != File->getModificationTime()) {
2824 Change MTimeChange = {.Kind: Change::ModTime, .Old: StoredTime,
2825 .New: File->getModificationTime()};
2826
2827 // In case the modification time changes but not the content,
2828 // accept the cached file as legit.
2829 if (ValidateASTInputFilesContent)
2830 return HasInputContentChanged(MTimeChange);
2831
2832 return MTimeChange;
2833 }
2834 return Change{.Kind: Change::None};
2835 };
2836
2837 bool IsOutOfDate = false;
2838 auto FileChange = SkipChecks ? Change{.Kind: Change::None} : HasInputFileChanged();
2839 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2840 // enabled, it is better to check the contents of the inputs. Since we can't
2841 // get correct modified time information for inputs from overriden inputs.
2842 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2843 F.StandardCXXModule && FileChange.Kind == Change::None)
2844 FileChange = HasInputContentChanged(FileChange);
2845
2846 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2847 // it is better to check the content of the input files because we cannot rely
2848 // on the file modification time, which will be the same (zero) for these
2849 // files.
2850 if (!StoredTime && ValidateASTInputFilesContent &&
2851 FileChange.Kind == Change::None)
2852 FileChange = HasInputContentChanged(FileChange);
2853
2854 // For an overridden file, there is nothing to validate.
2855 if (!Overridden && FileChange.Kind != Change::None) {
2856 if (Complain) {
2857 // Build a list of the PCH imports that got us here (in reverse).
2858 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2859 while (!ImportStack.back()->ImportedBy.empty())
2860 ImportStack.push_back(Elt: ImportStack.back()->ImportedBy[0]);
2861
2862 // The top-level AST file is stale.
2863 StringRef TopLevelASTFileName(ImportStack.back()->FileName);
2864 Diag(DiagID: diag::err_fe_ast_file_modified)
2865 << *Filename << moduleKindForDiagnostic(Kind: ImportStack.back()->Kind)
2866 << TopLevelASTFileName << FileChange.Kind
2867 << (FileChange.Old && FileChange.New)
2868 << llvm::itostr(X: FileChange.Old.value_or(u: 0))
2869 << llvm::itostr(X: FileChange.New.value_or(u: 0));
2870
2871 // Print the import stack.
2872 if (ImportStack.size() > 1) {
2873 Diag(DiagID: diag::note_ast_file_required_by)
2874 << *Filename << ImportStack[0]->FileName;
2875 for (unsigned I = 1; I < ImportStack.size(); ++I)
2876 Diag(DiagID: diag::note_ast_file_required_by)
2877 << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;
2878 }
2879
2880 Diag(DiagID: diag::note_ast_file_rebuild_required) << TopLevelASTFileName;
2881 }
2882
2883 IsOutOfDate = true;
2884 }
2885 // FIXME: If the file is overridden and we've already opened it,
2886 // issue an error (or split it into a separate FileEntry).
2887
2888 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2889
2890 // Note that we've loaded this input file.
2891 F.InputFilesLoaded[ID-1] = IF;
2892 return IF;
2893}
2894
2895ASTReader::TemporarilyOwnedStringRef
2896ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
2897 ModuleFile &ModF) {
2898 return ResolveImportedPath(Buf, Path, Prefix: ModF.BaseDirectory);
2899}
2900
2901ASTReader::TemporarilyOwnedStringRef
2902ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,
2903 StringRef Prefix) {
2904 assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");
2905
2906 if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(path: Path) ||
2907 Path == "<built-in>" || Path == "<command line>")
2908 return {Path, Buf};
2909
2910 Buf.clear();
2911 llvm::sys::path::append(path&: Buf, a: Prefix, b: Path);
2912 StringRef ResolvedPath{Buf.data(), Buf.size()};
2913 return {ResolvedPath, Buf};
2914}
2915
2916std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
2917 StringRef P,
2918 ModuleFile &ModF) {
2919 return ResolveImportedPathAndAllocate(Buf, Path: P, Prefix: ModF.BaseDirectory);
2920}
2921
2922std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,
2923 StringRef P,
2924 StringRef Prefix) {
2925 auto ResolvedPath = ResolveImportedPath(Buf, Path: P, Prefix);
2926 return ResolvedPath->str();
2927}
2928
2929static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2930 switch (ARR) {
2931 case ASTReader::Failure: return true;
2932 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2933 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2934 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2935 case ASTReader::ConfigurationMismatch:
2936 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2937 case ASTReader::HadErrors: return true;
2938 case ASTReader::Success: return false;
2939 }
2940
2941 llvm_unreachable("unknown ASTReadResult");
2942}
2943
2944ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2945 BitstreamCursor &Stream, StringRef Filename,
2946 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
2947 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
2948 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: OPTIONS_BLOCK_ID)) {
2949 // FIXME this drops errors on the floor.
2950 consumeError(Err: std::move(Err));
2951 return Failure;
2952 }
2953
2954 // Read all of the records in the options block.
2955 RecordData Record;
2956 ASTReadResult Result = Success;
2957 while (true) {
2958 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2959 if (!MaybeEntry) {
2960 // FIXME this drops errors on the floor.
2961 consumeError(Err: MaybeEntry.takeError());
2962 return Failure;
2963 }
2964 llvm::BitstreamEntry Entry = MaybeEntry.get();
2965
2966 switch (Entry.Kind) {
2967 case llvm::BitstreamEntry::Error:
2968 case llvm::BitstreamEntry::SubBlock:
2969 return Failure;
2970
2971 case llvm::BitstreamEntry::EndBlock:
2972 return Result;
2973
2974 case llvm::BitstreamEntry::Record:
2975 // The interesting case.
2976 break;
2977 }
2978
2979 // Read and process a record.
2980 Record.clear();
2981 Expected<unsigned> MaybeRecordType = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record);
2982 if (!MaybeRecordType) {
2983 // FIXME this drops errors on the floor.
2984 consumeError(Err: MaybeRecordType.takeError());
2985 return Failure;
2986 }
2987 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2988 case LANGUAGE_OPTIONS: {
2989 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2990 if (ParseLanguageOptions(Record, ModuleFilename: Filename, Complain, Listener,
2991 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
2992 Result = ConfigurationMismatch;
2993 break;
2994 }
2995
2996 case TARGET_OPTIONS: {
2997 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2998 if (ParseTargetOptions(Record, ModuleFilename: Filename, Complain, Listener,
2999 AllowCompatibleDifferences: AllowCompatibleConfigurationMismatch))
3000 Result = ConfigurationMismatch;
3001 break;
3002 }
3003
3004 case FILE_SYSTEM_OPTIONS: {
3005 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3006 if (!AllowCompatibleConfigurationMismatch &&
3007 ParseFileSystemOptions(Record, Complain, Listener))
3008 Result = ConfigurationMismatch;
3009 break;
3010 }
3011
3012 case HEADER_SEARCH_OPTIONS: {
3013 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3014 if (!AllowCompatibleConfigurationMismatch &&
3015 ParseHeaderSearchOptions(Record, ModuleFilename: Filename, Complain, Listener))
3016 Result = ConfigurationMismatch;
3017 break;
3018 }
3019
3020 case PREPROCESSOR_OPTIONS:
3021 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
3022 if (!AllowCompatibleConfigurationMismatch &&
3023 ParsePreprocessorOptions(Record, ModuleFilename: Filename, Complain, Listener,
3024 SuggestedPredefines))
3025 Result = ConfigurationMismatch;
3026 break;
3027 }
3028 }
3029}
3030
3031ASTReader::ASTReadResult
3032ASTReader::ReadControlBlock(ModuleFile &F,
3033 SmallVectorImpl<ImportedModule> &Loaded,
3034 const ModuleFile *ImportedBy,
3035 unsigned ClientLoadCapabilities) {
3036 BitstreamCursor &Stream = F.Stream;
3037
3038 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: CONTROL_BLOCK_ID)) {
3039 Error(Err: std::move(Err));
3040 return Failure;
3041 }
3042
3043 // Lambda to read the unhashed control block the first time it's called.
3044 //
3045 // For PCM files, the unhashed control block cannot be read until after the
3046 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
3047 // need to look ahead before reading the IMPORTS record. For consistency,
3048 // this block is always read somehow (see BitstreamEntry::EndBlock).
3049 bool HasReadUnhashedControlBlock = false;
3050 auto readUnhashedControlBlockOnce = [&]() {
3051 if (!HasReadUnhashedControlBlock) {
3052 HasReadUnhashedControlBlock = true;
3053 if (ASTReadResult Result =
3054 readUnhashedControlBlock(F, WasImportedBy: ImportedBy, ClientLoadCapabilities))
3055 return Result;
3056 }
3057 return Success;
3058 };
3059
3060 bool DisableValidation = shouldDisableValidationForFile(M: F);
3061
3062 // Read all of the records and blocks in the control block.
3063 RecordData Record;
3064 unsigned NumInputs = 0;
3065 unsigned NumUserInputs = 0;
3066 StringRef BaseDirectoryAsWritten;
3067 while (true) {
3068 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3069 if (!MaybeEntry) {
3070 Error(Err: MaybeEntry.takeError());
3071 return Failure;
3072 }
3073 llvm::BitstreamEntry Entry = MaybeEntry.get();
3074
3075 switch (Entry.Kind) {
3076 case llvm::BitstreamEntry::Error:
3077 Error(Msg: "malformed block record in AST file");
3078 return Failure;
3079 case llvm::BitstreamEntry::EndBlock: {
3080 // Validate the module before returning. This call catches an AST with
3081 // no module name and no imports.
3082 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3083 return Result;
3084
3085 // Validate input files.
3086 const HeaderSearchOptions &HSOpts =
3087 PP.getHeaderSearchInfo().getHeaderSearchOpts();
3088
3089 // All user input files reside at the index range [0, NumUserInputs), and
3090 // system input files reside at [NumUserInputs, NumInputs). For explicitly
3091 // loaded module files, ignore missing inputs.
3092 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
3093 F.Kind != MK_PrebuiltModule) {
3094 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
3095
3096 // If we are reading a module, we will create a verification timestamp,
3097 // so we verify all input files. Otherwise, verify only user input
3098 // files.
3099
3100 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3101 if (HSOpts.ModulesValidateOncePerBuildSession &&
3102 F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3103 F.Kind == MK_ImplicitModule)
3104 N = ForceValidateUserInputs ? NumUserInputs : 0;
3105
3106 if (N != 0)
3107 Diag(DiagID: diag::remark_module_validation)
3108 << N << F.ModuleName << F.FileName;
3109
3110 for (unsigned I = 0; I < N; ++I) {
3111 InputFile IF = getInputFile(F, ID: I+1, Complain);
3112 if (!IF.getFile() || IF.isOutOfDate())
3113 return OutOfDate;
3114 }
3115 }
3116
3117 if (Listener)
3118 Listener->visitModuleFile(Filename: F.FileName, Kind: F.Kind);
3119
3120 if (Listener && Listener->needsInputFileVisitation()) {
3121 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
3122 : NumUserInputs;
3123 for (unsigned I = 0; I < N; ++I) {
3124 bool IsSystem = I >= NumUserInputs;
3125 InputFileInfo FI = getInputFileInfo(F, ID: I + 1);
3126 auto FilenameAsRequested = ResolveImportedPath(
3127 Buf&: PathBuf, Path: FI.UnresolvedImportedFilenameAsRequested, ModF&: F);
3128 Listener->visitInputFile(
3129 Filename: *FilenameAsRequested, isSystem: IsSystem, isOverridden: FI.Overridden,
3130 isExplicitModule: F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
3131 }
3132 }
3133
3134 return Success;
3135 }
3136
3137 case llvm::BitstreamEntry::SubBlock:
3138 switch (Entry.ID) {
3139 case INPUT_FILES_BLOCK_ID:
3140 F.InputFilesCursor = Stream;
3141 if (llvm::Error Err = Stream.SkipBlock()) {
3142 Error(Err: std::move(Err));
3143 return Failure;
3144 }
3145 if (ReadBlockAbbrevs(Cursor&: F.InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID)) {
3146 Error(Msg: "malformed block record in AST file");
3147 return Failure;
3148 }
3149 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
3150 continue;
3151
3152 case OPTIONS_BLOCK_ID:
3153 // If we're reading the first module for this group, check its options
3154 // are compatible with ours. For modules it imports, no further checking
3155 // is required, because we checked them when we built it.
3156 if (Listener && !ImportedBy) {
3157 // Should we allow the configuration of the module file to differ from
3158 // the configuration of the current translation unit in a compatible
3159 // way?
3160 //
3161 // FIXME: Allow this for files explicitly specified with -include-pch.
3162 bool AllowCompatibleConfigurationMismatch =
3163 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
3164
3165 ASTReadResult Result =
3166 ReadOptionsBlock(Stream, Filename: F.FileName, ClientLoadCapabilities,
3167 AllowCompatibleConfigurationMismatch, Listener&: *Listener,
3168 SuggestedPredefines);
3169 if (Result == Failure) {
3170 Error(Msg: "malformed block record in AST file");
3171 return Result;
3172 }
3173
3174 if (DisableValidation ||
3175 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3176 Result = Success;
3177
3178 // If we can't load the module, exit early since we likely
3179 // will rebuild the module anyway. The stream may be in the
3180 // middle of a block.
3181 if (Result != Success)
3182 return Result;
3183 } else if (llvm::Error Err = Stream.SkipBlock()) {
3184 Error(Err: std::move(Err));
3185 return Failure;
3186 }
3187 continue;
3188
3189 default:
3190 if (llvm::Error Err = Stream.SkipBlock()) {
3191 Error(Err: std::move(Err));
3192 return Failure;
3193 }
3194 continue;
3195 }
3196
3197 case llvm::BitstreamEntry::Record:
3198 // The interesting case.
3199 break;
3200 }
3201
3202 // Read and process a record.
3203 Record.clear();
3204 StringRef Blob;
3205 Expected<unsigned> MaybeRecordType =
3206 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3207 if (!MaybeRecordType) {
3208 Error(Err: MaybeRecordType.takeError());
3209 return Failure;
3210 }
3211 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3212 case METADATA: {
3213 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3214 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3215 Diag(DiagID: Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3216 : diag::err_ast_file_version_too_new)
3217 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3218 return VersionMismatch;
3219 }
3220
3221 bool hasErrors = Record[7];
3222 if (hasErrors && !DisableValidation) {
3223 // If requested by the caller and the module hasn't already been read
3224 // or compiled, mark modules on error as out-of-date.
3225 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3226 canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3227 return OutOfDate;
3228
3229 if (!AllowASTWithCompilerErrors) {
3230 Diag(DiagID: diag::err_ast_file_with_compiler_errors)
3231 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName;
3232 return HadErrors;
3233 }
3234 }
3235 if (hasErrors) {
3236 Diags.ErrorOccurred = true;
3237 Diags.UncompilableErrorOccurred = true;
3238 Diags.UnrecoverableErrorOccurred = true;
3239 }
3240
3241 F.RelocatablePCH = Record[4];
3242 // Relative paths in a relocatable PCH are relative to our sysroot.
3243 if (F.RelocatablePCH)
3244 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3245
3246 F.StandardCXXModule = Record[5];
3247
3248 F.HasTimestamps = Record[6];
3249
3250 const std::string &CurBranch = getClangFullRepositoryVersion();
3251 StringRef ASTBranch = Blob;
3252 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3253 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3254 Diag(DiagID: diag::err_ast_file_different_branch)
3255 << moduleKindForDiagnostic(Kind: F.Kind) << F.FileName << ASTBranch
3256 << CurBranch;
3257 return VersionMismatch;
3258 }
3259 break;
3260 }
3261
3262 case IMPORT: {
3263 // Validate the AST before processing any imports (otherwise, untangling
3264 // them can be error-prone and expensive). A module will have a name and
3265 // will already have been validated, but this catches the PCH case.
3266 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3267 return Result;
3268
3269 unsigned Idx = 0;
3270 // Read information about the AST file.
3271 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3272
3273 // The import location will be the local one for now; we will adjust
3274 // all import locations of module imports after the global source
3275 // location info are setup, in ReadAST.
3276 auto [ImportLoc, ImportModuleFileIndex] =
3277 ReadUntranslatedSourceLocation(Raw: Record[Idx++]);
3278 // The import location must belong to the current module file itself.
3279 assert(ImportModuleFileIndex == 0);
3280
3281 StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);
3282
3283 bool IsImportingStdCXXModule = Record[Idx++];
3284
3285 off_t StoredSize = 0;
3286 time_t StoredModTime = 0;
3287 ASTFileSignature StoredSignature;
3288 std::string ImportedFile;
3289 std::string StoredFile;
3290 bool IgnoreImportedByNote = false;
3291
3292 // For prebuilt and explicit modules first consult the file map for
3293 // an override. Note that here we don't search prebuilt module
3294 // directories if we're not importing standard c++ module, only the
3295 // explicit name to file mappings. Also, we will still verify the
3296 // size/signature making sure it is essentially the same file but
3297 // perhaps in a different location.
3298 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3299 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3300 ModuleName: ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3301
3302 if (IsImportingStdCXXModule && ImportedFile.empty()) {
3303 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3304 return Missing;
3305 }
3306
3307 if (!IsImportingStdCXXModule) {
3308 StoredSize = (off_t)Record[Idx++];
3309 StoredModTime = (time_t)Record[Idx++];
3310
3311 StringRef SignatureBytes = Blob.substr(Start: 0, N: ASTFileSignature::size);
3312 StoredSignature = ASTFileSignature::create(First: SignatureBytes.begin(),
3313 Last: SignatureBytes.end());
3314 Blob = Blob.substr(Start: ASTFileSignature::size);
3315
3316 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3317 // ModuleCache as when writing.
3318 StoredFile = ReadPathBlob(BaseDirectory: BaseDirectoryAsWritten, Record, Idx, Blob);
3319 if (ImportedFile.empty()) {
3320 ImportedFile = StoredFile;
3321 } else if (!getDiags().isIgnored(
3322 DiagID: diag::warn_module_file_mapping_mismatch,
3323 Loc: CurrentImportLoc)) {
3324 auto ImportedFileRef =
3325 PP.getFileManager().getOptionalFileRef(Filename: ImportedFile);
3326 auto StoredFileRef =
3327 PP.getFileManager().getOptionalFileRef(Filename: StoredFile);
3328 if ((ImportedFileRef && StoredFileRef) &&
3329 (*ImportedFileRef != *StoredFileRef)) {
3330 Diag(DiagID: diag::warn_module_file_mapping_mismatch)
3331 << ImportedFile << StoredFile;
3332 Diag(DiagID: diag::note_module_file_imported_by)
3333 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3334 IgnoreImportedByNote = true;
3335 }
3336 }
3337 }
3338
3339 // If our client can't cope with us being out of date, we can't cope with
3340 // our dependency being missing.
3341 unsigned Capabilities = ClientLoadCapabilities;
3342 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3343 Capabilities &= ~ARR_Missing;
3344
3345 // Load the AST file.
3346 auto Result = ReadASTCore(FileName: ImportedFile, Type: ImportedKind, ImportLoc, ImportedBy: &F,
3347 Loaded, ExpectedSize: StoredSize, ExpectedModTime: StoredModTime,
3348 ExpectedSignature: StoredSignature, ClientLoadCapabilities: Capabilities);
3349
3350 // Check the AST we just read from ImportedFile contains a different
3351 // module than we expected (ImportedName). This can occur for C++20
3352 // Modules when given a mismatch via -fmodule-file=<name>=<file>
3353 if (IsImportingStdCXXModule) {
3354 if (const auto *Imported =
3355 getModuleManager().lookupByFileName(FileName: ImportedFile);
3356 Imported != nullptr && Imported->ModuleName != ImportedName) {
3357 Diag(DiagID: diag::err_failed_to_find_module_file) << ImportedName;
3358 Result = Missing;
3359 }
3360 }
3361
3362 // If we diagnosed a problem, produce a backtrace.
3363 bool recompilingFinalized = Result == OutOfDate &&
3364 (Capabilities & ARR_OutOfDate) &&
3365 getModuleManager()
3366 .getModuleCache()
3367 .getInMemoryModuleCache()
3368 .isPCMFinal(Filename: F.FileName);
3369 if (!IgnoreImportedByNote &&
3370 (isDiagnosedResult(ARR: Result, Caps: Capabilities) || recompilingFinalized))
3371 Diag(DiagID: diag::note_module_file_imported_by)
3372 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3373
3374 switch (Result) {
3375 case Failure: return Failure;
3376 // If we have to ignore the dependency, we'll have to ignore this too.
3377 case Missing:
3378 case OutOfDate: return OutOfDate;
3379 case VersionMismatch: return VersionMismatch;
3380 case ConfigurationMismatch: return ConfigurationMismatch;
3381 case HadErrors: return HadErrors;
3382 case Success: break;
3383 }
3384 break;
3385 }
3386
3387 case ORIGINAL_FILE:
3388 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3389 F.ActualOriginalSourceFileName = std::string(Blob);
3390 F.OriginalSourceFileName = ResolveImportedPathAndAllocate(
3391 Buf&: PathBuf, P: F.ActualOriginalSourceFileName, ModF&: F);
3392 break;
3393
3394 case ORIGINAL_FILE_ID:
3395 F.OriginalSourceFileID = FileID::get(V: Record[0]);
3396 break;
3397
3398 case MODULE_NAME:
3399 F.ModuleName = std::string(Blob);
3400 Diag(DiagID: diag::remark_module_import)
3401 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3402 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3403 if (Listener)
3404 Listener->ReadModuleName(ModuleName: F.ModuleName);
3405
3406 // Validate the AST as soon as we have a name so we can exit early on
3407 // failure.
3408 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3409 return Result;
3410
3411 break;
3412
3413 case MODULE_DIRECTORY: {
3414 // Save the BaseDirectory as written in the PCM for computing the module
3415 // filename for the ModuleCache.
3416 BaseDirectoryAsWritten = Blob;
3417 assert(!F.ModuleName.empty() &&
3418 "MODULE_DIRECTORY found before MODULE_NAME");
3419 F.BaseDirectory = std::string(Blob);
3420 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3421 break;
3422 // If we've already loaded a module map file covering this module, we may
3423 // have a better path for it (relative to the current build).
3424 Module *M = PP.getHeaderSearchInfo().lookupModule(
3425 ModuleName: F.ModuleName, ImportLoc: SourceLocation(), /*AllowSearch*/ true,
3426 /*AllowExtraModuleMapSearch*/ true);
3427 if (M && M->Directory) {
3428 // If we're implicitly loading a module, the base directory can't
3429 // change between the build and use.
3430 // Don't emit module relocation error if we have -fno-validate-pch
3431 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3432 DisableValidationForModuleKind::Module) &&
3433 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
3434 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(DirName: Blob);
3435 if (!BuildDir || *BuildDir != M->Directory) {
3436 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
3437 Diag(DiagID: diag::err_imported_module_relocated)
3438 << F.ModuleName << Blob << M->Directory->getName();
3439 return OutOfDate;
3440 }
3441 }
3442 F.BaseDirectory = std::string(M->Directory->getName());
3443 }
3444 break;
3445 }
3446
3447 case MODULE_MAP_FILE:
3448 if (ASTReadResult Result =
3449 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3450 return Result;
3451 break;
3452
3453 case INPUT_FILE_OFFSETS:
3454 NumInputs = Record[0];
3455 NumUserInputs = Record[1];
3456 F.InputFileOffsets =
3457 (const llvm::support::unaligned_uint64_t *)Blob.data();
3458 F.InputFilesLoaded.resize(new_size: NumInputs);
3459 F.InputFileInfosLoaded.resize(new_size: NumInputs);
3460 F.NumUserInputFiles = NumUserInputs;
3461 break;
3462 }
3463 }
3464}
3465
3466llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3467 unsigned ClientLoadCapabilities) {
3468 BitstreamCursor &Stream = F.Stream;
3469
3470 if (llvm::Error Err = Stream.EnterSubBlock(BlockID: AST_BLOCK_ID))
3471 return Err;
3472 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3473
3474 // Read all of the records and blocks for the AST file.
3475 RecordData Record;
3476 while (true) {
3477 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3478 if (!MaybeEntry)
3479 return MaybeEntry.takeError();
3480 llvm::BitstreamEntry Entry = MaybeEntry.get();
3481
3482 switch (Entry.Kind) {
3483 case llvm::BitstreamEntry::Error:
3484 return llvm::createStringError(
3485 EC: std::errc::illegal_byte_sequence,
3486 Fmt: "error at end of module block in AST file");
3487 case llvm::BitstreamEntry::EndBlock:
3488 // Outside of C++, we do not store a lookup map for the translation unit.
3489 // Instead, mark it as needing a lookup map to be built if this module
3490 // contains any declarations lexically within it (which it always does!).
3491 // This usually has no cost, since we very rarely need the lookup map for
3492 // the translation unit outside C++.
3493 if (ASTContext *Ctx = ContextObj) {
3494 DeclContext *DC = Ctx->getTranslationUnitDecl();
3495 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3496 DC->setMustBuildLookupTable();
3497 }
3498
3499 return llvm::Error::success();
3500 case llvm::BitstreamEntry::SubBlock:
3501 switch (Entry.ID) {
3502 case DECLTYPES_BLOCK_ID:
3503 // We lazily load the decls block, but we want to set up the
3504 // DeclsCursor cursor to point into it. Clone our current bitcode
3505 // cursor to it, enter the block and read the abbrevs in that block.
3506 // With the main cursor, we just skip over it.
3507 F.DeclsCursor = Stream;
3508 if (llvm::Error Err = Stream.SkipBlock())
3509 return Err;
3510 if (llvm::Error Err = ReadBlockAbbrevs(
3511 Cursor&: F.DeclsCursor, BlockID: DECLTYPES_BLOCK_ID, StartOfBlockOffset: &F.DeclsBlockStartOffset))
3512 return Err;
3513 break;
3514
3515 case PREPROCESSOR_BLOCK_ID:
3516 F.MacroCursor = Stream;
3517 if (!PP.getExternalSource())
3518 PP.setExternalSource(this);
3519
3520 if (llvm::Error Err = Stream.SkipBlock())
3521 return Err;
3522 if (llvm::Error Err =
3523 ReadBlockAbbrevs(Cursor&: F.MacroCursor, BlockID: PREPROCESSOR_BLOCK_ID))
3524 return Err;
3525 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3526 break;
3527
3528 case PREPROCESSOR_DETAIL_BLOCK_ID:
3529 F.PreprocessorDetailCursor = Stream;
3530
3531 if (llvm::Error Err = Stream.SkipBlock()) {
3532 return Err;
3533 }
3534 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: F.PreprocessorDetailCursor,
3535 BlockID: PREPROCESSOR_DETAIL_BLOCK_ID))
3536 return Err;
3537 F.PreprocessorDetailStartOffset
3538 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3539
3540 if (!PP.getPreprocessingRecord())
3541 PP.createPreprocessingRecord();
3542 if (!PP.getPreprocessingRecord()->getExternalSource())
3543 PP.getPreprocessingRecord()->SetExternalSource(*this);
3544 break;
3545
3546 case SOURCE_MANAGER_BLOCK_ID:
3547 if (llvm::Error Err = ReadSourceManagerBlock(F))
3548 return Err;
3549 break;
3550
3551 case SUBMODULE_BLOCK_ID:
3552 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3553 return Err;
3554 break;
3555
3556 case COMMENTS_BLOCK_ID: {
3557 BitstreamCursor C = Stream;
3558
3559 if (llvm::Error Err = Stream.SkipBlock())
3560 return Err;
3561 if (llvm::Error Err = ReadBlockAbbrevs(Cursor&: C, BlockID: COMMENTS_BLOCK_ID))
3562 return Err;
3563 CommentsCursors.push_back(Elt: std::make_pair(x&: C, y: &F));
3564 break;
3565 }
3566
3567 default:
3568 if (llvm::Error Err = Stream.SkipBlock())
3569 return Err;
3570 break;
3571 }
3572 continue;
3573
3574 case llvm::BitstreamEntry::Record:
3575 // The interesting case.
3576 break;
3577 }
3578
3579 // Read and process a record.
3580 Record.clear();
3581 StringRef Blob;
3582 Expected<unsigned> MaybeRecordType =
3583 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
3584 if (!MaybeRecordType)
3585 return MaybeRecordType.takeError();
3586 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3587
3588 // If we're not loading an AST context, we don't care about most records.
3589 if (!ContextObj) {
3590 switch (RecordType) {
3591 case IDENTIFIER_TABLE:
3592 case IDENTIFIER_OFFSET:
3593 case INTERESTING_IDENTIFIERS:
3594 case STATISTICS:
3595 case PP_ASSUME_NONNULL_LOC:
3596 case PP_CONDITIONAL_STACK:
3597 case PP_COUNTER_VALUE:
3598 case SOURCE_LOCATION_OFFSETS:
3599 case MODULE_OFFSET_MAP:
3600 case SOURCE_MANAGER_LINE_TABLE:
3601 case PPD_ENTITIES_OFFSETS:
3602 case HEADER_SEARCH_TABLE:
3603 case IMPORTED_MODULES:
3604 case MACRO_OFFSET:
3605 break;
3606 default:
3607 continue;
3608 }
3609 }
3610
3611 switch (RecordType) {
3612 default: // Default behavior: ignore.
3613 break;
3614
3615 case TYPE_OFFSET: {
3616 if (F.LocalNumTypes != 0)
3617 return llvm::createStringError(
3618 EC: std::errc::illegal_byte_sequence,
3619 Fmt: "duplicate TYPE_OFFSET record in AST file");
3620 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3621 F.LocalNumTypes = Record[0];
3622 F.BaseTypeIndex = getTotalNumTypes();
3623
3624 if (F.LocalNumTypes > 0)
3625 TypesLoaded.resize(NewSize: TypesLoaded.size() + F.LocalNumTypes);
3626
3627 break;
3628 }
3629
3630 case DECL_OFFSET: {
3631 if (F.LocalNumDecls != 0)
3632 return llvm::createStringError(
3633 EC: std::errc::illegal_byte_sequence,
3634 Fmt: "duplicate DECL_OFFSET record in AST file");
3635 F.DeclOffsets = (const DeclOffset *)Blob.data();
3636 F.LocalNumDecls = Record[0];
3637 F.BaseDeclIndex = getTotalNumDecls();
3638
3639 if (F.LocalNumDecls > 0)
3640 DeclsLoaded.resize(NewSize: DeclsLoaded.size() + F.LocalNumDecls);
3641
3642 break;
3643 }
3644
3645 case TU_UPDATE_LEXICAL: {
3646 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3647 LexicalContents Contents(
3648 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3649 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3650 TULexicalDecls.push_back(x: std::make_pair(x: &F, y&: Contents));
3651 TU->setHasExternalLexicalStorage(true);
3652 break;
3653 }
3654
3655 case UPDATE_VISIBLE: {
3656 unsigned Idx = 0;
3657 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3658 auto *Data = (const unsigned char*)Blob.data();
3659 PendingVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3660 // If we've already loaded the decl, perform the updates when we finish
3661 // loading this block.
3662 if (Decl *D = GetExistingDecl(ID))
3663 PendingUpdateRecords.push_back(
3664 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3665 break;
3666 }
3667
3668 case UPDATE_MODULE_LOCAL_VISIBLE: {
3669 unsigned Idx = 0;
3670 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3671 auto *Data = (const unsigned char *)Blob.data();
3672 PendingModuleLocalVisibleUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3673 // If we've already loaded the decl, perform the updates when we finish
3674 // loading this block.
3675 if (Decl *D = GetExistingDecl(ID))
3676 PendingUpdateRecords.push_back(
3677 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3678 break;
3679 }
3680
3681 case UPDATE_TU_LOCAL_VISIBLE: {
3682 if (F.Kind != MK_MainFile)
3683 break;
3684 unsigned Idx = 0;
3685 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3686 auto *Data = (const unsigned char *)Blob.data();
3687 TULocalUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3688 // If we've already loaded the decl, perform the updates when we finish
3689 // loading this block.
3690 if (Decl *D = GetExistingDecl(ID))
3691 PendingUpdateRecords.push_back(
3692 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3693 break;
3694 }
3695
3696 case CXX_ADDED_TEMPLATE_SPECIALIZATION: {
3697 unsigned Idx = 0;
3698 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3699 auto *Data = (const unsigned char *)Blob.data();
3700 PendingSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3701 // If we've already loaded the decl, perform the updates when we finish
3702 // loading this block.
3703 if (Decl *D = GetExistingDecl(ID))
3704 PendingUpdateRecords.push_back(
3705 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3706 break;
3707 }
3708
3709 case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {
3710 unsigned Idx = 0;
3711 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3712 auto *Data = (const unsigned char *)Blob.data();
3713 PendingPartialSpecializationsUpdates[ID].push_back(Elt: UpdateData{.Mod: &F, .Data: Data});
3714 // If we've already loaded the decl, perform the updates when we finish
3715 // loading this block.
3716 if (Decl *D = GetExistingDecl(ID))
3717 PendingUpdateRecords.push_back(
3718 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3719 break;
3720 }
3721
3722 case IDENTIFIER_TABLE:
3723 F.IdentifierTableData =
3724 reinterpret_cast<const unsigned char *>(Blob.data());
3725 if (Record[0]) {
3726 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3727 Buckets: F.IdentifierTableData + Record[0],
3728 Payload: F.IdentifierTableData + sizeof(uint32_t),
3729 Base: F.IdentifierTableData,
3730 InfoObj: ASTIdentifierLookupTrait(*this, F));
3731
3732 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3733 }
3734 break;
3735
3736 case IDENTIFIER_OFFSET: {
3737 if (F.LocalNumIdentifiers != 0)
3738 return llvm::createStringError(
3739 EC: std::errc::illegal_byte_sequence,
3740 Fmt: "duplicate IDENTIFIER_OFFSET record in AST file");
3741 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3742 F.LocalNumIdentifiers = Record[0];
3743 F.BaseIdentifierID = getTotalNumIdentifiers();
3744
3745 if (F.LocalNumIdentifiers > 0)
3746 IdentifiersLoaded.resize(new_size: IdentifiersLoaded.size()
3747 + F.LocalNumIdentifiers);
3748 break;
3749 }
3750
3751 case INTERESTING_IDENTIFIERS:
3752 F.PreloadIdentifierOffsets.assign(first: Record.begin(), last: Record.end());
3753 break;
3754
3755 case EAGERLY_DESERIALIZED_DECLS:
3756 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3757 // about "interesting" decls (for instance, if we're building a module).
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 MODULAR_CODEGEN_DECLS:
3763 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3764 // them (ie: if we're not codegenerating this module).
3765 if (F.Kind == MK_MainFile ||
3766 getContext().getLangOpts().BuildingPCHWithObjectFile)
3767 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3768 EagerlyDeserializedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3769 break;
3770
3771 case SPECIAL_TYPES:
3772 if (SpecialTypes.empty()) {
3773 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3774 SpecialTypes.push_back(Elt: getGlobalTypeID(F, LocalID: Record[I]));
3775 break;
3776 }
3777
3778 if (Record.empty())
3779 break;
3780
3781 if (SpecialTypes.size() != Record.size())
3782 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3783 Fmt: "invalid special-types record");
3784
3785 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3786 serialization::TypeID ID = getGlobalTypeID(F, LocalID: Record[I]);
3787 if (!SpecialTypes[I])
3788 SpecialTypes[I] = ID;
3789 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3790 // merge step?
3791 }
3792 break;
3793
3794 case STATISTICS:
3795 TotalNumStatements += Record[0];
3796 TotalNumMacros += Record[1];
3797 TotalLexicalDeclContexts += Record[2];
3798 TotalVisibleDeclContexts += Record[3];
3799 TotalModuleLocalVisibleDeclContexts += Record[4];
3800 TotalTULocalVisibleDeclContexts += Record[5];
3801 break;
3802
3803 case UNUSED_FILESCOPED_DECLS:
3804 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3805 UnusedFileScopedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3806 break;
3807
3808 case DELEGATING_CTORS:
3809 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3810 DelegatingCtorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3811 break;
3812
3813 case WEAK_UNDECLARED_IDENTIFIERS:
3814 if (Record.size() % 3 != 0)
3815 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3816 Fmt: "invalid weak identifiers record");
3817
3818 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3819 // files. This isn't the way to do it :)
3820 WeakUndeclaredIdentifiers.clear();
3821
3822 // Translate the weak, undeclared identifiers into global IDs.
3823 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3824 WeakUndeclaredIdentifiers.push_back(
3825 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3826 WeakUndeclaredIdentifiers.push_back(
3827 Elt: getGlobalIdentifierID(M&: F, LocalID: Record[I++]));
3828 WeakUndeclaredIdentifiers.push_back(
3829 Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
3830 }
3831 break;
3832
3833 case SELECTOR_OFFSETS: {
3834 F.SelectorOffsets = (const uint32_t *)Blob.data();
3835 F.LocalNumSelectors = Record[0];
3836 unsigned LocalBaseSelectorID = Record[1];
3837 F.BaseSelectorID = getTotalNumSelectors();
3838
3839 if (F.LocalNumSelectors > 0) {
3840 // Introduce the global -> local mapping for selectors within this
3841 // module.
3842 GlobalSelectorMap.insert(Val: std::make_pair(x: getTotalNumSelectors()+1, y: &F));
3843
3844 // Introduce the local -> global mapping for selectors within this
3845 // module.
3846 F.SelectorRemap.insertOrReplace(
3847 Val: std::make_pair(x&: LocalBaseSelectorID,
3848 y: F.BaseSelectorID - LocalBaseSelectorID));
3849
3850 SelectorsLoaded.resize(N: SelectorsLoaded.size() + F.LocalNumSelectors);
3851 }
3852 break;
3853 }
3854
3855 case METHOD_POOL:
3856 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3857 if (Record[0])
3858 F.SelectorLookupTable
3859 = ASTSelectorLookupTable::Create(
3860 Buckets: F.SelectorLookupTableData + Record[0],
3861 Base: F.SelectorLookupTableData,
3862 InfoObj: ASTSelectorLookupTrait(*this, F));
3863 TotalNumMethodPoolEntries += Record[1];
3864 break;
3865
3866 case REFERENCED_SELECTOR_POOL:
3867 if (!Record.empty()) {
3868 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3869 ReferencedSelectorsData.push_back(Elt: getGlobalSelectorID(M&: F,
3870 LocalID: Record[Idx++]));
3871 ReferencedSelectorsData.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx).
3872 getRawEncoding());
3873 }
3874 }
3875 break;
3876
3877 case PP_ASSUME_NONNULL_LOC: {
3878 unsigned Idx = 0;
3879 if (!Record.empty())
3880 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3881 ReadSourceLocation(ModuleFile&: F, Record, Idx));
3882 break;
3883 }
3884
3885 case PP_UNSAFE_BUFFER_USAGE: {
3886 if (!Record.empty()) {
3887 SmallVector<SourceLocation, 64> SrcLocs;
3888 unsigned Idx = 0;
3889 while (Idx < Record.size())
3890 SrcLocs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx));
3891 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
3892 }
3893 break;
3894 }
3895
3896 case PP_CONDITIONAL_STACK:
3897 if (!Record.empty()) {
3898 unsigned Idx = 0, End = Record.size() - 1;
3899 bool ReachedEOFWhileSkipping = Record[Idx++];
3900 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3901 if (ReachedEOFWhileSkipping) {
3902 SourceLocation HashToken = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3903 SourceLocation IfTokenLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3904 bool FoundNonSkipPortion = Record[Idx++];
3905 bool FoundElse = Record[Idx++];
3906 SourceLocation ElseLoc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3907 SkipInfo.emplace(args&: HashToken, args&: IfTokenLoc, args&: FoundNonSkipPortion,
3908 args&: FoundElse, args&: ElseLoc);
3909 }
3910 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3911 while (Idx < End) {
3912 auto Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx);
3913 bool WasSkipping = Record[Idx++];
3914 bool FoundNonSkip = Record[Idx++];
3915 bool FoundElse = Record[Idx++];
3916 ConditionalStack.push_back(
3917 Elt: {.IfLoc: Loc, .WasSkipping: WasSkipping, .FoundNonSkip: FoundNonSkip, .FoundElse: FoundElse});
3918 }
3919 PP.setReplayablePreambleConditionalStack(s: ConditionalStack, SkipInfo);
3920 }
3921 break;
3922
3923 case PP_COUNTER_VALUE:
3924 if (!Record.empty() && Listener)
3925 Listener->ReadCounter(M: F, Value: Record[0]);
3926 break;
3927
3928 case FILE_SORTED_DECLS:
3929 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
3930 F.NumFileSortedDecls = Record[0];
3931 break;
3932
3933 case SOURCE_LOCATION_OFFSETS: {
3934 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3935 F.LocalNumSLocEntries = Record[0];
3936 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3937 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3938 std::tie(args&: F.SLocEntryBaseID, args&: F.SLocEntryBaseOffset) =
3939 SourceMgr.AllocateLoadedSLocEntries(NumSLocEntries: F.LocalNumSLocEntries,
3940 TotalSize: SLocSpaceSize);
3941 if (!F.SLocEntryBaseID) {
3942 Diags.Report(Loc: SourceLocation(), DiagID: diag::remark_sloc_usage);
3943 SourceMgr.noteSLocAddressSpaceUsage(Diag&: Diags);
3944 return llvm::createStringError(EC: std::errc::invalid_argument,
3945 Fmt: "ran out of source locations");
3946 }
3947 // Make our entry in the range map. BaseID is negative and growing, so
3948 // we invert it. Because we invert it, though, we need the other end of
3949 // the range.
3950 unsigned RangeStart =
3951 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3952 GlobalSLocEntryMap.insert(Val: std::make_pair(x&: RangeStart, y: &F));
3953 F.FirstLoc = SourceLocation::getFromRawEncoding(Encoding: F.SLocEntryBaseOffset);
3954
3955 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3956 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3957 GlobalSLocOffsetMap.insert(
3958 Val: std::make_pair(x: SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3959 - SLocSpaceSize,y: &F));
3960
3961 TotalNumSLocEntries += F.LocalNumSLocEntries;
3962 break;
3963 }
3964
3965 case MODULE_OFFSET_MAP:
3966 F.ModuleOffsetMap = Blob;
3967 break;
3968
3969 case SOURCE_MANAGER_LINE_TABLE:
3970 ParseLineTable(F, Record);
3971 break;
3972
3973 case EXT_VECTOR_DECLS:
3974 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3975 ExtVectorDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
3976 break;
3977
3978 case VTABLE_USES:
3979 if (Record.size() % 3 != 0)
3980 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
3981 Fmt: "Invalid VTABLE_USES record");
3982
3983 // Later tables overwrite earlier ones.
3984 // FIXME: Modules will have some trouble with this. This is clearly not
3985 // the right way to do this.
3986 VTableUses.clear();
3987
3988 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3989 VTableUses.push_back(
3990 Elt: {.ID: ReadDeclID(F, Record, Idx),
3991 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx).getRawEncoding(),
3992 .Used: (bool)Record[Idx++]});
3993 }
3994 break;
3995
3996 case PENDING_IMPLICIT_INSTANTIATIONS:
3997
3998 if (Record.size() % 2 != 0)
3999 return llvm::createStringError(
4000 EC: std::errc::illegal_byte_sequence,
4001 Fmt: "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
4002
4003 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4004 PendingInstantiations.push_back(
4005 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4006 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4007 }
4008 break;
4009
4010 case SEMA_DECL_REFS:
4011 if (Record.size() != 3)
4012 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4013 Fmt: "Invalid SEMA_DECL_REFS block");
4014 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4015 SemaDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4016 break;
4017
4018 case PPD_ENTITIES_OFFSETS: {
4019 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
4020 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
4021 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
4022
4023 unsigned LocalBasePreprocessedEntityID = Record[0];
4024
4025 unsigned StartingID;
4026 if (!PP.getPreprocessingRecord())
4027 PP.createPreprocessingRecord();
4028 if (!PP.getPreprocessingRecord()->getExternalSource())
4029 PP.getPreprocessingRecord()->SetExternalSource(*this);
4030 StartingID
4031 = PP.getPreprocessingRecord()
4032 ->allocateLoadedEntities(NumEntities: F.NumPreprocessedEntities);
4033 F.BasePreprocessedEntityID = StartingID;
4034
4035 if (F.NumPreprocessedEntities > 0) {
4036 // Introduce the global -> local mapping for preprocessed entities in
4037 // this module.
4038 GlobalPreprocessedEntityMap.insert(Val: std::make_pair(x&: StartingID, y: &F));
4039
4040 // Introduce the local -> global mapping for preprocessed entities in
4041 // this module.
4042 F.PreprocessedEntityRemap.insertOrReplace(
4043 Val: std::make_pair(x&: LocalBasePreprocessedEntityID,
4044 y: F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
4045 }
4046
4047 break;
4048 }
4049
4050 case PPD_SKIPPED_RANGES: {
4051 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
4052 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
4053 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
4054
4055 if (!PP.getPreprocessingRecord())
4056 PP.createPreprocessingRecord();
4057 if (!PP.getPreprocessingRecord()->getExternalSource())
4058 PP.getPreprocessingRecord()->SetExternalSource(*this);
4059 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
4060 ->allocateSkippedRanges(NumRanges: F.NumPreprocessedSkippedRanges);
4061
4062 if (F.NumPreprocessedSkippedRanges > 0)
4063 GlobalSkippedRangeMap.insert(
4064 Val: std::make_pair(x&: F.BasePreprocessedSkippedRangeID, y: &F));
4065 break;
4066 }
4067
4068 case DECL_UPDATE_OFFSETS:
4069 if (Record.size() % 2 != 0)
4070 return llvm::createStringError(
4071 EC: std::errc::illegal_byte_sequence,
4072 Fmt: "invalid DECL_UPDATE_OFFSETS block in AST file");
4073 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4074 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4075 DeclUpdateOffsets[ID].push_back(Elt: std::make_pair(x: &F, y&: Record[I++]));
4076
4077 // If we've already loaded the decl, perform the updates when we finish
4078 // loading this block.
4079 if (Decl *D = GetExistingDecl(ID))
4080 PendingUpdateRecords.push_back(
4081 Elt: PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
4082 }
4083 break;
4084
4085 case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
4086 if (Record.size() % 5 != 0)
4087 return llvm::createStringError(
4088 EC: std::errc::illegal_byte_sequence,
4089 Fmt: "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
4090 "file");
4091 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4092 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4093
4094 uint64_t BaseOffset = F.DeclsBlockStartOffset;
4095 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
4096 uint64_t LocalLexicalOffset = Record[I++];
4097 uint64_t LexicalOffset =
4098 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
4099 uint64_t LocalVisibleOffset = Record[I++];
4100 uint64_t VisibleOffset =
4101 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
4102 uint64_t LocalModuleLocalOffset = Record[I++];
4103 uint64_t ModuleLocalOffset =
4104 LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;
4105 uint64_t TULocalLocalOffset = Record[I++];
4106 uint64_t TULocalOffset =
4107 TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;
4108
4109 DelayedNamespaceOffsetMap[ID] = {
4110 {.VisibleOffset: VisibleOffset, .ModuleLocalOffset: TULocalOffset, .TULocalOffset: ModuleLocalOffset}, .LexicalOffset: LexicalOffset};
4111
4112 assert(!GetExistingDecl(ID) &&
4113 "We shouldn't load the namespace in the front of delayed "
4114 "namespace lexical and visible block");
4115 }
4116 break;
4117 }
4118
4119 case RELATED_DECLS_MAP:
4120 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
4121 GlobalDeclID ID = ReadDeclID(F, Record, Idx&: I);
4122 auto &RelatedDecls = RelatedDeclsMap[ID];
4123 unsigned NN = Record[I++];
4124 RelatedDecls.reserve(N: NN);
4125 for (unsigned II = 0; II < NN; II++)
4126 RelatedDecls.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4127 }
4128 break;
4129
4130 case OBJC_CATEGORIES_MAP:
4131 if (F.LocalNumObjCCategoriesInMap != 0)
4132 return llvm::createStringError(
4133 EC: std::errc::illegal_byte_sequence,
4134 Fmt: "duplicate OBJC_CATEGORIES_MAP record in AST file");
4135
4136 F.LocalNumObjCCategoriesInMap = Record[0];
4137 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
4138 break;
4139
4140 case OBJC_CATEGORIES:
4141 F.ObjCCategories.swap(RHS&: Record);
4142 break;
4143
4144 case CUDA_SPECIAL_DECL_REFS:
4145 // Later tables overwrite earlier ones.
4146 // FIXME: Modules will have trouble with this.
4147 CUDASpecialDeclRefs.clear();
4148 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4149 CUDASpecialDeclRefs.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4150 break;
4151
4152 case HEADER_SEARCH_TABLE:
4153 F.HeaderFileInfoTableData = Blob.data();
4154 F.LocalNumHeaderFileInfos = Record[1];
4155 if (Record[0]) {
4156 F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(
4157 Buckets: (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
4158 Base: (const unsigned char *)F.HeaderFileInfoTableData,
4159 InfoObj: HeaderFileInfoTrait(*this, F));
4160
4161 PP.getHeaderSearchInfo().SetExternalSource(this);
4162 if (!PP.getHeaderSearchInfo().getExternalLookup())
4163 PP.getHeaderSearchInfo().SetExternalLookup(this);
4164 }
4165 break;
4166
4167 case FP_PRAGMA_OPTIONS:
4168 // Later tables overwrite earlier ones.
4169 FPPragmaOptions.swap(RHS&: Record);
4170 break;
4171
4172 case DECLS_WITH_EFFECTS_TO_VERIFY:
4173 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4174 DeclsWithEffectsToVerify.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4175 break;
4176
4177 case OPENCL_EXTENSIONS:
4178 for (unsigned I = 0, E = Record.size(); I != E; ) {
4179 auto Name = ReadString(Record, Idx&: I);
4180 auto &OptInfo = OpenCLExtensions.OptMap[Name];
4181 OptInfo.Supported = Record[I++] != 0;
4182 OptInfo.Enabled = Record[I++] != 0;
4183 OptInfo.WithPragma = Record[I++] != 0;
4184 OptInfo.Avail = Record[I++];
4185 OptInfo.Core = Record[I++];
4186 OptInfo.Opt = Record[I++];
4187 }
4188 break;
4189
4190 case TENTATIVE_DEFINITIONS:
4191 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4192 TentativeDefinitions.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4193 break;
4194
4195 case KNOWN_NAMESPACES:
4196 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4197 KnownNamespaces.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4198 break;
4199
4200 case UNDEFINED_BUT_USED:
4201 if (Record.size() % 2 != 0)
4202 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4203 Fmt: "invalid undefined-but-used record");
4204 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
4205 UndefinedButUsed.push_back(
4206 Elt: {.ID: ReadDeclID(F, Record, Idx&: I),
4207 .RawLoc: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding()});
4208 }
4209 break;
4210
4211 case DELETE_EXPRS_TO_ANALYZE:
4212 for (unsigned I = 0, N = Record.size(); I != N;) {
4213 DelayedDeleteExprs.push_back(Elt: ReadDeclID(F, Record, Idx&: I).getRawValue());
4214 const uint64_t Count = Record[I++];
4215 DelayedDeleteExprs.push_back(Elt: Count);
4216 for (uint64_t C = 0; C < Count; ++C) {
4217 DelayedDeleteExprs.push_back(Elt: ReadSourceLocation(ModuleFile&: F, Record, Idx&: I).getRawEncoding());
4218 bool IsArrayForm = Record[I++] == 1;
4219 DelayedDeleteExprs.push_back(Elt: IsArrayForm);
4220 }
4221 }
4222 break;
4223
4224 case VTABLES_TO_EMIT:
4225 if (F.Kind == MK_MainFile ||
4226 getContext().getLangOpts().BuildingPCHWithObjectFile)
4227 for (unsigned I = 0, N = Record.size(); I != N;)
4228 VTablesToEmit.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4229 break;
4230
4231 case IMPORTED_MODULES:
4232 if (!F.isModule()) {
4233 // If we aren't loading a module (which has its own exports), make
4234 // all of the imported modules visible.
4235 // FIXME: Deal with macros-only imports.
4236 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
4237 unsigned GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[I++]);
4238 SourceLocation Loc = ReadSourceLocation(ModuleFile&: F, Record, Idx&: I);
4239 if (GlobalID) {
4240 PendingImportedModules.push_back(Elt: ImportedSubmodule(GlobalID, Loc));
4241 if (DeserializationListener)
4242 DeserializationListener->ModuleImportRead(ID: GlobalID, ImportLoc: Loc);
4243 }
4244 }
4245 }
4246 break;
4247
4248 case MACRO_OFFSET: {
4249 if (F.LocalNumMacros != 0)
4250 return llvm::createStringError(
4251 EC: std::errc::illegal_byte_sequence,
4252 Fmt: "duplicate MACRO_OFFSET record in AST file");
4253 F.MacroOffsets = (const uint32_t *)Blob.data();
4254 F.LocalNumMacros = Record[0];
4255 unsigned LocalBaseMacroID = Record[1];
4256 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
4257 F.BaseMacroID = getTotalNumMacros();
4258
4259 if (F.LocalNumMacros > 0) {
4260 // Introduce the global -> local mapping for macros within this module.
4261 GlobalMacroMap.insert(Val: std::make_pair(x: getTotalNumMacros() + 1, y: &F));
4262
4263 // Introduce the local -> global mapping for macros within this module.
4264 F.MacroRemap.insertOrReplace(
4265 Val: std::make_pair(x&: LocalBaseMacroID,
4266 y: F.BaseMacroID - LocalBaseMacroID));
4267
4268 MacrosLoaded.resize(new_size: MacrosLoaded.size() + F.LocalNumMacros);
4269 }
4270 break;
4271 }
4272
4273 case LATE_PARSED_TEMPLATE:
4274 LateParsedTemplates.emplace_back(
4275 Args: std::piecewise_construct, Args: std::forward_as_tuple(args: &F),
4276 Args: std::forward_as_tuple(args: Record.begin(), args: Record.end()));
4277 break;
4278
4279 case OPTIMIZE_PRAGMA_OPTIONS:
4280 if (Record.size() != 1)
4281 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4282 Fmt: "invalid pragma optimize record");
4283 OptimizeOffPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[0]);
4284 break;
4285
4286 case MSSTRUCT_PRAGMA_OPTIONS:
4287 if (Record.size() != 1)
4288 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4289 Fmt: "invalid pragma ms_struct record");
4290 PragmaMSStructState = Record[0];
4291 break;
4292
4293 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
4294 if (Record.size() != 2)
4295 return llvm::createStringError(
4296 EC: std::errc::illegal_byte_sequence,
4297 Fmt: "invalid pragma pointers to members record");
4298 PragmaMSPointersToMembersState = Record[0];
4299 PointersToMembersPragmaLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4300 break;
4301
4302 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
4303 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4304 UnusedLocalTypedefNameCandidates.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
4305 break;
4306
4307 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
4308 if (Record.size() != 1)
4309 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4310 Fmt: "invalid cuda pragma options record");
4311 ForceHostDeviceDepth = Record[0];
4312 break;
4313
4314 case ALIGN_PACK_PRAGMA_OPTIONS: {
4315 if (Record.size() < 3)
4316 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4317 Fmt: "invalid pragma pack record");
4318 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Raw: Record[0]);
4319 PragmaAlignPackCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4320 unsigned NumStackEntries = Record[2];
4321 unsigned Idx = 3;
4322 // Reset the stack when importing a new module.
4323 PragmaAlignPackStack.clear();
4324 for (unsigned I = 0; I < NumStackEntries; ++I) {
4325 PragmaAlignPackStackEntry Entry;
4326 Entry.Value = ReadAlignPackInfo(Raw: Record[Idx++]);
4327 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4328 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4329 PragmaAlignPackStrings.push_back(Elt: ReadString(Record, Idx));
4330 Entry.SlotLabel = PragmaAlignPackStrings.back();
4331 PragmaAlignPackStack.push_back(Elt: Entry);
4332 }
4333 break;
4334 }
4335
4336 case FLOAT_CONTROL_PRAGMA_OPTIONS: {
4337 if (Record.size() < 3)
4338 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
4339 Fmt: "invalid pragma float control record");
4340 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(I: Record[0]);
4341 FpPragmaCurrentLocation = ReadSourceLocation(MF&: F, Raw: Record[1]);
4342 unsigned NumStackEntries = Record[2];
4343 unsigned Idx = 3;
4344 // Reset the stack when importing a new module.
4345 FpPragmaStack.clear();
4346 for (unsigned I = 0; I < NumStackEntries; ++I) {
4347 FpPragmaStackEntry Entry;
4348 Entry.Value = FPOptionsOverride::getFromOpaqueInt(I: Record[Idx++]);
4349 Entry.Location = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4350 Entry.PushLocation = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
4351 FpPragmaStrings.push_back(Elt: ReadString(Record, Idx));
4352 Entry.SlotLabel = FpPragmaStrings.back();
4353 FpPragmaStack.push_back(Elt: Entry);
4354 }
4355 break;
4356 }
4357
4358 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
4359 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4360 DeclsToCheckForDeferredDiags.insert(X: ReadDeclID(F, Record, Idx&: I));
4361 break;
4362 }
4363 }
4364}
4365
4366void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4367 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4368
4369 // Additional remapping information.
4370 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4371 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4372 F.ModuleOffsetMap = StringRef();
4373
4374 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4375 RemapBuilder MacroRemap(F.MacroRemap);
4376 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4377 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4378 RemapBuilder SelectorRemap(F.SelectorRemap);
4379
4380 auto &ImportedModuleVector = F.TransitiveImports;
4381 assert(ImportedModuleVector.empty());
4382
4383 while (Data < DataEnd) {
4384 // FIXME: Looking up dependency modules by filename is horrible. Let's
4385 // start fixing this with prebuilt, explicit and implicit modules and see
4386 // how it goes...
4387 using namespace llvm::support;
4388 ModuleKind Kind = static_cast<ModuleKind>(
4389 endian::readNext<uint8_t, llvm::endianness::little>(memory&: Data));
4390 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(memory&: Data);
4391 StringRef Name = StringRef((const char*)Data, Len);
4392 Data += Len;
4393 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
4394 Kind == MK_ImplicitModule
4395 ? ModuleMgr.lookupByModuleName(ModName: Name)
4396 : ModuleMgr.lookupByFileName(FileName: Name));
4397 if (!OM) {
4398 std::string Msg = "refers to unknown module, cannot find ";
4399 Msg.append(str: std::string(Name));
4400 Error(Msg);
4401 return;
4402 }
4403
4404 ImportedModuleVector.push_back(Elt: OM);
4405
4406 uint32_t MacroIDOffset =
4407 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4408 uint32_t PreprocessedEntityIDOffset =
4409 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4410 uint32_t SubmoduleIDOffset =
4411 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4412 uint32_t SelectorIDOffset =
4413 endian::readNext<uint32_t, llvm::endianness::little>(memory&: Data);
4414
4415 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4416 RemapBuilder &Remap) {
4417 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4418 if (Offset != None)
4419 Remap.insert(Val: std::make_pair(x&: Offset,
4420 y: static_cast<int>(BaseOffset - Offset)));
4421 };
4422
4423 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4424 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4425 PreprocessedEntityRemap);
4426 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4427 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4428 }
4429}
4430
4431ASTReader::ASTReadResult
4432ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4433 const ModuleFile *ImportedBy,
4434 unsigned ClientLoadCapabilities) {
4435 unsigned Idx = 0;
4436 F.ModuleMapPath = ReadPath(F, Record, Idx);
4437
4438 // Try to resolve ModuleName in the current header search context and
4439 // verify that it is found in the same module map file as we saved. If the
4440 // top-level AST file is a main file, skip this check because there is no
4441 // usable header search context.
4442 assert(!F.ModuleName.empty() &&
4443 "MODULE_NAME should come before MODULE_MAP_FILE");
4444 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4445 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4446 // An implicitly-loaded module file should have its module listed in some
4447 // module map file that we've already loaded.
4448 Module *M =
4449 PP.getHeaderSearchInfo().lookupModule(ModuleName: F.ModuleName, ImportLoc: F.ImportLoc);
4450 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4451 OptionalFileEntryRef ModMap =
4452 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4453 // Don't emit module relocation error if we have -fno-validate-pch
4454 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4455 DisableValidationForModuleKind::Module) &&
4456 !ModMap) {
4457 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities)) {
4458 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4459 // This module was defined by an imported (explicit) module.
4460 Diag(DiagID: diag::err_module_file_conflict) << F.ModuleName << F.FileName
4461 << ASTFE->getName();
4462 // TODO: Add a note with the module map paths if they differ.
4463 } else {
4464 // This module was built with a different module map.
4465 Diag(DiagID: diag::err_imported_module_not_found)
4466 << F.ModuleName << F.FileName
4467 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4468 << !ImportedBy;
4469 // In case it was imported by a PCH, there's a chance the user is
4470 // just missing to include the search path to the directory containing
4471 // the modulemap.
4472 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4473 Diag(DiagID: diag::note_imported_by_pch_module_not_found)
4474 << llvm::sys::path::parent_path(path: F.ModuleMapPath);
4475 }
4476 }
4477 return OutOfDate;
4478 }
4479
4480 assert(M && M->Name == F.ModuleName && "found module with different name");
4481
4482 // Check the primary module map file.
4483 auto StoredModMap = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
4484 if (!StoredModMap || *StoredModMap != ModMap) {
4485 assert(ModMap && "found module is missing module map file");
4486 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4487 "top-level import should be verified");
4488 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4489 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4490 Diag(DiagID: diag::err_imported_module_modmap_changed)
4491 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4492 << ModMap->getName() << F.ModuleMapPath << NotImported;
4493 return OutOfDate;
4494 }
4495
4496 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4497 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4498 // FIXME: we should use input files rather than storing names.
4499 std::string Filename = ReadPath(F, Record, Idx);
4500 auto SF = FileMgr.getOptionalFileRef(Filename, OpenFile: false, CacheFailure: false);
4501 if (!SF) {
4502 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4503 Error(Msg: "could not find file '" + Filename +"' referenced by AST file");
4504 return OutOfDate;
4505 }
4506 AdditionalStoredMaps.insert(V: *SF);
4507 }
4508
4509 // Check any additional module map files (e.g. module.private.modulemap)
4510 // that are not in the pcm.
4511 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4512 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4513 // Remove files that match
4514 // Note: SmallPtrSet::erase is really remove
4515 if (!AdditionalStoredMaps.erase(V: ModMap)) {
4516 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4517 Diag(DiagID: diag::err_module_different_modmap)
4518 << F.ModuleName << /*new*/0 << ModMap.getName();
4519 return OutOfDate;
4520 }
4521 }
4522 }
4523
4524 // Check any additional module map files that are in the pcm, but not
4525 // found in header search. Cases that match are already removed.
4526 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4527 if (!canRecoverFromOutOfDate(ModuleFileName: F.FileName, ClientLoadCapabilities))
4528 Diag(DiagID: diag::err_module_different_modmap)
4529 << F.ModuleName << /*not new*/1 << ModMap.getName();
4530 return OutOfDate;
4531 }
4532 }
4533
4534 if (Listener)
4535 Listener->ReadModuleMapFile(ModuleMapPath: F.ModuleMapPath);
4536 return Success;
4537}
4538
4539/// Move the given method to the back of the global list of methods.
4540static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4541 // Find the entry for this selector in the method pool.
4542 SemaObjC::GlobalMethodPool::iterator Known =
4543 S.ObjC().MethodPool.find(Val: Method->getSelector());
4544 if (Known == S.ObjC().MethodPool.end())
4545 return;
4546
4547 // Retrieve the appropriate method list.
4548 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4549 : Known->second.second;
4550 bool Found = false;
4551 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4552 if (!Found) {
4553 if (List->getMethod() == Method) {
4554 Found = true;
4555 } else {
4556 // Keep searching.
4557 continue;
4558 }
4559 }
4560
4561 if (List->getNext())
4562 List->setMethod(List->getNext()->getMethod());
4563 else
4564 List->setMethod(Method);
4565 }
4566}
4567
4568void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4569 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4570 for (Decl *D : Names) {
4571 bool wasHidden = !D->isUnconditionallyVisible();
4572 D->setVisibleDespiteOwningModule();
4573
4574 if (wasHidden && SemaObj) {
4575 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Val: D)) {
4576 moveMethodToBackOfGlobalList(S&: *SemaObj, Method);
4577 }
4578 }
4579 }
4580}
4581
4582void ASTReader::makeModuleVisible(Module *Mod,
4583 Module::NameVisibilityKind NameVisibility,
4584 SourceLocation ImportLoc) {
4585 llvm::SmallPtrSet<Module *, 4> Visited;
4586 SmallVector<Module *, 4> Stack;
4587 Stack.push_back(Elt: Mod);
4588 while (!Stack.empty()) {
4589 Mod = Stack.pop_back_val();
4590
4591 if (NameVisibility <= Mod->NameVisibility) {
4592 // This module already has this level of visibility (or greater), so
4593 // there is nothing more to do.
4594 continue;
4595 }
4596
4597 if (Mod->isUnimportable()) {
4598 // Modules that aren't importable cannot be made visible.
4599 continue;
4600 }
4601
4602 // Update the module's name visibility.
4603 Mod->NameVisibility = NameVisibility;
4604
4605 // If we've already deserialized any names from this module,
4606 // mark them as visible.
4607 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Val: Mod);
4608 if (Hidden != HiddenNamesMap.end()) {
4609 auto HiddenNames = std::move(*Hidden);
4610 HiddenNamesMap.erase(I: Hidden);
4611 makeNamesVisible(Names: HiddenNames.second, Owner: HiddenNames.first);
4612 assert(!HiddenNamesMap.contains(Mod) &&
4613 "making names visible added hidden names");
4614 }
4615
4616 // Push any exported modules onto the stack to be marked as visible.
4617 SmallVector<Module *, 16> Exports;
4618 Mod->getExportedModules(Exported&: Exports);
4619 for (SmallVectorImpl<Module *>::iterator
4620 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4621 Module *Exported = *I;
4622 if (Visited.insert(Ptr: Exported).second)
4623 Stack.push_back(Elt: Exported);
4624 }
4625 }
4626}
4627
4628/// We've merged the definition \p MergedDef into the existing definition
4629/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4630/// visible.
4631void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4632 NamedDecl *MergedDef) {
4633 if (!Def->isUnconditionallyVisible()) {
4634 // If MergedDef is visible or becomes visible, make the definition visible.
4635 if (MergedDef->isUnconditionallyVisible())
4636 Def->setVisibleDespiteOwningModule();
4637 else {
4638 getContext().mergeDefinitionIntoModule(
4639 ND: Def, M: MergedDef->getImportedOwningModule(),
4640 /*NotifyListeners*/ false);
4641 PendingMergedDefinitionsToDeduplicate.insert(X: Def);
4642 }
4643 }
4644}
4645
4646bool ASTReader::loadGlobalIndex() {
4647 if (GlobalIndex)
4648 return false;
4649
4650 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4651 !PP.getLangOpts().Modules)
4652 return true;
4653
4654 // Try to load the global index.
4655 TriedLoadingGlobalIndex = true;
4656 StringRef ModuleCachePath
4657 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4658 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4659 GlobalModuleIndex::readIndex(Path: ModuleCachePath);
4660 if (llvm::Error Err = std::move(Result.second)) {
4661 assert(!Result.first);
4662 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
4663 return true;
4664 }
4665
4666 GlobalIndex.reset(p: Result.first);
4667 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4668 return false;
4669}
4670
4671bool ASTReader::isGlobalIndexUnavailable() const {
4672 return PP.getLangOpts().Modules && UseGlobalIndex &&
4673 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4674}
4675
4676/// Given a cursor at the start of an AST file, scan ahead and drop the
4677/// cursor into the start of the given block ID, returning false on success and
4678/// true on failure.
4679static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4680 while (true) {
4681 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4682 if (!MaybeEntry) {
4683 // FIXME this drops errors on the floor.
4684 consumeError(Err: MaybeEntry.takeError());
4685 return true;
4686 }
4687 llvm::BitstreamEntry Entry = MaybeEntry.get();
4688
4689 switch (Entry.Kind) {
4690 case llvm::BitstreamEntry::Error:
4691 case llvm::BitstreamEntry::EndBlock:
4692 return true;
4693
4694 case llvm::BitstreamEntry::Record:
4695 // Ignore top-level records.
4696 if (Expected<unsigned> Skipped = Cursor.skipRecord(AbbrevID: Entry.ID))
4697 break;
4698 else {
4699 // FIXME this drops errors on the floor.
4700 consumeError(Err: Skipped.takeError());
4701 return true;
4702 }
4703
4704 case llvm::BitstreamEntry::SubBlock:
4705 if (Entry.ID == BlockID) {
4706 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4707 // FIXME this drops the error on the floor.
4708 consumeError(Err: std::move(Err));
4709 return true;
4710 }
4711 // Found it!
4712 return false;
4713 }
4714
4715 if (llvm::Error Err = Cursor.SkipBlock()) {
4716 // FIXME this drops the error on the floor.
4717 consumeError(Err: std::move(Err));
4718 return true;
4719 }
4720 }
4721 }
4722}
4723
4724ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
4725 SourceLocation ImportLoc,
4726 unsigned ClientLoadCapabilities,
4727 ModuleFile **NewLoadedModuleFile) {
4728 llvm::TimeTraceScope scope("ReadAST", FileName);
4729
4730 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4731 llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(
4732 CurrentDeserializingModuleKind, Type);
4733
4734 // Defer any pending actions until we get to the end of reading the AST file.
4735 Deserializing AnASTFile(this);
4736
4737 // Bump the generation number.
4738 unsigned PreviousGeneration = 0;
4739 if (ContextObj)
4740 PreviousGeneration = incrementGeneration(C&: *ContextObj);
4741
4742 unsigned NumModules = ModuleMgr.size();
4743 SmallVector<ImportedModule, 4> Loaded;
4744 if (ASTReadResult ReadResult =
4745 ReadASTCore(FileName, Type, ImportLoc,
4746 /*ImportedBy=*/nullptr, Loaded, ExpectedSize: 0, ExpectedModTime: 0, ExpectedSignature: ASTFileSignature(),
4747 ClientLoadCapabilities)) {
4748 ModuleMgr.removeModules(First: ModuleMgr.begin() + NumModules);
4749
4750 // If we find that any modules are unusable, the global index is going
4751 // to be out-of-date. Just remove it.
4752 GlobalIndex.reset();
4753 ModuleMgr.setGlobalIndex(nullptr);
4754 return ReadResult;
4755 }
4756
4757 if (NewLoadedModuleFile && !Loaded.empty())
4758 *NewLoadedModuleFile = Loaded.back().Mod;
4759
4760 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4761 // remove modules from this point. Various fields are updated during reading
4762 // the AST block and removing the modules would result in dangling pointers.
4763 // They are generally only incidentally dereferenced, ie. a binary search
4764 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4765 // be dereferenced but it wouldn't actually be used.
4766
4767 // Load the AST blocks of all of the modules that we loaded. We can still
4768 // hit errors parsing the ASTs at this point.
4769 for (ImportedModule &M : Loaded) {
4770 ModuleFile &F = *M.Mod;
4771 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4772
4773 // Read the AST block.
4774 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4775 Error(Err: std::move(Err));
4776 return Failure;
4777 }
4778
4779 // The AST block should always have a definition for the main module.
4780 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4781 Error(DiagID: diag::err_module_file_missing_top_level_submodule, Arg1: F.FileName);
4782 return Failure;
4783 }
4784
4785 // Read the extension blocks.
4786 while (!SkipCursorToBlock(Cursor&: F.Stream, BlockID: EXTENSION_BLOCK_ID)) {
4787 if (llvm::Error Err = ReadExtensionBlock(F)) {
4788 Error(Err: std::move(Err));
4789 return Failure;
4790 }
4791 }
4792
4793 // Once read, set the ModuleFile bit base offset and update the size in
4794 // bits of all files we've seen.
4795 F.GlobalBitOffset = TotalModulesSizeInBits;
4796 TotalModulesSizeInBits += F.SizeInBits;
4797 GlobalBitOffsetsMap.insert(Val: std::make_pair(x&: F.GlobalBitOffset, y: &F));
4798 }
4799
4800 // Preload source locations and interesting indentifiers.
4801 for (ImportedModule &M : Loaded) {
4802 ModuleFile &F = *M.Mod;
4803
4804 // Map the original source file ID into the ID space of the current
4805 // compilation.
4806 if (F.OriginalSourceFileID.isValid())
4807 F.OriginalSourceFileID = TranslateFileID(F, FID: F.OriginalSourceFileID);
4808
4809 for (auto Offset : F.PreloadIdentifierOffsets) {
4810 const unsigned char *Data = F.IdentifierTableData + Offset;
4811
4812 ASTIdentifierLookupTrait Trait(*this, F);
4813 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
4814 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
4815
4816 IdentifierInfo *II;
4817 if (!PP.getLangOpts().CPlusPlus) {
4818 // Identifiers present in both the module file and the importing
4819 // instance are marked out-of-date so that they can be deserialized
4820 // on next use via ASTReader::updateOutOfDateIdentifier().
4821 // Identifiers present in the module file but not in the importing
4822 // instance are ignored for now, preventing growth of the identifier
4823 // table. They will be deserialized on first use via ASTReader::get().
4824 auto It = PP.getIdentifierTable().find(Name: Key);
4825 if (It == PP.getIdentifierTable().end())
4826 continue;
4827 II = It->second;
4828 } else {
4829 // With C++ modules, not many identifiers are considered interesting.
4830 // All identifiers in the module file can be placed into the identifier
4831 // table of the importing instance and marked as out-of-date. This makes
4832 // ASTReader::get() a no-op, and deserialization will take place on
4833 // first/next use via ASTReader::updateOutOfDateIdentifier().
4834 II = &PP.getIdentifierTable().getOwn(Name: Key);
4835 }
4836
4837 II->setOutOfDate(true);
4838
4839 // Mark this identifier as being from an AST file so that we can track
4840 // whether we need to serialize it.
4841 markIdentifierFromAST(Reader&: *this, II&: *II, /*IsModule=*/true);
4842
4843 // Associate the ID with the identifier so that the writer can reuse it.
4844 auto ID = Trait.ReadIdentifierID(d: Data + KeyDataLen.first);
4845 SetIdentifierInfo(ID, II);
4846 }
4847 }
4848
4849 // Builtins and library builtins have already been initialized. Mark all
4850 // identifiers as out-of-date, so that they are deserialized on first use.
4851 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4852 for (auto &Id : PP.getIdentifierTable())
4853 Id.second->setOutOfDate(true);
4854
4855 // Mark selectors as out of date.
4856 for (const auto &Sel : SelectorGeneration)
4857 SelectorOutOfDate[Sel.first] = true;
4858
4859 // Setup the import locations and notify the module manager that we've
4860 // committed to these module files.
4861 for (ImportedModule &M : Loaded) {
4862 ModuleFile &F = *M.Mod;
4863
4864 ModuleMgr.moduleFileAccepted(MF: &F);
4865
4866 // Set the import location.
4867 F.DirectImportLoc = ImportLoc;
4868 // FIXME: We assume that locations from PCH / preamble do not need
4869 // any translation.
4870 if (!M.ImportedBy)
4871 F.ImportLoc = M.ImportLoc;
4872 else
4873 F.ImportLoc = TranslateSourceLocation(ModuleFile&: *M.ImportedBy, Loc: M.ImportLoc);
4874 }
4875
4876 // Resolve any unresolved module exports.
4877 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4878 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4879 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: *Unresolved.File,LocalID: Unresolved.ID);
4880 Module *ResolvedMod = getSubmodule(GlobalID);
4881
4882 switch (Unresolved.Kind) {
4883 case UnresolvedModuleRef::Conflict:
4884 if (ResolvedMod) {
4885 Module::Conflict Conflict;
4886 Conflict.Other = ResolvedMod;
4887 Conflict.Message = Unresolved.String.str();
4888 Unresolved.Mod->Conflicts.push_back(x: Conflict);
4889 }
4890 continue;
4891
4892 case UnresolvedModuleRef::Import:
4893 if (ResolvedMod)
4894 Unresolved.Mod->Imports.insert(X: ResolvedMod);
4895 continue;
4896
4897 case UnresolvedModuleRef::Affecting:
4898 if (ResolvedMod)
4899 Unresolved.Mod->AffectingClangModules.insert(X: ResolvedMod);
4900 continue;
4901
4902 case UnresolvedModuleRef::Export:
4903 if (ResolvedMod || Unresolved.IsWildcard)
4904 Unresolved.Mod->Exports.push_back(
4905 Elt: Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4906 continue;
4907 }
4908 }
4909 UnresolvedModuleRefs.clear();
4910
4911 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4912 // Might be unnecessary as use declarations are only used to build the
4913 // module itself.
4914
4915 if (ContextObj)
4916 InitializeContext();
4917
4918 if (SemaObj)
4919 UpdateSema();
4920
4921 if (DeserializationListener)
4922 DeserializationListener->ReaderInitialized(Reader: this);
4923
4924 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4925 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4926 // If this AST file is a precompiled preamble, then set the
4927 // preamble file ID of the source manager to the file source file
4928 // from which the preamble was built.
4929 if (Type == MK_Preamble) {
4930 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4931 } else if (Type == MK_MainFile) {
4932 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4933 }
4934 }
4935
4936 // For any Objective-C class definitions we have already loaded, make sure
4937 // that we load any additional categories.
4938 if (ContextObj) {
4939 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4940 loadObjCCategories(ID: ObjCClassesLoaded[I]->getGlobalID(),
4941 D: ObjCClassesLoaded[I], PreviousGeneration);
4942 }
4943 }
4944
4945 const HeaderSearchOptions &HSOpts =
4946 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4947 if (HSOpts.ModulesValidateOncePerBuildSession) {
4948 // Now we are certain that the module and all modules it depends on are
4949 // up-to-date. For implicitly-built module files, ensure the corresponding
4950 // timestamp files are up-to-date in this build session.
4951 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4952 ImportedModule &M = Loaded[I];
4953 if (M.Mod->Kind == MK_ImplicitModule &&
4954 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4955 getModuleManager().getModuleCache().updateModuleTimestamp(
4956 ModuleFilename: M.Mod->FileName);
4957 }
4958 }
4959
4960 return Success;
4961}
4962
4963static ASTFileSignature readASTFileSignature(StringRef PCH);
4964
4965/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.
4966static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4967 // FIXME checking magic headers is done in other places such as
4968 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4969 // always done the same. Unify it all with a helper.
4970 if (!Stream.canSkipToPos(pos: 4))
4971 return llvm::createStringError(
4972 EC: std::errc::illegal_byte_sequence,
4973 Fmt: "file too small to contain precompiled file magic");
4974 for (unsigned C : {'C', 'P', 'C', 'H'})
4975 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(NumBits: 8)) {
4976 if (Res.get() != C)
4977 return llvm::createStringError(
4978 EC: std::errc::illegal_byte_sequence,
4979 Fmt: "file doesn't start with precompiled file magic");
4980 } else
4981 return Res.takeError();
4982 return llvm::Error::success();
4983}
4984
4985static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4986 switch (Kind) {
4987 case MK_PCH:
4988 return 0; // PCH
4989 case MK_ImplicitModule:
4990 case MK_ExplicitModule:
4991 case MK_PrebuiltModule:
4992 return 1; // module
4993 case MK_MainFile:
4994 case MK_Preamble:
4995 return 2; // main source file
4996 }
4997 llvm_unreachable("unknown module kind");
4998}
4999
5000ASTReader::ASTReadResult
5001ASTReader::ReadASTCore(StringRef FileName,
5002 ModuleKind Type,
5003 SourceLocation ImportLoc,
5004 ModuleFile *ImportedBy,
5005 SmallVectorImpl<ImportedModule> &Loaded,
5006 off_t ExpectedSize, time_t ExpectedModTime,
5007 ASTFileSignature ExpectedSignature,
5008 unsigned ClientLoadCapabilities) {
5009 ModuleFile *M;
5010 std::string ErrorStr;
5011 ModuleManager::AddModuleResult AddResult
5012 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
5013 Generation: getGeneration(), ExpectedSize, ExpectedModTime,
5014 ExpectedSignature, ReadSignature: readASTFileSignature,
5015 Module&: M, ErrorStr);
5016
5017 switch (AddResult) {
5018 case ModuleManager::AlreadyLoaded:
5019 Diag(DiagID: diag::remark_module_import)
5020 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
5021 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
5022 return Success;
5023
5024 case ModuleManager::NewlyLoaded:
5025 // Load module file below.
5026 break;
5027
5028 case ModuleManager::Missing:
5029 // The module file was missing; if the client can handle that, return
5030 // it.
5031 if (ClientLoadCapabilities & ARR_Missing)
5032 return Missing;
5033
5034 // Otherwise, return an error.
5035 Diag(DiagID: diag::err_ast_file_not_found)
5036 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5037 << ErrorStr;
5038 return Failure;
5039
5040 case ModuleManager::OutOfDate:
5041 // We couldn't load the module file because it is out-of-date. If the
5042 // client can handle out-of-date, return it.
5043 if (ClientLoadCapabilities & ARR_OutOfDate)
5044 return OutOfDate;
5045
5046 // Otherwise, return an error.
5047 Diag(DiagID: diag::err_ast_file_out_of_date)
5048 << moduleKindForDiagnostic(Kind: Type) << FileName << !ErrorStr.empty()
5049 << ErrorStr;
5050 return Failure;
5051 }
5052
5053 assert(M && "Missing module file");
5054
5055 bool ShouldFinalizePCM = false;
5056 auto FinalizeOrDropPCM = llvm::make_scope_exit(F: [&]() {
5057 auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();
5058 if (ShouldFinalizePCM)
5059 MC.finalizePCM(Filename: FileName);
5060 else
5061 MC.tryToDropPCM(Filename: FileName);
5062 });
5063 ModuleFile &F = *M;
5064 BitstreamCursor &Stream = F.Stream;
5065 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(Buffer: *F.Buffer));
5066 F.SizeInBits = F.Buffer->getBufferSize() * 8;
5067
5068 // Sniff for the signature.
5069 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5070 Diag(DiagID: diag::err_ast_file_invalid)
5071 << moduleKindForDiagnostic(Kind: Type) << FileName << std::move(Err);
5072 return Failure;
5073 }
5074
5075 // This is used for compatibility with older PCH formats.
5076 bool HaveReadControlBlock = false;
5077 while (true) {
5078 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5079 if (!MaybeEntry) {
5080 Error(Err: MaybeEntry.takeError());
5081 return Failure;
5082 }
5083 llvm::BitstreamEntry Entry = MaybeEntry.get();
5084
5085 switch (Entry.Kind) {
5086 case llvm::BitstreamEntry::Error:
5087 case llvm::BitstreamEntry::Record:
5088 case llvm::BitstreamEntry::EndBlock:
5089 Error(Msg: "invalid record at top-level of AST file");
5090 return Failure;
5091
5092 case llvm::BitstreamEntry::SubBlock:
5093 break;
5094 }
5095
5096 switch (Entry.ID) {
5097 case CONTROL_BLOCK_ID:
5098 HaveReadControlBlock = true;
5099 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
5100 case Success:
5101 // Check that we didn't try to load a non-module AST file as a module.
5102 //
5103 // FIXME: Should we also perform the converse check? Loading a module as
5104 // a PCH file sort of works, but it's a bit wonky.
5105 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
5106 Type == MK_PrebuiltModule) &&
5107 F.ModuleName.empty()) {
5108 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
5109 if (Result != OutOfDate ||
5110 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
5111 Diag(DiagID: diag::err_module_file_not_module) << FileName;
5112 return Result;
5113 }
5114 break;
5115
5116 case Failure: return Failure;
5117 case Missing: return Missing;
5118 case OutOfDate: return OutOfDate;
5119 case VersionMismatch: return VersionMismatch;
5120 case ConfigurationMismatch: return ConfigurationMismatch;
5121 case HadErrors: return HadErrors;
5122 }
5123 break;
5124
5125 case AST_BLOCK_ID:
5126 if (!HaveReadControlBlock) {
5127 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
5128 Diag(DiagID: diag::err_ast_file_version_too_old)
5129 << moduleKindForDiagnostic(Kind: Type) << FileName;
5130 return VersionMismatch;
5131 }
5132
5133 // Record that we've loaded this module.
5134 Loaded.push_back(Elt: ImportedModule(M, ImportedBy, ImportLoc));
5135 ShouldFinalizePCM = true;
5136 return Success;
5137
5138 default:
5139 if (llvm::Error Err = Stream.SkipBlock()) {
5140 Error(Err: std::move(Err));
5141 return Failure;
5142 }
5143 break;
5144 }
5145 }
5146
5147 llvm_unreachable("unexpected break; expected return");
5148}
5149
5150ASTReader::ASTReadResult
5151ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
5152 unsigned ClientLoadCapabilities) {
5153 const HeaderSearchOptions &HSOpts =
5154 PP.getHeaderSearchInfo().getHeaderSearchOpts();
5155 bool AllowCompatibleConfigurationMismatch =
5156 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
5157 bool DisableValidation = shouldDisableValidationForFile(M: F);
5158
5159 ASTReadResult Result = readUnhashedControlBlockImpl(
5160 F: &F, StreamData: F.Data, Filename: F.FileName, ClientLoadCapabilities,
5161 AllowCompatibleConfigurationMismatch, Listener: Listener.get(),
5162 ValidateDiagnosticOptions: WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
5163
5164 // If F was directly imported by another module, it's implicitly validated by
5165 // the importing module.
5166 if (DisableValidation || WasImportedBy ||
5167 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
5168 return Success;
5169
5170 if (Result == Failure) {
5171 Error(Msg: "malformed block record in AST file");
5172 return Failure;
5173 }
5174
5175 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
5176 // If this module has already been finalized in the ModuleCache, we're stuck
5177 // with it; we can only load a single version of each module.
5178 //
5179 // This can happen when a module is imported in two contexts: in one, as a
5180 // user module; in another, as a system module (due to an import from
5181 // another module marked with the [system] flag). It usually indicates a
5182 // bug in the module map: this module should also be marked with [system].
5183 //
5184 // If -Wno-system-headers (the default), and the first import is as a
5185 // system module, then validation will fail during the as-user import,
5186 // since -Werror flags won't have been validated. However, it's reasonable
5187 // to treat this consistently as a system module.
5188 //
5189 // If -Wsystem-headers, the PCM on disk was built with
5190 // -Wno-system-headers, and the first import is as a user module, then
5191 // validation will fail during the as-system import since the PCM on disk
5192 // doesn't guarantee that -Werror was respected. However, the -Werror
5193 // flags were checked during the initial as-user import.
5194 if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(
5195 Filename: F.FileName)) {
5196 Diag(DiagID: diag::warn_module_system_bit_conflict) << F.FileName;
5197 return Success;
5198 }
5199 }
5200
5201 return Result;
5202}
5203
5204ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
5205 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
5206 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
5207 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
5208 // Initialize a stream.
5209 BitstreamCursor Stream(StreamData);
5210
5211 // Sniff for the signature.
5212 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5213 // FIXME this drops the error on the floor.
5214 consumeError(Err: std::move(Err));
5215 return Failure;
5216 }
5217
5218 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5219 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5220 return Failure;
5221
5222 // Read all of the records in the options block.
5223 RecordData Record;
5224 ASTReadResult Result = Success;
5225 while (true) {
5226 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5227 if (!MaybeEntry) {
5228 // FIXME this drops the error on the floor.
5229 consumeError(Err: MaybeEntry.takeError());
5230 return Failure;
5231 }
5232 llvm::BitstreamEntry Entry = MaybeEntry.get();
5233
5234 switch (Entry.Kind) {
5235 case llvm::BitstreamEntry::Error:
5236 case llvm::BitstreamEntry::SubBlock:
5237 return Failure;
5238
5239 case llvm::BitstreamEntry::EndBlock:
5240 return Result;
5241
5242 case llvm::BitstreamEntry::Record:
5243 // The interesting case.
5244 break;
5245 }
5246
5247 // Read and process a record.
5248 Record.clear();
5249 StringRef Blob;
5250 Expected<unsigned> MaybeRecordType =
5251 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5252 if (!MaybeRecordType) {
5253 // FIXME this drops the error.
5254 return Failure;
5255 }
5256 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
5257 case SIGNATURE:
5258 if (F) {
5259 F->Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5260 assert(F->Signature != ASTFileSignature::createDummy() &&
5261 "Dummy AST file signature not backpatched in ASTWriter.");
5262 }
5263 break;
5264 case AST_BLOCK_HASH:
5265 if (F) {
5266 F->ASTBlockHash = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5267 assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&
5268 "Dummy AST block hash not backpatched in ASTWriter.");
5269 }
5270 break;
5271 case DIAGNOSTIC_OPTIONS: {
5272 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5273 if (Listener && ValidateDiagnosticOptions &&
5274 !AllowCompatibleConfigurationMismatch &&
5275 ParseDiagnosticOptions(Record, ModuleFilename: Filename, Complain, Listener&: *Listener))
5276 Result = OutOfDate; // Don't return early. Read the signature.
5277 break;
5278 }
5279 case HEADER_SEARCH_PATHS: {
5280 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5281 if (Listener && !AllowCompatibleConfigurationMismatch &&
5282 ParseHeaderSearchPaths(Record, Complain, Listener&: *Listener))
5283 Result = ConfigurationMismatch;
5284 break;
5285 }
5286 case DIAG_PRAGMA_MAPPINGS:
5287 if (!F)
5288 break;
5289 if (F->PragmaDiagMappings.empty())
5290 F->PragmaDiagMappings.swap(RHS&: Record);
5291 else
5292 F->PragmaDiagMappings.insert(I: F->PragmaDiagMappings.end(),
5293 From: Record.begin(), To: Record.end());
5294 break;
5295 case HEADER_SEARCH_ENTRY_USAGE:
5296 if (F)
5297 F->SearchPathUsage = ReadBitVector(Record, Blob);
5298 break;
5299 case VFS_USAGE:
5300 if (F)
5301 F->VFSUsage = ReadBitVector(Record, Blob);
5302 break;
5303 }
5304 }
5305}
5306
5307/// Parse a record and blob containing module file extension metadata.
5308static bool parseModuleFileExtensionMetadata(
5309 const SmallVectorImpl<uint64_t> &Record,
5310 StringRef Blob,
5311 ModuleFileExtensionMetadata &Metadata) {
5312 if (Record.size() < 4) return true;
5313
5314 Metadata.MajorVersion = Record[0];
5315 Metadata.MinorVersion = Record[1];
5316
5317 unsigned BlockNameLen = Record[2];
5318 unsigned UserInfoLen = Record[3];
5319
5320 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5321
5322 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5323 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5324 Blob.data() + BlockNameLen + UserInfoLen);
5325 return false;
5326}
5327
5328llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5329 BitstreamCursor &Stream = F.Stream;
5330
5331 RecordData Record;
5332 while (true) {
5333 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5334 if (!MaybeEntry)
5335 return MaybeEntry.takeError();
5336 llvm::BitstreamEntry Entry = MaybeEntry.get();
5337
5338 switch (Entry.Kind) {
5339 case llvm::BitstreamEntry::SubBlock:
5340 if (llvm::Error Err = Stream.SkipBlock())
5341 return Err;
5342 continue;
5343 case llvm::BitstreamEntry::EndBlock:
5344 return llvm::Error::success();
5345 case llvm::BitstreamEntry::Error:
5346 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
5347 Fmt: "malformed block record in AST file");
5348 case llvm::BitstreamEntry::Record:
5349 break;
5350 }
5351
5352 Record.clear();
5353 StringRef Blob;
5354 Expected<unsigned> MaybeRecCode =
5355 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5356 if (!MaybeRecCode)
5357 return MaybeRecCode.takeError();
5358 switch (MaybeRecCode.get()) {
5359 case EXTENSION_METADATA: {
5360 ModuleFileExtensionMetadata Metadata;
5361 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5362 return llvm::createStringError(
5363 EC: std::errc::illegal_byte_sequence,
5364 Fmt: "malformed EXTENSION_METADATA in AST file");
5365
5366 // Find a module file extension with this block name.
5367 auto Known = ModuleFileExtensions.find(Key: Metadata.BlockName);
5368 if (Known == ModuleFileExtensions.end()) break;
5369
5370 // Form a reader.
5371 if (auto Reader = Known->second->createExtensionReader(Metadata, Reader&: *this,
5372 Mod&: F, Stream)) {
5373 F.ExtensionReaders.push_back(x: std::move(Reader));
5374 }
5375
5376 break;
5377 }
5378 }
5379 }
5380
5381 llvm_unreachable("ReadExtensionBlock should return from while loop");
5382}
5383
5384void ASTReader::InitializeContext() {
5385 assert(ContextObj && "no context to initialize");
5386 ASTContext &Context = *ContextObj;
5387
5388 // If there's a listener, notify them that we "read" the translation unit.
5389 if (DeserializationListener)
5390 DeserializationListener->DeclRead(
5391 ID: GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),
5392 D: Context.getTranslationUnitDecl());
5393
5394 // FIXME: Find a better way to deal with collisions between these
5395 // built-in types. Right now, we just ignore the problem.
5396
5397 // Load the special types.
5398 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5399 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5400 if (!Context.CFConstantStringTypeDecl)
5401 Context.setCFConstantStringType(GetType(ID: String));
5402 }
5403
5404 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5405 QualType FileType = GetType(ID: File);
5406 if (FileType.isNull()) {
5407 Error(Msg: "FILE type is NULL");
5408 return;
5409 }
5410
5411 if (!Context.FILEDecl) {
5412 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5413 Context.setFILEDecl(Typedef->getDecl());
5414 else {
5415 const TagType *Tag = FileType->getAs<TagType>();
5416 if (!Tag) {
5417 Error(Msg: "Invalid FILE type in AST file");
5418 return;
5419 }
5420 Context.setFILEDecl(Tag->getDecl());
5421 }
5422 }
5423 }
5424
5425 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5426 QualType Jmp_bufType = GetType(ID: Jmp_buf);
5427 if (Jmp_bufType.isNull()) {
5428 Error(Msg: "jmp_buf type is NULL");
5429 return;
5430 }
5431
5432 if (!Context.jmp_bufDecl) {
5433 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5434 Context.setjmp_bufDecl(Typedef->getDecl());
5435 else {
5436 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5437 if (!Tag) {
5438 Error(Msg: "Invalid jmp_buf type in AST file");
5439 return;
5440 }
5441 Context.setjmp_bufDecl(Tag->getDecl());
5442 }
5443 }
5444 }
5445
5446 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5447 QualType Sigjmp_bufType = GetType(ID: Sigjmp_buf);
5448 if (Sigjmp_bufType.isNull()) {
5449 Error(Msg: "sigjmp_buf type is NULL");
5450 return;
5451 }
5452
5453 if (!Context.sigjmp_bufDecl) {
5454 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5455 Context.setsigjmp_bufDecl(Typedef->getDecl());
5456 else {
5457 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5458 assert(Tag && "Invalid sigjmp_buf type in AST file");
5459 Context.setsigjmp_bufDecl(Tag->getDecl());
5460 }
5461 }
5462 }
5463
5464 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5465 if (Context.ObjCIdRedefinitionType.isNull())
5466 Context.ObjCIdRedefinitionType = GetType(ID: ObjCIdRedef);
5467 }
5468
5469 if (TypeID ObjCClassRedef =
5470 SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5471 if (Context.ObjCClassRedefinitionType.isNull())
5472 Context.ObjCClassRedefinitionType = GetType(ID: ObjCClassRedef);
5473 }
5474
5475 if (TypeID ObjCSelRedef =
5476 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5477 if (Context.ObjCSelRedefinitionType.isNull())
5478 Context.ObjCSelRedefinitionType = GetType(ID: ObjCSelRedef);
5479 }
5480
5481 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5482 QualType Ucontext_tType = GetType(ID: Ucontext_t);
5483 if (Ucontext_tType.isNull()) {
5484 Error(Msg: "ucontext_t type is NULL");
5485 return;
5486 }
5487
5488 if (!Context.ucontext_tDecl) {
5489 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5490 Context.setucontext_tDecl(Typedef->getDecl());
5491 else {
5492 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5493 assert(Tag && "Invalid ucontext_t type in AST file");
5494 Context.setucontext_tDecl(Tag->getDecl());
5495 }
5496 }
5497 }
5498 }
5499
5500 ReadPragmaDiagnosticMappings(Diag&: Context.getDiagnostics());
5501
5502 // If there were any CUDA special declarations, deserialize them.
5503 if (!CUDASpecialDeclRefs.empty()) {
5504 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5505 Context.setcudaConfigureCallDecl(
5506 cast<FunctionDecl>(Val: GetDecl(ID: CUDASpecialDeclRefs[0])));
5507 }
5508
5509 // Re-export any modules that were imported by a non-module AST file.
5510 // FIXME: This does not make macro-only imports visible again.
5511 for (auto &Import : PendingImportedModules) {
5512 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
5513 makeModuleVisible(Mod: Imported, NameVisibility: Module::AllVisible,
5514 /*ImportLoc=*/Import.ImportLoc);
5515 if (Import.ImportLoc.isValid())
5516 PP.makeModuleVisible(M: Imported, Loc: Import.ImportLoc);
5517 // This updates visibility for Preprocessor only. For Sema, which can be
5518 // nullptr here, we do the same later, in UpdateSema().
5519 }
5520 }
5521
5522 // Hand off these modules to Sema.
5523 PendingImportedModulesSema.append(RHS: PendingImportedModules);
5524 PendingImportedModules.clear();
5525}
5526
5527void ASTReader::finalizeForWriting() {
5528 // Nothing to do for now.
5529}
5530
5531/// Reads and return the signature record from \p PCH's control block, or
5532/// else returns 0.
5533static ASTFileSignature readASTFileSignature(StringRef PCH) {
5534 BitstreamCursor Stream(PCH);
5535 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5536 // FIXME this drops the error on the floor.
5537 consumeError(Err: std::move(Err));
5538 return ASTFileSignature();
5539 }
5540
5541 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5542 if (SkipCursorToBlock(Cursor&: Stream, BlockID: UNHASHED_CONTROL_BLOCK_ID))
5543 return ASTFileSignature();
5544
5545 // Scan for SIGNATURE inside the diagnostic options block.
5546 ASTReader::RecordData Record;
5547 while (true) {
5548 Expected<llvm::BitstreamEntry> MaybeEntry =
5549 Stream.advanceSkippingSubblocks();
5550 if (!MaybeEntry) {
5551 // FIXME this drops the error on the floor.
5552 consumeError(Err: MaybeEntry.takeError());
5553 return ASTFileSignature();
5554 }
5555 llvm::BitstreamEntry Entry = MaybeEntry.get();
5556
5557 if (Entry.Kind != llvm::BitstreamEntry::Record)
5558 return ASTFileSignature();
5559
5560 Record.clear();
5561 StringRef Blob;
5562 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5563 if (!MaybeRecord) {
5564 // FIXME this drops the error on the floor.
5565 consumeError(Err: MaybeRecord.takeError());
5566 return ASTFileSignature();
5567 }
5568 if (SIGNATURE == MaybeRecord.get()) {
5569 auto Signature = ASTFileSignature::create(First: Blob.begin(), Last: Blob.end());
5570 assert(Signature != ASTFileSignature::createDummy() &&
5571 "Dummy AST file signature not backpatched in ASTWriter.");
5572 return Signature;
5573 }
5574 }
5575}
5576
5577/// Retrieve the name of the original source file name
5578/// directly from the AST file, without actually loading the AST
5579/// file.
5580std::string ASTReader::getOriginalSourceFile(
5581 const std::string &ASTFileName, FileManager &FileMgr,
5582 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5583 // Open the AST file.
5584 auto Buffer = FileMgr.getBufferForFile(Filename: ASTFileName, /*IsVolatile=*/isVolatile: false,
5585 /*RequiresNullTerminator=*/false,
5586 /*MaybeLimit=*/std::nullopt,
5587 /*IsText=*/false);
5588 if (!Buffer) {
5589 Diags.Report(DiagID: diag::err_fe_unable_to_read_pch_file)
5590 << ASTFileName << Buffer.getError().message();
5591 return std::string();
5592 }
5593
5594 // Initialize the stream
5595 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(Buffer: **Buffer));
5596
5597 // Sniff for the signature.
5598 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5599 Diags.Report(DiagID: diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5600 return std::string();
5601 }
5602
5603 // Scan for the CONTROL_BLOCK_ID block.
5604 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID)) {
5605 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5606 return std::string();
5607 }
5608
5609 // Scan for ORIGINAL_FILE inside the control block.
5610 RecordData Record;
5611 while (true) {
5612 Expected<llvm::BitstreamEntry> MaybeEntry =
5613 Stream.advanceSkippingSubblocks();
5614 if (!MaybeEntry) {
5615 // FIXME this drops errors on the floor.
5616 consumeError(Err: MaybeEntry.takeError());
5617 return std::string();
5618 }
5619 llvm::BitstreamEntry Entry = MaybeEntry.get();
5620
5621 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5622 return std::string();
5623
5624 if (Entry.Kind != llvm::BitstreamEntry::Record) {
5625 Diags.Report(DiagID: diag::err_fe_pch_malformed_block) << ASTFileName;
5626 return std::string();
5627 }
5628
5629 Record.clear();
5630 StringRef Blob;
5631 Expected<unsigned> MaybeRecord = Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5632 if (!MaybeRecord) {
5633 // FIXME this drops the errors on the floor.
5634 consumeError(Err: MaybeRecord.takeError());
5635 return std::string();
5636 }
5637 if (ORIGINAL_FILE == MaybeRecord.get())
5638 return Blob.str();
5639 }
5640}
5641
5642namespace {
5643
5644 class SimplePCHValidator : public ASTReaderListener {
5645 const LangOptions &ExistingLangOpts;
5646 const TargetOptions &ExistingTargetOpts;
5647 const PreprocessorOptions &ExistingPPOpts;
5648 std::string ExistingModuleCachePath;
5649 FileManager &FileMgr;
5650 bool StrictOptionMatches;
5651
5652 public:
5653 SimplePCHValidator(const LangOptions &ExistingLangOpts,
5654 const TargetOptions &ExistingTargetOpts,
5655 const PreprocessorOptions &ExistingPPOpts,
5656 StringRef ExistingModuleCachePath, FileManager &FileMgr,
5657 bool StrictOptionMatches)
5658 : ExistingLangOpts(ExistingLangOpts),
5659 ExistingTargetOpts(ExistingTargetOpts),
5660 ExistingPPOpts(ExistingPPOpts),
5661 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),
5662 StrictOptionMatches(StrictOptionMatches) {}
5663
5664 bool ReadLanguageOptions(const LangOptions &LangOpts,
5665 StringRef ModuleFilename, bool Complain,
5666 bool AllowCompatibleDifferences) override {
5667 return checkLanguageOptions(LangOpts: ExistingLangOpts, ExistingLangOpts: LangOpts, ModuleFilename,
5668 Diags: nullptr, AllowCompatibleDifferences);
5669 }
5670
5671 bool ReadTargetOptions(const TargetOptions &TargetOpts,
5672 StringRef ModuleFilename, bool Complain,
5673 bool AllowCompatibleDifferences) override {
5674 return checkTargetOptions(TargetOpts: ExistingTargetOpts, ExistingTargetOpts: TargetOpts, ModuleFilename,
5675 Diags: nullptr, AllowCompatibleDifferences);
5676 }
5677
5678 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5679 StringRef ModuleFilename,
5680 StringRef SpecificModuleCachePath,
5681 bool Complain) override {
5682 return checkModuleCachePath(VFS&: FileMgr.getVirtualFileSystem(),
5683 SpecificModuleCachePath,
5684 ExistingModuleCachePath, ModuleFilename,
5685 Diags: nullptr, LangOpts: ExistingLangOpts, PPOpts: ExistingPPOpts);
5686 }
5687
5688 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5689 StringRef ModuleFilename, bool ReadMacros,
5690 bool Complain,
5691 std::string &SuggestedPredefines) override {
5692 return checkPreprocessorOptions(
5693 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,
5694 FileMgr, SuggestedPredefines, LangOpts: ExistingLangOpts,
5695 Validation: StrictOptionMatches ? OptionValidateStrictMatches
5696 : OptionValidateContradictions);
5697 }
5698 };
5699
5700} // namespace
5701
5702bool ASTReader::readASTFileControlBlock(
5703 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
5704 const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
5705 ASTReaderListener &Listener, bool ValidateDiagnosticOptions,
5706 unsigned ClientLoadCapabilities) {
5707 // Open the AST file.
5708 std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5709 llvm::MemoryBuffer *Buffer =
5710 ModCache.getInMemoryModuleCache().lookupPCM(Filename);
5711 if (!Buffer) {
5712 // FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5713 // read again later, but we do not have the context here to determine if it
5714 // is safe to change the result of InMemoryModuleCache::getPCMState().
5715
5716 // FIXME: This allows use of the VFS; we do not allow use of the
5717 // VFS when actually loading a module.
5718 auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5719 if (!BufferOrErr)
5720 return true;
5721 OwnedBuffer = std::move(*BufferOrErr);
5722 Buffer = OwnedBuffer.get();
5723 }
5724
5725 // Initialize the stream
5726 StringRef Bytes = PCHContainerRdr.ExtractPCH(Buffer: *Buffer);
5727 BitstreamCursor Stream(Bytes);
5728
5729 // Sniff for the signature.
5730 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5731 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
5732 return true;
5733 }
5734
5735 // Scan for the CONTROL_BLOCK_ID block.
5736 if (SkipCursorToBlock(Cursor&: Stream, BlockID: CONTROL_BLOCK_ID))
5737 return true;
5738
5739 bool NeedsInputFiles = Listener.needsInputFileVisitation();
5740 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5741 bool NeedsImports = Listener.needsImportVisitation();
5742 BitstreamCursor InputFilesCursor;
5743 uint64_t InputFilesOffsetBase = 0;
5744
5745 RecordData Record;
5746 std::string ModuleDir;
5747 bool DoneWithControlBlock = false;
5748 SmallString<0> PathBuf;
5749 PathBuf.reserve(N: 256);
5750 // Additional path buffer to use when multiple paths need to be resolved.
5751 // For example, when deserializing input files that contains a path that was
5752 // resolved from a vfs overlay and an external location.
5753 SmallString<0> AdditionalPathBuf;
5754 AdditionalPathBuf.reserve(N: 256);
5755 while (!DoneWithControlBlock) {
5756 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5757 if (!MaybeEntry) {
5758 // FIXME this drops the error on the floor.
5759 consumeError(Err: MaybeEntry.takeError());
5760 return true;
5761 }
5762 llvm::BitstreamEntry Entry = MaybeEntry.get();
5763
5764 switch (Entry.Kind) {
5765 case llvm::BitstreamEntry::SubBlock: {
5766 switch (Entry.ID) {
5767 case OPTIONS_BLOCK_ID: {
5768 std::string IgnoredSuggestedPredefines;
5769 if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,
5770 /*AllowCompatibleConfigurationMismatch*/ false,
5771 Listener, SuggestedPredefines&: IgnoredSuggestedPredefines) != Success)
5772 return true;
5773 break;
5774 }
5775
5776 case INPUT_FILES_BLOCK_ID:
5777 InputFilesCursor = Stream;
5778 if (llvm::Error Err = Stream.SkipBlock()) {
5779 // FIXME this drops the error on the floor.
5780 consumeError(Err: std::move(Err));
5781 return true;
5782 }
5783 if (NeedsInputFiles &&
5784 ReadBlockAbbrevs(Cursor&: InputFilesCursor, BlockID: INPUT_FILES_BLOCK_ID))
5785 return true;
5786 InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
5787 break;
5788
5789 default:
5790 if (llvm::Error Err = Stream.SkipBlock()) {
5791 // FIXME this drops the error on the floor.
5792 consumeError(Err: std::move(Err));
5793 return true;
5794 }
5795 break;
5796 }
5797
5798 continue;
5799 }
5800
5801 case llvm::BitstreamEntry::EndBlock:
5802 DoneWithControlBlock = true;
5803 break;
5804
5805 case llvm::BitstreamEntry::Error:
5806 return true;
5807
5808 case llvm::BitstreamEntry::Record:
5809 break;
5810 }
5811
5812 if (DoneWithControlBlock) break;
5813
5814 Record.clear();
5815 StringRef Blob;
5816 Expected<unsigned> MaybeRecCode =
5817 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5818 if (!MaybeRecCode) {
5819 // FIXME this drops the error.
5820 return Failure;
5821 }
5822 switch ((ControlRecordTypes)MaybeRecCode.get()) {
5823 case METADATA:
5824 if (Record[0] != VERSION_MAJOR)
5825 return true;
5826 if (Listener.ReadFullVersionInformation(FullVersion: Blob))
5827 return true;
5828 break;
5829 case MODULE_NAME:
5830 Listener.ReadModuleName(ModuleName: Blob);
5831 break;
5832 case MODULE_DIRECTORY:
5833 ModuleDir = std::string(Blob);
5834 break;
5835 case MODULE_MAP_FILE: {
5836 unsigned Idx = 0;
5837 std::string PathStr = ReadString(Record, Idx);
5838 auto Path = ResolveImportedPath(Buf&: PathBuf, Path: PathStr, Prefix: ModuleDir);
5839 Listener.ReadModuleMapFile(ModuleMapPath: *Path);
5840 break;
5841 }
5842 case INPUT_FILE_OFFSETS: {
5843 if (!NeedsInputFiles)
5844 break;
5845
5846 unsigned NumInputFiles = Record[0];
5847 unsigned NumUserFiles = Record[1];
5848 const llvm::support::unaligned_uint64_t *InputFileOffs =
5849 (const llvm::support::unaligned_uint64_t *)Blob.data();
5850 for (unsigned I = 0; I != NumInputFiles; ++I) {
5851 // Go find this input file.
5852 bool isSystemFile = I >= NumUserFiles;
5853
5854 if (isSystemFile && !NeedsSystemInputFiles)
5855 break; // the rest are system input files
5856
5857 BitstreamCursor &Cursor = InputFilesCursor;
5858 SavedStreamPosition SavedPosition(Cursor);
5859 if (llvm::Error Err =
5860 Cursor.JumpToBit(BitNo: InputFilesOffsetBase + InputFileOffs[I])) {
5861 // FIXME this drops errors on the floor.
5862 consumeError(Err: std::move(Err));
5863 }
5864
5865 Expected<unsigned> MaybeCode = Cursor.ReadCode();
5866 if (!MaybeCode) {
5867 // FIXME this drops errors on the floor.
5868 consumeError(Err: MaybeCode.takeError());
5869 }
5870 unsigned Code = MaybeCode.get();
5871
5872 RecordData Record;
5873 StringRef Blob;
5874 bool shouldContinue = false;
5875 Expected<unsigned> MaybeRecordType =
5876 Cursor.readRecord(AbbrevID: Code, Vals&: Record, Blob: &Blob);
5877 if (!MaybeRecordType) {
5878 // FIXME this drops errors on the floor.
5879 consumeError(Err: MaybeRecordType.takeError());
5880 }
5881 switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5882 case INPUT_FILE_HASH:
5883 break;
5884 case INPUT_FILE:
5885 bool Overridden = static_cast<bool>(Record[3]);
5886 auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =
5887 getUnresolvedInputFilenames(Record, InputBlob: Blob);
5888 auto FilenameAsRequestedBuf = ResolveImportedPath(
5889 Buf&: PathBuf, Path: UnresolvedFilenameAsRequested, Prefix: ModuleDir);
5890 StringRef Filename;
5891 if (UnresolvedFilename.empty())
5892 Filename = *FilenameAsRequestedBuf;
5893 else {
5894 auto FilenameBuf = ResolveImportedPath(
5895 Buf&: AdditionalPathBuf, Path: UnresolvedFilename, Prefix: ModuleDir);
5896 Filename = *FilenameBuf;
5897 }
5898 shouldContinue = Listener.visitInputFile(
5899 FilenameAsRequested: *FilenameAsRequestedBuf, Filename, isSystem: isSystemFile, isOverridden: Overridden,
5900 /*IsExplicitModule=*/isExplicitModule: false);
5901 break;
5902 }
5903 if (!shouldContinue)
5904 break;
5905 }
5906 break;
5907 }
5908
5909 case IMPORT: {
5910 if (!NeedsImports)
5911 break;
5912
5913 unsigned Idx = 0;
5914 // Read information about the AST file.
5915
5916 // Skip Kind
5917 Idx++;
5918
5919 // Skip ImportLoc
5920 Idx++;
5921
5922 StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);
5923
5924 bool IsStandardCXXModule = Record[Idx++];
5925
5926 // In C++20 Modules, we don't record the path to imported
5927 // modules in the BMI files.
5928 if (IsStandardCXXModule) {
5929 Listener.visitImport(ModuleName, /*Filename=*/"");
5930 continue;
5931 }
5932
5933 // Skip Size and ModTime.
5934 Idx += 1 + 1;
5935 // Skip signature.
5936 Blob = Blob.substr(Start: ASTFileSignature::size);
5937
5938 StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);
5939 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: FilenameStr, Prefix: ModuleDir);
5940 Listener.visitImport(ModuleName, Filename: *Filename);
5941 break;
5942 }
5943
5944 default:
5945 // No other validation to perform.
5946 break;
5947 }
5948 }
5949
5950 // Look for module file extension blocks, if requested.
5951 if (FindModuleFileExtensions) {
5952 BitstreamCursor SavedStream = Stream;
5953 while (!SkipCursorToBlock(Cursor&: Stream, BlockID: EXTENSION_BLOCK_ID)) {
5954 bool DoneWithExtensionBlock = false;
5955 while (!DoneWithExtensionBlock) {
5956 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5957 if (!MaybeEntry) {
5958 // FIXME this drops the error.
5959 return true;
5960 }
5961 llvm::BitstreamEntry Entry = MaybeEntry.get();
5962
5963 switch (Entry.Kind) {
5964 case llvm::BitstreamEntry::SubBlock:
5965 if (llvm::Error Err = Stream.SkipBlock()) {
5966 // FIXME this drops the error on the floor.
5967 consumeError(Err: std::move(Err));
5968 return true;
5969 }
5970 continue;
5971
5972 case llvm::BitstreamEntry::EndBlock:
5973 DoneWithExtensionBlock = true;
5974 continue;
5975
5976 case llvm::BitstreamEntry::Error:
5977 return true;
5978
5979 case llvm::BitstreamEntry::Record:
5980 break;
5981 }
5982
5983 Record.clear();
5984 StringRef Blob;
5985 Expected<unsigned> MaybeRecCode =
5986 Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
5987 if (!MaybeRecCode) {
5988 // FIXME this drops the error.
5989 return true;
5990 }
5991 switch (MaybeRecCode.get()) {
5992 case EXTENSION_METADATA: {
5993 ModuleFileExtensionMetadata Metadata;
5994 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5995 return true;
5996
5997 Listener.readModuleFileExtension(Metadata);
5998 break;
5999 }
6000 }
6001 }
6002 }
6003 Stream = std::move(SavedStream);
6004 }
6005
6006 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
6007 if (readUnhashedControlBlockImpl(
6008 F: nullptr, StreamData: Bytes, Filename, ClientLoadCapabilities,
6009 /*AllowCompatibleConfigurationMismatch*/ false, Listener: &Listener,
6010 ValidateDiagnosticOptions) != Success)
6011 return true;
6012
6013 return false;
6014}
6015
6016bool ASTReader::isAcceptableASTFile(
6017 StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,
6018 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
6019 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
6020 StringRef ExistingModuleCachePath, bool RequireStrictOptionMatches) {
6021 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
6022 ExistingModuleCachePath, FileMgr,
6023 RequireStrictOptionMatches);
6024 return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,
6025 /*FindModuleFileExtensions=*/false, Listener&: validator,
6026 /*ValidateDiagnosticOptions=*/true);
6027}
6028
6029llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
6030 unsigned ClientLoadCapabilities) {
6031 // Enter the submodule block.
6032 if (llvm::Error Err = F.Stream.EnterSubBlock(BlockID: SUBMODULE_BLOCK_ID))
6033 return Err;
6034
6035 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
6036 bool KnowsTopLevelModule = ModMap.findModule(Name: F.ModuleName) != nullptr;
6037 // If we don't know the top-level module, there's no point in doing qualified
6038 // lookup of its submodules; it won't find anything anywhere within this tree.
6039 // Let's skip that and avoid some string lookups.
6040 auto CreateModule = !KnowsTopLevelModule
6041 ? &ModuleMap::createModule
6042 : &ModuleMap::findOrCreateModuleFirst;
6043
6044 bool First = true;
6045 Module *CurrentModule = nullptr;
6046 RecordData Record;
6047 while (true) {
6048 Expected<llvm::BitstreamEntry> MaybeEntry =
6049 F.Stream.advanceSkippingSubblocks();
6050 if (!MaybeEntry)
6051 return MaybeEntry.takeError();
6052 llvm::BitstreamEntry Entry = MaybeEntry.get();
6053
6054 switch (Entry.Kind) {
6055 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6056 case llvm::BitstreamEntry::Error:
6057 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6058 Fmt: "malformed block record in AST file");
6059 case llvm::BitstreamEntry::EndBlock:
6060 return llvm::Error::success();
6061 case llvm::BitstreamEntry::Record:
6062 // The interesting case.
6063 break;
6064 }
6065
6066 // Read a record.
6067 StringRef Blob;
6068 Record.clear();
6069 Expected<unsigned> MaybeKind = F.Stream.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6070 if (!MaybeKind)
6071 return MaybeKind.takeError();
6072 unsigned Kind = MaybeKind.get();
6073
6074 if ((Kind == SUBMODULE_METADATA) != First)
6075 return llvm::createStringError(
6076 EC: std::errc::illegal_byte_sequence,
6077 Fmt: "submodule metadata record should be at beginning of block");
6078 First = false;
6079
6080 // Submodule information is only valid if we have a current module.
6081 // FIXME: Should we error on these cases?
6082 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
6083 Kind != SUBMODULE_DEFINITION)
6084 continue;
6085
6086 switch (Kind) {
6087 default: // Default behavior: ignore.
6088 break;
6089
6090 case SUBMODULE_DEFINITION: {
6091 if (Record.size() < 13)
6092 return llvm::createStringError(EC: std::errc::illegal_byte_sequence,
6093 Fmt: "malformed module definition");
6094
6095 StringRef Name = Blob;
6096 unsigned Idx = 0;
6097 SubmoduleID GlobalID = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6098 SubmoduleID Parent = getGlobalSubmoduleID(M&: F, LocalID: Record[Idx++]);
6099 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
6100 SourceLocation DefinitionLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6101 FileID InferredAllowedBy = ReadFileID(F, Record, Idx);
6102 bool IsFramework = Record[Idx++];
6103 bool IsExplicit = Record[Idx++];
6104 bool IsSystem = Record[Idx++];
6105 bool IsExternC = Record[Idx++];
6106 bool InferSubmodules = Record[Idx++];
6107 bool InferExplicitSubmodules = Record[Idx++];
6108 bool InferExportWildcard = Record[Idx++];
6109 bool ConfigMacrosExhaustive = Record[Idx++];
6110 bool ModuleMapIsPrivate = Record[Idx++];
6111 bool NamedModuleHasInit = Record[Idx++];
6112
6113 Module *ParentModule = nullptr;
6114 if (Parent)
6115 ParentModule = getSubmodule(GlobalID: Parent);
6116
6117 CurrentModule = std::invoke(fn&: CreateModule, args: &ModMap, args&: Name, args&: ParentModule,
6118 args&: IsFramework, args&: IsExplicit);
6119
6120 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
6121 if (GlobalIndex >= SubmodulesLoaded.size() ||
6122 SubmodulesLoaded[GlobalIndex])
6123 return llvm::createStringError(EC: std::errc::invalid_argument,
6124 Fmt: "too many submodules");
6125
6126 if (!ParentModule) {
6127 if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {
6128 // Don't emit module relocation error if we have -fno-validate-pch
6129 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
6130 DisableValidationForModuleKind::Module)) {
6131 assert(CurFile != F.File && "ModuleManager did not de-duplicate");
6132
6133 Diag(DiagID: diag::err_module_file_conflict)
6134 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
6135 << F.File.getName();
6136
6137 auto CurModMapFile =
6138 ModMap.getContainingModuleMapFile(Module: CurrentModule);
6139 auto ModMapFile = FileMgr.getOptionalFileRef(Filename: F.ModuleMapPath);
6140 if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)
6141 Diag(DiagID: diag::note_module_file_conflict)
6142 << CurModMapFile->getName() << ModMapFile->getName();
6143
6144 return llvm::make_error<AlreadyReportedDiagnosticError>();
6145 }
6146 }
6147
6148 F.DidReadTopLevelSubmodule = true;
6149 CurrentModule->setASTFile(F.File);
6150 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
6151 }
6152
6153 CurrentModule->Kind = Kind;
6154 // Note that we may be rewriting an existing location and it is important
6155 // to keep doing that. In particular, we would like to prefer a
6156 // `DefinitionLoc` loaded from the module file instead of the location
6157 // created in the current source manager, because it allows the new
6158 // location to be marked as "unaffecting" when writing and avoid creating
6159 // duplicate locations for the same module map file.
6160 CurrentModule->DefinitionLoc = DefinitionLoc;
6161 CurrentModule->Signature = F.Signature;
6162 CurrentModule->IsFromModuleFile = true;
6163 if (InferredAllowedBy.isValid())
6164 ModMap.setInferredModuleAllowedBy(M: CurrentModule, ModMapFID: InferredAllowedBy);
6165 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
6166 CurrentModule->IsExternC = IsExternC;
6167 CurrentModule->InferSubmodules = InferSubmodules;
6168 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
6169 CurrentModule->InferExportWildcard = InferExportWildcard;
6170 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
6171 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
6172 CurrentModule->NamedModuleHasInit = NamedModuleHasInit;
6173 if (DeserializationListener)
6174 DeserializationListener->ModuleRead(ID: GlobalID, Mod: CurrentModule);
6175
6176 SubmodulesLoaded[GlobalIndex] = CurrentModule;
6177
6178 // Clear out data that will be replaced by what is in the module file.
6179 CurrentModule->LinkLibraries.clear();
6180 CurrentModule->ConfigMacros.clear();
6181 CurrentModule->UnresolvedConflicts.clear();
6182 CurrentModule->Conflicts.clear();
6183
6184 // The module is available unless it's missing a requirement; relevant
6185 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
6186 // Missing headers that were present when the module was built do not
6187 // make it unavailable -- if we got this far, this must be an explicitly
6188 // imported module file.
6189 CurrentModule->Requirements.clear();
6190 CurrentModule->MissingHeaders.clear();
6191 CurrentModule->IsUnimportable =
6192 ParentModule && ParentModule->IsUnimportable;
6193 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
6194 break;
6195 }
6196
6197 case SUBMODULE_UMBRELLA_HEADER: {
6198 // FIXME: This doesn't work for framework modules as `Filename` is the
6199 // name as written in the module file and does not include
6200 // `Headers/`, so this path will never exist.
6201 auto Filename = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6202 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename: *Filename)) {
6203 if (!CurrentModule->getUmbrellaHeaderAsWritten()) {
6204 // FIXME: NameAsWritten
6205 ModMap.setUmbrellaHeaderAsWritten(Mod: CurrentModule, UmbrellaHeader: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6206 }
6207 // Note that it's too late at this point to return out of date if the
6208 // name from the PCM doesn't match up with the one in the module map,
6209 // but also quite unlikely since we will have already checked the
6210 // modification time and size of the module map file itself.
6211 }
6212 break;
6213 }
6214
6215 case SUBMODULE_HEADER:
6216 case SUBMODULE_EXCLUDED_HEADER:
6217 case SUBMODULE_PRIVATE_HEADER:
6218 // We lazily associate headers with their modules via the HeaderInfo table.
6219 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
6220 // of complete filenames or remove it entirely.
6221 break;
6222
6223 case SUBMODULE_TEXTUAL_HEADER:
6224 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
6225 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
6226 // them here.
6227 break;
6228
6229 case SUBMODULE_TOPHEADER: {
6230 auto HeaderName = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6231 CurrentModule->addTopHeaderFilename(Filename: *HeaderName);
6232 break;
6233 }
6234
6235 case SUBMODULE_UMBRELLA_DIR: {
6236 // See comments in SUBMODULE_UMBRELLA_HEADER
6237 auto Dirname = ResolveImportedPath(Buf&: PathBuf, Path: Blob, ModF&: F);
6238 if (auto Umbrella =
6239 PP.getFileManager().getOptionalDirectoryRef(DirName: *Dirname)) {
6240 if (!CurrentModule->getUmbrellaDirAsWritten()) {
6241 // FIXME: NameAsWritten
6242 ModMap.setUmbrellaDirAsWritten(Mod: CurrentModule, UmbrellaDir: *Umbrella, NameAsWritten: Blob, PathRelativeToRootModuleDirectory: "");
6243 }
6244 }
6245 break;
6246 }
6247
6248 case SUBMODULE_METADATA: {
6249 F.BaseSubmoduleID = getTotalNumSubmodules();
6250 F.LocalNumSubmodules = Record[0];
6251 unsigned LocalBaseSubmoduleID = Record[1];
6252 if (F.LocalNumSubmodules > 0) {
6253 // Introduce the global -> local mapping for submodules within this
6254 // module.
6255 GlobalSubmoduleMap.insert(Val: std::make_pair(x: getTotalNumSubmodules()+1,y: &F));
6256
6257 // Introduce the local -> global mapping for submodules within this
6258 // module.
6259 F.SubmoduleRemap.insertOrReplace(
6260 Val: std::make_pair(x&: LocalBaseSubmoduleID,
6261 y: F.BaseSubmoduleID - LocalBaseSubmoduleID));
6262
6263 SubmodulesLoaded.resize(N: SubmodulesLoaded.size() + F.LocalNumSubmodules);
6264 }
6265 break;
6266 }
6267
6268 case SUBMODULE_IMPORTS:
6269 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6270 UnresolvedModuleRef Unresolved;
6271 Unresolved.File = &F;
6272 Unresolved.Mod = CurrentModule;
6273 Unresolved.ID = Record[Idx];
6274 Unresolved.Kind = UnresolvedModuleRef::Import;
6275 Unresolved.IsWildcard = false;
6276 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6277 }
6278 break;
6279
6280 case SUBMODULE_AFFECTING_MODULES:
6281 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
6282 UnresolvedModuleRef Unresolved;
6283 Unresolved.File = &F;
6284 Unresolved.Mod = CurrentModule;
6285 Unresolved.ID = Record[Idx];
6286 Unresolved.Kind = UnresolvedModuleRef::Affecting;
6287 Unresolved.IsWildcard = false;
6288 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6289 }
6290 break;
6291
6292 case SUBMODULE_EXPORTS:
6293 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
6294 UnresolvedModuleRef Unresolved;
6295 Unresolved.File = &F;
6296 Unresolved.Mod = CurrentModule;
6297 Unresolved.ID = Record[Idx];
6298 Unresolved.Kind = UnresolvedModuleRef::Export;
6299 Unresolved.IsWildcard = Record[Idx + 1];
6300 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6301 }
6302
6303 // Once we've loaded the set of exports, there's no reason to keep
6304 // the parsed, unresolved exports around.
6305 CurrentModule->UnresolvedExports.clear();
6306 break;
6307
6308 case SUBMODULE_REQUIRES:
6309 CurrentModule->addRequirement(Feature: Blob, RequiredState: Record[0], LangOpts: PP.getLangOpts(),
6310 Target: PP.getTargetInfo());
6311 break;
6312
6313 case SUBMODULE_LINK_LIBRARY:
6314 ModMap.resolveLinkAsDependencies(Mod: CurrentModule);
6315 CurrentModule->LinkLibraries.push_back(
6316 Elt: Module::LinkLibrary(std::string(Blob), Record[0]));
6317 break;
6318
6319 case SUBMODULE_CONFIG_MACRO:
6320 CurrentModule->ConfigMacros.push_back(x: Blob.str());
6321 break;
6322
6323 case SUBMODULE_CONFLICT: {
6324 UnresolvedModuleRef Unresolved;
6325 Unresolved.File = &F;
6326 Unresolved.Mod = CurrentModule;
6327 Unresolved.ID = Record[0];
6328 Unresolved.Kind = UnresolvedModuleRef::Conflict;
6329 Unresolved.IsWildcard = false;
6330 Unresolved.String = Blob;
6331 UnresolvedModuleRefs.push_back(Elt: Unresolved);
6332 break;
6333 }
6334
6335 case SUBMODULE_INITIALIZERS: {
6336 if (!ContextObj)
6337 break;
6338 SmallVector<GlobalDeclID, 16> Inits;
6339 for (unsigned I = 0; I < Record.size(); /*in loop*/)
6340 Inits.push_back(Elt: ReadDeclID(F, Record, Idx&: I));
6341 ContextObj->addLazyModuleInitializers(M: CurrentModule, IDs: Inits);
6342 break;
6343 }
6344
6345 case SUBMODULE_EXPORT_AS:
6346 CurrentModule->ExportAsModule = Blob.str();
6347 ModMap.addLinkAsDependency(Mod: CurrentModule);
6348 break;
6349 }
6350 }
6351}
6352
6353/// Parse the record that corresponds to a LangOptions data
6354/// structure.
6355///
6356/// This routine parses the language options from the AST file and then gives
6357/// them to the AST listener if one is set.
6358///
6359/// \returns true if the listener deems the file unacceptable, false otherwise.
6360bool ASTReader::ParseLanguageOptions(const RecordData &Record,
6361 StringRef ModuleFilename, bool Complain,
6362 ASTReaderListener &Listener,
6363 bool AllowCompatibleDifferences) {
6364 LangOptions LangOpts;
6365 unsigned Idx = 0;
6366#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
6367 LangOpts.Name = Record[Idx++];
6368#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
6369 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
6370#include "clang/Basic/LangOptions.def"
6371#define SANITIZER(NAME, ID) \
6372 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
6373#include "clang/Basic/Sanitizers.def"
6374
6375 for (unsigned N = Record[Idx++]; N; --N)
6376 LangOpts.ModuleFeatures.push_back(x: ReadString(Record, Idx));
6377
6378 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
6379 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
6380 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
6381
6382 LangOpts.CurrentModule = ReadString(Record, Idx);
6383
6384 // Comment options.
6385 for (unsigned N = Record[Idx++]; N; --N) {
6386 LangOpts.CommentOpts.BlockCommandNames.push_back(
6387 x: ReadString(Record, Idx));
6388 }
6389 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
6390
6391 // OpenMP offloading options.
6392 for (unsigned N = Record[Idx++]; N; --N) {
6393 LangOpts.OMPTargetTriples.push_back(x: llvm::Triple(ReadString(Record, Idx)));
6394 }
6395
6396 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
6397
6398 return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
6399 AllowCompatibleDifferences);
6400}
6401
6402bool ASTReader::ParseTargetOptions(const RecordData &Record,
6403 StringRef ModuleFilename, bool Complain,
6404 ASTReaderListener &Listener,
6405 bool AllowCompatibleDifferences) {
6406 unsigned Idx = 0;
6407 TargetOptions TargetOpts;
6408 TargetOpts.Triple = ReadString(Record, Idx);
6409 TargetOpts.CPU = ReadString(Record, Idx);
6410 TargetOpts.TuneCPU = ReadString(Record, Idx);
6411 TargetOpts.ABI = ReadString(Record, Idx);
6412 for (unsigned N = Record[Idx++]; N; --N) {
6413 TargetOpts.FeaturesAsWritten.push_back(x: ReadString(Record, Idx));
6414 }
6415 for (unsigned N = Record[Idx++]; N; --N) {
6416 TargetOpts.Features.push_back(x: ReadString(Record, Idx));
6417 }
6418
6419 return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
6420 AllowCompatibleDifferences);
6421}
6422
6423bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,
6424 StringRef ModuleFilename, bool Complain,
6425 ASTReaderListener &Listener) {
6426 DiagnosticOptions DiagOpts;
6427 unsigned Idx = 0;
6428#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
6429#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
6430 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
6431#include "clang/Basic/DiagnosticOptions.def"
6432
6433 for (unsigned N = Record[Idx++]; N; --N)
6434 DiagOpts.Warnings.push_back(x: ReadString(Record, Idx));
6435 for (unsigned N = Record[Idx++]; N; --N)
6436 DiagOpts.Remarks.push_back(x: ReadString(Record, Idx));
6437
6438 return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
6439}
6440
6441bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
6442 ASTReaderListener &Listener) {
6443 FileSystemOptions FSOpts;
6444 unsigned Idx = 0;
6445 FSOpts.WorkingDir = ReadString(Record, Idx);
6446 return Listener.ReadFileSystemOptions(FSOpts, Complain);
6447}
6448
6449bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
6450 StringRef ModuleFilename,
6451 bool Complain,
6452 ASTReaderListener &Listener) {
6453 HeaderSearchOptions HSOpts;
6454 unsigned Idx = 0;
6455 HSOpts.Sysroot = ReadString(Record, Idx);
6456
6457 HSOpts.ResourceDir = ReadString(Record, Idx);
6458 HSOpts.ModuleCachePath = ReadString(Record, Idx);
6459 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
6460 HSOpts.DisableModuleHash = Record[Idx++];
6461 HSOpts.ImplicitModuleMaps = Record[Idx++];
6462 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
6463 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
6464 HSOpts.UseBuiltinIncludes = Record[Idx++];
6465 HSOpts.UseStandardSystemIncludes = Record[Idx++];
6466 HSOpts.UseStandardCXXIncludes = Record[Idx++];
6467 HSOpts.UseLibcxx = Record[Idx++];
6468 std::string SpecificModuleCachePath = ReadString(Record, Idx);
6469
6470 return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename,
6471 SpecificModuleCachePath, Complain);
6472}
6473
6474bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,
6475 ASTReaderListener &Listener) {
6476 HeaderSearchOptions HSOpts;
6477 unsigned Idx = 0;
6478
6479 // Include entries.
6480 for (unsigned N = Record[Idx++]; N; --N) {
6481 std::string Path = ReadString(Record, Idx);
6482 frontend::IncludeDirGroup Group
6483 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
6484 bool IsFramework = Record[Idx++];
6485 bool IgnoreSysRoot = Record[Idx++];
6486 HSOpts.UserEntries.emplace_back(args: std::move(Path), args&: Group, args&: IsFramework,
6487 args&: IgnoreSysRoot);
6488 }
6489
6490 // System header prefixes.
6491 for (unsigned N = Record[Idx++]; N; --N) {
6492 std::string Prefix = ReadString(Record, Idx);
6493 bool IsSystemHeader = Record[Idx++];
6494 HSOpts.SystemHeaderPrefixes.emplace_back(args: std::move(Prefix), args&: IsSystemHeader);
6495 }
6496
6497 // VFS overlay files.
6498 for (unsigned N = Record[Idx++]; N; --N) {
6499 std::string VFSOverlayFile = ReadString(Record, Idx);
6500 HSOpts.VFSOverlayFiles.emplace_back(args: std::move(VFSOverlayFile));
6501 }
6502
6503 return Listener.ReadHeaderSearchPaths(HSOpts, Complain);
6504}
6505
6506bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
6507 StringRef ModuleFilename,
6508 bool Complain,
6509 ASTReaderListener &Listener,
6510 std::string &SuggestedPredefines) {
6511 PreprocessorOptions PPOpts;
6512 unsigned Idx = 0;
6513
6514 // Macro definitions/undefs
6515 bool ReadMacros = Record[Idx++];
6516 if (ReadMacros) {
6517 for (unsigned N = Record[Idx++]; N; --N) {
6518 std::string Macro = ReadString(Record, Idx);
6519 bool IsUndef = Record[Idx++];
6520 PPOpts.Macros.push_back(x: std::make_pair(x&: Macro, y&: IsUndef));
6521 }
6522 }
6523
6524 // Includes
6525 for (unsigned N = Record[Idx++]; N; --N) {
6526 PPOpts.Includes.push_back(x: ReadString(Record, Idx));
6527 }
6528
6529 // Macro Includes
6530 for (unsigned N = Record[Idx++]; N; --N) {
6531 PPOpts.MacroIncludes.push_back(x: ReadString(Record, Idx));
6532 }
6533
6534 PPOpts.UsePredefines = Record[Idx++];
6535 PPOpts.DetailedRecord = Record[Idx++];
6536 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
6537 PPOpts.ObjCXXARCStandardLibrary =
6538 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
6539 SuggestedPredefines.clear();
6540 return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
6541 Complain, SuggestedPredefines);
6542}
6543
6544std::pair<ModuleFile *, unsigned>
6545ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
6546 GlobalPreprocessedEntityMapType::iterator
6547 I = GlobalPreprocessedEntityMap.find(K: GlobalIndex);
6548 assert(I != GlobalPreprocessedEntityMap.end() &&
6549 "Corrupted global preprocessed entity map");
6550 ModuleFile *M = I->second;
6551 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
6552 return std::make_pair(x&: M, y&: LocalIndex);
6553}
6554
6555llvm::iterator_range<PreprocessingRecord::iterator>
6556ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
6557 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
6558 return PPRec->getIteratorsForLoadedRange(start: Mod.BasePreprocessedEntityID,
6559 count: Mod.NumPreprocessedEntities);
6560
6561 return llvm::make_range(x: PreprocessingRecord::iterator(),
6562 y: PreprocessingRecord::iterator());
6563}
6564
6565bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
6566 unsigned int ClientLoadCapabilities) {
6567 return ClientLoadCapabilities & ARR_OutOfDate &&
6568 !getModuleManager()
6569 .getModuleCache()
6570 .getInMemoryModuleCache()
6571 .isPCMFinal(Filename: ModuleFileName);
6572}
6573
6574llvm::iterator_range<ASTReader::ModuleDeclIterator>
6575ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
6576 return llvm::make_range(
6577 x: ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
6578 y: ModuleDeclIterator(this, &Mod,
6579 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
6580}
6581
6582SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
6583 auto I = GlobalSkippedRangeMap.find(K: GlobalIndex);
6584 assert(I != GlobalSkippedRangeMap.end() &&
6585 "Corrupted global skipped range map");
6586 ModuleFile *M = I->second;
6587 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
6588 assert(LocalIndex < M->NumPreprocessedSkippedRanges);
6589 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
6590 SourceRange Range(ReadSourceLocation(MF&: *M, Raw: RawRange.getBegin()),
6591 ReadSourceLocation(MF&: *M, Raw: RawRange.getEnd()));
6592 assert(Range.isValid());
6593 return Range;
6594}
6595
6596PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
6597 PreprocessedEntityID PPID = Index+1;
6598 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6599 ModuleFile &M = *PPInfo.first;
6600 unsigned LocalIndex = PPInfo.second;
6601 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6602
6603 if (!PP.getPreprocessingRecord()) {
6604 Error(Msg: "no preprocessing record");
6605 return nullptr;
6606 }
6607
6608 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
6609 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
6610 BitNo: M.MacroOffsetsBase + PPOffs.getOffset())) {
6611 Error(Err: std::move(Err));
6612 return nullptr;
6613 }
6614
6615 Expected<llvm::BitstreamEntry> MaybeEntry =
6616 M.PreprocessorDetailCursor.advance(Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
6617 if (!MaybeEntry) {
6618 Error(Err: MaybeEntry.takeError());
6619 return nullptr;
6620 }
6621 llvm::BitstreamEntry Entry = MaybeEntry.get();
6622
6623 if (Entry.Kind != llvm::BitstreamEntry::Record)
6624 return nullptr;
6625
6626 // Read the record.
6627 SourceRange Range(ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin()),
6628 ReadSourceLocation(MF&: M, Raw: PPOffs.getEnd()));
6629 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6630 StringRef Blob;
6631 RecordData Record;
6632 Expected<unsigned> MaybeRecType =
6633 M.PreprocessorDetailCursor.readRecord(AbbrevID: Entry.ID, Vals&: Record, Blob: &Blob);
6634 if (!MaybeRecType) {
6635 Error(Err: MaybeRecType.takeError());
6636 return nullptr;
6637 }
6638 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6639 case PPD_MACRO_EXPANSION: {
6640 bool isBuiltin = Record[0];
6641 IdentifierInfo *Name = nullptr;
6642 MacroDefinitionRecord *Def = nullptr;
6643 if (isBuiltin)
6644 Name = getLocalIdentifier(M, LocalID: Record[1]);
6645 else {
6646 PreprocessedEntityID GlobalID =
6647 getGlobalPreprocessedEntityID(M, LocalID: Record[1]);
6648 Def = cast<MacroDefinitionRecord>(
6649 Val: PPRec.getLoadedPreprocessedEntity(Index: GlobalID - 1));
6650 }
6651
6652 MacroExpansion *ME;
6653 if (isBuiltin)
6654 ME = new (PPRec) MacroExpansion(Name, Range);
6655 else
6656 ME = new (PPRec) MacroExpansion(Def, Range);
6657
6658 return ME;
6659 }
6660
6661 case PPD_MACRO_DEFINITION: {
6662 // Decode the identifier info and then check again; if the macro is
6663 // still defined and associated with the identifier,
6664 IdentifierInfo *II = getLocalIdentifier(M, LocalID: Record[0]);
6665 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6666
6667 if (DeserializationListener)
6668 DeserializationListener->MacroDefinitionRead(PPID, MD);
6669
6670 return MD;
6671 }
6672
6673 case PPD_INCLUSION_DIRECTIVE: {
6674 const char *FullFileNameStart = Blob.data() + Record[0];
6675 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6676 OptionalFileEntryRef File;
6677 if (!FullFileName.empty())
6678 File = PP.getFileManager().getOptionalFileRef(Filename: FullFileName);
6679
6680 // FIXME: Stable encoding
6681 InclusionDirective::InclusionKind Kind
6682 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6683 InclusionDirective *ID
6684 = new (PPRec) InclusionDirective(PPRec, Kind,
6685 StringRef(Blob.data(), Record[0]),
6686 Record[1], Record[3],
6687 File,
6688 Range);
6689 return ID;
6690 }
6691 }
6692
6693 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6694}
6695
6696/// Find the next module that contains entities and return the ID
6697/// of the first entry.
6698///
6699/// \param SLocMapI points at a chunk of a module that contains no
6700/// preprocessed entities or the entities it contains are not the ones we are
6701/// looking for.
6702PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6703 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6704 ++SLocMapI;
6705 for (GlobalSLocOffsetMapType::const_iterator
6706 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6707 ModuleFile &M = *SLocMapI->second;
6708 if (M.NumPreprocessedEntities)
6709 return M.BasePreprocessedEntityID;
6710 }
6711
6712 return getTotalNumPreprocessedEntities();
6713}
6714
6715namespace {
6716
6717struct PPEntityComp {
6718 const ASTReader &Reader;
6719 ModuleFile &M;
6720
6721 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6722
6723 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6724 SourceLocation LHS = getLoc(PPE: L);
6725 SourceLocation RHS = getLoc(PPE: R);
6726 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6727 }
6728
6729 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6730 SourceLocation LHS = getLoc(PPE: L);
6731 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6732 }
6733
6734 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6735 SourceLocation RHS = getLoc(PPE: R);
6736 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6737 }
6738
6739 SourceLocation getLoc(const PPEntityOffset &PPE) const {
6740 return Reader.ReadSourceLocation(MF&: M, Raw: PPE.getBegin());
6741 }
6742};
6743
6744} // namespace
6745
6746PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6747 bool EndsAfter) const {
6748 if (SourceMgr.isLocalSourceLocation(Loc))
6749 return getTotalNumPreprocessedEntities();
6750
6751 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6752 K: SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6753 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6754 "Corrupted global sloc offset map");
6755
6756 if (SLocMapI->second->NumPreprocessedEntities == 0)
6757 return findNextPreprocessedEntity(SLocMapI);
6758
6759 ModuleFile &M = *SLocMapI->second;
6760
6761 using pp_iterator = const PPEntityOffset *;
6762
6763 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6764 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6765
6766 size_t Count = M.NumPreprocessedEntities;
6767 size_t Half;
6768 pp_iterator First = pp_begin;
6769 pp_iterator PPI;
6770
6771 if (EndsAfter) {
6772 PPI = std::upper_bound(first: pp_begin, last: pp_end, val: Loc,
6773 comp: PPEntityComp(*this, M));
6774 } else {
6775 // Do a binary search manually instead of using std::lower_bound because
6776 // The end locations of entities may be unordered (when a macro expansion
6777 // is inside another macro argument), but for this case it is not important
6778 // whether we get the first macro expansion or its containing macro.
6779 while (Count > 0) {
6780 Half = Count / 2;
6781 PPI = First;
6782 std::advance(i&: PPI, n: Half);
6783 if (SourceMgr.isBeforeInTranslationUnit(
6784 LHS: ReadSourceLocation(MF&: M, Raw: PPI->getEnd()), RHS: Loc)) {
6785 First = PPI;
6786 ++First;
6787 Count = Count - Half - 1;
6788 } else
6789 Count = Half;
6790 }
6791 }
6792
6793 if (PPI == pp_end)
6794 return findNextPreprocessedEntity(SLocMapI);
6795
6796 return M.BasePreprocessedEntityID + (PPI - pp_begin);
6797}
6798
6799/// Returns a pair of [Begin, End) indices of preallocated
6800/// preprocessed entities that \arg Range encompasses.
6801std::pair<unsigned, unsigned>
6802 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6803 if (Range.isInvalid())
6804 return std::make_pair(x: 0,y: 0);
6805 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6806
6807 PreprocessedEntityID BeginID =
6808 findPreprocessedEntity(Loc: Range.getBegin(), EndsAfter: false);
6809 PreprocessedEntityID EndID = findPreprocessedEntity(Loc: Range.getEnd(), EndsAfter: true);
6810 return std::make_pair(x&: BeginID, y&: EndID);
6811}
6812
6813/// Optionally returns true or false if the preallocated preprocessed
6814/// entity with index \arg Index came from file \arg FID.
6815std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6816 FileID FID) {
6817 if (FID.isInvalid())
6818 return false;
6819
6820 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(GlobalIndex: Index);
6821 ModuleFile &M = *PPInfo.first;
6822 unsigned LocalIndex = PPInfo.second;
6823 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6824
6825 SourceLocation Loc = ReadSourceLocation(MF&: M, Raw: PPOffs.getBegin());
6826 if (Loc.isInvalid())
6827 return false;
6828
6829 if (SourceMgr.isInFileID(Loc: SourceMgr.getFileLoc(Loc), FID))
6830 return true;
6831 else
6832 return false;
6833}
6834
6835namespace {
6836
6837 /// Visitor used to search for information about a header file.
6838 class HeaderFileInfoVisitor {
6839 FileEntryRef FE;
6840 std::optional<HeaderFileInfo> HFI;
6841
6842 public:
6843 explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}
6844
6845 bool operator()(ModuleFile &M) {
6846 HeaderFileInfoLookupTable *Table
6847 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6848 if (!Table)
6849 return false;
6850
6851 // Look in the on-disk hash table for an entry for this file name.
6852 HeaderFileInfoLookupTable::iterator Pos = Table->find(EKey: FE);
6853 if (Pos == Table->end())
6854 return false;
6855
6856 HFI = *Pos;
6857 return true;
6858 }
6859
6860 std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6861 };
6862
6863} // namespace
6864
6865HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {
6866 HeaderFileInfoVisitor Visitor(FE);
6867 ModuleMgr.visit(Visitor);
6868 if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6869 return *HFI;
6870
6871 return HeaderFileInfo();
6872}
6873
6874void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6875 using DiagState = DiagnosticsEngine::DiagState;
6876 SmallVector<DiagState *, 32> DiagStates;
6877
6878 for (ModuleFile &F : ModuleMgr) {
6879 unsigned Idx = 0;
6880 auto &Record = F.PragmaDiagMappings;
6881 if (Record.empty())
6882 continue;
6883
6884 DiagStates.clear();
6885
6886 auto ReadDiagState = [&](const DiagState &BasedOn,
6887 bool IncludeNonPragmaStates) {
6888 unsigned BackrefID = Record[Idx++];
6889 if (BackrefID != 0)
6890 return DiagStates[BackrefID - 1];
6891
6892 // A new DiagState was created here.
6893 Diag.DiagStates.push_back(x: BasedOn);
6894 DiagState *NewState = &Diag.DiagStates.back();
6895 DiagStates.push_back(Elt: NewState);
6896 unsigned Size = Record[Idx++];
6897 assert(Idx + Size * 2 <= Record.size() &&
6898 "Invalid data, not enough diag/map pairs");
6899 while (Size--) {
6900 unsigned DiagID = Record[Idx++];
6901 DiagnosticMapping NewMapping =
6902 DiagnosticMapping::deserialize(Bits: Record[Idx++]);
6903 if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6904 continue;
6905
6906 DiagnosticMapping &Mapping = NewState->getOrAddMapping(Diag: DiagID);
6907
6908 // If this mapping was specified as a warning but the severity was
6909 // upgraded due to diagnostic settings, simulate the current diagnostic
6910 // settings (and use a warning).
6911 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6912 NewMapping.setSeverity(diag::Severity::Warning);
6913 NewMapping.setUpgradedFromWarning(false);
6914 }
6915
6916 Mapping = NewMapping;
6917 }
6918 return NewState;
6919 };
6920
6921 // Read the first state.
6922 DiagState *FirstState;
6923 if (F.Kind == MK_ImplicitModule) {
6924 // Implicitly-built modules are reused with different diagnostic
6925 // settings. Use the initial diagnostic state from Diag to simulate this
6926 // compilation's diagnostic settings.
6927 FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6928 DiagStates.push_back(Elt: FirstState);
6929
6930 // Skip the initial diagnostic state from the serialized module.
6931 assert(Record[1] == 0 &&
6932 "Invalid data, unexpected backref in initial state");
6933 Idx = 3 + Record[2] * 2;
6934 assert(Idx < Record.size() &&
6935 "Invalid data, not enough state change pairs in initial state");
6936 } else if (F.isModule()) {
6937 // For an explicit module, preserve the flags from the module build
6938 // command line (-w, -Weverything, -Werror, ...) along with any explicit
6939 // -Wblah flags.
6940 unsigned Flags = Record[Idx++];
6941 DiagState Initial(*Diag.getDiagnosticIDs());
6942 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6943 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6944 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6945 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6946 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6947 Initial.ExtBehavior = (diag::Severity)Flags;
6948 FirstState = ReadDiagState(Initial, true);
6949
6950 assert(F.OriginalSourceFileID.isValid());
6951
6952 // Set up the root buffer of the module to start with the initial
6953 // diagnostic state of the module itself, to cover files that contain no
6954 // explicit transitions (for which we did not serialize anything).
6955 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6956 .StateTransitions.push_back(Elt: {FirstState, 0});
6957 } else {
6958 // For prefix ASTs, start with whatever the user configured on the
6959 // command line.
6960 Idx++; // Skip flags.
6961 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
6962 }
6963
6964 // Read the state transitions.
6965 unsigned NumLocations = Record[Idx++];
6966 while (NumLocations--) {
6967 assert(Idx < Record.size() &&
6968 "Invalid data, missing pragma diagnostic states");
6969 FileID FID = ReadFileID(F, Record, Idx);
6970 assert(FID.isValid() && "invalid FileID for transition");
6971 unsigned Transitions = Record[Idx++];
6972
6973 // Note that we don't need to set up Parent/ParentOffset here, because
6974 // we won't be changing the diagnostic state within imported FileIDs
6975 // (other than perhaps appending to the main source file, which has no
6976 // parent).
6977 auto &F = Diag.DiagStatesByLoc.Files[FID];
6978 F.StateTransitions.reserve(N: F.StateTransitions.size() + Transitions);
6979 for (unsigned I = 0; I != Transitions; ++I) {
6980 unsigned Offset = Record[Idx++];
6981 auto *State = ReadDiagState(*FirstState, false);
6982 F.StateTransitions.push_back(Elt: {State, Offset});
6983 }
6984 }
6985
6986 // Read the final state.
6987 assert(Idx < Record.size() &&
6988 "Invalid data, missing final pragma diagnostic state");
6989 SourceLocation CurStateLoc = ReadSourceLocation(MF&: F, Raw: Record[Idx++]);
6990 auto *CurState = ReadDiagState(*FirstState, false);
6991
6992 if (!F.isModule()) {
6993 Diag.DiagStatesByLoc.CurDiagState = CurState;
6994 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6995
6996 // Preserve the property that the imaginary root file describes the
6997 // current state.
6998 FileID NullFile;
6999 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
7000 if (T.empty())
7001 T.push_back(Elt: {CurState, 0});
7002 else
7003 T[0].State = CurState;
7004 }
7005
7006 // Don't try to read these mappings again.
7007 Record.clear();
7008 }
7009}
7010
7011/// Get the correct cursor and offset for loading a type.
7012ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
7013 auto [M, Index] = translateTypeIDToIndex(ID);
7014 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
7015 M->DeclsBlockStartOffset);
7016}
7017
7018static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
7019 switch (code) {
7020#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
7021 case TYPE_##CODE_ID: return Type::CLASS_ID;
7022#include "clang/Serialization/TypeBitCodes.def"
7023 default:
7024 return std::nullopt;
7025 }
7026}
7027
7028/// Read and return the type with the given index..
7029///
7030/// The index is the type ID, shifted and minus the number of predefs. This
7031/// routine actually reads the record corresponding to the type at the given
7032/// location. It is a helper routine for GetType, which deals with reading type
7033/// IDs.
7034QualType ASTReader::readTypeRecord(TypeID ID) {
7035 assert(ContextObj && "reading type with no AST context");
7036 ASTContext &Context = *ContextObj;
7037 RecordLocation Loc = TypeCursorForIndex(ID);
7038 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
7039
7040 // Keep track of where we are in the stream, then jump back there
7041 // after reading this type.
7042 SavedStreamPosition SavedPosition(DeclsCursor);
7043
7044 ReadingKindTracker ReadingKind(Read_Type, *this);
7045
7046 // Note that we are loading a type record.
7047 Deserializing AType(this);
7048
7049 if (llvm::Error Err = DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
7050 Error(Err: std::move(Err));
7051 return QualType();
7052 }
7053 Expected<unsigned> RawCode = DeclsCursor.ReadCode();
7054 if (!RawCode) {
7055 Error(Err: RawCode.takeError());
7056 return QualType();
7057 }
7058
7059 ASTRecordReader Record(*this, *Loc.F);
7060 Expected<unsigned> Code = Record.readRecord(Cursor&: DeclsCursor, AbbrevID: RawCode.get());
7061 if (!Code) {
7062 Error(Err: Code.takeError());
7063 return QualType();
7064 }
7065 if (Code.get() == TYPE_EXT_QUAL) {
7066 QualType baseType = Record.readQualType();
7067 Qualifiers quals = Record.readQualifiers();
7068 return Context.getQualifiedType(T: baseType, Qs: quals);
7069 }
7070
7071 auto maybeClass = getTypeClassForCode(code: (TypeCode) Code.get());
7072 if (!maybeClass) {
7073 Error(Msg: "Unexpected code for type");
7074 return QualType();
7075 }
7076
7077 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
7078 return TypeReader.read(kind: *maybeClass);
7079}
7080
7081namespace clang {
7082
7083class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
7084 ASTRecordReader &Reader;
7085
7086 SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }
7087 SourceRange readSourceRange() { return Reader.readSourceRange(); }
7088
7089 TypeSourceInfo *GetTypeSourceInfo() {
7090 return Reader.readTypeSourceInfo();
7091 }
7092
7093 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
7094 return Reader.readNestedNameSpecifierLoc();
7095 }
7096
7097 Attr *ReadAttr() {
7098 return Reader.readAttr();
7099 }
7100
7101public:
7102 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
7103
7104 // We want compile-time assurance that we've enumerated all of
7105 // these, so unfortunately we have to declare them first, then
7106 // define them out-of-line.
7107#define ABSTRACT_TYPELOC(CLASS, PARENT)
7108#define TYPELOC(CLASS, PARENT) \
7109 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
7110#include "clang/AST/TypeLocNodes.def"
7111
7112 void VisitFunctionTypeLoc(FunctionTypeLoc);
7113 void VisitArrayTypeLoc(ArrayTypeLoc);
7114};
7115
7116} // namespace clang
7117
7118void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
7119 // nothing to do
7120}
7121
7122void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
7123 TL.setBuiltinLoc(readSourceLocation());
7124 if (TL.needsExtraLocalData()) {
7125 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
7126 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
7127 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
7128 TL.setModeAttr(Reader.readInt());
7129 }
7130}
7131
7132void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
7133 TL.setNameLoc(readSourceLocation());
7134}
7135
7136void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
7137 TL.setStarLoc(readSourceLocation());
7138}
7139
7140void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
7141 // nothing to do
7142}
7143
7144void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
7145 // nothing to do
7146}
7147
7148void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {
7149 // nothing to do
7150}
7151
7152void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
7153 TL.setExpansionLoc(readSourceLocation());
7154}
7155
7156void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
7157 TL.setCaretLoc(readSourceLocation());
7158}
7159
7160void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
7161 TL.setAmpLoc(readSourceLocation());
7162}
7163
7164void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
7165 TL.setAmpAmpLoc(readSourceLocation());
7166}
7167
7168void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
7169 TL.setStarLoc(readSourceLocation());
7170 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7171}
7172
7173void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
7174 TL.setLBracketLoc(readSourceLocation());
7175 TL.setRBracketLoc(readSourceLocation());
7176 if (Reader.readBool())
7177 TL.setSizeExpr(Reader.readExpr());
7178 else
7179 TL.setSizeExpr(nullptr);
7180}
7181
7182void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
7183 VisitArrayTypeLoc(TL);
7184}
7185
7186void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
7187 VisitArrayTypeLoc(TL);
7188}
7189
7190void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
7191 VisitArrayTypeLoc(TL);
7192}
7193
7194void TypeLocReader::VisitDependentSizedArrayTypeLoc(
7195 DependentSizedArrayTypeLoc TL) {
7196 VisitArrayTypeLoc(TL);
7197}
7198
7199void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
7200 DependentAddressSpaceTypeLoc TL) {
7201
7202 TL.setAttrNameLoc(readSourceLocation());
7203 TL.setAttrOperandParensRange(readSourceRange());
7204 TL.setAttrExprOperand(Reader.readExpr());
7205}
7206
7207void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
7208 DependentSizedExtVectorTypeLoc TL) {
7209 TL.setNameLoc(readSourceLocation());
7210}
7211
7212void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
7213 TL.setNameLoc(readSourceLocation());
7214}
7215
7216void TypeLocReader::VisitDependentVectorTypeLoc(
7217 DependentVectorTypeLoc TL) {
7218 TL.setNameLoc(readSourceLocation());
7219}
7220
7221void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
7222 TL.setNameLoc(readSourceLocation());
7223}
7224
7225void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
7226 TL.setAttrNameLoc(readSourceLocation());
7227 TL.setAttrOperandParensRange(readSourceRange());
7228 TL.setAttrRowOperand(Reader.readExpr());
7229 TL.setAttrColumnOperand(Reader.readExpr());
7230}
7231
7232void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
7233 DependentSizedMatrixTypeLoc TL) {
7234 TL.setAttrNameLoc(readSourceLocation());
7235 TL.setAttrOperandParensRange(readSourceRange());
7236 TL.setAttrRowOperand(Reader.readExpr());
7237 TL.setAttrColumnOperand(Reader.readExpr());
7238}
7239
7240void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
7241 TL.setLocalRangeBegin(readSourceLocation());
7242 TL.setLParenLoc(readSourceLocation());
7243 TL.setRParenLoc(readSourceLocation());
7244 TL.setExceptionSpecRange(readSourceRange());
7245 TL.setLocalRangeEnd(readSourceLocation());
7246 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
7247 TL.setParam(i, VD: Reader.readDeclAs<ParmVarDecl>());
7248 }
7249}
7250
7251void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
7252 VisitFunctionTypeLoc(TL);
7253}
7254
7255void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
7256 VisitFunctionTypeLoc(TL);
7257}
7258
7259void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
7260 TL.setNameLoc(readSourceLocation());
7261}
7262
7263void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
7264 TL.setNameLoc(readSourceLocation());
7265}
7266
7267void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
7268 TL.setNameLoc(readSourceLocation());
7269}
7270
7271void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
7272 TL.setTypeofLoc(readSourceLocation());
7273 TL.setLParenLoc(readSourceLocation());
7274 TL.setRParenLoc(readSourceLocation());
7275}
7276
7277void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
7278 TL.setTypeofLoc(readSourceLocation());
7279 TL.setLParenLoc(readSourceLocation());
7280 TL.setRParenLoc(readSourceLocation());
7281 TL.setUnmodifiedTInfo(GetTypeSourceInfo());
7282}
7283
7284void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
7285 TL.setDecltypeLoc(readSourceLocation());
7286 TL.setRParenLoc(readSourceLocation());
7287}
7288
7289void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {
7290 TL.setEllipsisLoc(readSourceLocation());
7291}
7292
7293void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
7294 TL.setKWLoc(readSourceLocation());
7295 TL.setLParenLoc(readSourceLocation());
7296 TL.setRParenLoc(readSourceLocation());
7297 TL.setUnderlyingTInfo(GetTypeSourceInfo());
7298}
7299
7300ConceptReference *ASTRecordReader::readConceptReference() {
7301 auto NNS = readNestedNameSpecifierLoc();
7302 auto TemplateKWLoc = readSourceLocation();
7303 auto ConceptNameLoc = readDeclarationNameInfo();
7304 auto FoundDecl = readDeclAs<NamedDecl>();
7305 auto NamedConcept = readDeclAs<ConceptDecl>();
7306 auto *CR = ConceptReference::Create(
7307 C: getContext(), NNS, TemplateKWLoc, ConceptNameInfo: ConceptNameLoc, FoundDecl, NamedConcept,
7308 ArgsAsWritten: (readBool() ? readASTTemplateArgumentListInfo() : nullptr));
7309 return CR;
7310}
7311
7312void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
7313 TL.setNameLoc(readSourceLocation());
7314 if (Reader.readBool())
7315 TL.setConceptReference(Reader.readConceptReference());
7316 if (Reader.readBool())
7317 TL.setRParenLoc(readSourceLocation());
7318}
7319
7320void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
7321 DeducedTemplateSpecializationTypeLoc TL) {
7322 TL.setTemplateNameLoc(readSourceLocation());
7323}
7324
7325void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
7326 TL.setNameLoc(readSourceLocation());
7327}
7328
7329void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
7330 TL.setNameLoc(readSourceLocation());
7331}
7332
7333void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
7334 TL.setAttr(ReadAttr());
7335}
7336
7337void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {
7338 // Nothing to do
7339}
7340
7341void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
7342 // Nothing to do.
7343}
7344
7345void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(
7346 HLSLAttributedResourceTypeLoc TL) {
7347 // Nothing to do.
7348}
7349
7350void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {
7351 // Nothing to do.
7352}
7353
7354void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
7355 TL.setNameLoc(readSourceLocation());
7356}
7357
7358void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
7359 SubstTemplateTypeParmTypeLoc TL) {
7360 TL.setNameLoc(readSourceLocation());
7361}
7362
7363void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
7364 SubstTemplateTypeParmPackTypeLoc TL) {
7365 TL.setNameLoc(readSourceLocation());
7366}
7367
7368void TypeLocReader::VisitTemplateSpecializationTypeLoc(
7369 TemplateSpecializationTypeLoc TL) {
7370 TL.setTemplateKeywordLoc(readSourceLocation());
7371 TL.setTemplateNameLoc(readSourceLocation());
7372 TL.setLAngleLoc(readSourceLocation());
7373 TL.setRAngleLoc(readSourceLocation());
7374 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
7375 TL.setArgLocInfo(i,
7376 AI: Reader.readTemplateArgumentLocInfo(
7377 Kind: TL.getTypePtr()->template_arguments()[i].getKind()));
7378}
7379
7380void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
7381 TL.setLParenLoc(readSourceLocation());
7382 TL.setRParenLoc(readSourceLocation());
7383}
7384
7385void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
7386 TL.setElaboratedKeywordLoc(readSourceLocation());
7387 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7388}
7389
7390void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
7391 TL.setNameLoc(readSourceLocation());
7392}
7393
7394void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
7395 TL.setElaboratedKeywordLoc(readSourceLocation());
7396 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7397 TL.setNameLoc(readSourceLocation());
7398}
7399
7400void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
7401 DependentTemplateSpecializationTypeLoc TL) {
7402 TL.setElaboratedKeywordLoc(readSourceLocation());
7403 TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
7404 TL.setTemplateKeywordLoc(readSourceLocation());
7405 TL.setTemplateNameLoc(readSourceLocation());
7406 TL.setLAngleLoc(readSourceLocation());
7407 TL.setRAngleLoc(readSourceLocation());
7408 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
7409 TL.setArgLocInfo(i: I,
7410 AI: Reader.readTemplateArgumentLocInfo(
7411 Kind: TL.getTypePtr()->template_arguments()[I].getKind()));
7412}
7413
7414void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
7415 TL.setEllipsisLoc(readSourceLocation());
7416}
7417
7418void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
7419 TL.setNameLoc(readSourceLocation());
7420 TL.setNameEndLoc(readSourceLocation());
7421}
7422
7423void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
7424 if (TL.getNumProtocols()) {
7425 TL.setProtocolLAngleLoc(readSourceLocation());
7426 TL.setProtocolRAngleLoc(readSourceLocation());
7427 }
7428 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7429 TL.setProtocolLoc(i, Loc: readSourceLocation());
7430}
7431
7432void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
7433 TL.setHasBaseTypeAsWritten(Reader.readBool());
7434 TL.setTypeArgsLAngleLoc(readSourceLocation());
7435 TL.setTypeArgsRAngleLoc(readSourceLocation());
7436 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
7437 TL.setTypeArgTInfo(i, TInfo: GetTypeSourceInfo());
7438 TL.setProtocolLAngleLoc(readSourceLocation());
7439 TL.setProtocolRAngleLoc(readSourceLocation());
7440 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
7441 TL.setProtocolLoc(i, Loc: readSourceLocation());
7442}
7443
7444void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
7445 TL.setStarLoc(readSourceLocation());
7446}
7447
7448void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
7449 TL.setKWLoc(readSourceLocation());
7450 TL.setLParenLoc(readSourceLocation());
7451 TL.setRParenLoc(readSourceLocation());
7452}
7453
7454void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
7455 TL.setKWLoc(readSourceLocation());
7456}
7457
7458void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
7459 TL.setNameLoc(readSourceLocation());
7460}
7461void TypeLocReader::VisitDependentBitIntTypeLoc(
7462 clang::DependentBitIntTypeLoc TL) {
7463 TL.setNameLoc(readSourceLocation());
7464}
7465
7466void ASTRecordReader::readTypeLoc(TypeLoc TL) {
7467 TypeLocReader TLR(*this);
7468 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
7469 TLR.Visit(TyLoc: TL);
7470}
7471
7472TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
7473 QualType InfoTy = readType();
7474 if (InfoTy.isNull())
7475 return nullptr;
7476
7477 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(T: InfoTy);
7478 readTypeLoc(TL: TInfo->getTypeLoc());
7479 return TInfo;
7480}
7481
7482static unsigned getIndexForTypeID(serialization::TypeID ID) {
7483 return (ID & llvm::maskTrailingOnes<TypeID>(N: 32)) >> Qualifiers::FastWidth;
7484}
7485
7486static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {
7487 return ID >> 32;
7488}
7489
7490static bool isPredefinedType(serialization::TypeID ID) {
7491 // We don't need to erase the higher bits since if these bits are not 0,
7492 // it must be larger than NUM_PREDEF_TYPE_IDS.
7493 return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;
7494}
7495
7496std::pair<ModuleFile *, unsigned>
7497ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7498 assert(!isPredefinedType(ID) &&
7499 "Predefined type shouldn't be in TypesLoaded");
7500 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);
7501 assert(ModuleFileIndex && "Untranslated Local Decl?");
7502
7503 ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];
7504 assert(OwningModuleFile &&
7505 "untranslated type ID or local type ID shouldn't be in TypesLoaded");
7506
7507 return {OwningModuleFile,
7508 OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};
7509}
7510
7511QualType ASTReader::GetType(TypeID ID) {
7512 assert(ContextObj && "reading type with no AST context");
7513 ASTContext &Context = *ContextObj;
7514
7515 unsigned FastQuals = ID & Qualifiers::FastMask;
7516
7517 if (isPredefinedType(ID)) {
7518 QualType T;
7519 unsigned Index = getIndexForTypeID(ID);
7520 switch ((PredefinedTypeIDs)Index) {
7521 case PREDEF_TYPE_LAST_ID:
7522 // We should never use this one.
7523 llvm_unreachable("Invalid predefined type");
7524 break;
7525 case PREDEF_TYPE_NULL_ID:
7526 return QualType();
7527 case PREDEF_TYPE_VOID_ID:
7528 T = Context.VoidTy;
7529 break;
7530 case PREDEF_TYPE_BOOL_ID:
7531 T = Context.BoolTy;
7532 break;
7533 case PREDEF_TYPE_CHAR_U_ID:
7534 case PREDEF_TYPE_CHAR_S_ID:
7535 // FIXME: Check that the signedness of CharTy is correct!
7536 T = Context.CharTy;
7537 break;
7538 case PREDEF_TYPE_UCHAR_ID:
7539 T = Context.UnsignedCharTy;
7540 break;
7541 case PREDEF_TYPE_USHORT_ID:
7542 T = Context.UnsignedShortTy;
7543 break;
7544 case PREDEF_TYPE_UINT_ID:
7545 T = Context.UnsignedIntTy;
7546 break;
7547 case PREDEF_TYPE_ULONG_ID:
7548 T = Context.UnsignedLongTy;
7549 break;
7550 case PREDEF_TYPE_ULONGLONG_ID:
7551 T = Context.UnsignedLongLongTy;
7552 break;
7553 case PREDEF_TYPE_UINT128_ID:
7554 T = Context.UnsignedInt128Ty;
7555 break;
7556 case PREDEF_TYPE_SCHAR_ID:
7557 T = Context.SignedCharTy;
7558 break;
7559 case PREDEF_TYPE_WCHAR_ID:
7560 T = Context.WCharTy;
7561 break;
7562 case PREDEF_TYPE_SHORT_ID:
7563 T = Context.ShortTy;
7564 break;
7565 case PREDEF_TYPE_INT_ID:
7566 T = Context.IntTy;
7567 break;
7568 case PREDEF_TYPE_LONG_ID:
7569 T = Context.LongTy;
7570 break;
7571 case PREDEF_TYPE_LONGLONG_ID:
7572 T = Context.LongLongTy;
7573 break;
7574 case PREDEF_TYPE_INT128_ID:
7575 T = Context.Int128Ty;
7576 break;
7577 case PREDEF_TYPE_BFLOAT16_ID:
7578 T = Context.BFloat16Ty;
7579 break;
7580 case PREDEF_TYPE_HALF_ID:
7581 T = Context.HalfTy;
7582 break;
7583 case PREDEF_TYPE_FLOAT_ID:
7584 T = Context.FloatTy;
7585 break;
7586 case PREDEF_TYPE_DOUBLE_ID:
7587 T = Context.DoubleTy;
7588 break;
7589 case PREDEF_TYPE_LONGDOUBLE_ID:
7590 T = Context.LongDoubleTy;
7591 break;
7592 case PREDEF_TYPE_SHORT_ACCUM_ID:
7593 T = Context.ShortAccumTy;
7594 break;
7595 case PREDEF_TYPE_ACCUM_ID:
7596 T = Context.AccumTy;
7597 break;
7598 case PREDEF_TYPE_LONG_ACCUM_ID:
7599 T = Context.LongAccumTy;
7600 break;
7601 case PREDEF_TYPE_USHORT_ACCUM_ID:
7602 T = Context.UnsignedShortAccumTy;
7603 break;
7604 case PREDEF_TYPE_UACCUM_ID:
7605 T = Context.UnsignedAccumTy;
7606 break;
7607 case PREDEF_TYPE_ULONG_ACCUM_ID:
7608 T = Context.UnsignedLongAccumTy;
7609 break;
7610 case PREDEF_TYPE_SHORT_FRACT_ID:
7611 T = Context.ShortFractTy;
7612 break;
7613 case PREDEF_TYPE_FRACT_ID:
7614 T = Context.FractTy;
7615 break;
7616 case PREDEF_TYPE_LONG_FRACT_ID:
7617 T = Context.LongFractTy;
7618 break;
7619 case PREDEF_TYPE_USHORT_FRACT_ID:
7620 T = Context.UnsignedShortFractTy;
7621 break;
7622 case PREDEF_TYPE_UFRACT_ID:
7623 T = Context.UnsignedFractTy;
7624 break;
7625 case PREDEF_TYPE_ULONG_FRACT_ID:
7626 T = Context.UnsignedLongFractTy;
7627 break;
7628 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
7629 T = Context.SatShortAccumTy;
7630 break;
7631 case PREDEF_TYPE_SAT_ACCUM_ID:
7632 T = Context.SatAccumTy;
7633 break;
7634 case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
7635 T = Context.SatLongAccumTy;
7636 break;
7637 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
7638 T = Context.SatUnsignedShortAccumTy;
7639 break;
7640 case PREDEF_TYPE_SAT_UACCUM_ID:
7641 T = Context.SatUnsignedAccumTy;
7642 break;
7643 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
7644 T = Context.SatUnsignedLongAccumTy;
7645 break;
7646 case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
7647 T = Context.SatShortFractTy;
7648 break;
7649 case PREDEF_TYPE_SAT_FRACT_ID:
7650 T = Context.SatFractTy;
7651 break;
7652 case PREDEF_TYPE_SAT_LONG_FRACT_ID:
7653 T = Context.SatLongFractTy;
7654 break;
7655 case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
7656 T = Context.SatUnsignedShortFractTy;
7657 break;
7658 case PREDEF_TYPE_SAT_UFRACT_ID:
7659 T = Context.SatUnsignedFractTy;
7660 break;
7661 case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
7662 T = Context.SatUnsignedLongFractTy;
7663 break;
7664 case PREDEF_TYPE_FLOAT16_ID:
7665 T = Context.Float16Ty;
7666 break;
7667 case PREDEF_TYPE_FLOAT128_ID:
7668 T = Context.Float128Ty;
7669 break;
7670 case PREDEF_TYPE_IBM128_ID:
7671 T = Context.Ibm128Ty;
7672 break;
7673 case PREDEF_TYPE_OVERLOAD_ID:
7674 T = Context.OverloadTy;
7675 break;
7676 case PREDEF_TYPE_UNRESOLVED_TEMPLATE:
7677 T = Context.UnresolvedTemplateTy;
7678 break;
7679 case PREDEF_TYPE_BOUND_MEMBER:
7680 T = Context.BoundMemberTy;
7681 break;
7682 case PREDEF_TYPE_PSEUDO_OBJECT:
7683 T = Context.PseudoObjectTy;
7684 break;
7685 case PREDEF_TYPE_DEPENDENT_ID:
7686 T = Context.DependentTy;
7687 break;
7688 case PREDEF_TYPE_UNKNOWN_ANY:
7689 T = Context.UnknownAnyTy;
7690 break;
7691 case PREDEF_TYPE_NULLPTR_ID:
7692 T = Context.NullPtrTy;
7693 break;
7694 case PREDEF_TYPE_CHAR8_ID:
7695 T = Context.Char8Ty;
7696 break;
7697 case PREDEF_TYPE_CHAR16_ID:
7698 T = Context.Char16Ty;
7699 break;
7700 case PREDEF_TYPE_CHAR32_ID:
7701 T = Context.Char32Ty;
7702 break;
7703 case PREDEF_TYPE_OBJC_ID:
7704 T = Context.ObjCBuiltinIdTy;
7705 break;
7706 case PREDEF_TYPE_OBJC_CLASS:
7707 T = Context.ObjCBuiltinClassTy;
7708 break;
7709 case PREDEF_TYPE_OBJC_SEL:
7710 T = Context.ObjCBuiltinSelTy;
7711 break;
7712#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7713 case PREDEF_TYPE_##Id##_ID: \
7714 T = Context.SingletonId; \
7715 break;
7716#include "clang/Basic/OpenCLImageTypes.def"
7717#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7718 case PREDEF_TYPE_##Id##_ID: \
7719 T = Context.Id##Ty; \
7720 break;
7721#include "clang/Basic/OpenCLExtensionTypes.def"
7722 case PREDEF_TYPE_SAMPLER_ID:
7723 T = Context.OCLSamplerTy;
7724 break;
7725 case PREDEF_TYPE_EVENT_ID:
7726 T = Context.OCLEventTy;
7727 break;
7728 case PREDEF_TYPE_CLK_EVENT_ID:
7729 T = Context.OCLClkEventTy;
7730 break;
7731 case PREDEF_TYPE_QUEUE_ID:
7732 T = Context.OCLQueueTy;
7733 break;
7734 case PREDEF_TYPE_RESERVE_ID_ID:
7735 T = Context.OCLReserveIDTy;
7736 break;
7737 case PREDEF_TYPE_AUTO_DEDUCT:
7738 T = Context.getAutoDeductType();
7739 break;
7740 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7741 T = Context.getAutoRRefDeductType();
7742 break;
7743 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7744 T = Context.ARCUnbridgedCastTy;
7745 break;
7746 case PREDEF_TYPE_BUILTIN_FN:
7747 T = Context.BuiltinFnTy;
7748 break;
7749 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7750 T = Context.IncompleteMatrixIdxTy;
7751 break;
7752 case PREDEF_TYPE_ARRAY_SECTION:
7753 T = Context.ArraySectionTy;
7754 break;
7755 case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7756 T = Context.OMPArrayShapingTy;
7757 break;
7758 case PREDEF_TYPE_OMP_ITERATOR:
7759 T = Context.OMPIteratorTy;
7760 break;
7761#define SVE_TYPE(Name, Id, SingletonId) \
7762 case PREDEF_TYPE_##Id##_ID: \
7763 T = Context.SingletonId; \
7764 break;
7765#include "clang/Basic/AArch64ACLETypes.def"
7766#define PPC_VECTOR_TYPE(Name, Id, Size) \
7767 case PREDEF_TYPE_##Id##_ID: \
7768 T = Context.Id##Ty; \
7769 break;
7770#include "clang/Basic/PPCTypes.def"
7771#define RVV_TYPE(Name, Id, SingletonId) \
7772 case PREDEF_TYPE_##Id##_ID: \
7773 T = Context.SingletonId; \
7774 break;
7775#include "clang/Basic/RISCVVTypes.def"
7776#define WASM_TYPE(Name, Id, SingletonId) \
7777 case PREDEF_TYPE_##Id##_ID: \
7778 T = Context.SingletonId; \
7779 break;
7780#include "clang/Basic/WebAssemblyReferenceTypes.def"
7781#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
7782 case PREDEF_TYPE_##Id##_ID: \
7783 T = Context.SingletonId; \
7784 break;
7785#include "clang/Basic/AMDGPUTypes.def"
7786#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
7787 case PREDEF_TYPE_##Id##_ID: \
7788 T = Context.SingletonId; \
7789 break;
7790#include "clang/Basic/HLSLIntangibleTypes.def"
7791 }
7792
7793 assert(!T.isNull() && "Unknown predefined type");
7794 return T.withFastQualifiers(TQs: FastQuals);
7795 }
7796
7797 unsigned Index = translateTypeIDToIndex(ID).second;
7798
7799 assert(Index < TypesLoaded.size() && "Type index out-of-range");
7800 if (TypesLoaded[Index].isNull()) {
7801 TypesLoaded[Index] = readTypeRecord(ID);
7802 if (TypesLoaded[Index].isNull())
7803 return QualType();
7804
7805 TypesLoaded[Index]->setFromAST();
7806 if (DeserializationListener)
7807 DeserializationListener->TypeRead(Idx: TypeIdx::fromTypeID(ID),
7808 T: TypesLoaded[Index]);
7809 }
7810
7811 return TypesLoaded[Index].withFastQualifiers(TQs: FastQuals);
7812}
7813
7814QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {
7815 return GetType(ID: getGlobalTypeID(F, LocalID));
7816}
7817
7818serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
7819 LocalTypeID LocalID) const {
7820 if (isPredefinedType(ID: LocalID))
7821 return LocalID;
7822
7823 if (!F.ModuleOffsetMap.empty())
7824 ReadModuleOffsetMap(F);
7825
7826 unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID: LocalID);
7827 LocalID &= llvm::maskTrailingOnes<TypeID>(N: 32);
7828
7829 if (ModuleFileIndex == 0)
7830 LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
7831
7832 ModuleFile &MF =
7833 ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
7834 ModuleFileIndex = MF.Index + 1;
7835 return ((uint64_t)ModuleFileIndex << 32) | LocalID;
7836}
7837
7838TemplateArgumentLocInfo
7839ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7840 switch (Kind) {
7841 case TemplateArgument::Expression:
7842 return readExpr();
7843 case TemplateArgument::Type:
7844 return readTypeSourceInfo();
7845 case TemplateArgument::Template: {
7846 NestedNameSpecifierLoc QualifierLoc =
7847 readNestedNameSpecifierLoc();
7848 SourceLocation TemplateNameLoc = readSourceLocation();
7849 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7850 TemplateNameLoc, SourceLocation());
7851 }
7852 case TemplateArgument::TemplateExpansion: {
7853 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7854 SourceLocation TemplateNameLoc = readSourceLocation();
7855 SourceLocation EllipsisLoc = readSourceLocation();
7856 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7857 TemplateNameLoc, EllipsisLoc);
7858 }
7859 case TemplateArgument::Null:
7860 case TemplateArgument::Integral:
7861 case TemplateArgument::Declaration:
7862 case TemplateArgument::NullPtr:
7863 case TemplateArgument::StructuralValue:
7864 case TemplateArgument::Pack:
7865 // FIXME: Is this right?
7866 return TemplateArgumentLocInfo();
7867 }
7868 llvm_unreachable("unexpected template argument loc");
7869}
7870
7871TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7872 TemplateArgument Arg = readTemplateArgument();
7873
7874 if (Arg.getKind() == TemplateArgument::Expression) {
7875 if (readBool()) // bool InfoHasSameExpr.
7876 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7877 }
7878 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Kind: Arg.getKind()));
7879}
7880
7881void ASTRecordReader::readTemplateArgumentListInfo(
7882 TemplateArgumentListInfo &Result) {
7883 Result.setLAngleLoc(readSourceLocation());
7884 Result.setRAngleLoc(readSourceLocation());
7885 unsigned NumArgsAsWritten = readInt();
7886 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7887 Result.addArgument(Loc: readTemplateArgumentLoc());
7888}
7889
7890const ASTTemplateArgumentListInfo *
7891ASTRecordReader::readASTTemplateArgumentListInfo() {
7892 TemplateArgumentListInfo Result;
7893 readTemplateArgumentListInfo(Result);
7894 return ASTTemplateArgumentListInfo::Create(C: getContext(), List: Result);
7895}
7896
7897Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }
7898
7899void ASTReader::CompleteRedeclChain(const Decl *D) {
7900 if (NumCurrentElementsDeserializing) {
7901 // We arrange to not care about the complete redeclaration chain while we're
7902 // deserializing. Just remember that the AST has marked this one as complete
7903 // but that it's not actually complete yet, so we know we still need to
7904 // complete it later.
7905 PendingIncompleteDeclChains.push_back(Elt: const_cast<Decl*>(D));
7906 return;
7907 }
7908
7909 if (!D->getDeclContext()) {
7910 assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7911 return;
7912 }
7913
7914 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7915
7916 // If this is a named declaration, complete it by looking it up
7917 // within its context.
7918 //
7919 // FIXME: Merging a function definition should merge
7920 // all mergeable entities within it.
7921 if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(Val: DC)) {
7922 if (DeclarationName Name = cast<NamedDecl>(Val: D)->getDeclName()) {
7923 if (!getContext().getLangOpts().CPlusPlus &&
7924 isa<TranslationUnitDecl>(Val: DC)) {
7925 // Outside of C++, we don't have a lookup table for the TU, so update
7926 // the identifier instead. (For C++ modules, we don't store decls
7927 // in the serialized identifier table, so we do the lookup in the TU.)
7928 auto *II = Name.getAsIdentifierInfo();
7929 assert(II && "non-identifier name in C?");
7930 if (II->isOutOfDate())
7931 updateOutOfDateIdentifier(II: *II);
7932 } else
7933 DC->lookup(Name);
7934 } else if (needsAnonymousDeclarationNumber(D: cast<NamedDecl>(Val: D))) {
7935 // Find all declarations of this kind from the relevant context.
7936 for (auto *DCDecl : cast<Decl>(Val: D->getLexicalDeclContext())->redecls()) {
7937 auto *DC = cast<DeclContext>(Val: DCDecl);
7938 SmallVector<Decl*, 8> Decls;
7939 FindExternalLexicalDecls(
7940 DC, IsKindWeWant: [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7941 }
7942 }
7943 }
7944
7945 RedeclarableTemplateDecl *Template = nullptr;
7946 ArrayRef<TemplateArgument> Args;
7947 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D)) {
7948 Template = CTSD->getSpecializedTemplate();
7949 Args = CTSD->getTemplateArgs().asArray();
7950 } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D)) {
7951 Template = VTSD->getSpecializedTemplate();
7952 Args = VTSD->getTemplateArgs().asArray();
7953 } else if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
7954 if (auto *Tmplt = FD->getPrimaryTemplate()) {
7955 Template = Tmplt;
7956 Args = FD->getTemplateSpecializationArgs()->asArray();
7957 }
7958 }
7959
7960 if (Template) {
7961 // For partitial specialization, load all the specializations for safety.
7962 if (isa<ClassTemplatePartialSpecializationDecl,
7963 VarTemplatePartialSpecializationDecl>(Val: D))
7964 Template->loadLazySpecializationsImpl();
7965 else
7966 Template->loadLazySpecializationsImpl(Args);
7967 }
7968}
7969
7970CXXCtorInitializer **
7971ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7972 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
7973 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7974 SavedStreamPosition SavedPosition(Cursor);
7975 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
7976 Error(Err: std::move(Err));
7977 return nullptr;
7978 }
7979 ReadingKindTracker ReadingKind(Read_Decl, *this);
7980 Deserializing D(this);
7981
7982 Expected<unsigned> MaybeCode = Cursor.ReadCode();
7983 if (!MaybeCode) {
7984 Error(Err: MaybeCode.takeError());
7985 return nullptr;
7986 }
7987 unsigned Code = MaybeCode.get();
7988
7989 ASTRecordReader Record(*this, *Loc.F);
7990 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
7991 if (!MaybeRecCode) {
7992 Error(Err: MaybeRecCode.takeError());
7993 return nullptr;
7994 }
7995 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7996 Error(Msg: "malformed AST file: missing C++ ctor initializers");
7997 return nullptr;
7998 }
7999
8000 return Record.readCXXCtorInitializers();
8001}
8002
8003CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
8004 assert(ContextObj && "reading base specifiers with no AST context");
8005 ASTContext &Context = *ContextObj;
8006
8007 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8008 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
8009 SavedStreamPosition SavedPosition(Cursor);
8010 if (llvm::Error Err = Cursor.JumpToBit(BitNo: Loc.Offset)) {
8011 Error(Err: std::move(Err));
8012 return nullptr;
8013 }
8014 ReadingKindTracker ReadingKind(Read_Decl, *this);
8015 Deserializing D(this);
8016
8017 Expected<unsigned> MaybeCode = Cursor.ReadCode();
8018 if (!MaybeCode) {
8019 Error(Err: MaybeCode.takeError());
8020 return nullptr;
8021 }
8022 unsigned Code = MaybeCode.get();
8023
8024 ASTRecordReader Record(*this, *Loc.F);
8025 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, AbbrevID: Code);
8026 if (!MaybeRecCode) {
8027 Error(Err: MaybeCode.takeError());
8028 return nullptr;
8029 }
8030 unsigned RecCode = MaybeRecCode.get();
8031
8032 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
8033 Error(Msg: "malformed AST file: missing C++ base specifiers");
8034 return nullptr;
8035 }
8036
8037 unsigned NumBases = Record.readInt();
8038 void *Mem = Context.Allocate(Size: sizeof(CXXBaseSpecifier) * NumBases);
8039 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
8040 for (unsigned I = 0; I != NumBases; ++I)
8041 Bases[I] = Record.readCXXBaseSpecifier();
8042 return Bases;
8043}
8044
8045GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,
8046 LocalDeclID LocalID) const {
8047 if (LocalID < NUM_PREDEF_DECL_IDS)
8048 return GlobalDeclID(LocalID.getRawValue());
8049
8050 unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();
8051 DeclID ID = LocalID.getLocalDeclIndex();
8052
8053 if (!F.ModuleOffsetMap.empty())
8054 ReadModuleOffsetMap(F);
8055
8056 ModuleFile *OwningModuleFile =
8057 OwningModuleFileIndex == 0
8058 ? &F
8059 : F.TransitiveImports[OwningModuleFileIndex - 1];
8060
8061 if (OwningModuleFileIndex == 0)
8062 ID -= NUM_PREDEF_DECL_IDS;
8063
8064 uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;
8065 return GlobalDeclID(NewModuleFileIndex, ID);
8066}
8067
8068bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {
8069 // Predefined decls aren't from any module.
8070 if (ID < NUM_PREDEF_DECL_IDS)
8071 return false;
8072
8073 unsigned ModuleFileIndex = ID.getModuleFileIndex();
8074 return M.Index == ModuleFileIndex - 1;
8075}
8076
8077ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
8078 // Predefined decls aren't from any module.
8079 if (ID < NUM_PREDEF_DECL_IDS)
8080 return nullptr;
8081
8082 uint64_t ModuleFileIndex = ID.getModuleFileIndex();
8083 assert(ModuleFileIndex && "Untranslated Local Decl?");
8084
8085 return &getModuleManager()[ModuleFileIndex - 1];
8086}
8087
8088ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
8089 if (!D->isFromASTFile())
8090 return nullptr;
8091
8092 return getOwningModuleFile(ID: D->getGlobalID());
8093}
8094
8095SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
8096 if (ID < NUM_PREDEF_DECL_IDS)
8097 return SourceLocation();
8098
8099 if (Decl *D = GetExistingDecl(ID))
8100 return D->getLocation();
8101
8102 SourceLocation Loc;
8103 DeclCursorForID(ID, Location&: Loc);
8104 return Loc;
8105}
8106
8107Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {
8108 assert(ContextObj && "reading predefined decl without AST context");
8109 ASTContext &Context = *ContextObj;
8110 Decl *NewLoaded = nullptr;
8111 switch (ID) {
8112 case PREDEF_DECL_NULL_ID:
8113 return nullptr;
8114
8115 case PREDEF_DECL_TRANSLATION_UNIT_ID:
8116 return Context.getTranslationUnitDecl();
8117
8118 case PREDEF_DECL_OBJC_ID_ID:
8119 if (Context.ObjCIdDecl)
8120 return Context.ObjCIdDecl;
8121 NewLoaded = Context.getObjCIdDecl();
8122 break;
8123
8124 case PREDEF_DECL_OBJC_SEL_ID:
8125 if (Context.ObjCSelDecl)
8126 return Context.ObjCSelDecl;
8127 NewLoaded = Context.getObjCSelDecl();
8128 break;
8129
8130 case PREDEF_DECL_OBJC_CLASS_ID:
8131 if (Context.ObjCClassDecl)
8132 return Context.ObjCClassDecl;
8133 NewLoaded = Context.getObjCClassDecl();
8134 break;
8135
8136 case PREDEF_DECL_OBJC_PROTOCOL_ID:
8137 if (Context.ObjCProtocolClassDecl)
8138 return Context.ObjCProtocolClassDecl;
8139 NewLoaded = Context.getObjCProtocolDecl();
8140 break;
8141
8142 case PREDEF_DECL_INT_128_ID:
8143 if (Context.Int128Decl)
8144 return Context.Int128Decl;
8145 NewLoaded = Context.getInt128Decl();
8146 break;
8147
8148 case PREDEF_DECL_UNSIGNED_INT_128_ID:
8149 if (Context.UInt128Decl)
8150 return Context.UInt128Decl;
8151 NewLoaded = Context.getUInt128Decl();
8152 break;
8153
8154 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
8155 if (Context.ObjCInstanceTypeDecl)
8156 return Context.ObjCInstanceTypeDecl;
8157 NewLoaded = Context.getObjCInstanceTypeDecl();
8158 break;
8159
8160 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
8161 if (Context.BuiltinVaListDecl)
8162 return Context.BuiltinVaListDecl;
8163 NewLoaded = Context.getBuiltinVaListDecl();
8164 break;
8165
8166 case PREDEF_DECL_VA_LIST_TAG:
8167 if (Context.VaListTagDecl)
8168 return Context.VaListTagDecl;
8169 NewLoaded = Context.getVaListTagDecl();
8170 break;
8171
8172 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
8173 if (Context.BuiltinMSVaListDecl)
8174 return Context.BuiltinMSVaListDecl;
8175 NewLoaded = Context.getBuiltinMSVaListDecl();
8176 break;
8177
8178 case PREDEF_DECL_BUILTIN_MS_GUID_ID:
8179 // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.
8180 return Context.getMSGuidTagDecl();
8181
8182 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
8183 if (Context.ExternCContext)
8184 return Context.ExternCContext;
8185 NewLoaded = Context.getExternCContextDecl();
8186 break;
8187
8188 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
8189 if (Context.CFConstantStringTypeDecl)
8190 return Context.CFConstantStringTypeDecl;
8191 NewLoaded = Context.getCFConstantStringDecl();
8192 break;
8193
8194 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
8195 if (Context.CFConstantStringTagDecl)
8196 return Context.CFConstantStringTagDecl;
8197 NewLoaded = Context.getCFConstantStringTagDecl();
8198 break;
8199
8200#define BuiltinTemplate(BTName) \
8201 case PREDEF_DECL##BTName##_ID: \
8202 if (Context.Decl##BTName) \
8203 return Context.Decl##BTName; \
8204 NewLoaded = Context.get##BTName##Decl(); \
8205 break;
8206#include "clang/Basic/BuiltinTemplates.inc"
8207
8208 case NUM_PREDEF_DECL_IDS:
8209 llvm_unreachable("Invalid decl ID");
8210 break;
8211 }
8212
8213 assert(NewLoaded && "Failed to load predefined decl?");
8214
8215 if (DeserializationListener)
8216 DeserializationListener->PredefinedDeclBuilt(ID, D: NewLoaded);
8217
8218 return NewLoaded;
8219}
8220
8221unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {
8222 ModuleFile *OwningModuleFile = getOwningModuleFile(ID: GlobalID);
8223 if (!OwningModuleFile) {
8224 assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");
8225 return GlobalID.getRawValue();
8226 }
8227
8228 return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();
8229}
8230
8231Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {
8232 assert(ContextObj && "reading decl with no AST context");
8233
8234 if (ID < NUM_PREDEF_DECL_IDS) {
8235 Decl *D = getPredefinedDecl(ID: (PredefinedDeclIDs)ID);
8236 if (D) {
8237 // Track that we have merged the declaration with ID \p ID into the
8238 // pre-existing predefined declaration \p D.
8239 auto &Merged = KeyDecls[D->getCanonicalDecl()];
8240 if (Merged.empty())
8241 Merged.push_back(Elt: ID);
8242 }
8243 return D;
8244 }
8245
8246 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8247
8248 if (Index >= DeclsLoaded.size()) {
8249 assert(0 && "declaration ID out-of-range for AST file");
8250 Error(Msg: "declaration ID out-of-range for AST file");
8251 return nullptr;
8252 }
8253
8254 return DeclsLoaded[Index];
8255}
8256
8257Decl *ASTReader::GetDecl(GlobalDeclID ID) {
8258 if (ID < NUM_PREDEF_DECL_IDS)
8259 return GetExistingDecl(ID);
8260
8261 unsigned Index = translateGlobalDeclIDToIndex(GlobalID: ID);
8262
8263 if (Index >= DeclsLoaded.size()) {
8264 assert(0 && "declaration ID out-of-range for AST file");
8265 Error(Msg: "declaration ID out-of-range for AST file");
8266 return nullptr;
8267 }
8268
8269 if (!DeclsLoaded[Index]) {
8270 ReadDeclRecord(ID);
8271 if (DeserializationListener)
8272 DeserializationListener->DeclRead(ID, D: DeclsLoaded[Index]);
8273 }
8274
8275 return DeclsLoaded[Index];
8276}
8277
8278LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
8279 GlobalDeclID GlobalID) {
8280 if (GlobalID < NUM_PREDEF_DECL_IDS)
8281 return LocalDeclID::get(Reader&: *this, MF&: M, Value: GlobalID.getRawValue());
8282
8283 if (!M.ModuleOffsetMap.empty())
8284 ReadModuleOffsetMap(F&: M);
8285
8286 ModuleFile *Owner = getOwningModuleFile(ID: GlobalID);
8287 DeclID ID = GlobalID.getLocalDeclIndex();
8288
8289 if (Owner == &M) {
8290 ID += NUM_PREDEF_DECL_IDS;
8291 return LocalDeclID::get(Reader&: *this, MF&: M, Value: ID);
8292 }
8293
8294 uint64_t OrignalModuleFileIndex = 0;
8295 for (unsigned I = 0; I < M.TransitiveImports.size(); I++)
8296 if (M.TransitiveImports[I] == Owner) {
8297 OrignalModuleFileIndex = I + 1;
8298 break;
8299 }
8300
8301 if (!OrignalModuleFileIndex)
8302 return LocalDeclID();
8303
8304 return LocalDeclID::get(Reader&: *this, MF&: M, ModuleFileIndex: OrignalModuleFileIndex, LocalDeclID: ID);
8305}
8306
8307GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
8308 unsigned &Idx) {
8309 if (Idx >= Record.size()) {
8310 Error(Msg: "Corrupted AST file");
8311 return GlobalDeclID(0);
8312 }
8313
8314 return getGlobalDeclID(F, LocalID: LocalDeclID::get(Reader&: *this, MF&: F, Value: Record[Idx++]));
8315}
8316
8317/// Resolve the offset of a statement into a statement.
8318///
8319/// This operation will read a new statement from the external
8320/// source each time it is called, and is meant to be used via a
8321/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
8322Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
8323 // Switch case IDs are per Decl.
8324 ClearSwitchCaseIDs();
8325
8326 // Offset here is a global offset across the entire chain.
8327 RecordLocation Loc = getLocalBitOffset(GlobalOffset: Offset);
8328 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(BitNo: Loc.Offset)) {
8329 Error(Err: std::move(Err));
8330 return nullptr;
8331 }
8332 assert(NumCurrentElementsDeserializing == 0 &&
8333 "should not be called while already deserializing");
8334 Deserializing D(this);
8335 return ReadStmtFromStream(F&: *Loc.F);
8336}
8337
8338bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,
8339 const Decl *D) {
8340 assert(D);
8341
8342 auto It = SpecLookups.find(Val: D);
8343 if (It == SpecLookups.end())
8344 return false;
8345
8346 // Get Decl may violate the iterator from SpecializationsLookups so we store
8347 // the DeclIDs in ahead.
8348 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8349 It->second.Table.findAll();
8350
8351 // Since we've loaded all the specializations, we can erase it from
8352 // the lookup table.
8353 SpecLookups.erase(I: It);
8354
8355 bool NewSpecsFound = false;
8356 Deserializing LookupResults(this);
8357 for (auto &Info : Infos) {
8358 if (GetExistingDecl(ID: Info))
8359 continue;
8360 NewSpecsFound = true;
8361 GetDecl(ID: Info);
8362 }
8363
8364 return NewSpecsFound;
8365}
8366
8367bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {
8368 assert(D);
8369
8370 bool NewSpecsFound =
8371 LoadExternalSpecializationsImpl(SpecLookups&: PartialSpecializationsLookups, D);
8372 if (OnlyPartial)
8373 return NewSpecsFound;
8374
8375 NewSpecsFound |= LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D);
8376 return NewSpecsFound;
8377}
8378
8379bool ASTReader::LoadExternalSpecializationsImpl(
8380 SpecLookupTableTy &SpecLookups, const Decl *D,
8381 ArrayRef<TemplateArgument> TemplateArgs) {
8382 assert(D);
8383
8384 auto It = SpecLookups.find(Val: D);
8385 if (It == SpecLookups.end())
8386 return false;
8387
8388 llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {
8389 std::string Name;
8390 llvm::raw_string_ostream OS(Name);
8391 auto *ND = cast<NamedDecl>(Val: D);
8392 ND->getNameForDiagnostic(OS, Policy: ND->getASTContext().getPrintingPolicy(),
8393 /*Qualified=*/true);
8394 return Name;
8395 });
8396
8397 Deserializing LookupResults(this);
8398 auto HashValue = StableHashForTemplateArguments(Args: TemplateArgs);
8399
8400 // Get Decl may violate the iterator from SpecLookups
8401 llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =
8402 It->second.Table.find(EKey: HashValue);
8403
8404 bool NewSpecsFound = false;
8405 for (auto &Info : Infos) {
8406 if (GetExistingDecl(ID: Info))
8407 continue;
8408 NewSpecsFound = true;
8409 GetDecl(ID: Info);
8410 }
8411
8412 return NewSpecsFound;
8413}
8414
8415bool ASTReader::LoadExternalSpecializations(
8416 const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {
8417 assert(D);
8418
8419 bool NewDeclsFound = LoadExternalSpecializationsImpl(
8420 SpecLookups&: PartialSpecializationsLookups, D, TemplateArgs);
8421 NewDeclsFound |=
8422 LoadExternalSpecializationsImpl(SpecLookups&: SpecializationsLookups, D, TemplateArgs);
8423
8424 return NewDeclsFound;
8425}
8426
8427void ASTReader::FindExternalLexicalDecls(
8428 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
8429 SmallVectorImpl<Decl *> &Decls) {
8430 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
8431
8432 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
8433 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
8434 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
8435 auto K = (Decl::Kind)+LexicalDecls[I];
8436 if (!IsKindWeWant(K))
8437 continue;
8438
8439 auto ID = (DeclID) + LexicalDecls[I + 1];
8440
8441 // Don't add predefined declarations to the lexical context more
8442 // than once.
8443 if (ID < NUM_PREDEF_DECL_IDS) {
8444 if (PredefsVisited[ID])
8445 continue;
8446
8447 PredefsVisited[ID] = true;
8448 }
8449
8450 if (Decl *D = GetLocalDecl(F&: *M, LocalID: LocalDeclID::get(Reader&: *this, MF&: *M, Value: ID))) {
8451 assert(D->getKind() == K && "wrong kind for lexical decl");
8452 if (!DC->isDeclInLexicalTraversal(D))
8453 Decls.push_back(Elt: D);
8454 }
8455 }
8456 };
8457
8458 if (isa<TranslationUnitDecl>(Val: DC)) {
8459 for (const auto &Lexical : TULexicalDecls)
8460 Visit(Lexical.first, Lexical.second);
8461 } else {
8462 auto I = LexicalDecls.find(Val: DC);
8463 if (I != LexicalDecls.end())
8464 Visit(I->second.first, I->second.second);
8465 }
8466
8467 ++NumLexicalDeclContextsRead;
8468}
8469
8470namespace {
8471
8472class UnalignedDeclIDComp {
8473 ASTReader &Reader;
8474 ModuleFile &Mod;
8475
8476public:
8477 UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)
8478 : Reader(Reader), Mod(M) {}
8479
8480 bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {
8481 SourceLocation LHS = getLocation(ID: L);
8482 SourceLocation RHS = getLocation(ID: R);
8483 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8484 }
8485
8486 bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {
8487 SourceLocation RHS = getLocation(ID: R);
8488 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8489 }
8490
8491 bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {
8492 SourceLocation LHS = getLocation(ID: L);
8493 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
8494 }
8495
8496 SourceLocation getLocation(unaligned_decl_id_t ID) const {
8497 return Reader.getSourceManager().getFileLoc(
8498 Loc: Reader.getSourceLocationForDeclID(
8499 ID: Reader.getGlobalDeclID(F&: Mod, LocalID: LocalDeclID::get(Reader, MF&: Mod, Value: ID))));
8500 }
8501};
8502
8503} // namespace
8504
8505void ASTReader::FindFileRegionDecls(FileID File,
8506 unsigned Offset, unsigned Length,
8507 SmallVectorImpl<Decl *> &Decls) {
8508 SourceManager &SM = getSourceManager();
8509
8510 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(Val: File);
8511 if (I == FileDeclIDs.end())
8512 return;
8513
8514 FileDeclsInfo &DInfo = I->second;
8515 if (DInfo.Decls.empty())
8516 return;
8517
8518 SourceLocation
8519 BeginLoc = SM.getLocForStartOfFile(FID: File).getLocWithOffset(Offset);
8520 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Offset: Length);
8521
8522 UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);
8523 ArrayRef<unaligned_decl_id_t>::iterator BeginIt =
8524 llvm::lower_bound(Range&: DInfo.Decls, Value&: BeginLoc, C: DIDComp);
8525 if (BeginIt != DInfo.Decls.begin())
8526 --BeginIt;
8527
8528 // If we are pointing at a top-level decl inside an objc container, we need
8529 // to backtrack until we find it otherwise we will fail to report that the
8530 // region overlaps with an objc container.
8531 while (BeginIt != DInfo.Decls.begin() &&
8532 GetDecl(ID: getGlobalDeclID(F&: *DInfo.Mod,
8533 LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *BeginIt)))
8534 ->isTopLevelDeclInObjCContainer())
8535 --BeginIt;
8536
8537 ArrayRef<unaligned_decl_id_t>::iterator EndIt =
8538 llvm::upper_bound(Range&: DInfo.Decls, Value&: EndLoc, C: DIDComp);
8539 if (EndIt != DInfo.Decls.end())
8540 ++EndIt;
8541
8542 for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;
8543 ++DIt)
8544 Decls.push_back(Elt: GetDecl(ID: getGlobalDeclID(
8545 F&: *DInfo.Mod, LocalID: LocalDeclID::get(Reader&: *this, MF&: *DInfo.Mod, Value: *DIt))));
8546}
8547
8548bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8549 DeclarationName Name,
8550 const DeclContext *OriginalDC) {
8551 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
8552 "DeclContext has no visible decls in storage");
8553 if (!Name)
8554 return false;
8555
8556 // Load the list of declarations.
8557 SmallVector<NamedDecl *, 64> Decls;
8558 llvm::SmallPtrSet<NamedDecl *, 8> Found;
8559
8560 auto Find = [&, this](auto &&Table, auto &&Key) {
8561 for (GlobalDeclID ID : Table.find(Key)) {
8562 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8563 if (ND->getDeclName() == Name && Found.insert(Ptr: ND).second)
8564 Decls.push_back(Elt: ND);
8565 }
8566 };
8567
8568 Deserializing LookupResults(this);
8569
8570 // FIXME: Clear the redundancy with templated lambda in C++20 when that's
8571 // available.
8572 if (auto It = Lookups.find(Val: DC); It != Lookups.end()) {
8573 ++NumVisibleDeclContextsRead;
8574 Find(It->second.Table, Name);
8575 }
8576
8577 auto FindModuleLocalLookup = [&, this](Module *NamedModule) {
8578 if (auto It = ModuleLocalLookups.find(Val: DC); It != ModuleLocalLookups.end()) {
8579 ++NumModuleLocalVisibleDeclContexts;
8580 Find(It->second.Table, std::make_pair(x&: Name, y&: NamedModule));
8581 }
8582 };
8583 if (auto *NamedModule =
8584 OriginalDC ? cast<Decl>(Val: OriginalDC)->getTopLevelOwningNamedModule()
8585 : nullptr)
8586 FindModuleLocalLookup(NamedModule);
8587 // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.
8588 // We're going to find a decl but the decl context of the lookup is
8589 // unspecified. In this case, the OriginalDC may be the decl context in other
8590 // module.
8591 if (ContextObj && ContextObj->getCurrentNamedModule())
8592 FindModuleLocalLookup(ContextObj->getCurrentNamedModule());
8593
8594 if (auto It = TULocalLookups.find(Val: DC); It != TULocalLookups.end()) {
8595 ++NumTULocalVisibleDeclContexts;
8596 Find(It->second.Table, Name);
8597 }
8598
8599 SetExternalVisibleDeclsForName(DC, Name, Decls);
8600 return !Decls.empty();
8601}
8602
8603void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
8604 if (!DC->hasExternalVisibleStorage())
8605 return;
8606
8607 DeclsMap Decls;
8608
8609 auto findAll = [&](auto &LookupTables, unsigned &NumRead) {
8610 auto It = LookupTables.find(DC);
8611 if (It == LookupTables.end())
8612 return;
8613
8614 NumRead++;
8615
8616 for (GlobalDeclID ID : It->second.Table.findAll()) {
8617 NamedDecl *ND = cast<NamedDecl>(Val: GetDecl(ID));
8618 Decls[ND->getDeclName()].push_back(Elt: ND);
8619 }
8620
8621 // FIXME: Why a PCH test is failing if we remove the iterator after findAll?
8622 };
8623
8624 findAll(Lookups, NumVisibleDeclContextsRead);
8625 findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);
8626 findAll(TULocalLookups, NumTULocalVisibleDeclContexts);
8627
8628 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
8629 SetExternalVisibleDeclsForName(DC, Name: I->first, Decls: I->second);
8630 }
8631 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
8632}
8633
8634const serialization::reader::DeclContextLookupTable *
8635ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
8636 auto I = Lookups.find(Val: Primary);
8637 return I == Lookups.end() ? nullptr : &I->second;
8638}
8639
8640const serialization::reader::ModuleLocalLookupTable *
8641ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {
8642 auto I = ModuleLocalLookups.find(Val: Primary);
8643 return I == ModuleLocalLookups.end() ? nullptr : &I->second;
8644}
8645
8646const serialization::reader::DeclContextLookupTable *
8647ASTReader::getTULocalLookupTables(DeclContext *Primary) const {
8648 auto I = TULocalLookups.find(Val: Primary);
8649 return I == TULocalLookups.end() ? nullptr : &I->second;
8650}
8651
8652serialization::reader::LazySpecializationInfoLookupTable *
8653ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {
8654 assert(D->isCanonicalDecl());
8655 auto &LookupTable =
8656 IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;
8657 auto I = LookupTable.find(Val: D);
8658 return I == LookupTable.end() ? nullptr : &I->second;
8659}
8660
8661bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {
8662 assert(D->isCanonicalDecl());
8663 return PartialSpecializationsLookups.contains(Val: D) ||
8664 SpecializationsLookups.contains(Val: D);
8665}
8666
8667/// Under non-PCH compilation the consumer receives the objc methods
8668/// before receiving the implementation, and codegen depends on this.
8669/// We simulate this by deserializing and passing to consumer the methods of the
8670/// implementation before passing the deserialized implementation decl.
8671static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
8672 ASTConsumer *Consumer) {
8673 assert(ImplD && Consumer);
8674
8675 for (auto *I : ImplD->methods())
8676 Consumer->HandleInterestingDecl(D: DeclGroupRef(I));
8677
8678 Consumer->HandleInterestingDecl(D: DeclGroupRef(ImplD));
8679}
8680
8681void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
8682 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(Val: D))
8683 PassObjCImplDeclToConsumer(ImplD, Consumer);
8684 else
8685 Consumer->HandleInterestingDecl(D: DeclGroupRef(D));
8686}
8687
8688void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {
8689 Consumer->HandleVTable(RD);
8690}
8691
8692void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
8693 this->Consumer = Consumer;
8694
8695 if (Consumer)
8696 PassInterestingDeclsToConsumer();
8697
8698 if (DeserializationListener)
8699 DeserializationListener->ReaderInitialized(Reader: this);
8700}
8701
8702void ASTReader::PrintStats() {
8703 std::fprintf(stderr, format: "*** AST File Statistics:\n");
8704
8705 unsigned NumTypesLoaded =
8706 TypesLoaded.size() - llvm::count(Range: TypesLoaded.materialized(), Element: QualType());
8707 unsigned NumDeclsLoaded =
8708 DeclsLoaded.size() -
8709 llvm::count(Range: DeclsLoaded.materialized(), Element: (Decl *)nullptr);
8710 unsigned NumIdentifiersLoaded =
8711 IdentifiersLoaded.size() -
8712 llvm::count(Range&: IdentifiersLoaded, Element: (IdentifierInfo *)nullptr);
8713 unsigned NumMacrosLoaded =
8714 MacrosLoaded.size() - llvm::count(Range&: MacrosLoaded, Element: (MacroInfo *)nullptr);
8715 unsigned NumSelectorsLoaded =
8716 SelectorsLoaded.size() - llvm::count(Range&: SelectorsLoaded, Element: Selector());
8717
8718 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
8719 std::fprintf(stderr, format: " %u/%u source location entries read (%f%%)\n",
8720 NumSLocEntriesRead, TotalNumSLocEntries,
8721 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
8722 if (!TypesLoaded.empty())
8723 std::fprintf(stderr, format: " %u/%u types read (%f%%)\n",
8724 NumTypesLoaded, (unsigned)TypesLoaded.size(),
8725 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
8726 if (!DeclsLoaded.empty())
8727 std::fprintf(stderr, format: " %u/%u declarations read (%f%%)\n",
8728 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
8729 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
8730 if (!IdentifiersLoaded.empty())
8731 std::fprintf(stderr, format: " %u/%u identifiers read (%f%%)\n",
8732 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
8733 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
8734 if (!MacrosLoaded.empty())
8735 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8736 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
8737 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
8738 if (!SelectorsLoaded.empty())
8739 std::fprintf(stderr, format: " %u/%u selectors read (%f%%)\n",
8740 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
8741 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
8742 if (TotalNumStatements)
8743 std::fprintf(stderr, format: " %u/%u statements read (%f%%)\n",
8744 NumStatementsRead, TotalNumStatements,
8745 ((float)NumStatementsRead/TotalNumStatements * 100));
8746 if (TotalNumMacros)
8747 std::fprintf(stderr, format: " %u/%u macros read (%f%%)\n",
8748 NumMacrosRead, TotalNumMacros,
8749 ((float)NumMacrosRead/TotalNumMacros * 100));
8750 if (TotalLexicalDeclContexts)
8751 std::fprintf(stderr, format: " %u/%u lexical declcontexts read (%f%%)\n",
8752 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
8753 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
8754 * 100));
8755 if (TotalVisibleDeclContexts)
8756 std::fprintf(stderr, format: " %u/%u visible declcontexts read (%f%%)\n",
8757 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
8758 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
8759 * 100));
8760 if (TotalModuleLocalVisibleDeclContexts)
8761 std::fprintf(
8762 stderr, format: " %u/%u module local visible declcontexts read (%f%%)\n",
8763 NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,
8764 ((float)NumModuleLocalVisibleDeclContexts /
8765 TotalModuleLocalVisibleDeclContexts * 100));
8766 if (TotalTULocalVisibleDeclContexts)
8767 std::fprintf(stderr, format: " %u/%u visible declcontexts in GMF read (%f%%)\n",
8768 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,
8769 ((float)NumTULocalVisibleDeclContexts /
8770 TotalTULocalVisibleDeclContexts * 100));
8771 if (TotalNumMethodPoolEntries)
8772 std::fprintf(stderr, format: " %u/%u method pool entries read (%f%%)\n",
8773 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
8774 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
8775 * 100));
8776 if (NumMethodPoolLookups)
8777 std::fprintf(stderr, format: " %u/%u method pool lookups succeeded (%f%%)\n",
8778 NumMethodPoolHits, NumMethodPoolLookups,
8779 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
8780 if (NumMethodPoolTableLookups)
8781 std::fprintf(stderr, format: " %u/%u method pool table lookups succeeded (%f%%)\n",
8782 NumMethodPoolTableHits, NumMethodPoolTableLookups,
8783 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
8784 * 100.0));
8785 if (NumIdentifierLookupHits)
8786 std::fprintf(stderr,
8787 format: " %u / %u identifier table lookups succeeded (%f%%)\n",
8788 NumIdentifierLookupHits, NumIdentifierLookups,
8789 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
8790
8791 if (GlobalIndex) {
8792 std::fprintf(stderr, format: "\n");
8793 GlobalIndex->printStats();
8794 }
8795
8796 std::fprintf(stderr, format: "\n");
8797 dump();
8798 std::fprintf(stderr, format: "\n");
8799}
8800
8801template<typename Key, typename ModuleFile, unsigned InitialCapacity>
8802LLVM_DUMP_METHOD static void
8803dumpModuleIDMap(StringRef Name,
8804 const ContinuousRangeMap<Key, ModuleFile *,
8805 InitialCapacity> &Map) {
8806 if (Map.begin() == Map.end())
8807 return;
8808
8809 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
8810
8811 llvm::errs() << Name << ":\n";
8812 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
8813 I != IEnd; ++I)
8814 llvm::errs() << " " << (DeclID)I->first << " -> " << I->second->FileName
8815 << "\n";
8816}
8817
8818LLVM_DUMP_METHOD void ASTReader::dump() {
8819 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
8820 dumpModuleIDMap(Name: "Global bit offset map", Map: GlobalBitOffsetsMap);
8821 dumpModuleIDMap(Name: "Global source location entry map", Map: GlobalSLocEntryMap);
8822 dumpModuleIDMap(Name: "Global macro map", Map: GlobalMacroMap);
8823 dumpModuleIDMap(Name: "Global submodule map", Map: GlobalSubmoduleMap);
8824 dumpModuleIDMap(Name: "Global selector map", Map: GlobalSelectorMap);
8825 dumpModuleIDMap(Name: "Global preprocessed entity map",
8826 Map: GlobalPreprocessedEntityMap);
8827
8828 llvm::errs() << "\n*** PCH/Modules Loaded:";
8829 for (ModuleFile &M : ModuleMgr)
8830 M.dump();
8831}
8832
8833/// Return the amount of memory used by memory buffers, breaking down
8834/// by heap-backed versus mmap'ed memory.
8835void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
8836 for (ModuleFile &I : ModuleMgr) {
8837 if (llvm::MemoryBuffer *buf = I.Buffer) {
8838 size_t bytes = buf->getBufferSize();
8839 switch (buf->getBufferKind()) {
8840 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
8841 sizes.malloc_bytes += bytes;
8842 break;
8843 case llvm::MemoryBuffer::MemoryBuffer_MMap:
8844 sizes.mmap_bytes += bytes;
8845 break;
8846 }
8847 }
8848 }
8849}
8850
8851void ASTReader::InitializeSema(Sema &S) {
8852 SemaObj = &S;
8853 S.addExternalSource(E: this);
8854
8855 // Makes sure any declarations that were deserialized "too early"
8856 // still get added to the identifier's declaration chains.
8857 for (GlobalDeclID ID : PreloadedDeclIDs) {
8858 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID));
8859 pushExternalDeclIntoScope(D, Name: D->getDeclName());
8860 }
8861 PreloadedDeclIDs.clear();
8862
8863 // FIXME: What happens if these are changed by a module import?
8864 if (!FPPragmaOptions.empty()) {
8865 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
8866 FPOptionsOverride NewOverrides =
8867 FPOptionsOverride::getFromOpaqueInt(I: FPPragmaOptions[0]);
8868 SemaObj->CurFPFeatures =
8869 NewOverrides.applyOverrides(LO: SemaObj->getLangOpts());
8870 }
8871
8872 for (GlobalDeclID ID : DeclsWithEffectsToVerify) {
8873 Decl *D = GetDecl(ID);
8874 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
8875 SemaObj->addDeclWithEffects(D: FD, FX: FD->getFunctionEffects());
8876 else if (auto *BD = dyn_cast<BlockDecl>(Val: D))
8877 SemaObj->addDeclWithEffects(D: BD, FX: BD->getFunctionEffects());
8878 else
8879 llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");
8880 }
8881 DeclsWithEffectsToVerify.clear();
8882
8883 SemaObj->OpenCLFeatures = OpenCLExtensions;
8884
8885 UpdateSema();
8886}
8887
8888void ASTReader::UpdateSema() {
8889 assert(SemaObj && "no Sema to update");
8890
8891 // Load the offsets of the declarations that Sema references.
8892 // They will be lazily deserialized when needed.
8893 if (!SemaDeclRefs.empty()) {
8894 assert(SemaDeclRefs.size() % 3 == 0);
8895 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
8896 if (!SemaObj->StdNamespace)
8897 SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();
8898 if (!SemaObj->StdBadAlloc)
8899 SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();
8900 if (!SemaObj->StdAlignValT)
8901 SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();
8902 }
8903 SemaDeclRefs.clear();
8904 }
8905
8906 // Update the state of pragmas. Use the same API as if we had encountered the
8907 // pragma in the source.
8908 if(OptimizeOffPragmaLocation.isValid())
8909 SemaObj->ActOnPragmaOptimize(/* On = */ false, PragmaLoc: OptimizeOffPragmaLocation);
8910 if (PragmaMSStructState != -1)
8911 SemaObj->ActOnPragmaMSStruct(Kind: (PragmaMSStructKind)PragmaMSStructState);
8912 if (PointersToMembersPragmaLocation.isValid()) {
8913 SemaObj->ActOnPragmaMSPointersToMembers(
8914 Kind: (LangOptions::PragmaMSPointersToMembersKind)
8915 PragmaMSPointersToMembersState,
8916 PragmaLoc: PointersToMembersPragmaLocation);
8917 }
8918 SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;
8919
8920 if (PragmaAlignPackCurrentValue) {
8921 // The bottom of the stack might have a default value. It must be adjusted
8922 // to the current value to ensure that the packing state is preserved after
8923 // popping entries that were included/imported from a PCH/module.
8924 bool DropFirst = false;
8925 if (!PragmaAlignPackStack.empty() &&
8926 PragmaAlignPackStack.front().Location.isInvalid()) {
8927 assert(PragmaAlignPackStack.front().Value ==
8928 SemaObj->AlignPackStack.DefaultValue &&
8929 "Expected a default alignment value");
8930 SemaObj->AlignPackStack.Stack.emplace_back(
8931 Args&: PragmaAlignPackStack.front().SlotLabel,
8932 Args&: SemaObj->AlignPackStack.CurrentValue,
8933 Args&: SemaObj->AlignPackStack.CurrentPragmaLocation,
8934 Args&: PragmaAlignPackStack.front().PushLocation);
8935 DropFirst = true;
8936 }
8937 for (const auto &Entry :
8938 llvm::ArrayRef(PragmaAlignPackStack).drop_front(N: DropFirst ? 1 : 0)) {
8939 SemaObj->AlignPackStack.Stack.emplace_back(
8940 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8941 }
8942 if (PragmaAlignPackCurrentLocation.isInvalid()) {
8943 assert(*PragmaAlignPackCurrentValue ==
8944 SemaObj->AlignPackStack.DefaultValue &&
8945 "Expected a default align and pack value");
8946 // Keep the current values.
8947 } else {
8948 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
8949 SemaObj->AlignPackStack.CurrentPragmaLocation =
8950 PragmaAlignPackCurrentLocation;
8951 }
8952 }
8953 if (FpPragmaCurrentValue) {
8954 // The bottom of the stack might have a default value. It must be adjusted
8955 // to the current value to ensure that fp-pragma state is preserved after
8956 // popping entries that were included/imported from a PCH/module.
8957 bool DropFirst = false;
8958 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
8959 assert(FpPragmaStack.front().Value ==
8960 SemaObj->FpPragmaStack.DefaultValue &&
8961 "Expected a default pragma float_control value");
8962 SemaObj->FpPragmaStack.Stack.emplace_back(
8963 Args&: FpPragmaStack.front().SlotLabel, Args&: SemaObj->FpPragmaStack.CurrentValue,
8964 Args&: SemaObj->FpPragmaStack.CurrentPragmaLocation,
8965 Args&: FpPragmaStack.front().PushLocation);
8966 DropFirst = true;
8967 }
8968 for (const auto &Entry :
8969 llvm::ArrayRef(FpPragmaStack).drop_front(N: DropFirst ? 1 : 0))
8970 SemaObj->FpPragmaStack.Stack.emplace_back(
8971 Args: Entry.SlotLabel, Args: Entry.Value, Args: Entry.Location, Args: Entry.PushLocation);
8972 if (FpPragmaCurrentLocation.isInvalid()) {
8973 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
8974 "Expected a default pragma float_control value");
8975 // Keep the current values.
8976 } else {
8977 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
8978 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
8979 }
8980 }
8981
8982 // For non-modular AST files, restore visiblity of modules.
8983 for (auto &Import : PendingImportedModulesSema) {
8984 if (Import.ImportLoc.isInvalid())
8985 continue;
8986 if (Module *Imported = getSubmodule(GlobalID: Import.ID)) {
8987 SemaObj->makeModuleVisible(Mod: Imported, ImportLoc: Import.ImportLoc);
8988 }
8989 }
8990 PendingImportedModulesSema.clear();
8991}
8992
8993IdentifierInfo *ASTReader::get(StringRef Name) {
8994 // Note that we are loading an identifier.
8995 Deserializing AnIdentifier(this);
8996
8997 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8998 NumIdentifierLookups,
8999 NumIdentifierLookupHits);
9000
9001 // We don't need to do identifier table lookups in C++ modules (we preload
9002 // all interesting declarations, and don't need to use the scope for name
9003 // lookups). Perform the lookup in PCH files, though, since we don't build
9004 // a complete initial identifier table if we're carrying on from a PCH.
9005 if (PP.getLangOpts().CPlusPlus) {
9006 for (auto *F : ModuleMgr.pch_modules())
9007 if (Visitor(*F))
9008 break;
9009 } else {
9010 // If there is a global index, look there first to determine which modules
9011 // provably do not have any results for this identifier.
9012 GlobalModuleIndex::HitSet Hits;
9013 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
9014 if (!loadGlobalIndex()) {
9015 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
9016 HitsPtr = &Hits;
9017 }
9018 }
9019
9020 ModuleMgr.visit(Visitor, ModuleFilesHit: HitsPtr);
9021 }
9022
9023 IdentifierInfo *II = Visitor.getIdentifierInfo();
9024 markIdentifierUpToDate(II);
9025 return II;
9026}
9027
9028namespace clang {
9029
9030 /// An identifier-lookup iterator that enumerates all of the
9031 /// identifiers stored within a set of AST files.
9032 class ASTIdentifierIterator : public IdentifierIterator {
9033 /// The AST reader whose identifiers are being enumerated.
9034 const ASTReader &Reader;
9035
9036 /// The current index into the chain of AST files stored in
9037 /// the AST reader.
9038 unsigned Index;
9039
9040 /// The current position within the identifier lookup table
9041 /// of the current AST file.
9042 ASTIdentifierLookupTable::key_iterator Current;
9043
9044 /// The end position within the identifier lookup table of
9045 /// the current AST file.
9046 ASTIdentifierLookupTable::key_iterator End;
9047
9048 /// Whether to skip any modules in the ASTReader.
9049 bool SkipModules;
9050
9051 public:
9052 explicit ASTIdentifierIterator(const ASTReader &Reader,
9053 bool SkipModules = false);
9054
9055 StringRef Next() override;
9056 };
9057
9058} // namespace clang
9059
9060ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
9061 bool SkipModules)
9062 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
9063}
9064
9065StringRef ASTIdentifierIterator::Next() {
9066 while (Current == End) {
9067 // If we have exhausted all of our AST files, we're done.
9068 if (Index == 0)
9069 return StringRef();
9070
9071 --Index;
9072 ModuleFile &F = Reader.ModuleMgr[Index];
9073 if (SkipModules && F.isModule())
9074 continue;
9075
9076 ASTIdentifierLookupTable *IdTable =
9077 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
9078 Current = IdTable->key_begin();
9079 End = IdTable->key_end();
9080 }
9081
9082 // We have any identifiers remaining in the current AST file; return
9083 // the next one.
9084 StringRef Result = *Current;
9085 ++Current;
9086 return Result;
9087}
9088
9089namespace {
9090
9091/// A utility for appending two IdentifierIterators.
9092class ChainedIdentifierIterator : public IdentifierIterator {
9093 std::unique_ptr<IdentifierIterator> Current;
9094 std::unique_ptr<IdentifierIterator> Queued;
9095
9096public:
9097 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
9098 std::unique_ptr<IdentifierIterator> Second)
9099 : Current(std::move(First)), Queued(std::move(Second)) {}
9100
9101 StringRef Next() override {
9102 if (!Current)
9103 return StringRef();
9104
9105 StringRef result = Current->Next();
9106 if (!result.empty())
9107 return result;
9108
9109 // Try the queued iterator, which may itself be empty.
9110 Current.reset();
9111 std::swap(x&: Current, y&: Queued);
9112 return Next();
9113 }
9114};
9115
9116} // namespace
9117
9118IdentifierIterator *ASTReader::getIdentifiers() {
9119 if (!loadGlobalIndex()) {
9120 std::unique_ptr<IdentifierIterator> ReaderIter(
9121 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
9122 std::unique_ptr<IdentifierIterator> ModulesIter(
9123 GlobalIndex->createIdentifierIterator());
9124 return new ChainedIdentifierIterator(std::move(ReaderIter),
9125 std::move(ModulesIter));
9126 }
9127
9128 return new ASTIdentifierIterator(*this);
9129}
9130
9131namespace clang {
9132namespace serialization {
9133
9134 class ReadMethodPoolVisitor {
9135 ASTReader &Reader;
9136 Selector Sel;
9137 unsigned PriorGeneration;
9138 unsigned InstanceBits = 0;
9139 unsigned FactoryBits = 0;
9140 bool InstanceHasMoreThanOneDecl = false;
9141 bool FactoryHasMoreThanOneDecl = false;
9142 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
9143 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
9144
9145 public:
9146 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
9147 unsigned PriorGeneration)
9148 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
9149
9150 bool operator()(ModuleFile &M) {
9151 if (!M.SelectorLookupTable)
9152 return false;
9153
9154 // If we've already searched this module file, skip it now.
9155 if (M.Generation <= PriorGeneration)
9156 return true;
9157
9158 ++Reader.NumMethodPoolTableLookups;
9159 ASTSelectorLookupTable *PoolTable
9160 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
9161 ASTSelectorLookupTable::iterator Pos = PoolTable->find(EKey: Sel);
9162 if (Pos == PoolTable->end())
9163 return false;
9164
9165 ++Reader.NumMethodPoolTableHits;
9166 ++Reader.NumSelectorsRead;
9167 // FIXME: Not quite happy with the statistics here. We probably should
9168 // disable this tracking when called via LoadSelector.
9169 // Also, should entries without methods count as misses?
9170 ++Reader.NumMethodPoolEntriesRead;
9171 ASTSelectorLookupTrait::data_type Data = *Pos;
9172 if (Reader.DeserializationListener)
9173 Reader.DeserializationListener->SelectorRead(iD: Data.ID, Sel);
9174
9175 // Append methods in the reverse order, so that later we can process them
9176 // in the order they appear in the source code by iterating through
9177 // the vector in the reverse order.
9178 InstanceMethods.append(in_start: Data.Instance.rbegin(), in_end: Data.Instance.rend());
9179 FactoryMethods.append(in_start: Data.Factory.rbegin(), in_end: Data.Factory.rend());
9180 InstanceBits = Data.InstanceBits;
9181 FactoryBits = Data.FactoryBits;
9182 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
9183 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
9184 return false;
9185 }
9186
9187 /// Retrieve the instance methods found by this visitor.
9188 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
9189 return InstanceMethods;
9190 }
9191
9192 /// Retrieve the instance methods found by this visitor.
9193 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
9194 return FactoryMethods;
9195 }
9196
9197 unsigned getInstanceBits() const { return InstanceBits; }
9198 unsigned getFactoryBits() const { return FactoryBits; }
9199
9200 bool instanceHasMoreThanOneDecl() const {
9201 return InstanceHasMoreThanOneDecl;
9202 }
9203
9204 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
9205 };
9206
9207} // namespace serialization
9208} // namespace clang
9209
9210/// Add the given set of methods to the method list.
9211static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
9212 ObjCMethodList &List) {
9213 for (ObjCMethodDecl *M : llvm::reverse(C&: Methods))
9214 S.ObjC().addMethodToGlobalList(List: &List, Method: M);
9215}
9216
9217void ASTReader::ReadMethodPool(Selector Sel) {
9218 // Get the selector generation and update it to the current generation.
9219 unsigned &Generation = SelectorGeneration[Sel];
9220 unsigned PriorGeneration = Generation;
9221 Generation = getGeneration();
9222 SelectorOutOfDate[Sel] = false;
9223
9224 // Search for methods defined with this selector.
9225 ++NumMethodPoolLookups;
9226 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
9227 ModuleMgr.visit(Visitor);
9228
9229 if (Visitor.getInstanceMethods().empty() &&
9230 Visitor.getFactoryMethods().empty())
9231 return;
9232
9233 ++NumMethodPoolHits;
9234
9235 if (!getSema())
9236 return;
9237
9238 Sema &S = *getSema();
9239 auto &Methods = S.ObjC().MethodPool[Sel];
9240
9241 Methods.first.setBits(Visitor.getInstanceBits());
9242 Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
9243 Methods.second.setBits(Visitor.getFactoryBits());
9244 Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
9245
9246 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
9247 // when building a module we keep every method individually and may need to
9248 // update hasMoreThanOneDecl as we add the methods.
9249 addMethodsToPool(S, Methods: Visitor.getInstanceMethods(), List&: Methods.first);
9250 addMethodsToPool(S, Methods: Visitor.getFactoryMethods(), List&: Methods.second);
9251}
9252
9253void ASTReader::updateOutOfDateSelector(Selector Sel) {
9254 if (SelectorOutOfDate[Sel])
9255 ReadMethodPool(Sel);
9256}
9257
9258void ASTReader::ReadKnownNamespaces(
9259 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
9260 Namespaces.clear();
9261
9262 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
9263 if (NamespaceDecl *Namespace
9264 = dyn_cast_or_null<NamespaceDecl>(Val: GetDecl(ID: KnownNamespaces[I])))
9265 Namespaces.push_back(Elt: Namespace);
9266 }
9267}
9268
9269void ASTReader::ReadUndefinedButUsed(
9270 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
9271 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
9272 UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];
9273 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: U.ID));
9274 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: U.RawLoc);
9275 Undefined.insert(KV: std::make_pair(x&: D, y&: Loc));
9276 }
9277 UndefinedButUsed.clear();
9278}
9279
9280void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
9281 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
9282 Exprs) {
9283 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
9284 FieldDecl *FD =
9285 cast<FieldDecl>(Val: GetDecl(ID: GlobalDeclID(DelayedDeleteExprs[Idx++])));
9286 uint64_t Count = DelayedDeleteExprs[Idx++];
9287 for (uint64_t C = 0; C < Count; ++C) {
9288 SourceLocation DeleteLoc =
9289 SourceLocation::getFromRawEncoding(Encoding: DelayedDeleteExprs[Idx++]);
9290 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
9291 Exprs[FD].push_back(Elt: std::make_pair(x&: DeleteLoc, y: IsArrayForm));
9292 }
9293 }
9294}
9295
9296void ASTReader::ReadTentativeDefinitions(
9297 SmallVectorImpl<VarDecl *> &TentativeDefs) {
9298 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
9299 VarDecl *Var = dyn_cast_or_null<VarDecl>(Val: GetDecl(ID: TentativeDefinitions[I]));
9300 if (Var)
9301 TentativeDefs.push_back(Elt: Var);
9302 }
9303 TentativeDefinitions.clear();
9304}
9305
9306void ASTReader::ReadUnusedFileScopedDecls(
9307 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
9308 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
9309 DeclaratorDecl *D
9310 = dyn_cast_or_null<DeclaratorDecl>(Val: GetDecl(ID: UnusedFileScopedDecls[I]));
9311 if (D)
9312 Decls.push_back(Elt: D);
9313 }
9314 UnusedFileScopedDecls.clear();
9315}
9316
9317void ASTReader::ReadDelegatingConstructors(
9318 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
9319 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
9320 CXXConstructorDecl *D
9321 = dyn_cast_or_null<CXXConstructorDecl>(Val: GetDecl(ID: DelegatingCtorDecls[I]));
9322 if (D)
9323 Decls.push_back(Elt: D);
9324 }
9325 DelegatingCtorDecls.clear();
9326}
9327
9328void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
9329 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
9330 TypedefNameDecl *D
9331 = dyn_cast_or_null<TypedefNameDecl>(Val: GetDecl(ID: ExtVectorDecls[I]));
9332 if (D)
9333 Decls.push_back(Elt: D);
9334 }
9335 ExtVectorDecls.clear();
9336}
9337
9338void ASTReader::ReadUnusedLocalTypedefNameCandidates(
9339 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
9340 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
9341 ++I) {
9342 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
9343 Val: GetDecl(ID: UnusedLocalTypedefNameCandidates[I]));
9344 if (D)
9345 Decls.insert(X: D);
9346 }
9347 UnusedLocalTypedefNameCandidates.clear();
9348}
9349
9350void ASTReader::ReadDeclsToCheckForDeferredDiags(
9351 llvm::SmallSetVector<Decl *, 4> &Decls) {
9352 for (auto I : DeclsToCheckForDeferredDiags) {
9353 auto *D = dyn_cast_or_null<Decl>(Val: GetDecl(ID: I));
9354 if (D)
9355 Decls.insert(X: D);
9356 }
9357 DeclsToCheckForDeferredDiags.clear();
9358}
9359
9360void ASTReader::ReadReferencedSelectors(
9361 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
9362 if (ReferencedSelectorsData.empty())
9363 return;
9364
9365 // If there are @selector references added them to its pool. This is for
9366 // implementation of -Wselector.
9367 unsigned int DataSize = ReferencedSelectorsData.size()-1;
9368 unsigned I = 0;
9369 while (I < DataSize) {
9370 Selector Sel = DecodeSelector(Idx: ReferencedSelectorsData[I++]);
9371 SourceLocation SelLoc
9372 = SourceLocation::getFromRawEncoding(Encoding: ReferencedSelectorsData[I++]);
9373 Sels.push_back(Elt: std::make_pair(x&: Sel, y&: SelLoc));
9374 }
9375 ReferencedSelectorsData.clear();
9376}
9377
9378void ASTReader::ReadWeakUndeclaredIdentifiers(
9379 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
9380 if (WeakUndeclaredIdentifiers.empty())
9381 return;
9382
9383 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
9384 IdentifierInfo *WeakId
9385 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9386 IdentifierInfo *AliasId
9387 = DecodeIdentifierInfo(ID: WeakUndeclaredIdentifiers[I++]);
9388 SourceLocation Loc =
9389 SourceLocation::getFromRawEncoding(Encoding: WeakUndeclaredIdentifiers[I++]);
9390 WeakInfo WI(AliasId, Loc);
9391 WeakIDs.push_back(Elt: std::make_pair(x&: WeakId, y&: WI));
9392 }
9393 WeakUndeclaredIdentifiers.clear();
9394}
9395
9396void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
9397 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
9398 ExternalVTableUse VT;
9399 VTableUse &TableInfo = VTableUses[Idx++];
9400 VT.Record = dyn_cast_or_null<CXXRecordDecl>(Val: GetDecl(ID: TableInfo.ID));
9401 VT.Location = SourceLocation::getFromRawEncoding(Encoding: TableInfo.RawLoc);
9402 VT.DefinitionRequired = TableInfo.Used;
9403 VTables.push_back(Elt: VT);
9404 }
9405
9406 VTableUses.clear();
9407}
9408
9409void ASTReader::ReadPendingInstantiations(
9410 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
9411 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
9412 PendingInstantiation &Inst = PendingInstantiations[Idx++];
9413 ValueDecl *D = cast<ValueDecl>(Val: GetDecl(ID: Inst.ID));
9414 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: Inst.RawLoc);
9415
9416 Pending.push_back(Elt: std::make_pair(x&: D, y&: Loc));
9417 }
9418 PendingInstantiations.clear();
9419}
9420
9421void ASTReader::ReadLateParsedTemplates(
9422 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
9423 &LPTMap) {
9424 for (auto &LPT : LateParsedTemplates) {
9425 ModuleFile *FMod = LPT.first;
9426 RecordDataImpl &LateParsed = LPT.second;
9427 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
9428 /* In loop */) {
9429 FunctionDecl *FD = ReadDeclAs<FunctionDecl>(F&: *FMod, R: LateParsed, I&: Idx);
9430
9431 auto LT = std::make_unique<LateParsedTemplate>();
9432 LT->D = ReadDecl(F&: *FMod, R: LateParsed, I&: Idx);
9433 LT->FPO = FPOptions::getFromOpaqueInt(Value: LateParsed[Idx++]);
9434
9435 ModuleFile *F = getOwningModuleFile(D: LT->D);
9436 assert(F && "No module");
9437
9438 unsigned TokN = LateParsed[Idx++];
9439 LT->Toks.reserve(N: TokN);
9440 for (unsigned T = 0; T < TokN; ++T)
9441 LT->Toks.push_back(Elt: ReadToken(M&: *F, Record: LateParsed, Idx));
9442
9443 LPTMap.insert(KV: std::make_pair(x&: FD, y: std::move(LT)));
9444 }
9445 }
9446
9447 LateParsedTemplates.clear();
9448}
9449
9450void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {
9451 if (!Lambda->getLambdaContextDecl())
9452 return;
9453
9454 auto LambdaInfo =
9455 std::make_pair(x: Lambda->getLambdaContextDecl()->getCanonicalDecl(),
9456 y: Lambda->getLambdaIndexInContext());
9457
9458 // Handle the import and then include case for lambdas.
9459 if (auto Iter = LambdaDeclarationsForMerging.find(Val: LambdaInfo);
9460 Iter != LambdaDeclarationsForMerging.end() &&
9461 Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {
9462 CXXRecordDecl *Previous =
9463 cast<CXXRecordDecl>(Val: Iter->second)->getMostRecentDecl();
9464 Lambda->setPreviousDecl(Previous);
9465 // FIXME: It will be best to use the Previous type when we creating the
9466 // lambda directly. But that requires us to get the lambda context decl and
9467 // lambda index before creating the lambda, which needs a drastic change in
9468 // the parser.
9469 const_cast<QualType &>(Lambda->TypeForDecl->CanonicalType) =
9470 Previous->TypeForDecl->CanonicalType;
9471 return;
9472 }
9473
9474 // Keep track of this lambda so it can be merged with another lambda that
9475 // is loaded later.
9476 LambdaDeclarationsForMerging.insert(KV: {LambdaInfo, Lambda});
9477}
9478
9479void ASTReader::LoadSelector(Selector Sel) {
9480 // It would be complicated to avoid reading the methods anyway. So don't.
9481 ReadMethodPool(Sel);
9482}
9483
9484void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
9485 assert(ID && "Non-zero identifier ID required");
9486 unsigned Index = translateIdentifierIDToIndex(ID).second;
9487 assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
9488 IdentifiersLoaded[Index] = II;
9489 if (DeserializationListener)
9490 DeserializationListener->IdentifierRead(ID, II);
9491}
9492
9493/// Set the globally-visible declarations associated with the given
9494/// identifier.
9495///
9496/// If the AST reader is currently in a state where the given declaration IDs
9497/// cannot safely be resolved, they are queued until it is safe to resolve
9498/// them.
9499///
9500/// \param II an IdentifierInfo that refers to one or more globally-visible
9501/// declarations.
9502///
9503/// \param DeclIDs the set of declaration IDs with the name @p II that are
9504/// visible at global scope.
9505///
9506/// \param Decls if non-null, this vector will be populated with the set of
9507/// deserialized declarations. These declarations will not be pushed into
9508/// scope.
9509void ASTReader::SetGloballyVisibleDecls(
9510 IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,
9511 SmallVectorImpl<Decl *> *Decls) {
9512 if (NumCurrentElementsDeserializing && !Decls) {
9513 PendingIdentifierInfos[II].append(in_start: DeclIDs.begin(), in_end: DeclIDs.end());
9514 return;
9515 }
9516
9517 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
9518 if (!SemaObj) {
9519 // Queue this declaration so that it will be added to the
9520 // translation unit scope and identifier's declaration chain
9521 // once a Sema object is known.
9522 PreloadedDeclIDs.push_back(Elt: DeclIDs[I]);
9523 continue;
9524 }
9525
9526 NamedDecl *D = cast<NamedDecl>(Val: GetDecl(ID: DeclIDs[I]));
9527
9528 // If we're simply supposed to record the declarations, do so now.
9529 if (Decls) {
9530 Decls->push_back(Elt: D);
9531 continue;
9532 }
9533
9534 // Introduce this declaration into the translation-unit scope
9535 // and add it to the declaration chain for this identifier, so
9536 // that (unqualified) name lookup will find it.
9537 pushExternalDeclIntoScope(D, Name: II);
9538 }
9539}
9540
9541std::pair<ModuleFile *, unsigned>
9542ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
9543 if (ID == 0)
9544 return {nullptr, 0};
9545
9546 unsigned ModuleFileIndex = ID >> 32;
9547 unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(N: 32);
9548
9549 assert(ModuleFileIndex && "not translating loaded IdentifierID?");
9550 assert(getModuleManager().size() > ModuleFileIndex - 1);
9551
9552 ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
9553 assert(LocalID < MF.LocalNumIdentifiers);
9554 return {&MF, MF.BaseIdentifierID + LocalID};
9555}
9556
9557IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
9558 if (ID == 0)
9559 return nullptr;
9560
9561 if (IdentifiersLoaded.empty()) {
9562 Error(Msg: "no identifier table in AST file");
9563 return nullptr;
9564 }
9565
9566 auto [M, Index] = translateIdentifierIDToIndex(ID);
9567 if (!IdentifiersLoaded[Index]) {
9568 assert(M != nullptr && "Untranslated Identifier ID?");
9569 assert(Index >= M->BaseIdentifierID);
9570 unsigned LocalIndex = Index - M->BaseIdentifierID;
9571 const unsigned char *Data =
9572 M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
9573
9574 ASTIdentifierLookupTrait Trait(*this, *M);
9575 auto KeyDataLen = Trait.ReadKeyDataLength(d&: Data);
9576 auto Key = Trait.ReadKey(d: Data, n: KeyDataLen.first);
9577 auto &II = PP.getIdentifierTable().get(Name: Key);
9578 IdentifiersLoaded[Index] = &II;
9579 bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
9580 markIdentifierFromAST(Reader&: *this, II, IsModule);
9581 if (DeserializationListener)
9582 DeserializationListener->IdentifierRead(ID, II: &II);
9583 }
9584
9585 return IdentifiersLoaded[Index];
9586}
9587
9588IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
9589 return DecodeIdentifierInfo(ID: getGlobalIdentifierID(M, LocalID));
9590}
9591
9592IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
9593 if (LocalID < NUM_PREDEF_IDENT_IDS)
9594 return LocalID;
9595
9596 if (!M.ModuleOffsetMap.empty())
9597 ReadModuleOffsetMap(F&: M);
9598
9599 unsigned ModuleFileIndex = LocalID >> 32;
9600 LocalID &= llvm::maskTrailingOnes<IdentifierID>(N: 32);
9601 ModuleFile *MF =
9602 ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9603 assert(MF && "malformed identifier ID encoding?");
9604
9605 if (!ModuleFileIndex)
9606 LocalID -= NUM_PREDEF_IDENT_IDS;
9607
9608 return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
9609}
9610
9611MacroInfo *ASTReader::getMacro(MacroID ID) {
9612 if (ID == 0)
9613 return nullptr;
9614
9615 if (MacrosLoaded.empty()) {
9616 Error(Msg: "no macro table in AST file");
9617 return nullptr;
9618 }
9619
9620 ID -= NUM_PREDEF_MACRO_IDS;
9621 if (!MacrosLoaded[ID]) {
9622 GlobalMacroMapType::iterator I
9623 = GlobalMacroMap.find(K: ID + NUM_PREDEF_MACRO_IDS);
9624 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
9625 ModuleFile *M = I->second;
9626 unsigned Index = ID - M->BaseMacroID;
9627 MacrosLoaded[ID] =
9628 ReadMacroRecord(F&: *M, Offset: M->MacroOffsetsBase + M->MacroOffsets[Index]);
9629
9630 if (DeserializationListener)
9631 DeserializationListener->MacroRead(ID: ID + NUM_PREDEF_MACRO_IDS,
9632 MI: MacrosLoaded[ID]);
9633 }
9634
9635 return MacrosLoaded[ID];
9636}
9637
9638MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
9639 if (LocalID < NUM_PREDEF_MACRO_IDS)
9640 return LocalID;
9641
9642 if (!M.ModuleOffsetMap.empty())
9643 ReadModuleOffsetMap(F&: M);
9644
9645 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9646 = M.MacroRemap.find(K: LocalID - NUM_PREDEF_MACRO_IDS);
9647 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
9648
9649 return LocalID + I->second;
9650}
9651
9652serialization::SubmoduleID
9653ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {
9654 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
9655 return LocalID;
9656
9657 if (!M.ModuleOffsetMap.empty())
9658 ReadModuleOffsetMap(F&: M);
9659
9660 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9661 = M.SubmoduleRemap.find(K: LocalID - NUM_PREDEF_SUBMODULE_IDS);
9662 assert(I != M.SubmoduleRemap.end()
9663 && "Invalid index into submodule index remap");
9664
9665 return LocalID + I->second;
9666}
9667
9668Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
9669 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
9670 assert(GlobalID == 0 && "Unhandled global submodule ID");
9671 return nullptr;
9672 }
9673
9674 if (GlobalID > SubmodulesLoaded.size()) {
9675 Error(Msg: "submodule ID out of range in AST file");
9676 return nullptr;
9677 }
9678
9679 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
9680}
9681
9682Module *ASTReader::getModule(unsigned ID) {
9683 return getSubmodule(GlobalID: ID);
9684}
9685
9686ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {
9687 if (ID & 1) {
9688 // It's a module, look it up by submodule ID.
9689 auto I = GlobalSubmoduleMap.find(K: getGlobalSubmoduleID(M, LocalID: ID >> 1));
9690 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
9691 } else {
9692 // It's a prefix (preamble, PCH, ...). Look it up by index.
9693 int IndexFromEnd = static_cast<int>(ID >> 1);
9694 assert(IndexFromEnd && "got reference to unknown module file");
9695 return getModuleManager().pch_modules().end()[-IndexFromEnd];
9696 }
9697}
9698
9699unsigned ASTReader::getModuleFileID(ModuleFile *M) {
9700 if (!M)
9701 return 1;
9702
9703 // For a file representing a module, use the submodule ID of the top-level
9704 // module as the file ID. For any other kind of file, the number of such
9705 // files loaded beforehand will be the same on reload.
9706 // FIXME: Is this true even if we have an explicit module file and a PCH?
9707 if (M->isModule())
9708 return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
9709
9710 auto PCHModules = getModuleManager().pch_modules();
9711 auto I = llvm::find(Range&: PCHModules, Val: M);
9712 assert(I != PCHModules.end() && "emitting reference to unknown file");
9713 return std::distance(first: I, last: PCHModules.end()) << 1;
9714}
9715
9716std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {
9717 if (Module *M = getSubmodule(GlobalID: ID))
9718 return ASTSourceDescriptor(*M);
9719
9720 // If there is only a single PCH, return it instead.
9721 // Chained PCH are not supported.
9722 const auto &PCHChain = ModuleMgr.pch_modules();
9723 if (std::distance(first: std::begin(cont: PCHChain), last: std::end(cont: PCHChain))) {
9724 ModuleFile &MF = ModuleMgr.getPrimaryModule();
9725 StringRef ModuleName = llvm::sys::path::filename(path: MF.OriginalSourceFileName);
9726 StringRef FileName = llvm::sys::path::filename(path: MF.FileName);
9727 return ASTSourceDescriptor(ModuleName,
9728 llvm::sys::path::parent_path(path: MF.FileName),
9729 FileName, MF.Signature);
9730 }
9731 return std::nullopt;
9732}
9733
9734ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
9735 auto I = DefinitionSource.find(Val: FD);
9736 if (I == DefinitionSource.end())
9737 return EK_ReplyHazy;
9738 return I->second ? EK_Never : EK_Always;
9739}
9740
9741bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
9742 return ThisDeclarationWasADefinitionSet.contains(V: FD);
9743}
9744
9745Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
9746 return DecodeSelector(Idx: getGlobalSelectorID(M, LocalID));
9747}
9748
9749Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
9750 if (ID == 0)
9751 return Selector();
9752
9753 if (ID > SelectorsLoaded.size()) {
9754 Error(Msg: "selector ID out of range in AST file");
9755 return Selector();
9756 }
9757
9758 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
9759 // Load this selector from the selector table.
9760 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(K: ID);
9761 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
9762 ModuleFile &M = *I->second;
9763 ASTSelectorLookupTrait Trait(*this, M);
9764 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
9765 SelectorsLoaded[ID - 1] =
9766 Trait.ReadKey(d: M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
9767 if (DeserializationListener)
9768 DeserializationListener->SelectorRead(iD: ID, Sel: SelectorsLoaded[ID - 1]);
9769 }
9770
9771 return SelectorsLoaded[ID - 1];
9772}
9773
9774Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
9775 return DecodeSelector(ID);
9776}
9777
9778uint32_t ASTReader::GetNumExternalSelectors() {
9779 // ID 0 (the null selector) is considered an external selector.
9780 return getTotalNumSelectors() + 1;
9781}
9782
9783serialization::SelectorID
9784ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
9785 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
9786 return LocalID;
9787
9788 if (!M.ModuleOffsetMap.empty())
9789 ReadModuleOffsetMap(F&: M);
9790
9791 ContinuousRangeMap<uint32_t, int, 2>::iterator I
9792 = M.SelectorRemap.find(K: LocalID - NUM_PREDEF_SELECTOR_IDS);
9793 assert(I != M.SelectorRemap.end()
9794 && "Invalid index into selector index remap");
9795
9796 return LocalID + I->second;
9797}
9798
9799DeclarationNameLoc
9800ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
9801 switch (Name.getNameKind()) {
9802 case DeclarationName::CXXConstructorName:
9803 case DeclarationName::CXXDestructorName:
9804 case DeclarationName::CXXConversionFunctionName:
9805 return DeclarationNameLoc::makeNamedTypeLoc(TInfo: readTypeSourceInfo());
9806
9807 case DeclarationName::CXXOperatorName:
9808 return DeclarationNameLoc::makeCXXOperatorNameLoc(Range: readSourceRange());
9809
9810 case DeclarationName::CXXLiteralOperatorName:
9811 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
9812 Loc: readSourceLocation());
9813
9814 case DeclarationName::Identifier:
9815 case DeclarationName::ObjCZeroArgSelector:
9816 case DeclarationName::ObjCOneArgSelector:
9817 case DeclarationName::ObjCMultiArgSelector:
9818 case DeclarationName::CXXUsingDirective:
9819 case DeclarationName::CXXDeductionGuideName:
9820 break;
9821 }
9822 return DeclarationNameLoc();
9823}
9824
9825DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
9826 DeclarationNameInfo NameInfo;
9827 NameInfo.setName(readDeclarationName());
9828 NameInfo.setLoc(readSourceLocation());
9829 NameInfo.setInfo(readDeclarationNameLoc(Name: NameInfo.getName()));
9830 return NameInfo;
9831}
9832
9833TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {
9834 return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());
9835}
9836
9837SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {
9838 auto Kind = readInt();
9839 auto ResultType = readQualType();
9840 auto Value = readAPInt();
9841 SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);
9842 assert(Op.isValid());
9843 return Op;
9844}
9845
9846void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
9847 Info.QualifierLoc = readNestedNameSpecifierLoc();
9848 unsigned NumTPLists = readInt();
9849 Info.NumTemplParamLists = NumTPLists;
9850 if (NumTPLists) {
9851 Info.TemplParamLists =
9852 new (getContext()) TemplateParameterList *[NumTPLists];
9853 for (unsigned i = 0; i != NumTPLists; ++i)
9854 Info.TemplParamLists[i] = readTemplateParameterList();
9855 }
9856}
9857
9858TemplateParameterList *
9859ASTRecordReader::readTemplateParameterList() {
9860 SourceLocation TemplateLoc = readSourceLocation();
9861 SourceLocation LAngleLoc = readSourceLocation();
9862 SourceLocation RAngleLoc = readSourceLocation();
9863
9864 unsigned NumParams = readInt();
9865 SmallVector<NamedDecl *, 16> Params;
9866 Params.reserve(N: NumParams);
9867 while (NumParams--)
9868 Params.push_back(Elt: readDeclAs<NamedDecl>());
9869
9870 bool HasRequiresClause = readBool();
9871 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
9872
9873 TemplateParameterList *TemplateParams = TemplateParameterList::Create(
9874 C: getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
9875 return TemplateParams;
9876}
9877
9878void ASTRecordReader::readTemplateArgumentList(
9879 SmallVectorImpl<TemplateArgument> &TemplArgs,
9880 bool Canonicalize) {
9881 unsigned NumTemplateArgs = readInt();
9882 TemplArgs.reserve(N: NumTemplateArgs);
9883 while (NumTemplateArgs--)
9884 TemplArgs.push_back(Elt: readTemplateArgument(Canonicalize));
9885}
9886
9887/// Read a UnresolvedSet structure.
9888void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
9889 unsigned NumDecls = readInt();
9890 Set.reserve(C&: getContext(), N: NumDecls);
9891 while (NumDecls--) {
9892 GlobalDeclID ID = readDeclID();
9893 AccessSpecifier AS = (AccessSpecifier) readInt();
9894 Set.addLazyDecl(C&: getContext(), ID, AS);
9895 }
9896}
9897
9898CXXBaseSpecifier
9899ASTRecordReader::readCXXBaseSpecifier() {
9900 bool isVirtual = readBool();
9901 bool isBaseOfClass = readBool();
9902 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
9903 bool inheritConstructors = readBool();
9904 TypeSourceInfo *TInfo = readTypeSourceInfo();
9905 SourceRange Range = readSourceRange();
9906 SourceLocation EllipsisLoc = readSourceLocation();
9907 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
9908 EllipsisLoc);
9909 Result.setInheritConstructors(inheritConstructors);
9910 return Result;
9911}
9912
9913CXXCtorInitializer **
9914ASTRecordReader::readCXXCtorInitializers() {
9915 ASTContext &Context = getContext();
9916 unsigned NumInitializers = readInt();
9917 assert(NumInitializers && "wrote ctor initializers but have no inits");
9918 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
9919 for (unsigned i = 0; i != NumInitializers; ++i) {
9920 TypeSourceInfo *TInfo = nullptr;
9921 bool IsBaseVirtual = false;
9922 FieldDecl *Member = nullptr;
9923 IndirectFieldDecl *IndirectMember = nullptr;
9924
9925 CtorInitializerType Type = (CtorInitializerType) readInt();
9926 switch (Type) {
9927 case CTOR_INITIALIZER_BASE:
9928 TInfo = readTypeSourceInfo();
9929 IsBaseVirtual = readBool();
9930 break;
9931
9932 case CTOR_INITIALIZER_DELEGATING:
9933 TInfo = readTypeSourceInfo();
9934 break;
9935
9936 case CTOR_INITIALIZER_MEMBER:
9937 Member = readDeclAs<FieldDecl>();
9938 break;
9939
9940 case CTOR_INITIALIZER_INDIRECT_MEMBER:
9941 IndirectMember = readDeclAs<IndirectFieldDecl>();
9942 break;
9943 }
9944
9945 SourceLocation MemberOrEllipsisLoc = readSourceLocation();
9946 Expr *Init = readExpr();
9947 SourceLocation LParenLoc = readSourceLocation();
9948 SourceLocation RParenLoc = readSourceLocation();
9949
9950 CXXCtorInitializer *BOMInit;
9951 if (Type == CTOR_INITIALIZER_BASE)
9952 BOMInit = new (Context)
9953 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
9954 RParenLoc, MemberOrEllipsisLoc);
9955 else if (Type == CTOR_INITIALIZER_DELEGATING)
9956 BOMInit = new (Context)
9957 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
9958 else if (Member)
9959 BOMInit = new (Context)
9960 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
9961 Init, RParenLoc);
9962 else
9963 BOMInit = new (Context)
9964 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
9965 LParenLoc, Init, RParenLoc);
9966
9967 if (/*IsWritten*/readBool()) {
9968 unsigned SourceOrder = readInt();
9969 BOMInit->setSourceOrder(SourceOrder);
9970 }
9971
9972 CtorInitializers[i] = BOMInit;
9973 }
9974
9975 return CtorInitializers;
9976}
9977
9978NestedNameSpecifierLoc
9979ASTRecordReader::readNestedNameSpecifierLoc() {
9980 ASTContext &Context = getContext();
9981 unsigned N = readInt();
9982 NestedNameSpecifierLocBuilder Builder;
9983 for (unsigned I = 0; I != N; ++I) {
9984 auto Kind = readNestedNameSpecifierKind();
9985 switch (Kind) {
9986 case NestedNameSpecifier::Identifier: {
9987 IdentifierInfo *II = readIdentifier();
9988 SourceRange Range = readSourceRange();
9989 Builder.Extend(Context, Identifier: II, IdentifierLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9990 break;
9991 }
9992
9993 case NestedNameSpecifier::Namespace: {
9994 NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
9995 SourceRange Range = readSourceRange();
9996 Builder.Extend(Context, Namespace: NS, NamespaceLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
9997 break;
9998 }
9999
10000 case NestedNameSpecifier::NamespaceAlias: {
10001 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
10002 SourceRange Range = readSourceRange();
10003 Builder.Extend(Context, Alias, AliasLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10004 break;
10005 }
10006
10007 case NestedNameSpecifier::TypeSpec: {
10008 TypeSourceInfo *T = readTypeSourceInfo();
10009 if (!T)
10010 return NestedNameSpecifierLoc();
10011 SourceLocation ColonColonLoc = readSourceLocation();
10012 Builder.Extend(Context, TL: T->getTypeLoc(), ColonColonLoc);
10013 break;
10014 }
10015
10016 case NestedNameSpecifier::Global: {
10017 SourceLocation ColonColonLoc = readSourceLocation();
10018 Builder.MakeGlobal(Context, ColonColonLoc);
10019 break;
10020 }
10021
10022 case NestedNameSpecifier::Super: {
10023 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
10024 SourceRange Range = readSourceRange();
10025 Builder.MakeSuper(Context, RD, SuperLoc: Range.getBegin(), ColonColonLoc: Range.getEnd());
10026 break;
10027 }
10028 }
10029 }
10030
10031 return Builder.getWithLocInContext(Context);
10032}
10033
10034SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
10035 unsigned &Idx) {
10036 SourceLocation beg = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10037 SourceLocation end = ReadSourceLocation(ModuleFile&: F, Record, Idx);
10038 return SourceRange(beg, end);
10039}
10040
10041llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,
10042 const StringRef Blob) {
10043 unsigned Count = Record[0];
10044 const char *Byte = Blob.data();
10045 llvm::BitVector Ret = llvm::BitVector(Count, false);
10046 for (unsigned I = 0; I < Count; ++Byte)
10047 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
10048 if (*Byte & (1 << Bit))
10049 Ret[I] = true;
10050 return Ret;
10051}
10052
10053/// Read a floating-point value
10054llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
10055 return llvm::APFloat(Sem, readAPInt());
10056}
10057
10058// Read a string
10059std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
10060 unsigned Len = Record[Idx++];
10061 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
10062 Idx += Len;
10063 return Result;
10064}
10065
10066StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,
10067 StringRef &Blob) {
10068 unsigned Len = Record[Idx++];
10069 StringRef Result = Blob.substr(Start: 0, N: Len);
10070 Blob = Blob.substr(Start: Len);
10071 return Result;
10072}
10073
10074std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
10075 unsigned &Idx) {
10076 return ReadPath(BaseDirectory: F.BaseDirectory, Record, Idx);
10077}
10078
10079std::string ASTReader::ReadPath(StringRef BaseDirectory,
10080 const RecordData &Record, unsigned &Idx) {
10081 std::string Filename = ReadString(Record, Idx);
10082 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10083}
10084
10085std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,
10086 const RecordData &Record, unsigned &Idx,
10087 StringRef &Blob) {
10088 StringRef Filename = ReadStringBlob(Record, Idx, Blob);
10089 return ResolveImportedPathAndAllocate(Buf&: PathBuf, P: Filename, Prefix: BaseDirectory);
10090}
10091
10092VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
10093 unsigned &Idx) {
10094 unsigned Major = Record[Idx++];
10095 unsigned Minor = Record[Idx++];
10096 unsigned Subminor = Record[Idx++];
10097 if (Minor == 0)
10098 return VersionTuple(Major);
10099 if (Subminor == 0)
10100 return VersionTuple(Major, Minor - 1);
10101 return VersionTuple(Major, Minor - 1, Subminor - 1);
10102}
10103
10104CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
10105 const RecordData &Record,
10106 unsigned &Idx) {
10107 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, R: Record, I&: Idx);
10108 return CXXTemporary::Create(C: getContext(), Destructor: Decl);
10109}
10110
10111DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
10112 return Diag(Loc: CurrentImportLoc, DiagID);
10113}
10114
10115DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
10116 return Diags.Report(Loc, DiagID);
10117}
10118
10119void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,
10120 llvm::function_ref<void()> Fn) {
10121 // When Sema is available, avoid duplicate errors.
10122 if (SemaObj) {
10123 SemaObj->runWithSufficientStackSpace(Loc, Fn);
10124 return;
10125 }
10126
10127 StackHandler.runWithSufficientStackSpace(Loc, Fn);
10128}
10129
10130/// Retrieve the identifier table associated with the
10131/// preprocessor.
10132IdentifierTable &ASTReader::getIdentifierTable() {
10133 return PP.getIdentifierTable();
10134}
10135
10136/// Record that the given ID maps to the given switch-case
10137/// statement.
10138void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
10139 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
10140 "Already have a SwitchCase with this ID");
10141 (*CurrSwitchCaseStmts)[ID] = SC;
10142}
10143
10144/// Retrieve the switch-case statement with the given ID.
10145SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
10146 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
10147 return (*CurrSwitchCaseStmts)[ID];
10148}
10149
10150void ASTReader::ClearSwitchCaseIDs() {
10151 CurrSwitchCaseStmts->clear();
10152}
10153
10154void ASTReader::ReadComments() {
10155 ASTContext &Context = getContext();
10156 std::vector<RawComment *> Comments;
10157 for (SmallVectorImpl<std::pair<BitstreamCursor,
10158 serialization::ModuleFile *>>::iterator
10159 I = CommentsCursors.begin(),
10160 E = CommentsCursors.end();
10161 I != E; ++I) {
10162 Comments.clear();
10163 BitstreamCursor &Cursor = I->first;
10164 serialization::ModuleFile &F = *I->second;
10165 SavedStreamPosition SavedPosition(Cursor);
10166
10167 RecordData Record;
10168 while (true) {
10169 Expected<llvm::BitstreamEntry> MaybeEntry =
10170 Cursor.advanceSkippingSubblocks(
10171 Flags: BitstreamCursor::AF_DontPopBlockAtEnd);
10172 if (!MaybeEntry) {
10173 Error(Err: MaybeEntry.takeError());
10174 return;
10175 }
10176 llvm::BitstreamEntry Entry = MaybeEntry.get();
10177
10178 switch (Entry.Kind) {
10179 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
10180 case llvm::BitstreamEntry::Error:
10181 Error(Msg: "malformed block record in AST file");
10182 return;
10183 case llvm::BitstreamEntry::EndBlock:
10184 goto NextCursor;
10185 case llvm::BitstreamEntry::Record:
10186 // The interesting case.
10187 break;
10188 }
10189
10190 // Read a record.
10191 Record.clear();
10192 Expected<unsigned> MaybeComment = Cursor.readRecord(AbbrevID: Entry.ID, Vals&: Record);
10193 if (!MaybeComment) {
10194 Error(Err: MaybeComment.takeError());
10195 return;
10196 }
10197 switch ((CommentRecordTypes)MaybeComment.get()) {
10198 case COMMENTS_RAW_COMMENT: {
10199 unsigned Idx = 0;
10200 SourceRange SR = ReadSourceRange(F, Record, Idx);
10201 RawComment::CommentKind Kind =
10202 (RawComment::CommentKind) Record[Idx++];
10203 bool IsTrailingComment = Record[Idx++];
10204 bool IsAlmostTrailingComment = Record[Idx++];
10205 Comments.push_back(x: new (Context) RawComment(
10206 SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
10207 break;
10208 }
10209 }
10210 }
10211 NextCursor:
10212 for (RawComment *C : Comments) {
10213 SourceLocation CommentLoc = C->getBeginLoc();
10214 if (CommentLoc.isValid()) {
10215 FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(Loc: CommentLoc);
10216 if (Loc.first.isValid())
10217 Context.Comments.OrderedComments[Loc.first].emplace(args&: Loc.second, args&: C);
10218 }
10219 }
10220 }
10221}
10222
10223void ASTReader::visitInputFileInfos(
10224 serialization::ModuleFile &MF, bool IncludeSystem,
10225 llvm::function_ref<void(const serialization::InputFileInfo &IFI,
10226 bool IsSystem)>
10227 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 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I+1);
10235 Visitor(IFI, IsSystem);
10236 }
10237}
10238
10239void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
10240 bool IncludeSystem, bool Complain,
10241 llvm::function_ref<void(const serialization::InputFile &IF,
10242 bool isSystem)> Visitor) {
10243 unsigned NumUserInputs = MF.NumUserInputFiles;
10244 unsigned NumInputs = MF.InputFilesLoaded.size();
10245 assert(NumUserInputs <= NumInputs);
10246 unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
10247 for (unsigned I = 0; I < N; ++I) {
10248 bool IsSystem = I >= NumUserInputs;
10249 InputFile IF = getInputFile(F&: MF, ID: I+1, Complain);
10250 Visitor(IF, IsSystem);
10251 }
10252}
10253
10254void ASTReader::visitTopLevelModuleMaps(
10255 serialization::ModuleFile &MF,
10256 llvm::function_ref<void(FileEntryRef FE)> Visitor) {
10257 unsigned NumInputs = MF.InputFilesLoaded.size();
10258 for (unsigned I = 0; I < NumInputs; ++I) {
10259 InputFileInfo IFI = getInputFileInfo(F&: MF, ID: I + 1);
10260 if (IFI.TopLevel && IFI.ModuleMap)
10261 if (auto FE = getInputFile(F&: MF, ID: I + 1).getFile())
10262 Visitor(*FE);
10263 }
10264}
10265
10266void ASTReader::finishPendingActions() {
10267 while (!PendingIdentifierInfos.empty() ||
10268 !PendingDeducedFunctionTypes.empty() ||
10269 !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10270 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10271 !PendingUpdateRecords.empty() ||
10272 !PendingObjCExtensionIvarRedeclarations.empty()) {
10273 // If any identifiers with corresponding top-level declarations have
10274 // been loaded, load those declarations now.
10275 using TopLevelDeclsMap =
10276 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
10277 TopLevelDeclsMap TopLevelDecls;
10278
10279 while (!PendingIdentifierInfos.empty()) {
10280 IdentifierInfo *II = PendingIdentifierInfos.back().first;
10281 SmallVector<GlobalDeclID, 4> DeclIDs =
10282 std::move(PendingIdentifierInfos.back().second);
10283 PendingIdentifierInfos.pop_back();
10284
10285 SetGloballyVisibleDecls(II, DeclIDs, Decls: &TopLevelDecls[II]);
10286 }
10287
10288 // Load each function type that we deferred loading because it was a
10289 // deduced type that might refer to a local type declared within itself.
10290 for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {
10291 auto *FD = PendingDeducedFunctionTypes[I].first;
10292 FD->setType(GetType(ID: PendingDeducedFunctionTypes[I].second));
10293
10294 if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {
10295 // If we gave a function a deduced return type, remember that we need to
10296 // propagate that along the redeclaration chain.
10297 if (DT->isDeduced()) {
10298 PendingDeducedTypeUpdates.insert(
10299 KV: {FD->getCanonicalDecl(), FD->getReturnType()});
10300 continue;
10301 }
10302
10303 // The function has undeduced DeduceType return type. We hope we can
10304 // find the deduced type by iterating the redecls in other modules
10305 // later.
10306 PendingUndeducedFunctionDecls.push_back(Elt: FD);
10307 continue;
10308 }
10309 }
10310 PendingDeducedFunctionTypes.clear();
10311
10312 // Load each variable type that we deferred loading because it was a
10313 // deduced type that might refer to a local type declared within itself.
10314 for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {
10315 auto *VD = PendingDeducedVarTypes[I].first;
10316 VD->setType(GetType(ID: PendingDeducedVarTypes[I].second));
10317 }
10318 PendingDeducedVarTypes.clear();
10319
10320 // Load pending declaration chains.
10321 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
10322 loadPendingDeclChain(D: PendingDeclChains[I].first,
10323 LocalOffset: PendingDeclChains[I].second);
10324 PendingDeclChains.clear();
10325
10326 // Make the most recent of the top-level declarations visible.
10327 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
10328 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
10329 IdentifierInfo *II = TLD->first;
10330 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
10331 pushExternalDeclIntoScope(D: cast<NamedDecl>(Val: TLD->second[I]), Name: II);
10332 }
10333 }
10334
10335 // Load any pending macro definitions.
10336 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
10337 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
10338 SmallVector<PendingMacroInfo, 2> GlobalIDs;
10339 GlobalIDs.swap(RHS&: PendingMacroIDs.begin()[I].second);
10340 // Initialize the macro history from chained-PCHs ahead of module imports.
10341 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10342 ++IDIdx) {
10343 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10344 if (!Info.M->isModule())
10345 resolvePendingMacro(II, PMInfo: Info);
10346 }
10347 // Handle module imports.
10348 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
10349 ++IDIdx) {
10350 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
10351 if (Info.M->isModule())
10352 resolvePendingMacro(II, PMInfo: Info);
10353 }
10354 }
10355 PendingMacroIDs.clear();
10356
10357 // Wire up the DeclContexts for Decls that we delayed setting until
10358 // recursive loading is completed.
10359 while (!PendingDeclContextInfos.empty()) {
10360 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
10361 PendingDeclContextInfos.pop_front();
10362 DeclContext *SemaDC = cast<DeclContext>(Val: GetDecl(ID: Info.SemaDC));
10363 DeclContext *LexicalDC = cast<DeclContext>(Val: GetDecl(ID: Info.LexicalDC));
10364 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, Ctx&: getContext());
10365 }
10366
10367 // Perform any pending declaration updates.
10368 while (!PendingUpdateRecords.empty()) {
10369 auto Update = PendingUpdateRecords.pop_back_val();
10370 ReadingKindTracker ReadingKind(Read_Decl, *this);
10371 loadDeclUpdateRecords(Record&: Update);
10372 }
10373
10374 while (!PendingObjCExtensionIvarRedeclarations.empty()) {
10375 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;
10376 auto DuplicateIvars =
10377 PendingObjCExtensionIvarRedeclarations.back().second;
10378 StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
10379 StructuralEquivalenceContext Ctx(
10380 ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),
10381 ExtensionsPair.second->getASTContext(), NonEquivalentDecls,
10382 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,
10383 /*Complain =*/false,
10384 /*ErrorOnTagTypeMismatch =*/true);
10385 if (Ctx.IsEquivalent(D1: ExtensionsPair.first, D2: ExtensionsPair.second)) {
10386 // Merge redeclared ivars with their predecessors.
10387 for (auto IvarPair : DuplicateIvars) {
10388 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;
10389 // Change semantic DeclContext but keep the lexical one.
10390 Ivar->setDeclContextsImpl(SemaDC: PrevIvar->getDeclContext(),
10391 LexicalDC: Ivar->getLexicalDeclContext(),
10392 Ctx&: getContext());
10393 getContext().setPrimaryMergedDecl(D: Ivar, Primary: PrevIvar->getCanonicalDecl());
10394 }
10395 // Invalidate duplicate extension and the cached ivar list.
10396 ExtensionsPair.first->setInvalidDecl();
10397 ExtensionsPair.second->getClassInterface()
10398 ->getDefinition()
10399 ->setIvarList(nullptr);
10400 } else {
10401 for (auto IvarPair : DuplicateIvars) {
10402 Diag(Loc: IvarPair.first->getLocation(),
10403 DiagID: diag::err_duplicate_ivar_declaration)
10404 << IvarPair.first->getIdentifier();
10405 Diag(Loc: IvarPair.second->getLocation(), DiagID: diag::note_previous_definition);
10406 }
10407 }
10408 PendingObjCExtensionIvarRedeclarations.pop_back();
10409 }
10410 }
10411
10412 // At this point, all update records for loaded decls are in place, so any
10413 // fake class definitions should have become real.
10414 assert(PendingFakeDefinitionData.empty() &&
10415 "faked up a class definition but never saw the real one");
10416
10417 // If we deserialized any C++ or Objective-C class definitions, any
10418 // Objective-C protocol definitions, or any redeclarable templates, make sure
10419 // that all redeclarations point to the definitions. Note that this can only
10420 // happen now, after the redeclaration chains have been fully wired.
10421 for (Decl *D : PendingDefinitions) {
10422 if (TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
10423 if (const TagType *TagT = dyn_cast<TagType>(Val: TD->getTypeForDecl())) {
10424 // Make sure that the TagType points at the definition.
10425 const_cast<TagType*>(TagT)->decl = TD;
10426 }
10427
10428 if (auto RD = dyn_cast<CXXRecordDecl>(Val: D)) {
10429 for (auto *R = getMostRecentExistingDecl(D: RD); R;
10430 R = R->getPreviousDecl()) {
10431 assert((R == D) ==
10432 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
10433 "declaration thinks it's the definition but it isn't");
10434 cast<CXXRecordDecl>(Val: R)->DefinitionData = RD->DefinitionData;
10435 }
10436 }
10437
10438 continue;
10439 }
10440
10441 if (auto ID = dyn_cast<ObjCInterfaceDecl>(Val: D)) {
10442 // Make sure that the ObjCInterfaceType points at the definition.
10443 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(Val: ID->TypeForDecl))
10444 ->Decl = ID;
10445
10446 for (auto *R = getMostRecentExistingDecl(D: ID); R; R = R->getPreviousDecl())
10447 cast<ObjCInterfaceDecl>(Val: R)->Data = ID->Data;
10448
10449 continue;
10450 }
10451
10452 if (auto PD = dyn_cast<ObjCProtocolDecl>(Val: D)) {
10453 for (auto *R = getMostRecentExistingDecl(D: PD); R; R = R->getPreviousDecl())
10454 cast<ObjCProtocolDecl>(Val: R)->Data = PD->Data;
10455
10456 continue;
10457 }
10458
10459 auto RTD = cast<RedeclarableTemplateDecl>(Val: D)->getCanonicalDecl();
10460 for (auto *R = getMostRecentExistingDecl(D: RTD); R; R = R->getPreviousDecl())
10461 cast<RedeclarableTemplateDecl>(Val: R)->Common = RTD->Common;
10462 }
10463 PendingDefinitions.clear();
10464
10465 for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {
10466 auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {
10467 if (auto *VD = dyn_cast<VarDecl>(Val: D))
10468 return VD->isThisDeclarationADefinition() ||
10469 VD->isThisDeclarationADemotedDefinition();
10470
10471 if (auto *TD = dyn_cast<TagDecl>(Val: D))
10472 return TD->isThisDeclarationADefinition() ||
10473 TD->isThisDeclarationADemotedDefinition();
10474
10475 if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
10476 return FD->isThisDeclarationADefinition() || PendingBodies.count(Key: FD);
10477
10478 if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(Val: D))
10479 return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);
10480
10481 // Conservatively return false here.
10482 return false;
10483 };
10484
10485 auto hasDefinition = [&hasDefinitionImpl](Decl *D) {
10486 return hasDefinitionImpl(D, hasDefinitionImpl);
10487 };
10488
10489 // It is not good to prevent multiple declarations since the forward
10490 // declaration is common. Let's try to avoid duplicated definitions
10491 // only.
10492 if (!hasDefinition(D) || !hasDefinition(Previous))
10493 continue;
10494
10495 Module *PM = Previous->getOwningModule();
10496 Module *DM = D->getOwningModule();
10497 Diag(Loc: D->getLocation(), DiagID: diag::warn_decls_in_multiple_modules)
10498 << cast<NamedDecl>(Val: Previous) << PM->getTopLevelModuleName()
10499 << (DM ? DM->getTopLevelModuleName() : "global module");
10500 Diag(Loc: Previous->getLocation(), DiagID: diag::note_also_found);
10501 }
10502 PendingWarningForDuplicatedDefsInModuleUnits.clear();
10503
10504 // Load the bodies of any functions or methods we've encountered. We do
10505 // this now (delayed) so that we can be sure that the declaration chains
10506 // have been fully wired up (hasBody relies on this).
10507 // FIXME: We shouldn't require complete redeclaration chains here.
10508 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
10509 PBEnd = PendingBodies.end();
10510 PB != PBEnd; ++PB) {
10511 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: PB->first)) {
10512 // FIXME: Check for =delete/=default?
10513 const FunctionDecl *Defn = nullptr;
10514 if (!getContext().getLangOpts().Modules || !FD->hasBody(Definition&: Defn)) {
10515 FD->setLazyBody(PB->second);
10516 } else {
10517 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
10518 mergeDefinitionVisibility(Def: NonConstDefn, MergedDef: FD);
10519
10520 if (!FD->isLateTemplateParsed() &&
10521 !NonConstDefn->isLateTemplateParsed() &&
10522 // We only perform ODR checks for decls not in the explicit
10523 // global module fragment.
10524 !shouldSkipCheckingODR(D: FD) &&
10525 !shouldSkipCheckingODR(D: NonConstDefn) &&
10526 FD->getODRHash() != NonConstDefn->getODRHash()) {
10527 if (!isa<CXXMethodDecl>(Val: FD)) {
10528 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10529 } else if (FD->getLexicalParent()->isFileContext() &&
10530 NonConstDefn->getLexicalParent()->isFileContext()) {
10531 // Only diagnose out-of-line method definitions. If they are
10532 // in class definitions, then an error will be generated when
10533 // processing the class bodies.
10534 PendingFunctionOdrMergeFailures[FD].push_back(Elt: NonConstDefn);
10535 }
10536 }
10537 }
10538 continue;
10539 }
10540
10541 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: PB->first);
10542 if (!getContext().getLangOpts().Modules || !MD->hasBody())
10543 MD->setLazyBody(PB->second);
10544 }
10545 PendingBodies.clear();
10546
10547 // Inform any classes that had members added that they now have more members.
10548 for (auto [RD, MD] : PendingAddedClassMembers) {
10549 RD->addedMember(D: MD);
10550 }
10551 PendingAddedClassMembers.clear();
10552
10553 // Do some cleanup.
10554 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
10555 getContext().deduplicateMergedDefinitionsFor(ND);
10556 PendingMergedDefinitionsToDeduplicate.clear();
10557
10558 // For each decl chain that we wanted to complete while deserializing, mark
10559 // it as "still needs to be completed".
10560 for (Decl *D : PendingIncompleteDeclChains)
10561 markIncompleteDeclChain(D);
10562 PendingIncompleteDeclChains.clear();
10563
10564 assert(PendingIdentifierInfos.empty() &&
10565 "Should be empty at the end of finishPendingActions");
10566 assert(PendingDeducedFunctionTypes.empty() &&
10567 "Should be empty at the end of finishPendingActions");
10568 assert(PendingDeducedVarTypes.empty() &&
10569 "Should be empty at the end of finishPendingActions");
10570 assert(PendingDeclChains.empty() &&
10571 "Should be empty at the end of finishPendingActions");
10572 assert(PendingMacroIDs.empty() &&
10573 "Should be empty at the end of finishPendingActions");
10574 assert(PendingDeclContextInfos.empty() &&
10575 "Should be empty at the end of finishPendingActions");
10576 assert(PendingUpdateRecords.empty() &&
10577 "Should be empty at the end of finishPendingActions");
10578 assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10579 "Should be empty at the end of finishPendingActions");
10580 assert(PendingFakeDefinitionData.empty() &&
10581 "Should be empty at the end of finishPendingActions");
10582 assert(PendingDefinitions.empty() &&
10583 "Should be empty at the end of finishPendingActions");
10584 assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10585 "Should be empty at the end of finishPendingActions");
10586 assert(PendingBodies.empty() &&
10587 "Should be empty at the end of finishPendingActions");
10588 assert(PendingAddedClassMembers.empty() &&
10589 "Should be empty at the end of finishPendingActions");
10590 assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10591 "Should be empty at the end of finishPendingActions");
10592 assert(PendingIncompleteDeclChains.empty() &&
10593 "Should be empty at the end of finishPendingActions");
10594}
10595
10596void ASTReader::diagnoseOdrViolations() {
10597 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
10598 PendingRecordOdrMergeFailures.empty() &&
10599 PendingFunctionOdrMergeFailures.empty() &&
10600 PendingEnumOdrMergeFailures.empty() &&
10601 PendingObjCInterfaceOdrMergeFailures.empty() &&
10602 PendingObjCProtocolOdrMergeFailures.empty())
10603 return;
10604
10605 // Trigger the import of the full definition of each class that had any
10606 // odr-merging problems, so we can produce better diagnostics for them.
10607 // These updates may in turn find and diagnose some ODR failures, so take
10608 // ownership of the set first.
10609 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
10610 PendingOdrMergeFailures.clear();
10611 for (auto &Merge : OdrMergeFailures) {
10612 Merge.first->buildLookup();
10613 Merge.first->decls_begin();
10614 Merge.first->bases_begin();
10615 Merge.first->vbases_begin();
10616 for (auto &RecordPair : Merge.second) {
10617 auto *RD = RecordPair.first;
10618 RD->decls_begin();
10619 RD->bases_begin();
10620 RD->vbases_begin();
10621 }
10622 }
10623
10624 // Trigger the import of the full definition of each record in C/ObjC.
10625 auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);
10626 PendingRecordOdrMergeFailures.clear();
10627 for (auto &Merge : RecordOdrMergeFailures) {
10628 Merge.first->decls_begin();
10629 for (auto &D : Merge.second)
10630 D->decls_begin();
10631 }
10632
10633 // Trigger the import of the full interface definition.
10634 auto ObjCInterfaceOdrMergeFailures =
10635 std::move(PendingObjCInterfaceOdrMergeFailures);
10636 PendingObjCInterfaceOdrMergeFailures.clear();
10637 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10638 Merge.first->decls_begin();
10639 for (auto &InterfacePair : Merge.second)
10640 InterfacePair.first->decls_begin();
10641 }
10642
10643 // Trigger the import of functions.
10644 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
10645 PendingFunctionOdrMergeFailures.clear();
10646 for (auto &Merge : FunctionOdrMergeFailures) {
10647 Merge.first->buildLookup();
10648 Merge.first->decls_begin();
10649 Merge.first->getBody();
10650 for (auto &FD : Merge.second) {
10651 FD->buildLookup();
10652 FD->decls_begin();
10653 FD->getBody();
10654 }
10655 }
10656
10657 // Trigger the import of enums.
10658 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
10659 PendingEnumOdrMergeFailures.clear();
10660 for (auto &Merge : EnumOdrMergeFailures) {
10661 Merge.first->decls_begin();
10662 for (auto &Enum : Merge.second) {
10663 Enum->decls_begin();
10664 }
10665 }
10666
10667 // Trigger the import of the full protocol definition.
10668 auto ObjCProtocolOdrMergeFailures =
10669 std::move(PendingObjCProtocolOdrMergeFailures);
10670 PendingObjCProtocolOdrMergeFailures.clear();
10671 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10672 Merge.first->decls_begin();
10673 for (auto &ProtocolPair : Merge.second)
10674 ProtocolPair.first->decls_begin();
10675 }
10676
10677 // For each declaration from a merged context, check that the canonical
10678 // definition of that context also contains a declaration of the same
10679 // entity.
10680 //
10681 // Caution: this loop does things that might invalidate iterators into
10682 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
10683 while (!PendingOdrMergeChecks.empty()) {
10684 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
10685
10686 // FIXME: Skip over implicit declarations for now. This matters for things
10687 // like implicitly-declared special member functions. This isn't entirely
10688 // correct; we can end up with multiple unmerged declarations of the same
10689 // implicit entity.
10690 if (D->isImplicit())
10691 continue;
10692
10693 DeclContext *CanonDef = D->getDeclContext();
10694
10695 bool Found = false;
10696 const Decl *DCanon = D->getCanonicalDecl();
10697
10698 for (auto *RI : D->redecls()) {
10699 if (RI->getLexicalDeclContext() == CanonDef) {
10700 Found = true;
10701 break;
10702 }
10703 }
10704 if (Found)
10705 continue;
10706
10707 // Quick check failed, time to do the slow thing. Note, we can't just
10708 // look up the name of D in CanonDef here, because the member that is
10709 // in CanonDef might not be found by name lookup (it might have been
10710 // replaced by a more recent declaration in the lookup table), and we
10711 // can't necessarily find it in the redeclaration chain because it might
10712 // be merely mergeable, not redeclarable.
10713 llvm::SmallVector<const NamedDecl*, 4> Candidates;
10714 for (auto *CanonMember : CanonDef->decls()) {
10715 if (CanonMember->getCanonicalDecl() == DCanon) {
10716 // This can happen if the declaration is merely mergeable and not
10717 // actually redeclarable (we looked for redeclarations earlier).
10718 //
10719 // FIXME: We should be able to detect this more efficiently, without
10720 // pulling in all of the members of CanonDef.
10721 Found = true;
10722 break;
10723 }
10724 if (auto *ND = dyn_cast<NamedDecl>(Val: CanonMember))
10725 if (ND->getDeclName() == D->getDeclName())
10726 Candidates.push_back(Elt: ND);
10727 }
10728
10729 if (!Found) {
10730 // The AST doesn't like TagDecls becoming invalid after they've been
10731 // completed. We only really need to mark FieldDecls as invalid here.
10732 if (!isa<TagDecl>(Val: D))
10733 D->setInvalidDecl();
10734
10735 // Ensure we don't accidentally recursively enter deserialization while
10736 // we're producing our diagnostic.
10737 Deserializing RecursionGuard(this);
10738
10739 std::string CanonDefModule =
10740 ODRDiagsEmitter::getOwningModuleNameForDiagnostic(
10741 D: cast<Decl>(Val: CanonDef));
10742 Diag(Loc: D->getLocation(), DiagID: diag::err_module_odr_violation_missing_decl)
10743 << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)
10744 << CanonDef << CanonDefModule.empty() << CanonDefModule;
10745
10746 if (Candidates.empty())
10747 Diag(Loc: cast<Decl>(Val: CanonDef)->getLocation(),
10748 DiagID: diag::note_module_odr_violation_no_possible_decls) << D;
10749 else {
10750 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
10751 Diag(Loc: Candidates[I]->getLocation(),
10752 DiagID: diag::note_module_odr_violation_possible_decl)
10753 << Candidates[I];
10754 }
10755
10756 DiagnosedOdrMergeFailures.insert(Ptr: CanonDef);
10757 }
10758 }
10759
10760 if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&
10761 FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&
10762 ObjCInterfaceOdrMergeFailures.empty() &&
10763 ObjCProtocolOdrMergeFailures.empty())
10764 return;
10765
10766 ODRDiagsEmitter DiagsEmitter(Diags, getContext(),
10767 getPreprocessor().getLangOpts());
10768
10769 // Issue any pending ODR-failure diagnostics.
10770 for (auto &Merge : OdrMergeFailures) {
10771 // If we've already pointed out a specific problem with this class, don't
10772 // bother issuing a general "something's different" diagnostic.
10773 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10774 continue;
10775
10776 bool Diagnosed = false;
10777 CXXRecordDecl *FirstRecord = Merge.first;
10778 for (auto &RecordPair : Merge.second) {
10779 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord: RecordPair.first,
10780 SecondDD: RecordPair.second)) {
10781 Diagnosed = true;
10782 break;
10783 }
10784 }
10785
10786 if (!Diagnosed) {
10787 // All definitions are updates to the same declaration. This happens if a
10788 // module instantiates the declaration of a class template specialization
10789 // and two or more other modules instantiate its definition.
10790 //
10791 // FIXME: Indicate which modules had instantiations of this definition.
10792 // FIXME: How can this even happen?
10793 Diag(Loc: Merge.first->getLocation(),
10794 DiagID: diag::err_module_odr_violation_different_instantiations)
10795 << Merge.first;
10796 }
10797 }
10798
10799 // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note
10800 // that in C++ this is done as a part of CXXRecordDecl ODR checking.
10801 for (auto &Merge : RecordOdrMergeFailures) {
10802 // If we've already pointed out a specific problem with this class, don't
10803 // bother issuing a general "something's different" diagnostic.
10804 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10805 continue;
10806
10807 RecordDecl *FirstRecord = Merge.first;
10808 bool Diagnosed = false;
10809 for (auto *SecondRecord : Merge.second) {
10810 if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {
10811 Diagnosed = true;
10812 break;
10813 }
10814 }
10815 (void)Diagnosed;
10816 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10817 }
10818
10819 // Issue ODR failures diagnostics for functions.
10820 for (auto &Merge : FunctionOdrMergeFailures) {
10821 FunctionDecl *FirstFunction = Merge.first;
10822 bool Diagnosed = false;
10823 for (auto &SecondFunction : Merge.second) {
10824 if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {
10825 Diagnosed = true;
10826 break;
10827 }
10828 }
10829 (void)Diagnosed;
10830 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10831 }
10832
10833 // Issue ODR failures diagnostics for enums.
10834 for (auto &Merge : EnumOdrMergeFailures) {
10835 // If we've already pointed out a specific problem with this enum, don't
10836 // bother issuing a general "something's different" diagnostic.
10837 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10838 continue;
10839
10840 EnumDecl *FirstEnum = Merge.first;
10841 bool Diagnosed = false;
10842 for (auto &SecondEnum : Merge.second) {
10843 if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {
10844 Diagnosed = true;
10845 break;
10846 }
10847 }
10848 (void)Diagnosed;
10849 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10850 }
10851
10852 for (auto &Merge : ObjCInterfaceOdrMergeFailures) {
10853 // If we've already pointed out a specific problem with this interface,
10854 // don't bother issuing a general "something's different" diagnostic.
10855 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10856 continue;
10857
10858 bool Diagnosed = false;
10859 ObjCInterfaceDecl *FirstID = Merge.first;
10860 for (auto &InterfacePair : Merge.second) {
10861 if (DiagsEmitter.diagnoseMismatch(FirstID, SecondID: InterfacePair.first,
10862 SecondDD: InterfacePair.second)) {
10863 Diagnosed = true;
10864 break;
10865 }
10866 }
10867 (void)Diagnosed;
10868 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10869 }
10870
10871 for (auto &Merge : ObjCProtocolOdrMergeFailures) {
10872 // If we've already pointed out a specific problem with this protocol,
10873 // don't bother issuing a general "something's different" diagnostic.
10874 if (!DiagnosedOdrMergeFailures.insert(Ptr: Merge.first).second)
10875 continue;
10876
10877 ObjCProtocolDecl *FirstProtocol = Merge.first;
10878 bool Diagnosed = false;
10879 for (auto &ProtocolPair : Merge.second) {
10880 if (DiagsEmitter.diagnoseMismatch(FirstProtocol, SecondProtocol: ProtocolPair.first,
10881 SecondDD: ProtocolPair.second)) {
10882 Diagnosed = true;
10883 break;
10884 }
10885 }
10886 (void)Diagnosed;
10887 assert(Diagnosed && "Unable to emit ODR diagnostic.");
10888 }
10889}
10890
10891void ASTReader::StartedDeserializing() {
10892 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
10893 ReadTimer->startTimer();
10894}
10895
10896void ASTReader::FinishedDeserializing() {
10897 assert(NumCurrentElementsDeserializing &&
10898 "FinishedDeserializing not paired with StartedDeserializing");
10899 if (NumCurrentElementsDeserializing == 1) {
10900 // We decrease NumCurrentElementsDeserializing only after pending actions
10901 // are finished, to avoid recursively re-calling finishPendingActions().
10902 finishPendingActions();
10903 }
10904 --NumCurrentElementsDeserializing;
10905
10906 if (NumCurrentElementsDeserializing == 0) {
10907 {
10908 // Guard variable to avoid recursively entering the process of passing
10909 // decls to consumer.
10910 SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,
10911 /*NewValue=*/false);
10912
10913 // Propagate exception specification and deduced type updates along
10914 // redeclaration chains.
10915 //
10916 // We do this now rather than in finishPendingActions because we want to
10917 // be able to walk the complete redeclaration chains of the updated decls.
10918 while (!PendingExceptionSpecUpdates.empty() ||
10919 !PendingDeducedTypeUpdates.empty() ||
10920 !PendingUndeducedFunctionDecls.empty()) {
10921 auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10922 PendingExceptionSpecUpdates.clear();
10923 for (auto Update : ESUpdates) {
10924 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10925 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10926 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10927 if (auto *Listener = getContext().getASTMutationListener())
10928 Listener->ResolvedExceptionSpec(FD: cast<FunctionDecl>(Val: Update.second));
10929 for (auto *Redecl : Update.second->redecls())
10930 getContext().adjustExceptionSpec(FD: cast<FunctionDecl>(Val: Redecl), ESI);
10931 }
10932
10933 auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10934 PendingDeducedTypeUpdates.clear();
10935 for (auto Update : DTUpdates) {
10936 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10937 // FIXME: If the return type is already deduced, check that it
10938 // matches.
10939 getContext().adjustDeducedFunctionResultType(FD: Update.first,
10940 ResultType: Update.second);
10941 }
10942
10943 auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10944 PendingUndeducedFunctionDecls.clear();
10945 // We hope we can find the deduced type for the functions by iterating
10946 // redeclarations in other modules.
10947 for (FunctionDecl *UndeducedFD : UDTUpdates)
10948 (void)UndeducedFD->getMostRecentDecl();
10949 }
10950
10951 if (ReadTimer)
10952 ReadTimer->stopTimer();
10953
10954 diagnoseOdrViolations();
10955 }
10956
10957 // We are not in recursive loading, so it's safe to pass the "interesting"
10958 // decls to the consumer.
10959 if (Consumer)
10960 PassInterestingDeclsToConsumer();
10961 }
10962}
10963
10964void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
10965 if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {
10966 // Remove any fake results before adding any real ones.
10967 auto It = PendingFakeLookupResults.find(Key: II);
10968 if (It != PendingFakeLookupResults.end()) {
10969 for (auto *ND : It->second)
10970 SemaObj->IdResolver.RemoveDecl(D: ND);
10971 // FIXME: this works around module+PCH performance issue.
10972 // Rather than erase the result from the map, which is O(n), just clear
10973 // the vector of NamedDecls.
10974 It->second.clear();
10975 }
10976 }
10977
10978 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
10979 SemaObj->TUScope->AddDecl(D);
10980 } else if (SemaObj->TUScope) {
10981 // Adding the decl to IdResolver may have failed because it was already in
10982 // (even though it was not added in scope). If it is already in, make sure
10983 // it gets in the scope as well.
10984 if (llvm::is_contained(Range: SemaObj->IdResolver.decls(Name), Element: D))
10985 SemaObj->TUScope->AddDecl(D);
10986 }
10987}
10988
10989ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,
10990 ASTContext *Context,
10991 const PCHContainerReader &PCHContainerRdr,
10992 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
10993 StringRef isysroot,
10994 DisableValidationForModuleKind DisableValidationKind,
10995 bool AllowASTWithCompilerErrors,
10996 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
10997 bool ForceValidateUserInputs,
10998 bool ValidateASTInputFilesContent, bool UseGlobalIndex,
10999 std::unique_ptr<llvm::Timer> ReadTimer)
11000 : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)
11001 ? cast<ASTReaderListener>(Val: new SimpleASTReaderListener(PP))
11002 : cast<ASTReaderListener>(Val: new PCHValidator(PP, *this))),
11003 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11004 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),
11005 StackHandler(Diags), PP(PP), ContextObj(Context),
11006 ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,
11007 PP.getHeaderSearchInfo()),
11008 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11009 DisableValidationKind(DisableValidationKind),
11010 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11011 AllowConfigurationMismatch(AllowConfigurationMismatch),
11012 ValidateSystemInputs(ValidateSystemInputs),
11013 ForceValidateUserInputs(ForceValidateUserInputs),
11014 ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11015 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11016 SourceMgr.setExternalSLocEntrySource(this);
11017
11018 PathBuf.reserve(N: 256);
11019
11020 for (const auto &Ext : Extensions) {
11021 auto BlockName = Ext->getExtensionMetadata().BlockName;
11022 auto Known = ModuleFileExtensions.find(Key: BlockName);
11023 if (Known != ModuleFileExtensions.end()) {
11024 Diags.Report(DiagID: diag::warn_duplicate_module_file_extension)
11025 << BlockName;
11026 continue;
11027 }
11028
11029 ModuleFileExtensions.insert(KV: {BlockName, Ext});
11030 }
11031}
11032
11033ASTReader::~ASTReader() {
11034 if (OwnsDeserializationListener)
11035 delete DeserializationListener;
11036}
11037
11038IdentifierResolver &ASTReader::getIdResolver() {
11039 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11040}
11041
11042Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11043 unsigned AbbrevID) {
11044 Idx = 0;
11045 Record.clear();
11046 return Cursor.readRecord(AbbrevID, Vals&: Record);
11047}
11048//===----------------------------------------------------------------------===//
11049//// OMPClauseReader implementation
11050////===----------------------------------------------------------------------===//
11051
11052// This has to be in namespace clang because it's friended by all
11053// of the OMP clauses.
11054namespace clang {
11055
11056class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11057 ASTRecordReader &Record;
11058 ASTContext &Context;
11059
11060public:
11061 OMPClauseReader(ASTRecordReader &Record)
11062 : Record(Record), Context(Record.getContext()) {}
11063#define GEN_CLANG_CLAUSE_CLASS
11064#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11065#include "llvm/Frontend/OpenMP/OMP.inc"
11066 OMPClause *readClause();
11067 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11068 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11069};
11070
11071} // end namespace clang
11072
11073OMPClause *ASTRecordReader::readOMPClause() {
11074 return OMPClauseReader(*this).readClause();
11075}
11076
11077OMPClause *OMPClauseReader::readClause() {
11078 OMPClause *C = nullptr;
11079 switch (llvm::omp::Clause(Record.readInt())) {
11080 case llvm::omp::OMPC_if:
11081 C = new (Context) OMPIfClause();
11082 break;
11083 case llvm::omp::OMPC_final:
11084 C = new (Context) OMPFinalClause();
11085 break;
11086 case llvm::omp::OMPC_num_threads:
11087 C = new (Context) OMPNumThreadsClause();
11088 break;
11089 case llvm::omp::OMPC_safelen:
11090 C = new (Context) OMPSafelenClause();
11091 break;
11092 case llvm::omp::OMPC_simdlen:
11093 C = new (Context) OMPSimdlenClause();
11094 break;
11095 case llvm::omp::OMPC_sizes: {
11096 unsigned NumSizes = Record.readInt();
11097 C = OMPSizesClause::CreateEmpty(C: Context, NumSizes);
11098 break;
11099 }
11100 case llvm::omp::OMPC_permutation: {
11101 unsigned NumLoops = Record.readInt();
11102 C = OMPPermutationClause::CreateEmpty(C: Context, NumLoops);
11103 break;
11104 }
11105 case llvm::omp::OMPC_full:
11106 C = OMPFullClause::CreateEmpty(C: Context);
11107 break;
11108 case llvm::omp::OMPC_partial:
11109 C = OMPPartialClause::CreateEmpty(C: Context);
11110 break;
11111 case llvm::omp::OMPC_allocator:
11112 C = new (Context) OMPAllocatorClause();
11113 break;
11114 case llvm::omp::OMPC_collapse:
11115 C = new (Context) OMPCollapseClause();
11116 break;
11117 case llvm::omp::OMPC_default:
11118 C = new (Context) OMPDefaultClause();
11119 break;
11120 case llvm::omp::OMPC_proc_bind:
11121 C = new (Context) OMPProcBindClause();
11122 break;
11123 case llvm::omp::OMPC_schedule:
11124 C = new (Context) OMPScheduleClause();
11125 break;
11126 case llvm::omp::OMPC_ordered:
11127 C = OMPOrderedClause::CreateEmpty(C: Context, NumLoops: Record.readInt());
11128 break;
11129 case llvm::omp::OMPC_nowait:
11130 C = new (Context) OMPNowaitClause();
11131 break;
11132 case llvm::omp::OMPC_untied:
11133 C = new (Context) OMPUntiedClause();
11134 break;
11135 case llvm::omp::OMPC_mergeable:
11136 C = new (Context) OMPMergeableClause();
11137 break;
11138 case llvm::omp::OMPC_read:
11139 C = new (Context) OMPReadClause();
11140 break;
11141 case llvm::omp::OMPC_write:
11142 C = new (Context) OMPWriteClause();
11143 break;
11144 case llvm::omp::OMPC_update:
11145 C = OMPUpdateClause::CreateEmpty(C: Context, IsExtended: Record.readInt());
11146 break;
11147 case llvm::omp::OMPC_capture:
11148 C = new (Context) OMPCaptureClause();
11149 break;
11150 case llvm::omp::OMPC_compare:
11151 C = new (Context) OMPCompareClause();
11152 break;
11153 case llvm::omp::OMPC_fail:
11154 C = new (Context) OMPFailClause();
11155 break;
11156 case llvm::omp::OMPC_seq_cst:
11157 C = new (Context) OMPSeqCstClause();
11158 break;
11159 case llvm::omp::OMPC_acq_rel:
11160 C = new (Context) OMPAcqRelClause();
11161 break;
11162 case llvm::omp::OMPC_absent: {
11163 unsigned NumKinds = Record.readInt();
11164 C = OMPAbsentClause::CreateEmpty(C: Context, NumKinds);
11165 break;
11166 }
11167 case llvm::omp::OMPC_holds:
11168 C = new (Context) OMPHoldsClause();
11169 break;
11170 case llvm::omp::OMPC_contains: {
11171 unsigned NumKinds = Record.readInt();
11172 C = OMPContainsClause::CreateEmpty(C: Context, NumKinds);
11173 break;
11174 }
11175 case llvm::omp::OMPC_no_openmp:
11176 C = new (Context) OMPNoOpenMPClause();
11177 break;
11178 case llvm::omp::OMPC_no_openmp_routines:
11179 C = new (Context) OMPNoOpenMPRoutinesClause();
11180 break;
11181 case llvm::omp::OMPC_no_openmp_constructs:
11182 C = new (Context) OMPNoOpenMPConstructsClause();
11183 break;
11184 case llvm::omp::OMPC_no_parallelism:
11185 C = new (Context) OMPNoParallelismClause();
11186 break;
11187 case llvm::omp::OMPC_acquire:
11188 C = new (Context) OMPAcquireClause();
11189 break;
11190 case llvm::omp::OMPC_release:
11191 C = new (Context) OMPReleaseClause();
11192 break;
11193 case llvm::omp::OMPC_relaxed:
11194 C = new (Context) OMPRelaxedClause();
11195 break;
11196 case llvm::omp::OMPC_weak:
11197 C = new (Context) OMPWeakClause();
11198 break;
11199 case llvm::omp::OMPC_threads:
11200 C = new (Context) OMPThreadsClause();
11201 break;
11202 case llvm::omp::OMPC_simd:
11203 C = new (Context) OMPSIMDClause();
11204 break;
11205 case llvm::omp::OMPC_nogroup:
11206 C = new (Context) OMPNogroupClause();
11207 break;
11208 case llvm::omp::OMPC_unified_address:
11209 C = new (Context) OMPUnifiedAddressClause();
11210 break;
11211 case llvm::omp::OMPC_unified_shared_memory:
11212 C = new (Context) OMPUnifiedSharedMemoryClause();
11213 break;
11214 case llvm::omp::OMPC_reverse_offload:
11215 C = new (Context) OMPReverseOffloadClause();
11216 break;
11217 case llvm::omp::OMPC_dynamic_allocators:
11218 C = new (Context) OMPDynamicAllocatorsClause();
11219 break;
11220 case llvm::omp::OMPC_atomic_default_mem_order:
11221 C = new (Context) OMPAtomicDefaultMemOrderClause();
11222 break;
11223 case llvm::omp::OMPC_self_maps:
11224 C = new (Context) OMPSelfMapsClause();
11225 break;
11226 case llvm::omp::OMPC_at:
11227 C = new (Context) OMPAtClause();
11228 break;
11229 case llvm::omp::OMPC_severity:
11230 C = new (Context) OMPSeverityClause();
11231 break;
11232 case llvm::omp::OMPC_message:
11233 C = new (Context) OMPMessageClause();
11234 break;
11235 case llvm::omp::OMPC_private:
11236 C = OMPPrivateClause::CreateEmpty(C: Context, N: Record.readInt());
11237 break;
11238 case llvm::omp::OMPC_firstprivate:
11239 C = OMPFirstprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11240 break;
11241 case llvm::omp::OMPC_lastprivate:
11242 C = OMPLastprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11243 break;
11244 case llvm::omp::OMPC_shared:
11245 C = OMPSharedClause::CreateEmpty(C: Context, N: Record.readInt());
11246 break;
11247 case llvm::omp::OMPC_reduction: {
11248 unsigned N = Record.readInt();
11249 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11250 C = OMPReductionClause::CreateEmpty(C: Context, N, Modifier);
11251 break;
11252 }
11253 case llvm::omp::OMPC_task_reduction:
11254 C = OMPTaskReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11255 break;
11256 case llvm::omp::OMPC_in_reduction:
11257 C = OMPInReductionClause::CreateEmpty(C: Context, N: Record.readInt());
11258 break;
11259 case llvm::omp::OMPC_linear:
11260 C = OMPLinearClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11261 break;
11262 case llvm::omp::OMPC_aligned:
11263 C = OMPAlignedClause::CreateEmpty(C: Context, NumVars: Record.readInt());
11264 break;
11265 case llvm::omp::OMPC_copyin:
11266 C = OMPCopyinClause::CreateEmpty(C: Context, N: Record.readInt());
11267 break;
11268 case llvm::omp::OMPC_copyprivate:
11269 C = OMPCopyprivateClause::CreateEmpty(C: Context, N: Record.readInt());
11270 break;
11271 case llvm::omp::OMPC_flush:
11272 C = OMPFlushClause::CreateEmpty(C: Context, N: Record.readInt());
11273 break;
11274 case llvm::omp::OMPC_depobj:
11275 C = OMPDepobjClause::CreateEmpty(C: Context);
11276 break;
11277 case llvm::omp::OMPC_depend: {
11278 unsigned NumVars = Record.readInt();
11279 unsigned NumLoops = Record.readInt();
11280 C = OMPDependClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11281 break;
11282 }
11283 case llvm::omp::OMPC_device:
11284 C = new (Context) OMPDeviceClause();
11285 break;
11286 case llvm::omp::OMPC_map: {
11287 OMPMappableExprListSizeTy Sizes;
11288 Sizes.NumVars = Record.readInt();
11289 Sizes.NumUniqueDeclarations = Record.readInt();
11290 Sizes.NumComponentLists = Record.readInt();
11291 Sizes.NumComponents = Record.readInt();
11292 C = OMPMapClause::CreateEmpty(C: Context, Sizes);
11293 break;
11294 }
11295 case llvm::omp::OMPC_num_teams:
11296 C = OMPNumTeamsClause::CreateEmpty(C: Context, N: Record.readInt());
11297 break;
11298 case llvm::omp::OMPC_thread_limit:
11299 C = OMPThreadLimitClause::CreateEmpty(C: Context, N: Record.readInt());
11300 break;
11301 case llvm::omp::OMPC_priority:
11302 C = new (Context) OMPPriorityClause();
11303 break;
11304 case llvm::omp::OMPC_grainsize:
11305 C = new (Context) OMPGrainsizeClause();
11306 break;
11307 case llvm::omp::OMPC_num_tasks:
11308 C = new (Context) OMPNumTasksClause();
11309 break;
11310 case llvm::omp::OMPC_hint:
11311 C = new (Context) OMPHintClause();
11312 break;
11313 case llvm::omp::OMPC_dist_schedule:
11314 C = new (Context) OMPDistScheduleClause();
11315 break;
11316 case llvm::omp::OMPC_defaultmap:
11317 C = new (Context) OMPDefaultmapClause();
11318 break;
11319 case llvm::omp::OMPC_to: {
11320 OMPMappableExprListSizeTy Sizes;
11321 Sizes.NumVars = Record.readInt();
11322 Sizes.NumUniqueDeclarations = Record.readInt();
11323 Sizes.NumComponentLists = Record.readInt();
11324 Sizes.NumComponents = Record.readInt();
11325 C = OMPToClause::CreateEmpty(C: Context, Sizes);
11326 break;
11327 }
11328 case llvm::omp::OMPC_from: {
11329 OMPMappableExprListSizeTy Sizes;
11330 Sizes.NumVars = Record.readInt();
11331 Sizes.NumUniqueDeclarations = Record.readInt();
11332 Sizes.NumComponentLists = Record.readInt();
11333 Sizes.NumComponents = Record.readInt();
11334 C = OMPFromClause::CreateEmpty(C: Context, Sizes);
11335 break;
11336 }
11337 case llvm::omp::OMPC_use_device_ptr: {
11338 OMPMappableExprListSizeTy Sizes;
11339 Sizes.NumVars = Record.readInt();
11340 Sizes.NumUniqueDeclarations = Record.readInt();
11341 Sizes.NumComponentLists = Record.readInt();
11342 Sizes.NumComponents = Record.readInt();
11343 C = OMPUseDevicePtrClause::CreateEmpty(C: Context, Sizes);
11344 break;
11345 }
11346 case llvm::omp::OMPC_use_device_addr: {
11347 OMPMappableExprListSizeTy Sizes;
11348 Sizes.NumVars = Record.readInt();
11349 Sizes.NumUniqueDeclarations = Record.readInt();
11350 Sizes.NumComponentLists = Record.readInt();
11351 Sizes.NumComponents = Record.readInt();
11352 C = OMPUseDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11353 break;
11354 }
11355 case llvm::omp::OMPC_is_device_ptr: {
11356 OMPMappableExprListSizeTy Sizes;
11357 Sizes.NumVars = Record.readInt();
11358 Sizes.NumUniqueDeclarations = Record.readInt();
11359 Sizes.NumComponentLists = Record.readInt();
11360 Sizes.NumComponents = Record.readInt();
11361 C = OMPIsDevicePtrClause::CreateEmpty(C: Context, Sizes);
11362 break;
11363 }
11364 case llvm::omp::OMPC_has_device_addr: {
11365 OMPMappableExprListSizeTy Sizes;
11366 Sizes.NumVars = Record.readInt();
11367 Sizes.NumUniqueDeclarations = Record.readInt();
11368 Sizes.NumComponentLists = Record.readInt();
11369 Sizes.NumComponents = Record.readInt();
11370 C = OMPHasDeviceAddrClause::CreateEmpty(C: Context, Sizes);
11371 break;
11372 }
11373 case llvm::omp::OMPC_allocate:
11374 C = OMPAllocateClause::CreateEmpty(C: Context, N: Record.readInt());
11375 break;
11376 case llvm::omp::OMPC_nontemporal:
11377 C = OMPNontemporalClause::CreateEmpty(C: Context, N: Record.readInt());
11378 break;
11379 case llvm::omp::OMPC_inclusive:
11380 C = OMPInclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11381 break;
11382 case llvm::omp::OMPC_exclusive:
11383 C = OMPExclusiveClause::CreateEmpty(C: Context, N: Record.readInt());
11384 break;
11385 case llvm::omp::OMPC_order:
11386 C = new (Context) OMPOrderClause();
11387 break;
11388 case llvm::omp::OMPC_init:
11389 C = OMPInitClause::CreateEmpty(C: Context, N: Record.readInt());
11390 break;
11391 case llvm::omp::OMPC_use:
11392 C = new (Context) OMPUseClause();
11393 break;
11394 case llvm::omp::OMPC_destroy:
11395 C = new (Context) OMPDestroyClause();
11396 break;
11397 case llvm::omp::OMPC_novariants:
11398 C = new (Context) OMPNovariantsClause();
11399 break;
11400 case llvm::omp::OMPC_nocontext:
11401 C = new (Context) OMPNocontextClause();
11402 break;
11403 case llvm::omp::OMPC_detach:
11404 C = new (Context) OMPDetachClause();
11405 break;
11406 case llvm::omp::OMPC_uses_allocators:
11407 C = OMPUsesAllocatorsClause::CreateEmpty(C: Context, N: Record.readInt());
11408 break;
11409 case llvm::omp::OMPC_affinity:
11410 C = OMPAffinityClause::CreateEmpty(C: Context, N: Record.readInt());
11411 break;
11412 case llvm::omp::OMPC_filter:
11413 C = new (Context) OMPFilterClause();
11414 break;
11415 case llvm::omp::OMPC_bind:
11416 C = OMPBindClause::CreateEmpty(C: Context);
11417 break;
11418 case llvm::omp::OMPC_align:
11419 C = new (Context) OMPAlignClause();
11420 break;
11421 case llvm::omp::OMPC_ompx_dyn_cgroup_mem:
11422 C = new (Context) OMPXDynCGroupMemClause();
11423 break;
11424 case llvm::omp::OMPC_doacross: {
11425 unsigned NumVars = Record.readInt();
11426 unsigned NumLoops = Record.readInt();
11427 C = OMPDoacrossClause::CreateEmpty(C: Context, N: NumVars, NumLoops);
11428 break;
11429 }
11430 case llvm::omp::OMPC_ompx_attribute:
11431 C = new (Context) OMPXAttributeClause();
11432 break;
11433 case llvm::omp::OMPC_ompx_bare:
11434 C = new (Context) OMPXBareClause();
11435 break;
11436#define OMP_CLAUSE_NO_CLASS(Enum, Str) \
11437 case llvm::omp::Enum: \
11438 break;
11439#include "llvm/Frontend/OpenMP/OMPKinds.def"
11440 default:
11441 break;
11442 }
11443 assert(C && "Unknown OMPClause type");
11444
11445 Visit(S: C);
11446 C->setLocStart(Record.readSourceLocation());
11447 C->setLocEnd(Record.readSourceLocation());
11448
11449 return C;
11450}
11451
11452void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11453 C->setPreInitStmt(S: Record.readSubStmt(),
11454 ThisRegion: static_cast<OpenMPDirectiveKind>(Record.readInt()));
11455}
11456
11457void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11458 VisitOMPClauseWithPreInit(C);
11459 C->setPostUpdateExpr(Record.readSubExpr());
11460}
11461
11462void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11463 VisitOMPClauseWithPreInit(C);
11464 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11465 C->setNameModifierLoc(Record.readSourceLocation());
11466 C->setColonLoc(Record.readSourceLocation());
11467 C->setCondition(Record.readSubExpr());
11468 C->setLParenLoc(Record.readSourceLocation());
11469}
11470
11471void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11472 VisitOMPClauseWithPreInit(C);
11473 C->setCondition(Record.readSubExpr());
11474 C->setLParenLoc(Record.readSourceLocation());
11475}
11476
11477void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11478 VisitOMPClauseWithPreInit(C);
11479 C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());
11480 C->setNumThreads(Record.readSubExpr());
11481 C->setModifierLoc(Record.readSourceLocation());
11482 C->setLParenLoc(Record.readSourceLocation());
11483}
11484
11485void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11486 C->setSafelen(Record.readSubExpr());
11487 C->setLParenLoc(Record.readSourceLocation());
11488}
11489
11490void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11491 C->setSimdlen(Record.readSubExpr());
11492 C->setLParenLoc(Record.readSourceLocation());
11493}
11494
11495void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11496 for (Expr *&E : C->getSizesRefs())
11497 E = Record.readSubExpr();
11498 C->setLParenLoc(Record.readSourceLocation());
11499}
11500
11501void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {
11502 for (Expr *&E : C->getArgsRefs())
11503 E = Record.readSubExpr();
11504 C->setLParenLoc(Record.readSourceLocation());
11505}
11506
11507void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11508
11509void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11510 C->setFactor(Record.readSubExpr());
11511 C->setLParenLoc(Record.readSourceLocation());
11512}
11513
11514void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11515 C->setAllocator(Record.readExpr());
11516 C->setLParenLoc(Record.readSourceLocation());
11517}
11518
11519void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11520 C->setNumForLoops(Record.readSubExpr());
11521 C->setLParenLoc(Record.readSourceLocation());
11522}
11523
11524void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11525 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11526 C->setLParenLoc(Record.readSourceLocation());
11527 C->setDefaultKindKwLoc(Record.readSourceLocation());
11528}
11529
11530void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11531 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11532 C->setLParenLoc(Record.readSourceLocation());
11533 C->setProcBindKindKwLoc(Record.readSourceLocation());
11534}
11535
11536void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
11537 VisitOMPClauseWithPreInit(C);
11538 C->setScheduleKind(
11539 static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
11540 C->setFirstScheduleModifier(
11541 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11542 C->setSecondScheduleModifier(
11543 static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
11544 C->setChunkSize(Record.readSubExpr());
11545 C->setLParenLoc(Record.readSourceLocation());
11546 C->setFirstScheduleModifierLoc(Record.readSourceLocation());
11547 C->setSecondScheduleModifierLoc(Record.readSourceLocation());
11548 C->setScheduleKindLoc(Record.readSourceLocation());
11549 C->setCommaLoc(Record.readSourceLocation());
11550}
11551
11552void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
11553 C->setNumForLoops(Record.readSubExpr());
11554 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11555 C->setLoopNumIterations(NumLoop: I, NumIterations: Record.readSubExpr());
11556 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
11557 C->setLoopCounter(NumLoop: I, Counter: Record.readSubExpr());
11558 C->setLParenLoc(Record.readSourceLocation());
11559}
11560
11561void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
11562 C->setEventHandler(Record.readSubExpr());
11563 C->setLParenLoc(Record.readSourceLocation());
11564}
11565
11566void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
11567
11568void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
11569
11570void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
11571
11572void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
11573
11574void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
11575
11576void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
11577 if (C->isExtended()) {
11578 C->setLParenLoc(Record.readSourceLocation());
11579 C->setArgumentLoc(Record.readSourceLocation());
11580 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
11581 }
11582}
11583
11584void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
11585
11586void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
11587
11588// Read the parameter of fail clause. This will have been saved when
11589// OMPClauseWriter is called.
11590void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {
11591 C->setLParenLoc(Record.readSourceLocation());
11592 SourceLocation FailParameterLoc = Record.readSourceLocation();
11593 C->setFailParameterLoc(FailParameterLoc);
11594 OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();
11595 C->setFailParameter(CKind);
11596}
11597
11598void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {
11599 unsigned Count = C->getDirectiveKinds().size();
11600 C->setLParenLoc(Record.readSourceLocation());
11601 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11602 DKVec.reserve(N: Count);
11603 for (unsigned I = 0; I < Count; I++) {
11604 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11605 }
11606 C->setDirectiveKinds(DKVec);
11607}
11608
11609void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {
11610 C->setExpr(Record.readExpr());
11611 C->setLParenLoc(Record.readSourceLocation());
11612}
11613
11614void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {
11615 unsigned Count = C->getDirectiveKinds().size();
11616 C->setLParenLoc(Record.readSourceLocation());
11617 llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;
11618 DKVec.reserve(N: Count);
11619 for (unsigned I = 0; I < Count; I++) {
11620 DKVec.push_back(Elt: Record.readEnum<OpenMPDirectiveKind>());
11621 }
11622 C->setDirectiveKinds(DKVec);
11623}
11624
11625void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}
11626
11627void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(
11628 OMPNoOpenMPRoutinesClause *) {}
11629
11630void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(
11631 OMPNoOpenMPConstructsClause *) {}
11632
11633void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}
11634
11635void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
11636
11637void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
11638
11639void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
11640
11641void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
11642
11643void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
11644
11645void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
11646
11647void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
11648
11649void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
11650
11651void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
11652
11653void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
11654 unsigned NumVars = C->varlist_size();
11655 SmallVector<Expr *, 16> Vars;
11656 Vars.reserve(N: NumVars);
11657 for (unsigned I = 0; I != NumVars; ++I)
11658 Vars.push_back(Elt: Record.readSubExpr());
11659 C->setVarRefs(Vars);
11660 C->setIsTarget(Record.readBool());
11661 C->setIsTargetSync(Record.readBool());
11662 C->setLParenLoc(Record.readSourceLocation());
11663 C->setVarLoc(Record.readSourceLocation());
11664}
11665
11666void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
11667 C->setInteropVar(Record.readSubExpr());
11668 C->setLParenLoc(Record.readSourceLocation());
11669 C->setVarLoc(Record.readSourceLocation());
11670}
11671
11672void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
11673 C->setInteropVar(Record.readSubExpr());
11674 C->setLParenLoc(Record.readSourceLocation());
11675 C->setVarLoc(Record.readSourceLocation());
11676}
11677
11678void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
11679 VisitOMPClauseWithPreInit(C);
11680 C->setCondition(Record.readSubExpr());
11681 C->setLParenLoc(Record.readSourceLocation());
11682}
11683
11684void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
11685 VisitOMPClauseWithPreInit(C);
11686 C->setCondition(Record.readSubExpr());
11687 C->setLParenLoc(Record.readSourceLocation());
11688}
11689
11690void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
11691
11692void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
11693 OMPUnifiedSharedMemoryClause *) {}
11694
11695void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
11696
11697void
11698OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
11699}
11700
11701void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
11702 OMPAtomicDefaultMemOrderClause *C) {
11703 C->setAtomicDefaultMemOrderKind(
11704 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
11705 C->setLParenLoc(Record.readSourceLocation());
11706 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
11707}
11708
11709void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}
11710
11711void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
11712 C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
11713 C->setLParenLoc(Record.readSourceLocation());
11714 C->setAtKindKwLoc(Record.readSourceLocation());
11715}
11716
11717void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
11718 C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));
11719 C->setLParenLoc(Record.readSourceLocation());
11720 C->setSeverityKindKwLoc(Record.readSourceLocation());
11721}
11722
11723void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
11724 C->setMessageString(Record.readSubExpr());
11725 C->setLParenLoc(Record.readSourceLocation());
11726}
11727
11728void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
11729 C->setLParenLoc(Record.readSourceLocation());
11730 unsigned NumVars = C->varlist_size();
11731 SmallVector<Expr *, 16> Vars;
11732 Vars.reserve(N: NumVars);
11733 for (unsigned i = 0; i != NumVars; ++i)
11734 Vars.push_back(Elt: Record.readSubExpr());
11735 C->setVarRefs(Vars);
11736 Vars.clear();
11737 for (unsigned i = 0; i != NumVars; ++i)
11738 Vars.push_back(Elt: Record.readSubExpr());
11739 C->setPrivateCopies(Vars);
11740}
11741
11742void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
11743 VisitOMPClauseWithPreInit(C);
11744 C->setLParenLoc(Record.readSourceLocation());
11745 unsigned NumVars = C->varlist_size();
11746 SmallVector<Expr *, 16> Vars;
11747 Vars.reserve(N: NumVars);
11748 for (unsigned i = 0; i != NumVars; ++i)
11749 Vars.push_back(Elt: Record.readSubExpr());
11750 C->setVarRefs(Vars);
11751 Vars.clear();
11752 for (unsigned i = 0; i != NumVars; ++i)
11753 Vars.push_back(Elt: Record.readSubExpr());
11754 C->setPrivateCopies(Vars);
11755 Vars.clear();
11756 for (unsigned i = 0; i != NumVars; ++i)
11757 Vars.push_back(Elt: Record.readSubExpr());
11758 C->setInits(Vars);
11759}
11760
11761void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
11762 VisitOMPClauseWithPostUpdate(C);
11763 C->setLParenLoc(Record.readSourceLocation());
11764 C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
11765 C->setKindLoc(Record.readSourceLocation());
11766 C->setColonLoc(Record.readSourceLocation());
11767 unsigned NumVars = C->varlist_size();
11768 SmallVector<Expr *, 16> Vars;
11769 Vars.reserve(N: NumVars);
11770 for (unsigned i = 0; i != NumVars; ++i)
11771 Vars.push_back(Elt: Record.readSubExpr());
11772 C->setVarRefs(Vars);
11773 Vars.clear();
11774 for (unsigned i = 0; i != NumVars; ++i)
11775 Vars.push_back(Elt: Record.readSubExpr());
11776 C->setPrivateCopies(Vars);
11777 Vars.clear();
11778 for (unsigned i = 0; i != NumVars; ++i)
11779 Vars.push_back(Elt: Record.readSubExpr());
11780 C->setSourceExprs(Vars);
11781 Vars.clear();
11782 for (unsigned i = 0; i != NumVars; ++i)
11783 Vars.push_back(Elt: Record.readSubExpr());
11784 C->setDestinationExprs(Vars);
11785 Vars.clear();
11786 for (unsigned i = 0; i != NumVars; ++i)
11787 Vars.push_back(Elt: Record.readSubExpr());
11788 C->setAssignmentOps(Vars);
11789}
11790
11791void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
11792 C->setLParenLoc(Record.readSourceLocation());
11793 unsigned NumVars = C->varlist_size();
11794 SmallVector<Expr *, 16> Vars;
11795 Vars.reserve(N: NumVars);
11796 for (unsigned i = 0; i != NumVars; ++i)
11797 Vars.push_back(Elt: Record.readSubExpr());
11798 C->setVarRefs(Vars);
11799}
11800
11801void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
11802 VisitOMPClauseWithPostUpdate(C);
11803 C->setLParenLoc(Record.readSourceLocation());
11804 C->setModifierLoc(Record.readSourceLocation());
11805 C->setColonLoc(Record.readSourceLocation());
11806 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11807 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11808 C->setQualifierLoc(NNSL);
11809 C->setNameInfo(DNI);
11810
11811 unsigned NumVars = C->varlist_size();
11812 SmallVector<Expr *, 16> Vars;
11813 Vars.reserve(N: NumVars);
11814 for (unsigned i = 0; i != NumVars; ++i)
11815 Vars.push_back(Elt: Record.readSubExpr());
11816 C->setVarRefs(Vars);
11817 Vars.clear();
11818 for (unsigned i = 0; i != NumVars; ++i)
11819 Vars.push_back(Elt: Record.readSubExpr());
11820 C->setPrivates(Vars);
11821 Vars.clear();
11822 for (unsigned i = 0; i != NumVars; ++i)
11823 Vars.push_back(Elt: Record.readSubExpr());
11824 C->setLHSExprs(Vars);
11825 Vars.clear();
11826 for (unsigned i = 0; i != NumVars; ++i)
11827 Vars.push_back(Elt: Record.readSubExpr());
11828 C->setRHSExprs(Vars);
11829 Vars.clear();
11830 for (unsigned i = 0; i != NumVars; ++i)
11831 Vars.push_back(Elt: Record.readSubExpr());
11832 C->setReductionOps(Vars);
11833 if (C->getModifier() == OMPC_REDUCTION_inscan) {
11834 Vars.clear();
11835 for (unsigned i = 0; i != NumVars; ++i)
11836 Vars.push_back(Elt: Record.readSubExpr());
11837 C->setInscanCopyOps(Vars);
11838 Vars.clear();
11839 for (unsigned i = 0; i != NumVars; ++i)
11840 Vars.push_back(Elt: Record.readSubExpr());
11841 C->setInscanCopyArrayTemps(Vars);
11842 Vars.clear();
11843 for (unsigned i = 0; i != NumVars; ++i)
11844 Vars.push_back(Elt: Record.readSubExpr());
11845 C->setInscanCopyArrayElems(Vars);
11846 }
11847 unsigned NumFlags = Record.readInt();
11848 SmallVector<bool, 16> Flags;
11849 Flags.reserve(N: NumFlags);
11850 for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(Size: NumFlags))
11851 Flags.push_back(Elt: Record.readInt());
11852 C->setPrivateVariableReductionFlags(Flags);
11853}
11854
11855void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
11856 VisitOMPClauseWithPostUpdate(C);
11857 C->setLParenLoc(Record.readSourceLocation());
11858 C->setColonLoc(Record.readSourceLocation());
11859 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11860 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11861 C->setQualifierLoc(NNSL);
11862 C->setNameInfo(DNI);
11863
11864 unsigned NumVars = C->varlist_size();
11865 SmallVector<Expr *, 16> Vars;
11866 Vars.reserve(N: NumVars);
11867 for (unsigned I = 0; I != NumVars; ++I)
11868 Vars.push_back(Elt: Record.readSubExpr());
11869 C->setVarRefs(Vars);
11870 Vars.clear();
11871 for (unsigned I = 0; I != NumVars; ++I)
11872 Vars.push_back(Elt: Record.readSubExpr());
11873 C->setPrivates(Vars);
11874 Vars.clear();
11875 for (unsigned I = 0; I != NumVars; ++I)
11876 Vars.push_back(Elt: Record.readSubExpr());
11877 C->setLHSExprs(Vars);
11878 Vars.clear();
11879 for (unsigned I = 0; I != NumVars; ++I)
11880 Vars.push_back(Elt: Record.readSubExpr());
11881 C->setRHSExprs(Vars);
11882 Vars.clear();
11883 for (unsigned I = 0; I != NumVars; ++I)
11884 Vars.push_back(Elt: Record.readSubExpr());
11885 C->setReductionOps(Vars);
11886}
11887
11888void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
11889 VisitOMPClauseWithPostUpdate(C);
11890 C->setLParenLoc(Record.readSourceLocation());
11891 C->setColonLoc(Record.readSourceLocation());
11892 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
11893 DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
11894 C->setQualifierLoc(NNSL);
11895 C->setNameInfo(DNI);
11896
11897 unsigned NumVars = C->varlist_size();
11898 SmallVector<Expr *, 16> Vars;
11899 Vars.reserve(N: NumVars);
11900 for (unsigned I = 0; I != NumVars; ++I)
11901 Vars.push_back(Elt: Record.readSubExpr());
11902 C->setVarRefs(Vars);
11903 Vars.clear();
11904 for (unsigned I = 0; I != NumVars; ++I)
11905 Vars.push_back(Elt: Record.readSubExpr());
11906 C->setPrivates(Vars);
11907 Vars.clear();
11908 for (unsigned I = 0; I != NumVars; ++I)
11909 Vars.push_back(Elt: Record.readSubExpr());
11910 C->setLHSExprs(Vars);
11911 Vars.clear();
11912 for (unsigned I = 0; I != NumVars; ++I)
11913 Vars.push_back(Elt: Record.readSubExpr());
11914 C->setRHSExprs(Vars);
11915 Vars.clear();
11916 for (unsigned I = 0; I != NumVars; ++I)
11917 Vars.push_back(Elt: Record.readSubExpr());
11918 C->setReductionOps(Vars);
11919 Vars.clear();
11920 for (unsigned I = 0; I != NumVars; ++I)
11921 Vars.push_back(Elt: Record.readSubExpr());
11922 C->setTaskgroupDescriptors(Vars);
11923}
11924
11925void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
11926 VisitOMPClauseWithPostUpdate(C);
11927 C->setLParenLoc(Record.readSourceLocation());
11928 C->setColonLoc(Record.readSourceLocation());
11929 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
11930 C->setModifierLoc(Record.readSourceLocation());
11931 unsigned NumVars = C->varlist_size();
11932 SmallVector<Expr *, 16> Vars;
11933 Vars.reserve(N: NumVars);
11934 for (unsigned i = 0; i != NumVars; ++i)
11935 Vars.push_back(Elt: Record.readSubExpr());
11936 C->setVarRefs(Vars);
11937 Vars.clear();
11938 for (unsigned i = 0; i != NumVars; ++i)
11939 Vars.push_back(Elt: Record.readSubExpr());
11940 C->setPrivates(Vars);
11941 Vars.clear();
11942 for (unsigned i = 0; i != NumVars; ++i)
11943 Vars.push_back(Elt: Record.readSubExpr());
11944 C->setInits(Vars);
11945 Vars.clear();
11946 for (unsigned i = 0; i != NumVars; ++i)
11947 Vars.push_back(Elt: Record.readSubExpr());
11948 C->setUpdates(Vars);
11949 Vars.clear();
11950 for (unsigned i = 0; i != NumVars; ++i)
11951 Vars.push_back(Elt: Record.readSubExpr());
11952 C->setFinals(Vars);
11953 C->setStep(Record.readSubExpr());
11954 C->setCalcStep(Record.readSubExpr());
11955 Vars.clear();
11956 for (unsigned I = 0; I != NumVars + 1; ++I)
11957 Vars.push_back(Elt: Record.readSubExpr());
11958 C->setUsedExprs(Vars);
11959}
11960
11961void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
11962 C->setLParenLoc(Record.readSourceLocation());
11963 C->setColonLoc(Record.readSourceLocation());
11964 unsigned NumVars = C->varlist_size();
11965 SmallVector<Expr *, 16> Vars;
11966 Vars.reserve(N: NumVars);
11967 for (unsigned i = 0; i != NumVars; ++i)
11968 Vars.push_back(Elt: Record.readSubExpr());
11969 C->setVarRefs(Vars);
11970 C->setAlignment(Record.readSubExpr());
11971}
11972
11973void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
11974 C->setLParenLoc(Record.readSourceLocation());
11975 unsigned NumVars = C->varlist_size();
11976 SmallVector<Expr *, 16> Exprs;
11977 Exprs.reserve(N: NumVars);
11978 for (unsigned i = 0; i != NumVars; ++i)
11979 Exprs.push_back(Elt: Record.readSubExpr());
11980 C->setVarRefs(Exprs);
11981 Exprs.clear();
11982 for (unsigned i = 0; i != NumVars; ++i)
11983 Exprs.push_back(Elt: Record.readSubExpr());
11984 C->setSourceExprs(Exprs);
11985 Exprs.clear();
11986 for (unsigned i = 0; i != NumVars; ++i)
11987 Exprs.push_back(Elt: Record.readSubExpr());
11988 C->setDestinationExprs(Exprs);
11989 Exprs.clear();
11990 for (unsigned i = 0; i != NumVars; ++i)
11991 Exprs.push_back(Elt: Record.readSubExpr());
11992 C->setAssignmentOps(Exprs);
11993}
11994
11995void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
11996 C->setLParenLoc(Record.readSourceLocation());
11997 unsigned NumVars = C->varlist_size();
11998 SmallVector<Expr *, 16> Exprs;
11999 Exprs.reserve(N: NumVars);
12000 for (unsigned i = 0; i != NumVars; ++i)
12001 Exprs.push_back(Elt: Record.readSubExpr());
12002 C->setVarRefs(Exprs);
12003 Exprs.clear();
12004 for (unsigned i = 0; i != NumVars; ++i)
12005 Exprs.push_back(Elt: Record.readSubExpr());
12006 C->setSourceExprs(Exprs);
12007 Exprs.clear();
12008 for (unsigned i = 0; i != NumVars; ++i)
12009 Exprs.push_back(Elt: Record.readSubExpr());
12010 C->setDestinationExprs(Exprs);
12011 Exprs.clear();
12012 for (unsigned i = 0; i != NumVars; ++i)
12013 Exprs.push_back(Elt: Record.readSubExpr());
12014 C->setAssignmentOps(Exprs);
12015}
12016
12017void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12018 C->setLParenLoc(Record.readSourceLocation());
12019 unsigned NumVars = C->varlist_size();
12020 SmallVector<Expr *, 16> Vars;
12021 Vars.reserve(N: NumVars);
12022 for (unsigned i = 0; i != NumVars; ++i)
12023 Vars.push_back(Elt: Record.readSubExpr());
12024 C->setVarRefs(Vars);
12025}
12026
12027void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12028 C->setDepobj(Record.readSubExpr());
12029 C->setLParenLoc(Record.readSourceLocation());
12030}
12031
12032void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12033 C->setLParenLoc(Record.readSourceLocation());
12034 C->setModifier(Record.readSubExpr());
12035 C->setDependencyKind(
12036 static_cast<OpenMPDependClauseKind>(Record.readInt()));
12037 C->setDependencyLoc(Record.readSourceLocation());
12038 C->setColonLoc(Record.readSourceLocation());
12039 C->setOmpAllMemoryLoc(Record.readSourceLocation());
12040 unsigned NumVars = C->varlist_size();
12041 SmallVector<Expr *, 16> Vars;
12042 Vars.reserve(N: NumVars);
12043 for (unsigned I = 0; I != NumVars; ++I)
12044 Vars.push_back(Elt: Record.readSubExpr());
12045 C->setVarRefs(Vars);
12046 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12047 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12048}
12049
12050void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12051 VisitOMPClauseWithPreInit(C);
12052 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12053 C->setDevice(Record.readSubExpr());
12054 C->setModifierLoc(Record.readSourceLocation());
12055 C->setLParenLoc(Record.readSourceLocation());
12056}
12057
12058void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12059 C->setLParenLoc(Record.readSourceLocation());
12060 bool HasIteratorModifier = false;
12061 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12062 C->setMapTypeModifier(
12063 I, T: static_cast<OpenMPMapModifierKind>(Record.readInt()));
12064 C->setMapTypeModifierLoc(I, TLoc: Record.readSourceLocation());
12065 if (C->getMapTypeModifier(Cnt: I) == OMPC_MAP_MODIFIER_iterator)
12066 HasIteratorModifier = true;
12067 }
12068 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12069 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12070 C->setMapType(
12071 static_cast<OpenMPMapClauseKind>(Record.readInt()));
12072 C->setMapLoc(Record.readSourceLocation());
12073 C->setColonLoc(Record.readSourceLocation());
12074 auto NumVars = C->varlist_size();
12075 auto UniqueDecls = C->getUniqueDeclarationsNum();
12076 auto TotalLists = C->getTotalComponentListNum();
12077 auto TotalComponents = C->getTotalComponentsNum();
12078
12079 SmallVector<Expr *, 16> Vars;
12080 Vars.reserve(N: NumVars);
12081 for (unsigned i = 0; i != NumVars; ++i)
12082 Vars.push_back(Elt: Record.readExpr());
12083 C->setVarRefs(Vars);
12084
12085 SmallVector<Expr *, 16> UDMappers;
12086 UDMappers.reserve(N: NumVars);
12087 for (unsigned I = 0; I < NumVars; ++I)
12088 UDMappers.push_back(Elt: Record.readExpr());
12089 C->setUDMapperRefs(UDMappers);
12090
12091 if (HasIteratorModifier)
12092 C->setIteratorModifier(Record.readExpr());
12093
12094 SmallVector<ValueDecl *, 16> Decls;
12095 Decls.reserve(N: UniqueDecls);
12096 for (unsigned i = 0; i < UniqueDecls; ++i)
12097 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12098 C->setUniqueDecls(Decls);
12099
12100 SmallVector<unsigned, 16> ListsPerDecl;
12101 ListsPerDecl.reserve(N: UniqueDecls);
12102 for (unsigned i = 0; i < UniqueDecls; ++i)
12103 ListsPerDecl.push_back(Elt: Record.readInt());
12104 C->setDeclNumLists(ListsPerDecl);
12105
12106 SmallVector<unsigned, 32> ListSizes;
12107 ListSizes.reserve(N: TotalLists);
12108 for (unsigned i = 0; i < TotalLists; ++i)
12109 ListSizes.push_back(Elt: Record.readInt());
12110 C->setComponentListSizes(ListSizes);
12111
12112 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12113 Components.reserve(N: TotalComponents);
12114 for (unsigned i = 0; i < TotalComponents; ++i) {
12115 Expr *AssociatedExprPr = Record.readExpr();
12116 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12117 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12118 /*IsNonContiguous=*/Args: false);
12119 }
12120 C->setComponents(Components, CLSs: ListSizes);
12121}
12122
12123void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12124 C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12125 C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());
12126 C->setLParenLoc(Record.readSourceLocation());
12127 C->setColonLoc(Record.readSourceLocation());
12128 C->setAllocator(Record.readSubExpr());
12129 C->setAlignment(Record.readSubExpr());
12130 unsigned NumVars = C->varlist_size();
12131 SmallVector<Expr *, 16> Vars;
12132 Vars.reserve(N: NumVars);
12133 for (unsigned i = 0; i != NumVars; ++i)
12134 Vars.push_back(Elt: Record.readSubExpr());
12135 C->setVarRefs(Vars);
12136}
12137
12138void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12139 VisitOMPClauseWithPreInit(C);
12140 C->setLParenLoc(Record.readSourceLocation());
12141 unsigned NumVars = C->varlist_size();
12142 SmallVector<Expr *, 16> Vars;
12143 Vars.reserve(N: NumVars);
12144 for (unsigned I = 0; I != NumVars; ++I)
12145 Vars.push_back(Elt: Record.readSubExpr());
12146 C->setVarRefs(Vars);
12147}
12148
12149void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12150 VisitOMPClauseWithPreInit(C);
12151 C->setLParenLoc(Record.readSourceLocation());
12152 unsigned NumVars = C->varlist_size();
12153 SmallVector<Expr *, 16> Vars;
12154 Vars.reserve(N: NumVars);
12155 for (unsigned I = 0; I != NumVars; ++I)
12156 Vars.push_back(Elt: Record.readSubExpr());
12157 C->setVarRefs(Vars);
12158}
12159
12160void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12161 VisitOMPClauseWithPreInit(C);
12162 C->setPriority(Record.readSubExpr());
12163 C->setLParenLoc(Record.readSourceLocation());
12164}
12165
12166void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12167 VisitOMPClauseWithPreInit(C);
12168 C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());
12169 C->setGrainsize(Record.readSubExpr());
12170 C->setModifierLoc(Record.readSourceLocation());
12171 C->setLParenLoc(Record.readSourceLocation());
12172}
12173
12174void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12175 VisitOMPClauseWithPreInit(C);
12176 C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());
12177 C->setNumTasks(Record.readSubExpr());
12178 C->setModifierLoc(Record.readSourceLocation());
12179 C->setLParenLoc(Record.readSourceLocation());
12180}
12181
12182void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12183 C->setHint(Record.readSubExpr());
12184 C->setLParenLoc(Record.readSourceLocation());
12185}
12186
12187void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12188 VisitOMPClauseWithPreInit(C);
12189 C->setDistScheduleKind(
12190 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12191 C->setChunkSize(Record.readSubExpr());
12192 C->setLParenLoc(Record.readSourceLocation());
12193 C->setDistScheduleKindLoc(Record.readSourceLocation());
12194 C->setCommaLoc(Record.readSourceLocation());
12195}
12196
12197void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12198 C->setDefaultmapKind(
12199 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12200 C->setDefaultmapModifier(
12201 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12202 C->setLParenLoc(Record.readSourceLocation());
12203 C->setDefaultmapModifierLoc(Record.readSourceLocation());
12204 C->setDefaultmapKindLoc(Record.readSourceLocation());
12205}
12206
12207void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12208 C->setLParenLoc(Record.readSourceLocation());
12209 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12210 C->setMotionModifier(
12211 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12212 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12213 }
12214 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12215 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12216 C->setColonLoc(Record.readSourceLocation());
12217 auto NumVars = C->varlist_size();
12218 auto UniqueDecls = C->getUniqueDeclarationsNum();
12219 auto TotalLists = C->getTotalComponentListNum();
12220 auto TotalComponents = C->getTotalComponentsNum();
12221
12222 SmallVector<Expr *, 16> Vars;
12223 Vars.reserve(N: NumVars);
12224 for (unsigned i = 0; i != NumVars; ++i)
12225 Vars.push_back(Elt: Record.readSubExpr());
12226 C->setVarRefs(Vars);
12227
12228 SmallVector<Expr *, 16> UDMappers;
12229 UDMappers.reserve(N: NumVars);
12230 for (unsigned I = 0; I < NumVars; ++I)
12231 UDMappers.push_back(Elt: Record.readSubExpr());
12232 C->setUDMapperRefs(UDMappers);
12233
12234 SmallVector<ValueDecl *, 16> Decls;
12235 Decls.reserve(N: UniqueDecls);
12236 for (unsigned i = 0; i < UniqueDecls; ++i)
12237 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12238 C->setUniqueDecls(Decls);
12239
12240 SmallVector<unsigned, 16> ListsPerDecl;
12241 ListsPerDecl.reserve(N: UniqueDecls);
12242 for (unsigned i = 0; i < UniqueDecls; ++i)
12243 ListsPerDecl.push_back(Elt: Record.readInt());
12244 C->setDeclNumLists(ListsPerDecl);
12245
12246 SmallVector<unsigned, 32> ListSizes;
12247 ListSizes.reserve(N: TotalLists);
12248 for (unsigned i = 0; i < TotalLists; ++i)
12249 ListSizes.push_back(Elt: Record.readInt());
12250 C->setComponentListSizes(ListSizes);
12251
12252 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12253 Components.reserve(N: TotalComponents);
12254 for (unsigned i = 0; i < TotalComponents; ++i) {
12255 Expr *AssociatedExprPr = Record.readSubExpr();
12256 bool IsNonContiguous = Record.readBool();
12257 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12258 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12259 }
12260 C->setComponents(Components, CLSs: ListSizes);
12261}
12262
12263void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12264 C->setLParenLoc(Record.readSourceLocation());
12265 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12266 C->setMotionModifier(
12267 I, T: static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12268 C->setMotionModifierLoc(I, TLoc: Record.readSourceLocation());
12269 }
12270 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12271 C->setMapperIdInfo(Record.readDeclarationNameInfo());
12272 C->setColonLoc(Record.readSourceLocation());
12273 auto NumVars = C->varlist_size();
12274 auto UniqueDecls = C->getUniqueDeclarationsNum();
12275 auto TotalLists = C->getTotalComponentListNum();
12276 auto TotalComponents = C->getTotalComponentsNum();
12277
12278 SmallVector<Expr *, 16> Vars;
12279 Vars.reserve(N: NumVars);
12280 for (unsigned i = 0; i != NumVars; ++i)
12281 Vars.push_back(Elt: Record.readSubExpr());
12282 C->setVarRefs(Vars);
12283
12284 SmallVector<Expr *, 16> UDMappers;
12285 UDMappers.reserve(N: NumVars);
12286 for (unsigned I = 0; I < NumVars; ++I)
12287 UDMappers.push_back(Elt: Record.readSubExpr());
12288 C->setUDMapperRefs(UDMappers);
12289
12290 SmallVector<ValueDecl *, 16> Decls;
12291 Decls.reserve(N: UniqueDecls);
12292 for (unsigned i = 0; i < UniqueDecls; ++i)
12293 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12294 C->setUniqueDecls(Decls);
12295
12296 SmallVector<unsigned, 16> ListsPerDecl;
12297 ListsPerDecl.reserve(N: UniqueDecls);
12298 for (unsigned i = 0; i < UniqueDecls; ++i)
12299 ListsPerDecl.push_back(Elt: Record.readInt());
12300 C->setDeclNumLists(ListsPerDecl);
12301
12302 SmallVector<unsigned, 32> ListSizes;
12303 ListSizes.reserve(N: TotalLists);
12304 for (unsigned i = 0; i < TotalLists; ++i)
12305 ListSizes.push_back(Elt: Record.readInt());
12306 C->setComponentListSizes(ListSizes);
12307
12308 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12309 Components.reserve(N: TotalComponents);
12310 for (unsigned i = 0; i < TotalComponents; ++i) {
12311 Expr *AssociatedExprPr = Record.readSubExpr();
12312 bool IsNonContiguous = Record.readBool();
12313 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12314 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl, Args&: IsNonContiguous);
12315 }
12316 C->setComponents(Components, CLSs: ListSizes);
12317}
12318
12319void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12320 C->setLParenLoc(Record.readSourceLocation());
12321 auto NumVars = C->varlist_size();
12322 auto UniqueDecls = C->getUniqueDeclarationsNum();
12323 auto TotalLists = C->getTotalComponentListNum();
12324 auto TotalComponents = C->getTotalComponentsNum();
12325
12326 SmallVector<Expr *, 16> Vars;
12327 Vars.reserve(N: NumVars);
12328 for (unsigned i = 0; i != NumVars; ++i)
12329 Vars.push_back(Elt: Record.readSubExpr());
12330 C->setVarRefs(Vars);
12331 Vars.clear();
12332 for (unsigned i = 0; i != NumVars; ++i)
12333 Vars.push_back(Elt: Record.readSubExpr());
12334 C->setPrivateCopies(Vars);
12335 Vars.clear();
12336 for (unsigned i = 0; i != NumVars; ++i)
12337 Vars.push_back(Elt: Record.readSubExpr());
12338 C->setInits(Vars);
12339
12340 SmallVector<ValueDecl *, 16> Decls;
12341 Decls.reserve(N: UniqueDecls);
12342 for (unsigned i = 0; i < UniqueDecls; ++i)
12343 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12344 C->setUniqueDecls(Decls);
12345
12346 SmallVector<unsigned, 16> ListsPerDecl;
12347 ListsPerDecl.reserve(N: UniqueDecls);
12348 for (unsigned i = 0; i < UniqueDecls; ++i)
12349 ListsPerDecl.push_back(Elt: Record.readInt());
12350 C->setDeclNumLists(ListsPerDecl);
12351
12352 SmallVector<unsigned, 32> ListSizes;
12353 ListSizes.reserve(N: TotalLists);
12354 for (unsigned i = 0; i < TotalLists; ++i)
12355 ListSizes.push_back(Elt: Record.readInt());
12356 C->setComponentListSizes(ListSizes);
12357
12358 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12359 Components.reserve(N: TotalComponents);
12360 for (unsigned i = 0; i < TotalComponents; ++i) {
12361 auto *AssociatedExprPr = Record.readSubExpr();
12362 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12363 Components.emplace_back(Args&: AssociatedExprPr, Args&: AssociatedDecl,
12364 /*IsNonContiguous=*/Args: false);
12365 }
12366 C->setComponents(Components, CLSs: ListSizes);
12367}
12368
12369void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12370 C->setLParenLoc(Record.readSourceLocation());
12371 auto NumVars = C->varlist_size();
12372 auto UniqueDecls = C->getUniqueDeclarationsNum();
12373 auto TotalLists = C->getTotalComponentListNum();
12374 auto TotalComponents = C->getTotalComponentsNum();
12375
12376 SmallVector<Expr *, 16> Vars;
12377 Vars.reserve(N: NumVars);
12378 for (unsigned i = 0; i != NumVars; ++i)
12379 Vars.push_back(Elt: Record.readSubExpr());
12380 C->setVarRefs(Vars);
12381
12382 SmallVector<ValueDecl *, 16> Decls;
12383 Decls.reserve(N: UniqueDecls);
12384 for (unsigned i = 0; i < UniqueDecls; ++i)
12385 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12386 C->setUniqueDecls(Decls);
12387
12388 SmallVector<unsigned, 16> ListsPerDecl;
12389 ListsPerDecl.reserve(N: UniqueDecls);
12390 for (unsigned i = 0; i < UniqueDecls; ++i)
12391 ListsPerDecl.push_back(Elt: Record.readInt());
12392 C->setDeclNumLists(ListsPerDecl);
12393
12394 SmallVector<unsigned, 32> ListSizes;
12395 ListSizes.reserve(N: TotalLists);
12396 for (unsigned i = 0; i < TotalLists; ++i)
12397 ListSizes.push_back(Elt: Record.readInt());
12398 C->setComponentListSizes(ListSizes);
12399
12400 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12401 Components.reserve(N: TotalComponents);
12402 for (unsigned i = 0; i < TotalComponents; ++i) {
12403 Expr *AssociatedExpr = Record.readSubExpr();
12404 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12405 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12406 /*IsNonContiguous*/ Args: false);
12407 }
12408 C->setComponents(Components, CLSs: ListSizes);
12409}
12410
12411void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12412 C->setLParenLoc(Record.readSourceLocation());
12413 auto NumVars = C->varlist_size();
12414 auto UniqueDecls = C->getUniqueDeclarationsNum();
12415 auto TotalLists = C->getTotalComponentListNum();
12416 auto TotalComponents = C->getTotalComponentsNum();
12417
12418 SmallVector<Expr *, 16> Vars;
12419 Vars.reserve(N: NumVars);
12420 for (unsigned i = 0; i != NumVars; ++i)
12421 Vars.push_back(Elt: Record.readSubExpr());
12422 C->setVarRefs(Vars);
12423 Vars.clear();
12424
12425 SmallVector<ValueDecl *, 16> Decls;
12426 Decls.reserve(N: UniqueDecls);
12427 for (unsigned i = 0; i < UniqueDecls; ++i)
12428 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12429 C->setUniqueDecls(Decls);
12430
12431 SmallVector<unsigned, 16> ListsPerDecl;
12432 ListsPerDecl.reserve(N: UniqueDecls);
12433 for (unsigned i = 0; i < UniqueDecls; ++i)
12434 ListsPerDecl.push_back(Elt: Record.readInt());
12435 C->setDeclNumLists(ListsPerDecl);
12436
12437 SmallVector<unsigned, 32> ListSizes;
12438 ListSizes.reserve(N: TotalLists);
12439 for (unsigned i = 0; i < TotalLists; ++i)
12440 ListSizes.push_back(Elt: Record.readInt());
12441 C->setComponentListSizes(ListSizes);
12442
12443 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12444 Components.reserve(N: TotalComponents);
12445 for (unsigned i = 0; i < TotalComponents; ++i) {
12446 Expr *AssociatedExpr = Record.readSubExpr();
12447 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12448 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12449 /*IsNonContiguous=*/Args: false);
12450 }
12451 C->setComponents(Components, CLSs: ListSizes);
12452}
12453
12454void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {
12455 C->setLParenLoc(Record.readSourceLocation());
12456 auto NumVars = C->varlist_size();
12457 auto UniqueDecls = C->getUniqueDeclarationsNum();
12458 auto TotalLists = C->getTotalComponentListNum();
12459 auto TotalComponents = C->getTotalComponentsNum();
12460
12461 SmallVector<Expr *, 16> Vars;
12462 Vars.reserve(N: NumVars);
12463 for (unsigned I = 0; I != NumVars; ++I)
12464 Vars.push_back(Elt: Record.readSubExpr());
12465 C->setVarRefs(Vars);
12466 Vars.clear();
12467
12468 SmallVector<ValueDecl *, 16> Decls;
12469 Decls.reserve(N: UniqueDecls);
12470 for (unsigned I = 0; I < UniqueDecls; ++I)
12471 Decls.push_back(Elt: Record.readDeclAs<ValueDecl>());
12472 C->setUniqueDecls(Decls);
12473
12474 SmallVector<unsigned, 16> ListsPerDecl;
12475 ListsPerDecl.reserve(N: UniqueDecls);
12476 for (unsigned I = 0; I < UniqueDecls; ++I)
12477 ListsPerDecl.push_back(Elt: Record.readInt());
12478 C->setDeclNumLists(ListsPerDecl);
12479
12480 SmallVector<unsigned, 32> ListSizes;
12481 ListSizes.reserve(N: TotalLists);
12482 for (unsigned i = 0; i < TotalLists; ++i)
12483 ListSizes.push_back(Elt: Record.readInt());
12484 C->setComponentListSizes(ListSizes);
12485
12486 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12487 Components.reserve(N: TotalComponents);
12488 for (unsigned I = 0; I < TotalComponents; ++I) {
12489 Expr *AssociatedExpr = Record.readSubExpr();
12490 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12491 Components.emplace_back(Args&: AssociatedExpr, Args&: AssociatedDecl,
12492 /*IsNonContiguous=*/Args: false);
12493 }
12494 C->setComponents(Components, CLSs: ListSizes);
12495}
12496
12497void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12498 C->setLParenLoc(Record.readSourceLocation());
12499 unsigned NumVars = C->varlist_size();
12500 SmallVector<Expr *, 16> Vars;
12501 Vars.reserve(N: NumVars);
12502 for (unsigned i = 0; i != NumVars; ++i)
12503 Vars.push_back(Elt: Record.readSubExpr());
12504 C->setVarRefs(Vars);
12505 Vars.clear();
12506 Vars.reserve(N: NumVars);
12507 for (unsigned i = 0; i != NumVars; ++i)
12508 Vars.push_back(Elt: Record.readSubExpr());
12509 C->setPrivateRefs(Vars);
12510}
12511
12512void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12513 C->setLParenLoc(Record.readSourceLocation());
12514 unsigned NumVars = C->varlist_size();
12515 SmallVector<Expr *, 16> Vars;
12516 Vars.reserve(N: NumVars);
12517 for (unsigned i = 0; i != NumVars; ++i)
12518 Vars.push_back(Elt: Record.readSubExpr());
12519 C->setVarRefs(Vars);
12520}
12521
12522void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12523 C->setLParenLoc(Record.readSourceLocation());
12524 unsigned NumVars = C->varlist_size();
12525 SmallVector<Expr *, 16> Vars;
12526 Vars.reserve(N: NumVars);
12527 for (unsigned i = 0; i != NumVars; ++i)
12528 Vars.push_back(Elt: Record.readSubExpr());
12529 C->setVarRefs(Vars);
12530}
12531
12532void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12533 C->setLParenLoc(Record.readSourceLocation());
12534 unsigned NumOfAllocators = C->getNumberOfAllocators();
12535 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12536 Data.reserve(N: NumOfAllocators);
12537 for (unsigned I = 0; I != NumOfAllocators; ++I) {
12538 OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12539 D.Allocator = Record.readSubExpr();
12540 D.AllocatorTraits = Record.readSubExpr();
12541 D.LParenLoc = Record.readSourceLocation();
12542 D.RParenLoc = Record.readSourceLocation();
12543 }
12544 C->setAllocatorsData(Data);
12545}
12546
12547void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12548 C->setLParenLoc(Record.readSourceLocation());
12549 C->setModifier(Record.readSubExpr());
12550 C->setColonLoc(Record.readSourceLocation());
12551 unsigned NumOfLocators = C->varlist_size();
12552 SmallVector<Expr *, 4> Locators;
12553 Locators.reserve(N: NumOfLocators);
12554 for (unsigned I = 0; I != NumOfLocators; ++I)
12555 Locators.push_back(Elt: Record.readSubExpr());
12556 C->setVarRefs(Locators);
12557}
12558
12559void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12560 C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12561 C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());
12562 C->setLParenLoc(Record.readSourceLocation());
12563 C->setKindKwLoc(Record.readSourceLocation());
12564 C->setModifierKwLoc(Record.readSourceLocation());
12565}
12566
12567void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12568 VisitOMPClauseWithPreInit(C);
12569 C->setThreadID(Record.readSubExpr());
12570 C->setLParenLoc(Record.readSourceLocation());
12571}
12572
12573void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12574 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12575 C->setLParenLoc(Record.readSourceLocation());
12576 C->setBindKindLoc(Record.readSourceLocation());
12577}
12578
12579void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12580 C->setAlignment(Record.readExpr());
12581 C->setLParenLoc(Record.readSourceLocation());
12582}
12583
12584void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {
12585 VisitOMPClauseWithPreInit(C);
12586 C->setSize(Record.readSubExpr());
12587 C->setLParenLoc(Record.readSourceLocation());
12588}
12589
12590void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {
12591 C->setLParenLoc(Record.readSourceLocation());
12592 C->setDependenceType(
12593 static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));
12594 C->setDependenceLoc(Record.readSourceLocation());
12595 C->setColonLoc(Record.readSourceLocation());
12596 unsigned NumVars = C->varlist_size();
12597 SmallVector<Expr *, 16> Vars;
12598 Vars.reserve(N: NumVars);
12599 for (unsigned I = 0; I != NumVars; ++I)
12600 Vars.push_back(Elt: Record.readSubExpr());
12601 C->setVarRefs(Vars);
12602 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12603 C->setLoopData(NumLoop: I, Cnt: Record.readSubExpr());
12604}
12605
12606void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {
12607 AttrVec Attrs;
12608 Record.readAttributes(Attrs);
12609 C->setAttrs(Attrs);
12610 C->setLocStart(Record.readSourceLocation());
12611 C->setLParenLoc(Record.readSourceLocation());
12612 C->setLocEnd(Record.readSourceLocation());
12613}
12614
12615void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}
12616
12617OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12618 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12619 TI.Sets.resize(N: readUInt32());
12620 for (auto &Set : TI.Sets) {
12621 Set.Kind = readEnum<llvm::omp::TraitSet>();
12622 Set.Selectors.resize(N: readUInt32());
12623 for (auto &Selector : Set.Selectors) {
12624 Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12625 Selector.ScoreOrCondition = nullptr;
12626 if (readBool())
12627 Selector.ScoreOrCondition = readExprRef();
12628 Selector.Properties.resize(N: readUInt32());
12629 for (auto &Property : Selector.Properties)
12630 Property.Kind = readEnum<llvm::omp::TraitProperty>();
12631 }
12632 }
12633 return &TI;
12634}
12635
12636void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12637 if (!Data)
12638 return;
12639 if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12640 // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12641 skipInts(N: 3);
12642 }
12643 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12644 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12645 Clauses[I] = readOMPClause();
12646 Data->setClauses(Clauses);
12647 if (Data->hasAssociatedStmt())
12648 Data->setAssociatedStmt(readStmt());
12649 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12650 Data->getChildren()[I] = readStmt();
12651}
12652
12653SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {
12654 unsigned NumVars = readInt();
12655 llvm::SmallVector<Expr *> VarList;
12656 for (unsigned I = 0; I < NumVars; ++I)
12657 VarList.push_back(Elt: readExpr());
12658 return VarList;
12659}
12660
12661SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {
12662 unsigned NumExprs = readInt();
12663 llvm::SmallVector<Expr *> ExprList;
12664 for (unsigned I = 0; I < NumExprs; ++I)
12665 ExprList.push_back(Elt: readSubExpr());
12666 return ExprList;
12667}
12668
12669OpenACCClause *ASTRecordReader::readOpenACCClause() {
12670 OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();
12671 SourceLocation BeginLoc = readSourceLocation();
12672 SourceLocation EndLoc = readSourceLocation();
12673
12674 switch (ClauseKind) {
12675 case OpenACCClauseKind::Default: {
12676 SourceLocation LParenLoc = readSourceLocation();
12677 OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();
12678 return OpenACCDefaultClause::Create(C: getContext(), K: DCK, BeginLoc, LParenLoc,
12679 EndLoc);
12680 }
12681 case OpenACCClauseKind::If: {
12682 SourceLocation LParenLoc = readSourceLocation();
12683 Expr *CondExpr = readSubExpr();
12684 return OpenACCIfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: CondExpr,
12685 EndLoc);
12686 }
12687 case OpenACCClauseKind::Self: {
12688 SourceLocation LParenLoc = readSourceLocation();
12689 bool isConditionExprClause = readBool();
12690 if (isConditionExprClause) {
12691 Expr *CondExpr = readBool() ? readSubExpr() : nullptr;
12692 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc,
12693 ConditionExpr: CondExpr, EndLoc);
12694 }
12695 unsigned NumVars = readInt();
12696 llvm::SmallVector<Expr *> VarList;
12697 for (unsigned I = 0; I < NumVars; ++I)
12698 VarList.push_back(Elt: readSubExpr());
12699 return OpenACCSelfClause::Create(C: getContext(), BeginLoc, LParenLoc, ConditionExpr: VarList,
12700 EndLoc);
12701 }
12702 case OpenACCClauseKind::NumGangs: {
12703 SourceLocation LParenLoc = readSourceLocation();
12704 unsigned NumClauses = readInt();
12705 llvm::SmallVector<Expr *> IntExprs;
12706 for (unsigned I = 0; I < NumClauses; ++I)
12707 IntExprs.push_back(Elt: readSubExpr());
12708 return OpenACCNumGangsClause::Create(C: getContext(), BeginLoc, LParenLoc,
12709 IntExprs, EndLoc);
12710 }
12711 case OpenACCClauseKind::NumWorkers: {
12712 SourceLocation LParenLoc = readSourceLocation();
12713 Expr *IntExpr = readSubExpr();
12714 return OpenACCNumWorkersClause::Create(C: getContext(), BeginLoc, LParenLoc,
12715 IntExpr, EndLoc);
12716 }
12717 case OpenACCClauseKind::DeviceNum: {
12718 SourceLocation LParenLoc = readSourceLocation();
12719 Expr *IntExpr = readSubExpr();
12720 return OpenACCDeviceNumClause::Create(C: getContext(), BeginLoc, LParenLoc,
12721 IntExpr, EndLoc);
12722 }
12723 case OpenACCClauseKind::DefaultAsync: {
12724 SourceLocation LParenLoc = readSourceLocation();
12725 Expr *IntExpr = readSubExpr();
12726 return OpenACCDefaultAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
12727 IntExpr, EndLoc);
12728 }
12729 case OpenACCClauseKind::VectorLength: {
12730 SourceLocation LParenLoc = readSourceLocation();
12731 Expr *IntExpr = readSubExpr();
12732 return OpenACCVectorLengthClause::Create(C: getContext(), BeginLoc, LParenLoc,
12733 IntExpr, EndLoc);
12734 }
12735 case OpenACCClauseKind::Private: {
12736 SourceLocation LParenLoc = readSourceLocation();
12737 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12738 return OpenACCPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12739 VarList, EndLoc);
12740 }
12741 case OpenACCClauseKind::Host: {
12742 SourceLocation LParenLoc = readSourceLocation();
12743 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12744 return OpenACCHostClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
12745 EndLoc);
12746 }
12747 case OpenACCClauseKind::Device: {
12748 SourceLocation LParenLoc = readSourceLocation();
12749 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12750 return OpenACCDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
12751 VarList, EndLoc);
12752 }
12753 case OpenACCClauseKind::FirstPrivate: {
12754 SourceLocation LParenLoc = readSourceLocation();
12755 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12756 return OpenACCFirstPrivateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12757 VarList, EndLoc);
12758 }
12759 case OpenACCClauseKind::Attach: {
12760 SourceLocation LParenLoc = readSourceLocation();
12761 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12762 return OpenACCAttachClause::Create(C: getContext(), BeginLoc, LParenLoc,
12763 VarList, EndLoc);
12764 }
12765 case OpenACCClauseKind::Detach: {
12766 SourceLocation LParenLoc = readSourceLocation();
12767 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12768 return OpenACCDetachClause::Create(C: getContext(), BeginLoc, LParenLoc,
12769 VarList, EndLoc);
12770 }
12771 case OpenACCClauseKind::Delete: {
12772 SourceLocation LParenLoc = readSourceLocation();
12773 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12774 return OpenACCDeleteClause::Create(C: getContext(), BeginLoc, LParenLoc,
12775 VarList, EndLoc);
12776 }
12777 case OpenACCClauseKind::UseDevice: {
12778 SourceLocation LParenLoc = readSourceLocation();
12779 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12780 return OpenACCUseDeviceClause::Create(C: getContext(), BeginLoc, LParenLoc,
12781 VarList, EndLoc);
12782 }
12783 case OpenACCClauseKind::DevicePtr: {
12784 SourceLocation LParenLoc = readSourceLocation();
12785 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12786 return OpenACCDevicePtrClause::Create(C: getContext(), BeginLoc, LParenLoc,
12787 VarList, EndLoc);
12788 }
12789 case OpenACCClauseKind::NoCreate: {
12790 SourceLocation LParenLoc = readSourceLocation();
12791 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12792 return OpenACCNoCreateClause::Create(C: getContext(), BeginLoc, LParenLoc,
12793 VarList, EndLoc);
12794 }
12795 case OpenACCClauseKind::Present: {
12796 SourceLocation LParenLoc = readSourceLocation();
12797 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12798 return OpenACCPresentClause::Create(C: getContext(), BeginLoc, LParenLoc,
12799 VarList, EndLoc);
12800 }
12801 case OpenACCClauseKind::PCopy:
12802 case OpenACCClauseKind::PresentOrCopy:
12803 case OpenACCClauseKind::Copy: {
12804 SourceLocation LParenLoc = readSourceLocation();
12805 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12806 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12807 return OpenACCCopyClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12808 LParenLoc, Mods: ModList, VarList, EndLoc);
12809 }
12810 case OpenACCClauseKind::CopyIn:
12811 case OpenACCClauseKind::PCopyIn:
12812 case OpenACCClauseKind::PresentOrCopyIn: {
12813 SourceLocation LParenLoc = readSourceLocation();
12814 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12815 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12816 return OpenACCCopyInClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12817 LParenLoc, Mods: ModList, VarList, EndLoc);
12818 }
12819 case OpenACCClauseKind::CopyOut:
12820 case OpenACCClauseKind::PCopyOut:
12821 case OpenACCClauseKind::PresentOrCopyOut: {
12822 SourceLocation LParenLoc = readSourceLocation();
12823 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12824 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12825 return OpenACCCopyOutClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12826 LParenLoc, Mods: ModList, VarList, EndLoc);
12827 }
12828 case OpenACCClauseKind::Create:
12829 case OpenACCClauseKind::PCreate:
12830 case OpenACCClauseKind::PresentOrCreate: {
12831 SourceLocation LParenLoc = readSourceLocation();
12832 OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();
12833 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12834 return OpenACCCreateClause::Create(C: getContext(), Spelling: ClauseKind, BeginLoc,
12835 LParenLoc, Mods: ModList, VarList, EndLoc);
12836 }
12837 case OpenACCClauseKind::Async: {
12838 SourceLocation LParenLoc = readSourceLocation();
12839 Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;
12840 return OpenACCAsyncClause::Create(C: getContext(), BeginLoc, LParenLoc,
12841 IntExpr: AsyncExpr, EndLoc);
12842 }
12843 case OpenACCClauseKind::Wait: {
12844 SourceLocation LParenLoc = readSourceLocation();
12845 Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;
12846 SourceLocation QueuesLoc = readSourceLocation();
12847 llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();
12848 return OpenACCWaitClause::Create(C: getContext(), BeginLoc, LParenLoc,
12849 DevNumExpr, QueuesLoc, QueueIdExprs,
12850 EndLoc);
12851 }
12852 case OpenACCClauseKind::DeviceType:
12853 case OpenACCClauseKind::DType: {
12854 SourceLocation LParenLoc = readSourceLocation();
12855 llvm::SmallVector<DeviceTypeArgument> Archs;
12856 unsigned NumArchs = readInt();
12857
12858 for (unsigned I = 0; I < NumArchs; ++I) {
12859 IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;
12860 SourceLocation Loc = readSourceLocation();
12861 Archs.emplace_back(Args&: Loc, Args&: Ident);
12862 }
12863
12864 return OpenACCDeviceTypeClause::Create(C: getContext(), K: ClauseKind, BeginLoc,
12865 LParenLoc, Archs, EndLoc);
12866 }
12867 case OpenACCClauseKind::Reduction: {
12868 SourceLocation LParenLoc = readSourceLocation();
12869 OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
12870 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12871 return OpenACCReductionClause::Create(C: getContext(), BeginLoc, LParenLoc, Operator: Op,
12872 VarList, EndLoc);
12873 }
12874 case OpenACCClauseKind::Seq:
12875 return OpenACCSeqClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12876 case OpenACCClauseKind::NoHost:
12877 return OpenACCNoHostClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12878 case OpenACCClauseKind::Finalize:
12879 return OpenACCFinalizeClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12880 case OpenACCClauseKind::IfPresent:
12881 return OpenACCIfPresentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12882 case OpenACCClauseKind::Independent:
12883 return OpenACCIndependentClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12884 case OpenACCClauseKind::Auto:
12885 return OpenACCAutoClause::Create(Ctx: getContext(), BeginLoc, EndLoc);
12886 case OpenACCClauseKind::Collapse: {
12887 SourceLocation LParenLoc = readSourceLocation();
12888 bool HasForce = readBool();
12889 Expr *LoopCount = readSubExpr();
12890 return OpenACCCollapseClause::Create(C: getContext(), BeginLoc, LParenLoc,
12891 HasForce, LoopCount, EndLoc);
12892 }
12893 case OpenACCClauseKind::Tile: {
12894 SourceLocation LParenLoc = readSourceLocation();
12895 unsigned NumClauses = readInt();
12896 llvm::SmallVector<Expr *> SizeExprs;
12897 for (unsigned I = 0; I < NumClauses; ++I)
12898 SizeExprs.push_back(Elt: readSubExpr());
12899 return OpenACCTileClause::Create(C: getContext(), BeginLoc, LParenLoc,
12900 SizeExprs, EndLoc);
12901 }
12902 case OpenACCClauseKind::Gang: {
12903 SourceLocation LParenLoc = readSourceLocation();
12904 unsigned NumExprs = readInt();
12905 llvm::SmallVector<OpenACCGangKind> GangKinds;
12906 llvm::SmallVector<Expr *> Exprs;
12907 for (unsigned I = 0; I < NumExprs; ++I) {
12908 GangKinds.push_back(Elt: readEnum<OpenACCGangKind>());
12909 // Can't use `readSubExpr` because this is usable from a 'decl' construct.
12910 Exprs.push_back(Elt: readExpr());
12911 }
12912 return OpenACCGangClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12913 GangKinds, IntExprs: Exprs, EndLoc);
12914 }
12915 case OpenACCClauseKind::Worker: {
12916 SourceLocation LParenLoc = readSourceLocation();
12917 Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;
12918 return OpenACCWorkerClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12919 IntExpr: WorkerExpr, EndLoc);
12920 }
12921 case OpenACCClauseKind::Vector: {
12922 SourceLocation LParenLoc = readSourceLocation();
12923 Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;
12924 return OpenACCVectorClause::Create(Ctx: getContext(), BeginLoc, LParenLoc,
12925 IntExpr: VectorExpr, EndLoc);
12926 }
12927 case OpenACCClauseKind::Link: {
12928 SourceLocation LParenLoc = readSourceLocation();
12929 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12930 return OpenACCLinkClause::Create(C: getContext(), BeginLoc, LParenLoc, VarList,
12931 EndLoc);
12932 }
12933 case OpenACCClauseKind::DeviceResident: {
12934 SourceLocation LParenLoc = readSourceLocation();
12935 llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12936 return OpenACCDeviceResidentClause::Create(C: getContext(), BeginLoc,
12937 LParenLoc, VarList, EndLoc);
12938 }
12939
12940 case OpenACCClauseKind::Bind: {
12941 SourceLocation LParenLoc = readSourceLocation();
12942 bool IsString = readBool();
12943 if (IsString)
12944 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
12945 SL: cast<StringLiteral>(Val: readExpr()), EndLoc);
12946 return OpenACCBindClause::Create(C: getContext(), BeginLoc, LParenLoc,
12947 ID: readIdentifier(), EndLoc);
12948 }
12949 case OpenACCClauseKind::Shortloop:
12950 case OpenACCClauseKind::Invalid:
12951 llvm_unreachable("Clause serialization not yet implemented");
12952 }
12953 llvm_unreachable("Invalid Clause Kind");
12954}
12955
12956void ASTRecordReader::readOpenACCClauseList(
12957 MutableArrayRef<const OpenACCClause *> Clauses) {
12958 for (unsigned I = 0; I < Clauses.size(); ++I)
12959 Clauses[I] = readOpenACCClause();
12960}
12961
12962void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {
12963 unsigned NumVars = readInt();
12964 A->Clauses.resize(N: NumVars);
12965 readOpenACCClauseList(Clauses: A->Clauses);
12966}
12967
12968static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {
12969 // TODO: Maybe it is better to check PrimaryModuleName is a valid
12970 // module name?
12971 llvm::FoldingSetNodeID ID;
12972 ID.AddString(String: PrimaryModuleName);
12973 return ID.computeStableHash();
12974}
12975
12976UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {
12977 if (!M)
12978 return std::nullopt;
12979
12980 if (M->isHeaderLikeModule())
12981 return std::nullopt;
12982
12983 if (M->isGlobalModule())
12984 return std::nullopt;
12985
12986 StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();
12987 return getStableHashForModuleName(PrimaryModuleName);
12988}
12989

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