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