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

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 14 05:08:09 PDT 2021


jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, dexonsmith.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 <https://reviews.llvm.org/D102473>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102482

Files:
  clang/include/clang/Frontend/CompilerInstance.h
  clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
  clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp


Index: clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
===================================================================
--- clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -19,9 +19,10 @@
 using namespace dependencies;
 
 static CompilerInvocation
-makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps) {
+makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps,
+                                         const CompilerInvocation &Invocation) {
   // Make a deep copy of the invocation.
-  CompilerInvocation CI(*Deps.Invocation);
+  CompilerInvocation CI(Invocation);
 
   // Remove options incompatible with explicit module build.
   CI.getFrontendOpts().Inputs.clear();
@@ -38,7 +39,7 @@
 }
 
 static std::vector<std::string>
-serializeCompilerInvocation(CompilerInvocation &CI) {
+serializeCompilerInvocation(const CompilerInvocation &CI) {
   // Set up string allocator.
   llvm::BumpPtrAllocator Alloc;
   llvm::StringSaver Strings(Alloc);
@@ -55,7 +56,7 @@
 std::vector<std::string> ModuleDeps::getCanonicalCommandLine(
     std::function<StringRef(ModuleID)> LookupPCMPath,
     std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const {
-  CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this));
+  CompilerInvocation CI(Invocation);
 
   dependencies::detail::collectPCMAndModuleMapPaths(
       ClangModuleDeps, LookupPCMPath, LookupModuleDeps,
@@ -66,9 +67,7 @@
 
 std::vector<std::string>
 ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const {
-  CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this));
-
-  return serializeCompilerInvocation(CI);
+  return serializeCompilerInvocation(Invocation);
 }
 
 void dependencies::detail::collectPCMAndModuleMapPaths(
@@ -190,11 +189,9 @@
         MD.FileDeps.insert(IF.getFile()->getName());
       });
 
-  // FIXME: Prepare the CompilerInvocation for building this module **now**, so
-  //        that we store the actual context hash for this module (not just the
-  //        context hash inherited from the original TU).
-  MD.Invocation = Instance.getInvocationPtr();
-  MD.ID.ContextHash = MD.Invocation->getModuleHash();
+  MD.Invocation =
+      makeInvocationForModuleBuildWithoutPaths(MD, Instance.getInvocation());
+  MD.ID.ContextHash = MD.Invocation.getModuleHash();
 
   llvm::DenseSet<const Module *> AddedModules;
   addAllSubmoduleDeps(M, MD, AddedModules);
Index: clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
===================================================================
--- clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
+++ clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
@@ -75,9 +75,8 @@
   // the primary TU.
   bool ImportedByMainFile = false;
 
-  /// The compiler invocation associated with the translation unit that imports
-  /// this module.
-  std::shared_ptr<CompilerInvocation> Invocation;
+  /// Compiler invocation that can be used to build this module (without paths).
+  CompilerInvocation Invocation;
 
   /// Gets the canonical command line suitable for passing to clang.
   ///
Index: clang/include/clang/Frontend/CompilerInstance.h
===================================================================
--- clang/include/clang/Frontend/CompilerInstance.h
+++ clang/include/clang/Frontend/CompilerInstance.h
@@ -225,11 +225,9 @@
 
   bool hasInvocation() const { return Invocation != nullptr; }
 
-  CompilerInvocation &getInvocation() { return *getInvocationPtr(); }
-
-  std::shared_ptr<CompilerInvocation> getInvocationPtr() {
+  CompilerInvocation &getInvocation() {
     assert(Invocation && "Compiler instance has no invocation!");
-    return Invocation;
+    return *Invocation;
   }
 
   /// setInvocation - Replace the current invocation.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102482.345402.patch
Type: text/x-patch
Size: 3879 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210514/68c857a8/attachment.bin>


More information about the cfe-commits mailing list