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 break;
1891 default:
1892 llvm_unreachable("missing deserialization code for annotation token");
1893 }
1894 } else {
1895 Tok.setLength(Record[Idx++]);
1896 if (IdentifierInfo *II = getLocalIdentifier(M, Record[Idx++]))
1897 Tok.setIdentifierInfo(II);
1898 }
1899 return Tok;
1900}
1901
1903 BitstreamCursor &Stream = F.MacroCursor;
1904
1905 // Keep track of where we are in the stream, then jump back there
1906 // after reading this macro.
1907 SavedStreamPosition SavedPosition(Stream);
1908
1909 if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1910 // FIXME this drops errors on the floor.
1911 consumeError(std::move(Err));
1912 return nullptr;
1913 }
1916 MacroInfo *Macro = nullptr;
1917 llvm::MutableArrayRef<Token> MacroTokens;
1918
1919 while (true) {
1920 // Advance to the next record, but if we get to the end of the block, don't
1921 // pop it (removing all the abbreviations from the cursor) since we want to
1922 // be able to reseek within the block and read entries.
1923 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1925 Stream.advanceSkippingSubblocks(Flags);
1926 if (!MaybeEntry) {
1927 Error(MaybeEntry.takeError());
1928 return Macro;
1929 }
1930 llvm::BitstreamEntry Entry = MaybeEntry.get();
1931
1932 switch (Entry.Kind) {
1933 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1934 case llvm::BitstreamEntry::Error:
1935 Error("malformed block record in AST file");
1936 return Macro;
1937 case llvm::BitstreamEntry::EndBlock:
1938 return Macro;
1939 case llvm::BitstreamEntry::Record:
1940 // The interesting case.
1941 break;
1942 }
1943
1944 // Read a record.
1945 Record.clear();
1947 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1948 RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1949 else {
1950 Error(MaybeRecType.takeError());
1951 return Macro;
1952 }
1953 switch (RecType) {
1954 case PP_MODULE_MACRO:
1956 return Macro;
1957
1960 // If we already have a macro, that means that we've hit the end
1961 // of the definition of the macro we were looking for. We're
1962 // done.
1963 if (Macro)
1964 return Macro;
1965
1966 unsigned NextIndex = 1; // Skip identifier ID.
1967 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1968 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1969 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1970 MI->setIsUsed(Record[NextIndex++]);
1971 MI->setUsedForHeaderGuard(Record[NextIndex++]);
1972 MacroTokens = MI->allocateTokens(Record[NextIndex++],
1973 PP.getPreprocessorAllocator());
1974 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1975 // Decode function-like macro info.
1976 bool isC99VarArgs = Record[NextIndex++];
1977 bool isGNUVarArgs = Record[NextIndex++];
1978 bool hasCommaPasting = Record[NextIndex++];
1979 MacroParams.clear();
1980 unsigned NumArgs = Record[NextIndex++];
1981 for (unsigned i = 0; i != NumArgs; ++i)
1982 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1983
1984 // Install function-like macro info.
1985 MI->setIsFunctionLike();
1986 if (isC99VarArgs) MI->setIsC99Varargs();
1987 if (isGNUVarArgs) MI->setIsGNUVarargs();
1988 if (hasCommaPasting) MI->setHasCommaPasting();
1989 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1990 }
1991
1992 // Remember that we saw this macro last so that we add the tokens that
1993 // form its body to it.
1994 Macro = MI;
1995
1996 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1997 Record[NextIndex]) {
1998 // We have a macro definition. Register the association
2000 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
2001 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
2002 PreprocessingRecord::PPEntityID PPID =
2003 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
2004 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
2005 PPRec.getPreprocessedEntity(PPID));
2006 if (PPDef)
2007 PPRec.RegisterMacroDefinition(Macro, PPDef);
2008 }
2009
2010 ++NumMacrosRead;
2011 break;
2012 }
2013
2014 case PP_TOKEN: {
2015 // If we see a TOKEN before a PP_MACRO_*, then the file is
2016 // erroneous, just pretend we didn't see this.
2017 if (!Macro) break;
2018 if (MacroTokens.empty()) {
2019 Error("unexpected number of macro tokens for a macro in AST file");
2020 return Macro;
2021 }
2022
2023 unsigned Idx = 0;
2024 MacroTokens[0] = ReadToken(F, Record, Idx);
2025 MacroTokens = MacroTokens.drop_front();
2026 break;
2027 }
2028 }
2029 }
2030}
2031
2034 unsigned LocalID) const {
2035 if (!M.ModuleOffsetMap.empty())
2036 ReadModuleOffsetMap(M);
2037
2040 assert(I != M.PreprocessedEntityRemap.end()
2041 && "Invalid index into preprocessed entity index remap");
2042
2043 return LocalID + I->second;
2044}
2045
2046const FileEntry *HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2047 FileManager &FileMgr = Reader.getFileManager();
2048 if (!Key.Imported) {
2049 if (auto File = FileMgr.getFile(Key.Filename))
2050 return *File;
2051 return nullptr;
2052 }
2053
2054 std::string Resolved = std::string(Key.Filename);
2055 Reader.ResolveImportedPath(M, Resolved);
2056 if (auto File = FileMgr.getFile(Resolved))
2057 return *File;
2058 return nullptr;
2059}
2060
2061unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
2062 uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];
2063 memcpy(buf, &ikey.Size, sizeof(ikey.Size));
2064 memcpy(buf + sizeof(ikey.Size), &ikey.ModTime, sizeof(ikey.ModTime));
2065 return llvm::xxh3_64bits(buf);
2066}
2067
2069HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {
2070 internal_key_type ikey = {ekey.getSize(),
2071 M.HasTimestamps ? ekey.getModificationTime() : 0,
2072 ekey.getName(), /*Imported*/ false};
2073 return ikey;
2074}
2075
2076bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
2077 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
2078 return false;
2079
2080 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
2081 return true;
2082
2083 // Determine whether the actual files are equivalent.
2084 const FileEntry *FEA = getFile(a);
2085 const FileEntry *FEB = getFile(b);
2086 return FEA && FEA == FEB;
2087}
2088
2089std::pair<unsigned, unsigned>
2090HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
2091 return readULEBKeyDataLength(d);
2092}
2093
2095HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
2096 using namespace llvm::support;
2097
2098 internal_key_type ikey;
2099 ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(d));
2100 ikey.ModTime =
2101 time_t(endian::readNext<uint64_t, llvm::endianness::little>(d));
2102 ikey.Filename = (const char *)d;
2103 ikey.Imported = true;
2104 return ikey;
2105}
2106
2108HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
2109 unsigned DataLen) {
2110 using namespace llvm::support;
2111
2112 const unsigned char *End = d + DataLen;
2113 HeaderFileInfo HFI;
2114 unsigned Flags = *d++;
2115
2116 bool Included = (Flags >> 6) & 0x01;
2117 if (Included)
2118 if (const FileEntry *FE = getFile(key))
2119 // Not using \c Preprocessor::markIncluded(), since that would attempt to
2120 // deserialize this header file info again.
2121 Reader.getPreprocessor().getIncludedFiles().insert(FE);
2122
2123 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
2124 HFI.isImport |= (Flags >> 5) & 0x01;
2125 HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
2126 HFI.DirInfo = (Flags >> 1) & 0x07;
2127 HFI.IndexHeaderMapHeader = Flags & 0x01;
2128 HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2129 M, endian::readNext<IdentifierID, llvm::endianness::little>(d));
2130 if (unsigned FrameworkOffset =
2131 endian::readNext<uint32_t, llvm::endianness::little>(d)) {
2132 // The framework offset is 1 greater than the actual offset,
2133 // since 0 is used as an indicator for "no framework name".
2134 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
2135 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
2136 }
2137
2138 assert((End - d) % 4 == 0 &&
2139 "Wrong data length in HeaderFileInfo deserialization");
2140 while (d != End) {
2141 uint32_t LocalSMID =
2142 endian::readNext<uint32_t, llvm::endianness::little>(d);
2143 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);
2144 LocalSMID >>= 3;
2145
2146 // This header is part of a module. Associate it with the module to enable
2147 // implicit module import.
2148 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
2149 Module *Mod = Reader.getSubmodule(GlobalSMID);
2150 FileManager &FileMgr = Reader.getFileManager();
2151 ModuleMap &ModMap =
2152 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
2153
2154 std::string Filename = std::string(key.Filename);
2155 if (key.Imported)
2156 Reader.ResolveImportedPath(M, Filename);
2157 if (auto FE = FileMgr.getOptionalFileRef(Filename)) {
2158 // FIXME: NameAsWritten
2159 Module::Header H = {std::string(key.Filename), "", *FE};
2160 ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);
2161 }
2162 HFI.mergeModuleMembership(HeaderRole);
2163 }
2164
2165 // This HeaderFileInfo was externally loaded.
2166 HFI.External = true;
2167 HFI.IsValid = true;
2168 return HFI;
2169}
2170
2172 uint32_t MacroDirectivesOffset) {
2173 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
2174 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
2175}
2176
2178 // Note that we are loading defined macros.
2179 Deserializing Macros(this);
2180
2181 for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
2182 BitstreamCursor &MacroCursor = I.MacroCursor;
2183
2184 // If there was no preprocessor block, skip this file.
2185 if (MacroCursor.getBitcodeBytes().empty())
2186 continue;
2187
2188 BitstreamCursor Cursor = MacroCursor;
2189 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
2190 Error(std::move(Err));
2191 return;
2192 }
2193
2195 while (true) {
2196 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
2197 if (!MaybeE) {
2198 Error(MaybeE.takeError());
2199 return;
2200 }
2201 llvm::BitstreamEntry E = MaybeE.get();
2202
2203 switch (E.Kind) {
2204 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
2205 case llvm::BitstreamEntry::Error:
2206 Error("malformed block record in AST file");
2207 return;
2208 case llvm::BitstreamEntry::EndBlock:
2209 goto NextCursor;
2210
2211 case llvm::BitstreamEntry::Record: {
2212 Record.clear();
2213 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
2214 if (!MaybeRecord) {
2215 Error(MaybeRecord.takeError());
2216 return;
2217 }
2218 switch (MaybeRecord.get()) {
2219 default: // Default behavior: ignore.
2220 break;
2221
2224 IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
2225 if (II->isOutOfDate())
2226 updateOutOfDateIdentifier(*II);
2227 break;
2228 }
2229
2230 case PP_TOKEN:
2231 // Ignore tokens.
2232 break;
2233 }
2234 break;
2235 }
2236 }
2237 }
2238 NextCursor: ;
2239 }
2240}
2241
2242namespace {
2243
2244 /// Visitor class used to look up identifirs in an AST file.
2245 class IdentifierLookupVisitor {
2246 StringRef Name;
2247 unsigned NameHash;
2248 unsigned PriorGeneration;
2249 unsigned &NumIdentifierLookups;
2250 unsigned &NumIdentifierLookupHits;
2251 IdentifierInfo *Found = nullptr;
2252
2253 public:
2254 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2255 unsigned &NumIdentifierLookups,
2256 unsigned &NumIdentifierLookupHits)
2257 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2258 PriorGeneration(PriorGeneration),
2259 NumIdentifierLookups(NumIdentifierLookups),
2260 NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2261
2262 bool operator()(ModuleFile &M) {
2263 // If we've already searched this module file, skip it now.
2264 if (M.Generation <= PriorGeneration)
2265 return true;
2266
2269 if (!IdTable)
2270 return false;
2271
2272 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2273 Found);
2274 ++NumIdentifierLookups;
2275 ASTIdentifierLookupTable::iterator Pos =
2276 IdTable->find_hashed(Name, NameHash, &Trait);
2277 if (Pos == IdTable->end())
2278 return false;
2279
2280 // Dereferencing the iterator has the effect of building the
2281 // IdentifierInfo node and populating it with the various
2282 // declarations it needs.
2283 ++NumIdentifierLookupHits;
2284 Found = *Pos;
2285 return true;
2286 }
2287
2288 // Retrieve the identifier info found within the module
2289 // files.
2290 IdentifierInfo *getIdentifierInfo() const { return Found; }
2291 };
2292
2293} // namespace
2294
2296 // Note that we are loading an identifier.
2297 Deserializing AnIdentifier(this);
2298
2299 unsigned PriorGeneration = 0;
2300 if (getContext().getLangOpts().Modules)
2301 PriorGeneration = IdentifierGeneration[&II];
2302
2303 // If there is a global index, look there first to determine which modules
2304 // provably do not have any results for this identifier.
2306 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2307 if (!loadGlobalIndex()) {
2308 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2309 HitsPtr = &Hits;
2310 }
2311 }
2312
2313 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2314 NumIdentifierLookups,
2315 NumIdentifierLookupHits);
2316 ModuleMgr.visit(Visitor, HitsPtr);
2317 markIdentifierUpToDate(&II);
2318}
2319
2321 if (!II)
2322 return;
2323
2324 const_cast<IdentifierInfo *>(II)->setOutOfDate(false);
2325
2326 // Update the generation for this identifier.
2327 if (getContext().getLangOpts().Modules)
2328 IdentifierGeneration[II] = getGeneration();
2329}
2330
2332 const PendingMacroInfo &PMInfo) {
2333 ModuleFile &M = *PMInfo.M;
2334
2335 BitstreamCursor &Cursor = M.MacroCursor;
2336 SavedStreamPosition SavedPosition(Cursor);
2337 if (llvm::Error Err =
2338 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2339 Error(std::move(Err));
2340 return;
2341 }
2342
2343 struct ModuleMacroRecord {
2344 SubmoduleID SubModID;
2345 MacroInfo *MI;
2347 };
2349
2350 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2351 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2352 // macro histroy.
2354 while (true) {
2356 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2357 if (!MaybeEntry) {
2358 Error(MaybeEntry.takeError());
2359 return;
2360 }
2361 llvm::BitstreamEntry Entry = MaybeEntry.get();
2362
2363 if (Entry.Kind != llvm::BitstreamEntry::Record) {
2364 Error("malformed block record in AST file");
2365 return;
2366 }
2367
2368 Record.clear();
2369 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2370 if (!MaybePP) {
2371 Error(MaybePP.takeError());
2372 return;
2373 }
2374 switch ((PreprocessorRecordTypes)MaybePP.get()) {
2376 break;
2377
2378 case PP_MODULE_MACRO: {
2379 ModuleMacros.push_back(ModuleMacroRecord());
2380 auto &Info = ModuleMacros.back();
2381 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2382 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2383 for (int I = 2, N = Record.size(); I != N; ++I)
2384 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2385 continue;
2386 }
2387
2388 default:
2389 Error("malformed block record in AST file");
2390 return;
2391 }
2392
2393 // We found the macro directive history; that's the last record
2394 // for this macro.
2395 break;
2396 }
2397
2398 // Module macros are listed in reverse dependency order.
2399 {
2400 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2402 for (auto &MMR : ModuleMacros) {
2403 Overrides.clear();
2404 for (unsigned ModID : MMR.Overrides) {
2405 Module *Mod = getSubmodule(ModID);
2406 auto *Macro = PP.getModuleMacro(Mod, II);
2407 assert(Macro && "missing definition for overridden macro");
2408 Overrides.push_back(Macro);
2409 }
2410
2411 bool Inserted = false;
2412 Module *Owner = getSubmodule(MMR.SubModID);
2413 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2414 }
2415 }
2416
2417 // Don't read the directive history for a module; we don't have anywhere
2418 // to put it.
2419 if (M.isModule())
2420 return;
2421
2422 // Deserialize the macro directives history in reverse source-order.
2423 MacroDirective *Latest = nullptr, *Earliest = nullptr;
2424 unsigned Idx = 0, N = Record.size();
2425 while (Idx < N) {
2426 MacroDirective *MD = nullptr;
2427 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2429 switch (K) {
2431 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2432 MD = PP.AllocateDefMacroDirective(MI, Loc);
2433 break;
2434 }
2436 MD = PP.AllocateUndefMacroDirective(Loc);
2437 break;
2439 bool isPublic = Record[Idx++];
2440 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2441 break;
2442 }
2443
2444 if (!Latest)
2445 Latest = MD;
2446 if (Earliest)
2447 Earliest->setPrevious(MD);
2448 Earliest = MD;
2449 }
2450
2451 if (Latest)
2452 PP.setLoadedMacroDirective(II, Earliest, Latest);
2453}
2454
2455bool ASTReader::shouldDisableValidationForFile(
2456 const serialization::ModuleFile &M) const {
2457 if (DisableValidationKind == DisableValidationForModuleKind::None)
2458 return false;
2459
2460 // If a PCH is loaded and validation is disabled for PCH then disable
2461 // validation for the PCH and the modules it loads.
2462 ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);
2463
2464 switch (K) {
2465 case MK_MainFile:
2466 case MK_Preamble:
2467 case MK_PCH:
2468 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2469 case MK_ImplicitModule:
2470 case MK_ExplicitModule:
2471 case MK_PrebuiltModule:
2472 return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2473 }
2474
2475 return false;
2476}
2477
2478InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2479 // If this ID is bogus, just return an empty input file.
2480 if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2481 return InputFileInfo();
2482
2483 // If we've already loaded this input file, return it.
2484 if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2485 return F.InputFileInfosLoaded[ID - 1];
2486
2487 // Go find this input file.
2488 BitstreamCursor &Cursor = F.InputFilesCursor;
2489 SavedStreamPosition SavedPosition(Cursor);
2490 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2491 F.InputFileOffsets[ID - 1])) {
2492 // FIXME this drops errors on the floor.
2493 consumeError(std::move(Err));
2494 }
2495
2496 Expected<unsigned> MaybeCode = Cursor.ReadCode();
2497 if (!MaybeCode) {
2498 // FIXME this drops errors on the floor.
2499 consumeError(MaybeCode.takeError());
2500 }
2501 unsigned Code = MaybeCode.get();
2502 RecordData Record;
2503 StringRef Blob;
2504
2505 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2506 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2507 "invalid record type for input file");
2508 else {
2509 // FIXME this drops errors on the floor.
2510 consumeError(Maybe.takeError());
2511 }
2512
2513 assert(Record[0] == ID && "Bogus stored ID or offset");
2514 InputFileInfo R;
2515 R.StoredSize = static_cast<off_t>(Record[1]);
2516 R.StoredTime = static_cast<time_t>(Record[2]);
2517 R.Overridden = static_cast<bool>(Record[3]);
2518 R.Transient = static_cast<bool>(Record[4]);
2519 R.TopLevel = static_cast<bool>(Record[5]);
2520 R.ModuleMap = static_cast<bool>(Record[6]);
2521 std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2522 uint16_t AsRequestedLength = Record[7];
2523
2524 std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2525 std::string Name = Blob.substr(AsRequestedLength).str();
2526
2527 ResolveImportedPath(F, NameAsRequested);
2528 ResolveImportedPath(F, Name);
2529
2530 if (Name.empty())
2531 Name = NameAsRequested;
2532
2533 return std::make_pair(std::move(NameAsRequested), std::move(Name));
2534 }();
2535
2536 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2537 if (!MaybeEntry) // FIXME this drops errors on the floor.
2538 consumeError(MaybeEntry.takeError());
2539 llvm::BitstreamEntry Entry = MaybeEntry.get();
2540 assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2541 "expected record type for input file hash");
2542
2543 Record.clear();
2544 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2545 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2546 "invalid record type for input file hash");
2547 else {
2548 // FIXME this drops errors on the floor.
2549 consumeError(Maybe.takeError());
2550 }
2551 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2552 static_cast<uint64_t>(Record[0]);
2553
2554 // Note that we've loaded this input file info.
2555 F.InputFileInfosLoaded[ID - 1] = R;
2556 return R;
2557}
2558
2559static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2560InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2561 // If this ID is bogus, just return an empty input file.
2562 if (ID == 0 || ID > F.InputFilesLoaded.size())
2563 return InputFile();
2564
2565 // If we've already loaded this input file, return it.
2566 if (F.InputFilesLoaded[ID-1].getFile())
2567 return F.InputFilesLoaded[ID-1];
2568
2569 if (F.InputFilesLoaded[ID-1].isNotFound())
2570 return InputFile();
2571
2572 // Go find this input file.
2573 BitstreamCursor &Cursor = F.InputFilesCursor;
2574 SavedStreamPosition SavedPosition(Cursor);
2575 if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2576 F.InputFileOffsets[ID - 1])) {
2577 // FIXME this drops errors on the floor.
2578 consumeError(std::move(Err));
2579 }
2580
2581 InputFileInfo FI = getInputFileInfo(F, ID);
2582 off_t StoredSize = FI.StoredSize;
2583 time_t StoredTime = FI.StoredTime;
2584 bool Overridden = FI.Overridden;
2585 bool Transient = FI.Transient;
2586 StringRef Filename = FI.FilenameAsRequested;
2587 uint64_t StoredContentHash = FI.ContentHash;
2588
2589 // For standard C++ modules, we don't need to check the inputs.
2590 bool SkipChecks = F.StandardCXXModule;
2591
2592 const HeaderSearchOptions &HSOpts =
2593 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2594
2595 // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++20
2596 // modules.
2598 SkipChecks = false;
2599 Overridden = false;
2600 }
2601
2602 auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
2603
2604 // For an overridden file, create a virtual file with the stored
2605 // size/timestamp.
2606 if ((Overridden || Transient || SkipChecks) && !File)
2607 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2608
2609 if (!File) {
2610 if (Complain) {
2611 std::string ErrorStr = "could not find file '";
2612 ErrorStr += Filename;
2613 ErrorStr += "' referenced by AST file '";
2614 ErrorStr += F.FileName;
2615 ErrorStr += "'";
2616 Error(ErrorStr);
2617 }
2618 // Record that we didn't find the file.
2620 return InputFile();
2621 }
2622
2623 // Check if there was a request to override the contents of the file
2624 // that was part of the precompiled header. Overriding such a file
2625 // can lead to problems when lexing using the source locations from the
2626 // PCH.
2627 SourceManager &SM = getSourceManager();
2628 // FIXME: Reject if the overrides are different.
2629 if ((!Overridden && !Transient) && !SkipChecks &&
2630 SM.isFileOverridden(*File)) {
2631 if (Complain)
2632 Error(diag::err_fe_pch_file_overridden, Filename);
2633
2634 // After emitting the diagnostic, bypass the overriding file to recover
2635 // (this creates a separate FileEntry).
2636 File = SM.bypassFileContentsOverride(*File);
2637 if (!File) {
2639 return InputFile();
2640 }
2641 }
2642
2643 struct Change {
2644 enum ModificationKind {
2645 Size,
2646 ModTime,
2647 Content,
2648 None,
2649 } Kind;
2650 std::optional<int64_t> Old = std::nullopt;
2651 std::optional<int64_t> New = std::nullopt;
2652 };
2653 auto HasInputContentChanged = [&](Change OriginalChange) {
2654 assert(ValidateASTInputFilesContent &&
2655 "We should only check the content of the inputs with "
2656 "ValidateASTInputFilesContent enabled.");
2657
2658 if (StoredContentHash == 0)
2659 return OriginalChange;
2660
2661 auto MemBuffOrError = FileMgr.getBufferForFile(*File);
2662 if (!MemBuffOrError) {
2663 if (!Complain)
2664 return OriginalChange;
2665 std::string ErrorStr = "could not get buffer for file '";
2666 ErrorStr += File->getName();
2667 ErrorStr += "'";
2668 Error(ErrorStr);
2669 return OriginalChange;
2670 }
2671
2672 auto ContentHash = xxh3_64bits(MemBuffOrError.get()->getBuffer());
2673 if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2674 return Change{Change::None};
2675
2676 return Change{Change::Content};
2677 };
2678 auto HasInputFileChanged = [&]() {
2679 if (StoredSize != File->getSize())
2680 return Change{Change::Size, StoredSize, File->getSize()};
2681 if (!shouldDisableValidationForFile(F) && StoredTime &&
2682 StoredTime != File->getModificationTime()) {
2683 Change MTimeChange = {Change::ModTime, StoredTime,
2684 File->getModificationTime()};
2685
2686 // In case the modification time changes but not the content,
2687 // accept the cached file as legit.
2688 if (ValidateASTInputFilesContent)
2689 return HasInputContentChanged(MTimeChange);
2690
2691 return MTimeChange;
2692 }
2693 return Change{Change::None};
2694 };
2695
2696 bool IsOutOfDate = false;
2697 auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();
2698 // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent
2699 // enabled, it is better to check the contents of the inputs. Since we can't
2700 // get correct modified time information for inputs from overriden inputs.
2701 if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&
2702 F.StandardCXXModule && FileChange.Kind == Change::None)
2703 FileChange = HasInputContentChanged(FileChange);
2704
2705 // When we have StoredTime equal to zero and ValidateASTInputFilesContent,
2706 // it is better to check the content of the input files because we cannot rely
2707 // on the file modification time, which will be the same (zero) for these
2708 // files.
2709 if (!StoredTime && ValidateASTInputFilesContent &&
2710 FileChange.Kind == Change::None)
2711 FileChange = HasInputContentChanged(FileChange);
2712
2713 // For an overridden file, there is nothing to validate.
2714 if (!Overridden && FileChange.Kind != Change::None) {
2715 if (Complain && !Diags.isDiagnosticInFlight()) {
2716 // Build a list of the PCH imports that got us here (in reverse).
2717 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2718 while (!ImportStack.back()->ImportedBy.empty())
2719 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2720
2721 // The top-level PCH is stale.
2722 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2723 Diag(diag::err_fe_ast_file_modified)
2724 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2725 << TopLevelPCHName << FileChange.Kind
2726 << (FileChange.Old && FileChange.New)
2727 << llvm::itostr(FileChange.Old.value_or(0))
2728 << llvm::itostr(FileChange.New.value_or(0));
2729
2730 // Print the import stack.
2731 if (ImportStack.size() > 1) {
2732 Diag(diag::note_pch_required_by)
2733 << Filename << ImportStack[0]->FileName;
2734 for (unsigned I = 1; I < ImportStack.size(); ++I)
2735 Diag(diag::note_pch_required_by)
2736 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2737 }
2738
2739 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2740 }
2741
2742 IsOutOfDate = true;
2743 }
2744 // FIXME: If the file is overridden and we've already opened it,
2745 // issue an error (or split it into a separate FileEntry).
2746
2747 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2748
2749 // Note that we've loaded this input file.
2750 F.InputFilesLoaded[ID-1] = IF;
2751 return IF;
2752}
2753
2754/// If we are loading a relocatable PCH or module file, and the filename
2755/// is not an absolute path, add the system or module root to the beginning of
2756/// the file name.
2758 // Resolve relative to the base directory, if we have one.
2759 if (!M.BaseDirectory.empty())
2760 return ResolveImportedPath(Filename, M.BaseDirectory);
2761}
2762
2763void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2764 if (Filename.empty() || llvm::sys::path::is_absolute(Filename) ||
2765 Filename == "<built-in>" || Filename == "<command line>")
2766 return;
2767
2768 SmallString<128> Buffer;
2769 llvm::sys::path::append(Buffer, Prefix, Filename);
2770 Filename.assign(Buffer.begin(), Buffer.end());
2771}
2772
2773static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2774 switch (ARR) {
2775 case ASTReader::Failure: return true;
2776 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2777 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2780 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2781 case ASTReader::HadErrors: return true;
2782 case ASTReader::Success: return false;
2783 }
2784
2785 llvm_unreachable("unknown ASTReadResult");
2786}
2787
2788ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2789 BitstreamCursor &Stream, StringRef Filename,
2790 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
2791 ASTReaderListener &Listener, std::string &SuggestedPredefines) {
2792 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2793 // FIXME this drops errors on the floor.
2794 consumeError(std::move(Err));
2795 return Failure;
2796 }
2797
2798 // Read all of the records in the options block.
2799 RecordData Record;
2800 ASTReadResult Result = Success;
2801 while (true) {
2802 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2803 if (!MaybeEntry) {
2804 // FIXME this drops errors on the floor.
2805 consumeError(MaybeEntry.takeError());
2806 return Failure;
2807 }
2808 llvm::BitstreamEntry Entry = MaybeEntry.get();
2809
2810 switch (Entry.Kind) {
2811 case llvm::BitstreamEntry::Error:
2812 case llvm::BitstreamEntry::SubBlock:
2813 return Failure;
2814
2815 case llvm::BitstreamEntry::EndBlock:
2816 return Result;
2817
2818 case llvm::BitstreamEntry::Record:
2819 // The interesting case.
2820 break;
2821 }
2822
2823 // Read and process a record.
2824 Record.clear();
2825 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2826 if (!MaybeRecordType) {
2827 // FIXME this drops errors on the floor.
2828 consumeError(MaybeRecordType.takeError());
2829 return Failure;
2830 }
2831 switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2832 case LANGUAGE_OPTIONS: {
2833 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2834 if (ParseLanguageOptions(Record, Filename, Complain, Listener,
2835 AllowCompatibleConfigurationMismatch))
2836 Result = ConfigurationMismatch;
2837 break;
2838 }
2839
2840 case TARGET_OPTIONS: {
2841 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2842 if (ParseTargetOptions(Record, Filename, Complain, Listener,
2843 AllowCompatibleConfigurationMismatch))
2844 Result = ConfigurationMismatch;
2845 break;
2846 }
2847
2848 case FILE_SYSTEM_OPTIONS: {
2849 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2850 if (!AllowCompatibleConfigurationMismatch &&
2851 ParseFileSystemOptions(Record, Complain, Listener))
2852 Result = ConfigurationMismatch;
2853 break;
2854 }
2855
2856 case HEADER_SEARCH_OPTIONS: {
2857 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2858 if (!AllowCompatibleConfigurationMismatch &&
2859 ParseHeaderSearchOptions(Record, Filename, Complain, Listener))
2860 Result = ConfigurationMismatch;
2861 break;
2862 }
2863
2865 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2866 if (!AllowCompatibleConfigurationMismatch &&
2867 ParsePreprocessorOptions(Record, Filename, Complain, Listener,
2868 SuggestedPredefines))
2869 Result = ConfigurationMismatch;
2870 break;
2871 }
2872 }
2873}
2874
2876ASTReader::ReadControlBlock(ModuleFile &F,
2878 const ModuleFile *ImportedBy,
2879 unsigned ClientLoadCapabilities) {
2880 BitstreamCursor &Stream = F.Stream;
2881
2882 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2883 Error(std::move(Err));
2884 return Failure;
2885 }
2886
2887 // Lambda to read the unhashed control block the first time it's called.
2888 //
2889 // For PCM files, the unhashed control block cannot be read until after the
2890 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still
2891 // need to look ahead before reading the IMPORTS record. For consistency,
2892 // this block is always read somehow (see BitstreamEntry::EndBlock).
2893 bool HasReadUnhashedControlBlock = false;
2894 auto readUnhashedControlBlockOnce = [&]() {
2895 if (!HasReadUnhashedControlBlock) {
2896 HasReadUnhashedControlBlock = true;
2897 if (ASTReadResult Result =
2898 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2899 return Result;
2900 }
2901 return Success;
2902 };
2903
2904 bool DisableValidation = shouldDisableValidationForFile(F);
2905
2906 // Read all of the records and blocks in the control block.
2907 RecordData Record;
2908 unsigned NumInputs = 0;
2909 unsigned NumUserInputs = 0;
2910 StringRef BaseDirectoryAsWritten;
2911 while (true) {
2912 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2913 if (!MaybeEntry) {
2914 Error(MaybeEntry.takeError());
2915 return Failure;
2916 }
2917 llvm::BitstreamEntry Entry = MaybeEntry.get();
2918
2919 switch (Entry.Kind) {
2920 case llvm::BitstreamEntry::Error:
2921 Error("malformed block record in AST file");
2922 return Failure;
2923 case llvm::BitstreamEntry::EndBlock: {
2924 // Validate the module before returning. This call catches an AST with
2925 // no module name and no imports.
2926 if (ASTReadResult Result = readUnhashedControlBlockOnce())
2927 return Result;
2928
2929 // Validate input files.
2930 const HeaderSearchOptions &HSOpts =
2931 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2932
2933 // All user input files reside at the index range [0, NumUserInputs), and
2934 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2935 // loaded module files, ignore missing inputs.
2936 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2937 F.Kind != MK_PrebuiltModule) {
2938 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2939
2940 // If we are reading a module, we will create a verification timestamp,
2941 // so we verify all input files. Otherwise, verify only user input
2942 // files.
2943
2944 unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
2948 N = NumUserInputs;
2949
2950 for (unsigned I = 0; I < N; ++I) {
2951 InputFile IF = getInputFile(F, I+1, Complain);
2952 if (!IF.getFile() || IF.isOutOfDate())
2953 return OutOfDate;
2954 }
2955 }
2956
2957 if (Listener)
2958 Listener->visitModuleFile(F.FileName, F.Kind);
2959
2960 if (Listener && Listener->needsInputFileVisitation()) {
2961 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2962 : NumUserInputs;
2963 for (unsigned I = 0; I < N; ++I) {
2964 bool IsSystem = I >= NumUserInputs;
2965 InputFileInfo FI = getInputFileInfo(F, I + 1);
2966 Listener->visitInputFile(
2967 FI.FilenameAsRequested, IsSystem, FI.Overridden,
2969 }
2970 }
2971
2972 return Success;
2973 }
2974
2975 case llvm::BitstreamEntry::SubBlock:
2976 switch (Entry.ID) {
2978 F.InputFilesCursor = Stream;
2979 if (llvm::Error Err = Stream.SkipBlock()) {
2980 Error(std::move(Err));
2981 return Failure;
2982 }
2983 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2984 Error("malformed block record in AST file");
2985 return Failure;
2986 }
2987 F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
2988 continue;
2989
2990 case OPTIONS_BLOCK_ID:
2991 // If we're reading the first module for this group, check its options
2992 // are compatible with ours. For modules it imports, no further checking
2993 // is required, because we checked them when we built it.
2994 if (Listener && !ImportedBy) {
2995 // Should we allow the configuration of the module file to differ from
2996 // the configuration of the current translation unit in a compatible
2997 // way?
2998 //
2999 // FIXME: Allow this for files explicitly specified with -include-pch.
3000 bool AllowCompatibleConfigurationMismatch =
3002
3003 ASTReadResult Result =
3004 ReadOptionsBlock(Stream, F.FileName, ClientLoadCapabilities,
3005 AllowCompatibleConfigurationMismatch, *Listener,
3006 SuggestedPredefines);
3007 if (Result == Failure) {
3008 Error("malformed block record in AST file");
3009 return Result;
3010 }
3011
3012 if (DisableValidation ||
3013 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
3014 Result = Success;
3015
3016 // If we can't load the module, exit early since we likely
3017 // will rebuild the module anyway. The stream may be in the
3018 // middle of a block.
3019 if (Result != Success)
3020 return Result;
3021 } else if (llvm::Error Err = Stream.SkipBlock()) {
3022 Error(std::move(Err));
3023 return Failure;
3024 }
3025 continue;
3026
3027 default:
3028 if (llvm::Error Err = Stream.SkipBlock()) {
3029 Error(std::move(Err));
3030 return Failure;
3031 }
3032 continue;
3033 }
3034
3035 case llvm::BitstreamEntry::Record:
3036 // The interesting case.
3037 break;
3038 }
3039
3040 // Read and process a record.
3041 Record.clear();
3042 StringRef Blob;
3043 Expected<unsigned> MaybeRecordType =
3044 Stream.readRecord(Entry.ID, Record, &Blob);
3045 if (!MaybeRecordType) {
3046 Error(MaybeRecordType.takeError());
3047 return Failure;
3048 }
3049 switch ((ControlRecordTypes)MaybeRecordType.get()) {
3050 case METADATA: {
3051 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
3052 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3053 Diag(Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old
3054 : diag::err_ast_file_version_too_new)
3056 return VersionMismatch;
3057 }
3058
3059 bool hasErrors = Record[7];
3060 if (hasErrors && !DisableValidation) {
3061 // If requested by the caller and the module hasn't already been read
3062 // or compiled, mark modules on error as out-of-date.
3063 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
3064 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3065 return OutOfDate;
3066
3067 if (!AllowASTWithCompilerErrors) {
3068 Diag(diag::err_ast_file_with_compiler_errors)
3070 return HadErrors;
3071 }
3072 }
3073 if (hasErrors) {
3074 Diags.ErrorOccurred = true;
3075 Diags.UncompilableErrorOccurred = true;
3076 Diags.UnrecoverableErrorOccurred = true;
3077 }
3078
3079 F.RelocatablePCH = Record[4];
3080 // Relative paths in a relocatable PCH are relative to our sysroot.
3081 if (F.RelocatablePCH)
3082 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
3083
3085
3086 F.HasTimestamps = Record[6];
3087
3088 const std::string &CurBranch = getClangFullRepositoryVersion();
3089 StringRef ASTBranch = Blob;
3090 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
3091 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3092 Diag(diag::err_ast_file_different_branch)
3093 << moduleKindForDiagnostic(F.Kind) << F.FileName << ASTBranch
3094 << CurBranch;
3095 return VersionMismatch;
3096 }
3097 break;
3098 }
3099
3100 case IMPORTS: {
3101 // Validate the AST before processing any imports (otherwise, untangling
3102 // them can be error-prone and expensive). A module will have a name and
3103 // will already have been validated, but this catches the PCH case.
3104 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3105 return Result;
3106
3107 // Load each of the imported PCH files.
3108 unsigned Idx = 0, N = Record.size();
3109 while (Idx < N) {
3110 // Read information about the AST file.
3111 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
3112 // Whether we're importing a standard c++ module.
3113 bool IsImportingStdCXXModule = Record[Idx++];
3114 // The import location will be the local one for now; we will adjust
3115 // all import locations of module imports after the global source
3116 // location info are setup, in ReadAST.
3117 auto [ImportLoc, ImportModuleFileIndex] =
3118 ReadUntranslatedSourceLocation(Record[Idx++]);
3119 // The import location must belong to the current module file itself.
3120 assert(ImportModuleFileIndex == 0);
3121 off_t StoredSize = !IsImportingStdCXXModule ? (off_t)Record[Idx++] : 0;
3122 time_t StoredModTime =
3123 !IsImportingStdCXXModule ? (time_t)Record[Idx++] : 0;
3124
3125 ASTFileSignature StoredSignature;
3126 if (!IsImportingStdCXXModule) {
3127 auto FirstSignatureByte = Record.begin() + Idx;
3128 StoredSignature = ASTFileSignature::create(
3129 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
3131 }
3132
3133 std::string ImportedName = ReadString(Record, Idx);
3134 std::string ImportedFile;
3135
3136 // For prebuilt and explicit modules first consult the file map for
3137 // an override. Note that here we don't search prebuilt module
3138 // directories if we're not importing standard c++ module, only the
3139 // explicit name to file mappings. Also, we will still verify the
3140 // size/signature making sure it is essentially the same file but
3141 // perhaps in a different location.
3142 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
3143 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
3144 ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);
3145
3146 // For C++20 Modules, we won't record the path to the imported modules
3147 // in the BMI
3148 if (!IsImportingStdCXXModule) {
3149 if (ImportedFile.empty()) {
3150 // Use BaseDirectoryAsWritten to ensure we use the same path in the
3151 // ModuleCache as when writing.
3152 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
3153 } else
3154 SkipPath(Record, Idx);
3155 } else if (ImportedFile.empty()) {
3156 Diag(clang::diag::err_failed_to_find_module_file) << ImportedName;
3157 return Missing;
3158 }
3159
3160 // If our client can't cope with us being out of date, we can't cope with
3161 // our dependency being missing.
3162 unsigned Capabilities = ClientLoadCapabilities;
3163 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3164 Capabilities &= ~ARR_Missing;
3165
3166 // Load the AST file.
3167 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
3168 Loaded, StoredSize, StoredModTime,
3169 StoredSignature, Capabilities);
3170
3171 // If we diagnosed a problem, produce a backtrace.
3172 bool recompilingFinalized =
3173 Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
3174 getModuleManager().getModuleCache().isPCMFinal(F.FileName);
3175 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
3176 Diag(diag::note_module_file_imported_by)
3177 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
3178 if (recompilingFinalized)
3179 Diag(diag::note_module_file_conflict);
3180
3181 switch (Result) {
3182 case Failure: return Failure;
3183 // If we have to ignore the dependency, we'll have to ignore this too.
3184 case Missing:
3185 case OutOfDate: return OutOfDate;
3186 case VersionMismatch: return VersionMismatch;
3187 case ConfigurationMismatch: return ConfigurationMismatch;
3188 case HadErrors: return HadErrors;
3189 case Success: break;
3190 }
3191 }
3192 break;
3193 }
3194
3195 case ORIGINAL_FILE:
3196 F.OriginalSourceFileID = FileID::get(Record[0]);
3197 F.ActualOriginalSourceFileName = std::string(Blob);
3199 ResolveImportedPath(F, F.OriginalSourceFileName);
3200 break;
3201
3202 case ORIGINAL_FILE_ID:
3203 F.OriginalSourceFileID = FileID::get(Record[0]);
3204 break;
3205
3206 case MODULE_NAME:
3207 F.ModuleName = std::string(Blob);
3208 Diag(diag::remark_module_import)
3209 << F.ModuleName << F.FileName << (ImportedBy ? true : false)
3210 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
3211 if (Listener)
3212 Listener->ReadModuleName(F.ModuleName);
3213
3214 // Validate the AST as soon as we have a name so we can exit early on
3215 // failure.
3216 if (ASTReadResult Result = readUnhashedControlBlockOnce())
3217 return Result;
3218
3219 break;
3220
3221 case MODULE_DIRECTORY: {
3222 // Save the BaseDirectory as written in the PCM for computing the module
3223 // filename for the ModuleCache.
3224 BaseDirectoryAsWritten = Blob;
3225 assert(!F.ModuleName.empty() &&
3226 "MODULE_DIRECTORY found before MODULE_NAME");
3227 F.BaseDirectory = std::string(Blob);
3228 if (!PP.getPreprocessorOpts().ModulesCheckRelocated)
3229 break;
3230 // If we've already loaded a module map file covering this module, we may
3231 // have a better path for it (relative to the current build).
3232 Module *M = PP.getHeaderSearchInfo().lookupModule(
3233 F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
3234 /*AllowExtraModuleMapSearch*/ true);
3235 if (M && M->Directory) {
3236 // If we're implicitly loading a module, the base directory can't
3237 // change between the build and use.
3238 // Don't emit module relocation error if we have -fno-validate-pch
3239 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3242 auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(Blob);
3243 if (!BuildDir || *BuildDir != M->Directory) {
3244 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3245 Diag(diag::err_imported_module_relocated)
3246 << F.ModuleName << Blob << M->Directory->getName();
3247 return OutOfDate;
3248 }
3249 }
3250 F.BaseDirectory = std::string(M->Directory->getName());
3251 }
3252 break;
3253 }
3254
3255 case MODULE_MAP_FILE:
3256 if (ASTReadResult Result =
3257 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
3258 return Result;
3259 break;
3260
3261 case INPUT_FILE_OFFSETS:
3262 NumInputs = Record[0];
3263 NumUserInputs = Record[1];
3265 (const llvm::support::unaligned_uint64_t *)Blob.data();
3266 F.InputFilesLoaded.resize(NumInputs);
3267 F.InputFileInfosLoaded.resize(NumInputs);
3268 F.NumUserInputFiles = NumUserInputs;
3269 break;
3270 }
3271 }
3272}
3273
3274llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
3275 unsigned ClientLoadCapabilities) {
3276 BitstreamCursor &Stream = F.Stream;
3277
3278 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
3279 return Err;
3280 F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
3281
3282 // Read all of the records and blocks for the AST file.
3283 RecordData Record;
3284 while (true) {
3285 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
3286 if (!MaybeEntry)
3287 return MaybeEntry.takeError();
3288 llvm::BitstreamEntry Entry = MaybeEntry.get();
3289
3290 switch (Entry.Kind) {
3291 case llvm::BitstreamEntry::Error:
3292 return llvm::createStringError(
3293 std::errc::illegal_byte_sequence,
3294 "error at end of module block in AST file");
3295 case llvm::BitstreamEntry::EndBlock:
3296 // Outside of C++, we do not store a lookup map for the translation unit.
3297 // Instead, mark it as needing a lookup map to be built if this module
3298 // contains any declarations lexically within it (which it always does!).
3299 // This usually has no cost, since we very rarely need the lookup map for
3300 // the translation unit outside C++.
3301 if (ASTContext *Ctx = ContextObj) {
3302 DeclContext *DC = Ctx->getTranslationUnitDecl();
3303 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3305 }
3306
3307 return llvm::Error::success();
3308 case llvm::BitstreamEntry::SubBlock:
3309 switch (Entry.ID) {
3310 case DECLTYPES_BLOCK_ID:
3311 // We lazily load the decls block, but we want to set up the
3312 // DeclsCursor cursor to point into it. Clone our current bitcode
3313 // cursor to it, enter the block and read the abbrevs in that block.
3314 // With the main cursor, we just skip over it.
3315 F.DeclsCursor = Stream;
3316 if (llvm::Error Err = Stream.SkipBlock())
3317 return Err;
3318 if (llvm::Error Err = ReadBlockAbbrevs(
3320 return Err;
3321 break;
3322
3324 F.MacroCursor = Stream;
3325 if (!PP.getExternalSource())
3326 PP.setExternalSource(this);
3327
3328 if (llvm::Error Err = Stream.SkipBlock())
3329 return Err;
3330 if (llvm::Error Err =
3331 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3332 return Err;
3333 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3334 break;
3335
3337 F.PreprocessorDetailCursor = Stream;
3338
3339 if (llvm::Error Err = Stream.SkipBlock()) {
3340 return Err;
3341 }
3342 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3344 return Err;
3346 = F.PreprocessorDetailCursor.GetCurrentBitNo();
3347
3348 if (!PP.getPreprocessingRecord())
3349 PP.createPreprocessingRecord();
3350 if (!PP.getPreprocessingRecord()->getExternalSource())
3351 PP.getPreprocessingRecord()->SetExternalSource(*this);
3352 break;
3353
3355 if (llvm::Error Err = ReadSourceManagerBlock(F))
3356 return Err;
3357 break;
3358
3359 case SUBMODULE_BLOCK_ID:
3360 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3361 return Err;
3362 break;
3363
3364 case COMMENTS_BLOCK_ID: {
3365 BitstreamCursor C = Stream;
3366
3367 if (llvm::Error Err = Stream.SkipBlock())
3368 return Err;
3369 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3370 return Err;
3371 CommentsCursors.push_back(std::make_pair(C, &F));
3372 break;
3373 }
3374
3375 default:
3376 if (llvm::Error Err = Stream.SkipBlock())
3377 return Err;
3378 break;
3379 }
3380 continue;
3381
3382 case llvm::BitstreamEntry::Record:
3383 // The interesting case.
3384 break;
3385 }
3386
3387 // Read and process a record.
3388 Record.clear();
3389 StringRef Blob;
3390 Expected<unsigned> MaybeRecordType =
3391 Stream.readRecord(Entry.ID, Record, &Blob);
3392 if (!MaybeRecordType)
3393 return MaybeRecordType.takeError();
3394 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3395
3396 // If we're not loading an AST context, we don't care about most records.
3397 if (!ContextObj) {
3398 switch (RecordType) {
3399 case IDENTIFIER_TABLE:
3400 case IDENTIFIER_OFFSET:
3402 case STATISTICS:
3405 case PP_COUNTER_VALUE:
3407 case MODULE_OFFSET_MAP:
3411 case IMPORTED_MODULES:
3412 case MACRO_OFFSET:
3413 break;
3414 default:
3415 continue;
3416 }
3417 }
3418
3419 switch (RecordType) {
3420 default: // Default behavior: ignore.
3421 break;
3422
3423 case TYPE_OFFSET: {
3424 if (F.LocalNumTypes != 0)
3425 return llvm::createStringError(
3426 std::errc::illegal_byte_sequence,
3427 "duplicate TYPE_OFFSET record in AST file");
3428 F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
3429 F.LocalNumTypes = Record[0];
3430 F.BaseTypeIndex = getTotalNumTypes();
3431
3432 if (F.LocalNumTypes > 0)
3433 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3434
3435 break;
3436 }
3437
3438 case DECL_OFFSET: {
3439 if (F.LocalNumDecls != 0)
3440 return llvm::createStringError(
3441 std::errc::illegal_byte_sequence,
3442 "duplicate DECL_OFFSET record in AST file");
3443 F.DeclOffsets = (const DeclOffset *)Blob.data();
3444 F.LocalNumDecls = Record[0];
3445 F.BaseDeclIndex = getTotalNumDecls();
3446
3447 if (F.LocalNumDecls > 0)
3448 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3449
3450 break;
3451 }
3452
3453 case TU_UPDATE_LEXICAL: {
3454 DeclContext *TU = ContextObj->getTranslationUnitDecl();
3455 LexicalContents Contents(
3456 reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3457 static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3458 TULexicalDecls.push_back(std::make_pair(&F, Contents));
3460 break;
3461 }
3462
3463 case UPDATE_VISIBLE: {
3464 unsigned Idx = 0;
3465 GlobalDeclID ID = ReadDeclID(F, Record, Idx);
3466 auto *Data = (const unsigned char*)Blob.data();
3467 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3468 // If we've already loaded the decl, perform the updates when we finish
3469 // loading this block.
3470 if (Decl *D = GetExistingDecl(ID))
3471 PendingUpdateRecords.push_back(
3472 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3473 break;
3474 }
3475
3476 case IDENTIFIER_TABLE:
3478 reinterpret_cast<const unsigned char *>(Blob.data());
3479 if (Record[0]) {
3480 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3482 F.IdentifierTableData + sizeof(uint32_t),
3484 ASTIdentifierLookupTrait(*this, F));
3485
3486 PP.getIdentifierTable().setExternalIdentifierLookup(this);
3487 }
3488 break;
3489
3490 case IDENTIFIER_OFFSET: {
3491 if (F.LocalNumIdentifiers != 0)
3492 return llvm::createStringError(
3493 std::errc::illegal_byte_sequence,
3494 "duplicate IDENTIFIER_OFFSET record in AST file");
3495 F.IdentifierOffsets = (const uint32_t *)Blob.data();
3497 F.BaseIdentifierID = getTotalNumIdentifiers();
3498
3499 if (F.LocalNumIdentifiers > 0)
3500 IdentifiersLoaded.resize(IdentifiersLoaded.size()
3502 break;
3503 }
3504
3506 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3507 break;
3508
3510 // FIXME: Skip reading this record if our ASTConsumer doesn't care
3511 // about "interesting" decls (for instance, if we're building a module).
3512 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3513 EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));
3514 break;
3515
3517 // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3518 // them (ie: if we're not codegenerating this module).
3519 if (F.Kind == MK_MainFile ||
3520 getContext().getLangOpts().BuildingPCHWithObjectFile)
3521 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3522 EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));
3523 break;
3524
3525 case SPECIAL_TYPES:
3526 if (SpecialTypes.empty()) {
3527 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3528 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3529 break;
3530 }
3531
3532 if (SpecialTypes.size() != Record.size())
3533 return llvm::createStringError(std::errc::illegal_byte_sequence,
3534 "invalid special-types record");
3535
3536 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3537 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3538 if (!SpecialTypes[I])
3539 SpecialTypes[I] = ID;
3540 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3541 // merge step?
3542 }
3543 break;
3544
3545 case STATISTICS:
3546 TotalNumStatements += Record[0];
3547 TotalNumMacros += Record[1];
3548 TotalLexicalDeclContexts += Record[2];
3549 TotalVisibleDeclContexts += Record[3];
3550 break;
3551
3553 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3554 UnusedFileScopedDecls.push_back(ReadDeclID(F, Record, I));
3555 break;
3556
3557 case DELEGATING_CTORS:
3558 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3559 DelegatingCtorDecls.push_back(ReadDeclID(F, Record, I));
3560 break;
3561
3563 if (Record.size() % 3 != 0)
3564 return llvm::createStringError(std::errc::illegal_byte_sequence,
3565 "invalid weak identifiers record");
3566
3567 // FIXME: Ignore weak undeclared identifiers from non-original PCH
3568 // files. This isn't the way to do it :)
3569 WeakUndeclaredIdentifiers.clear();
3570
3571 // Translate the weak, undeclared identifiers into global IDs.
3572 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3573 WeakUndeclaredIdentifiers.push_back(
3574 getGlobalIdentifierID(F, Record[I++]));
3575 WeakUndeclaredIdentifiers.push_back(
3576 getGlobalIdentifierID(F, Record[I++]));
3577 WeakUndeclaredIdentifiers.push_back(
3578 ReadSourceLocation(F, Record, I).getRawEncoding());
3579 }
3580 break;
3581
3582 case SELECTOR_OFFSETS: {
3583 F.SelectorOffsets = (const uint32_t *)Blob.data();
3585 unsigned LocalBaseSelectorID = Record[1];
3586 F.BaseSelectorID = getTotalNumSelectors();
3587
3588 if (F.LocalNumSelectors > 0) {
3589 // Introduce the global -> local mapping for selectors within this
3590 // module.
3591 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3592
3593 // Introduce the local -> global mapping for selectors within this
3594 // module.
3596 std::make_pair(LocalBaseSelectorID,
3597 F.BaseSelectorID - LocalBaseSelectorID));
3598
3599 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3600 }
3601 break;
3602 }
3603
3604 case METHOD_POOL:
3605 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3606 if (Record[0])
3608 = ASTSelectorLookupTable::Create(
3611 ASTSelectorLookupTrait(*this, F));
3612 TotalNumMethodPoolEntries += Record[1];
3613 break;
3614
3616 if (!Record.empty()) {
3617 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3618 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3619 Record[Idx++]));
3620 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3621 getRawEncoding());
3622 }
3623 }
3624 break;
3625
3626 case PP_ASSUME_NONNULL_LOC: {
3627 unsigned Idx = 0;
3628 if (!Record.empty())
3629 PP.setPreambleRecordedPragmaAssumeNonNullLoc(
3630 ReadSourceLocation(F, Record, Idx));
3631 break;
3632 }
3633
3635 if (!Record.empty()) {
3637 unsigned Idx = 0;
3638 while (Idx < Record.size())
3639 SrcLocs.push_back(ReadSourceLocation(F, Record, Idx));
3640 PP.setDeserializedSafeBufferOptOutMap(SrcLocs);
3641 }
3642 break;
3643 }
3644
3646 if (!Record.empty()) {
3647 unsigned Idx = 0, End = Record.size() - 1;
3648 bool ReachedEOFWhileSkipping = Record[Idx++];
3649 std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3650 if (ReachedEOFWhileSkipping) {
3651 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3652 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3653 bool FoundNonSkipPortion = Record[Idx++];
3654 bool FoundElse = Record[Idx++];
3655 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3656 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3657 FoundElse, ElseLoc);
3658 }
3659 SmallVector<PPConditionalInfo, 4> ConditionalStack;
3660 while (Idx < End) {
3661 auto Loc = ReadSourceLocation(F, Record, Idx);
3662 bool WasSkipping = Record[Idx++];
3663 bool FoundNonSkip = Record[Idx++];
3664 bool FoundElse = Record[Idx++];
3665 ConditionalStack.push_back(
3666 {Loc, WasSkipping, FoundNonSkip, FoundElse});
3667 }
3668 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3669 }
3670 break;
3671
3672 case PP_COUNTER_VALUE:
3673 if (!Record.empty() && Listener)
3674 Listener->ReadCounter(F, Record[0]);
3675 break;
3676
3677 case FILE_SORTED_DECLS:
3678 F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();
3680 break;
3681
3683 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3685 SourceLocation::UIntTy SLocSpaceSize = Record[1];
3687 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3688 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3689 SLocSpaceSize);
3690 if (!F.SLocEntryBaseID) {
3691 if (!Diags.isDiagnosticInFlight()) {
3692 Diags.Report(SourceLocation(), diag::remark_sloc_usage);
3693 SourceMgr.noteSLocAddressSpaceUsage(Diags);
3694 }
3695 return llvm::createStringError(std::errc::invalid_argument,
3696 "ran out of source locations");
3697 }
3698 // Make our entry in the range map. BaseID is negative and growing, so
3699 // we invert it. Because we invert it, though, we need the other end of
3700 // the range.
3701 unsigned RangeStart =
3703 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3705
3706 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3707 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3708 GlobalSLocOffsetMap.insert(
3709 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3710 - SLocSpaceSize,&F));
3711
3712 TotalNumSLocEntries += F.LocalNumSLocEntries;
3713 break;
3714 }
3715
3716 case MODULE_OFFSET_MAP:
3717 F.ModuleOffsetMap = Blob;
3718 break;
3719
3721 ParseLineTable(F, Record);
3722 break;
3723
3724 case EXT_VECTOR_DECLS:
3725 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3726 ExtVectorDecls.push_back(ReadDeclID(F, Record, I));
3727 break;
3728
3729 case VTABLE_USES:
3730 if (Record.size() % 3 != 0)
3731 return llvm::createStringError(std::errc::illegal_byte_sequence,
3732 "Invalid VTABLE_USES record");
3733
3734 // Later tables overwrite earlier ones.
3735 // FIXME: Modules will have some trouble with this. This is clearly not
3736 // the right way to do this.
3737 VTableUses.clear();
3738
3739 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3740 VTableUses.push_back(
3741 {ReadDeclID(F, Record, Idx),
3742 ReadSourceLocation(F, Record, Idx).getRawEncoding(),
3743 (bool)Record[Idx++]});
3744 }
3745 break;
3746
3748
3749 if (Record.size() % 2 != 0)
3750 return llvm::createStringError(
3751 std::errc::illegal_byte_sequence,
3752 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3753
3754 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3755 PendingInstantiations.push_back(
3756 {ReadDeclID(F, Record, I),
3757 ReadSourceLocation(F, Record, I).getRawEncoding()});
3758 }
3759 break;
3760
3761 case SEMA_DECL_REFS:
3762 if (Record.size() != 3)
3763 return llvm::createStringError(std::errc::illegal_byte_sequence,
3764 "Invalid SEMA_DECL_REFS block");
3765 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3766 SemaDeclRefs.push_back(ReadDeclID(F, Record, I));
3767 break;
3768
3769 case PPD_ENTITIES_OFFSETS: {
3770 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3771 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3772 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3773
3774 unsigned LocalBasePreprocessedEntityID = Record[0];
3775
3776 unsigned StartingID;
3777 if (!PP.getPreprocessingRecord())
3778 PP.createPreprocessingRecord();
3779 if (!PP.getPreprocessingRecord()->getExternalSource())
3780 PP.getPreprocessingRecord()->SetExternalSource(*this);
3781 StartingID
3782 = PP.getPreprocessingRecord()
3783 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3784 F.BasePreprocessedEntityID = StartingID;
3785
3786 if (F.NumPreprocessedEntities > 0) {
3787 // Introduce the global -> local mapping for preprocessed entities in
3788 // this module.
3789 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3790
3791 // Introduce the local -> global mapping for preprocessed entities in
3792 // this module.
3794 std::make_pair(LocalBasePreprocessedEntityID,
3795 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3796 }
3797
3798 break;
3799 }
3800
3801 case PPD_SKIPPED_RANGES: {
3802 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3803 assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3804 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3805
3806 if (!PP.getPreprocessingRecord())
3807 PP.createPreprocessingRecord();
3808 if (!PP.getPreprocessingRecord()->getExternalSource())
3809 PP.getPreprocessingRecord()->SetExternalSource(*this);
3810 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3811 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3812
3814 GlobalSkippedRangeMap.insert(
3815 std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3816 break;
3817 }
3818
3820 if (Record.size() % 2 != 0)
3821 return llvm::createStringError(
3822 std::errc::illegal_byte_sequence,
3823 "invalid DECL_UPDATE_OFFSETS block in AST file");
3824 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
3825 GlobalDeclID ID = ReadDeclID(F, Record, I);
3826 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I++]));
3827
3828 // If we've already loaded the decl, perform the updates when we finish
3829 // loading this block.
3830 if (Decl *D = GetExistingDecl(ID))
3831 PendingUpdateRecords.push_back(
3832 PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3833 }
3834 break;
3835
3837 if (Record.size() % 3 != 0)
3838 return llvm::createStringError(
3839 std::errc::illegal_byte_sequence,
3840 "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
3841 "file");
3842 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {
3843 GlobalDeclID ID = ReadDeclID(F, Record, I);
3844
3845 uint64_t BaseOffset = F.DeclsBlockStartOffset;
3846 assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");
3847 uint64_t LocalLexicalOffset = Record[I++];
3848 uint64_t LexicalOffset =
3849 LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;
3850 uint64_t LocalVisibleOffset = Record[I++];
3851 uint64_t VisibleOffset =
3852 LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;
3853
3854 DelayedNamespaceOffsetMap[ID] = {LexicalOffset, VisibleOffset};
3855
3856 assert(!GetExistingDecl(ID) &&
3857 "We shouldn't load the namespace in the front of delayed "
3858 "namespace lexical and visible block");
3859 }
3860 break;
3861 }
3862
3864 if (F.LocalNumObjCCategoriesInMap != 0)
3865 return llvm::createStringError(
3866 std::errc::illegal_byte_sequence,
3867 "duplicate OBJC_CATEGORIES_MAP record in AST file");
3868
3870 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3871 break;
3872
3873 case OBJC_CATEGORIES:
3874 F.ObjCCategories.swap(Record);
3875 break;
3876
3878 // Later tables overwrite earlier ones.
3879 // FIXME: Modules will have trouble with this.
3880 CUDASpecialDeclRefs.clear();
3881 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3882 CUDASpecialDeclRefs.push_back(ReadDeclID(F, Record, I));
3883 break;
3884
3886 F.HeaderFileInfoTableData = Blob.data();
3888 if (Record[0]) {
3890 = HeaderFileInfoLookupTable::Create(
3891 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3892 (const unsigned char *)F.HeaderFileInfoTableData,
3893 HeaderFileInfoTrait(*this, F,
3894 &PP.getHeaderSearchInfo(),
3895 Blob.data() + Record[2]));
3896
3897 PP.getHeaderSearchInfo().SetExternalSource(this);
3898 if (!PP.getHeaderSearchInfo().getExternalLookup())
3899 PP.getHeaderSearchInfo().SetExternalLookup(this);
3900 }
3901 break;
3902
3903 case FP_PRAGMA_OPTIONS:
3904 // Later tables overwrite earlier ones.
3905 FPPragmaOptions.swap(Record);
3906 break;
3907
3908 case OPENCL_EXTENSIONS:
3909 for (unsigned I = 0, E = Record.size(); I != E; ) {
3910 auto Name = ReadString(Record, I);
3911 auto &OptInfo = OpenCLExtensions.OptMap[Name];
3912 OptInfo.Supported = Record[I++] != 0;
3913 OptInfo.Enabled = Record[I++] != 0;
3914 OptInfo.WithPragma = Record[I++] != 0;
3915 OptInfo.Avail = Record[I++];
3916 OptInfo.Core = Record[I++];
3917 OptInfo.Opt = Record[I++];
3918 }
3919 break;
3920
3922 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3923 TentativeDefinitions.push_back(ReadDeclID(F, Record, I));
3924 break;
3925
3926 case KNOWN_NAMESPACES:
3927 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
3928 KnownNamespaces.push_back(ReadDeclID(F, Record, I));
3929 break;
3930
3931 case UNDEFINED_BUT_USED:
3932 if (Record.size() % 2 != 0)
3933 return llvm::createStringError(std::errc::illegal_byte_sequence,
3934 "invalid undefined-but-used record");
3935 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3936 UndefinedButUsed.push_back(
3937 {ReadDeclID(F, Record, I),
3938 ReadSourceLocation(F, Record, I).getRawEncoding()});
3939 }
3940 break;
3941
3943 for (unsigned I = 0, N = Record.size(); I != N;) {
3944 DelayedDeleteExprs.push_back(ReadDeclID(F, Record, I).getRawValue());
3945 const uint64_t Count = Record[I++];
3946 DelayedDeleteExprs.push_back(Count);
3947 for (uint64_t C = 0; C < Count; ++C) {
3948 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3949 bool IsArrayForm = Record[I++] == 1;
3950 DelayedDeleteExprs.push_back(IsArrayForm);
3951 }
3952 }
3953 break;
3954
3955 case VTABLES_TO_EMIT:
3956 if (F.Kind == MK_MainFile ||
3957 getContext().getLangOpts().BuildingPCHWithObjectFile)
3958 for (unsigned I = 0, N = Record.size(); I != N;)
3959 VTablesToEmit.push_back(ReadDeclID(F, Record, I));
3960 break;
3961
3962 case IMPORTED_MODULES:
3963 if (!F.isModule()) {
3964 // If we aren't loading a module (which has its own exports), make
3965 // all of the imported modules visible.
3966 // FIXME: Deal with macros-only imports.
3967 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3968 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3969 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3970 if (GlobalID) {
3971 PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3972 if (DeserializationListener)
3973 DeserializationListener->ModuleImportRead(GlobalID, Loc);
3974 }
3975 }
3976 }
3977 break;
3978
3979 case MACRO_OFFSET: {
3980 if (F.LocalNumMacros != 0)
3981 return llvm::createStringError(
3982 std::errc::illegal_byte_sequence,
3983 "duplicate MACRO_OFFSET record in AST file");
3984 F.MacroOffsets = (const uint32_t *)Blob.data();
3985 F.LocalNumMacros = Record[0];
3986 unsigned LocalBaseMacroID = Record[1];
3988 F.BaseMacroID = getTotalNumMacros();
3989
3990 if (F.LocalNumMacros > 0) {
3991 // Introduce the global -> local mapping for macros within this module.
3992 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3993
3994 // Introduce the local -> global mapping for macros within this module.
3996 std::make_pair(LocalBaseMacroID,
3997 F.BaseMacroID - LocalBaseMacroID));
3998
3999 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
4000 }
4001 break;
4002 }
4003
4005 LateParsedTemplates.emplace_back(
4006 std::piecewise_construct, std::forward_as_tuple(&F),
4007 std::forward_as_tuple(Record.begin(), Record.end()));
4008 break;
4009
4011 if (Record.size() != 1)
4012 return llvm::createStringError(std::errc::illegal_byte_sequence,
4013 "invalid pragma optimize record");
4014 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
4015 break;
4016
4018 if (Record.size() != 1)
4019 return llvm::createStringError(std::errc::illegal_byte_sequence,
4020 "invalid pragma ms_struct record");
4021 PragmaMSStructState = Record[0];
4022 break;
4023
4025 if (Record.size() != 2)
4026 return llvm::createStringError(
4027 std::errc::illegal_byte_sequence,
4028 "invalid pragma pointers to members record");
4029 PragmaMSPointersToMembersState = Record[0];
4030 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
4031 break;
4032
4034 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4035 UnusedLocalTypedefNameCandidates.push_back(ReadDeclID(F, Record, I));
4036 break;
4037
4039 if (Record.size() != 1)
4040 return llvm::createStringError(std::errc::illegal_byte_sequence,
4041 "invalid cuda pragma options record");
4042 ForceHostDeviceDepth = Record[0];
4043 break;
4044
4046 if (Record.size() < 3)
4047 return llvm::createStringError(std::errc::illegal_byte_sequence,
4048 "invalid pragma pack record");
4049 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
4050 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
4051 unsigned NumStackEntries = Record[2];
4052 unsigned Idx = 3;
4053 // Reset the stack when importing a new module.
4054 PragmaAlignPackStack.clear();
4055 for (unsigned I = 0; I < NumStackEntries; ++I) {
4056 PragmaAlignPackStackEntry Entry;
4057 Entry.Value = ReadAlignPackInfo(Record[Idx++]);
4058 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4059 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4060 PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
4061 Entry.SlotLabel = PragmaAlignPackStrings.back();
4062 PragmaAlignPackStack.push_back(Entry);
4063 }
4064 break;
4065 }
4066
4068 if (Record.size() < 3)
4069 return llvm::createStringError(std::errc::illegal_byte_sequence,
4070 "invalid pragma float control record");
4071 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
4072 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
4073 unsigned NumStackEntries = Record[2];
4074 unsigned Idx = 3;
4075 // Reset the stack when importing a new module.
4076 FpPragmaStack.clear();
4077 for (unsigned I = 0; I < NumStackEntries; ++I) {
4078 FpPragmaStackEntry Entry;
4079 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
4080 Entry.Location = ReadSourceLocation(F, Record[Idx++]);
4081 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
4082 FpPragmaStrings.push_back(ReadString(Record, Idx));
4083 Entry.SlotLabel = FpPragmaStrings.back();
4084 FpPragmaStack.push_back(Entry);
4085 }
4086 break;
4087 }
4088
4090 for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
4091 DeclsToCheckForDeferredDiags.insert(ReadDeclID(F, Record, I));
4092 break;
4093 }
4094 }
4095}
4096
4097void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
4098 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
4099
4100 // Additional remapping information.
4101 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
4102 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
4103 F.ModuleOffsetMap = StringRef();
4104
4106 RemapBuilder MacroRemap(F.MacroRemap);
4107 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
4108 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
4109 RemapBuilder SelectorRemap(F.SelectorRemap);
4110
4111 auto &ImportedModuleVector = F.TransitiveImports;
4112 assert(ImportedModuleVector.empty());
4113
4114 while (Data < DataEnd) {
4115 // FIXME: Looking up dependency modules by filename is horrible. Let's
4116 // start fixing this with prebuilt, explicit and implicit modules and see
4117 // how it goes...
4118 using namespace llvm::support;
4119 ModuleKind Kind = static_cast<ModuleKind>(
4120 endian::readNext<uint8_t, llvm::endianness::little>(Data));
4121 uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(Data);
4122 StringRef Name = StringRef((const char*)Data, Len);
4123 Data += Len;
4126 ? ModuleMgr.lookupByModuleName(Name)
4127 : ModuleMgr.lookupByFileName(Name));
4128 if (!OM) {
4129 std::string Msg = "refers to unknown module, cannot find ";
4130 Msg.append(std::string(Name));
4131 Error(Msg);
4132 return;
4133 }
4134
4135 ImportedModuleVector.push_back(OM);
4136
4137 uint32_t MacroIDOffset =
4138 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4139 uint32_t PreprocessedEntityIDOffset =
4140 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4141 uint32_t SubmoduleIDOffset =
4142 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4143 uint32_t SelectorIDOffset =
4144 endian::readNext<uint32_t, llvm::endianness::little>(Data);
4145
4146 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
4147 RemapBuilder &Remap) {
4148 constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
4149 if (Offset != None)
4150 Remap.insert(std::make_pair(Offset,
4151 static_cast<int>(BaseOffset - Offset)));
4152 };
4153
4154 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
4155 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
4156 PreprocessedEntityRemap);
4157 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
4158 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4159 }
4160}
4161
4163ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
4164 const ModuleFile *ImportedBy,
4165 unsigned ClientLoadCapabilities) {
4166 unsigned Idx = 0;
4167 F.ModuleMapPath = ReadPath(F, Record, Idx);
4168
4169 // Try to resolve ModuleName in the current header search context and
4170 // verify that it is found in the same module map file as we saved. If the
4171 // top-level AST file is a main file, skip this check because there is no
4172 // usable header search context.
4173 assert(!F.ModuleName.empty() &&
4174 "MODULE_NAME should come before MODULE_MAP_FILE");
4175 if (PP.getPreprocessorOpts().ModulesCheckRelocated &&
4176 F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
4177 // An implicitly-loaded module file should have its module listed in some
4178 // module map file that we've already loaded.
4179 Module *M =
4180 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
4181 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
4182 OptionalFileEntryRef ModMap =
4183 M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;
4184 // Don't emit module relocation error if we have -fno-validate-pch
4185 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
4187 !ModMap) {
4188 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
4189 if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {
4190 // This module was defined by an imported (explicit) module.
4191 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
4192 << ASTFE->getName();
4193 } else {
4194 // This module was built with a different module map.
4195 Diag(diag::err_imported_module_not_found)
4196 << F.ModuleName << F.FileName
4197 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
4198 << !ImportedBy;
4199 // In case it was imported by a PCH, there's a chance the user is
4200 // just missing to include the search path to the directory containing
4201 // the modulemap.
4202 if (ImportedBy && ImportedBy->Kind == MK_PCH)
4203 Diag(diag::note_imported_by_pch_module_not_found)
4204 << llvm::sys::path::parent_path(F.ModuleMapPath);
4205 }
4206 }
4207 return OutOfDate;
4208 }
4209
4210 assert(M && M->Name == F.ModuleName && "found module with different name");
4211
4212 // Check the primary module map file.
4213 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
4214 if (!StoredModMap || *StoredModMap != ModMap) {
4215 assert(ModMap && "found module is missing module map file");
4216 assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
4217 "top-level import should be verified");
4218 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
4219 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4220 Diag(diag::err_imported_module_modmap_changed)
4221 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
4222 << ModMap->getName() << F.ModuleMapPath << NotImported;
4223 return OutOfDate;
4224 }
4225
4226 ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;
4227 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
4228 // FIXME: we should use input files rather than storing names.
4229 std::string Filename = ReadPath(F, Record, Idx);
4230 auto SF = FileMgr.getOptionalFileRef(Filename, false, false);
4231 if (!SF) {
4232 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4233 Error("could not find file '" + Filename +"' referenced by AST file");
4234 return OutOfDate;
4235 }
4236 AdditionalStoredMaps.insert(*SF);
4237 }
4238
4239 // Check any additional module map files (e.g. module.private.modulemap)
4240 // that are not in the pcm.
4241 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4242 for (FileEntryRef ModMap : *AdditionalModuleMaps) {
4243 // Remove files that match
4244 // Note: SmallPtrSet::erase is really remove
4245 if (!AdditionalStoredMaps.erase(ModMap)) {
4246 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4247 Diag(diag::err_module_different_modmap)
4248 << F.ModuleName << /*new*/0 << ModMap.getName();
4249 return OutOfDate;
4250 }
4251 }
4252 }
4253
4254 // Check any additional module map files that are in the pcm, but not
4255 // found in header search. Cases that match are already removed.
4256 for (FileEntryRef ModMap : AdditionalStoredMaps) {
4257 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4258 Diag(diag::err_module_different_modmap)
4259 << F.ModuleName << /*not new*/1 << ModMap.getName();
4260 return OutOfDate;
4261 }
4262 }
4263
4264 if (Listener)
4265 Listener->ReadModuleMapFile(F.ModuleMapPath);
4266 return Success;
4267}
4268
4269/// Move the given method to the back of the global list of methods.
4271 // Find the entry for this selector in the method pool.
4273 S.ObjC().MethodPool.find(Method->getSelector());
4274 if (Known == S.ObjC().MethodPool.end())
4275 return;
4276
4277 // Retrieve the appropriate method list.
4278 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4279 : Known->second.second;
4280 bool Found = false;
4281 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4282 if (!Found) {
4283 if (List->getMethod() == Method) {
4284 Found = true;
4285 } else {
4286 // Keep searching.
4287 continue;
4288 }
4289 }
4290
4291 if (List->getNext())
4292 List->setMethod(List->getNext()->getMethod());
4293 else
4294 List->setMethod(Method);
4295 }
4296}
4297
4299 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4300 for (Decl *D : Names) {
4301 bool wasHidden = !D->isUnconditionallyVisible();
4303
4304 if (wasHidden && SemaObj) {
4305 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4306 moveMethodToBackOfGlobalList(*SemaObj, Method);
4307 }
4308 }
4309 }
4310}
4311
4313 Module::NameVisibilityKind NameVisibility,
4314 SourceLocation ImportLoc) {
4317 Stack.push_back(Mod);
4318 while (!Stack.empty()) {
4319 Mod = Stack.pop_back_val();
4320
4321 if (NameVisibility <= Mod->NameVisibility) {
4322 // This module already has this level of visibility (or greater), so
4323 // there is nothing more to do.
4324 continue;
4325 }
4326
4327 if (Mod->isUnimportable()) {
4328 // Modules that aren't importable cannot be made visible.
4329 continue;
4330 }
4331
4332 // Update the module's name visibility.
4333 Mod->NameVisibility = NameVisibility;
4334
4335 // If we've already deserialized any names from this module,
4336 // mark them as visible.
4337 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4338 if (Hidden != HiddenNamesMap.end()) {
4339 auto HiddenNames = std::move(*Hidden);
4340 HiddenNamesMap.erase(Hidden);
4341 makeNamesVisible(HiddenNames.second, HiddenNames.first);
4342 assert(!HiddenNamesMap.contains(Mod) &&
4343 "making names visible added hidden names");
4344 }
4345
4346 // Push any exported modules onto the stack to be marked as visible.
4348 Mod->getExportedModules(Exports);
4350 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4351 Module *Exported = *I;
4352 if (Visited.insert(Exported).second)
4353 Stack.push_back(Exported);
4354 }
4355 }
4356}
4357
4358/// We've merged the definition \p MergedDef into the existing definition
4359/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4360/// visible.
4362 NamedDecl *MergedDef) {
4363 if (!Def->isUnconditionallyVisible()) {
4364 // If MergedDef is visible or becomes visible, make the definition visible.
4365 if (MergedDef->isUnconditionallyVisible())
4367 else {
4368 getContext().mergeDefinitionIntoModule(
4369 Def, MergedDef->getImportedOwningModule(),
4370 /*NotifyListeners*/ false);
4371 PendingMergedDefinitionsToDeduplicate.insert(Def);
4372 }
4373 }
4374}
4375
4377 if (GlobalIndex)
4378 return false;
4379
4380 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4381 !PP.getLangOpts().Modules)
4382 return true;
4383
4384 // Try to load the global index.
4385 TriedLoadingGlobalIndex = true;
4386 StringRef ModuleCachePath
4387 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4388 std::pair<GlobalModuleIndex *, llvm::Error> Result =
4389 GlobalModuleIndex::readIndex(ModuleCachePath);
4390 if (llvm::Error Err = std::move(Result.second)) {
4391 assert(!Result.first);
4392 consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4393 return true;
4394 }
4395
4396 GlobalIndex.reset(Result.first);
4397 ModuleMgr.setGlobalIndex(GlobalIndex.get());
4398 return false;
4399}
4400
4402 return PP.getLangOpts().Modules && UseGlobalIndex &&
4403 !hasGlobalIndex() && TriedLoadingGlobalIndex;
4404}
4405
4407 // Overwrite the timestamp file contents so that file's mtime changes.
4408 std::string TimestampFilename = MF.getTimestampFilename();
4409 std::error_code EC;
4410 llvm::raw_fd_ostream OS(TimestampFilename, EC,
4411 llvm::sys::fs::OF_TextWithCRLF);
4412 if (EC)
4413 return;
4414 OS << "Timestamp file\n";
4415 OS.close();
4416 OS.clear_error(); // Avoid triggering a fatal error.
4417}
4418
4419/// Given a cursor at the start of an AST file, scan ahead and drop the
4420/// cursor into the start of the given block ID, returning false on success and
4421/// true on failure.
4422static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4423 while (true) {
4424 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4425 if (!MaybeEntry) {
4426 // FIXME this drops errors on the floor.
4427 consumeError(MaybeEntry.takeError());
4428 return true;
4429 }
4430 llvm::BitstreamEntry Entry = MaybeEntry.get();
4431
4432 switch (Entry.Kind) {
4433 case llvm::BitstreamEntry::Error:
4434 case llvm::BitstreamEntry::EndBlock:
4435 return true;
4436
4437 case llvm::BitstreamEntry::Record:
4438 // Ignore top-level records.
4439 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4440 break;
4441 else {
4442 // FIXME this drops errors on the floor.
4443 consumeError(Skipped.takeError());
4444 return true;
4445 }
4446
4447 case llvm::BitstreamEntry::SubBlock:
4448 if (Entry.ID == BlockID) {
4449 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4450 // FIXME this drops the error on the floor.
4451 consumeError(std::move(Err));
4452 return true;
4453 }
4454 // Found it!
4455 return false;
4456 }
4457
4458 if (llvm::Error Err = Cursor.SkipBlock()) {
4459 // FIXME this drops the error on the floor.
4460 consumeError(std::move(Err));
4461 return true;
4462 }
4463 }
4464 }
4465}
4466
4468 SourceLocation ImportLoc,
4469 unsigned ClientLoadCapabilities,
4470 ModuleFile **NewLoadedModuleFile) {
4471 llvm::TimeTraceScope scope("ReadAST", FileName);
4472
4473 llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4475 CurrentDeserializingModuleKind, Type);
4476
4477 // Defer any pending actions until we get to the end of reading the AST file.
4478 Deserializing AnASTFile(this);
4479
4480 // Bump the generation number.
4481 unsigned PreviousGeneration = 0;
4482 if (ContextObj)
4483 PreviousGeneration = incrementGeneration(*ContextObj);
4484
4485 unsigned NumModules = ModuleMgr.size();
4487 if (ASTReadResult ReadResult =
4488 ReadASTCore(FileName, Type, ImportLoc,
4489 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4490 ClientLoadCapabilities)) {
4491 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);
4492
4493 // If we find that any modules are unusable, the global index is going
4494 // to be out-of-date. Just remove it.
4495 GlobalIndex.reset();
4496 ModuleMgr.setGlobalIndex(nullptr);
4497 return ReadResult;
4498 }
4499
4500 if (NewLoadedModuleFile && !Loaded.empty())
4501 *NewLoadedModuleFile = Loaded.back().Mod;
4502
4503 // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4504 // remove modules from this point. Various fields are updated during reading
4505 // the AST block and removing the modules would result in dangling pointers.
4506 // They are generally only incidentally dereferenced, ie. a binary search
4507 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4508 // be dereferenced but it wouldn't actually be used.
4509
4510 // Load the AST blocks of all of the modules that we loaded. We can still
4511 // hit errors parsing the ASTs at this point.
4512 for (ImportedModule &M : Loaded) {
4513 ModuleFile &F = *M.Mod;
4514 llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);
4515
4516 // Read the AST block.
4517 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4518 Error(std::move(Err));
4519 return Failure;
4520 }
4521
4522 // The AST block should always have a definition for the main module.
4523 if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4524 Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4525 return Failure;
4526 }
4527
4528 // Read the extension blocks.
4530 if (llvm::Error Err = ReadExtensionBlock(F)) {
4531 Error(std::move(Err));
4532 return Failure;
4533 }
4534 }
4535
4536 // Once read, set the ModuleFile bit base offset and update the size in
4537 // bits of all files we've seen.
4538 F.GlobalBitOffset = TotalModulesSizeInBits;
4539 TotalModulesSizeInBits += F.SizeInBits;
4540 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4541 }
4542
4543 // Preload source locations and interesting indentifiers.
4544 for (ImportedModule &M : Loaded) {
4545 ModuleFile &F = *M.Mod;
4546
4547 // Map the original source file ID into the ID space of the current
4548 // compilation.
4550 F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
4551
4552 for (auto Offset : F.PreloadIdentifierOffsets) {
4553 const unsigned char *Data = F.IdentifierTableData + Offset;
4554
4555 ASTIdentifierLookupTrait Trait(*this, F);
4556 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4557 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4558
4559 IdentifierInfo *II;
4560 if (!PP.getLangOpts().CPlusPlus) {
4561 // Identifiers present in both the module file and the importing
4562 // instance are marked out-of-date so that they can be deserialized
4563 // on next use via ASTReader::updateOutOfDateIdentifier().
4564 // Identifiers present in the module file but not in the importing
4565 // instance are ignored for now, preventing growth of the identifier
4566 // table. They will be deserialized on first use via ASTReader::get().
4567 auto It = PP.getIdentifierTable().find(Key);
4568 if (It == PP.getIdentifierTable().end())
4569 continue;
4570 II = It->second;
4571 } else {
4572 // With C++ modules, not many identifiers are considered interesting.
4573 // All identifiers in the module file can be placed into the identifier
4574 // table of the importing instance and marked as out-of-date. This makes
4575 // ASTReader::get() a no-op, and deserialization will take place on
4576 // first/next use via ASTReader::updateOutOfDateIdentifier().
4577 II = &PP.getIdentifierTable().getOwn(Key);
4578 }
4579
4580 II->setOutOfDate(true);
4581
4582 // Mark this identifier as being from an AST file so that we can track
4583 // whether we need to serialize it.
4584 markIdentifierFromAST(*this, *II, /*IsModule=*/true);
4585
4586 // Associate the ID with the identifier so that the writer can reuse it.
4587 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4588 SetIdentifierInfo(ID, II);
4589 }
4590 }
4591
4592 // Builtins and library builtins have already been initialized. Mark all
4593 // identifiers as out-of-date, so that they are deserialized on first use.
4594 if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)
4595 for (auto &Id : PP.getIdentifierTable())
4596 Id.second->setOutOfDate(true);
4597
4598 // Mark selectors as out of date.
4599 for (const auto &Sel : SelectorGeneration)
4600 SelectorOutOfDate[Sel.first] = true;
4601
4602 // Setup the import locations and notify the module manager that we've
4603 // committed to these module files.
4604 for (ImportedModule &M : Loaded) {
4605 ModuleFile &F = *M.Mod;
4606
4607 ModuleMgr.moduleFileAccepted(&F);
4608
4609 // Set the import location.
4610 F.DirectImportLoc = ImportLoc;
4611 // FIXME: We assume that locations from PCH / preamble do not need
4612 // any translation.
4613 if (!M.ImportedBy)
4614 F.ImportLoc = M.ImportLoc;
4615 else
4616 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4617 }
4618
4619 // Resolve any unresolved module exports.
4620 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4621 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4622 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4623 Module *ResolvedMod = getSubmodule(GlobalID);
4624
4625 switch (Unresolved.Kind) {
4626 case UnresolvedModuleRef::Conflict:
4627 if (ResolvedMod) {
4628 Module::Conflict Conflict;
4629 Conflict.Other = ResolvedMod;
4630 Conflict.Message = Unresolved.String.str();
4631 Unresolved.Mod->Conflicts.push_back(Conflict);
4632 }
4633 continue;
4634
4635 case UnresolvedModuleRef::Import:
4636 if (ResolvedMod)
4637 Unresolved.Mod->Imports.insert(ResolvedMod);
4638 continue;
4639
4640 case UnresolvedModuleRef::Affecting:
4641 if (ResolvedMod)
4642 Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);
4643 continue;
4644
4645 case UnresolvedModuleRef::Export:
4646 if (ResolvedMod || Unresolved.IsWildcard)
4647 Unresolved.Mod->Exports.push_back(
4648 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4649 continue;
4650 }
4651 }
4652 UnresolvedModuleRefs.clear();
4653
4654 // FIXME: How do we load the 'use'd modules? They may not be submodules.
4655 // Might be unnecessary as use declarations are only used to build the
4656 // module itself.
4657
4658 if (ContextObj)
4659 InitializeContext();
4660
4661 if (SemaObj)
4662 UpdateSema();
4663
4664 if (DeserializationListener)
4665 DeserializationListener->ReaderInitialized(this);
4666
4667 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4668 if (PrimaryModule.OriginalSourceFileID.isValid()) {
4669 // If this AST file is a precompiled preamble, then set the
4670 // preamble file ID of the source manager to the file source file
4671 // from which the preamble was built.
4672 if (Type == MK_Preamble) {
4673 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4674 } else if (Type == MK_MainFile) {
4675 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4676 }
4677 }
4678
4679 // For any Objective-C class definitions we have already loaded, make sure
4680 // that we load any additional categories.
4681 if (ContextObj) {
4682 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4683 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4684 ObjCClassesLoaded[I], PreviousGeneration);
4685 }
4686 }
4687
4688 HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
4690 // Now we are certain that the module and all modules it depends on are
4691 // up-to-date. For implicitly-built module files, ensure the corresponding
4692 // timestamp files are up-to-date in this build session.
4693 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4694 ImportedModule &M = Loaded[I];
4695 if (M.Mod->Kind == MK_ImplicitModule &&
4696 M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4697 updateModuleTimestamp(*M.Mod);
4698 }
4699 }
4700
4701 return Success;
4702}
4703
4704static ASTFileSignature readASTFileSignature(StringRef PCH);
4705
4706/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4707static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4708 // FIXME checking magic headers is done in other places such as
4709 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4710 // always done the same. Unify it all with a helper.
4711 if (!Stream.canSkipToPos(4))
4712 return llvm::createStringError(std::errc::illegal_byte_sequence,
4713 "file too small to contain AST file magic");
4714 for (unsigned C : {'C', 'P', 'C', 'H'})
4715 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4716 if (Res.get() != C)
4717 return llvm::createStringError(
4718 std::errc::illegal_byte_sequence,
4719 "file doesn't start with AST file magic");
4720 } else
4721 return Res.takeError();
4722 return llvm::Error::success();
4723}
4724
4726 switch (Kind) {
4727 case MK_PCH:
4728 return 0; // PCH
4729 case MK_ImplicitModule:
4730 case MK_ExplicitModule:
4731 case MK_PrebuiltModule:
4732 return 1; // module
4733 case MK_MainFile:
4734 case MK_Preamble:
4735 return 2; // main source file
4736 }
4737 llvm_unreachable("unknown module kind");
4738}
4739
4741ASTReader::ReadASTCore(StringRef FileName,
4743 SourceLocation ImportLoc,
4744 ModuleFile *ImportedBy,
4746 off_t ExpectedSize, time_t ExpectedModTime,
4747 ASTFileSignature ExpectedSignature,
4748 unsigned ClientLoadCapabilities) {
4749 ModuleFile *M;
4750 std::string ErrorStr;
4752 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4753 getGeneration(), ExpectedSize, ExpectedModTime,
4754 ExpectedSignature, readASTFileSignature,
4755 M, ErrorStr);
4756
4757 switch (AddResult) {
4759 Diag(diag::remark_module_import)
4760 << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4761 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4762 return Success;
4763
4765 // Load module file below.
4766 break;
4767
4769 // The module file was missing; if the client can handle that, return
4770 // it.
4771 if (ClientLoadCapabilities & ARR_Missing)
4772 return Missing;
4773
4774 // Otherwise, return an error.
4775 Diag(diag::err_ast_file_not_found)
4776 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4777 << ErrorStr;
4778 return Failure;
4779
4781 // We couldn't load the module file because it is out-of-date. If the
4782 // client can handle out-of-date, return it.
4783 if (ClientLoadCapabilities & ARR_OutOfDate)
4784 return OutOfDate;
4785
4786 // Otherwise, return an error.
4787 Diag(diag::err_ast_file_out_of_date)
4788 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4789 << ErrorStr;
4790 return Failure;
4791 }
4792
4793 assert(M && "Missing module file");
4794
4795 bool ShouldFinalizePCM = false;
4796 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4797 auto &MC = getModuleManager().getModuleCache();
4798 if (ShouldFinalizePCM)
4799 MC.finalizePCM(FileName);
4800 else
4801 MC.tryToDropPCM(FileName);
4802 });
4803 ModuleFile &F = *M;
4804 BitstreamCursor &Stream = F.Stream;
4805 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4806 F.SizeInBits = F.Buffer->getBufferSize() * 8;
4807
4808 // Sniff for the signature.
4809 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4810 Diag(diag::err_ast_file_invalid)
4811 << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4812 return Failure;
4813 }
4814
4815 // This is used for compatibility with older PCH formats.
4816 bool HaveReadControlBlock = false;
4817 while (true) {
4818 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4819 if (!MaybeEntry) {
4820 Error(MaybeEntry.takeError());
4821 return Failure;
4822 }
4823 llvm::BitstreamEntry Entry = MaybeEntry.get();
4824
4825 switch (Entry.Kind) {
4826 case llvm::BitstreamEntry::Error:
4827 case llvm::BitstreamEntry::Record:
4828 case llvm::BitstreamEntry::EndBlock:
4829 Error("invalid record at top-level of AST file");
4830 return Failure;
4831
4832 case llvm::BitstreamEntry::SubBlock:
4833 break;
4834 }
4835
4836 switch (Entry.ID) {
4837 case CONTROL_BLOCK_ID:
4838 HaveReadControlBlock = true;
4839 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4840 case Success:
4841 // Check that we didn't try to load a non-module AST file as a module.
4842 //
4843 // FIXME: Should we also perform the converse check? Loading a module as
4844 // a PCH file sort of works, but it's a bit wonky.
4846 Type == MK_PrebuiltModule) &&
4847 F.ModuleName.empty()) {
4848 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4849 if (Result != OutOfDate ||
4850 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4851 Diag(diag::err_module_file_not_module) << FileName;
4852 return Result;
4853 }
4854 break;
4855
4856 case Failure: return Failure;
4857 case Missing: return Missing;
4858 case OutOfDate: return OutOfDate;
4859 case VersionMismatch: return VersionMismatch;
4860 case ConfigurationMismatch: return ConfigurationMismatch;
4861 case HadErrors: return HadErrors;
4862 }
4863 break;
4864
4865 case AST_BLOCK_ID:
4866 if (!HaveReadControlBlock) {
4867 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4868 Diag(diag::err_ast_file_version_too_old)
4870 return VersionMismatch;
4871 }
4872
4873 // Record that we've loaded this module.
4874 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4875 ShouldFinalizePCM = true;
4876 return Success;
4877
4878 default:
4879 if (llvm::Error Err = Stream.SkipBlock()) {
4880 Error(std::move(Err));
4881 return Failure;
4882 }
4883 break;
4884 }
4885 }
4886
4887 llvm_unreachable("unexpected break; expected return");
4888}
4889
4891ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4892 unsigned ClientLoadCapabilities) {
4893 const HeaderSearchOptions &HSOpts =
4894 PP.getHeaderSearchInfo().getHeaderSearchOpts();
4895 bool AllowCompatibleConfigurationMismatch =
4897 bool DisableValidation = shouldDisableValidationForFile(F);
4898
4899 ASTReadResult Result = readUnhashedControlBlockImpl(
4900 &F, F.Data, F.FileName, ClientLoadCapabilities,
4901 AllowCompatibleConfigurationMismatch, Listener.get(),
4902 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4903
4904 // If F was directly imported by another module, it's implicitly validated by
4905 // the importing module.
4906 if (DisableValidation || WasImportedBy ||
4907 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4908 return Success;
4909
4910 if (Result == Failure) {
4911 Error("malformed block record in AST file");
4912 return Failure;
4913 }
4914
4915 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4916 // If this module has already been finalized in the ModuleCache, we're stuck
4917 // with it; we can only load a single version of each module.
4918 //
4919 // This can happen when a module is imported in two contexts: in one, as a
4920 // user module; in another, as a system module (due to an import from
4921 // another module marked with the [system] flag). It usually indicates a
4922 // bug in the module map: this module should also be marked with [system].
4923 //
4924 // If -Wno-system-headers (the default), and the first import is as a
4925 // system module, then validation will fail during the as-user import,
4926 // since -Werror flags won't have been validated. However, it's reasonable
4927 // to treat this consistently as a system module.
4928 //
4929 // If -Wsystem-headers, the PCM on disk was built with
4930 // -Wno-system-headers, and the first import is as a user module, then
4931 // validation will fail during the as-system import since the PCM on disk
4932 // doesn't guarantee that -Werror was respected. However, the -Werror
4933 // flags were checked during the initial as-user import.
4934 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4935 Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4936 return Success;
4937 }
4938 }
4939
4940 return Result;
4941}
4942
4943ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4944 ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,
4945 unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,
4946 ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {
4947 // Initialize a stream.
4948 BitstreamCursor Stream(StreamData);
4949
4950 // Sniff for the signature.
4951 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4952 // FIXME this drops the error on the floor.
4953 consumeError(std::move(Err));
4954 return Failure;
4955 }
4956
4957 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4959 return Failure;
4960
4961 // Read all of the records in the options block.
4962 RecordData Record;
4963 ASTReadResult Result = Success;
4964 while (true) {
4965 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4966 if (!MaybeEntry) {
4967 // FIXME this drops the error on the floor.
4968 consumeError(MaybeEntry.takeError());
4969 return Failure;
4970 }
4971 llvm::BitstreamEntry Entry = MaybeEntry.get();
4972
4973 switch (Entry.Kind) {
4974 case llvm::BitstreamEntry::Error:
4975 case llvm::BitstreamEntry::SubBlock:
4976 return Failure;
4977
4978 case llvm::BitstreamEntry::EndBlock:
4979 return Result;
4980
4981 case llvm::BitstreamEntry::Record:
4982 // The interesting case.
4983 break;
4984 }
4985
4986 // Read and process a record.
4987 Record.clear();
4988 StringRef Blob;
4989 Expected<unsigned> MaybeRecordType =
4990 Stream.readRecord(Entry.ID, Record, &Blob);
4991 if (!MaybeRecordType) {
4992 // FIXME this drops the error.
4993 return Failure;
4994 }
4995 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4996 case SIGNATURE:
4997 if (F) {
4998 F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5000 "Dummy AST file signature not backpatched in ASTWriter.");
5001 }
5002 break;
5003 case AST_BLOCK_HASH:
5004 if (F) {
5005 F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());
5007 "Dummy AST block hash not backpatched in ASTWriter.");
5008 }
5009 break;
5010 case DIAGNOSTIC_OPTIONS: {
5011 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
5012 if (Listener && ValidateDiagnosticOptions &&
5013 !AllowCompatibleConfigurationMismatch &&
5014 ParseDiagnosticOptions(Record, Filename, Complain, *Listener))
5015 Result = OutOfDate; // Don't return early. Read the signature.
5016 break;
5017 }
5018 case HEADER_SEARCH_PATHS: {
5019 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
5020 if (Listener && !AllowCompatibleConfigurationMismatch &&
5021 ParseHeaderSearchPaths(Record, Complain, *Listener))
5022 Result = ConfigurationMismatch;
5023 break;
5024 }
5026 if (!F)
5027 break;
5028 if (F->PragmaDiagMappings.empty())
5029 F->PragmaDiagMappings.swap(Record);
5030 else
5031 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
5032 Record.begin(), Record.end());
5033 break;
5035 if (F)
5036 F->SearchPathUsage = ReadBitVector(Record, Blob);
5037 break;
5038 case VFS_USAGE:
5039 if (F)
5040 F->VFSUsage = ReadBitVector(Record, Blob);
5041 break;
5042 }
5043 }
5044}
5045
5046/// Parse a record and blob containing module file extension metadata.
5049 StringRef Blob,
5050 ModuleFileExtensionMetadata &Metadata) {
5051 if (Record.size() < 4) return true;
5052
5053 Metadata.MajorVersion = Record[0];
5054 Metadata.MinorVersion = Record[1];
5055
5056 unsigned BlockNameLen = Record[2];
5057 unsigned UserInfoLen = Record[3];
5058
5059 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
5060
5061 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
5062 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
5063 Blob.data() + BlockNameLen + UserInfoLen);
5064 return false;
5065}
5066
5067llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
5068 BitstreamCursor &Stream = F.Stream;
5069
5070 RecordData Record;
5071 while (true) {
5072 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5073 if (!MaybeEntry)
5074 return MaybeEntry.takeError();
5075 llvm::BitstreamEntry Entry = MaybeEntry.get();
5076
5077 switch (Entry.Kind) {
5078 case llvm::BitstreamEntry::SubBlock:
5079 if (llvm::Error Err = Stream.SkipBlock())
5080 return Err;
5081 continue;
5082 case llvm::BitstreamEntry::EndBlock:
5083 return llvm::Error::success();
5084 case llvm::BitstreamEntry::Error:
5085 return llvm::createStringError(std::errc::illegal_byte_sequence,
5086 "malformed block record in AST file");
5087 case llvm::BitstreamEntry::Record:
5088 break;
5089 }
5090
5091 Record.clear();
5092 StringRef Blob;
5093 Expected<unsigned> MaybeRecCode =
5094 Stream.readRecord(Entry.ID, Record, &Blob);
5095 if (!MaybeRecCode)
5096 return MaybeRecCode.takeError();
5097 switch (MaybeRecCode.get()) {
5098 case EXTENSION_METADATA: {
5100 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5101 return llvm::createStringError(
5102 std::errc::illegal_byte_sequence,
5103 "malformed EXTENSION_METADATA in AST file");
5104
5105 // Find a module file extension with this block name.
5106 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
5107 if (Known == ModuleFileExtensions.end()) break;
5108
5109 // Form a reader.
5110 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
5111 F, Stream)) {
5112 F.ExtensionReaders.push_back(std::move(Reader));
5113 }
5114
5115 break;
5116 }
5117 }
5118 }
5119
5120 return llvm::Error::success();
5121}
5122
5124 assert(ContextObj && "no context to initialize");
5125 ASTContext &Context = *ContextObj;
5126
5127 // If there's a listener, notify them that we "read" the translation unit.
5128 if (DeserializationListener)
5129 DeserializationListener->DeclRead(
5131 Context.getTranslationUnitDecl());
5132
5133 // FIXME: Find a better way to deal with collisions between these
5134 // built-in types. Right now, we just ignore the problem.
5135
5136 // Load the special types.
5137 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5138 if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5139 if (!Context.CFConstantStringTypeDecl)
5140 Context.setCFConstantStringType(GetType(String));
5141 }
5142
5143 if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5144 QualType FileType = GetType(File);
5145 if (FileType.isNull()) {
5146 Error("FILE type is NULL");
5147 return;
5148 }
5149
5150 if (!Context.FILEDecl) {
5151 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
5152 Context.setFILEDecl(Typedef->getDecl());
5153 else {
5154 const TagType *Tag = FileType->getAs<TagType>();
5155 if (!Tag) {
5156 Error("Invalid FILE type in AST file");
5157 return;
5158 }
5159 Context.setFILEDecl(Tag->getDecl());
5160 }
5161 }
5162 }
5163
5164 if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5165 QualType Jmp_bufType = GetType(Jmp_buf);
5166 if (Jmp_bufType.isNull()) {
5167 Error("jmp_buf type is NULL");
5168 return;
5169 }
5170
5171 if (!Context.jmp_bufDecl) {
5172 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
5173 Context.setjmp_bufDecl(Typedef->getDecl());
5174 else {
5175 const TagType *Tag = Jmp_bufType->getAs<TagType>();
5176 if (!Tag) {
5177 Error("Invalid jmp_buf type in AST file");
5178 return;
5179 }
5180 Context.setjmp_bufDecl(Tag->getDecl());
5181 }
5182 }
5183 }
5184
5185 if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5186 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
5187 if (Sigjmp_bufType.isNull()) {
5188 Error("sigjmp_buf type is NULL");
5189 return;
5190 }
5191
5192 if (!Context.sigjmp_bufDecl) {
5193 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
5194 Context.setsigjmp_bufDecl(Typedef->getDecl());
5195 else {
5196 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
5197 assert(Tag && "Invalid sigjmp_buf type in AST file");
5198 Context.setsigjmp_bufDecl(Tag->getDecl());
5199 }
5200 }
5201 }
5202
5203 if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5204 if (Context.ObjCIdRedefinitionType.isNull())
5205 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
5206 }
5207
5208 if (TypeID ObjCClassRedef =
5210 if (Context.ObjCClassRedefinitionType.isNull())
5211 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
5212 }
5213
5214 if (TypeID ObjCSelRedef =
5215 SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5216 if (Context.ObjCSelRedefinitionType.isNull())
5217 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
5218 }
5219
5220 if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5221 QualType Ucontext_tType = GetType(Ucontext_t);
5222 if (Ucontext_tType.isNull()) {
5223 Error("ucontext_t type is NULL");
5224 return;
5225 }
5226
5227 if (!Context.ucontext_tDecl) {
5228 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
5229 Context.setucontext_tDecl(Typedef->getDecl());
5230 else {
5231 const TagType *Tag = Ucontext_tType->getAs<TagType>();
5232 assert(Tag && "Invalid ucontext_t type in AST file");
5233 Context.setucontext_tDecl(Tag->getDecl());
5234 }
5235 }
5236 }
5237 }
5238
5239 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
5240
5241 // If there were any CUDA special declarations, deserialize them.
5242 if (!CUDASpecialDeclRefs.empty()) {
5243 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
5245 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5246 }
5247
5248 // Re-export any modules that were imported by a non-module AST file.
5249 // FIXME: This does not make macro-only imports visible again.
5250 for (auto &Import : PendingImportedModules) {
5251 if (Module *Imported = getSubmodule(Import.ID)) {
5252 makeModuleVisible(Imported, Module::AllVisible,
5253 /*ImportLoc=*/Import.ImportLoc);
5254 if (Import.ImportLoc.isValid())
5255 PP.makeModuleVisible(Imported, Import.ImportLoc);
5256 // This updates visibility for Preprocessor only. For Sema, which can be
5257 // nullptr here, we do the same later, in UpdateSema().
5258 }
5259 }
5260
5261 // Hand off these modules to Sema.
5262 PendingImportedModulesSema.append(PendingImportedModules);
5263 PendingImportedModules.clear();
5264}
5265
5267 // Nothing to do for now.
5268}
5269
5270/// Reads and return the signature record from \p PCH's control block, or
5271/// else returns 0.
5273 BitstreamCursor Stream(PCH);
5274 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5275 // FIXME this drops the error on the floor.
5276 consumeError(std::move(Err));
5277 return ASTFileSignature();
5278 }
5279
5280 // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5282 return ASTFileSignature();
5283
5284 // Scan for SIGNATURE inside the diagnostic options block.
5286 while (true) {
5288 Stream.advanceSkippingSubblocks();
5289 if (!MaybeEntry) {
5290 // FIXME this drops the error on the floor.
5291 consumeError(MaybeEntry.takeError());
5292 return ASTFileSignature();
5293 }
5294 llvm::BitstreamEntry Entry = MaybeEntry.get();
5295
5296 if (Entry.Kind != llvm::BitstreamEntry::Record)
5297 return ASTFileSignature();
5298
5299 Record.clear();
5300 StringRef Blob;
5301 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5302 if (!MaybeRecord) {
5303 // FIXME this drops the error on the floor.
5304 consumeError(MaybeRecord.takeError());
5305 return ASTFileSignature();
5306 }
5307 if (SIGNATURE == MaybeRecord.get()) {
5308 auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
5309 assert(Signature != ASTFileSignature::createDummy() &&
5310 "Dummy AST file signature not backpatched in ASTWriter.");
5311 return Signature;
5312 }
5313 }
5314}
5315
5316/// Retrieve the name of the original source file name
5317/// directly from the AST file, without actually loading the AST
5318/// file.
5320 const std::string &ASTFileName, FileManager &FileMgr,
5321 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5322 // Open the AST file.
5323 auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,
5324 /*RequiresNullTerminator=*/false);
5325 if (!Buffer) {
5326 Diags.Report(diag::err_fe_unable_to_read_pch_file)
5327 << ASTFileName << Buffer.getError().message();
5328 return std::string();
5329 }
5330
5331 // Initialize the stream
5332 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5333
5334 // Sniff for the signature.