[PATCH] D148543: [llvm-driver] Check more specific tools first

Alex Brachet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 17 10:05:37 PDT 2023

abrachet created this revision.
abrachet added a reviewer: phosek.
Herald added a project: All.
abrachet requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We allow tools to be called {tool}-{suffix} to support tools being
called something like clang-17. Many clang tools follow this pattern
too for example, clang-scan-deps. It is pure luck wether clang or
clang-scan-deps will appear first in the LLVMDriverTools.def file. If
the former appears first, clang-scan-deps will match against clang,
which is not preferable. Instead we can control the order that tools
are checked so that we always look at more specific tool names first.



Index: llvm/tools/llvm-driver/llvm-driver.cpp
--- llvm/tools/llvm-driver/llvm-driver.cpp
+++ llvm/tools/llvm-driver/llvm-driver.cpp
@@ -15,6 +15,8 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/WithColor.h"
+#include <map>
 using namespace llvm;
 #define LLVM_DRIVER_TOOL(tool, entry)                                          \
@@ -26,6 +28,12 @@
 #include "LLVMDriverTools.def"
+using ToolEntry = int(int, char **, const llvm::ToolContext &);
+std::map<StringRef, ToolEntry &, std::greater<StringRef>> ToolMap{
+#define LLVM_DRIVER_TOOL(tool, entry) {tool, entry##_main},
+#include "LLVMDriverTools.def"
 static void printHelpMessage() {
   llvm::outs() << "OVERVIEW: llvm toolchain driver\n\n"
                << "USAGE: llvm [subcommand] [options]\n\n"
@@ -68,10 +76,9 @@
     return {Argv0, sys::path::filename(Argv0).data(), false};
-#define LLVM_DRIVER_TOOL(tool, entry)                                          \
-  if (Is(tool))                                                                \
-    return entry##_main(Argc, Argv, MakeDriverArgs());
-#include "LLVMDriverTools.def"
+  for (auto &[tool, entry] : ToolMap)
+    if (Is(tool))
+      return entry(Argc, Argv, MakeDriverArgs());
   if (Is("llvm") || Argv0 == Argv[0])
     return findTool(Argc - 1, Argv + 1, Argv0);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148543.514281.patch
Type: text/x-patch
Size: 1398 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230417/0a1838cc/attachment.bin>

More information about the llvm-commits mailing list