[clang] [Frontend] Move -mllvm and loading of plugins to address a fixme. NFC (PR #192476)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 08:35:42 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Vassil Vassilev (vgvassilev)

<details>
<summary>Changes</summary>

This is a continuation to #<!-- -->192266 

---
Full diff: https://github.com/llvm/llvm-project/pull/192476.diff


4 Files Affected:

- (modified) clang/include/clang/Frontend/CompilerInstance.h (+10-7) 
- (modified) clang/lib/DependencyScanning/DependencyScannerImpl.cpp (+2) 
- (modified) clang/lib/Frontend/CompilerInstance.cpp (+25-7) 
- (modified) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp (+4-17) 


``````````diff
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index bce9ff421ec87..1cb7077977864 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -142,6 +142,9 @@ class CompilerInstance : public ModuleLoader {
   /// The frontend timer.
   std::unique_ptr<llvm::Timer> FrontendTimer;
 
+  /// Whether PrepareForExecution has already been called.
+  bool PreparedForExecution = false;
+
   /// The ASTReader, if one exists.
   IntrusiveRefCntPtr<ASTReader> TheASTReader;
 
@@ -253,6 +256,13 @@ class CompilerInstance : public ModuleLoader {
   // of the context or else not CompilerInstance specific.
   bool ExecuteAction(FrontendAction &Act);
 
+  /// Prepare the CompilerInstance for executing a frontend action.
+  ///
+  /// Called by ExecuteAction. Consolidates instance-level setup that was
+  /// previously duplicated across tool entry points (cc1_main,
+  /// clang-repl/Interpreter, etc.). This method is idempotent.
+  void PrepareForExecution();
+
   /// At the end of a compilation, print the number of warnings/errors.
   void printDiagnosticStats();
 
@@ -816,13 +826,6 @@ class CompilerInstance : public ModuleLoader {
                    bool UseTemporary, bool CreateMissingDirectories = false);
 
 private:
-  /// Prepare the CompilerInstance for executing a frontend action.
-  ///
-  /// Called by ExecuteAction. Consolidates instance-level setup that was
-  /// previously duplicated across tool entry points (cc1_main,
-  /// clang-repl/Interpreter, etc.).
-  void PrepareForExecution();
-
   /// Create a new output file and add it to the list of tracked output files.
   ///
   /// If \p OutputPath is empty, then createOutputFile will derive an output
diff --git a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
index c752f76e53712..df873c5003091 100644
--- a/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
+++ b/clang/lib/DependencyScanning/DependencyScannerImpl.cpp
@@ -418,6 +418,8 @@ void dependencies::initializeScanCompilerInstance(
     DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
     IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS) {
   ScanInstance.setBuildingModule(false);
+  // LLVM options are not going to affect dependency scanning.
+  ScanInstance.getFrontendOpts().LLVMArgs.clear();
   ScanInstance.createVirtualFileSystem(FS, DiagConsumer);
   ScanInstance.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
   if (Service.getOpts().Format == ScanningOutputFormat::P1689)
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index be097ba867407..90e24a0c839dd 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -50,6 +50,7 @@
 #include "llvm/Plugins/PassPlugin.h"
 #include "llvm/Support/AdvisoryLock.h"
 #include "llvm/Support/BuryPointer.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/FileSystem.h"
@@ -945,19 +946,36 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
 // High-Level Operations
 
 void CompilerInstance::PrepareForExecution() {
+  if (PreparedForExecution)
+    return;
+  PreparedForExecution = true;
+
   // Set up the frontend timer for -ftime-report. BackendConsumer uses
-  // getTimerGroup() and getFrontendTimer() when TimePasses is set. In the
-  // cc1 driver path this was done in cc1_main before calling
-  // ExecuteCompilerInvocation; we consolidate it here so that all tools
-  // (cc1, clang-repl, libclang, etc.) get consistent behavior.
+  // getTimerGroup() and getFrontendTimer() when TimePasses is set.
   if (getCodeGenOpts().TimePasses && !FrontendTimer) {
     createFrontendTimer();
     getFrontendTimer().startTimer();
   }
 
-  // FIXME: Consider consolidating additional per-instance setup here:
-  // - llvm::timeTraceProfilerInitialize) when TimeTracePath is set.
-  // - Plugin loading (LoadRequestedPlugins) and -mllvm argument processing.
+  // Load plugins and pass plugins from -fplugin / -fpass-plugin.
+  LoadRequestedPlugins();
+
+  // Honor -mllvm.
+  //
+  // FIXME: Remove this, one day.
+  // This should happen AFTER plugins have been loaded!
+  if (!getFrontendOpts().LLVMArgs.empty()) {
+    unsigned NumArgs = getFrontendOpts().LLVMArgs.size();
+    auto Args = std::make_unique<const char *[]>(NumArgs + 2);
+    Args[0] = "clang (LLVM option parsing)";
+    for (unsigned i = 0; i != NumArgs; ++i)
+      Args[i + 1] = getFrontendOpts().LLVMArgs[i].c_str();
+    Args[NumArgs + 1] = nullptr;
+    llvm::cl::ParseCommandLineOptions(
+        NumArgs + 1, Args.get(), /*Overview=*/"",
+        /*Errs=*/nullptr,
+        hasVirtualFileSystem() ? &getVirtualFileSystem() : nullptr);
+  }
 }
 
 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index e4622496758ac..01f3ae06122c9 100644
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -237,23 +237,10 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
     return true;
   }
 
-  Clang->LoadRequestedPlugins();
-
-  // Honor -mllvm.
-  //
-  // FIXME: Remove this, one day.
-  // This should happen AFTER plugins have been loaded!
-  if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
-    unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
-    auto Args = std::make_unique<const char*[]>(NumArgs + 2);
-    Args[0] = "clang (LLVM option parsing)";
-    for (unsigned i = 0; i != NumArgs; ++i)
-      Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
-    Args[NumArgs + 1] = nullptr;
-    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"",
-                                      /*Errs=*/nullptr,
-                                      /*VFS=*/&Clang->getVirtualFileSystem());
-  }
+  // Load plugins, process -mllvm, and set up timers. This is idempotent and
+  // also called by ExecuteAction, but we need it here before
+  // CreateFrontendAction because plugins can replace the main action.
+  Clang->PrepareForExecution();
 
 #if CLANG_ENABLE_STATIC_ANALYZER
   // These should happen AFTER plugins have been loaded!

``````````

</details>


https://github.com/llvm/llvm-project/pull/192476


More information about the cfe-commits mailing list