[clang][deps] NFC: Report modules' context hash

This patch eagerly constructs and modifies CompilerInvocation of modular dependencies in order to report the correct context hash instead of the hash of the original translation unit.

No functionality change here, since we currently don't modify CompilerInvocation in a way that affects the context hash.

Depends on D102473.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D102482
This commit is contained in:
Jan Svoboda 2021-05-17 09:16:25 +02:00
parent b9d5b0c201
commit d3fb4b9065
2 changed files with 11 additions and 15 deletions

View File

@ -75,9 +75,8 @@ struct ModuleDeps {
// the primary TU. // the primary TU.
bool ImportedByMainFile = false; bool ImportedByMainFile = false;
/// The compiler invocation associated with the translation unit that imports /// Compiler invocation that can be used to build this module (without paths).
/// this module. CompilerInvocation Invocation;
std::shared_ptr<CompilerInvocation> Invocation;
/// Gets the canonical command line suitable for passing to clang. /// Gets the canonical command line suitable for passing to clang.
/// ///

View File

@ -19,9 +19,10 @@ using namespace tooling;
using namespace dependencies; using namespace dependencies;
static CompilerInvocation static CompilerInvocation
makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps) { makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps,
const CompilerInvocation &Invocation) {
// Make a deep copy of the invocation. // Make a deep copy of the invocation.
CompilerInvocation CI(*Deps.Invocation); CompilerInvocation CI(Invocation);
// Remove options incompatible with explicit module build. // Remove options incompatible with explicit module build.
CI.getFrontendOpts().Inputs.clear(); CI.getFrontendOpts().Inputs.clear();
@ -38,7 +39,7 @@ makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps) {
} }
static std::vector<std::string> static std::vector<std::string>
serializeCompilerInvocation(CompilerInvocation &CI) { serializeCompilerInvocation(const CompilerInvocation &CI) {
// Set up string allocator. // Set up string allocator.
llvm::BumpPtrAllocator Alloc; llvm::BumpPtrAllocator Alloc;
llvm::StringSaver Strings(Alloc); llvm::StringSaver Strings(Alloc);
@ -55,7 +56,7 @@ serializeCompilerInvocation(CompilerInvocation &CI) {
std::vector<std::string> ModuleDeps::getCanonicalCommandLine( std::vector<std::string> ModuleDeps::getCanonicalCommandLine(
std::function<StringRef(ModuleID)> LookupPCMPath, std::function<StringRef(ModuleID)> LookupPCMPath,
std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const { std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const {
CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this)); CompilerInvocation CI(Invocation);
dependencies::detail::collectPCMAndModuleMapPaths( dependencies::detail::collectPCMAndModuleMapPaths(
ClangModuleDeps, LookupPCMPath, LookupModuleDeps, ClangModuleDeps, LookupPCMPath, LookupModuleDeps,
@ -66,9 +67,7 @@ std::vector<std::string> ModuleDeps::getCanonicalCommandLine(
std::vector<std::string> std::vector<std::string>
ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const { ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const {
CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this)); return serializeCompilerInvocation(Invocation);
return serializeCompilerInvocation(CI);
} }
void dependencies::detail::collectPCMAndModuleMapPaths( void dependencies::detail::collectPCMAndModuleMapPaths(
@ -190,11 +189,9 @@ ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
MD.FileDeps.insert(IF.getFile()->getName()); MD.FileDeps.insert(IF.getFile()->getName());
}); });
// FIXME: Prepare the CompilerInvocation for building this module **now**, so MD.Invocation =
// that we store the actual context hash for this module (not just the makeInvocationForModuleBuildWithoutPaths(MD, Instance.getInvocation());
// context hash inherited from the original TU). MD.ID.ContextHash = MD.Invocation.getModuleHash();
MD.Invocation = Instance.getInvocationPtr();
MD.ID.ContextHash = MD.Invocation->getModuleHash();
llvm::DenseSet<const Module *> AddedModules; llvm::DenseSet<const Module *> AddedModules;
addAllSubmoduleDeps(M, MD, AddedModules); addAllSubmoduleDeps(M, MD, AddedModules);