[PATCH] D92155: Load plugins when creating a CompilerInvocation.

Yafei Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 23:03:48 PST 2020


psionic12 created this revision.
psionic12 added reviewers: njames93, grosser, chapuni, john.brawn, alexfh, sammccall, nridge.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, mgorny.
Herald added a project: clang.
psionic12 requested review of this revision.
Herald added a subscriber: ilya-biryukov.

Since frontend plugins can report diagnostics nowadays,
the old way of loading plugins only fits for clang it self,
tools like clang-check and clangd will not load plugins
even if the `getFrontendOpts().Plugins` has paths of plugins.

To make all tools to report diagnostics from plugins,
move the loading into `CompilerInvocation::CreateFromArgs()`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92155

Files:
  clang-tools-extra/clangd/Compiler.cpp
  clang-tools-extra/clangd/tool/CMakeLists.txt
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
  clang/tools/clang-check/CMakeLists.txt


Index: clang/tools/clang-check/CMakeLists.txt
===================================================================
--- clang/tools/clang-check/CMakeLists.txt
+++ clang/tools/clang-check/CMakeLists.txt
@@ -19,3 +19,8 @@
   clangStaticAnalyzerFrontend
   clangTooling
   )
+
+# Support plugins.
+if(CLANG_PLUGIN_SUPPORT)
+    export_executable_symbols_for_plugins(clang-check)
+endif()
Index: clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -203,25 +203,6 @@
     return true;
   }
 
-  // Load any requested plugins.
-  for (const std::string &Path : Clang->getFrontendOpts().Plugins) {
-    std::string Error;
-    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
-      Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
-        << Path << Error;
-  }
-
-  // Check if any of the loaded plugins replaces the main AST action
-  for (const FrontendPluginRegistry::entry &Plugin :
-       FrontendPluginRegistry::entries()) {
-    std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
-    if (P->getActionType() == PluginASTAction::ReplaceAction) {
-      Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
-      Clang->getFrontendOpts().ActionName = Plugin.getName().str();
-      break;
-    }
-  }
-
   // Honor -mllvm.
   //
   // FIXME: Remove this, one day.
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3899,6 +3899,24 @@
   Res.getCodeGenOpts().Argv0 = Argv0;
   Res.getCodeGenOpts().CommandLineArgs = CommandLineArgs;
 
+  // Load any requested plugins.
+  for (const std::string &Path : Res.getFrontendOpts().Plugins) {
+    std::string Error;
+    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
+      Diags.Report(diag::err_fe_unable_to_load_plugin) << Path << Error;
+  }
+
+  // Check if any of the loaded plugins replaces the main AST action
+  for (const FrontendPluginRegistry::entry &Plugin :
+       FrontendPluginRegistry::entries()) {
+    std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
+    if (P->getActionType() == PluginASTAction::ReplaceAction) {
+      Res.getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
+      Res.getFrontendOpts().ActionName = Plugin.getName().str();
+      break;
+    }
+  }
+
   return Success;
 }
 
Index: clang-tools-extra/clangd/tool/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/tool/CMakeLists.txt
+++ clang-tools-extra/clangd/tool/CMakeLists.txt
@@ -39,3 +39,8 @@
   clangdSupport
   ${CLANGD_XPC_LIBS}
   )
+
+# Support plugins.
+if(CLANG_PLUGIN_SUPPORT)
+  export_executable_symbols_for_plugins(clangd)
+endif()
\ No newline at end of file
Index: clang-tools-extra/clangd/Compiler.cpp
===================================================================
--- clang-tools-extra/clangd/Compiler.cpp
+++ clang-tools-extra/clangd/Compiler.cpp
@@ -9,6 +9,8 @@
 #include "Compiler.h"
 #include "support/Logger.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendPluginRegistry.h"
 #include "clang/Lex/PreprocessorOptions.h"
 #include "clang/Serialization/PCHContainerOperations.h"
 #include "llvm/ADT/StringRef.h"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92155.307762.patch
Type: text/x-patch
Size: 3580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201126/002fd6f4/attachment-0001.bin>


More information about the cfe-commits mailing list