clang 20.0.0git
ASTReader.cpp
Go to the documentation of this file.
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"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclBase.h"
23#include "clang/AST/DeclCXX.h"
25#include "clang/AST/DeclGroup.h"
26#include "clang/AST/DeclObjC.h"
29#include "clang/AST/Expr.h"
30#include "clang/AST/ExprCXX.h"
39#include "clang/AST/Type.h"
40#include "clang/AST/TypeLoc.h"
54#include "clang/Basic/LLVM.h"
56#include "clang/Basic/Module.h"
70#include "clang/Basic/Version.h"
73#include "clang/Lex/MacroInfo.h"
74#include "clang/Lex/ModuleMap.h"
78#include "clang/Lex/Token.h"
80#include "clang/Sema/Scope.h"
81#include "clang/Sema/Sema.h"
82#include "clang/Sema/SemaCUDA.h"
83#include "clang/Sema/SemaObjC.h"
84#include "clang/Sema/Weak.h"
96#include "llvm/ADT/APFloat.h"
97#include "llvm/ADT/APInt.h"
98#include "llvm/ADT/APSInt.h"
99#include "llvm/ADT/ArrayRef.h"
100#include "llvm/ADT/DenseMap.h"
101#include "llvm/ADT/FloatingPointMode.h"
102#include "llvm/ADT/FoldingSet.h"
103#include "llvm/ADT/Hashing.h"
104#include "llvm/ADT/IntrusiveRefCntPtr.h"
105#include "llvm/ADT/STLExtras.h"
106#include "llvm/ADT/ScopeExit.h"
107#include "llvm/ADT/Sequence.h"
108#include "llvm/ADT/SmallPtrSet.h"
109#include "llvm/ADT/SmallString.h"
110#include "llvm/ADT/SmallVector.h"
111#include "llvm/ADT/StringExtras.h"
112#include "llvm/ADT/StringMap.h"
113#include "llvm/ADT/StringRef.h"
114#include "llvm/ADT/iterator_range.h"
115#include "llvm/Bitstream/BitstreamReader.h"
116#include "llvm/Support/Casting.h"
117#include "llvm/Support/Compiler.h"
118#include "llvm/Support/Compression.h"
119#include "llvm/Support/DJB.h"
120#include "llvm/Support/Endian.h"
121#include "llvm/Support/Error.h"
122#include "llvm/Support/ErrorHandling.h"
123#include "llvm/Support/FileSystem.h"
124#include "llvm/Support/LEB128.h"
125#include "llvm/Support/MemoryBuffer.h"
126#include "llvm/Support/Path.h"
127#include "llvm/Support/SaveAndRestore.h"
128#include "llvm/Support/TimeProfiler.h"
129#include "llvm/Support/Timer.h"
130#include "llvm/Support/VersionTuple.h"
131#include "llvm/Support/raw_ostream.h"
132#include "llvm/TargetParser/Triple.h"
133#include <algorithm>
134#include <cassert>
135#include <cstddef>
136#include <cstdint>
137#include <cstdio>
138#include <ctime>
139#include <iterator>
140#include <limits>
141#include <map>
142#include <memory>
143#include <optional>
144#include <string>
145#include <system_error>
146#include <tuple>
147#include <utility>
148#include <vector>
149
150using namespace clang;
151using namespace clang::serialization;
152using namespace clang::serialization::reader;
153using llvm::BitstreamCursor;
154
155//===----------------------------------------------------------------------===//
156// ChainedASTReaderListener implementation
157//===----------------------------------------------------------------------===//
158
159bool
161 return First->ReadFullVersionInformation(FullVersion) ||
162 Second->ReadFullVersionInformation(FullVersion);
163}
164
166 First->ReadModuleName(ModuleName);
167 Second->ReadModuleName(ModuleName);
168}
169
170void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
171 First->ReadModuleMapFile(ModuleMapPath);
172 Second->ReadModuleMapFile(ModuleMapPath);
173}
174
176 const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,
177 bool AllowCompatibleDifferences) {
178 return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
179 AllowCompatibleDifferences) ||
180 Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,
181 AllowCompatibleDifferences);
182}
183
185 const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,
186 bool AllowCompatibleDifferences) {
187 return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
188 AllowCompatibleDifferences) ||
189 Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,
190 AllowCompatibleDifferences);
191}
192
194 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, StringRef ModuleFilename,
195 bool Complain) {
196 return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||
197 Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);
198}
199
200bool
202 bool Complain) {
203 return First->ReadFileSystemOptions(FSOpts, Complain) ||
204 Second->ReadFileSystemOptions(FSOpts, Complain);
205}
206
208 const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,
209 StringRef SpecificModuleCachePath, bool Complain) {
210 return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
211 SpecificModuleCachePath, Complain) ||
212 Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename,
213 SpecificModuleCachePath, Complain);
214}
215
217 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
218 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
219 return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
220 Complain, SuggestedPredefines) ||
221 Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,
222 Complain, SuggestedPredefines);
223}
224
226 unsigned Value) {
227 First->ReadCounter(M, Value);
228 Second->ReadCounter(M, Value);
229}
230
232 return First->needsInputFileVisitation() ||
233 Second->needsInputFileVisitation();
234}
235
237 return First->needsSystemInputFileVisitation() ||
238 Second->needsSystemInputFileVisitation();
239}
240
242 ModuleKind Kind) {
243 First->visitModuleFile(Filename, Kind);
244 Second->visitModuleFile(Filename, Kind);
245}
246
248 bool isSystem,
249 bool isOverridden,
250 bool isExplicitModule) {
251 bool Continue = false;
252 if (First->needsInputFileVisitation() &&
253 (!isSystem || First->needsSystemInputFileVisitation()))
254 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
255 isExplicitModule);
256 if (Second->needsInputFileVisitation() &&
257 (!isSystem || Second->needsSystemInputFileVisitation()))
258 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
259 isExplicitModule);
260 return Continue;
261}
262
264 const ModuleFileExtensionMetadata &Metadata) {
265 First->readModuleFileExtension(Metadata);
266 Second->readModuleFileExtension(Metadata);
267}
268
269//===----------------------------------------------------------------------===//
270// PCH validator implementation
271//===----------------------------------------------------------------------===//
272
274
275/// Compare the given set of language options against an existing set of
276/// language options.
277///
278/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
279/// \param AllowCompatibleDifferences If true, differences between compatible
280/// language options will be permitted.
281///
282/// \returns true if the languagae options mis-match, false otherwise.
283static bool checkLanguageOptions(const LangOptions &LangOpts,
284 const LangOptions &ExistingLangOpts,
285 StringRef ModuleFilename,
286 DiagnosticsEngine *Diags,
287 bool AllowCompatibleDifferences = true) {
288#define LANGOPT(Name, Bits, Default, Description) \
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#define VALUE_LANGOPT(Name, Bits, Default, Description) \
303 if (ExistingLangOpts.Name != LangOpts.Name) { \
304 if (Diags) \
305 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
306 << Description << ModuleFilename; \
307 return true; \
308 }
309
310#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
311 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
312 if (Diags) \
313 Diags->Report(diag::err_ast_file_langopt_value_mismatch) \
314 << Description << ModuleFilename; \
315 return true; \
316 }
317
318#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
319 if (!AllowCompatibleDifferences) \
320 LANGOPT(Name, Bits, Default, Description)
321
322#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
323 if (!AllowCompatibleDifferences) \
324 ENUM_LANGOPT(Name, Bits, Default, Description)
325
326#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
327 if (!AllowCompatibleDifferences) \
328 VALUE_LANGOPT(Name, Bits, Default, Description)
329
330#define BENIGN_LANGOPT(Name, Bits, Default, Description)
331#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
332#define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description)
333#include "clang/Basic/LangOptions.def"
334
335 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
336 if (Diags)
337 Diags->Report(diag::err_ast_file_langopt_value_mismatch)
338 << "module features" << ModuleFilename;
339 return true;
340 }
341
342 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
343 if (Diags)
344 Diags->Report(diag::err_ast_file_langopt_value_mismatch)
345 << "target Objective-C runtime" << ModuleFilename;
346 return true;
347 }
348
349 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
351 if (Diags)
352 Diags->Report(diag::err_ast_file_langopt_value_mismatch)
353 << "block command names" << ModuleFilename;
354 return true;
355 }
356
357 // Sanitizer feature mismatches are treated as compatible differences. If
358 // compatible differences aren't allowed, we still only want to check for
359 // mismatches of non-modular sanitizers (the only ones which can affect AST
360 // generation).
361 if (!AllowCompatibleDifferences) {
362 SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
363 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
364 SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
365 ExistingSanitizers.clear(ModularSanitizers);
366 ImportedSanitizers.clear(ModularSanitizers);
367 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
368 const std::string Flag = "-fsanitize=";
369 if (Diags) {
370#define SANITIZER(NAME, ID) \
371 { \
372 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \
373 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \
374 if (InExistingModule != InImportedModule) \
375 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch) \
376 << InExistingModule << ModuleFilename << (Flag + NAME); \
377 }
378#include "clang/Basic/Sanitizers.def"
379 }
380 return true;
381 }
382 }
383
384 return false;
385}
386
387/// Compare the given set of target options against an existing set of
388/// target options.
389///
390/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
391///
392/// \returns true if the target options mis-match, false otherwise.
393static bool checkTargetOptions(const TargetOptions &TargetOpts,
394 const TargetOptions &ExistingTargetOpts,
395 StringRef ModuleFilename,
396 DiagnosticsEngine *Diags,
397 bool AllowCompatibleDifferences = true) {
398#define CHECK_TARGET_OPT(Field, Name) \
399 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
400 if (Diags) \
401 Diags->Report(diag::err_ast_file_targetopt_mismatch) \
402 << ModuleFilename << Name << TargetOpts.Field \
403 << ExistingTargetOpts.Field; \
404 return true; \
405 }
406
407 // The triple and ABI must match exactly.
408 CHECK_TARGET_OPT(Triple, "target");
409 CHECK_TARGET_OPT(ABI, "target ABI");
410
411 // We can tolerate different CPUs in many cases, notably when one CPU
412 // supports a strict superset of another. When allowing compatible
413 // differences skip this check.
414 if (!AllowCompatibleDifferences) {
415 CHECK_TARGET_OPT(CPU, "target CPU");
416 CHECK_TARGET_OPT(TuneCPU, "tune CPU");
417 }
418
419#undef CHECK_TARGET_OPT
420
421 // Compare feature sets.
422 SmallVector<StringRef, 4> ExistingFeatures(
423 ExistingTargetOpts.FeaturesAsWritten.begin(),
424 ExistingTargetOpts.FeaturesAsWritten.end());
425 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
426 TargetOpts.FeaturesAsWritten.end());
427 llvm::sort(ExistingFeatures);
428 llvm::sort(ReadFeatures);
429
430 // We compute the set difference in both directions explicitly so that we can
431 // diagnose the differences differently.
432 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
433 std::set_difference(
434 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
435 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
436 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
437 ExistingFeatures.begin(), ExistingFeatures.end(),
438 std::back_inserter(UnmatchedReadFeatures));
439
440 // If we are allowing compatible differences and the read feature set is
441 // a strict subset of the existing feature set, there is nothing to diagnose.
442 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
443 return false;
444
445 if (Diags) {
446 for (StringRef Feature : UnmatchedReadFeatures)
447 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch)
448 << /* is-existing-feature */ false << ModuleFilename << Feature;
449 for (StringRef Feature : UnmatchedExistingFeatures)
450 Diags->Report(diag::err_ast_file_targetopt_feature_mismatch)
451 << /* is-existing-feature */ true << ModuleFilename << Feature;
452 }
453
454 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
455}
456
458 StringRef ModuleFilename, bool Complain,
459 bool AllowCompatibleDifferences) {
460 const LangOptions &ExistingLangOpts = PP.getLangOpts();
461 return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,
462 Complain ? &Reader.Diags : nullptr,
463 AllowCompatibleDifferences);
464}
465
467 StringRef ModuleFilename, bool Complain,
468 bool AllowCompatibleDifferences) {
469 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
470 return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,
471 Complain ? &Reader.Diags : nullptr,
472 AllowCompatibleDifferences);
473}
474
475namespace {
476
477using MacroDefinitionsMap =
478 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
479using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
480
481} // namespace
482
484 DiagnosticsEngine &Diags,
485 StringRef ModuleFilename,
486 bool Complain) {
487 using Level = DiagnosticsEngine::Level;
488
489 // Check current mappings for new -Werror mappings, and the stored mappings
490 // for cases that were explicitly mapped to *not* be errors that are now
491 // errors because of options like -Werror.
492 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
493
494 for (DiagnosticsEngine *MappingSource : MappingSources) {
495 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
496 diag::kind DiagID = DiagIDMappingPair.first;
497 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
498 if (CurLevel < DiagnosticsEngine::Error)
499 continue; // not significant
500 Level StoredLevel =
501 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
502 if (StoredLevel < DiagnosticsEngine::Error) {
503 if (Complain)
504 Diags.Report(diag::err_ast_file_diagopt_mismatch)
505 << "-Werror=" + Diags.getDiagnosticIDs()
506 ->getWarningOptionForDiag(DiagID)
507 .str()
508 << ModuleFilename;
509 return true;
510 }
511 }
512 }
513
514 return false;
515}
516
519 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
520 return true;
521 return Ext >= diag::Severity::Error;
522}
523
525 DiagnosticsEngine &Diags,
526 StringRef ModuleFilename, bool IsSystem,
527 bool SystemHeaderWarningsInModule,
528 bool Complain) {
529 // Top-level options
530 if (IsSystem) {
531 if (Diags.getSuppressSystemWarnings())
532 return false;
533 // If -Wsystem-headers was not enabled before, and it was not explicit,
534 // be conservative
535 if (StoredDiags.getSuppressSystemWarnings() &&
536 !SystemHeaderWarningsInModule) {
537 if (Complain)
538 Diags.Report(diag::err_ast_file_diagopt_mismatch)
539 << "-Wsystem-headers" << ModuleFilename;
540 return true;
541 }
542 }
543
544 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
545 if (Complain)
546 Diags.Report(diag::err_ast_file_diagopt_mismatch)
547 << "-Werror" << ModuleFilename;
548 return true;
549 }
550
551 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
552 !StoredDiags.getEnableAllWarnings()) {
553 if (Complain)
554 Diags.Report(diag::err_ast_file_diagopt_mismatch)
555 << "-Weverything -Werror" << ModuleFilename;
556 return true;
557 }
558
559 if (isExtHandlingFromDiagsError(Diags) &&
560 !isExtHandlingFromDiagsError(StoredDiags)) {
561 if (Complain)
562 Diags.Report(diag::err_ast_file_diagopt_mismatch)
563 << "-pedantic-errors" << ModuleFilename;
564 return true;
565 }
566
567 return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,
568 Complain);
569}
570
571/// Return the top import module if it is implicit, nullptr otherwise.
573 Preprocessor &PP) {
574 // If the original import came from a file explicitly generated by the user,
575 // don't check the diagnostic mappings.
576 // FIXME: currently this is approximated by checking whether this is not a
577 // module import of an implicitly-loaded module file.
578 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
579 // the transitive closure of its imports, since unrelated modules cannot be
580 // imported until after this module finishes validation.
581 ModuleFile *TopImport = &*ModuleMgr.rbegin();
582 while (!TopImport->ImportedBy.empty())
583 TopImport = TopImport->ImportedBy[0];
584 if (TopImport->Kind != MK_ImplicitModule)
585 return nullptr;
586
587 StringRef ModuleName = TopImport->ModuleName;
588 assert(!ModuleName.empty() && "diagnostic options read before module name");
589
590 Module *M =
591 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
592 assert(M && "missing module");
593 return M;
594}
595
597 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, StringRef ModuleFilename,
598 bool Complain) {
599 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
602 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
603 // This should never fail, because we would have processed these options
604 // before writing them to an ASTFile.
605 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
606
607 ModuleManager &ModuleMgr = Reader.getModuleManager();
608 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
609
610 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
611 if (!TopM)
612 return false;
613
614 Module *Importer = PP.getCurrentModule();
615
616 DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();
617 bool SystemHeaderWarningsInModule =
618 Importer && llvm::is_contained(ExistingOpts.SystemHeaderWarningsModules,
619 Importer->Name);
620
621 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
622 // contains the union of their flags.
623 return checkDiagnosticMappings(*Diags, ExistingDiags, ModuleFilename,
624 TopM->IsSystem, SystemHeaderWarningsInModule,
625 Complain);
626}
627
628/// Collect the macro definitions provided by the given preprocessor
629/// options.
630static void
632 MacroDefinitionsMap &Macros,
633 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
634 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
635 StringRef Macro = PPOpts.Macros[I].first;
636 bool IsUndef = PPOpts.Macros[I].second;
637
638 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
639 StringRef MacroName = MacroPair.first;
640 StringRef MacroBody = MacroPair.second;
641
642 // For an #undef'd macro, we only care about the name.
643 if (IsUndef) {
644 if (MacroNames && !Macros.count(MacroName))
645 MacroNames->push_back(MacroName);
646
647 Macros[MacroName] = std::make_pair("", true);
648 continue;
649 }
650
651 // For a #define'd macro, figure out the actual definition.
652 if (MacroName.size() == Macro.size())
653 MacroBody = "1";
654 else {
655 // Note: GCC drops anything following an end-of-line character.
656 StringRef::size_type End = MacroBody.find_first_of("\n\r");
657 MacroBody = MacroBody.substr(0, End);
658 }
659
660 if (MacroNames && !Macros.count(MacroName))
661 MacroNames->push_back(MacroName);
662 Macros[MacroName] = std::make_pair(MacroBody, false);
663 }
664}
665
670};
671
672/// Check the preprocessor options deserialized from the control block
673/// against the preprocessor options in an existing preprocessor.
674///
675/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
676/// \param Validation If set to OptionValidateNone, ignore differences in
677/// preprocessor options. If set to OptionValidateContradictions,
678/// require that options passed both in the AST file and on the command
679/// line (-D or -U) match, but tolerate options missing in one or the
680/// other. If set to OptionValidateContradictions, require that there
681/// are no differences in the options between the two.
683 const PreprocessorOptions &PPOpts,
684 const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,
685 bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,
686 std::string &SuggestedPredefines, const LangOptions &LangOpts,
688 if (ReadMacros) {
689 // Check macro definitions.
690 MacroDefinitionsMap ASTFileMacros;
691 collectMacroDefinitions(PPOpts, ASTFileMacros);
692 MacroDefinitionsMap ExistingMacros;
693 SmallVector<StringRef, 4> ExistingMacroNames;
694 collectMacroDefinitions(ExistingPPOpts, ExistingMacros,
695 &ExistingMacroNames);
696
697 // Use a line marker to enter the <command line> file, as the defines and
698 // undefines here will have come from the command line.
699 SuggestedPredefines += "# 1 \"<command line>\" 1\n";
700
701 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
702 // Dig out the macro definition in the existing preprocessor options.
703 StringRef MacroName = ExistingMacroNames[I];
704 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
705
706 // Check whether we know anything about this macro name or not.
707 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
708 ASTFileMacros.find(MacroName);
709 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {
710 if (Validation == OptionValidateStrictMatches) {
711 // If strict matches are requested, don't tolerate any extra defines
712 // on the command line that are missing in the AST file.
713 if (Diags) {
714 Diags->Report(diag::err_ast_file_macro_def_undef)
715 << MacroName << true << ModuleFilename;
716 }
717 return true;
718 }
719 // FIXME: Check whether this identifier was referenced anywhere in the
720 // AST file. If so, we should reject the AST file. Unfortunately, this
721 // information isn't in the control block. What shall we do about it?
722
723 if (Existing.second) {
724 SuggestedPredefines += "#undef ";
725 SuggestedPredefines += MacroName.str();
726 SuggestedPredefines += '\n';
727 } else {
728 SuggestedPredefines += "#define ";
729 SuggestedPredefines += MacroName.str();
730 SuggestedPredefines += ' ';
731 SuggestedPredefines += Existing.first.str();
732 SuggestedPredefines += '\n';
733 }
734 continue;
735 }
736
737 // If the macro was defined in one but undef'd in the other, we have a
738 // conflict.
739 if (Existing.second != Known->second.second) {
740 if (Diags) {
741 Diags->Report(diag::err_ast_file_macro_def_undef)
742 << MacroName << Known->second.second << ModuleFilename;
743 }
744 return true;
745 }
746
747 // If the macro was #undef'd in both, or if the macro bodies are
748 // identical, it's fine.
749 if (Existing.second || Existing.first == Known->second.first) {
750 ASTFileMacros.erase(Known);
751 continue;
752 }
753
754 // The macro bodies differ; complain.
755 if (Diags) {
756 Diags->Report(diag::err_ast_file_macro_def_conflict)
757 << MacroName << Known->second.first << Existing.first
758 << ModuleFilename;
759 }
760 return true;
761 }
762
763 // Leave the <command line> file and return to <built-in>.
764 SuggestedPredefines += "# 1 \"<built-in>\" 2\n";
765
766 if (Validation == OptionValidateStrictMatches) {
767 // If strict matches are requested, don't tolerate any extra defines in
768 // the AST file that are missing on the command line.
769 for (const auto &MacroName : ASTFileMacros.keys()) {
770 if (Diags) {
771 Diags->Report(diag::err_ast_file_macro_def_undef)
772 << MacroName << false << ModuleFilename;
773 }
774 return true;
775 }
776 }
777 }
778
779 // Check whether we're using predefines.
780 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&
781 Validation != OptionValidateNone) {
782 if (Diags) {
783 Diags->Report(diag::err_ast_file_undef)
784 << ExistingPPOpts.UsePredefines << ModuleFilename;
785 }
786 return true;
787 }
788
789 // Detailed record is important since it is used for the module cache hash.
790 if (LangOpts.Modules &&
791 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&
792 Validation != OptionValidateNone) {
793 if (Diags) {
794 Diags->Report(diag::err_ast_file_pp_detailed_record)
795 << PPOpts.DetailedRecord << ModuleFilename;
796 }
797 return true;
798 }
799
800 // Compute the #include and #include_macros lines we need.
801 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
802 StringRef File = ExistingPPOpts.Includes[I];
803
804 if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
805 !ExistingPPOpts.PCHThroughHeader.empty()) {
806 // In case the through header is an include, we must add all the includes
807 // to the predefines so the start point can be determined.
808 SuggestedPredefines += "#include \"";
809 SuggestedPredefines += File;
810 SuggestedPredefines += "\"\n";
811 continue;
812 }
813
814 if (File == ExistingPPOpts.ImplicitPCHInclude)
815 continue;
816
817 if (llvm::is_contained(PPOpts.Includes, File))
818 continue;
819
820 SuggestedPredefines += "#include \"";
821 SuggestedPredefines += File;
822 SuggestedPredefines += "\"\n";
823 }
824
825 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
826 StringRef File = ExistingPPOpts.MacroIncludes[I];
827 if (llvm::is_contained(PPOpts.MacroIncludes, File))
828 continue;
829
830 SuggestedPredefines += "#__include_macros \"";
831 SuggestedPredefines += File;
832 SuggestedPredefines += "\"\n##\n";
833 }
834
835 return false;
836}
837
839 StringRef ModuleFilename,
840 bool ReadMacros, bool Complain,
841 std::string &SuggestedPredefines) {
842 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
843
845 PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,
846 Complain ? &Reader.Diags : nullptr, PP.getFileManager(),
847 SuggestedPredefines, PP.getLangOpts());
848}
849
851 const PreprocessorOptions &PPOpts, StringRef ModuleFilename,
852 bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {
854 ModuleFilename, ReadMacros, nullptr,
855 PP.getFileManager(), SuggestedPredefines,
857}
858
859/// Check that the specified and the existing module cache paths are equivalent.
860///
861/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
862/// \returns true when the module cache paths differ.
863static bool checkModuleCachePath(llvm::vfs::FileSystem &VFS,
864 StringRef SpecificModuleCachePath,
865 StringRef ExistingModuleCachePath,
866 StringRef ModuleFilename,
867 DiagnosticsEngine *Diags,
868 const LangOptions &LangOpts,
869 const PreprocessorOptions &PPOpts) {
870 if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||
871 SpecificModuleCachePath == ExistingModuleCachePath)
872 return false;
873 auto EqualOrErr =
874 VFS.equivalent(SpecificModuleCachePath, ExistingModuleCachePath);
875 if (EqualOrErr && *EqualOrErr)
876 return false;
877 if (Diags)
878 Diags->Report(diag::err_ast_file_modulecache_mismatch)
879 << SpecificModuleCachePath << ExistingModuleCachePath << ModuleFilename;
880 return true;
881}
882
884 StringRef ModuleFilename,
885 StringRef SpecificModuleCachePath,
886 bool Complain) {
888 Reader.getFileManager().getVirtualFileSystem(), SpecificModuleCachePath,
889 PP.getHeaderSearchInfo().getModuleCachePath(), ModuleFilename,
890 Complain ? &Reader.Diags : nullptr, PP.getLangOpts(),
892}
893
896}
897
898//===----------------------------------------------------------------------===//
899// AST reader implementation
900//===----------------------------------------------------------------------===//
901
902static uint64_t readULEB(const unsigned char *&P) {
903 unsigned Length = 0;
904 const char *Error = nullptr;
905
906 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
907 if (Error)
908 llvm::report_fatal_error(Error);
909 P += Length;
910 return Val;
911}
912
913/// Read ULEB-encoded key length and data length.
914static std::pair<unsigned, unsigned>
915readULEBKeyDataLength(const unsigned char *&P) {
916 unsigned KeyLen = readULEB(P);
917 if ((unsigned)KeyLen != KeyLen)
918 llvm::report_fatal_error("key too large");
919
920 unsigned DataLen = readULEB(P);
921 if ((unsigned)DataLen != DataLen)
922 llvm::report_fatal_error("data too large");
923
924 return std::make_pair(KeyLen, DataLen);
925}
926
928 bool TakeOwnership) {
929 DeserializationListener = Listener;
930 OwnsDeserializationListener = TakeOwnership;
931}
932
934 return serialization::ComputeHash(Sel);
935}
936
939#ifndef NDEBUG
940 if (!MF.ModuleOffsetMap.empty())
941 Reader.ReadModuleOffsetMap(MF);
942
943 unsigned ModuleFileIndex = ID.getModuleFileIndex();
944 unsigned LocalDeclID = ID.getLocalDeclIndex();
945
946 assert(ModuleFileIndex <= MF.TransitiveImports.size());
947
948 ModuleFile *OwningModuleFile =
949 ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];
950 assert(OwningModuleFile);
951
952 unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;
953
954 if (!ModuleFileIndex)
955 LocalNumDecls += NUM_PREDEF_DECL_IDS;
956
957 assert(LocalDeclID < LocalNumDecls);
958#endif
959 (void)Reader;
960 (void)MF;
961 return ID;
962}
963
965 unsigned ModuleFileIndex, unsigned LocalDeclID) {
966 DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;
967 return LocalDeclID::get(Reader, MF, Value);
968}
969
970std::pair<unsigned, unsigned>
972 return readULEBKeyDataLength(d);
973}
974
976ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
977 using namespace llvm::support;
978
979 SelectorTable &SelTable = Reader.getContext().Selectors;
980 unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(d);
981 const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
982 F, endian::readNext<IdentifierID, llvm::endianness::little>(d));
983 if (N == 0)
984 return SelTable.getNullarySelector(FirstII);
985 else if (N == 1)
986 return SelTable.getUnarySelector(FirstII);
987
989 Args.push_back(FirstII);
990 for (unsigned I = 1; I != N; ++I)
991 Args.push_back(Reader.getLocalIdentifier(
992 F, endian::readNext<IdentifierID, llvm::endianness::little>(d)));
993
994 return SelTable.getSelector(N, Args.data());
995}
996
999 unsigned DataLen) {
1000 using namespace llvm::support;
1001
1003
1004 Result.ID = Reader.getGlobalSelectorID(
1005 F, endian::readNext<uint32_t, llvm::endianness::little>(d));
1006 unsigned FullInstanceBits =
1007 endian::readNext<uint16_t, llvm::endianness::little>(d);
1008 unsigned FullFactoryBits =
1009 endian::readNext<uint16_t, llvm::endianness::little>(d);
1010 Result.InstanceBits = FullInstanceBits & 0x3;
1011 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
1012 Result.FactoryBits = FullFactoryBits & 0x3;
1013 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
1014 unsigned NumInstanceMethods = FullInstanceBits >> 3;
1015 unsigned NumFactoryMethods = FullFactoryBits >> 3;
1016
1017 // Load instance methods
1018 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1019 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1021 Reader, F,
1022 endian::readNext<DeclID, llvm::endianness::little>(d))))
1023 Result.Instance.push_back(Method);
1024 }
1025
1026 // Load factory methods
1027 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1028 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
1030 Reader, F,
1031 endian::readNext<DeclID, llvm::endianness::little>(d))))
1032 Result.Factory.push_back(Method);
1033 }
1034
1035 return Result;
1036}
1037
1039 return llvm::djbHash(a);
1040}
1041
1042std::pair<unsigned, unsigned>
1044 return readULEBKeyDataLength(d);
1045}
1046
1048ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
1049 assert(n >= 2 && d[n-1] == '\0');
1050 return StringRef((const char*) d, n-1);
1051}
1052
1053/// Whether the given identifier is "interesting".
1054static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,
1055 bool IsModule) {
1056 bool IsInteresting =
1057 II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||
1059 II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;
1060 return II.hadMacroDefinition() || II.isPoisoned() ||
1061 (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||
1062 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
1063 II.getFETokenInfo());
1064}
1065
1066static bool readBit(unsigned &Bits) {
1067 bool Value = Bits & 0x1;
1068 Bits >>= 1;
1069 return Value;
1070}
1071
1073 using namespace llvm::support;
1074
1075 IdentifierID RawID =
1076 endian::readNext<IdentifierID, llvm::endianness::little>(d);
1077 return Reader.getGlobalIdentifierID(F, RawID >> 1);
1078}
1079
1081 bool IsModule) {
1082 if (!II.isFromAST()) {
1083 II.setIsFromAST();
1084 if (isInterestingIdentifier(Reader, II, IsModule))
1086 }
1087}
1088
1090 const unsigned char* d,
1091 unsigned DataLen) {
1092 using namespace llvm::support;
1093
1094 IdentifierID RawID =
1095 endian::readNext<IdentifierID, llvm::endianness::little>(d);
1096 bool IsInteresting = RawID & 0x01;
1097
1098 DataLen -= sizeof(IdentifierID);
1099
1100 // Wipe out the "is interesting" bit.
1101 RawID = RawID >> 1;
1102
1103 // Build the IdentifierInfo and link the identifier ID with it.
1104 IdentifierInfo *II = KnownII;
1105 if (!II) {
1106 II = &Reader.getIdentifierTable().getOwn(k);
1107 KnownII = II;
1108 }
1109 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1110 markIdentifierFromAST(Reader, *II, IsModule);
1111 Reader.markIdentifierUpToDate(II);
1112
1113 IdentifierID ID = Reader.getGlobalIdentifierID(F, RawID);
1114 if (!IsInteresting) {
1115 // For uninteresting identifiers, there's nothing else to do. Just notify
1116 // the reader that we've finished loading this identifier.
1117 Reader.SetIdentifierInfo(ID, II);
1118 return II;
1119 }
1120
1121 unsigned ObjCOrBuiltinID =
1122 endian::readNext<uint16_t, llvm::endianness::little>(d);
1123 unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(d);
1124 bool CPlusPlusOperatorKeyword = readBit(Bits);
1125 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
1126 bool Poisoned = readBit(Bits);
1127 bool ExtensionToken = readBit(Bits);
1128 bool HadMacroDefinition = readBit(Bits);
1129
1130 assert(Bits == 0 && "Extra bits in the identifier?");
1131 DataLen -= sizeof(uint16_t) * 2;
1132
1133 // Set or check the various bits in the IdentifierInfo structure.
1134 // Token IDs are read-only.
1135 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1137 if (!F.isModule())
1138 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1139 assert(II->isExtensionToken() == ExtensionToken &&
1140 "Incorrect extension token flag");
1141 (void)ExtensionToken;
1142 if (Poisoned)
1143 II->setIsPoisoned(true);
1144 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1145 "Incorrect C++ operator keyword flag");
1146 (void)CPlusPlusOperatorKeyword;
1147
1148 // If this identifier is a macro, deserialize the macro
1149 // definition.
1150 if (HadMacroDefinition) {
1151 uint32_t MacroDirectivesOffset =
1152 endian::readNext<uint32_t, llvm::endianness::little>(d);
1153 DataLen -= 4;
1154
1155 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1156 }
1157
1158 Reader.SetIdentifierInfo(ID, II);
1159
1160 // Read all of the declarations visible at global scope with this
1161 // name.
1162 if (DataLen > 0) {
1164 for (; DataLen > 0; DataLen -= sizeof(DeclID))
1165 DeclIDs.push_back(Reader.getGlobalDeclID(
1167 Reader, F,
1168 endian::readNext<DeclID, llvm::endianness::little>(d))));
1169 Reader.SetGloballyVisibleDecls(II, DeclIDs);
1170 }
1171
1172 return II;
1173}
1174
1176 : Kind(Name.getNameKind()) {
1177 switch (Kind) {
1179 Data = (uint64_t)Name.getAsIdentifierInfo();
1180 break;
1184 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1185 break;
1187 Data = Name.getCXXOverloadedOperator();
1188 break;
1190 Data = (uint64_t)Name.getCXXLiteralIdentifier();
1191 break;
1193 Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1194 ->getDeclName().getAsIdentifierInfo();
1195 break;
1200 Data = 0;
1201 break;
1202 }
1203}
1204
1206 llvm::FoldingSetNodeID ID;
1207 ID.AddInteger(Kind);
1208
1209 switch (Kind) {
1213 ID.AddString(((IdentifierInfo*)Data)->getName());
1214 break;
1218 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1219 break;
1221 ID.AddInteger((OverloadedOperatorKind)Data);
1222 break;
1227 break;
1228 }
1229
1230 return ID.computeStableHash();
1231}
1232
1233ModuleFile *
1234ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1235 using namespace llvm::support;
1236
1237 uint32_t ModuleFileID =
1238 endian::readNext<uint32_t, llvm::endianness::little>(d);
1239 return Reader.getLocalModuleFile(F, ModuleFileID);
1240}
1241
1242std::pair<unsigned, unsigned>
1243ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1244 return readULEBKeyDataLength(d);
1245}
1246
1248ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1249 using namespace llvm::support;
1250
1251 auto Kind = (DeclarationName::NameKind)*d++;
1252 uint64_t Data;
1253 switch (Kind) {
1257 Data = (uint64_t)Reader.getLocalIdentifier(
1258 F, endian::readNext<IdentifierID, llvm::endianness::little>(d));
1259 break;
1263 Data = (uint64_t)Reader
1264 .getLocalSelector(
1265 F, endian::readNext<uint32_t, llvm::endianness::little>(d))
1266 .getAsOpaquePtr();
1267 break;
1269 Data = *d++; // OverloadedOperatorKind
1270 break;
1275 Data = 0;
1276 break;
1277 }
1278
1279 return DeclarationNameKey(Kind, Data);
1280}
1281
1282void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1283 const unsigned char *d,
1284 unsigned DataLen,
1285 data_type_builder &Val) {
1286 using namespace llvm::support;
1287
1288 for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1290 Reader, F, endian::readNext<DeclID, llvm::endianness::little>(d));
1291 Val.insert(Reader.getGlobalDeclID(F, ID));
1292 }
1293}
1294
1295bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1296 BitstreamCursor &Cursor,
1297 uint64_t Offset,
1298 DeclContext *DC) {
1299 assert(Offset != 0);
1300
1301 SavedStreamPosition SavedPosition(Cursor);
1302 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1303 Error(std::move(Err));
1304 return true;
1305 }
1306
1307 RecordData Record;
1308 StringRef Blob;
1309 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1310 if (!MaybeCode) {
1311 Error(MaybeCode.takeError());
1312 return true;
1313 }
1314 unsigned Code = MaybeCode.get();
1315
1316 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1317 if (!MaybeRecCode) {
1318 Error(MaybeRecCode.takeError());
1319 return true;
1320 }
1321 unsigned RecCode = MaybeRecCode.get();
1322 if (RecCode != DECL_CONTEXT_LEXICAL) {
1323 Error("Expected lexical block");
1324 return true;
1325 }
1326
1327 assert(!isa<TranslationUnitDecl>(DC) &&
1328 "expected a TU_UPDATE_LEXICAL record for TU");
1329 // If we are handling a C++ class template instantiation, we can see multiple
1330 // lexical updates for the same record. It's important that we select only one
1331 // of them, so that field numbering works properly. Just pick the first one we
1332 // see.
1333 auto &Lex = LexicalDecls[DC];
1334 if (!Lex.first) {
1335 Lex = std::make_pair(
1336 &M, llvm::ArrayRef(
1337 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1338 Blob.size() / sizeof(DeclID)));
1339 }
1341 return false;
1342}
1343
1344bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1345 BitstreamCursor &Cursor,
1346 uint64_t Offset,
1347 GlobalDeclID ID) {
1348 assert(Offset != 0);
1349
1350 SavedStreamPosition SavedPosition(Cursor);
1351 if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1352 Error(std::move(Err));
1353 return true;
1354 }
1355
1356 RecordData Record;
1357 StringRef Blob;
1358 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1359 if (!MaybeCode) {
1360 Error(MaybeCode.takeError());
1361 return true;
1362 }
1363 unsigned Code = MaybeCode.get();
1364
1365 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1366 if (!MaybeRecCode) {
1367 Error(MaybeRecCode.takeError());
1368 return true;
1369 }
1370 unsigned RecCode = MaybeRecCode.get();
1371 if (RecCode != DECL_CONTEXT_VISIBLE) {
1372 Error("Expected visible lookup table block");
1373 return true;
1374 }
1375
1376 // We can't safely determine the primary context yet, so delay attaching the
1377 // lookup table until we're done with recursive deserialization.
1378 auto *Data = (const unsigned char*)Blob.data();
1379 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1380 return false;
1381}
1382
1383void ASTReader::Error(StringRef Msg) const {
1384 Error(diag::err_fe_pch_malformed, Msg);
1385 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1386 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1387 Diag(diag::note_module_cache_path)
1388 << PP.getHeaderSearchInfo().getModuleCachePath();
1389 }
1390}
1391
1392void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1393 StringRef Arg3) const {
1394 if (Diags.isDiagnosticInFlight())
1395 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1396 else
1397 Diag(DiagID) << Arg1 << Arg2 << Arg3;
1398}
1399
1400void ASTReader::Error(llvm::Error &&Err) const {
1401 llvm::Error RemainingErr =
1402 handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1403 auto Diag = E.getDiagnostic().second;
1404
1405 // Ideally we'd just emit it, but have to handle a possible in-flight
1406 // diagnostic. Note that the location is currently ignored as well.
1407 auto NumArgs = Diag.getStorage()->NumDiagArgs;
1408 assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1409 StringRef Arg1, Arg2, Arg3;
1410 switch (NumArgs) {
1411 case 3:
1412 Arg3 = Diag.getStringArg(2);
1413 [[fallthrough]];
1414 case 2:
1415 Arg2 = Diag.getStringArg(1);
1416 [[fallthrough]];
1417 case 1:
1418 Arg1 = Diag.getStringArg(0);
1419 }
1420 Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1421 });
1422 if (RemainingErr)
1423 Error(toString(std::move(RemainingErr)));
1424}
1425
1426//===----------------------------------------------------------------------===//
1427// Source Manager Deserialization
1428//===----------------------------------------------------------------------===//
1429
1430/// Read the line table in the source manager block.
1431void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1432 unsigned Idx = 0;
1433 LineTableInfo &LineTable = SourceMgr.getLineTable();
1434
1435 // Parse the file names
1436 std::map<int, int> FileIDs;
1437 FileIDs[-1] = -1; // For unspecified filenames.
1438 for (unsigned I = 0; Record[Idx]; ++I) {
1439 // Extract the file name
1440 auto Filename = ReadPath(F, Record, Idx);
1441 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1442 }
1443 ++Idx;
1444
1445 // Parse the line entries
1446 std::vector<LineEntry> Entries;
1447 while (Idx < Record.size()) {
1448 FileID FID = ReadFileID(F, Record, Idx);
1449
1450 // Extract the line entries
1451 unsigned NumEntries = Record[Idx++];
1452 assert(NumEntries && "no line entries for file ID");
1453 Entries.clear();
1454 Entries.reserve(NumEntries);
1455 for (unsigned I = 0; I != NumEntries; ++I) {
1456 unsigned FileOffset = Record[Idx++];
1457 unsigned LineNo = Record[Idx++];
1458 int FilenameID = FileIDs[Record[Idx++]];
1461 unsigned IncludeOffset = Record[Idx++];
1462 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1463 FileKind, IncludeOffset));
1464 }
1465 LineTable.AddEntry(FID, Entries);
1466 }
1467}
1468
1469/// Read a source manager block
1470llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1471 using namespace SrcMgr;
1472
1473 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1474
1475 // Set the source-location entry cursor to the current position in
1476 // the stream. This cursor will be used to read the contents of the
1477 // source manager block initially, and then lazily read
1478 // source-location entries as needed.
1479 SLocEntryCursor = F.Stream;
1480
1481 // The stream itself is going to skip over the source manager block.
1482 if (llvm::Error Err = F.Stream.SkipBlock())
1483 return Err;
1484
1485 // Enter the source manager block.
1486 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1487 return Err;
1488 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1489
1490 RecordData Record;
1491 while (true) {
1493 SLocEntryCursor.advanceSkippingSubblocks();
1494 if (!MaybeE)
1495 return MaybeE.takeError();
1496 llvm::BitstreamEntry E = MaybeE.get();
1497
1498 switch (E.Kind) {
1499 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1500 case llvm::BitstreamEntry::Error:
1501 return llvm::createStringError(std::errc::illegal_byte_sequence,
1502 "malformed block record in AST file");
1503 case llvm::BitstreamEntry::EndBlock:
1504 return llvm::Error::success();
1505 case llvm::BitstreamEntry::Record:
1506 // The interesting case.
1507 break;
1508 }
1509
1510 // Read a record.
1511 Record.clear();
1512 StringRef Blob;
1513 Expected<unsigned> MaybeRecord =
1514 SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1515 if (!MaybeRecord)
1516 return MaybeRecord.takeError();
1517 switch (MaybeRecord.get()) {
1518 default: // Default behavior: ignore.
1519 break;
1520
1521 case SM_SLOC_FILE_ENTRY:
1524 // Once we hit one of the source location entries, we're done.
1525 return llvm::Error::success();
1526 }
1527 }
1528}
1529
1532 BitstreamCursor &Cursor = F->SLocEntryCursor;
1533 SavedStreamPosition SavedPosition(Cursor);
1534 if (llvm::Error Err = Cursor.JumpToBit(F->SLocEntryOffsetsBase +
1535 F->SLocEntryOffsets[Index]))
1536 return std::move(Err);
1537
1538 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
1539 if (!MaybeEntry)
1540 return MaybeEntry.takeError();
1541
1542 llvm::BitstreamEntry Entry = MaybeEntry.get();
1543 if (Entry.Kind != llvm::BitstreamEntry::Record)
1544 return llvm::createStringError(
1545 std::errc::illegal_byte_sequence,
1546 "incorrectly-formatted source location entry in AST file");
1547
1549 StringRef Blob;
1550 Expected<unsigned> MaybeSLOC = Cursor.readRecord(Entry.ID, Record, &Blob);
1551 if (!MaybeSLOC)
1552 return MaybeSLOC.takeError();
1553
1554 switch (MaybeSLOC.get()) {
1555 default:
1556 return llvm::createStringError(
1557 std::errc::illegal_byte_sequence,
1558 "incorrectly-formatted source location entry in AST file");
1559 case SM_SLOC_FILE_ENTRY:
1562 return F->SLocEntryBaseOffset + Record[0];
1563 }
1564}
1565
1567 auto SLocMapI =
1568 GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - SLocOffset - 1);
1569 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
1570 "Corrupted global sloc offset map");
1571 ModuleFile *F = SLocMapI->second;
1572
1573 bool Invalid = false;
1574
1575 auto It = llvm::upper_bound(
1576 llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,
1577 [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {
1578 int ID = F->SLocEntryBaseID + LocalIndex;
1579 std::size_t Index = -ID - 2;
1580 if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {
1581 assert(!SourceMgr.SLocEntryLoaded[Index]);
1582 auto MaybeEntryOffset = readSLocOffset(F, LocalIndex);
1583 if (!MaybeEntryOffset) {
1584 Error(MaybeEntryOffset.takeError());
1585 Invalid = true;
1586 return true;
1587 }
1588 SourceMgr.LoadedSLocEntryTable[Index] =
1589 SrcMgr::SLocEntry::getOffsetOnly(*MaybeEntryOffset);
1590 SourceMgr.SLocEntryOffsetLoaded[Index] = true;
1591 }
1592 return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();
1593 });
1594
1595 if (Invalid)
1596 return 0;
1597
1598 // The iterator points to the first entry with start offset greater than the
1599 // offset of interest. The previous entry must contain the offset of interest.
1600 return F->SLocEntryBaseID + *std::prev(It);
1601}
1602
1604 if (ID == 0)
1605 return false;
1606
1607 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1608 Error("source location entry ID out-of-range for AST file");
1609 return true;
1610 }
1611
1612 // Local helper to read the (possibly-compressed) buffer data following the
1613 // entry record.
1614 auto ReadBuffer = [this](
1615 BitstreamCursor &SLocEntryCursor,
1616 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1618 StringRef Blob;
1619 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1620 if (!MaybeCode) {
1621 Error(MaybeCode.takeError());
1622 return nullptr;
1623 }
1624 unsigned Code = MaybeCode.get();
1625
1626 Expected<unsigned> MaybeRecCode =
1627 SLocEntryCursor.readRecord(Code, Record, &Blob);
1628 if (!MaybeRecCode) {
1629 Error(MaybeRecCode.takeError());
1630 return nullptr;
1631 }
1632 unsigned RecCode = MaybeRecCode.get();
1633
1634 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1635 // Inspect the first byte to differentiate zlib (\x78) and zstd
1636 // (little-endian 0xFD2FB528).
1637 const llvm::compression::Format F =
1638 Blob.size() > 0 && Blob.data()[0] == 0x78
1639 ? llvm::compression::Format::Zlib
1640 : llvm::compression::Format::Zstd;
1641 if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {
1642 Error(Reason);
1643 return nullptr;
1644 }
1645 SmallVector<uint8_t, 0> Decompressed;
1646 if (llvm::Error E = llvm::compression::decompress(
1647 F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {
1648 Error("could not decompress embedded file contents: " +
1649 llvm::toString(std::move(E)));
1650 return nullptr;
1651 }
1652 return llvm::MemoryBuffer::getMemBufferCopy(
1653 llvm::toStringRef(Decompressed), Name);
1654 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1655 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1656 } else {
1657 Error("AST record has invalid code");
1658 return nullptr;
1659 }
1660 };
1661
1662 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1663 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1665 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1666 Error(std::move(Err));
1667 return true;
1668 }
1669
1670 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1672
1673 ++NumSLocEntriesRead;
1674 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1675 if (!MaybeEntry) {
1676 Error(MaybeEntry.takeError());
1677 return true;
1678 }
1679 llvm::BitstreamEntry Entry = MaybeEntry.get();
1680
1681 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1682 Error("incorrectly-formatted source location entry in AST file");
1683 return true;
1684 }
1685
1687 StringRef Blob;
1688 Expected<unsigned> MaybeSLOC =
1689 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1690 if (!MaybeSLOC) {
1691 Error(MaybeSLOC.takeError());
1692 return true;
1693 }
1694 switch (MaybeSLOC.get()) {
1695 default:
1696 Error("incorrectly-formatted source location entry in AST file");
1697 return true;
1698
1699 case SM_SLOC_FILE_ENTRY: {
1700 // We will detect whether a file changed and return 'Failure' for it, but
1701 // we will also try to fail gracefully by setting up the SLocEntry.
1702 unsigned InputID = Record[4];
1703 InputFile IF = getInputFile(*F, InputID);
1705 bool OverriddenBuffer = IF.isOverridden();
1706
1707 // Note that we only check if a File was returned. If it was out-of-date
1708 // we have complained but we will continue creating a FileID to recover
1709 // gracefully.
1710 if (!File)
1711 return true;
1712
1713 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1714 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1715 // This is the module's main file.
1716 IncludeLoc = getImportLocation(F);
1717 }
1719 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1720 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1721 BaseOffset + Record[0]);
1722 SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1723 FileInfo.NumCreatedFIDs = Record[5];
1724 if (Record[3])
1725 FileInfo.setHasLineDirectives();
1726
1727 unsigned NumFileDecls = Record[7];
1728 if (NumFileDecls && ContextObj) {
1729 const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];
1730 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1731 FileDeclIDs[FID] =
1732 FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));
1733 }
1734
1735 const SrcMgr::ContentCache &ContentCache =
1736 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1737 if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1738 ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1739 !ContentCache.getBufferIfLoaded()) {
1740 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1741 if (!Buffer)
1742 return true;
1743 SourceMgr.overrideFileContents(*File, std::move(Buffer));
1744 }
1745
1746 break;
1747 }
1748
1749 case SM_SLOC_BUFFER_ENTRY: {
1750 const char *Name = Blob.data();
1751 unsigned Offset = Record[0];
1753 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1754 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1755 if (IncludeLoc.isInvalid() && F->isModule()) {
1756 IncludeLoc = getImportLocation(F);
1757 }
1758
1759 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1760 if (!Buffer)
1761 return true;
1762 FileID FID = SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1763 BaseOffset + Offset, IncludeLoc);
1764 if (Record[3]) {
1765 auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();
1766 FileInfo.setHasLineDirectives();
1767 }
1768 break;
1769 }
1770
1773 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq);
1774 SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq);
1775 SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq);
1776 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,
1777 Record[5], Record[4], ID,
1778 BaseOffset + Record[0]);
1779 break;
1780 }
1781 }
1782
1783 return false;
1784}
1785
1786std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1787 if (ID == 0)
1788 return std::make_pair(SourceLocation(), "");
1789
1790 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1791 Error("source location entry ID out-of-range for AST file");
1792 return std::make_pair(SourceLocation(), "");
1793 }
1794
1795 // Find which module file this entry lands in.
1796 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1797 if (!M->isModule())
1798 return std::make_pair(SourceLocation(), "");
1799
1800 // FIXME: Can we map this down to a particular submodule? That would be
1801 // ideal.
1802 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1803}
1804
1805/// Find the location where the module F is imported.
1806SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1807 if (F->ImportLoc.isValid())
1808 return F->ImportLoc;
1809
1810 // Otherwise we have a PCH. It's considered to be "imported" at the first
1811 // location of its includer.
1812 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1813 // Main file is the importer.
1814 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1815 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1816 }
1817 return F->ImportedBy[0]->FirstLoc;
1818}
1819
1820/// Enter a subblock of the specified BlockID with the specified cursor. Read
1821/// the abbreviations that are at the top of the block and then leave the cursor
1822/// pointing into the block.
1823llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1824 unsigned BlockID,
1825 uint64_t *StartOfBlockOffset) {
1826 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1827 return Err;
1828
1829 if (StartOfBlockOffset)
1830 *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1831
1832 while (true) {
1833 uint64_t Offset = Cursor.GetCurrentBitNo();
1834 Expected<unsigned> MaybeCode = Cursor.ReadCode();
1835 if (!MaybeCode)
1836 return MaybeCode.takeError();
1837 unsigned Code = MaybeCode.get();
1838
1839 // We expect all abbrevs to be at the start of the block.
1840 if (Code != llvm::bitc::DEFINE_ABBREV) {
1841 if (llvm::Error Err = Cursor.JumpToBit(Offset))
1842 return Err;
1843 return llvm::Error::success();
1844 }
1845 if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1846 return Err;
1847 }
1848}
1849
1851 unsigned &Idx) {
1852 Token Tok;
1853 Tok.startToken();
1854 Tok.setLocation(ReadSourceLocation(M, Record, Idx));
1855 Tok.setKind((tok::TokenKind)Record[Idx++]);
1856 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1857
1858 if (Tok.isAnnotation()) {
1859 Tok.setAnnotationEndLoc(ReadSourceLocation(M, Record, Idx));
1860 switch (Tok.getKind()) {
1861 case tok::annot_pragma_loop_hint: {
1862 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1863 Info->PragmaName = ReadToken(M, Record, Idx);
1864 Info->Option = ReadToken(M, Record, Idx);
1865 unsigned NumTokens = Record[Idx++];
1867 Toks.reserve(NumTokens);
1868 for (unsigned I = 0; I < NumTokens; ++I)
1869 Toks.push_back(ReadToken(M, Record, Idx));
1870 Info->Toks = llvm::ArrayRef(Toks).copy(PP.getPreprocessorAllocator());
1871 Tok.setAnnotationValue(static_cast<void *>(Info));
1872 break;
1873 }
1874 case tok::annot_pragma_pack: {
1875 auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;
1876 Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);
1877 auto SlotLabel = ReadString(Record, Idx);
1878 Info->SlotLabel =
1879 llvm::StringRef(SlotLabel).copy(PP.getPreprocessorAllocator());
1880 Info->Alignment = ReadToken(M, Record, Idx);
1881 Tok.setAnnotationValue(static_cast<void *>(Info));
1882 break;
1883 }
1884 // Some annotation tokens do not use the PtrData field.
1885 case tok::annot_pragma_openmp:
1886 case tok::annot_pragma_openmp_end:
1887 case tok::annot_pragma_unused:
1888 case tok::annot_pragma_openacc:
1889 case tok::annot_pragma_openacc_end:
1890 case tok::annot_repl_input_end:
1891 break;
1892 default:
1893 llvm_unreachable("missing deserialization code for annotation token");
1894 }
1895 } else {
1896 Tok.setLength(Record[Idx++]);
1897 if (IdentifierInfo *II = getLocalIdentifier(M, Record[Idx++]))
1898 Tok.setIdentifierInfo(II);
1899 }
1900 return Tok;
1901}
1902
1904 BitstreamCursor &Stream = F.MacroCursor;
1905
1906 // Keep track of where we are in the stream, then jump back there
1907 // after reading this macro.
1908 SavedStreamPosition SavedPosition(Stream);
1909
1910 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1911 // FIXME this drops errors on the floor.
1912 consumeError(std::move(Err));
1913 return nullptr;
1914 }
1917 MacroInfo *Macro = nullptr;
1918 llvm::MutableArrayRef<Token> MacroTokens;
1919
1920 while (true) {
1921 // Advance to the next record, but if we get to the end of the block, don't
1922 // pop it (removing all the abbreviations from the cursor) since we want to
1923 // be able to reseek within the block and read entries.
1924 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1926 Stream.advanceSkippingSubblocks(Flags);
1927 if (!MaybeEntry) {
1928 Error(MaybeEntry.takeError());
1929 return Macro;
1930 }
1931 llvm::BitstreamEntry Entry = MaybeEntry.get();
1932
1933 switch (Entry.Kind) {
1934 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1935 case llvm::BitstreamEntry::Error:
1936 Error("malformed block record in AST file");
1937 return Macro;
1938 case llvm::BitstreamEntry::EndBlock:
1939 return Macro;
1940 case llvm::BitstreamEntry::Record:
1941 // The interesting case.
1942 break;
1943 }
1944
1945 // Read a record.
1946 Record.clear();
1948 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1949 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1950 else {
1951 Error(MaybeRecType.takeError());
1952 return Macro;
1953 }
1954 switch (RecType) {
1955 case PP_MODULE_MACRO:
1957 return Macro;
1958
1961 // If we already have a macro, that means that we've hit the end
1962 // of the definition of the macro we were looking for. We're
1963 // done.
1964 if (Macro)
1965 return Macro;
1966
1967 unsigned NextIndex = 1; // Skip identifier ID.
1968 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1969 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1970 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1971 MI->setIsUsed(Record[NextIndex++]);
1972 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1973 MacroTokens = MI->allocateTokens(Record[NextIndex++],
1974 PP.getPreprocessorAllocator());
1975 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1976 // Decode function-like macro info.
1977 bool isC99VarArgs = Record[NextIndex++];
1978 bool isGNUVarArgs = Record[NextIndex++];
1979 bool hasCommaPasting = Record[NextIndex++];
1980 MacroParams.clear();
1981 unsigned NumArgs = Record[NextIndex++];
1982 for (unsigned i = 0; i != NumArgs; ++i)
1983 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1984
1985 // Install function-like macro info.
1986 MI->setIsFunctionLike();
1987 if (isC99VarArgs) MI->setIsC99Varargs();
1988 if (isGNUVarArgs) MI->setIsGNUVarargs();
1989 if (hasCommaPasting) MI->setHasCommaPasting();
1990 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1991 }
1992
1993 // Remember that we saw this macro last so that we add the tokens that
1994 // form its body to it.
1995 Macro = MI;
1996
1997 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1998 Record[NextIndex]) {
1999 // We have a macro definition. Register the association
2001 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
2002 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2003 PreprocessingRecord::PPEntityID PPID =
2004 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
2005 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2006 PPRec.getPreprocessedEntity(PPID));
2007 if (PPDef)
2008 PPRec.RegisterMacroDefinition(Macro, PPDef);
2009 }
2010
2011 ++NumMacrosRead;
2012 break;
2013 }
2014
2015 case PP_TOKEN: {
2016 // If we see a TOKEN before a PP_MACRO_*, then the file is
2017 // erroneous, just pretend we didn't see this.
2018 if (!Macro) break;
2019 if (MacroTokens.empty()) {
2020 Error("unexpected number of macro tokens for a macro in AST file");
2021 return Macro;
2022 }
2023
2024 unsigned Idx = 0;
2025 MacroTokens[0] = ReadToken(F, Record, Idx);
2026 MacroTokens = MacroTokens.drop_front();
2027 break;
2028 }
2029 }
2030 }
2031}
2032
2035 unsigned LocalID) const {
2036 if (!M.ModuleOffsetMap.empty())
2037 ReadModuleOffsetMap(M);
2038
2041 assert(I != M.PreprocessedEntityRemap.end()
2042 && "Invalid index into preprocessed entity index remap");
2043
2044 return LocalID + I->second;
2045}
2046
2047const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2048 FileManager &FileMgr = Reader.getFileManager();
2049 if (!Key.Imported) {
2050 if (auto File = FileMgr.getFile(Key.Filename))
2051 return *File;
2052 return nullptr;
2053 }
2054
2055 std::string Resolved = std::string(Key.Filename);
2056 Reader.ResolveImportedPath(M, Resolved);
2057 if (auto File = FileMgr.getFile(Resolved))
2058 return *File;
2059 return nullptr;
2060}
2061
2062unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2063 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2064 memcpy(buf, &ikey.Size, sizeof(ikey.Size));
2065 memcpy(buf + sizeof(ikey.Size), &ikey.ModTime, sizeof(ikey.ModTime));
2066 return llvm::xxh3_64bits(buf);
2067}
2068
2070HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2071 internal_key_type ikey = {ekey.getSize(),
2072 M.HasTimestamps ? ekey.getModificationTime() : 0,
2073 ekey.getName(), /*Imported*/ false};
2074 return ikey;
2075}
2076
2077bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2078 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2079 return false;
2080
2081 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
2082 return true;
2083
2084 // Determine whether the actual files are equivalent.
2085 const FileEntry *FEA = getFile(a);
2086 const FileEntry *FEB = getFile(b);
2087 return FEA && FEA == FEB;
2088}
2089
2090std::pair<unsigned, unsigned>
2091HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2092 return readULEBKeyDataLength(d);
2093}
2094
2096HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2097 using namespace llvm::support;
2098
2099 internal_key_type ikey;
2100 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(d));
2101 ikey.ModTime =
2102 time_t(endian::readNext<uint64_t, llvm::endianness::little>(d));
2103 ikey.Filename = (const char *)d;
2104 ikey.Imported = true;
2105 return ikey;
2106}
2107
2109HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2110 unsigned DataLen) {
2111 using namespace llvm::support;
2112
2113 const unsigned char *End = d + DataLen;
2114 HeaderFileInfo HFI;
2115 unsigned Flags = *d++;
2116
2117 bool Included = (Flags >> 6) & 0x01;
2118 if (Included)
2119 if (const FileEntry *FE = getFile(key))
2120 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2121 // deserialize this header file info again.
2122 Reader.getPreprocessor().getIncludedFiles().insert(FE);
2123
2124 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2125 HFI.isImport |= (Flags >> 5) & 0x01;
2126 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2127 HFI.DirInfo = (Flags >> 1) & 0x07;
2128 HFI.IndexHeaderMapHeader = Flags & 0x01;
2129 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2130 M, endian::readNext<IdentifierID, llvm::endianness::little>(d));
2131 if (unsigned FrameworkOffset =
2132 endian::readNext<uint32_t, llvm::endianness::little>(d)) {
2133 // The framework offset is 1 greater than the actual offset,
2134 // since 0 is used as an indicator for "no framework name".
2135 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2136 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
2137 }
2138
2139 assert((End - d) % 4 == 0 &&
2140 "Wrong data length in HeaderFileInfo deserialization");
2141 while (d != End) {
2142 uint32_t LocalSMID =
2143 endian::readNext<uint32_t, llvm::endianness::little>(d);
2144 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2145 LocalSMID >>= 3;
2146
2147 // This header is part of a module. Associate it with the module to enable
2148 // implicit module import.
2149 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
2150 Module *Mod = Reader.getSubmodule(GlobalSMID);
2151 FileManager &FileMgr = Reader.getFileManager();
2152 ModuleMap &ModMap =
2153 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2154
2155 std::string Filename = std::string(key.Filename);
2156 if (key.Imported)
2157 Reader.ResolveImportedPath(M, Filename);
2158 if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2159 // FIXME: NameAsWritten
2160 Module::Header H = {std::string(key.Filename), "", *FE};
2161 ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);
2162 }
2163 HFI.mergeModuleMembership(HeaderRole);
2164 }
2165
2166 // This HeaderFileInfo was externally loaded.
2167 HFI.External = true;
2168 HFI.IsValid = true;
2169 return HFI;
2170}
2171
2173 uint32_t MacroDirectivesOffset) {
2174 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2175 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
2176}
2177
2179 // Note that we are loading defined macros.
2180 Deserializing Macros(this);
2181
2182 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
2183 BitstreamCursor &MacroCursor = I.MacroCursor;
2184
2185 // If there was no preprocessor block, skip this file.
2186 if (MacroCursor.getBitcodeBytes().empty())
2187 continue;
2188
2189 BitstreamCursor Cursor = MacroCursor;
2190 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
2191 Error(std::move(Err));
2192 return;
2193 }
2194
2196 while (true) {
2197 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2198 if (!MaybeE) {
2199 Error(MaybeE.takeError());
2200 return;
2201 }
2202 llvm::BitstreamEntry E = MaybeE.get();
2203
2204 switch (E.Kind) {
2205 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2206 case llvm::BitstreamEntry::Error:
2207 Error("malformed block record in AST file");
2208 return;
2209 case llvm::BitstreamEntry::EndBlock:
2210 goto NextCursor;
2211
2212 case llvm::BitstreamEntry::Record: {
2213 Record.clear();
2214 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2215 if (!MaybeRecord) {
2216 Error(MaybeRecord.takeError());
2217 return;
2218 }
2219 switch (MaybeRecord.get()) {
2220 default: // Default behavior: ignore.
2221 break;
2222
2225 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2226 if (II->isOutOfDate())
2227 updateOutOfDateIdentifier(*II);
2228 break;
2229 }
2230
2231 case PP_TOKEN:
2232 // Ignore tokens.
2233 break;
2234 }
2235 break;
2236 }
2237 }
2238 }
2239 NextCursor: ;
2240 }
2241}
2242
2243namespace {
2244
2245 /// Visitor class used to look up identifirs in an AST file.
2246 class IdentifierLookupVisitor {
2247 StringRef Name;
2248 unsigned NameHash;
2249 unsigned PriorGeneration;
2250 unsigned &NumIdentifierLookups;
2251 unsigned &NumIdentifierLookupHits;
2252 IdentifierInfo *Found = nullptr;
2253
2254 public:
2255 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2256 unsigned &NumIdentifierLookups,
2257 unsigned &NumIdentifierLookupHits)
2258 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2259 PriorGeneration(PriorGeneration),
2260 NumIdentifierLookups(NumIdentifierLookups),
2261 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2262
2263 bool operator()(ModuleFile &M) {
2264 // If we've already searched this module file, skip it now.
2265 if (M.Generation <= PriorGeneration)
2266 return true;
2267
2270 if (!IdTable)
2271 return false;
2272
2273 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2274 Found);
2275 ++NumIdentifierLookups;
2276 ASTIdentifierLookupTable::iterator Pos =
2277 IdTable->find_hashed(Name, NameHash, &Trait);
2278 if (Pos == IdTable->end())
2279 return false;
2280
2281 // Dereferencing the iterator has the effect of building the
2282 // IdentifierInfo node and populating it with the various
2283 // declarations it needs.
2284 ++NumIdentifierLookupHits;
2285 Found = *Pos;
2286 return true;
2287 }
2288
2289 // Retrieve the identifier info found within the module
2290 // files.
2291 IdentifierInfo *getIdentifierInfo() const { return Found; }
2292 };
2293
2294} // namespace
2295
2297 // Note that we are loading an identifier.
2298 Deserializing AnIdentifier(this);
2299
2300 unsigned PriorGeneration = 0;
2301 if (getContext().getLangOpts().Modules)
2302 PriorGeneration = IdentifierGeneration[&II];
2303
2304 // If there is a global index, look there first to determine which modules
2305 // provably do not have any results for this identifier.
2307 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2308 if (!loadGlobalIndex()) {
2309 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2310 HitsPtr = &Hits;
2311 }
2312 }
2313
2314 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2315 NumIdentifierLookups,
2316 NumIdentifierLookupHits);
2317 ModuleMgr.visit(Visitor, HitsPtr);
2318 markIdentifierUpToDate(&II);
2319}
2320
2322 if (!II)
2323 return;
2324
2325 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2326
2327 // Update the generation for this identifier.
2328 if (getContext().getLangOpts().Modules)
2329 IdentifierGeneration[II] = getGeneration();
2330}
2331
2333 const PendingMacroInfo &PMInfo) {
2334 ModuleFile &M = *PMInfo.M;
2335
2336 BitstreamCursor &Cursor = M.MacroCursor;
2337 SavedStreamPosition SavedPosition(Cursor);
2338 if (llvm::Error Err =
2339 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2340 Error(std::move(Err));
2341 return;
2342 }
2343
2344 struct ModuleMacroRecord {
2345 SubmoduleID SubModID;
2346 MacroInfo *MI;
2348 };
2350
2351 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2352 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2353 // macro histroy.
2355 while (true) {
2357 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2358 if (!MaybeEntry) {
2359 Error(MaybeEntry.takeError());
2360 return;
2361 }
2362 llvm::BitstreamEntry Entry = MaybeEntry.get();
2363
2364 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2365 Error("malformed block record in AST file");
2366 return;
2367 }
2368
2369 Record.clear();
2370 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2371 if (!MaybePP) {
2372 Error(MaybePP.takeError());
2373 return;
2374 }
2375 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2377 break;
2378
2379 case PP_MODULE_MACRO: {
2380 ModuleMacros.push_back(ModuleMacroRecord());
2381 auto &Info = ModuleMacros.back();
2382 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2383 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2384 for (int I = 2, N = Record.size(); I != N; ++I)
2385 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2386 continue;
2387 }
2388
2389 default:
2390 Error("malformed block record in AST file");
2391 return;
2392 }
2393
2394 // We found the macro directive history; that's the last record
2395 // for this macro.
2396 break;
2397 }
2398
2399 // Module macros are listed in reverse dependency order.
2400 {
2401 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2403 for (auto &MMR : ModuleMacros) {
2404 Overrides.clear();
2405 for (unsigned ModID : MMR.Overrides) {
2406 Module *Mod = getSubmodule(ModID);
2407 auto *Macro = PP.getModuleMacro(Mod, II);
2408 assert(Macro && "missing definition for overridden macro");
2409 Overrides.push_back(Macro);
2410 }
2411
2412 bool Inserted = false;
2413 Module *Owner = getSubmodule(MMR.SubModID);
2414 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2415 }
2416 }
2417
2418 // Don't read the directive history for a module; we don't have anywhere
2419 // to put it.
2420 if (M.isModule())
2421 return;
2422
2423 // Deserialize the macro directives history in reverse source-order.
2424 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2425 unsigned Idx = 0, N = Record.size();
2426 while (Idx < N) {
2427 MacroDirective *MD = nullptr;
2428 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2430 switch (K) {
2432 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2433 MD = PP.AllocateDefMacroDirective(MI, Loc);
2434 break;
2435 }
2437 MD = PP.AllocateUndefMacroDirective(Loc);
2438 break;
2440 bool isPublic = Record[Idx++];
2441 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2442 break;
2443 }
2444
2445 if (!Latest)
2446 Latest = MD;
2447 if (Earliest)
2448 Earliest->setPrevious(MD);
2449 Earliest = MD;
2450 }
2451
2452 if (Latest)
2453 PP.setLoadedMacroDirective(II, Earliest, Latest);
2454}
2455
2456bool ASTReader::shouldDisableValidationForFile(
2457 const serialization::ModuleFile &M) const {
2458 if (DisableValidationKind == DisableValidationForModuleKind::None)
2459 return false;
2460
2461 // If a PCH is loaded and validation is disabled for PCH then disable
2462 // validation for the PCH and the modules it loads.
2463 ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2464
2465 switch (K) {
2466 case MK_MainFile:
2467 case MK_Preamble:
2468 case MK_PCH:
2469 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2470 case MK_ImplicitModule:
2471 case MK_ExplicitModule:
2472 case MK_PrebuiltModule:
2473 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2474 }
2475
2476 return false;
2477}
2478
2479InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2480 // If this ID is bogus, just return an empty input file.
2481 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2482 return InputFileInfo();
2483
2484 // If we've already loaded this input file, return it.
2485 if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2486 return F.InputFileInfosLoaded[ID - 1];
2487
2488 // Go find this input file.
2489 BitstreamCursor &Cursor = F.InputFilesCursor;
2490 SavedStreamPosition SavedPosition(Cursor);
2491 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2492 F.InputFileOffsets[ID - 1])) {
2493 // FIXME this drops errors on the floor.
2494 consumeError(std::move(Err));
2495 }
2496
2497 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2498 if (!MaybeCode) {
2499 // FIXME this drops errors on the floor.
2500 consumeError(MaybeCode.takeError());
2501 }
2502 unsigned Code = MaybeCode.get();
2503 RecordData Record;
2504 StringRef Blob;
2505
2506 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2507 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2508 "invalid record type for input file");
2509 else {
2510 // FIXME this drops errors on the floor.
2511 consumeError(Maybe.takeError());
2512 }
2513
2514 assert(Record[0] == ID && "Bogus stored ID or offset");
2515 InputFileInfo R;
2516 R.StoredSize = static_cast<off_t>(Record[1]);
2517 R.StoredTime = static_cast<time_t>(Record[2]);
2518 R.Overridden = static_cast<bool>(Record[3]);
2519 R.Transient = static_cast<bool>(Record[4]);
2520 R.TopLevel = static_cast<bool>(Record[5]);
2521 R.ModuleMap = static_cast<bool>(Record[6]);
2522 std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2523 uint16_t AsRequestedLength = Record[7];
2524
2525 std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2526 std::string Name = Blob.substr(AsRequestedLength).str();
2527
2528 ResolveImportedPath(F, NameAsRequested);
2529 ResolveImportedPath(F, Name);
2530
2531 if (Name.empty())
2532 Name = NameAsRequested;
2533
2534 return std::make_pair(std::move(NameAsRequested), std::move(Name));
2535 }();
2536
2537 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2538 if (!MaybeEntry) // FIXME this drops errors on the floor.
2539 consumeError(MaybeEntry.takeError());
2540 llvm::BitstreamEntry Entry = MaybeEntry.get();
2541 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2542 "expected record type for input file hash");
2543
2544 Record.clear();
2545 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2546 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2547 "invalid record type for input file hash");
2548 else {
2549 // FIXME this drops errors on the floor.
2550 consumeError(Maybe.takeError());
2551 }
2552 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2553 static_cast<uint64_t>(Record[0]);
2554
2555 // Note that we've loaded this input file info.
2556 F.InputFileInfosLoaded[ID - 1] = R;
2557 return R;
2558}
2559
2560static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2561InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2562 // If this ID is bogus, just return an empty input file.
2563 if (ID == 0 || ID > F.InputFilesLoaded.size())
2564 return InputFile();
2565
2566 // If we've already loaded this input file, return it.
2567 if (F.InputFilesLoaded[ID-1].getFile())
2568 return F.InputFilesLoaded[ID-1];
2569
2570 if (F.InputFilesLoaded[ID-1].isNotFound())
2571 return InputFile();
2572
2573 // Go find this input file.
2574 BitstreamCursor &Cursor = F.InputFilesCursor;
2575 SavedStreamPosition SavedPosition(Cursor);
2576 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2577 F.InputFileOffsets[ID - 1])) {
2578 // FIXME this drops errors on the floor.
2579 consumeError(std::move(Err));
2580 }
2581
2582 InputFileInfo FI = getInputFileInfo(F, ID);
2583 off_t StoredSize = FI.StoredSize;
2584 time_t StoredTime = FI.StoredTime;
2585 bool Overridden = FI.Overridden;
2586 bool Transient = FI.Transient;
2587 StringRef Filename = FI.FilenameAsRequested;
2588 uint64_t StoredContentHash = FI.ContentHash;
2589
2590 // For standard C++ modules, we don't need to check the inputs.
2591 bool SkipChecks = F.StandardCXXModule;
2592
2593 const HeaderSearchOptions &HSOpts =
2594 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2595
2596 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2597 // modules.
2599 SkipChecks = false;
2600 Overridden = false;
2601 }
2602
2603 auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
2604
2605 // For an overridden file, create a virtual file with the stored
2606 // size/timestamp.
2607 if ((Overridden || Transient || SkipChecks) && !File)
2608 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2609
2610 if (!File) {
2611 if (Complain) {
2612 std::string ErrorStr = "could not find file '";
2613 ErrorStr += Filename;
2614 ErrorStr += "' referenced by AST file '";
2615 ErrorStr += F.FileName;
2616 ErrorStr += "'";
2617 Error(ErrorStr);
2618 }
2619 // Record that we didn't find the file.
2621 return InputFile();
2622 }
2623
2624 // Check if there was a request to override the contents of the file
2625 // that was part of the precompiled header. Overriding such a file
2626 // can lead to problems when lexing using the source locations from the
2627 // PCH.
2628 SourceManager &SM = getSourceManager();
2629 // FIXME: Reject if the overrides are different.
2630 if ((!Overridden && !Transient) && !SkipChecks &&
2631 SM.isFileOverridden(*File)) {
2632 if (Complain)
2633 Error(diag::err_fe_pch_file_overridden, Filename);
2634
2635 // After emitting the diagnostic, bypass the overriding file to recover
2636 // (this creates a separate FileEntry).
2637 File = SM.bypassFileContentsOverride(*File);
2638 if (!File) {
2640 return InputFile();
2641 }
2642 }
2643
2644 struct Change {
2645 enum ModificationKind {
2646 Size,
2647 ModTime,
2648 Content,
2649 None,
2650 } Kind;
2651 std::optional<int64_t> Old = std::nullopt;
2652 std::optional<int64_t> New = std::nullopt;
2653 };
2654 auto HasInputContentChanged = [&](Change OriginalChange) {
2655 assert(ValidateASTInputFilesContent &&
2656 "We should only check the content of the inputs with "
2657 "ValidateASTInputFilesContent enabled.");
2658
2659 if (StoredContentHash == 0)
2660 return OriginalChange;
2661
2662 auto MemBuffOrError = FileMgr.getBufferForFile(*File);
2663 if (!MemBuffOrError) {
2664 if (!Complain)
2665 return OriginalChange;
2666 std::string ErrorStr = "could not get buffer for file '";
2667 ErrorStr += File->getName();
2668 ErrorStr += "'";
2669 Error(ErrorStr);
2670 return OriginalChange;
2671 }
2672
2673 auto ContentHash = xxh3_64bits(MemBuffOrError.get()->getBuffer());
2674 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2675 return Change{Change::None};
2676
2677 return Change{Change::Content};
2678 };
2679 auto HasInputFileChanged = [&]() {
2680 if (StoredSize != File->getSize())
2681 return Change{Change::Size, StoredSize, File->getSize()};
2682 if (!shouldDisableValidationForFile(F) && StoredTime &&
2683 StoredTime != File->getModificationTime()) {
2684 Change MTimeChange = {Change::ModTime, StoredTime,
2685 File->getModificationTime()};
2686
2687 // In case the modification time changes but not the content,
2688 // accept the cached file as legit.
2689 if (ValidateASTInputFilesContent)
2690 return HasInputContentChanged(MTimeChange);
2691
2692 return MTimeChange;
2693 }
2694 return Change{Change::None};
2695 };
2696
2697 bool IsOutOfDate = false;
2698 auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();
2699 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2700 // enabled, it is better to check the contents of the inputs. Since we can't
2701 // get correct modified time information for inputs from overriden inputs.
2702 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2703 F.StandardCXXModule && FileChange.Kind == Change::None)
2704 FileChange = HasInputContentChanged(FileChange);
2705
2706 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2707 // it is better to check the content of the input files because we cannot rely
2708 // on the file modification time, which will be the same (zero) for these
2709 // files.
2710 if (!StoredTime && ValidateASTInputFilesContent &&
2711 FileChange.Kind == Change::None)
2712 FileChange = HasInputContentChanged(FileChange);
2713
2714 // For an overridden file, there is nothing to validate.
2715 if (!Overridden && FileChange.Kind != Change::None) {
2716 if (Complain && !Diags.isDiagnosticInFlight()) {
2717 // Build a list of the PCH imports that got us here (in reverse).
2718 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2719 while (!ImportStack.back()->ImportedBy.empty())
2720 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2721
2722 // The top-level PCH is stale.
2723 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2724 Diag(diag::err_fe_ast_file_modified)
2725 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2726 << TopLevelPCHName << FileChange.Kind
2727 << (FileChange.Old && FileChange.New)
2728 << llvm::itostr(FileChange.Old.value_or(0))
2729 << llvm::itostr(FileChange.New.value_or(0));
2730
2731 // Print the import stack.
2732 if (ImportStack.size() > 1) {
2733 Diag(diag::note_pch_required_by)
2734 << Filename << ImportStack[0]->FileName;
2735 for (unsigned I = 1; I < ImportStack.size(); ++I)
2736 Diag(diag::note_pch_required_by)
2737 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2738 }
2739
2740 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2741 }
2742
2743 IsOutOfDate = true;
2744 }
2745 // FIXME: If the file is overridden and we've already opened it,
2746 // issue an error (or split it into a separate FileEntry).
2747
2748 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2749
2750 // Note that we've loaded this input file.
2751 F.InputFilesLoaded[ID-1] = IF;
2752 return IF;
2753}
2754
2755/// If we are loading a relocatable PCH or module file, and the filename
2756/// is not an absolute path, add the system or module root to the beginning of
2757/// the file name.
2759 // Resolve relative to the base directory, if we have one.
2760 if (!M.BaseDirectory.empty())
2761 return ResolveImportedPath(Filename, M.BaseDirectory);
2762}
2763
2764void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2765 if (Filename.empty() || llvm::sys::path::is_absolute(Filename) ||
2766 Filename == "<built-in>" || Filename == "<command line>")
2767 return;
2768
2769 SmallString<128> Buffer;
2770 llvm::sys::path::append(Buffer, Prefix, Filename);
2771 Filename.assign(Buffer.begin(), Buffer.end());
2772}
2773
2774static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2775 switch (ARR) {
2776 case ASTReader::Failure: return true;
2777 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2778 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2781 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2782 case ASTReader::HadErrors: return true;
2783 case ASTReader::Success: return false;
2784 }
2785
2786 llvm_unreachable("unknown ASTReadResult");
2787}
2788
2789ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2790 BitstreamCursor &Stream, StringRef Filename,
2791 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
2792 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
2793 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2794 // FIXME this drops errors on the floor.
2795 consumeError(std::move(Err));
2796 return Failure;
2797 }
2798
2799 // Read all of the records in the options block.
2800 RecordData Record;
2801 ASTReadResult Result = Success;
2802 while (true) {
2803 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2804 if (!MaybeEntry) {
2805 // FIXME this drops errors on the floor.
2806 consumeError(MaybeEntry.takeError());
2807 return Failure;
2808 }
2809 llvm::BitstreamEntry Entry = MaybeEntry.get();
2810
2811 switch (Entry.Kind) {
2812 case llvm::BitstreamEntry::Error:
2813 case llvm::BitstreamEntry::SubBlock:
2814 return Failure;
2815
2816 case llvm::BitstreamEntry::EndBlock:
2817 return Result;
2818
2819 case llvm::BitstreamEntry::Record:
2820 // The interesting case.
2821 break;
2822 }
2823
2824 // Read and process a record.
2825 Record.clear();
2826 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2827 if (!MaybeRecordType) {
2828 // FIXME this drops errors on the floor.
2829 consumeError(MaybeRecordType.takeError());
2830 return Failure;
2831 }
2832 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2833 case LANGUAGE_OPTIONS: {
2834 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2835 if (ParseLanguageOptions(Record, Filename, Complain, Listener,
2836 AllowCompatibleConfigurationMismatch))
2837 Result = ConfigurationMismatch;
2838 break;
2839 }
2840
2841 case TARGET_OPTIONS: {
2842 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2843 if (ParseTargetOptions(Record, Filename, Complain, Listener,
2844 AllowCompatibleConfigurationMismatch))
2845 Result = ConfigurationMismatch;
2846 break;
2847 }
2848
2849 case FILE_SYSTEM_OPTIONS: {
2850 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2851 if (!AllowCompatibleConfigurationMismatch &&
2852 ParseFileSystemOptions(Record, Complain, Listener))
2853 Result = ConfigurationMismatch;
2854 break;
2855 }
2856
2857 case HEADER_SEARCH_OPTIONS: {
2858 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2859 if (!AllowCompatibleConfigurationMismatch &&
2860 ParseHeaderSearchOptions(Record, Filename, Complain, Listener))
2861 Result = ConfigurationMismatch;
2862 break;
2863 }
2864
2866 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2867 if (!AllowCompatibleConfigurationMismatch &&
2868 ParsePreprocessorOptions(Record, Filename, Complain, Listener,
2869 SuggestedPredefines))
2870 Result = ConfigurationMismatch;
2871 break;
2872 }
2873 }
2874}
2875
2877ASTReader::ReadControlBlock(ModuleFile &F,
2879 const ModuleFile *ImportedBy,
2880 unsigned ClientLoadCapabilities) {
2881 BitstreamCursor &Stream = F.Stream;
2882
2883 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2884 Error(std::move(Err));
2885 return Failure;
2886 }
2887
2888 // Lambda to read the unhashed control block the first time it's called.
2889 //
2890 // For PCM files, the unhashed control block cannot be read until after the
2891 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2892 // need to look ahead before reading the IMPORTS record. For consistency,
2893 // this block is always read somehow (see BitstreamEntry::EndBlock).
2894 bool HasReadUnhashedControlBlock = false;
2895 auto readUnhashedControlBlockOnce = [&]() {
2896 if (!HasReadUnhashedControlBlock) {
2897 HasReadUnhashedControlBlock = true;
2898 if (ASTReadResult Result =
2899 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2900 return Result;
2901 }
2902 return Success;
2903 };
2904
2905 bool DisableValidation = shouldDisableValidationForFile(F);
2906
2907 // Read all of the records and blocks in the control block.
2908 RecordData Record;
2909 unsigned NumInputs = 0;
2910 unsigned NumUserInputs = 0;
2911 StringRef BaseDirectoryAsWritten;
2912 while (true) {
2913 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2914 if (!MaybeEntry) {
2915 Error(MaybeEntry.takeError());
2916 return Failure;
2917 }
2918 llvm::BitstreamEntry Entry = MaybeEntry.get();
2919
2920 switch (Entry.Kind) {
2921 case llvm::BitstreamEntry::Error:
2922 Error("malformed block record in AST file");
2923 return Failure;
2924 case llvm::BitstreamEntry::EndBlock: {
2925 // Validate the module before returning. This call catches an AST with
2926 // no module name and no imports.
2927 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2928 return Result;
2929
2930 // Validate input files.
2931 const HeaderSearchOptions &HSOpts =
2932 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2933
2934 // All user input files reside at the index range [0, NumUserInputs), and
2935 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2936 // loaded module files, ignore missing inputs.
2937 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2938 F.Kind != MK_PrebuiltModule) {
2939 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2940
2941 // If we are reading a module, we will create a verification timestamp,
2942 // so we verify all input files. Otherwise, verify only user input
2943 // files.
2944
2945 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2949 N = NumUserInputs;
2950
2951 for (unsigned I = 0; I < N; ++I) {
2952 InputFile IF = getInputFile(F, I+1, Complain);
2953 if (!IF.getFile() || IF.isOutOfDate())
2954 return OutOfDate;
2955 }
2956 }
2957
2958 if (Listener)
2959 Listener->visitModuleFile(F.FileName, F.Kind);
2960
2961 if (Listener && Listener->needsInputFileVisitation()) {
2962 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2963 : NumUserInputs;
2964 for (unsigned I = 0; I < N; ++I) {
2965 bool IsSystem = I >= NumUserInputs;
2966 InputFileInfo FI = getInputFileInfo(F, I + 1);
2967 Listener->visitInputFile(
2968 FI.FilenameAsRequested, IsSystem, FI.Overridden,
2970 }
2971 }
2972
2973 return Success;
2974 }
2975
2976 case llvm::BitstreamEntry::SubBlock:
2977 switch (Entry.ID) {
2979 F.InputFilesCursor = Stream;
2980 if (llvm::Error Err = Stream.SkipBlock()) {
2981 Error(std::move(Err));
2982 return Failure;
2983 }
2984 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2985 Error("malformed block record in AST file");
2986 return Failure;
2987 }
2988 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2989 continue;
2990
2991 case OPTIONS_BLOCK_ID:
2992 // If we're reading the first module for this group, check its options
2993 // are compatible with ours. For modules it imports, no further checking
2994 // is required, because we checked them when we built it.
2995 if (Listener && !ImportedBy) {
2996 // Should we allow the configuration of the module file to differ from
2997 // the configuration of the current translation unit in a compatible
2998 // way?
2999 //
3000 // FIXME: Allow this for files explicitly specified with -include-pch.
3001 bool AllowCompatibleConfigurationMismatch =
3003
3004 ASTReadResult Result =
3005 ReadOptionsBlock(Stream, F.FileName, ClientLoadCapabilities,
3006 AllowCompatibleConfigurationMismatch, *Listener,
3007 SuggestedPredefines);
3008 if (Result == Failure) {
3009 Error("malformed block record in AST file");
3010 return Result;
3011 }
3012
3013 if (DisableValidation ||
3014 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3015 Result = Success;
3016
3017 // If we can't load the module, exit early since we likely
3018 // will rebuild the module anyway. The stream may be in the
3019 // middle of a block.
3020 if (Result != Success)
3021 return Result;
3022 } else if (llvm::Error Err = Stream.SkipBlock()) {
3023 Error(std::move(Err));
3024 return Failure;
3025 }
3026 continue;
3027
3028 default:
3029 if (llvm::Error Err = Stream.SkipBlock()) {
3030 Error(std::move(Err));
3031 return Failure;
3032 }
3033 continue;
3034 }
3035
3036 case llvm::BitstreamEntry::Record:
3037 // The interesting case.
3038 break;
3039 }
3040
3041 // Read and process a record.
3042 Record.clear();
3043 StringRef Blob;
3044 Expected<unsigned> MaybeRecordType =
3045 Stream.readRecord(Entry.ID, Record, &Blob);
3046 if (!MaybeRecordType) {
3047 Error(MaybeRecordType.takeError());
3048 return Failure;
3049 }
3050 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3051 case METADATA: {
3052 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3053 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3054 Diag(Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3055 : diag::err_ast_file_version_too_new)
3057 return VersionMismatch;
3058 }
3059
3060 bool hasErrors = Record[7];
3061 if (hasErrors && !DisableValidation) {
3062 // If requested by the caller and the module hasn't already been read
3063 // or compiled, mark modules on error as out-of-date.
3064 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3065 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3066 return OutOfDate;
3067
3068 if (!AllowASTWithCompilerErrors) {
3069 Diag(diag::err_ast_file_with_compiler_errors)
3071 return HadErrors;
3072 }
3073 }
3074 if (hasErrors) {
3075 Diags.ErrorOccurred = true;
3076 Diags.UncompilableErrorOccurred = true;
3077 Diags.UnrecoverableErrorOccurred = true;
3078 }
3079
3080 F.RelocatablePCH = Record[4];
3081 // Relative paths in a relocatable PCH are relative to our sysroot.
3082 if (F.RelocatablePCH)
3083 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3084
3086
3087 F.HasTimestamps = Record[6];
3088
3089 const std::string &CurBranch = getClangFullRepositoryVersion();
3090 StringRef ASTBranch = Blob;
3091 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3092 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3093 Diag(diag::err_ast_file_different_branch)
3094 << moduleKindForDiagnostic(F.Kind) << F.FileName << ASTBranch
3095 << CurBranch;
3096 return VersionMismatch;
3097 }
3098 break;
3099 }
3100
3101 case IMPORTS: {
3102 // Validate the AST before processing any imports (otherwise, untangling
3103 // them can be error-prone and expensive). A module will have a name and
3104 // will already have been validated, but this catches the PCH case.
3105 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3106 return Result;
3107
3108 // Load each of the imported PCH files.
3109 unsigned Idx = 0, N = Record.size();
3110 while (Idx < N) {
3111 // Read information about the AST file.
3112 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3113 // Whether we're importing a standard c++ module.
3114 bool IsImportingStdCXXModule = Record[Idx++];
3115 // The import location will be the local one for now; we will adjust
3116 // all import locations of module imports after the global source
3117 // location info are setup, in ReadAST.
3118 auto [ImportLoc, ImportModuleFileIndex] =
3119 ReadUntranslatedSourceLocation(Record[Idx++]);
3120 // The import location must belong to the current module file itself.
3121 assert(ImportModuleFileIndex == 0);
3122 off_t StoredSize = !IsImportingStdCXXModule ? (off_t)Record[Idx++] : 0;
3123 time_t StoredModTime =
3124 !IsImportingStdCXXModule ? (time_t)Record[Idx++] : 0;
3125
3126 ASTFileSignature StoredSignature;
3127 if (!IsImportingStdCXXModule) {
3128 auto FirstSignatureByte = Record.begin() + Idx;
3129 StoredSignature = ASTFileSignature::create(
3130 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
3132 }
3133
3134 std::string ImportedName = ReadString(Record, Idx);
3135 std::string ImportedFile;
3136
3137 // For prebuilt and explicit modules first consult the file map for
3138 // an override. Note that here we don't search prebuilt module
3139 // directories if we're not importing standard c++ module, only the
3140 // explicit name to file mappings. Also, we will still verify the
3141 // size/signature making sure it is essentially the same file but
3142 // perhaps in a different location.
3143 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3144 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3145 ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3146
3147 // For C++20 Modules, we won't record the path to the imported modules
3148 // in the BMI
3149 if (!IsImportingStdCXXModule) {
3150 if (ImportedFile.empty()) {
3151 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3152 // ModuleCache as when writing.
3153 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
3154 } else
3155 SkipPath(Record, Idx);
3156 } else if (ImportedFile.empty()) {
3157 Diag(clang::diag::err_failed_to_find_module_file) << ImportedName;
3158 return Missing;
3159 }
3160
3161 // If our client can't cope with us being out of date, we can't cope with
3162 // our dependency being missing.
3163 unsigned Capabilities = ClientLoadCapabilities;
3164 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3165 Capabilities &= ~ARR_Missing;
3166
3167 // Load the AST file.
3168 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
3169 Loaded, StoredSize, StoredModTime,
3170 StoredSignature, Capabilities);
3171
3172 // If we diagnosed a problem, produce a backtrace.
3173 bool recompilingFinalized =
3174 Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3175 getModuleManager().getModuleCache().isPCMFinal(F.FileName);
3176 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3177 Diag(diag::note_module_file_imported_by)
3178 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3179 if (recompilingFinalized)
3180 Diag(diag::note_module_file_conflict);
3181
3182 switch (Result) {
3183 case Failure: return Failure;
3184 // If we have to ignore the dependency, we'll have to ignore this too.
3185 case Missing:
3186 case OutOfDate: return OutOfDate;
3187 case VersionMismatch: return VersionMismatch;
3188 case ConfigurationMismatch: return ConfigurationMismatch;
3189 case HadErrors: return HadErrors;
3190 case Success: break;
3191 }
3192 }
3193 break;
3194 }
3195
3196 case ORIGINAL_FILE:
3197 F.OriginalSourceFileID = FileID::get(Record[0]);
3198 F.ActualOriginalSourceFileName = std::string(Blob);
3200 ResolveImportedPath(F, F.OriginalSourceFileName);
3201 break;
3202
3203 case ORIGINAL_FILE_ID:
3204 F.OriginalSourceFileID = FileID::get(Record[0]);
3205 break;
3206
3207 case MODULE_NAME:
3208 F.ModuleName = std::string(Blob);
3209 Diag(diag::remark_module_import)
3210 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3211 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3212 if (Listener)
3213 Listener->ReadModuleName(F.ModuleName);
3214
3215 // Validate the AST as soon as we have a name so we can exit early on
3216 // failure.
3217 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3218 return Result;
3219
3220 break;
3221
3222 case MODULE_DIRECTORY: {
3223 // Save the BaseDirectory as written in the PCM for computing the module
3224 // filename for the ModuleCache.
3225 BaseDirectoryAsWritten = Blob;
3226 assert(!F.ModuleName.empty() &&
3227 "MODULE_DIRECTORY found before MODULE_NAME");
3228 F.BaseDirectory = std::string(Blob);
3229 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3230 break;
3231 // If we've already loaded a module map file covering this module, we may
3232 // have a better path for it (relative to the current build).
3233 Module *M = PP.getHeaderSearchInfo().lookupModule(
3234 F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
3235 /*AllowExtraModuleMapSearch*/ true);
3236 if (M && M->Directory) {
3237 // If we're implicitly loading a module, the base directory can't
3238 // change between the build and use.
3239 // Don't emit module relocation error if we have -fno-validate-pch
3240 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3243 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(Blob);
3244 if (!BuildDir || *BuildDir != M->Directory) {
3245 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3246 Diag(diag::err_imported_module_relocated)
3247 << F.ModuleName << Blob << M->Directory->getName();
3248 return OutOfDate;
3249 }
3250 }
3251 F.BaseDirectory = std::string(M->Directory->getName());
3252 }
3253 break;
3254 }
3255
3256 case MODULE_MAP_FILE:
3257 if (ASTReadResult Result =
3258 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3259 return Result;
3260 break;
3261
3262 case INPUT_FILE_OFFSETS:
3263 NumInputs = Record[0];
3264 NumUserInputs = Record[1];
3266 (const llvm::support::unaligned_uint64_t *)Blob.data();
3267 F.InputFilesLoaded.resize(NumInputs);
3268 F.InputFileInfosLoaded.resize(NumInputs);
3269 F.NumUserInputFiles = NumUserInputs;
3270 break;
3271 }
3272 }
3273}
3274
3275llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3276 unsigned ClientLoadCapabilities) {
3277 BitstreamCursor &Stream = F.Stream;
3278
3279 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
3280 return Err;
3281 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3282
3283 // Read all of the records and blocks for the AST file.
3284 RecordData Record;
3285 while (true) {
3286 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3287 if (!MaybeEntry)
3288 return MaybeEntry.takeError();
3289 llvm::BitstreamEntry Entry = MaybeEntry.get();
3290
3291 switch (Entry.Kind) {
3292 case llvm::BitstreamEntry::Error:
3293 return llvm::createStringError(
3294 std::errc::illegal_byte_sequence,
3295 "error at end of module block in AST file");
3296 case llvm::BitstreamEntry::EndBlock:
3297 // Outside of C++, we do not store a lookup map for the translation unit.
3298 // Instead, mark it as needing a lookup map to be built if this module
3299 // contains any declarations lexically within it (which it always does!).
3300 // This usually has no cost, since we very rarely need the lookup map for
3301 // the translation unit outside C++.
3302 if (ASTContext *Ctx = ContextObj) {
3303 DeclContext *DC = Ctx->getTranslationUnitDecl();
3304 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3306 }
3307
3308 return llvm::Error::success();
3309 case llvm::BitstreamEntry::SubBlock:
3310 switch (Entry.ID) {
3311 case DECLTYPES_BLOCK_ID:
3312 // We lazily load the decls block, but we want to set up the
3313 // DeclsCursor cursor to point into it. Clone our current bitcode
3314 // cursor to it, enter the block and read the abbrevs in that block.
3315 // With the main cursor, we just skip over it.
3316 F.DeclsCursor = Stream;
3317 if (llvm::Error Err = Stream.SkipBlock())
3318 return Err;
3319 if (llvm::Error Err = ReadBlockAbbrevs(
3321 return Err;
3322 break;
3323
3325 F.MacroCursor = Stream;
3326 if (!PP.getExternalSource())
3327 PP.setExternalSource(this);
3328
3329 if (llvm::Error Err = Stream.SkipBlock())
3330 return Err;
3331 if (llvm::Error Err =
3332 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3333 return Err;
3334 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3335 break;
3336
3338 F.PreprocessorDetailCursor = Stream;
3339
3340 if (llvm::Error Err = Stream.SkipBlock()) {
3341 return Err;
3342 }
3343 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3345 return Err;
3347 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3348
3349 if (!PP.getPreprocessingRecord())
3350 PP.createPreprocessingRecord();
3351 if (!PP.getPreprocessingRecord()->getExternalSource())
3352 PP.getPreprocessingRecord()->SetExternalSource(*this);
3353 break;
3354
3356 if (llvm::Error Err = ReadSourceManagerBlock(F))
3357 return Err;
3358 break;
3359
3360 case SUBMODULE_BLOCK_ID:
3361 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3362 return Err;
3363 break;
3364
3365 case COMMENTS_BLOCK_ID: {
3366 BitstreamCursor C = Stream;
3367
3368 if (llvm::Error Err = Stream.SkipBlock())
3369 return Err;
3370 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3371 return Err;
3372 CommentsCursors.push_back(std::make_pair(C, &F));
3373 break;
3374 }
3375
3376 default:
3377 if (llvm::Error Err = Stream.SkipBlock())
3378 return Err;
3379 break;
3380 }
3381 continue;
3382
3383 case llvm::BitstreamEntry::Record:
3384 // The interesting case.
3385 break;
3386 }
3387
3388 // Read and process a record.
3389 Record.clear();
3390 StringRef Blob;
3391 Expected<unsigned> MaybeRecordType =
3392 Stream.readRecord(Entry.ID, Record, &Blob);
3393 if (!MaybeRecordType)
3394 return MaybeRecordType.takeError();
3395 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3396
3397 // If we're not loading an AST context, we don't care about most records.
3398 if (!ContextObj) {
3399 switch (RecordType) {
3400 case IDENTIFIER_TABLE:
3401 case IDENTIFIER_OFFSET:
3403 case STATISTICS:
3406 case PP_COUNTER_VALUE:
3408 case MODULE_OFFSET_MAP:
3412 case IMPORTED_MODULES:
3413 case MACRO_OFFSET:
3414 break;
3415 default:
3416 continue;
3417 }
3418 }
3419
3420 switch (RecordType) {
3421 default: // Default behavior: ignore.
3422 break;
3423
3424 case TYPE_OFFSET: {
3425 if (F.LocalNumTypes != 0)
3426 return llvm::createStringError(
3427 std::errc::illegal_byte_sequence,
3428 "duplicate TYPE_OFFSET record in AST file");
3429 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3430 F.LocalNumTypes = Record[0];
3431 F.BaseTypeIndex = getTotalNumTypes();
3432
3433 if (F.LocalNumTypes > 0)
3434 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3435
3436 break;
3437 }
3438
3439 case DECL_OFFSET: {
3440 if (F.LocalNumDecls != 0)
3441 return llvm::createStringError(
3442 std::errc::illegal_byte_sequence,
3443 "duplicate DECL_OFFSET record in AST file");
3444 F.DeclOffsets = (const DeclOffset *)Blob.data();
3445 F.LocalNumDecls = Record[0];
3446 F.BaseDeclIndex = getTotalNumDecls();
3447
3448 if (F.LocalNumDecls > 0)
3449 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3450
3451 break;
3452 }
3453
3454 case TU_UPDATE_LEXICAL: {
3455 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3456 LexicalContents Contents(
3457 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3458 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3459 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3461 break;
3462 }
3463
3464 case UPDATE_VISIBLE: {
3465 unsigned Idx = 0;
3466 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3467 auto *Data = (const unsigned char*)Blob.data();
3468 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3469 // If we've already loaded the decl, perform the updates when we finish
3470 // loading this block.
3471 if (Decl *D = GetExistingDecl(ID))
3472 PendingUpdateRecords.push_back(
3473 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3474 break;
3475 }
3476
3477 case IDENTIFIER_TABLE:
3479 reinterpret_cast<const unsigned char *>(Blob.data());
3480 if (Record[0]) {
3481 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3483 F.IdentifierTableData + sizeof(uint32_t),
3485 ASTIdentifierLookupTrait(*this, F));
3486
3487 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3488 }
3489 break;
3490
3491 case IDENTIFIER_OFFSET: {
3492 if (F.LocalNumIdentifiers != 0)
3493 return llvm::createStringError(
3494 std::errc::illegal_byte_sequence,
3495 "duplicate IDENTIFIER_OFFSET record in AST file");
3496 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3498 F.BaseIdentifierID = getTotalNumIdentifiers();
3499
3500 if (F.LocalNumIdentifiers > 0)
3501 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3503 break;
3504 }
3505
3507 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3508 break;
3509
3511 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3512 // about "interesting" decls (for instance, if we're building a module).
3513 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3514 EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));
3515 break;
3516
3518 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3519 // them (ie: if we're not codegenerating this module).
3520 if (F.Kind == MK_MainFile ||
3521 getContext().getLangOpts().BuildingPCHWithObjectFile)
3522 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3523 EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));
3524 break;
3525
3526 case SPECIAL_TYPES:
3527 if (SpecialTypes.empty()) {
3528 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3529 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3530 break;
3531 }
3532
3533 if (SpecialTypes.size() != Record.size())
3534 return llvm::createStringError(std::errc::illegal_byte_sequence,
3535 "invalid special-types record");
3536
3537 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3538 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3539 if (!SpecialTypes[I])
3540 SpecialTypes[I] = ID;
3541 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3542 // merge step?
3543 }
3544 break;
3545
3546 case STATISTICS:
3547 TotalNumStatements += Record[0];
3548 TotalNumMacros += Record[1];
3549 TotalLexicalDeclContexts += Record[2];
3550 TotalVisibleDeclContexts += Record[3];
3551 break;
3552
3554 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3555 UnusedFileScopedDecls.push_back(ReadDeclID(F, Record, I));
3556 break;
3557
3558 case DELEGATING_CTORS:
3559 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3560 DelegatingCtorDecls.push_back(ReadDeclID(F, Record, I));
3561 break;
3562
3564 if (Record.size() % 3 != 0)
3565 return llvm::createStringError(std::errc::illegal_byte_sequence,
3566 "invalid weak identifiers record");
3567
3568 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3569 // files. This isn't the way to do it :)
3570 WeakUndeclaredIdentifiers.clear();
3571
3572 // Translate the weak, undeclared identifiers into global IDs.
3573 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3574 WeakUndeclaredIdentifiers.push_back(
3575 getGlobalIdentifierID(F, Record[I++]));
3576 WeakUndeclaredIdentifiers.push_back(
3577 getGlobalIdentifierID(F, Record[I++]));
3578 WeakUndeclaredIdentifiers.push_back(
3579 ReadSourceLocation(F, Record, I).getRawEncoding());
3580 }
3581 break;
3582
3583 case SELECTOR_OFFSETS: {
3584 F.SelectorOffsets = (const uint32_t *)Blob.data();
3586 unsigned LocalBaseSelectorID = Record[1];
3587 F.BaseSelectorID = getTotalNumSelectors();
3588
3589 if (F.LocalNumSelectors > 0) {
3590 // Introduce the global -> local mapping for selectors within this
3591 // module.
3592 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3593
3594 // Introduce the local -> global mapping for selectors within this
3595 // module.
3597 std::make_pair(LocalBaseSelectorID,
3598 F.BaseSelectorID - LocalBaseSelectorID));
3599
3600 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3601 }
3602 break;
3603 }
3604
3605 case METHOD_POOL:
3606 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3607 if (Record[0])
3609 = ASTSelectorLookupTable::Create(
3612 ASTSelectorLookupTrait(*this, F));
3613 TotalNumMethodPoolEntries += Record[1];
3614 break;
3615
3617 if (!Record.empty()) {
3618 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3619 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3620 Record[Idx++]));
3621 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3622 getRawEncoding());
3623 }
3624 }
3625 break;
3626
3627 case PP_ASSUME_NONNULL_LOC: {
3628 unsigned Idx = 0;
3629 if (!Record.empty())
3630 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3631 ReadSourceLocation(F, Record, Idx));
3632 break;
3633 }
3634
3636 if (!Record.empty()) {
3638 unsigned Idx = 0;
3639 while (Idx < Record.size())
3640 SrcLocs.push_back(ReadSourceLocation(F, Record, Idx));
3641 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
3642 }
3643 break;
3644 }
3645
3647 if (!Record.empty()) {
3648 unsigned Idx = 0, End = Record.size() - 1;
3649 bool ReachedEOFWhileSkipping = Record[Idx++];
3650 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3651 if (ReachedEOFWhileSkipping) {
3652 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3653 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3654 bool FoundNonSkipPortion = Record[Idx++];
3655 bool FoundElse = Record[Idx++];
3656 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3657 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3658 FoundElse, ElseLoc);
3659 }
3660 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3661 while (Idx < End) {
3662 auto Loc = ReadSourceLocation(F, Record, Idx);
3663 bool WasSkipping = Record[Idx++];
3664 bool FoundNonSkip = Record[Idx++];
3665 bool FoundElse = Record[Idx++];
3666 ConditionalStack.push_back(
3667 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3668 }
3669 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3670 }
3671 break;
3672
3673 case PP_COUNTER_VALUE:
3674 if (!Record.empty() && Listener)
3675 Listener->ReadCounter(F, Record[0]);
3676 break;
3677
3678 case FILE_SORTED_DECLS:
3679 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
3681 break;
3682
3684 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3686 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3688 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3689 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3690 SLocSpaceSize);
3691 if (!F.SLocEntryBaseID) {
3692 if (!Diags.isDiagnosticInFlight()) {
3693 Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3694 SourceMgr.noteSLocAddressSpaceUsage(Diags);
3695 }
3696 return llvm::createStringError(std::errc::invalid_argument,
3697 "ran out of source locations");
3698 }
3699 // Make our entry in the range map. BaseID is negative and growing, so
3700 // we invert it. Because we invert it, though, we need the other end of
3701 // the range.
3702 unsigned RangeStart =
3704 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3706
3707 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3708 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3709 GlobalSLocOffsetMap.insert(
3710 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3711 - SLocSpaceSize,&F));
3712
3713 TotalNumSLocEntries += F.LocalNumSLocEntries;
3714 break;
3715 }
3716
3717 case MODULE_OFFSET_MAP:
3718 F.ModuleOffsetMap = Blob;
3719 break;
3720
3722 ParseLineTable(F, Record);
3723 break;
3724
3725 case EXT_VECTOR_DECLS:
3726 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3727 ExtVectorDecls.push_back(ReadDeclID(F, Record, I));
3728 break;
3729
3730 case VTABLE_USES:
3731 if (Record.size() % 3 != 0)
3732 return llvm::createStringError(std::errc::illegal_byte_sequence,
3733 "Invalid VTABLE_USES record");
3734
3735 // Later tables overwrite earlier ones.
3736 // FIXME: Modules will have some trouble with this. This is clearly not
3737 // the right way to do this.
3738 VTableUses.clear();
3739
3740 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3741 VTableUses.push_back(
3742 {ReadDeclID(F, Record, Idx),
3743 ReadSourceLocation(F, Record, Idx).getRawEncoding(),
3744 (bool)Record[Idx++]});
3745 }
3746 break;
3747
3749
3750 if (Record.size() % 2 != 0)
3751 return llvm::createStringError(
3752 std::errc::illegal_byte_sequence,
3753 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3754
3755 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3756 PendingInstantiations.push_back(
3757 {ReadDeclID(F, Record, I),
3758 ReadSourceLocation(F, Record, I).getRawEncoding()});
3759 }
3760 break;
3761
3762 case SEMA_DECL_REFS:
3763 if (Record.size() != 3)
3764 return llvm::createStringError(std::errc::illegal_byte_sequence,
3765 "Invalid SEMA_DECL_REFS block");
3766 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3767 SemaDeclRefs.push_back(ReadDeclID(F, Record, I));
3768 break;
3769
3770 case PPD_ENTITIES_OFFSETS: {
3771 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3772 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3773 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3774
3775 unsigned LocalBasePreprocessedEntityID = Record[0];
3776
3777 unsigned StartingID;
3778 if (!PP.getPreprocessingRecord())
3779 PP.createPreprocessingRecord();
3780 if (!PP.getPreprocessingRecord()->getExternalSource())
3781 PP.getPreprocessingRecord()->SetExternalSource(*this);
3782 StartingID
3783 = PP.getPreprocessingRecord()
3784 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3785 F.BasePreprocessedEntityID = StartingID;
3786
3787 if (F.NumPreprocessedEntities > 0) {
3788 // Introduce the global -> local mapping for preprocessed entities in
3789 // this module.
3790 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3791
3792 // Introduce the local -> global mapping for preprocessed entities in
3793 // this module.
3795 std::make_pair(LocalBasePreprocessedEntityID,
3796 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3797 }
3798
3799 break;
3800 }
3801
3802 case PPD_SKIPPED_RANGES: {
3803 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3804 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3805 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3806
3807 if (!PP.getPreprocessingRecord())
3808 PP.createPreprocessingRecord();
3809 if (!PP.getPreprocessingRecord()->getExternalSource())
3810 PP.getPreprocessingRecord()->SetExternalSource(*this);
3811 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3812 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3813
3815 GlobalSkippedRangeMap.insert(
3816 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3817 break;
3818 }
3819
3821 if (Record.size() % 2 != 0)
3822 return llvm::createStringError(
3823 std::errc::illegal_byte_sequence,
3824 "invalid DECL_UPDATE_OFFSETS block in AST file");
3825 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
3826 GlobalDeclID ID = ReadDeclID(F, Record, I);
3827 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I++]));
3828
3829 // If we've already loaded the decl, perform the updates when we finish
3830 // loading this block.
3831 if (Decl *D = GetExistingDecl(ID))
3832 PendingUpdateRecords.push_back(
3833 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3834 }
3835 break;
3836
3838 if (Record.size() % 3 != 0)
3839 return llvm::createStringError(
3840 std::errc::illegal_byte_sequence,
3841 "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
3842 "file");
3843 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
3844 GlobalDeclID ID = ReadDeclID(F, Record, I);
3845
3846 uint64_t BaseOffset = F.DeclsBlockStartOffset;
3847 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
3848 uint64_t LocalLexicalOffset = Record[I++];
3849 uint64_t LexicalOffset =
3850 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
3851 uint64_t LocalVisibleOffset = Record[I++];
3852 uint64_t VisibleOffset =
3853 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
3854
3855 DelayedNamespaceOffsetMap[ID] = {LexicalOffset, VisibleOffset};
3856
3857 assert(!GetExistingDecl(ID) &&
3858 "We shouldn't load the namespace in the front of delayed "
3859 "namespace lexical and visible block");
3860 }
3861 break;
3862 }
3863
3865 if (F.LocalNumObjCCategoriesInMap != 0)
3866 return llvm::createStringError(
3867 std::errc::illegal_byte_sequence,
3868 "duplicate OBJC_CATEGORIES_MAP record in AST file");
3869
3871 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3872 break;
3873
3874 case OBJC_CATEGORIES:
3875 F.ObjCCategories.swap(Record);
3876 break;
3877
3879 // Later tables overwrite earlier ones.
3880 // FIXME: Modules will have trouble with this.
3881 CUDASpecialDeclRefs.clear();
3882 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3883 CUDASpecialDeclRefs.push_back(ReadDeclID(F, Record, I));
3884 break;
3885
3887 F.HeaderFileInfoTableData = Blob.data();
3889 if (Record[0]) {
3891 = HeaderFileInfoLookupTable::Create(
3892 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3893 (const unsigned char *)F.HeaderFileInfoTableData,
3894 HeaderFileInfoTrait(*this, F,
3895 &PP.getHeaderSearchInfo(),
3896 Blob.data() + Record[2]));
3897
3898 PP.getHeaderSearchInfo().SetExternalSource(this);
3899 if (!PP.getHeaderSearchInfo().getExternalLookup())
3900 PP.getHeaderSearchInfo().SetExternalLookup(this);
3901 }
3902 break;
3903
3904 case FP_PRAGMA_OPTIONS:
3905 // Later tables overwrite earlier ones.
3906 FPPragmaOptions.swap(Record);
3907 break;
3908
3909 case OPENCL_EXTENSIONS:
3910 for (unsigned I = 0, E = Record.size(); I != E; ) {
3911 auto Name = ReadString(Record, I);
3912 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3913 OptInfo.Supported = Record[I++] != 0;
3914 OptInfo.Enabled = Record[I++] != 0;
3915 OptInfo.WithPragma = Record[I++] != 0;
3916 OptInfo.Avail = Record[I++];
3917 OptInfo.Core = Record[I++];
3918 OptInfo.Opt = Record[I++];
3919 }
3920 break;
3921
3923 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3924 TentativeDefinitions.push_back(ReadDeclID(F, Record, I));
3925 break;
3926
3927 case KNOWN_NAMESPACES:
3928 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3929 KnownNamespaces.push_back(ReadDeclID(F, Record, I));
3930 break;
3931
3932 case UNDEFINED_BUT_USED:
3933 if (Record.size() % 2 != 0)
3934 return llvm::createStringError(std::errc::illegal_byte_sequence,
3935 "invalid undefined-but-used record");
3936 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3937 UndefinedButUsed.push_back(
3938 {ReadDeclID(F, Record, I),
3939 ReadSourceLocation(F, Record, I).getRawEncoding()});
3940 }
3941 break;
3942
3944 for (unsigned I = 0, N = Record.size(); I != N;) {
3945 DelayedDeleteExprs.push_back(ReadDeclID(F, Record, I).getRawValue());
3946 const uint64_t Count = Record[I++];
3947 DelayedDeleteExprs.push_back(Count);
3948 for (uint64_t C = 0; C < Count; ++C) {
3949 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3950 bool IsArrayForm = Record[I++] == 1;
3951 DelayedDeleteExprs.push_back(IsArrayForm);
3952 }
3953 }
3954 break;
3955
3956 case VTABLES_TO_EMIT:
3957 if (F.Kind == MK_MainFile ||
3958 getContext().getLangOpts().BuildingPCHWithObjectFile)
3959 for (unsigned I = 0, N = Record.size(); I != N;)
3960 VTablesToEmit.push_back(ReadDeclID(F, Record, I));
3961 break;
3962
3963 case IMPORTED_MODULES:
3964 if (!F.isModule()) {
3965 // If we aren't loading a module (which has its own exports), make
3966 // all of the imported modules visible.
3967 // FIXME: Deal with macros-only imports.
3968 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3969 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3970 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3971 if (GlobalID) {
3972 PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3973 if (DeserializationListener)
3974 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3975 }
3976 }
3977 }
3978 break;
3979
3980 case MACRO_OFFSET: {
3981 if (F.LocalNumMacros != 0)
3982 return llvm::createStringError(
3983 std::errc::illegal_byte_sequence,
3984 "duplicate MACRO_OFFSET record in AST file");
3985 F.MacroOffsets = (const uint32_t *)Blob.data();
3986 F.LocalNumMacros = Record[0];
3987 unsigned LocalBaseMacroID = Record[1];
3989 F.BaseMacroID = getTotalNumMacros();
3990
3991 if (F.LocalNumMacros > 0) {
3992 // Introduce the global -> local mapping for macros within this module.
3993 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3994
3995 // Introduce the local -> global mapping for macros within this module.
3997 std::make_pair(LocalBaseMacroID,
3998 F.BaseMacroID - LocalBaseMacroID));
3999
4000 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
4001 }
4002 break;
4003 }
4004
4006 LateParsedTemplates.emplace_back(
4007 std::piecewise_construct, std::forward_as_tuple(&F),
4008 std::forward_as_tuple(Record.begin(), Record.end()));
4009 break;
4010
4012 if (Record.size() != 1)
4013 return llvm::createStringError(std::errc::illegal_byte_sequence,
4014 "invalid pragma optimize record");
4015 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
4016 break;
4017
4019 if (Record.size() != 1)
4020 return llvm::createStringError(std::errc::illegal_byte_sequence,
4021 "invalid pragma ms_struct record");
4022 PragmaMSStructState = Record[0];
4023 break;
4024
4026 if (Record.size() != 2)
4027 return llvm::createStringError(
4028 std::errc::illegal_byte_sequence,
4029 "invalid pragma pointers to members record");
4030 PragmaMSPointersToMembersState = Record[0];
4031 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
4032 break;
4033
4035 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4036 UnusedLocalTypedefNameCandidates.push_back(ReadDeclID(F, Record, I));
4037 break;
4038
4040 if (Record.size() != 1)
4041 return llvm::createStringError(std::errc::illegal_byte_sequence,
4042 "invalid cuda pragma options record");
4043 ForceHostDeviceDepth = Record[0];
4044 break;
4045
4047 if (Record.size() < 3)
4048 return llvm::createStringError(std::errc::illegal_byte_sequence,
4049 "invalid pragma pack record");
4050 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
4051 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
4052 unsigned NumStackEntries = Record[2];
4053 unsigned Idx = 3;
4054 // Reset the stack when importing a new module.
4055 PragmaAlignPackStack.clear();
4056 for (unsigned I = 0; I < NumStackEntries; ++I) {
4057 PragmaAlignPackStackEntry Entry;
4058 Entry.Value = ReadAlignPackInfo(Record[Idx++]);
4059 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4060 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4061 PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
4062 Entry.SlotLabel = PragmaAlignPackStrings.back();
4063 PragmaAlignPackStack.push_back(Entry);
4064 }
4065 break;
4066 }
4067
4069 if (Record.size() < 3)
4070 return llvm::createStringError(std::errc::illegal_byte_sequence,
4071 "invalid pragma float control record");
4072 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
4073 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
4074 unsigned NumStackEntries = Record[2];
4075 unsigned Idx = 3;
4076 // Reset the stack when importing a new module.
4077 FpPragmaStack.clear();
4078 for (unsigned I = 0; I < NumStackEntries; ++I) {
4079 FpPragmaStackEntry Entry;
4080 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
4081 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4082 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4083 FpPragmaStrings.push_back(ReadString(Record, Idx));
4084 Entry.SlotLabel = FpPragmaStrings.back();
4085 FpPragmaStack.push_back(Entry);
4086 }
4087 break;
4088 }
4089
4091 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4092 DeclsToCheckForDeferredDiags.insert(ReadDeclID(F, Record, I));
4093 break;
4094 }
4095 }
4096}
4097
4098void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4099 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4100
4101 // Additional remapping information.
4102 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4103 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4104 F.ModuleOffsetMap = StringRef();
4105
4107 RemapBuilder MacroRemap(F.MacroRemap);
4108 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4109 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4110 RemapBuilder SelectorRemap(F.SelectorRemap);
4111
4112 auto &ImportedModuleVector = F.TransitiveImports;
4113 assert(ImportedModuleVector.empty());
4114
4115 while (Data < DataEnd) {
4116 // FIXME: Looking up dependency modules by filename is horrible. Let's
4117 // start fixing this with prebuilt, explicit and implicit modules and see
4118 // how it goes...
4119 using namespace llvm::support;
4120 ModuleKind Kind = static_cast<ModuleKind>(
4121 endian::readNext<uint8_t, llvm::endianness::little>(Data));
4122 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(Data);
4123 StringRef Name = StringRef((const char*)Data, Len);
4124 Data += Len;
4127 ? ModuleMgr.lookupByModuleName(Name)
4128 : ModuleMgr.lookupByFileName(Name));
4129 if (!OM) {
4130 std::string Msg = "refers to unknown module, cannot find ";
4131 Msg.append(std::string(Name));
4132 Error(Msg);
4133 return;
4134 }
4135
4136 ImportedModuleVector.push_back(OM);
4137
4138 uint32_t MacroIDOffset =
4139 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4140 uint32_t PreprocessedEntityIDOffset =
4141 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4142 uint32_t SubmoduleIDOffset =
4143 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4144 uint32_t SelectorIDOffset =
4145 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4146
4147 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4148 RemapBuilder &Remap) {
4149 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4150 if (Offset != None)
4151 Remap.insert(std::make_pair(Offset,
4152 static_cast<int>(BaseOffset - Offset)));
4153 };
4154
4155 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4156 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4157 PreprocessedEntityRemap);
4158 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4159 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4160 }
4161}
4162
4164ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4165 const ModuleFile *ImportedBy,
4166 unsigned ClientLoadCapabilities) {
4167 unsigned Idx = 0;
4168 F.ModuleMapPath = ReadPath(F, Record, Idx);
4169
4170 // Try to resolve ModuleName in the current header search context and
4171 // verify that it is found in the same module map file as we saved. If the
4172 // top-level AST file is a main file, skip this check because there is no
4173 // usable header search context.
4174 assert(!F.ModuleName.empty() &&
4175 "MODULE_NAME should come before MODULE_MAP_FILE");
4176 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4177 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4178 // An implicitly-loaded module file should have its module listed in some
4179 // module map file that we've already loaded.
4180 Module *M =
4181 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
4182 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4183 OptionalFileEntryRef ModMap =
4184 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4185 // Don't emit module relocation error if we have -fno-validate-pch
4186 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4188 !ModMap) {
4189 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
4190 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4191 // This module was defined by an imported (explicit) module.
4192 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4193 << ASTFE->getName();
4194 } else {
4195 // This module was built with a different module map.
4196 Diag(diag::err_imported_module_not_found)
4197 << F.ModuleName << F.FileName
4198 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4199 << !ImportedBy;
4200 // In case it was imported by a PCH, there's a chance the user is
4201 // just missing to include the search path to the directory containing
4202 // the modulemap.
4203 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4204 Diag(diag::note_imported_by_pch_module_not_found)
4205 << llvm::sys::path::parent_path(F.ModuleMapPath);
4206 }
4207 }
4208 return OutOfDate;
4209 }
4210
4211 assert(M && M->Name == F.ModuleName && "found module with different name");
4212
4213 // Check the primary module map file.
4214 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
4215 if (!StoredModMap || *StoredModMap != ModMap) {
4216 assert(ModMap && "found module is missing module map file");
4217 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4218 "top-level import should be verified");
4219 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4220 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4221 Diag(diag::err_imported_module_modmap_changed)
4222 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4223 << ModMap->getName() << F.ModuleMapPath << NotImported;
4224 return OutOfDate;
4225 }
4226
4227 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4228 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4229 // FIXME: we should use input files rather than storing names.
4230 std::string Filename = ReadPath(F, Record, Idx);
4231 auto SF = FileMgr.getOptionalFileRef(Filename, false, false);
4232 if (!SF) {
4233 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4234 Error("could not find file '" + Filename +"' referenced by AST file");
4235 return OutOfDate;
4236 }
4237 AdditionalStoredMaps.insert(*SF);
4238 }
4239
4240 // Check any additional module map files (e.g. module.private.modulemap)
4241 // that are not in the pcm.
4242 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4243 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4244 // Remove files that match
4245 // Note: SmallPtrSet::erase is really remove
4246 if (!AdditionalStoredMaps.erase(ModMap)) {
4247 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4248 Diag(diag::err_module_different_modmap)
4249 << F.ModuleName << /*new*/0 << ModMap.getName();
4250 return OutOfDate;
4251 }
4252 }
4253 }
4254
4255 // Check any additional module map files that are in the pcm, but not
4256 // found in header search. Cases that match are already removed.
4257 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4258 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4259 Diag(diag::err_module_different_modmap)
4260 << F.ModuleName << /*not new*/1 << ModMap.getName();
4261 return OutOfDate;
4262 }
4263 }
4264
4265 if (Listener)
4266 Listener->ReadModuleMapFile(F.ModuleMapPath);
4267 return Success;
4268}
4269
4270/// Move the given method to the back of the global list of methods.
4272 // Find the entry for this selector in the method pool.
4274 S.ObjC().MethodPool.find(Method->getSelector());
4275 if (Known == S.ObjC().MethodPool.end())
4276 return;
4277
4278 // Retrieve the appropriate method list.
4279 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4280 : Known->second.second;
4281 bool Found = false;
4282 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4283 if (!Found) {
4284 if (List->getMethod() == Method) {
4285 Found = true;
4286 } else {
4287 // Keep searching.
4288 continue;
4289 }
4290 }
4291
4292 if (List->getNext())
4293 List->setMethod(List->getNext()->getMethod());
4294 else
4295 List->setMethod(Method);
4296 }
4297}
4298
4300 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4301 for (Decl *D : Names) {
4302 bool wasHidden = !D->isUnconditionallyVisible();
4304
4305 if (wasHidden && SemaObj) {
4306 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4307 moveMethodToBackOfGlobalList(*SemaObj, Method);
4308 }
4309 }
4310 }
4311}
4312
4314 Module::NameVisibilityKind NameVisibility,
4315 SourceLocation ImportLoc) {
4318 Stack.push_back(Mod);
4319 while (!Stack.empty()) {
4320 Mod = Stack.pop_back_val();
4321
4322 if (NameVisibility <= Mod->NameVisibility) {
4323 // This module already has this level of visibility (or greater), so
4324 // there is nothing more to do.
4325 continue;
4326 }
4327
4328 if (Mod->isUnimportable()) {
4329 // Modules that aren't importable cannot be made visible.
4330 continue;
4331 }
4332
4333 // Update the module's name visibility.
4334 Mod->NameVisibility = NameVisibility;
4335
4336 // If we've already deserialized any names from this module,
4337 // mark them as visible.
4338 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4339 if (Hidden != HiddenNamesMap.end()) {
4340 auto HiddenNames = std::move(*Hidden);
4341 HiddenNamesMap.erase(Hidden);
4342 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4343 assert(!HiddenNamesMap.contains(Mod) &&
4344 "making names visible added hidden names");
4345 }
4346
4347 // Push any exported modules onto the stack to be marked as visible.
4349 Mod->getExportedModules(Exports);
4351 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4352 Module *Exported = *I;
4353 if (Visited.insert(Exported).second)
4354 Stack.push_back(Exported);
4355 }
4356 }
4357}
4358
4359/// We've merged the definition \p MergedDef into the existing definition
4360/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4361/// visible.
4363 NamedDecl *MergedDef) {
4364 if (!Def->isUnconditionallyVisible()) {
4365 // If MergedDef is visible or becomes visible, make the definition visible.
4366 if (MergedDef->isUnconditionallyVisible())
4368 else {
4369 getContext().mergeDefinitionIntoModule(
4370 Def, MergedDef->getImportedOwningModule(),
4371 /*NotifyListeners*/ false);
4372 PendingMergedDefinitionsToDeduplicate.insert(Def);
4373 }
4374 }
4375}
4376
4378 if (GlobalIndex)
4379 return false;
4380
4381 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4382 !PP.getLangOpts().Modules)
4383 return true;
4384
4385 // Try to load the global index.
4386 TriedLoadingGlobalIndex = true;
4387 StringRef ModuleCachePath
4388 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4389 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4390 GlobalModuleIndex::readIndex(ModuleCachePath);
4391 if (llvm::Error Err = std::move(Result.second)) {
4392 assert(!Result.first);
4393 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4394 return true;
4395 }
4396
4397 GlobalIndex.reset(Result.first);
4398 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4399 return false;
4400}
4401
4403 return PP.getLangOpts().Modules && UseGlobalIndex &&
4404 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4405}
4406
4408 // Overwrite the timestamp file contents so that file's mtime changes.
4409 std::string TimestampFilename = MF.getTimestampFilename();
4410 std::error_code EC;
4411 llvm::raw_fd_ostream OS(TimestampFilename, EC,
4412 llvm::sys::fs::OF_TextWithCRLF);
4413 if (EC)
4414 return;
4415 OS << "Timestamp file\n";
4416 OS.close();
4417 OS.clear_error(); // Avoid triggering a fatal error.
4418}
4419
4420/// Given a cursor at the start of an AST file, scan ahead and drop the
4421/// cursor into the start of the given block ID, returning false on success and
4422/// true on failure.
4423static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4424 while (true) {
4425 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4426 if (!MaybeEntry) {
4427 // FIXME this drops errors on the floor.
4428 consumeError(MaybeEntry.takeError());
4429 return true;
4430 }
4431 llvm::BitstreamEntry Entry = MaybeEntry.get();
4432
4433 switch (Entry.Kind) {
4434 case llvm::BitstreamEntry::Error:
4435 case llvm::BitstreamEntry::EndBlock:
4436 return true;
4437
4438 case llvm::BitstreamEntry::Record:
4439 // Ignore top-level records.
4440 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4441 break;
4442 else {
4443 // FIXME this drops errors on the floor.
4444 consumeError(Skipped.takeError());
4445 return true;
4446 }
4447
4448 case llvm::BitstreamEntry::SubBlock:
4449 if (Entry.ID == BlockID) {
4450 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4451 // FIXME this drops the error on the floor.
4452 consumeError(std::move(Err));
4453 return true;
4454 }
4455 // Found it!
4456 return false;
4457 }
4458
4459 if (llvm::Error Err = Cursor.SkipBlock()) {
4460 // FIXME this drops the error on the floor.
4461 consumeError(std::move(Err));
4462 return true;
4463 }
4464 }
4465 }
4466}
4467
4469 SourceLocation ImportLoc,
4470 unsigned ClientLoadCapabilities,
4471 ModuleFile **NewLoadedModuleFile) {
4472 llvm::TimeTraceScope scope("ReadAST", FileName);
4473
4474 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4476 CurrentDeserializingModuleKind, Type);
4477
4478 // Defer any pending actions until we get to the end of reading the AST file.
4479 Deserializing AnASTFile(this);
4480
4481 // Bump the generation number.
4482 unsigned PreviousGeneration = 0;
4483 if (ContextObj)
4484 PreviousGeneration = incrementGeneration(*ContextObj);
4485
4486 unsigned NumModules = ModuleMgr.size();
4488 if (ASTReadResult ReadResult =
4489 ReadASTCore(FileName, Type, ImportLoc,
4490 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4491 ClientLoadCapabilities)) {
4492 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4493
4494 // If we find that any modules are unusable, the global index is going
4495 // to be out-of-date. Just remove it.
4496 GlobalIndex.reset();
4497 ModuleMgr.setGlobalIndex(nullptr);
4498 return ReadResult;
4499 }
4500
4501 if (NewLoadedModuleFile && !Loaded.empty())
4502 *NewLoadedModuleFile = Loaded.back().Mod;
4503
4504 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4505 // remove modules from this point. Various fields are updated during reading
4506 // the AST block and removing the modules would result in dangling pointers.
4507 // They are generally only incidentally dereferenced, ie. a binary search
4508 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4509 // be dereferenced but it wouldn't actually be used.
4510
4511 // Load the AST blocks of all of the modules that we loaded. We can still
4512 // hit errors parsing the ASTs at this point.
4513 for (ImportedModule &M : Loaded) {
4514 ModuleFile &F = *M.Mod;
4515 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4516
4517 // Read the AST block.
4518 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4519 Error(std::move(Err));
4520 return Failure;
4521 }
4522
4523 // The AST block should always have a definition for the main module.
4524 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4525 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4526 return Failure;
4527 }
4528
4529 // Read the extension blocks.
4531 if (llvm::Error Err = ReadExtensionBlock(F)) {
4532 Error(std::move(Err));
4533 return Failure;
4534 }
4535 }
4536
4537 // Once read, set the ModuleFile bit base offset and update the size in
4538 // bits of all files we've seen.
4539 F.GlobalBitOffset = TotalModulesSizeInBits;
4540 TotalModulesSizeInBits += F.SizeInBits;
4541 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4542 }
4543
4544 // Preload source locations and interesting indentifiers.
4545 for (ImportedModule &M : Loaded) {
4546 ModuleFile &F = *M.Mod;
4547
4548 // Map the original source file ID into the ID space of the current
4549 // compilation.
4551 F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4552
4553 for (auto Offset : F.PreloadIdentifierOffsets) {
4554 const unsigned char *Data = F.IdentifierTableData + Offset;
4555
4556 ASTIdentifierLookupTrait Trait(*this, F);
4557 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4558 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4559
4560 IdentifierInfo *II;
4561 if (!PP.getLangOpts().CPlusPlus) {
4562 // Identifiers present in both the module file and the importing
4563 // instance are marked out-of-date so that they can be deserialized
4564 // on next use via ASTReader::updateOutOfDateIdentifier().
4565 // Identifiers present in the module file but not in the importing
4566 // instance are ignored for now, preventing growth of the identifier
4567 // table. They will be deserialized on first use via ASTReader::get().
4568 auto It = PP.getIdentifierTable().find(Key);
4569 if (It == PP.getIdentifierTable().end())
4570 continue;
4571 II = It->second;
4572 } else {
4573 // With C++ modules, not many identifiers are considered interesting.
4574 // All identifiers in the module file can be placed into the identifier
4575 // table of the importing instance and marked as out-of-date. This makes
4576 // ASTReader::get() a no-op, and deserialization will take place on
4577 // first/next use via ASTReader::updateOutOfDateIdentifier().
4578 II = &PP.getIdentifierTable().getOwn(Key);
4579 }
4580
4581 II->setOutOfDate(true);
4582
4583 // Mark this identifier as being from an AST file so that we can track
4584 // whether we need to serialize it.
4585 markIdentifierFromAST(*this, *II, /*IsModule=*/true);
4586
4587 // Associate the ID with the identifier so that the writer can reuse it.
4588 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4589 SetIdentifierInfo(ID, II);
4590 }
4591 }
4592
4593 // Builtins and library builtins have already been initialized. Mark all
4594 // identifiers as out-of-date, so that they are deserialized on first use.
4595 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4596 for (auto &Id : PP.getIdentifierTable())
4597 Id.second->setOutOfDate(true);
4598
4599 // Mark selectors as out of date.
4600 for (const auto &Sel : SelectorGeneration)
4601 SelectorOutOfDate[Sel.first] = true;
4602
4603 // Setup the import locations and notify the module manager that we've
4604 // committed to these module files.
4605 for (ImportedModule &M : Loaded) {
4606 ModuleFile &F = *M.Mod;
4607
4608 ModuleMgr.moduleFileAccepted(&F);
4609
4610 // Set the import location.
4611 F.DirectImportLoc = ImportLoc;
4612 // FIXME: We assume that locations from PCH / preamble do not need
4613 // any translation.
4614 if (!M.ImportedBy)
4615 F.ImportLoc = M.ImportLoc;
4616 else
4617 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4618 }
4619
4620 // Resolve any unresolved module exports.
4621 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4622 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4623 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4624 Module *ResolvedMod = getSubmodule(GlobalID);
4625
4626 switch (Unresolved.Kind) {
4627 case UnresolvedModuleRef::Conflict:
4628 if (ResolvedMod) {
4629 Module::Conflict Conflict;
4630 Conflict.Other = ResolvedMod;
4631 Conflict.Message = Unresolved.String.str();
4632 Unresolved.Mod->Conflicts.push_back(Conflict);
4633 }
4634 continue;
4635
4636 case UnresolvedModuleRef::Import:
4637 if (ResolvedMod)
4638 Unresolved.Mod->Imports.insert(ResolvedMod);
4639 continue;
4640
4641 case UnresolvedModuleRef::Affecting:
4642 if (ResolvedMod)
4643 Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4644 continue;
4645
4646 case UnresolvedModuleRef::Export:
4647 if (ResolvedMod || Unresolved.IsWildcard)
4648 Unresolved.Mod->Exports.push_back(
4649 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4650 continue;
4651 }
4652 }
4653 UnresolvedModuleRefs.clear();
4654
4655 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4656 // Might be unnecessary as use declarations are only used to build the
4657 // module itself.
4658
4659 if (ContextObj)
4660 InitializeContext();
4661
4662 if (SemaObj)
4663 UpdateSema();
4664
4665 if (DeserializationListener)
4666 DeserializationListener->ReaderInitialized(this);
4667
4668 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4669 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4670 // If this AST file is a precompiled preamble, then set the
4671 // preamble file ID of the source manager to the file source file
4672 // from which the preamble was built.
4673 if (Type == MK_Preamble) {
4674 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4675 } else if (Type == MK_MainFile) {
4676 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4677 }
4678 }
4679
4680 // For any Objective-C class definitions we have already loaded, make sure
4681 // that we load any additional categories.
4682 if (ContextObj) {
4683 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4684 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4685 ObjCClassesLoaded[I], PreviousGeneration);
4686 }
4687 }
4688
4689 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4691 // Now we are certain that the module and all modules it depends on are
4692 // up-to-date. For implicitly-built module files, ensure the corresponding
4693 // timestamp files are up-to-date in this build session.
4694 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4695 ImportedModule &M = Loaded[I];
4696 if (M.Mod->Kind == MK_ImplicitModule &&
4697 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4698 updateModuleTimestamp(*M.Mod);
4699 }
4700 }
4701
4702 return Success;
4703}
4704
4705static ASTFileSignature readASTFileSignature(StringRef PCH);
4706
4707/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4708static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4709 // FIXME checking magic headers is done in other places such as
4710 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4711 // always done the same. Unify it all with a helper.
4712 if (!Stream.canSkipToPos(4))
4713 return llvm::createStringError(std::errc::illegal_byte_sequence,
4714 "file too small to contain AST file magic");
4715 for (unsigned C : {'C', 'P', 'C', 'H'})
4716 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4717 if (Res.get() != C)
4718 return llvm::createStringError(
4719 std::errc::illegal_byte_sequence,
4720 "file doesn't start with AST file magic");
4721 } else
4722 return Res.takeError();
4723 return llvm::Error::success();
4724}
4725
4727 switch (Kind) {
4728 case MK_PCH:
4729 return 0; // PCH
4730 case MK_ImplicitModule:
4731 case MK_ExplicitModule:
4732 case MK_PrebuiltModule:
4733 return 1; // module
4734 case MK_MainFile:
4735 case MK_Preamble:
4736 return 2; // main source file
4737 }
4738 llvm_unreachable("unknown module kind");
4739}
4740
4742ASTReader::ReadASTCore(StringRef FileName,
4744 SourceLocation ImportLoc,
4745 ModuleFile *ImportedBy,
4747 off_t ExpectedSize, time_t ExpectedModTime,
4748 ASTFileSignature ExpectedSignature,
4749 unsigned ClientLoadCapabilities) {
4750 ModuleFile *M;
4751 std::string ErrorStr;
4753 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4754 getGeneration(), ExpectedSize, ExpectedModTime,
4755 ExpectedSignature, readASTFileSignature,
4756 M, ErrorStr);
4757
4758 switch (AddResult) {
4760 Diag(diag::remark_module_import)
4761 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4762 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4763 return Success;
4764
4766 // Load module file below.
4767 break;
4768
4770 // The module file was missing; if the client can handle that, return
4771 // it.
4772 if (ClientLoadCapabilities & ARR_Missing)
4773 return Missing;
4774
4775 // Otherwise, return an error.
4776 Diag(diag::err_ast_file_not_found)
4777 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4778 << ErrorStr;
4779 return Failure;
4780
4782 // We couldn't load the module file because it is out-of-date. If the
4783 // client can handle out-of-date, return it.
4784 if (ClientLoadCapabilities & ARR_OutOfDate)
4785 return OutOfDate;
4786
4787 // Otherwise, return an error.
4788 Diag(diag::err_ast_file_out_of_date)
4789 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4790 << ErrorStr;
4791 return Failure;
4792 }
4793
4794 assert(M && "Missing module file");
4795
4796 bool ShouldFinalizePCM = false;
4797 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4798 auto &MC = getModuleManager().getModuleCache();
4799 if (ShouldFinalizePCM)
4800 MC.finalizePCM(FileName);
4801 else
4802 MC.tryToDropPCM(FileName);
4803 });
4804 ModuleFile &F = *M;
4805 BitstreamCursor &Stream = F.Stream;
4806 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4807 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4808
4809 // Sniff for the signature.
4810 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4811 Diag(diag::err_ast_file_invalid)
4812 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4813 return Failure;
4814 }
4815
4816 // This is used for compatibility with older PCH formats.
4817 bool HaveReadControlBlock = false;
4818 while (true) {
4819 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4820 if (!MaybeEntry) {
4821 Error(MaybeEntry.takeError());
4822 return Failure;
4823 }
4824 llvm::BitstreamEntry Entry = MaybeEntry.get();
4825
4826 switch (Entry.Kind) {
4827 case llvm::BitstreamEntry::Error:
4828 case llvm::BitstreamEntry::Record:
4829 case llvm::BitstreamEntry::EndBlock:
4830 Error("invalid record at top-level of AST file");
4831 return Failure;
4832
4833 case llvm::BitstreamEntry::SubBlock:
4834 break;
4835 }
4836
4837 switch (Entry.ID) {
4838 case CONTROL_BLOCK_ID:
4839 HaveReadControlBlock = true;
4840 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4841 case Success:
4842 // Check that we didn't try to load a non-module AST file as a module.
4843 //
4844 // FIXME: Should we also perform the converse check? Loading a module as
4845 // a PCH file sort of works, but it's a bit wonky.
4847 Type == MK_PrebuiltModule) &&
4848 F.ModuleName.empty()) {
4849 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4850 if (Result != OutOfDate ||
4851 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4852 Diag(diag::err_module_file_not_module) << FileName;
4853 return Result;
4854 }
4855 break;
4856
4857 case Failure: return Failure;
4858 case Missing: return Missing;
4859 case OutOfDate: return OutOfDate;
4860 case VersionMismatch: return VersionMismatch;
4861 case ConfigurationMismatch: return ConfigurationMismatch;
4862 case HadErrors: return HadErrors;
4863 }
4864 break;
4865
4866 case AST_BLOCK_ID:
4867 if (!HaveReadControlBlock) {
4868 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4869 Diag(diag::err_ast_file_version_too_old)
4871 return VersionMismatch;
4872 }
4873
4874 // Record that we've loaded this module.
4875 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4876 ShouldFinalizePCM = true;
4877 return Success;
4878
4879 default:
4880 if (llvm::Error Err = Stream.SkipBlock()) {
4881 Error(std::move(Err));
4882 return Failure;
4883 }
4884 break;
4885 }
4886 }
4887
4888 llvm_unreachable("unexpected break; expected return");
4889}
4890
4892ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4893 unsigned ClientLoadCapabilities) {
4894 const HeaderSearchOptions &HSOpts =
4895 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4896 bool AllowCompatibleConfigurationMismatch =
4898 bool DisableValidation = shouldDisableValidationForFile(F);
4899
4900 ASTReadResult Result = readUnhashedControlBlockImpl(
4901 &F, F.Data, F.FileName, ClientLoadCapabilities,
4902 AllowCompatibleConfigurationMismatch, Listener.get(),
4903 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4904
4905 // If F was directly imported by another module, it's implicitly validated by
4906 // the importing module.
4907 if (DisableValidation || WasImportedBy ||
4908 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4909 return Success;
4910
4911 if (Result == Failure) {
4912 Error("malformed block record in AST file");
4913 return Failure;
4914 }
4915
4916 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4917 // If this module has already been finalized in the ModuleCache, we're stuck
4918 // with it; we can only load a single version of each module.
4919 //
4920 // This can happen when a module is imported in two contexts: in one, as a
4921 // user module; in another, as a system module (due to an import from
4922 // another module marked with the [system] flag). It usually indicates a
4923 // bug in the module map: this module should also be marked with [system].
4924 //
4925 // If -Wno-system-headers (the default), and the first import is as a
4926 // system module, then validation will fail during the as-user import,
4927 // since -Werror flags won't have been validated. However, it's reasonable
4928 // to treat this consistently as a system module.
4929 //
4930 // If -Wsystem-headers, the PCM on disk was built with
4931 // -Wno-system-headers, and the first import is as a user module, then
4932 // validation will fail during the as-system import since the PCM on disk
4933 // doesn't guarantee that -Werror was respected. However, the -Werror
4934 // flags were checked during the initial as-user import.
4935 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4936 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4937 return Success;
4938 }
4939 }
4940
4941 return Result;
4942}
4943
4944ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4945 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
4946 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
4947 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
4948 // Initialize a stream.
4949 BitstreamCursor Stream(StreamData);
4950
4951 // Sniff for the signature.
4952 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4953 // FIXME this drops the error on the floor.
4954 consumeError(std::move(Err));
4955 return Failure;
4956 }
4957
4958 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4960 return Failure;
4961
4962 // Read all of the records in the options block.
4963 RecordData Record;
4964 ASTReadResult Result = Success;
4965 while (true) {
4966 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4967 if (!MaybeEntry) {
4968 // FIXME this drops the error on the floor.
4969 consumeError(MaybeEntry.takeError());
4970 return Failure;
4971 }
4972 llvm::BitstreamEntry Entry = MaybeEntry.get();
4973
4974 switch (Entry.Kind) {
4975 case llvm::BitstreamEntry::Error:
4976 case llvm::BitstreamEntry::SubBlock:
4977 return Failure;
4978
4979 case llvm::BitstreamEntry::EndBlock:
4980 return Result;
4981
4982 case llvm::BitstreamEntry::Record:
4983 // The interesting case.
4984 break;
4985 }
4986
4987 // Read and process a record.
4988 Record.clear();
4989 StringRef Blob;
4990 Expected<unsigned> MaybeRecordType =
4991 Stream.readRecord(Entry.ID, Record, &Blob);
4992 if (!MaybeRecordType) {
4993 // FIXME this drops the error.
4994 return Failure;
4995 }
4996 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4997 case SIGNATURE:
4998 if (F) {
4999 F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5001 "Dummy AST file signature not backpatched in ASTWriter.");
5002 }
5003 break;
5004 case AST_BLOCK_HASH:
5005 if (F) {
5006 F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());
5008 "Dummy AST block hash not backpatched in ASTWriter.");
5009 }
5010 break;
5011 case DIAGNOSTIC_OPTIONS: {
5012 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5013 if (Listener && ValidateDiagnosticOptions &&
5014 !AllowCompatibleConfigurationMismatch &&
5015 ParseDiagnosticOptions(Record, Filename, Complain, *Listener))
5016 Result = OutOfDate; // Don't return early. Read the signature.
5017 break;
5018 }
5019 case HEADER_SEARCH_PATHS: {
5020 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5021 if (Listener && !AllowCompatibleConfigurationMismatch &&
5022 ParseHeaderSearchPaths(Record, Complain, *Listener))
5023 Result = ConfigurationMismatch;
5024 break;
5025 }
5027 if (!F)
5028 break;
5029 if (F->PragmaDiagMappings.empty())
5030 F->PragmaDiagMappings.swap(Record);
5031 else
5032 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
5033 Record.begin(), Record.end());
5034 break;
5036 if (F)
5037 F->SearchPathUsage = ReadBitVector(Record, Blob);
5038 break;
5039 case VFS_USAGE:
5040 if (F)
5041 F->VFSUsage = ReadBitVector(Record, Blob);
5042 break;
5043 }
5044 }
5045}
5046
5047/// Parse a record and blob containing module file extension metadata.
5050 StringRef Blob,
5051 ModuleFileExtensionMetadata &Metadata) {
5052 if (Record.size() < 4) return true;
5053
5054 Metadata.MajorVersion = Record[0];
5055 Metadata.MinorVersion = Record[1];
5056
5057 unsigned BlockNameLen = Record[2];
5058 unsigned UserInfoLen = Record[3];
5059
5060 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5061
5062 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5063 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5064 Blob.data() + BlockNameLen + UserInfoLen);
5065 return false;
5066}
5067
5068llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5069 BitstreamCursor &Stream = F.Stream;
5070
5071 RecordData Record;
5072 while (true) {
5073 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5074 if (!MaybeEntry)
5075 return MaybeEntry.takeError();
5076 llvm::BitstreamEntry Entry = MaybeEntry.get();
5077
5078 switch (Entry.Kind) {
5079 case llvm::BitstreamEntry::SubBlock:
5080 if (llvm::Error Err = Stream.SkipBlock())
5081 return Err;
5082 continue;
5083 case llvm::BitstreamEntry::EndBlock:
5084 return llvm::Error::success();
5085 case llvm::BitstreamEntry::Error:
5086 return llvm::createStringError(std::errc::illegal_byte_sequence,
5087 "malformed block record in AST file");
5088 case llvm::BitstreamEntry::Record:
5089 break;
5090 }
5091
5092 Record.clear();
5093 StringRef Blob;
5094 Expected<unsigned> MaybeRecCode =
5095 Stream.readRecord(Entry.ID, Record, &Blob);
5096 if (!MaybeRecCode)
5097 return MaybeRecCode.takeError();
5098 switch (MaybeRecCode.get()) {
5099 case EXTENSION_METADATA: {
5101 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5102 return llvm::createStringError(
5103 std::errc::illegal_byte_sequence,
5104 "malformed EXTENSION_METADATA in AST file");
5105
5106 // Find a module file extension with this block name.
5107 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
5108 if (Known == ModuleFileExtensions.end()) break;
5109
5110 // Form a reader.
5111 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
5112 F, Stream)) {
5113 F.ExtensionReaders.push_back(std::move(Reader));
5114 }
5115
5116 break;
5117 }
5118 }
5119 }
5120
5121 return llvm::Error::success();
5122}
5123
5125 assert(ContextObj && "no context to initialize");
5126 ASTContext &Context = *ContextObj;
5127
5128 // If there's a listener, notify them that we "read" the translation unit.
5129 if (DeserializationListener)
5130 DeserializationListener->DeclRead(
5132 Context.getTranslationUnitDecl());
5133
5134 // FIXME: Find a better way to deal with collisions between these
5135 // built-in types. Right now, we just ignore the problem.
5136
5137 // Load the special types.
5138 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5139 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5140 if (!Context.CFConstantStringTypeDecl)
5141 Context.setCFConstantStringType(GetType(String));
5142 }
5143
5144 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5145 QualType FileType = GetType(File);
5146 if (FileType.isNull()) {
5147 Error("FILE type is NULL");
5148 return;
5149 }
5150
5151 if (!Context.FILEDecl) {
5152 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5153 Context.setFILEDecl(Typedef->getDecl());
5154 else {
5155 const TagType *Tag = FileType->getAs<TagType>();
5156 if (!Tag) {
5157 Error("Invalid FILE type in AST file");
5158 return;
5159 }
5160 Context.setFILEDecl(Tag->getDecl());
5161 }
5162 }
5163 }
5164
5165 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5166 QualType Jmp_bufType = GetType(Jmp_buf);
5167 if (Jmp_bufType.isNull()) {
5168 Error("jmp_buf type is NULL");
5169 return;
5170 }
5171
5172 if (!Context.jmp_bufDecl) {
5173 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5174 Context.setjmp_bufDecl(Typedef->getDecl());
5175 else {
5176 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5177 if (!Tag) {
5178 Error("Invalid jmp_buf type in AST file");
5179 return;
5180 }
5181 Context.setjmp_bufDecl(Tag->getDecl());
5182 }
5183 }
5184 }
5185
5186 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5187 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
5188 if (Sigjmp_bufType.isNull()) {
5189 Error("sigjmp_buf type is NULL");
5190 return;
5191 }
5192
5193 if (!Context.sigjmp_bufDecl) {
5194 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5195 Context.setsigjmp_bufDecl(Typedef->getDecl());
5196 else {
5197 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5198 assert(Tag && "Invalid sigjmp_buf type in AST file");
5199 Context.setsigjmp_bufDecl(Tag->getDecl());
5200 }
5201 }
5202 }
5203
5204 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5205 if (Context.ObjCIdRedefinitionType.isNull())
5206 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
5207 }
5208
5209 if (TypeID ObjCClassRedef =
5211 if (Context.ObjCClassRedefinitionType.isNull())
5212 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
5213 }
5214
5215 if (TypeID ObjCSelRedef =
5216 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5217 if (Context.ObjCSelRedefinitionType.isNull())
5218 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
5219 }
5220
5221 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5222 QualType Ucontext_tType = GetType(Ucontext_t);
5223 if (Ucontext_tType.isNull()) {
5224 Error("ucontext_t type is NULL");
5225 return;
5226 }
5227
5228 if (!Context.ucontext_tDecl) {
5229 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5230 Context.setucontext_tDecl(Typedef->getDecl());
5231 else {
5232 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5233 assert(Tag && "Invalid ucontext_t type in AST file");
5234 Context.setucontext_tDecl(Tag->getDecl());
5235 }
5236 }
5237 }
5238 }
5239
5240 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5241
5242 // If there were any CUDA special declarations, deserialize them.
5243 if (!CUDASpecialDeclRefs.empty()) {
5244 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5246 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5247 }
5248
5249 // Re-export any modules that were imported by a non-module AST file.
5250 // FIXME: This does not make macro-only imports visible again.
5251 for (auto &Import : PendingImportedModules) {
5252 if (Module *Imported = getSubmodule(Import.ID)) {
5253 makeModuleVisible(Imported, Module::AllVisible,
5254 /*ImportLoc=*/Import.ImportLoc);
5255 if (Import.ImportLoc.isValid())
5256 PP.makeModuleVisible(Imported, Import.ImportLoc);
5257 // This updates visibility for Preprocessor only. For Sema, which can be
5258 // nullptr here, we do the same later, in UpdateSema().
5259 }
5260 }
5261
5262 // Hand off these modules to Sema.
5263 PendingImportedModulesSema.append(PendingImportedModules);
5264 PendingImportedModules.clear();
5265}
5266
5268 // Nothing to do for now.
5269}
5270
5271/// Reads and return the signature record from \p PCH's control block, or
5272/// else returns 0.
5274 BitstreamCursor Stream(PCH);
5275 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5276 // FIXME this drops the error on the floor.
5277 consumeError(std::move(Err));
5278 return ASTFileSignature();
5279 }
5280
5281 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5283 return ASTFileSignature();
5284
5285 // Scan for SIGNATURE inside the diagnostic options block.
5287 while (true) {
5289 Stream.advanceSkippingSubblocks();
5290 if (!MaybeEntry) {
5291 // FIXME this drops the error on the floor.
5292 consumeError(MaybeEntry.takeError());
5293 return ASTFileSignature();
5294 }
5295 llvm::BitstreamEntry Entry = MaybeEntry.get();
5296
5297 if (Entry.Kind != llvm::BitstreamEntry::Record)
5298 return ASTFileSignature();
5299
5300 Record.clear();
5301 StringRef Blob;
5302 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5303 if (!MaybeRecord) {
5304 // FIXME this drops the error on the floor.
5305 consumeError(MaybeRecord.takeError());
5306 return ASTFileSignature();
5307 }
5308 if (SIGNATURE == MaybeRecord.get()) {
5309 auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5310 assert(Signature != ASTFileSignature::createDummy() &&
5311 "Dummy AST file signature not backpatched in ASTWriter.");
5312 return Signature;
5313 }
5314 }
5315}
5316
5317/// Retrieve the name of the original source file name
5318/// directly from the AST file, without actually loading the AST
5319/// file.
5321 const std::string &ASTFileName, FileManager &FileMgr,
5322 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5323 // Open the AST file.
5324 auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5325 /*RequiresNullTerminator=*/false);
5326 if (!Buffer) {
5327 Diags.Report(diag::err_fe_unable_to_read_pch_file)