kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: usaxena95, arphaman, mgrang.
kadircet requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Depends on D106527 <https://reviews.llvm.org/D106527>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106639

Files:
  clang-tools-extra/clangd/CompileCommands.cpp
  clang-tools-extra/clangd/CompileCommands.h
  clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
  clang-tools-extra/clangd/test/did-change-configuration-params.test
  clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp

Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -59,7 +59,7 @@
   Argv.push_back(FullFilename);
 
   auto Mangler = CommandMangler::forTests();
-  Mangler.adjust(Inputs.CompileCommand.CommandLine);
+  Mangler.adjust(Inputs.CompileCommand.CommandLine, FullFilename);
   Inputs.CompileCommand.Filename = FullFilename;
   Inputs.CompileCommand.Directory = testRoot();
   Inputs.Contents = Code;
Index: clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
+++ clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
@@ -43,7 +43,7 @@
   Mangler.ResourceDir = testPath("fake/resources");
   Mangler.Sysroot = testPath("fake/sysroot");
   std::vector<std::string> Cmd = {"clang++", "--", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"),
                                "-resource-dir=" + testPath("fake/resources"),
                                "-isysroot", testPath("fake/sysroot"), "--",
@@ -54,7 +54,7 @@
   auto Mangler = CommandMangler::forTests();
   Mangler.ResourceDir = testPath("fake/resources");
   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
 }
 
@@ -63,7 +63,7 @@
   Mangler.Sysroot = testPath("fake/sysroot");
 
   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_THAT(llvm::join(Cmd, " "),
               HasSubstr("-isysroot " + testPath("fake/sysroot")));
 }
@@ -73,19 +73,19 @@
   Mangler.ClangPath = testPath("fake/clang");
 
   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
 
   Cmd = {"unknown-binary", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_EQ(testPath("fake/unknown-binary"), Cmd.front());
 
   Cmd = {testPath("path/clang++"), "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_EQ(testPath("path/clang++"), Cmd.front());
 
   Cmd = {"foo/unknown-binary", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_EQ("foo/unknown-binary", Cmd.front());
 }
 
@@ -128,7 +128,7 @@
   auto Mangler = CommandMangler::forTests();
   Mangler.ClangPath = testPath("fake/clang");
   std::vector<std::string> Cmd = {(TempDir + "/bin/foo").str(), "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   // Directory based on resolved symlink, basename preserved.
   EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
 
@@ -145,13 +145,13 @@
   Mangler.ClangPath = testPath("fake/clang");
   // Driver found on PATH.
   Cmd = {"foo", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   // Found the symlink and resolved the path as above.
   EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
 
   // Symlink not resolved with -no-canonical-prefixes.
   Cmd = {"foo", "-no-canonical-prefixes", "foo.cc"};
-  Mangler.adjust(Cmd);
+  Mangler.adjust(Cmd, "foo.cc");
   EXPECT_EQ((TempDir + "/bin/foo").str(), Cmd.front());
 }
 #endif
@@ -170,7 +170,7 @@
       Argv = tooling::getInsertArgumentAdjuster("--hello")(Argv, "");
     });
     WithContextValue WithConfig(Config::Key, std::move(Cfg));
-    Mangler.adjust(Cmd);
+    Mangler.adjust(Cmd, "foo.cc");
   }
   // Edits are applied in given order and before other mangling and they always
   // go before filename.
Index: clang-tools-extra/clangd/test/did-change-configuration-params.test
===================================================================
--- clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ clang-tools-extra/clangd/test/did-change-configuration-params.test
@@ -48,7 +48,7 @@
 #
 # ERR: ASTWorker building file {{.*}}foo.c version 0 with command
 # ERR: [{{.*}}clangd-test2]
-# ERR: clang -c -Wall -Werror {{.*}} -- foo.c
+# ERR: clang -c -Wall -Werror {{.*}} -- {{.*}}foo.c
 ---
 {"jsonrpc":"2.0","id":5,"method":"shutdown"}
 ---
Index: clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
===================================================================
--- clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -764,7 +764,7 @@
   if (!Cmd)
     return llvm::None;
   if (ArgsAdjuster)
-    Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, Cmd->Filename);
+    Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, File);
   return Cmd;
 }
 
@@ -774,7 +774,7 @@
   Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
                          FallbackFlags.end());
   if (ArgsAdjuster)
-    Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
+    Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, File);
   return Cmd;
 }
 
Index: clang-tools-extra/clangd/CompileCommands.h
===================================================================
--- clang-tools-extra/clangd/CompileCommands.h
+++ clang-tools-extra/clangd/CompileCommands.h
@@ -12,6 +12,7 @@
 #include "clang/Tooling/ArgumentsAdjusters.h"
 #include "clang/Tooling/CompilationDatabase.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
 #include <deque>
 #include <string>
 #include <vector>
@@ -42,7 +43,7 @@
   //  - on mac, find clang and isysroot by querying the `xcrun` launcher
   static CommandMangler detect();
 
-  void adjust(std::vector<std::string> &Cmd) const;
+  void adjust(std::vector<std::string> &Cmd, llvm::StringRef File) const;
   explicit operator clang::tooling::ArgumentsAdjuster() &&;
 
 private:
Index: clang-tools-extra/clangd/CompileCommands.cpp
===================================================================
--- clang-tools-extra/clangd/CompileCommands.cpp
+++ clang-tools-extra/clangd/CompileCommands.cpp
@@ -192,7 +192,8 @@
 
 CommandMangler CommandMangler::forTests() { return CommandMangler(); }
 
-void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
+void CommandMangler::adjust(std::vector<std::string> &Cmd,
+                            llvm::StringRef File) const {
   trace::Span S("AdjustCompileFlags");
   auto &OptTable = clang::driver::getDriverOptTable();
   // OriginalArgs needs to outlive ArgList.
@@ -209,12 +210,20 @@
   // 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)) {
-    // In theory there might be more than one input, but clangd can't deal with
-    // them anyway.
-    if (auto *Input = ArgList.getLastArg(driver::options::OPT_INPUT)) {
-      Cmd.insert(Cmd.end(), {"--", Input->getAsString(ArgList)});
-      Cmd.erase(Cmd.begin() + Input->getIndex());
+    // Drop all the inputs and only add one for the current file.
+    llvm::SmallVector<unsigned, 1> IndicesToDrop;
+    for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT)) {
+      // Driver name is also considered an input, don't strip that away.
+      if (Input->getIndex() == 0)
+        continue;
+      IndicesToDrop.push_back(Input->getIndex());
+      llvm::errs() << "Dropping: " << Input->getAsString(ArgList) << '-'
+                   << Input->getValue(0) << '\n';
     }
+    llvm::sort(IndicesToDrop);
+    llvm::for_each(llvm::reverse(IndicesToDrop),
+                   [&Cmd](unsigned Idx) { Cmd.erase(Cmd.begin() + Idx); });
+    Cmd.insert(Cmd.end(), {"--", File.str()});
   }
 
   for (auto &Edit : Config::current().CompileFlags.Edits)
@@ -269,7 +278,7 @@
   return [Mangler = std::make_shared<CommandMangler>(std::move(*this))](
              const std::vector<std::string> &Args, llvm::StringRef File) {
     auto Result = Args;
-    Mangler->adjust(Result);
+    Mangler->adjust(Result, File);
     return Result;
   };
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to