[clang-tools-extra] 79c2616 - [clangd] Canonicalize inputs provided with `--`

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 6 06:10:12 PDT 2021


Author: Kadir Cetinkaya
Date: 2021-08-06T15:04:04+02:00
New Revision: 79c2616d315f54d72bcdeebb6576c84be7b585d8

URL: https://github.com/llvm/llvm-project/commit/79c2616d315f54d72bcdeebb6576c84be7b585d8
DIFF: https://github.com/llvm/llvm-project/commit/79c2616d315f54d72bcdeebb6576c84be7b585d8.diff

LOG: [clangd] Canonicalize inputs provided with `--`

We already strip all the inputs provided without `--`, this patch also
handles the cases with `--`.

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/CompileCommands.cpp
    clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/CompileCommands.cpp b/clang-tools-extra/clangd/CompileCommands.cpp
index 76c92a0bac73..57933169f911 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -237,21 +237,27 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd,
   // If there is a single `-arch` option, keep it.
   if (ArchOptCount < 2)
     IndicesToDrop.clear();
-  // Move the inputs to the end, separated via `--` from flags. This ensures
-  // modifications done in the following steps apply in more cases (like setting
-  // -x, which only affects inputs that come after it).
-  if (!ArgList.hasArgNoClaim(driver::options::OPT__DASH_DASH)) {
-    // Drop all the inputs and only add one for the current file.
-    for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT))
-      IndicesToDrop.push_back(Input->getIndex());
-    Cmd.push_back("--");
-    Cmd.push_back(File.str());
+
+  // Strip all the inputs and `--`. We'll put the input for the requested file
+  // explicitly at the end of the flags. This ensures modifications done in the
+  // following steps apply in more cases (like setting -x, which only affects
+  // inputs that come after it).
+  for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT))
+    IndicesToDrop.push_back(Input->getIndex());
+  // Anything after `--` is also treated as input, drop them as well.
+  if (auto *DashDash =
+          ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) {
+    Cmd.resize(DashDash->getIndex() + 1); // +1 to account for Cmd[0].
   }
   llvm::sort(IndicesToDrop);
   llvm::for_each(llvm::reverse(IndicesToDrop),
                  // +1 to account for the executable name in Cmd[0] that
                  // doesn't exist in ArgList.
                  [&Cmd](unsigned Idx) { Cmd.erase(Cmd.begin() + Idx + 1); });
+  // All the inputs are stripped, append the name for the requested file. Rest
+  // of the modifications should respect `--`.
+  Cmd.push_back("--");
+  Cmd.push_back(File.str());
 
   for (auto &Edit : Config::current().CompileFlags.Edits)
     Edit(Cmd);

diff  --git a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
index d1f97233fa67..c8749ec7f487 100644
--- a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
@@ -45,7 +45,7 @@ TEST(CommandMangler, Everything) {
   Mangler.ClangPath = testPath("fake/clang");
   Mangler.ResourceDir = testPath("fake/resources");
   Mangler.Sysroot = testPath("fake/sysroot");
-  std::vector<std::string> Cmd = {"clang++", "--", "foo.cc"};
+  std::vector<std::string> Cmd = {"clang++", "--", "foo.cc", "bar.cc"};
   Mangler.adjust(Cmd, "foo.cc");
   EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"),
                                "-resource-dir=" + testPath("fake/resources"),


        


More information about the cfe-commits mailing list