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