[clang] 4e637fc - [clang][deps] Make sure ScanInstance outlives collector

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 16 09:44:20 PDT 2022


Author: Jan Svoboda
Date: 2022-09-16T09:44:12-07:00
New Revision: 4e637fcb25503cc9b3bb9af54453ac2ebc7299b1

URL: https://github.com/llvm/llvm-project/commit/4e637fcb25503cc9b3bb9af54453ac2ebc7299b1
DIFF: https://github.com/llvm/llvm-project/commit/4e637fcb25503cc9b3bb9af54453ac2ebc7299b1.diff

LOG: [clang][deps] Make sure ScanInstance outlives collector

The `ScanInstance` is a local variable in `DependencyScanningAction::runInvocation()` that is referenced by `ModuleDepCollector`. Since D132405, `ModuleDepCollector` can escape the function and can outlive its `ScanInstance`. This patch fixes that.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D133988

Added: 
    

Modified: 
    clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 3968656f4632b..7474e0272505e 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -174,7 +174,8 @@ class DependencyScanningAction : public tooling::ToolAction {
     Scanned = true;
 
     // Create a compiler instance to handle the actual work.
-    CompilerInstance ScanInstance(std::move(PCHContainerOps));
+    ScanInstanceStorage.emplace(std::move(PCHContainerOps));
+    CompilerInstance &ScanInstance = *ScanInstanceStorage;
     ScanInstance.setInvocation(std::move(Invocation));
 
     // Create the compiler's actual diagnostics engine.
@@ -304,7 +305,8 @@ class DependencyScanningAction : public tooling::ToolAction {
   bool OptimizeArgs;
   bool EagerLoadModules;
   bool DisableFree;
-  llvm::Optional<StringRef> ModuleName;
+  Optional<StringRef> ModuleName;
+  Optional<CompilerInstance> ScanInstanceStorage;
   std::shared_ptr<ModuleDepCollector> MDC;
   std::vector<std::string> LastCC1Arguments;
   bool Scanned = false;


        


More information about the cfe-commits mailing list