[clang] Added removal of file extension when guessing the toolchain (PR #69887)

via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 22 11:11:06 PDT 2023


https://github.com/Overhatted created https://github.com/llvm/llvm-project/pull/69887

I'm using Buck2 to create the compile_commands.json and since it uses a cl.bat wrapper around cl.exe, that's what shows up in the compile_commands.json. Of course Buck2 could be changed to create a compile_commands.json with the cl.exe path but I think this change to clang is better.

Please let me know what you think.

>From 29715f3a97f077d1a8aaf2042de91ac08d6cc1a9 Mon Sep 17 00:00:00 2001
From: Overhatted <15021741+Overhatted at users.noreply.github.com>
Date: Sun, 22 Oct 2023 18:59:46 +0100
Subject: [PATCH] Added removal of file extension when guessing the toolchain

---
 clang/lib/Driver/ToolChain.cpp           |  7 ++++---
 clang/unittests/Driver/ToolChainTest.cpp | 12 ++++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index ab19166f18c2dcf..c8bb6ffe0a9bc7d 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -345,10 +345,11 @@ static const DriverSuffix *parseDriverSuffix(StringRef ProgName, size_t &Pos) {
   // added via -target as implicit first argument.
   const DriverSuffix *DS = FindDriverSuffix(ProgName, Pos);
 
-  if (!DS && ProgName.endswith(".exe")) {
-    // Try again after stripping the executable suffix:
+  if (!DS) {
+    // Try again after stripping the file extension suffix:
     // clang++.exe -> clang++
-    ProgName = ProgName.drop_back(StringRef(".exe").size());
+    // cl.bat -> cl
+    ProgName = ProgName.slice(0, ProgName.find('.'));
     DS = FindDriverSuffix(ProgName, Pos);
   }
 
diff --git a/clang/unittests/Driver/ToolChainTest.cpp b/clang/unittests/Driver/ToolChainTest.cpp
index acbbb87390d5e9a..f193706adf2cd2d 100644
--- a/clang/unittests/Driver/ToolChainTest.cpp
+++ b/clang/unittests/Driver/ToolChainTest.cpp
@@ -509,6 +509,18 @@ TEST(ToolChainTest, GetTargetAndMode) {
   EXPECT_TRUE(Res.ModeSuffix == "clang-dxc");
   EXPECT_STREQ(Res.DriverMode, "--driver-mode=dxc");
   EXPECT_FALSE(Res.TargetIsValid);
+
+  Res = ToolChain::getTargetAndModeFromProgramName("cl.exe");
+  EXPECT_TRUE(Res.TargetPrefix.empty());
+  EXPECT_TRUE(Res.ModeSuffix == "cl");
+  EXPECT_STREQ(Res.DriverMode, "--driver-mode=cl");
+  EXPECT_FALSE(Res.TargetIsValid);
+
+  Res = ToolChain::getTargetAndModeFromProgramName("cl.bat");
+  EXPECT_TRUE(Res.TargetPrefix.empty());
+  EXPECT_TRUE(Res.ModeSuffix == "cl");
+  EXPECT_STREQ(Res.DriverMode, "--driver-mode=cl");
+  EXPECT_FALSE(Res.TargetIsValid);
 }
 
 TEST(ToolChainTest, CommandOutput) {



More information about the cfe-commits mailing list