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