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