| //===- ASTReader.cpp - AST File Reader ------------------------------------===// |
| // |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| // See https://llvm.org/LICENSE.txt for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file defines the ASTReader class, which reads AST files. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "ASTCommon.h" |
| #include "ASTReaderInternals.h" |
| #include "clang/AST/ASTConsumer.h" |
| #include "clang/AST/ASTContext.h" |
| #include "clang/AST/ASTMutationListener.h" |
| #include "clang/AST/ASTUnresolvedSet.h" |
| #include "clang/AST/AbstractTypeReader.h" |
| #include "clang/AST/Decl.h" |
| #include "clang/AST/DeclBase.h" |
| #include "clang/AST/DeclCXX.h" |
| #include "clang/AST/DeclFriend.h" |
| #include "clang/AST/DeclGroup.h" |
| #include "clang/AST/DeclObjC.h" |
| #include "clang/AST/DeclTemplate.h" |
| #include "clang/AST/DeclarationName.h" |
| #include "clang/AST/Expr.h" |
| #include "clang/AST/ExprCXX.h" |
| #include "clang/AST/ExternalASTSource.h" |
| #include "clang/AST/NestedNameSpecifier.h" |
| #include "clang/AST/ODRHash.h" |
| #include "clang/AST/OpenMPClause.h" |
| #include "clang/AST/RawCommentList.h" |
| #include "clang/AST/TemplateBase.h" |
| #include "clang/AST/TemplateName.h" |
| #include "clang/AST/Type.h" |
| #include "clang/AST/TypeLoc.h" |
| #include "clang/AST/TypeLocVisitor.h" |
| #include "clang/AST/UnresolvedSet.h" |
| #include "clang/Basic/CommentOptions.h" |
| #include "clang/Basic/Diagnostic.h" |
| #include "clang/Basic/DiagnosticError.h" |
| #include "clang/Basic/DiagnosticOptions.h" |
| #include "clang/Basic/ExceptionSpecificationType.h" |
| #include "clang/Basic/FileManager.h" |
| #include "clang/Basic/FileSystemOptions.h" |
| #include "clang/Basic/IdentifierTable.h" |
| #include "clang/Basic/LLVM.h" |
| #include "clang/Basic/LangOptions.h" |
| #include "clang/Basic/Module.h" |
| #include "clang/Basic/ObjCRuntime.h" |
| #include "clang/Basic/OpenMPKinds.h" |
| #include "clang/Basic/OperatorKinds.h" |
| #include "clang/Basic/PragmaKinds.h" |
| #include "clang/Basic/Sanitizers.h" |
| #include "clang/Basic/SourceLocation.h" |
| #include "clang/Basic/SourceManager.h" |
| #include "clang/Basic/SourceManagerInternals.h" |
| #include "clang/Basic/Specifiers.h" |
| #include "clang/Basic/TargetInfo.h" |
| #include "clang/Basic/TargetOptions.h" |
| #include "clang/Basic/TokenKinds.h" |
| #include "clang/Basic/Version.h" |
| #include "clang/Lex/HeaderSearch.h" |
| #include "clang/Lex/HeaderSearchOptions.h" |
| #include "clang/Lex/MacroInfo.h" |
| #include "clang/Lex/ModuleMap.h" |
| #include "clang/Lex/PreprocessingRecord.h" |
| #include "clang/Lex/Preprocessor.h" |
| #include "clang/Lex/PreprocessorOptions.h" |
| #include "clang/Lex/Token.h" |
| #include "clang/Sema/ObjCMethodList.h" |
| #include "clang/Sema/Scope.h" |
| #include "clang/Sema/Sema.h" |
| #include "clang/Sema/Weak.h" |
| #include "clang/Serialization/ASTBitCodes.h" |
| #include "clang/Serialization/ASTDeserializationListener.h" |
| #include "clang/Serialization/ASTRecordReader.h" |
| #include "clang/Serialization/ContinuousRangeMap.h" |
| #include "clang/Serialization/GlobalModuleIndex.h" |
| #include "clang/Serialization/InMemoryModuleCache.h" |
| #include "clang/Serialization/ModuleFile.h" |
| #include "clang/Serialization/ModuleFileExtension.h" |
| #include "clang/Serialization/ModuleManager.h" |
| #include "clang/Serialization/PCHContainerOperations.h" |
| #include "clang/Serialization/SerializationDiagnostic.h" |
| #include "llvm/ADT/APFloat.h" |
| #include "llvm/ADT/APInt.h" |
| #include "llvm/ADT/APSInt.h" |
| #include "llvm/ADT/ArrayRef.h" |
| #include "llvm/ADT/DenseMap.h" |
| #include "llvm/ADT/FloatingPointMode.h" |
| #include "llvm/ADT/FoldingSet.h" |
| #include "llvm/ADT/Hashing.h" |
| #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| #include "llvm/ADT/None.h" |
| #include "llvm/ADT/Optional.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/ADT/ScopeExit.h" |
| #include "llvm/ADT/SmallPtrSet.h" |
| #include "llvm/ADT/SmallString.h" |
| #include "llvm/ADT/SmallVector.h" |
| #include "llvm/ADT/StringExtras.h" |
| #include "llvm/ADT/StringMap.h" |
| #include "llvm/ADT/StringRef.h" |
| #include "llvm/ADT/Triple.h" |
| #include "llvm/ADT/iterator_range.h" |
| #include "llvm/Bitstream/BitstreamReader.h" |
| #include "llvm/Support/Casting.h" |
| #include "llvm/Support/Compiler.h" |
| #include "llvm/Support/Compression.h" |
| #include "llvm/Support/DJB.h" |
| #include "llvm/Support/Endian.h" |
| #include "llvm/Support/Error.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/FileSystem.h" |
| #include "llvm/Support/LEB128.h" |
| #include "llvm/Support/MemoryBuffer.h" |
| #include "llvm/Support/Path.h" |
| #include "llvm/Support/SaveAndRestore.h" |
| #include "llvm/Support/Timer.h" |
| #include "llvm/Support/VersionTuple.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include <algorithm> |
| #include <cassert> |
| #include <cstddef> |
| #include <cstdint> |
| #include <cstdio> |
| #include <ctime> |
| #include <iterator> |
| #include <limits> |
| #include <map> |
| #include <memory> |
| #include <string> |
| #include <system_error> |
| #include <tuple> |
| #include <utility> |
| #include <vector> |
| |
| using namespace clang; |
| using namespace clang::serialization; |
| using namespace clang::serialization::reader; |
| using llvm::BitstreamCursor; |
| using llvm::RoundingMode; |
| |
| //===----------------------------------------------------------------------===// |
| // ChainedASTReaderListener implementation |
| //===----------------------------------------------------------------------===// |
| |
| bool |
| ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { |
| return First->ReadFullVersionInformation(FullVersion) || |
| Second->ReadFullVersionInformation(FullVersion); |
| } |
| |
| void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { |
| First->ReadModuleName(ModuleName); |
| Second->ReadModuleName(ModuleName); |
| } |
| |
| void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { |
| First->ReadModuleMapFile(ModuleMapPath); |
| Second->ReadModuleMapFile(ModuleMapPath); |
| } |
| |
| bool |
| ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, |
| bool Complain, |
| bool AllowCompatibleDifferences) { |
| return First->ReadLanguageOptions(LangOpts, Complain, |
| AllowCompatibleDifferences) || |
| Second->ReadLanguageOptions(LangOpts, Complain, |
| AllowCompatibleDifferences); |
| } |
| |
| bool ChainedASTReaderListener::ReadTargetOptions( |
| const TargetOptions &TargetOpts, bool Complain, |
| bool AllowCompatibleDifferences) { |
| return First->ReadTargetOptions(TargetOpts, Complain, |
| AllowCompatibleDifferences) || |
| Second->ReadTargetOptions(TargetOpts, Complain, |
| AllowCompatibleDifferences); |
| } |
| |
| bool ChainedASTReaderListener::ReadDiagnosticOptions( |
| IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { |
| return First->ReadDiagnosticOptions(DiagOpts, Complain) || |
| Second->ReadDiagnosticOptions(DiagOpts, Complain); |
| } |
| |
| bool |
| ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, |
| bool Complain) { |
| return First->ReadFileSystemOptions(FSOpts, Complain) || |
| Second->ReadFileSystemOptions(FSOpts, Complain); |
| } |
| |
| bool ChainedASTReaderListener::ReadHeaderSearchOptions( |
| const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, |
| bool Complain) { |
| return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, |
| Complain) || |
| Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, |
| Complain); |
| } |
| |
| bool ChainedASTReaderListener::ReadPreprocessorOptions( |
| const PreprocessorOptions &PPOpts, bool Complain, |
| std::string &SuggestedPredefines) { |
| return First->ReadPreprocessorOptions(PPOpts, Complain, |
| SuggestedPredefines) || |
| Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); |
| } |
| |
| void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, |
| unsigned Value) { |
| First->ReadCounter(M, Value); |
| Second->ReadCounter(M, Value); |
| } |
| |
| bool ChainedASTReaderListener::needsInputFileVisitation() { |
| return First->needsInputFileVisitation() || |
| Second->needsInputFileVisitation(); |
| } |
| |
| bool ChainedASTReaderListener::needsSystemInputFileVisitation() { |
| return First->needsSystemInputFileVisitation() || |
| Second->needsSystemInputFileVisitation(); |
| } |
| |
| void ChainedASTReaderListener::visitModuleFile(StringRef Filename, |
| ModuleKind Kind) { |
| First->visitModuleFile(Filename, Kind); |
| Second->visitModuleFile(Filename, Kind); |
| } |
| |
| bool ChainedASTReaderListener::visitInputFile(StringRef Filename, |
| bool isSystem, |
| bool isOverridden, |
| bool isExplicitModule) { |
| bool Continue = false; |
| if (First->needsInputFileVisitation() && |
| (!isSystem || First->needsSystemInputFileVisitation())) |
| Continue |= First->visitInputFile(Filename, isSystem, isOverridden, |
| isExplicitModule); |
| if (Second->needsInputFileVisitation() && |
| (!isSystem || Second->needsSystemInputFileVisitation())) |
| Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, |
| isExplicitModule); |
| return Continue; |
| } |
| |
| void ChainedASTReaderListener::readModuleFileExtension( |
| const ModuleFileExtensionMetadata &Metadata) { |
| First->readModuleFileExtension(Metadata); |
| Second->readModuleFileExtension(Metadata); |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // PCH validator implementation |
| //===----------------------------------------------------------------------===// |
| |
| ASTReaderListener::~ASTReaderListener() = default; |
| |
| /// Compare the given set of language options against an existing set of |
| /// language options. |
| /// |
| /// \param Diags If non-NULL, diagnostics will be emitted via this engine. |
| /// \param AllowCompatibleDifferences If true, differences between compatible |
| /// language options will be permitted. |
| /// |
| /// \returns true if the languagae options mis-match, false otherwise. |
| static bool checkLanguageOptions(const LangOptions &LangOpts, |
| const LangOptions &ExistingLangOpts, |
| DiagnosticsEngine *Diags, |
| bool AllowCompatibleDifferences = true) { |
| #define LANGOPT(Name, Bits, Default, Description) \ |
| if (ExistingLangOpts.Name != LangOpts.Name) { \ |
| if (Diags) \ |
| Diags->Report(diag::err_pch_langopt_mismatch) \ |
| << Description << LangOpts.Name << ExistingLangOpts.Name; \ |
| return true; \ |
| } |
| |
| #define VALUE_LANGOPT(Name, Bits, Default, Description) \ |
| if (ExistingLangOpts.Name != LangOpts.Name) { \ |
| if (Diags) \ |
| Diags->Report(diag::err_pch_langopt_value_mismatch) \ |
| << Description; \ |
| return true; \ |
| } |
| |
| #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ |
| if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ |
| if (Diags) \ |
| Diags->Report(diag::err_pch_langopt_value_mismatch) \ |
| << Description; \ |
| return true; \ |
| } |
| |
| #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ |
| if (!AllowCompatibleDifferences) \ |
| LANGOPT(Name, Bits, Default, Description) |
| |
| #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ |
| if (!AllowCompatibleDifferences) \ |
| ENUM_LANGOPT(Name, Bits, Default, Description) |
| |
| #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ |
| if (!AllowCompatibleDifferences) \ |
| VALUE_LANGOPT(Name, Bits, Default, Description) |
| |
| #define BENIGN_LANGOPT(Name, Bits, Default, Description) |
| #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) |
| #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) |
| #include "clang/Basic/LangOptions.def" |
| |
| if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { |
| if (Diags) |
| Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; |
| return true; |
| } |
| |
| if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { |
| if (Diags) |
| Diags->Report(diag::err_pch_langopt_value_mismatch) |
| << "target Objective-C runtime"; |
| return true; |
| } |
| |
| if (ExistingLangOpts.CommentOpts.BlockCommandNames != |
| LangOpts.CommentOpts.BlockCommandNames) { |
| if (Diags) |
| Diags->Report(diag::err_pch_langopt_value_mismatch) |
| << "block command names"; |
| return true; |
| } |
| |
| // Sanitizer feature mismatches are treated as compatible differences. If |
| // compatible differences aren't allowed, we still only want to check for |
| // mismatches of non-modular sanitizers (the only ones which can affect AST |
| // generation). |
| if (!AllowCompatibleDifferences) { |
| SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); |
| SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; |
| SanitizerSet ImportedSanitizers = LangOpts.Sanitize; |
| ExistingSanitizers.clear(ModularSanitizers); |
| ImportedSanitizers.clear(ModularSanitizers); |
| if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { |
| const std::string Flag = "-fsanitize="; |
| if (Diags) { |
| #define SANITIZER(NAME, ID) \ |
| { \ |
| bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ |
| bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ |
| if (InExistingModule != InImportedModule) \ |
| Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ |
| << InExistingModule << (Flag + NAME); \ |
| } |
| #include "clang/Basic/Sanitizers.def" |
| } |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| /// Compare the given set of target options against an existing set of |
| /// target options. |
| /// |
| /// \param Diags If non-NULL, diagnostics will be emitted via this engine. |
| /// |
| /// \returns true if the target options mis-match, false otherwise. |
| static bool checkTargetOptions(const TargetOptions &TargetOpts, |
| const TargetOptions &ExistingTargetOpts, |
| DiagnosticsEngine *Diags, |
| bool AllowCompatibleDifferences = true) { |
| #define CHECK_TARGET_OPT(Field, Name) \ |
| if (TargetOpts.Field != ExistingTargetOpts.Field) { \ |
| if (Diags) \ |
| Diags->Report(diag::err_pch_targetopt_mismatch) \ |
| << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ |
| return true; \ |
| } |
| |
| // The triple and ABI must match exactly. |
| CHECK_TARGET_OPT(Triple, "target"); |
| CHECK_TARGET_OPT(ABI, "target ABI"); |
| |
| // We can tolerate different CPUs in many cases, notably when one CPU |
| // supports a strict superset of another. When allowing compatible |
| // differences skip this check. |
| if (!AllowCompatibleDifferences) { |
| CHECK_TARGET_OPT(CPU, "target CPU"); |
| CHECK_TARGET_OPT(TuneCPU, "tune CPU"); |
| } |
| |
| #undef CHECK_TARGET_OPT |
| |
| // Compare feature sets. |
| SmallVector<StringRef, 4> ExistingFeatures( |
| ExistingTargetOpts.FeaturesAsWritten.begin(), |
| ExistingTargetOpts.FeaturesAsWritten.end()); |
| SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), |
| TargetOpts.FeaturesAsWritten.end()); |
| llvm::sort(ExistingFeatures); |
| llvm::sort(ReadFeatures); |
| |
| // We compute the set difference in both directions explicitly so that we can |
| // diagnose the differences differently. |
| SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; |
| std::set_difference( |
| ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), |
| ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); |
| std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), |
| ExistingFeatures.begin(), ExistingFeatures.end(), |
| std::back_inserter(UnmatchedReadFeatures)); |
| |
| // If we are allowing compatible differences and the read feature set is |
| // a strict subset of the existing feature set, there is nothing to diagnose. |
| if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) |
| return false; |
| |
| if (Diags) { |
| for (StringRef Feature : UnmatchedReadFeatures) |
| Diags->Report(diag::err_pch_targetopt_feature_mismatch) |
| << /* is-existing-feature */ false << Feature; |
| for (StringRef Feature : UnmatchedExistingFeatures) |
| Diags->Report(diag::err_pch_targetopt_feature_mismatch) |
| << /* is-existing-feature */ true << Feature; |
| } |
| |
| return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); |
| } |
| |
| bool |
| PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, |
| bool Complain, |
| bool AllowCompatibleDifferences) { |
| const LangOptions &ExistingLangOpts = PP.getLangOpts(); |
| return checkLanguageOptions(LangOpts, ExistingLangOpts, |
| Complain ? &Reader.Diags : nullptr, |
| AllowCompatibleDifferences); |
| } |
| |
| bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, |
| bool Complain, |
| bool AllowCompatibleDifferences) { |
| const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); |
| return checkTargetOptions(TargetOpts, ExistingTargetOpts, |
| Complain ? &Reader.Diags : nullptr, |
| AllowCompatibleDifferences); |
| } |
| |
| namespace { |
| |
| using MacroDefinitionsMap = |
| llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; |
| using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; |
| |
| } // namespace |
| |
| static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, |
| DiagnosticsEngine &Diags, |
| bool Complain) { |
| using Level = DiagnosticsEngine::Level; |
| |
| // Check current mappings for new -Werror mappings, and the stored mappings |
| // for cases that were explicitly mapped to *not* be errors that are now |
| // errors because of options like -Werror. |
| DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; |
| |
| for (DiagnosticsEngine *MappingSource : MappingSources) { |
| for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { |
| diag::kind DiagID = DiagIDMappingPair.first; |
| Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); |
| if (CurLevel < DiagnosticsEngine::Error) |
| continue; // not significant |
| Level StoredLevel = |
| StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); |
| if (StoredLevel < DiagnosticsEngine::Error) { |
| if (Complain) |
| Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + |
| Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); |
| return true; |
| } |
| } |
| } |
| |
| return false; |
| } |
| |
| static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { |
| diag::Severity Ext = Diags.getExtensionHandlingBehavior(); |
| if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) |
| return true; |
| return Ext >= diag::Severity::Error; |
| } |
| |
| static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, |
| DiagnosticsEngine &Diags, |
| bool IsSystem, bool Complain) { |
| // Top-level options |
| if (IsSystem) { |
| if (Diags.getSuppressSystemWarnings()) |
| return false; |
| // If -Wsystem-headers was not enabled before, be conservative |
| if (StoredDiags.getSuppressSystemWarnings()) { |
| if (Complain) |
| Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; |
| return true; |
| } |
| } |
| |
| if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { |
| if (Complain) |
| Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; |
| return true; |
| } |
| |
| if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && |
| !StoredDiags.getEnableAllWarnings()) { |
| if (Complain) |
| Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; |
| return true; |
| } |
| |
| if (isExtHandlingFromDiagsError(Diags) && |
| !isExtHandlingFromDiagsError(StoredDiags)) { |
| if (Complain) |
| Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; |
| return true; |
| } |
| |
| return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); |
| } |
| |
| /// Return the top import module if it is implicit, nullptr otherwise. |
| static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, |
| Preprocessor &PP) { |
| // If the original import came from a file explicitly generated by the user, |
| // don't check the diagnostic mappings. |
| // FIXME: currently this is approximated by checking whether this is not a |
| // module import of an implicitly-loaded module file. |
| // Note: ModuleMgr.rbegin() may not be the current module, but it must be in |
| // the transitive closure of its imports, since unrelated modules cannot be |
| // imported until after this module finishes validation. |
| ModuleFile *TopImport = &*ModuleMgr.rbegin(); |
| while (!TopImport->ImportedBy.empty()) |
| TopImport = TopImport->ImportedBy[0]; |
| if (TopImport->Kind != MK_ImplicitModule) |
| return nullptr; |
| |
| StringRef ModuleName = TopImport->ModuleName; |
| assert(!ModuleName.empty() && "diagnostic options read before module name"); |
| |
| Module *M = |
| PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); |
| assert(M && "missing module"); |
| return M; |
| } |
| |
| bool PCHValidator::ReadDiagnosticOptions( |
| IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { |
| DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); |
| IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); |
| IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| new DiagnosticsEngine(DiagIDs, DiagOpts.get())); |
| // This should never fail, because we would have processed these options |
| // before writing them to an ASTFile. |
| ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); |
| |
| ModuleManager &ModuleMgr = Reader.getModuleManager(); |
| assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); |
| |
| Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); |
| if (!TopM) |
| return false; |
| |
| // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that |
| // contains the union of their flags. |
| return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, |
| Complain); |
| } |
| |
| /// Collect the macro definitions provided by the given preprocessor |
| /// options. |
| static void |
| collectMacroDefinitions(const PreprocessorOptions &PPOpts, |
| MacroDefinitionsMap &Macros, |
| SmallVectorImpl<StringRef> *MacroNames = nullptr) { |
| for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { |
| StringRef Macro = PPOpts.Macros[I].first; |
| bool IsUndef = PPOpts.Macros[I].second; |
| |
| std::pair<StringRef, StringRef> MacroPair = Macro.split('='); |
| StringRef MacroName = MacroPair.first; |
| StringRef MacroBody = MacroPair.second; |
| |
| // For an #undef'd macro, we only care about the name. |
| if (IsUndef) { |
| if (MacroNames && !Macros.count(MacroName)) |
| MacroNames->push_back(MacroName); |
| |
| Macros[MacroName] = std::make_pair("", true); |
| continue; |
| } |
| |
| // For a #define'd macro, figure out the actual definition. |
| if (MacroName.size() == Macro.size()) |
| MacroBody = "1"; |
| else { |
| // Note: GCC drops anything following an end-of-line character. |
| StringRef::size_type End = MacroBody.find_first_of("\n\r"); |
| MacroBody = MacroBody.substr(0, End); |
| } |
| |
| if (MacroNames && !Macros.count(MacroName)) |
| MacroNames->push_back(MacroName); |
| Macros[MacroName] = std::make_pair(MacroBody, false); |
| } |
| } |
| |
| /// Check the preprocessor options deserialized from the control block |
| /// against the preprocessor options in an existing preprocessor. |
| /// |
| /// \param Diags If non-null, produce diagnostics for any mismatches incurred. |
| /// \param Validate If true, validate preprocessor options. If false, allow |
| /// macros defined by \p ExistingPPOpts to override those defined by |
| /// \p PPOpts in SuggestedPredefines. |
| static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, |
| const PreprocessorOptions &ExistingPPOpts, |
| DiagnosticsEngine *Diags, |
| FileManager &FileMgr, |
| std::string &SuggestedPredefines, |
| const LangOptions &LangOpts, |
| bool Validate = true) { |
| // Check macro definitions. |
| MacroDefinitionsMap ASTFileMacros; |
| collectMacroDefinitions(PPOpts, ASTFileMacros); |
| MacroDefinitionsMap ExistingMacros; |
| SmallVector<StringRef, 4> ExistingMacroNames; |
| collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); |
| |
| for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { |
| // Dig out the macro definition in the existing preprocessor options. |
| StringRef MacroName = ExistingMacroNames[I]; |
| std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; |
| |
| // Check whether we know anything about this macro name or not. |
| llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = |
| ASTFileMacros.find(MacroName); |
| if (!Validate || Known == ASTFileMacros.end()) { |
| // FIXME: Check whether this identifier was referenced anywhere in the |
| // AST file. If so, we should reject the AST file. Unfortunately, this |
| // information isn't in the control block. What shall we do about it? |
| |
| if (Existing.second) { |
| SuggestedPredefines += "#undef "; |
| SuggestedPredefines += MacroName.str(); |
| SuggestedPredefines += '\n'; |
| } else { |
| SuggestedPredefines += "#define "; |
| SuggestedPredefines += MacroName.str(); |
| SuggestedPredefines += ' '; |
| SuggestedPredefines += Existing.first.str(); |
| SuggestedPredefines += '\n'; |
| } |
| continue; |
| } |
| |
| // If the macro was defined in one but undef'd in the other, we have a |
| // conflict. |
| if (Existing.second != Known->second.second) { |
| if (Diags) { |
| Diags->Report(diag::err_pch_macro_def_undef) |
| << MacroName << Known->second.second; |
| } |
| return true; |
| } |
| |
| // If the macro was #undef'd in both, or if the macro bodies are identical, |
| // it's fine. |
| if (Existing.second || Existing.first == Known->second.first) |
| continue; |
| |
| // The macro bodies differ; complain. |
| if (Diags) { |
| Diags->Report(diag::err_pch_macro_def_conflict) |
| << MacroName << Known->second.first << Existing.first; |
| } |
| return true; |
| } |
| |
| // Check whether we're using predefines. |
| if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { |
| if (Diags) { |
| Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; |
| } |
| return true; |
| } |
| |
| // Detailed record is important since it is used for the module cache hash. |
| if (LangOpts.Modules && |
| PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { |
| if (Diags) { |
| Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; |
| } |
| return true; |
| } |
| |
| // Compute the #include and #include_macros lines we need. |
| for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { |
| StringRef File = ExistingPPOpts.Includes[I]; |
| |
| if (!ExistingPPOpts.ImplicitPCHInclude.empty() && |
| !ExistingPPOpts.PCHThroughHeader.empty()) { |
| // In case the through header is an include, we must add all the includes |
| // to the predefines so the start point can be determined. |
| SuggestedPredefines += "#include \""; |
| SuggestedPredefines += File; |
| SuggestedPredefines += "\"\n"; |
| continue; |
| } |
| |
| if (File == ExistingPPOpts.ImplicitPCHInclude) |
| continue; |
| |
| if (llvm::is_contained(PPOpts.Includes, File)) |
| continue; |
| |
| SuggestedPredefines += "#include \""; |
| SuggestedPredefines += File; |
| SuggestedPredefines += "\"\n"; |
| } |
| |
| for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { |
| StringRef File = ExistingPPOpts.MacroIncludes[I]; |
| if (llvm::is_contained(PPOpts.MacroIncludes, File)) |
| continue; |
| |
| SuggestedPredefines += "#__include_macros \""; |
| SuggestedPredefines += File; |
| SuggestedPredefines += "\"\n##\n"; |
| } |
| |
| return false; |
| } |
| |
| bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, |
| bool Complain, |
| std::string &SuggestedPredefines) { |
| const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); |
| |
| return checkPreprocessorOptions(PPOpts, ExistingPPOpts, |
| Complain? &Reader.Diags : nullptr, |
| PP.getFileManager(), |
| SuggestedPredefines, |
| PP.getLangOpts()); |
| } |
| |
| bool SimpleASTReaderListener::ReadPreprocessorOptions( |
| const PreprocessorOptions &PPOpts, |
| bool Complain, |
| std::string &SuggestedPredefines) { |
| return checkPreprocessorOptions(PPOpts, |
| PP.getPreprocessorOpts(), |
| nullptr, |
| PP.getFileManager(), |
| SuggestedPredefines, |
| PP.getLangOpts(), |
| false); |
| } |
| |
| /// Check the header search options deserialized from the control block |
| /// against the header search options in an existing preprocessor. |
| /// |
| /// \param Diags If non-null, produce diagnostics for any mismatches incurred. |
| static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, |
| StringRef SpecificModuleCachePath, |
| StringRef ExistingModuleCachePath, |
| DiagnosticsEngine *Diags, |
| const LangOptions &LangOpts, |
| const PreprocessorOptions &PPOpts) { |
| if (LangOpts.Modules) { |
| if (SpecificModuleCachePath != ExistingModuleCachePath && |
| !PPOpts.AllowPCHWithDifferentModulesCachePath) { |
| if (Diags) |
| Diags->Report(diag::err_pch_modulecache_mismatch) |
| << SpecificModuleCachePath << ExistingModuleCachePath; |
| return true; |
| } |
| } |
| |
| return false; |
| } |
| |
| bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, |
| StringRef SpecificModuleCachePath, |
| bool Complain) { |
| return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, |
| PP.getHeaderSearchInfo().getModuleCachePath(), |
| Complain ? &Reader.Diags : nullptr, |
| PP.getLangOpts(), PP.getPreprocessorOpts()); |
| } |
| |
| void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { |
| PP.setCounterValue(Value); |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // AST reader implementation |
| //===----------------------------------------------------------------------===// |
| |
| static uint64_t readULEB(const unsigned char *&P) { |
| unsigned Length = 0; |
| const char *Error = nullptr; |
| |
| uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); |
| if (Error) |
| llvm::report_fatal_error(Error); |
| P += Length; |
| return Val; |
| } |
| |
| /// Read ULEB-encoded key length and data length. |
| static std::pair<unsigned, unsigned> |
| readULEBKeyDataLength(const unsigned char *&P) { |
| unsigned KeyLen = readULEB(P); |
| if ((unsigned)KeyLen != KeyLen) |
| llvm::report_fatal_error("key too large"); |
| |
| unsigned DataLen = readULEB(P); |
| if ((unsigned)DataLen != DataLen) |
| llvm::report_fatal_error("data too large"); |
| |
| return std::make_pair(KeyLen, DataLen); |
| } |
| |
| void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, |
| bool TakeOwnership) { |
| DeserializationListener = Listener; |
| OwnsDeserializationListener = TakeOwnership; |
| } |
| |
| unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { |
| return serialization::ComputeHash(Sel); |
| } |
| |
| std::pair<unsigned, unsigned> |
| ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { |
| return readULEBKeyDataLength(d); |
| } |
| |
| ASTSelectorLookupTrait::internal_key_type |
| ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { |
| using namespace llvm::support; |
| |
| SelectorTable &SelTable = Reader.getContext().Selectors; |
| unsigned N = endian::readNext<uint16_t, little, unaligned>(d); |
| IdentifierInfo *FirstII = Reader.getLocalIdentifier( |
| F, endian::readNext<uint32_t, little, unaligned>(d)); |
| if (N == 0) |
| return SelTable.getNullarySelector(FirstII); |
| else if (N == 1) |
| return SelTable.getUnarySelector(FirstII); |
| |
| SmallVector<IdentifierInfo *, 16> Args; |
| Args.push_back(FirstII); |
| for (unsigned I = 1; I != N; ++I) |
| Args.push_back(Reader.getLocalIdentifier( |
| F, endian::readNext<uint32_t, little, unaligned>(d))); |
| |
| return SelTable.getSelector(N, Args.data()); |
| } |
| |
| ASTSelectorLookupTrait::data_type |
| ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, |
| unsigned DataLen) { |
| using namespace llvm::support; |
| |
| data_type Result; |
| |
| Result.ID = Reader.getGlobalSelectorID( |
| F, endian::readNext<uint32_t, little, unaligned>(d)); |
| unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); |
| unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); |
| Result.InstanceBits = FullInstanceBits & 0x3; |
| Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; |
| Result.FactoryBits = FullFactoryBits & 0x3; |
| Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; |
| unsigned NumInstanceMethods = FullInstanceBits >> 3; |
| unsigned NumFactoryMethods = FullFactoryBits >> 3; |
| |
| // Load instance methods |
| for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
| if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( |
| F, endian::readNext<uint32_t, little, unaligned>(d))) |
| Result.Instance.push_back(Method); |
| } |
| |
| // Load factory methods |
| for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
| if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( |
| F, endian::readNext<uint32_t, little, unaligned>(d))) |
| Result.Factory.push_back(Method); |
| } |
| |
| return Result; |
| } |
| |
| unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { |
| return llvm::djbHash(a); |
| } |
| |
| std::pair<unsigned, unsigned> |
| ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { |
| return readULEBKeyDataLength(d); |
| } |
| |
| ASTIdentifierLookupTraitBase::internal_key_type |
| ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { |
| assert(n >= 2 && d[n-1] == '\0'); |
| return StringRef((const char*) d, n-1); |
| } |
| |
| /// Whether the given identifier is "interesting". |
| static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, |
| bool IsModule) { |
| return II.hadMacroDefinition() || II.isPoisoned() || |
| (!IsModule && II.getObjCOrBuiltinID()) || |
| II.hasRevertedTokenIDToIdentifier() || |
| (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && |
| II.getFETokenInfo()); |
| } |
| |
| static bool readBit(unsigned &Bits) { |
| bool Value = Bits & 0x1; |
| Bits >>= 1; |
| return Value; |
| } |
| |
| IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { |
| using namespace llvm::support; |
| |
| unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); |
| return Reader.getGlobalIdentifierID(F, RawID >> 1); |
| } |
| |
| static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { |
| if (!II.isFromAST()) { |
| II.setIsFromAST(); |
| bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; |
| if (isInterestingIdentifier(Reader, II, IsModule)) |
| II.setChangedSinceDeserialization(); |
| } |
| } |
| |
| IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, |
| const unsigned char* d, |
| unsigned DataLen) { |
| using namespace llvm::support; |
| |
| unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); |
| bool IsInteresting = RawID & 0x01; |
| |
| // Wipe out the "is interesting" bit. |
| RawID = RawID >> 1; |
| |
| // Build the IdentifierInfo and link the identifier ID with it. |
| IdentifierInfo *II = KnownII; |
| if (!II) { |
| II = &Reader.getIdentifierTable().getOwn(k); |
| KnownII = II; |
| } |
| markIdentifierFromAST(Reader, *II); |
| Reader.markIdentifierUpToDate(II); |
| |
| IdentID ID = Reader.getGlobalIdentifierID(F, RawID); |
| if (!IsInteresting) { |
| // For uninteresting identifiers, there's nothing else to do. Just notify |
| // the reader that we've finished loading this identifier. |
| Reader.SetIdentifierInfo(ID, II); |
| return II; |
| } |
| |
| unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); |
| unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); |
| bool CPlusPlusOperatorKeyword = readBit(Bits); |
| bool HasRevertedTokenIDToIdentifier = readBit(Bits); |
| bool Poisoned = readBit(Bits); |
| bool ExtensionToken = readBit(Bits); |
| bool HadMacroDefinition = readBit(Bits); |
| |
| assert(Bits == 0 && "Extra bits in the identifier?"); |
| DataLen -= 8; |
| |
| // Set or check the various bits in the IdentifierInfo structure. |
| // Token IDs are read-only. |
| if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) |
| II->revertTokenIDToIdentifier(); |
| if (!F.isModule()) |
| II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
| assert(II->isExtensionToken() == ExtensionToken && |
| "Incorrect extension token flag"); |
| (void)ExtensionToken; |
| if (Poisoned) |
| II->setIsPoisoned(true); |
| assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| "Incorrect C++ operator keyword flag"); |
| (void)CPlusPlusOperatorKeyword; |
| |
| // If this identifier is a macro, deserialize the macro |
| // definition. |
| if (HadMacroDefinition) { |
| uint32_t MacroDirectivesOffset = |
| endian::readNext<uint32_t, little, unaligned>(d); |
| DataLen -= 4; |
| |
| Reader.addPendingMacro(II, &F, MacroDirectivesOffset); |
| } |
| |
| Reader.SetIdentifierInfo(ID, II); |
| |
| // Read all of the declarations visible at global scope with this |
| // name. |
| if (DataLen > 0) { |
| SmallVector<uint32_t, 4> DeclIDs; |
| for (; DataLen > 0; DataLen -= 4) |
| DeclIDs.push_back(Reader.getGlobalDeclID( |
| F, endian::readNext<uint32_t, little, unaligned>(d))); |
| Reader.SetGloballyVisibleDecls(II, DeclIDs); |
| } |
| |
| return II; |
| } |
| |
| DeclarationNameKey::DeclarationNameKey(DeclarationName Name) |
| : Kind(Name.getNameKind()) { |
| switch (Kind) { |
| case DeclarationName::Identifier: |
| Data = (uint64_t)Name.getAsIdentifierInfo(); |
| break; |
| case DeclarationName::ObjCZeroArgSelector: |
| case DeclarationName::ObjCOneArgSelector: |
| case DeclarationName::ObjCMultiArgSelector: |
| Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); |
| break; |
| case DeclarationName::CXXOperatorName: |
| Data = Name.getCXXOverloadedOperator(); |
| break; |
| case DeclarationName::CXXLiteralOperatorName: |
| Data = (uint64_t)Name.getCXXLiteralIdentifier(); |
| break; |
| case DeclarationName::CXXDeductionGuideName: |
| Data = (uint64_t)Name.getCXXDeductionGuideTemplate() |
| ->getDeclName().getAsIdentifierInfo(); |
| break; |
| case DeclarationName::CXXConstructorName: |
| case DeclarationName::CXXDestructorName: |
| case DeclarationName::CXXConversionFunctionName: |
| case DeclarationName::CXXUsingDirective: |
| Data = 0; |
| break; |
| } |
| } |
| |
| unsigned DeclarationNameKey::getHash() const { |
| llvm::FoldingSetNodeID ID; |
| ID.AddInteger(Kind); |
| |
| switch (Kind) { |
| case DeclarationName::Identifier: |
| case DeclarationName::CXXLiteralOperatorName: |
| case DeclarationName::CXXDeductionGuideName: |
| ID.AddString(((IdentifierInfo*)Data)->getName()); |
| break; |
| case DeclarationName::ObjCZeroArgSelector: |
| case DeclarationName::ObjCOneArgSelector: |
| case DeclarationName::ObjCMultiArgSelector: |
| ID.AddInteger(serialization::ComputeHash(Selector(Data))); |
| break; |
| case DeclarationName::CXXOperatorName: |
| ID.AddInteger((OverloadedOperatorKind)Data); |
| break; |
| case DeclarationName::CXXConstructorName: |
| case DeclarationName::CXXDestructorName: |
| case DeclarationName::CXXConversionFunctionName: |
| case DeclarationName::CXXUsingDirective: |
| break; |
| } |
| |
| return ID.ComputeHash(); |
| } |
| |
| ModuleFile * |
| ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { |
| using namespace llvm::support; |
| |
| uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); |
| return Reader.getLocalModuleFile(F, ModuleFileID); |
| } |
| |
| std::pair<unsigned, unsigned> |
| ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { |
| return readULEBKeyDataLength(d); |
| } |
| |
| ASTDeclContextNameLookupTrait::internal_key_type |
| ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { |
| using namespace llvm::support; |
| |
| auto Kind = (DeclarationName::NameKind)*d++; |
| uint64_t Data; |
| switch (Kind) { |
| case DeclarationName::Identifier: |
| case DeclarationName::CXXLiteralOperatorName: |
| case DeclarationName::CXXDeductionGuideName: |
| Data = (uint64_t)Reader.getLocalIdentifier( |
| F, endian::readNext<uint32_t, little, unaligned>(d)); |
| break; |
| case DeclarationName::ObjCZeroArgSelector: |
| case DeclarationName::ObjCOneArgSelector: |
| case DeclarationName::ObjCMultiArgSelector: |
| Data = |
| (uint64_t)Reader.getLocalSelector( |
| F, endian::readNext<uint32_t, little, unaligned>( |
| d)).getAsOpaquePtr(); |
| break; |
| case DeclarationName::CXXOperatorName: |
| Data = *d++; // OverloadedOperatorKind |
| break; |
| case DeclarationName::CXXConstructorName: |
| case DeclarationName::CXXDestructorName: |
| case DeclarationName::CXXConversionFunctionName: |
| case DeclarationName::CXXUsingDirective: |
| Data = 0; |
| break; |
| } |
| |
| return DeclarationNameKey(Kind, Data); |
| } |
| |
| void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, |
| const unsigned char *d, |
| unsigned DataLen, |
| data_type_builder &Val) { |
| using namespace llvm::support; |
| |
| for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { |
| uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); |
| Val.insert(Reader.getGlobalDeclID(F, LocalID)); |
| } |
| } |
| |
| bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, |
| BitstreamCursor &Cursor, |
| uint64_t Offset, |
| DeclContext *DC) { |
| assert(Offset != 0); |
| |
| SavedStreamPosition SavedPosition(Cursor); |
| if (llvm::Error Err = Cursor.JumpToBit(Offset)) { |
| Error(std::move(Err)); |
| return true; |
| } |
| |
| RecordData Record; |
| StringRef Blob; |
| Expected<unsigned> MaybeCode = Cursor.ReadCode(); |
| if (!MaybeCode) { |
| Error(MaybeCode.takeError()); |
| return true; |
| } |
| unsigned Code = MaybeCode.get(); |
| |
| Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); |
| if (!MaybeRecCode) { |
| Error(MaybeRecCode.takeError()); |
| return true; |
| } |
| unsigned RecCode = MaybeRecCode.get(); |
| if (RecCode != DECL_CONTEXT_LEXICAL) { |
| Error("Expected lexical block"); |
| return true; |
| } |
| |
| assert(!isa<TranslationUnitDecl>(DC) && |
| "expected a TU_UPDATE_LEXICAL record for TU"); |
| // If we are handling a C++ class template instantiation, we can see multiple |
| // lexical updates for the same record. It's important that we select only one |
| // of them, so that field numbering works properly. Just pick the first one we |
| // see. |
| auto &Lex = LexicalDecls[DC]; |
| if (!Lex.first) { |
| Lex = std::make_pair( |
| &M, llvm::makeArrayRef( |
| reinterpret_cast<const llvm::support::unaligned_uint32_t *>( |
| Blob.data()), |
| Blob.size() / 4)); |
| } |
| DC->setHasExternalLexicalStorage(true); |
| return false; |
| } |
| |
| bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, |
| BitstreamCursor &Cursor, |
| uint64_t Offset, |
| DeclID ID) { |
| assert(Offset != 0); |
| |
| SavedStreamPosition SavedPosition(Cursor); |
| if (llvm::Error Err = Cursor.JumpToBit(Offset)) { |
| Error(std::move(Err)); |
| return true; |
| } |
| |
| RecordData Record; |
| StringRef Blob; |
| Expected<unsigned> MaybeCode = Cursor.ReadCode(); |
| if (!MaybeCode) { |
| Error(MaybeCode.takeError()); |
| return true; |
| } |
| unsigned Code = MaybeCode.get(); |
| |
| Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); |
| if (!MaybeRecCode) { |
| Error(MaybeRecCode.takeError()); |
| return true; |
| } |
| unsigned RecCode = MaybeRecCode.get(); |
| if (RecCode != DECL_CONTEXT_VISIBLE) { |
| Error("Expected visible lookup table block"); |
| return true; |
| } |
| |
| // We can't safely determine the primary context yet, so delay attaching the |
| // lookup table until we're done with recursive deserialization. |
| auto *Data = (const unsigned char*)Blob.data(); |
| PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); |
| return false; |
| } |
| |
| void ASTReader::Error(StringRef Msg) const { |
| Error(diag::err_fe_pch_malformed, Msg); |
| if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && |
| !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { |
| Diag(diag::note_module_cache_path) |
| << PP.getHeaderSearchInfo().getModuleCachePath(); |
| } |
| } |
| |
| void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, |
| StringRef Arg3) const { |
| if (Diags.isDiagnosticInFlight()) |
| Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); |
| else |
| Diag(DiagID) << Arg1 << Arg2 << Arg3; |
| } |
| |
| void ASTReader::Error(llvm::Error &&Err) const { |
| llvm::Error RemainingErr = |
| handleErrors(std::move(Err), [this](const DiagnosticError &E) { |
| auto Diag = E.getDiagnostic().second; |
| |
| // Ideally we'd just emit it, but have to handle a possible in-flight |
| // diagnostic. Note that the location is currently ignored as well. |
| auto NumArgs = Diag.getStorage()->NumDiagArgs; |
| assert(NumArgs <= 3 && "Can only have up to 3 arguments"); |
| StringRef Arg1, Arg2, Arg3; |
| switch (NumArgs) { |
| case 3: |
| Arg3 = Diag.getStringArg(2); |
| LLVM_FALLTHROUGH; |
| case 2: |
| Arg2 = Diag.getStringArg(1); |
| LLVM_FALLTHROUGH; |
| case 1: |
| Arg1 = Diag.getStringArg(0); |
| } |
| Error(Diag.getDiagID(), Arg1, Arg2, Arg3); |
| }); |
| if (RemainingErr) |
| Error(toString(std::move(RemainingErr))); |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Source Manager Deserialization |
| //===----------------------------------------------------------------------===// |
| |
| /// Read the line table in the source manager block. |
| void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { |
| unsigned Idx = 0; |
| LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| |
| // Parse the file names |
| std::map<int, int> FileIDs; |
| FileIDs[-1] = -1; // For unspecified filenames. |
| for (unsigned I = 0; Record[Idx]; ++I) { |
| // Extract the file name |
| auto Filename = ReadPath(F, Record, Idx); |
| FileIDs[I] = LineTable.getLineTableFilenameID(Filename); |
| } |
| ++Idx; |
| |
| // Parse the line entries |
| std::vector<LineEntry> Entries; |
| while (Idx < Record.size()) { |
| int FID = Record[Idx++]; |
| assert(FID >= 0 && "Serialized line entries for non-local file."); |
| // Remap FileID from 1-based old view. |
| FID += F.SLocEntryBaseID - 1; |
| |
| // Extract the line entries |
| unsigned NumEntries = Record[Idx++]; |
| assert(NumEntries && "no line entries for file ID"); |
| Entries.clear(); |
| Entries.reserve(NumEntries); |
| for (unsigned I = 0; I != NumEntries; ++I) { |
| unsigned FileOffset = Record[Idx++]; |
| unsigned LineNo = Record[Idx++]; |
| int FilenameID = FileIDs[Record[Idx++]]; |
| SrcMgr::CharacteristicKind FileKind |
| = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| unsigned IncludeOffset = Record[Idx++]; |
| Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| FileKind, IncludeOffset)); |
| } |
| LineTable.AddEntry(FileID::get(FID), Entries); |
| } |
| } |
| |
| /// Read a source manager block |
| llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { |
| using namespace SrcMgr; |
| |
| BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; |
| |
| // Set the source-location entry cursor to the current position in |
| // the stream. This cursor will be used to read the contents of the |
| // source manager block initially, and then lazily read |
| // source-location entries as needed. |
| SLocEntryCursor = F.Stream; |
| |
| // The stream itself is going to skip over the source manager block. |
| if (llvm::Error Err = F.Stream.SkipBlock()) |
| return Err; |
| |
| // Enter the source manager block. |
| if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) |
| return Err; |
| F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); |
| |
| RecordData Record; |
| while (true) { |
| Expected<llvm::BitstreamEntry> MaybeE = |
| SLocEntryCursor.advanceSkippingSubblocks(); |
| if (!MaybeE) |
| return MaybeE.takeError(); |
| llvm::BitstreamEntry E = MaybeE.get(); |
| |
| switch (E.Kind) { |
| case llvm::BitstreamEntry::SubBlock: // Handled for us already. |
| case llvm::BitstreamEntry::Error: |
| return llvm::createStringError(std::errc::illegal_byte_sequence, |
| "malformed block record in AST file"); |
| case llvm::BitstreamEntry::EndBlock: |
| return llvm::Error::success(); |
| case llvm::BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| StringRef Blob; |
| Expected<unsigned> MaybeRecord = |
| SLocEntryCursor.readRecord(E.ID, Record, &Blob); |
| if (!MaybeRecord) |
| return MaybeRecord.takeError(); |
| switch (MaybeRecord.get()) { |
| default: // Default behavior: ignore. |
| break; |
| |
| case SM_SLOC_FILE_ENTRY: |
| case SM_SLOC_BUFFER_ENTRY: |
| case SM_SLOC_EXPANSION_ENTRY: |
| // Once we hit one of the source location entries, we're done. |
| return llvm::Error::success(); |
| } |
| } |
| } |
| |
| /// If a header file is not found at the path that we expect it to be |
| /// and the PCH file was moved from its original location, try to resolve the |
| /// file by assuming that header+PCH were moved together and the header is in |
| /// the same place relative to the PCH. |
| static std::string |
| resolveFileRelativeToOriginalDir(const std::string &Filename, |
| const std::string &OriginalDir, |
| const std::string &CurrDir) { |
| assert(OriginalDir != CurrDir && |
| "No point trying to resolve the file if the PCH dir didn't change"); |
| |
| using namespace llvm::sys; |
| |
| SmallString<128> filePath(Filename); |
| fs::make_absolute(filePath); |
| assert(path::is_absolute(OriginalDir)); |
| SmallString<128> currPCHPath(CurrDir); |
| |
| path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), |
| fileDirE = path::end(path::parent_path(filePath)); |
| path::const_iterator origDirI = path::begin(OriginalDir), |
| origDirE = path::end(OriginalDir); |
| // Skip the common path components from filePath and OriginalDir. |
| while (fileDirI != fileDirE && origDirI != origDirE && |
| *fileDirI == *origDirI) { |
| ++fileDirI; |
| ++origDirI; |
| } |
| for (; origDirI != origDirE; ++origDirI) |
| path::append(currPCHPath, ".."); |
| path::append(currPCHPath, fileDirI, fileDirE); |
| path::append(currPCHPath, path::filename(Filename)); |
| return std::string(currPCHPath.str()); |
| } |
| |
| bool ASTReader::ReadSLocEntry(int ID) { |
| if (ID == 0) |
| return false; |
| |
| if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { |
| Error("source location entry ID out-of-range for AST file"); |
| return true; |
| } |
| |
| // Local helper to read the (possibly-compressed) buffer data following the |
| // entry record. |
| auto ReadBuffer = [this]( |
| BitstreamCursor &SLocEntryCursor, |
| StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { |
| RecordData Record; |
| StringRef Blob; |
| Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); |
| if (!MaybeCode) { |
| Error(MaybeCode.takeError()); |
| return nullptr; |
| } |
| unsigned Code = MaybeCode.get(); |
| |
| Expected<unsigned> MaybeRecCode = |
| SLocEntryCursor.readRecord(Code, Record, &Blob); |
| if (!MaybeRecCode) { |
| Error(MaybeRecCode.takeError()); |
| return nullptr; |
| } |
| unsigned RecCode = MaybeRecCode.get(); |
| |
| if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { |
| if (!llvm::zlib::isAvailable()) { |
| Error("zlib is not available"); |
| return nullptr; |
| } |
| SmallString<0> Uncompressed; |
| if (llvm::Error E = |
| llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { |
| Error("could not decompress embedded file contents: " + |
| llvm::toString(std::move(E))); |
| return nullptr; |
| } |
| return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); |
| } else if (RecCode == SM_SLOC_BUFFER_BLOB) { |
| return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); |
| } else { |
| Error("AST record has invalid code"); |
| return nullptr; |
| } |
| }; |
| |
| ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; |
| if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( |
| F->SLocEntryOffsetsBase + |
| F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { |
| Error(std::move(Err)); |
| return true; |
| } |
| |
| BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; |
| SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; |
| |
| ++NumSLocEntriesRead; |
| Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); |
| if (!MaybeEntry) { |
| Error(MaybeEntry.takeError()); |
| return true; |
| } |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| |
| if (Entry.Kind != llvm::BitstreamEntry::Record) { |
| Error("incorrectly-formatted source location entry in AST file"); |
| return true; |
| } |
| |
| RecordData Record; |
| StringRef Blob; |
| Expected<unsigned> MaybeSLOC = |
| SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); |
| if (!MaybeSLOC) { |
| Error(MaybeSLOC.takeError()); |
| return true; |
| } |
| switch (MaybeSLOC.get()) { |
| default: |
| Error("incorrectly-formatted source location entry in AST file"); |
| return true; |
| |
| case SM_SLOC_FILE_ENTRY: { |
| // We will detect whether a file changed and return 'Failure' for it, but |
| // we will also try to fail gracefully by setting up the SLocEntry. |
| unsigned InputID = Record[4]; |
| InputFile IF = getInputFile(*F, InputID); |
| Optional<FileEntryRef> File = IF.getFile(); |
| bool OverriddenBuffer = IF.isOverridden(); |
| |
| // Note that we only check if a File was returned. If it was out-of-date |
| // we have complained but we will continue creating a FileID to recover |
| // gracefully. |
| if (!File) |
| return true; |
| |
| SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); |
| if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { |
| // This is the module's main file. |
| IncludeLoc = getImportLocation(F); |
| } |
| SrcMgr::CharacteristicKind |
| FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; |
| FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, |
| BaseOffset + Record[0]); |
| SrcMgr::FileInfo &FileInfo = |
| const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); |
| FileInfo.NumCreatedFIDs = Record[5]; |
| if (Record[3]) |
| FileInfo.setHasLineDirectives(); |
| |
| unsigned NumFileDecls = Record[7]; |
| if (NumFileDecls && ContextObj) { |
| const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; |
| assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); |
| FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, |
| NumFileDecls)); |
| } |
| |
| const SrcMgr::ContentCache &ContentCache = |
| SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); |
| if (OverriddenBuffer && !ContentCache.BufferOverridden && |
| ContentCache.ContentsEntry == ContentCache.OrigEntry && |
| !ContentCache.getBufferIfLoaded()) { |
| auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); |
| if (!Buffer) |
| return true; |
| SourceMgr.overrideFileContents(*File, std::move(Buffer)); |
| } |
| |
| break; |
| } |
| |
| case SM_SLOC_BUFFER_ENTRY: { |
| const char *Name = Blob.data(); |
| unsigned Offset = Record[0]; |
| SrcMgr::CharacteristicKind |
| FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; |
| SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); |
| if (IncludeLoc.isInvalid() && F->isModule()) { |
| IncludeLoc = getImportLocation(F); |
| } |
| |
| auto Buffer = ReadBuffer(SLocEntryCursor, Name); |
| if (!Buffer) |
| return true; |
| SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, |
| BaseOffset + Offset, IncludeLoc); |
| break; |
| } |
| |
| case SM_SLOC_EXPANSION_ENTRY: { |
| SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); |
| SourceMgr.createExpansionLoc(SpellingLoc, |
| ReadSourceLocation(*F, Record[2]), |
| ReadSourceLocation(*F, Record[3]), |
| Record[5], |
| Record[4], |
| ID, |
| BaseOffset + Record[0]); |
| break; |
| } |
| } |
| |
| return false; |
| } |
| |
| std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { |
| if (ID == 0) |
| return std::make_pair(SourceLocation(), ""); |
| |
| if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { |
| Error("source location entry ID out-of-range for AST file"); |
| return std::make_pair(SourceLocation(), ""); |
| } |
| |
| // Find which module file this entry lands in. |
| ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; |
| if (!M->isModule()) |
| return std::make_pair(SourceLocation(), ""); |
| |
| // FIXME: Can we map this down to a particular submodule? That would be |
| // ideal. |
| return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); |
| } |
| |
| /// Find the location where the module F is imported. |
| SourceLocation ASTReader::getImportLocation(ModuleFile *F) { |
| if (F->ImportLoc.isValid()) |
| return F->ImportLoc; |
| |
| // Otherwise we have a PCH. It's considered to be "imported" at the first |
| // location of its includer. |
| if (F->ImportedBy.empty() || !F->ImportedBy[0]) { |
| // Main file is the importer. |
| assert(SourceMgr.getMainFileID().isValid() && "missing main file"); |
| return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| } |
| return F->ImportedBy[0]->FirstLoc; |
| } |
| |
| /// Enter a subblock of the specified BlockID with the specified cursor. Read |
| /// the abbreviations that are at the top of the block and then leave the cursor |
| /// pointing into the block. |
| llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, |
| unsigned BlockID, |
| uint64_t *StartOfBlockOffset) { |
| if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) |
| return Err; |
| |
| if (StartOfBlockOffset) |
| *StartOfBlockOffset = Cursor.GetCurrentBitNo(); |
| |
| while (true) { |
| uint64_t Offset = Cursor.GetCurrentBitNo(); |
| Expected<unsigned> MaybeCode = Cursor.ReadCode(); |
| if (!MaybeCode) |
| return MaybeCode.takeError(); |
| unsigned Code = MaybeCode.get(); |
| |
| // We expect all abbrevs to be at the start of the block. |
| if (Code != llvm::bitc::DEFINE_ABBREV) { |
| if (llvm::Error Err = Cursor.JumpToBit(Offset)) |
| return Err; |
| return llvm::Error::success(); |
| } |
| if (llvm::Error Err = Cursor.ReadAbbrevRecord()) |
| return Err; |
| } |
| } |
| |
| Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, |
| unsigned &Idx) { |
| Token Tok; |
| Tok.startToken(); |
| Tok.setLocation(ReadSourceLocation(F, Record, Idx)); |
| Tok.setLength(Record[Idx++]); |
| if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) |
| Tok.setIdentifierInfo(II); |
| Tok.setKind((tok::TokenKind)Record[Idx++]); |
| Tok.setFlag((Token::TokenFlags)Record[Idx++]); |
| return Tok; |
| } |
| |
| MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { |
| BitstreamCursor &Stream = F.MacroCursor; |
| |
| // Keep track of where we are in the stream, then jump back there |
| // after reading this macro. |
| SavedStreamPosition SavedPosition(Stream); |
| |
| if (llvm::Error Err = Stream.JumpToBit(Offset)) { |
| // FIXME this drops errors on the floor. |
| consumeError(std::move(Err)); |
| return nullptr; |
| } |
| RecordData Record; |
| SmallVector<IdentifierInfo*, 16> MacroParams; |
| MacroInfo *Macro = nullptr; |
| |
| while (true) { |
| // Advance to the next record, but if we get to the end of the block, don't |
| // pop it (removing all the abbreviations from the cursor) since we want to |
| // be able to reseek within the block and read entries. |
| unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; |
| Expected<llvm::BitstreamEntry> MaybeEntry = |
| Stream.advanceSkippingSubblocks(Flags); |
| if (!MaybeEntry) { |
| Error(MaybeEntry.takeError()); |
| return Macro; |
| } |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| |
| switch (Entry.Kind) { |
| case llvm::BitstreamEntry::SubBlock: // Handled for us already. |
| case llvm::BitstreamEntry::Error: |
| Error("malformed block record in AST file"); |
| return Macro; |
| case llvm::BitstreamEntry::EndBlock: |
| return Macro; |
| case llvm::BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| PreprocessorRecordTypes RecType; |
| if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) |
| RecType = (PreprocessorRecordTypes)MaybeRecType.get(); |
| else { |
| Error(MaybeRecType.takeError()); |
| return Macro; |
| } |
| switch (RecType) { |
| case PP_MODULE_MACRO: |
| case PP_MACRO_DIRECTIVE_HISTORY: |
| return Macro; |
| |
| case PP_MACRO_OBJECT_LIKE: |
| case PP_MACRO_FUNCTION_LIKE: { |
| // If we already have a macro, that means that we've hit the end |
| // of the definition of the macro we were looking for. We're |
| // done. |
| if (Macro) |
| return Macro; |
| |
| unsigned NextIndex = 1; // Skip identifier ID. |
| SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); |
| MacroInfo *MI = PP.AllocateMacroInfo(Loc); |
| MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); |
| MI->setIsUsed(Record[NextIndex++]); |
| MI->setUsedForHeaderGuard(Record[NextIndex++]); |
| |
| if (RecType == PP_MACRO_FUNCTION_LIKE) { |
| // Decode function-like macro info. |
| bool isC99VarArgs = Record[NextIndex++]; |
| bool isGNUVarArgs = Record[NextIndex++]; |
| bool hasCommaPasting = Record[NextIndex++]; |
| MacroParams.clear(); |
| unsigned NumArgs = Record[NextIndex++]; |
| for (unsigned i = 0; i != NumArgs; ++i) |
| MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); |
| |
| // Install function-like macro info. |
| MI->setIsFunctionLike(); |
| if (isC99VarArgs) MI->setIsC99Varargs(); |
| if (isGNUVarArgs) MI->setIsGNUVarargs(); |
| if (hasCommaPasting) MI->setHasCommaPasting(); |
| MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); |
| } |
| |
| // Remember that we saw this macro last so that we add the tokens that |
| // form its body to it. |
| Macro = MI; |
| |
| if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && |
| Record[NextIndex]) { |
| // We have a macro definition. Register the association |
| PreprocessedEntityID |
| GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); |
| PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); |
| PreprocessingRecord::PPEntityID PPID = |
| PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); |
| MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( |
| PPRec.getPreprocessedEntity(PPID)); |
| if (PPDef) |
| PPRec.RegisterMacroDefinition(Macro, PPDef); |
| } |
| |
| ++NumMacrosRead; |
| break; |
| } |
| |
| case PP_TOKEN: { |
| // If we see a TOKEN before a PP_MACRO_*, then the file is |
| // erroneous, just pretend we didn't see this. |
| if (!Macro) break; |
| |
| unsigned Idx = 0; |
| Token Tok = ReadToken(F, Record, Idx); |
| Macro->AddTokenToBody(Tok); |
| break; |
| } |
| } |
| } |
| } |
| |
| PreprocessedEntityID |
| ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, |
| unsigned LocalID) const { |
| if (!M.ModuleOffsetMap.empty()) |
| ReadModuleOffsetMap(M); |
| |
| ContinuousRangeMap<uint32_t, int, 2>::const_iterator |
| I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); |
| assert(I != M.PreprocessedEntityRemap.end() |
| && "Invalid index into preprocessed entity index remap"); |
| |
| return LocalID + I->second; |
| } |
| |
| unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { |
| return llvm::hash_combine(ikey.Size, ikey.ModTime); |
| } |
| |
| HeaderFileInfoTrait::internal_key_type |
| HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { |
| internal_key_type ikey = {FE->getSize(), |
| M.HasTimestamps ? FE->getModificationTime() : 0, |
| FE->getName(), /*Imported*/ false}; |
| return ikey; |
| } |
| |
| bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { |
| if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) |
| return false; |
| |
| if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) |
| return true; |
| |
| // Determine whether the actual files are equivalent. |
| FileManager &FileMgr = Reader.getFileManager(); |
| auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { |
| if (!Key.Imported) { |
| if (auto File = FileMgr.getFile(Key.Filename)) |
| return *File; |
| return nullptr; |
| } |
| |
| std::string Resolved = std::string(Key.Filename); |
| Reader.ResolveImportedPath(M, Resolved); |
| if (auto File = FileMgr.getFile(Resolved)) |
| return *File; |
| return nullptr; |
| }; |
| |
| const FileEntry *FEA = GetFile(a); |
| const FileEntry *FEB = GetFile(b); |
| return FEA && FEA == FEB; |
| } |
| |
| std::pair<unsigned, unsigned> |
| HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { |
| return readULEBKeyDataLength(d); |
| } |
| |
| HeaderFileInfoTrait::internal_key_type |
| HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { |
| using namespace llvm::support; |
| |
| internal_key_type ikey; |
| ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); |
| ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); |
| ikey.Filename = (const char *)d; |
| ikey.Imported = true; |
| return ikey; |
| } |
| |
| HeaderFileInfoTrait::data_type |
| HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, |
| unsigned DataLen) { |
| using namespace llvm::support; |
| |
| const unsigned char *End = d + DataLen; |
| HeaderFileInfo HFI; |
| unsigned Flags = *d++; |
| // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. |
| HFI.isImport |= (Flags >> 5) & 0x01; |
| HFI.isPragmaOnce |= (Flags >> 4) & 0x01; |
| HFI.DirInfo = (Flags >> 1) & 0x07; |
| HFI.IndexHeaderMapHeader = Flags & 0x01; |
| // FIXME: Find a better way to handle this. Maybe just store a |
| // "has been included" flag? |
| HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), |
| HFI.NumIncludes); |
| HFI.ControllingMacroID = Reader.getGlobalIdentifierID( |
| M, endian::readNext<uint32_t, little, unaligned>(d)); |
| if (unsigned FrameworkOffset = |
| endian::readNext<uint32_t, little, unaligned>(d)) { |
| // The framework offset is 1 greater than the actual offset, |
| // since 0 is used as an indicator for "no framework name". |
| StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); |
| HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); |
| } |
| |
| assert((End - d) % 4 == 0 && |
| "Wrong data length in HeaderFileInfo deserialization"); |
| while (d != End) { |
| uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); |
| auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); |
| LocalSMID >>= 2; |
| |
| // This header is part of a module. Associate it with the module to enable |
| // implicit module import. |
| SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); |
| Module *Mod = Reader.getSubmodule(GlobalSMID); |
| FileManager &FileMgr = Reader.getFileManager(); |
| ModuleMap &ModMap = |
| Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); |
| |
| std::string Filename = std::string(key.Filename); |
| if (key.Imported) |
| Reader.ResolveImportedPath(M, Filename); |
| // FIXME: NameAsWritten |
| Module::Header H = {std::string(key.Filename), "", |
| *FileMgr.getFile(Filename)}; |
| ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); |
| HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); |
| } |
| |
| // This HeaderFileInfo was externally loaded. |
| HFI.External = true; |
| HFI.IsValid = true; |
| return HFI; |
| } |
| |
| void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, |
| uint32_t MacroDirectivesOffset) { |
| assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); |
| PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); |
| } |
| |
| void ASTReader::ReadDefinedMacros() { |
| // Note that we are loading defined macros. |
| Deserializing Macros(this); |
| |
| for (ModuleFile &I : llvm::reverse(ModuleMgr)) { |
| BitstreamCursor &MacroCursor = I.MacroCursor; |
| |
| // If there was no preprocessor block, skip this file. |
| if (MacroCursor.getBitcodeBytes().empty()) |
| continue; |
| |
| BitstreamCursor Cursor = MacroCursor; |
| if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { |
| Error(std::move(Err)); |
| return; |
| } |
| |
| RecordData Record; |
| while (true) { |
| Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); |
| if (!MaybeE) { |
| Error(MaybeE.takeError()); |
| return; |
| } |
| llvm::BitstreamEntry E = MaybeE.get(); |
| |
| switch (E.Kind) { |
| case llvm::BitstreamEntry::SubBlock: // Handled for us already. |
| case llvm::BitstreamEntry::Error: |
| Error("malformed block record in AST file"); |
| return; |
| case llvm::BitstreamEntry::EndBlock: |
| goto NextCursor; |
| |
| case llvm::BitstreamEntry::Record: { |
| Record.clear(); |
| Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); |
| if (!MaybeRecord) { |
| Error(MaybeRecord.takeError()); |
| return; |
| } |
| switch (MaybeRecord.get()) { |
| default: // Default behavior: ignore. |
| break; |
| |
| case PP_MACRO_OBJECT_LIKE: |
| case PP_MACRO_FUNCTION_LIKE: { |
| IdentifierInfo *II = getLocalIdentifier(I, Record[0]); |
| if (II->isOutOfDate()) |
| updateOutOfDateIdentifier(*II); |
| break; |
| } |
| |
| case PP_TOKEN: |
| // Ignore tokens. |
| break; |
| } |
| break; |
| } |
| } |
| } |
| NextCursor: ; |
| } |
| } |
| |
| namespace { |
| |
| /// Visitor class used to look up identifirs in an AST file. |
| class IdentifierLookupVisitor { |
| StringRef Name; |
| unsigned NameHash; |
| unsigned PriorGeneration; |
| unsigned &NumIdentifierLookups; |
| unsigned &NumIdentifierLookupHits; |
| IdentifierInfo *Found = nullptr; |
| |
| public: |
| IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, |
| unsigned &NumIdentifierLookups, |
| unsigned &NumIdentifierLookupHits) |
| : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), |
| PriorGeneration(PriorGeneration), |
| NumIdentifierLookups(NumIdentifierLookups), |
| NumIdentifierLookupHits(NumIdentifierLookupHits) {} |
| |
| bool operator()(ModuleFile &M) { |
| // If we've already searched this module file, skip it now. |
| if (M.Generation <= PriorGeneration) |
| return true; |
| |
| ASTIdentifierLookupTable *IdTable |
| = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; |
| if (!IdTable) |
| return false; |
| |
| ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, |
| Found); |
| ++NumIdentifierLookups; |
| ASTIdentifierLookupTable::iterator Pos = |
| IdTable->find_hashed(Name, NameHash, &Trait); |
| if (Pos == IdTable->end()) |
| return false; |
| |
| // Dereferencing the iterator has the effect of building the |
| // IdentifierInfo node and populating it with the various |
| // declarations it needs. |
| ++NumIdentifierLookupHits; |
| Found = *Pos; |
| return true; |
| } |
| |
| // Retrieve the identifier info found within the module |
| // files. |
| IdentifierInfo *getIdentifierInfo() const { return Found; } |
| }; |
| |
| } // namespace |
| |
| void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { |
| // Note that we are loading an identifier. |
| Deserializing AnIdentifier(this); |
| |
| unsigned PriorGeneration = 0; |
| if (getContext().getLangOpts().Modules) |
| PriorGeneration = IdentifierGeneration[&II]; |
| |
| // If there is a global index, look there first to determine which modules |
| // provably do not have any results for this identifier. |
| GlobalModuleIndex::HitSet Hits; |
| GlobalModuleIndex::HitSet *HitsPtr = nullptr; |
| if (!loadGlobalIndex()) { |
| if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { |
| HitsPtr = &Hits; |
| } |
| } |
| |
| IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, |
| NumIdentifierLookups, |
| NumIdentifierLookupHits); |
| ModuleMgr.visit(Visitor, HitsPtr); |
| markIdentifierUpToDate(&II); |
| } |
| |
| void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { |
| if (!II) |
| return; |
| |
| II->setOutOfDate(false); |
| |
| // Update the generation for this identifier. |
| if (getContext().getLangOpts().Modules) |
| IdentifierGeneration[II] = getGeneration(); |
| } |
| |
| void ASTReader::resolvePendingMacro(IdentifierInfo *II, |
| const PendingMacroInfo &PMInfo) { |
| ModuleFile &M = *PMInfo.M; |
| |
| BitstreamCursor &Cursor = M.MacroCursor; |
| SavedStreamPosition SavedPosition(Cursor); |
| if (llvm::Error Err = |
| Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { |
| Error(std::move(Err)); |
| return; |
| } |
| |
| struct ModuleMacroRecord { |
| SubmoduleID SubModID; |
| MacroInfo *MI; |
| SmallVector<SubmoduleID, 8> Overrides; |
| }; |
| llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; |
| |
| // We expect to see a sequence of PP_MODULE_MACRO records listing exported |
| // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete |
| // macro histroy. |
| RecordData Record; |
| while (true) { |
| Expected<llvm::BitstreamEntry> MaybeEntry = |
| Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); |
| if (!MaybeEntry) { |
| Error(MaybeEntry.takeError()); |
| return; |
| } |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| |
| if (Entry.Kind != llvm::BitstreamEntry::Record) { |
| Error("malformed block record in AST file"); |
| return; |
| } |
| |
| Record.clear(); |
| Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); |
| if (!MaybePP) { |
| Error(MaybePP.takeError()); |
| return; |
| } |
| switch ((PreprocessorRecordTypes)MaybePP.get()) { |
| case PP_MACRO_DIRECTIVE_HISTORY: |
| break; |
| |
| case PP_MODULE_MACRO: { |
| ModuleMacros.push_back(ModuleMacroRecord()); |
| auto &Info = ModuleMacros.back(); |
| Info.SubModID = getGlobalSubmoduleID(M, Record[0]); |
| Info.MI = getMacro(getGlobalMacroID(M, Record[1])); |
| for (int I = 2, N = Record.size(); I != N; ++I) |
| Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); |
| continue; |
| } |
| |
| default: |
| Error("malformed block record in AST file"); |
| return; |
| } |
| |
| // We found the macro directive history; that's the last record |
| // for this macro. |
| break; |
| } |
| |
| // Module macros are listed in reverse dependency order. |
| { |
| std::reverse(ModuleMacros.begin(), ModuleMacros.end()); |
| llvm::SmallVector<ModuleMacro*, 8> Overrides; |
| for (auto &MMR : ModuleMacros) { |
| Overrides.clear(); |
| for (unsigned ModID : MMR.Overrides) { |
| Module *Mod = getSubmodule(ModID); |
| auto *Macro = PP.getModuleMacro(Mod, II); |
| assert(Macro && "missing definition for overridden macro"); |
| Overrides.push_back(Macro); |
| } |
| |
| bool Inserted = false; |
| Module *Owner = getSubmodule(MMR.SubModID); |
| PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); |
| } |
| } |
| |
| // Don't read the directive history for a module; we don't have anywhere |
| // to put it. |
| if (M.isModule()) |
| return; |
| |
| // Deserialize the macro directives history in reverse source-order. |
| MacroDirective *Latest = nullptr, *Earliest = nullptr; |
| unsigned Idx = 0, N = Record.size(); |
| while (Idx < N) { |
| MacroDirective *MD = nullptr; |
| SourceLocation Loc = ReadSourceLocation(M, Record, Idx); |
| MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; |
| switch (K) { |
| case MacroDirective::MD_Define: { |
| MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); |
| MD = PP.AllocateDefMacroDirective(MI, Loc); |
| break; |
| } |
| case MacroDirective::MD_Undefine: |
| MD = PP.AllocateUndefMacroDirective(Loc); |
| break; |
| case MacroDirective::MD_Visibility: |
| bool isPublic = Record[Idx++]; |
| MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); |
| break; |
| } |
| |
| if (!Latest) |
| Latest = MD; |
| if (Earliest) |
| Earliest->setPrevious(MD); |
| Earliest = MD; |
| } |
| |
| if (Latest) |
| PP.setLoadedMacroDirective(II, Earliest, Latest); |
| } |
| |
| bool ASTReader::shouldDisableValidationForFile( |
| const serialization::ModuleFile &M) const { |
| if (DisableValidationKind == DisableValidationForModuleKind::None) |
| return false; |
| |
| // If a PCH is loaded and validation is disabled for PCH then disable |
| // validation for the PCH and the modules it loads. |
| ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); |
| |
| switch (K) { |
| case MK_MainFile: |
| case MK_Preamble: |
| case MK_PCH: |
| return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); |
| case MK_ImplicitModule: |
| case MK_ExplicitModule: |
| case MK_PrebuiltModule: |
| return bool(DisableValidationKind & DisableValidationForModuleKind::Module); |
| } |
| |
| return false; |
| } |
| |
| ASTReader::InputFileInfo |
| ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { |
| // Go find this input file. |
| BitstreamCursor &Cursor = F.InputFilesCursor; |
| SavedStreamPosition SavedPosition(Cursor); |
| if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { |
| // FIXME this drops errors on the floor. |
| consumeError(std::move(Err)); |
| } |
| |
| Expected<unsigned> MaybeCode = Cursor.ReadCode(); |
| if (!MaybeCode) { |
| // FIXME this drops errors on the floor. |
| consumeError(MaybeCode.takeError()); |
| } |
| unsigned Code = MaybeCode.get(); |
| RecordData Record; |
| StringRef Blob; |
| |
| if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) |
| assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && |
| "invalid record type for input file"); |
| else { |
| // FIXME this drops errors on the floor. |
| consumeError(Maybe.takeError()); |
| } |
| |
| assert(Record[0] == ID && "Bogus stored ID or offset"); |
| InputFileInfo R; |
| R.StoredSize = static_cast<off_t>(Record[1]); |
| R.StoredTime = static_cast<time_t>(Record[2]); |
| R.Overridden = static_cast<bool>(Record[3]); |
| R.Transient = static_cast<bool>(Record[4]); |
| R.TopLevelModuleMap = static_cast<bool>(Record[5]); |
| R.Filename = std::string(Blob); |
| ResolveImportedPath(F, R.Filename); |
| |
| Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); |
| if (!MaybeEntry) // FIXME this drops errors on the floor. |
| consumeError(MaybeEntry.takeError()); |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| assert(Entry.Kind == llvm::BitstreamEntry::Record && |
| "expected record type for input file hash"); |
| |
| Record.clear(); |
| if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) |
| assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && |
| "invalid record type for input file hash"); |
| else { |
| // FIXME this drops errors on the floor. |
| consumeError(Maybe.takeError()); |
| } |
| R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | |
| static_cast<uint64_t>(Record[0]); |
| return R; |
| } |
| |
| static unsigned moduleKindForDiagnostic(ModuleKind Kind); |
| InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { |
| // If this ID is bogus, just return an empty input file. |
| if (ID == 0 || ID > F.InputFilesLoaded.size()) |
| return InputFile(); |
| |
| // If we've already loaded this input file, return it. |
| if (F.InputFilesLoaded[ID-1].getFile()) |
| return F.InputFilesLoaded[ID-1]; |
| |
| if (F.InputFilesLoaded[ID-1].isNotFound()) |
| return InputFile(); |
| |
| // Go find this input file. |
| BitstreamCursor &Cursor = F.InputFilesCursor; |
| SavedStreamPosition SavedPosition(Cursor); |
| if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { |
| // FIXME this drops errors on the floor. |
| consumeError(std::move(Err)); |
| } |
| |
| InputFileInfo FI = readInputFileInfo(F, ID); |
| off_t StoredSize = FI.StoredSize; |
| time_t StoredTime = FI.StoredTime; |
| bool Overridden = FI.Overridden; |
| bool Transient = FI.Transient; |
| StringRef Filename = FI.Filename; |
| uint64_t StoredContentHash = FI.ContentHash; |
| |
| OptionalFileEntryRefDegradesToFileEntryPtr File = |
| expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); |
| |
| // If we didn't find the file, resolve it relative to the |
| // original directory from which this AST file was created. |
| if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && |
| F.OriginalDir != F.BaseDirectory) { |
| std::string Resolved = resolveFileRelativeToOriginalDir( |
| std::string(Filename), F.OriginalDir, F.BaseDirectory); |
| if (!Resolved.empty()) |
| File = expectedToOptional(FileMgr.getFileRef(Resolved)); |
| } |
| |
| // For an overridden file, create a virtual file with the stored |
| // size/timestamp. |
| if ((Overridden || Transient) && !File) |
| File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); |
| |
| if (!File) { |
| if (Complain) { |
| std::string ErrorStr = "could not find file '"; |
| ErrorStr += Filename; |
| ErrorStr += "' referenced by AST file '"; |
| ErrorStr += F.FileName; |
| ErrorStr += "'"; |
| Error(ErrorStr); |
| } |
| // Record that we didn't find the file. |
| F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); |
| return InputFile(); |
| } |
| |
| // Check if there was a request to override the contents of the file |
| // that was part of the precompiled header. Overriding such a file |
| // can lead to problems when lexing using the source locations from the |
| // PCH. |
| SourceManager &SM = getSourceManager(); |
| // FIXME: Reject if the overrides are different. |
| if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { |
| if (Complain) |
| Error(diag::err_fe_pch_file_overridden, Filename); |
| |
| // After emitting the diagnostic, bypass the overriding file to recover |
| // (this creates a separate FileEntry). |
| File = SM.bypassFileContentsOverride(*File); |
| if (!File) { |
| F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); |
| return InputFile(); |
| } |
| } |
| |
| struct Change { |
| enum ModificationKind { |
| Size, |
| ModTime, |
| Content, |
| None, |
| } Kind; |
| llvm::Optional<int64_t> Old = llvm::None; |
| llvm::Optional<int64_t> New = llvm::None; |
| }; |
| auto HasInputFileChanged = [&]() { |
| if (StoredSize != File->getSize()) |
| return Change{Change::Size, StoredSize, File->getSize()}; |
| if (!shouldDisableValidationForFile(F) && StoredTime && |
| StoredTime != File->getModificationTime()) { |
| Change MTimeChange = {Change::ModTime, StoredTime, |
| File->getModificationTime()}; |
| |
| // In case the modification time changes but not the content, |
| // accept the cached file as legit. |
| if (ValidateASTInputFilesContent && |
| StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { |
| auto MemBuffOrError = FileMgr.getBufferForFile(File); |
| if (!MemBuffOrError) { |
| if (!Complain) |
| return MTimeChange; |
| std::string ErrorStr = "could not get buffer for file '"; |
| ErrorStr += File->getName(); |
| ErrorStr += "'"; |
| Error(ErrorStr); |
| return MTimeChange; |
| } |
| |
| // FIXME: hash_value is not guaranteed to be stable! |
| auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); |
| if (StoredContentHash == static_cast<uint64_t>(ContentHash)) |
| return Change{Change::None}; |
| |
| return Change{Change::Content}; |
| } |
| return MTimeChange; |
| } |
| return Change{Change::None}; |
| }; |
| |
| bool IsOutOfDate = false; |
| auto FileChange = HasInputFileChanged(); |
| // For an overridden file, there is nothing to validate. |
| if (!Overridden && FileChange.Kind != Change::None) { |
| if (Complain && !Diags.isDiagnosticInFlight()) { |
| // Build a list of the PCH imports that got us here (in reverse). |
| SmallVector<ModuleFile *, 4> ImportStack(1, &F); |
| while (!ImportStack.back()->ImportedBy.empty()) |
| ImportStack.push_back(ImportStack.back()->ImportedBy[0]); |
| |
| // The top-level PCH is stale. |
| StringRef TopLevelPCHName(ImportStack.back()->FileName); |
| Diag(diag::err_fe_ast_file_modified) |
| << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) |
| << TopLevelPCHName << FileChange.Kind |
| << (FileChange.Old && FileChange.New) |
| << llvm::itostr(FileChange.Old.getValueOr(0)) |
| << llvm::itostr(FileChange.New.getValueOr(0)); |
| |
| // Print the import stack. |
| if (ImportStack.size() > 1) { |
| Diag(diag::note_pch_required_by) |
| << Filename << ImportStack[0]->FileName; |
| for (unsigned I = 1; I < ImportStack.size(); ++I) |
| Diag(diag::note_pch_required_by) |
| << ImportStack[I-1]->FileName << ImportStack[I]->FileName; |
| } |
| |
| Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; |
| } |
| |
| IsOutOfDate = true; |
| } |
| // FIXME: If the file is overridden and we've already opened it, |
| // issue an error (or split it into a separate FileEntry). |
| |
| InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); |
| |
| // Note that we've loaded this input file. |
| F.InputFilesLoaded[ID-1] = IF; |
| return IF; |
| } |
| |
| /// If we are loading a relocatable PCH or module file, and the filename |
| /// is not an absolute path, add the system or module root to the beginning of |
| /// the file name. |
| void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { |
| // Resolve relative to the base directory, if we have one. |
| if (!M.BaseDirectory.empty()) |
| return ResolveImportedPath(Filename, M.BaseDirectory); |
| } |
| |
| void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { |
| if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) |
| return; |
| |
| SmallString<128> Buffer; |
| llvm::sys::path::append(Buffer, Prefix, Filename); |
| Filename.assign(Buffer.begin(), Buffer.end()); |
| } |
| |
| static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { |
| switch (ARR) { |
| case ASTReader::Failure: return true; |
| case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); |
| case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); |
| case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); |
| case ASTReader::ConfigurationMismatch: |
| return !(Caps & ASTReader::ARR_ConfigurationMismatch); |
| case ASTReader::HadErrors: return true; |
| case ASTReader::Success: return false; |
| } |
| |
| llvm_unreachable("unknown ASTReadResult"); |
| } |
| |
| ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( |
| BitstreamCursor &Stream, unsigned ClientLoadCapabilities, |
| bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, |
| std::string &SuggestedPredefines) { |
| if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { |
| // FIXME this drops errors on the floor. |
| consumeError(std::move(Err)); |
| return Failure; |
| } |
| |
| // Read all of the records in the options block. |
| RecordData Record; |
| ASTReadResult Result = Success; |
| while (true) { |
| Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
| if (!MaybeEntry) { |
| // FIXME this drops errors on the floor. |
| consumeError(MaybeEntry.takeError()); |
| return Failure; |
| } |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| |
| switch (Entry.Kind) { |
| case llvm::BitstreamEntry::Error: |
| case llvm::BitstreamEntry::SubBlock: |
| return Failure; |
| |
| case llvm::BitstreamEntry::EndBlock: |
| return Result; |
| |
| case llvm::BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read and process a record. |
| Record.clear(); |
| Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); |
| if (!MaybeRecordType) { |
| // FIXME this drops errors on the floor. |
| consumeError(MaybeRecordType.takeError()); |
| return Failure; |
| } |
| switch ((OptionsRecordTypes)MaybeRecordType.get()) { |
| case LANGUAGE_OPTIONS: { |
| bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; |
| if (ParseLanguageOptions(Record, Complain, Listener, |
| AllowCompatibleConfigurationMismatch)) |
| Result = ConfigurationMismatch; |
| break; |
| } |
| |
| case TARGET_OPTIONS: { |
| bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; |
| if (ParseTargetOptions(Record, Complain, Listener, |
| AllowCompatibleConfigurationMismatch)) |
| Result = ConfigurationMismatch; |
| break; |
| } |
| |
| case FILE_SYSTEM_OPTIONS: { |
| bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; |
| if (!AllowCompatibleConfigurationMismatch && |
| ParseFileSystemOptions(Record, Complain, Listener)) |
| Result = ConfigurationMismatch; |
| break; |
| } |
| |
| case HEADER_SEARCH_OPTIONS: { |
| bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; |
| if (!AllowCompatibleConfigurationMismatch && |
| ParseHeaderSearchOptions(Record, Complain, Listener)) |
| Result = ConfigurationMismatch; |
| break; |
| } |
| |
| case PREPROCESSOR_OPTIONS: |
| bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; |
| if (!AllowCompatibleConfigurationMismatch && |
| ParsePreprocessorOptions(Record, Complain, Listener, |
| SuggestedPredefines)) |
| Result = ConfigurationMismatch; |
| break; |
| } |
| } |
| } |
| |
| ASTReader::ASTReadResult |
| ASTReader::ReadControlBlock(ModuleFile &F, |
| SmallVectorImpl<ImportedModule> &Loaded, |
| const ModuleFile *ImportedBy, |
| unsigned ClientLoadCapabilities) { |
| BitstreamCursor &Stream = F.Stream; |
| |
| if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { |
| Error(std::move(Err)); |
| return Failure; |
| } |
| |
| // Lambda to read the unhashed control block the first time it's called. |
| // |
| // For PCM files, the unhashed control block cannot be read until after the |
| // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still |
| // need to look ahead before reading the IMPORTS record. For consistency, |
| // this block is always read somehow (see BitstreamEntry::EndBlock). |
| bool HasReadUnhashedControlBlock = false; |
| auto readUnhashedControlBlockOnce = [&]() { |
| if (!HasReadUnhashedControlBlock) { |
| HasReadUnhashedControlBlock = true; |
| if (ASTReadResult Result = |
| readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) |
| return Result; |
| } |
| return Success; |
| }; |
| |
| bool DisableValidation = shouldDisableValidationForFile(F); |
| |
| // Read all of the records and blocks in the control block. |
| RecordData Record; |
| unsigned NumInputs = 0; |
| unsigned NumUserInputs = 0; |
| StringRef BaseDirectoryAsWritten; |
| while (true) { |
| Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); |
| if (!MaybeEntry) { |
| Error(MaybeEntry.takeError()); |
| return Failure; |
| } |
| llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| |
| switch (Entry.Kind) { |
| case llvm::BitstreamEntry::Error: |
| Error("malformed block record in AST file"); |
| return Failure; |
| case llvm::BitstreamEntry::EndBlock: { |
| // Validate the module before returning. This call catches an AST with |
| // no module name and no imports. |
| if (ASTReadResult Result = readUnhashedControlBlockOnce()) |
| return Result; |
| |
| // Validate input files. |
| const HeaderSearchOptions &HSOpts = |
| PP.getHeaderSearchInfo().getHeaderSearchOpts(); |
| |
| // All user input files reside at the index range [0, NumUserInputs), and |
| // system input files reside at [NumUserInputs, NumInputs). For explicitly |
| // loaded module files, ignore missing inputs. |
| if (!DisableValidation && F.Kind != MK_ExplicitModule && |
| F.Kind != MK_PrebuiltModule) { |
| bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; |
| |
| // If we are reading a module, we will create a verification timestamp, |
| // so we verify all input files. Otherwise, verify only user input |
| // files. |
| |
| unsigned N = NumUserInputs; |
| if (ValidateSystemInputs || |
| (HSOpts.ModulesValidateOncePerBuildSession && |
| F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && |
| F.Kind == MK_ImplicitModule)) |
| N = NumInputs; |
| |
| for (unsigned I = 0; I < N; ++I) { |
| InputFile IF = getInputFile(F, I+1, Complain); |
| if (!IF.getFile() || IF.isOutOfDate()) |
| return OutOfDate; |
| } |
| } |
| |
| if (Listener) |
| Listener->visitModuleFile(F.FileName, F.Kind); |
| |
| if (Listener && Listener->needsInputFileVisitation()) { |
| unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs |
| : NumUserInputs; |
| for (unsigned I = 0; I < N; ++I) { |
| bool IsSystem = I >= NumUserInputs; |
| InputFileInfo FI = readInputFileInfo(F, I+1); |
| Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, |
| F.Kind == MK_ExplicitModule || |
| F.Kind == MK_PrebuiltModule); |
| } |
| } |
| |
| return Success; |
| } |
| |
| case llvm::BitstreamEntry::SubBlock: |
| switch (Entry.ID) { |
| case INPUT_FILES_BLOCK_ID: |
| F.InputFilesCursor = Stream; |
| if (llvm::Error Err = Stream.SkipBlock()) { |
| Error(std::move(Err)); |
| return Failure; |
| } |
| if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { |
| Error("malformed block record in AST file"); |
| return Failure; |
| } |
| continue; |
| |
| case OPTIONS_BLOCK_ID: |
| // If we're reading the first module for this group, check its options |
| // are compatible with ours. For modules it imports, no further checking |
| // is required, because we checked them when we built it. |
| if (Listener && !ImportedBy) { |
| // Should we allow the configuration of the module file to differ from |
| |