[clang-tools-extra] 04e21fb - Revert "[clangd] Adjust compile flags to contain only the requested file as input"
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 23 08:59:46 PDT 2021
Author: Kadir Cetinkaya
Date: 2021-07-23T17:58:11+02:00
New Revision: 04e21fbc44c145d5599ef8db9aaf66b159107f33
URL: https://github.com/llvm/llvm-project/commit/04e21fbc44c145d5599ef8db9aaf66b159107f33
DIFF: https://github.com/llvm/llvm-project/commit/04e21fbc44c145d5599ef8db9aaf66b159107f33.diff
LOG: Revert "[clangd] Adjust compile flags to contain only the requested file as input"
This reverts commit ba5dd945ad9124f24452987be64040a6ea6cd25e.
Added:
Modified:
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
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CompileCommands.cpp b/clang-tools-extra/clangd/CompileCommands.cpp
index 4c1646ed9e05c..ba21e81d3fb52 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -13,7 +13,6 @@
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
-#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
@@ -194,8 +193,7 @@ CommandMangler CommandMangler::detect() {
CommandMangler CommandMangler::forTests() { return CommandMangler(); }
-void CommandMangler::adjust(std::vector<std::string> &Cmd,
- llvm::StringRef File) const {
+void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
trace::Span S("AdjustCompileFlags");
auto &OptTable = clang::driver::getDriverOptTable();
// OriginalArgs needs to outlive ArgList.
@@ -206,27 +204,18 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd,
// ParseArgs propagates missig arg/opt counts on error, but preserves
// everything it could parse in ArgList. So we just ignore those counts.
unsigned IgnoredCount;
- // Drop the executable name, as ParseArgs doesn't expect it. This means
- // indices are actually of by one between ArgList and OriginalArgs.
- auto ArgList =
- OptTable.ParseArgs(llvm::makeArrayRef(OriginalArgs).drop_front(),
- IgnoredCount, IgnoredCount);
+ auto ArgList = OptTable.ParseArgs(OriginalArgs, IgnoredCount, IgnoredCount);
// 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.
- llvm::SmallVector<unsigned, 1> IndicesToDrop;
- for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT))
- IndicesToDrop.push_back(Input->getIndex());
- 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); });
- Cmd.push_back("--");
- Cmd.push_back(File.str());
+ // 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());
+ }
}
for (auto &Edit : Config::current().CompileFlags.Edits)
@@ -276,7 +265,7 @@ CommandMangler::operator clang::tooling::ArgumentsAdjuster() && {
return [Mangler = std::make_shared<CommandMangler>(std::move(*this))](
const std::vector<std::string> &Args, llvm::StringRef File) {
auto Result = Args;
- Mangler->adjust(Result, File);
+ Mangler->adjust(Result);
return Result;
};
}
diff --git a/clang-tools-extra/clangd/CompileCommands.h b/clang-tools-extra/clangd/CompileCommands.h
index 50c1a3571ec42..759472413fdaf 100644
--- a/clang-tools-extra/clangd/CompileCommands.h
+++ b/clang-tools-extra/clangd/CompileCommands.h
@@ -12,7 +12,6 @@
#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>
@@ -47,7 +46,7 @@ struct CommandMangler {
// - on mac, find clang and isysroot by querying the `xcrun` launcher
static CommandMangler detect();
- void adjust(std::vector<std::string> &Cmd, llvm::StringRef File) const;
+ void adjust(std::vector<std::string> &Cmd) const;
explicit operator clang::tooling::ArgumentsAdjuster() &&;
private:
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index 1c66062fb9b3f..73f19abaeb9db 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -766,7 +766,7 @@ OverlayCDB::getCompileCommand(PathRef File) const {
if (!Cmd)
return llvm::None;
if (ArgsAdjuster)
- Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, File);
+ Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, Cmd->Filename);
return Cmd;
}
@@ -776,7 +776,7 @@ tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
FallbackFlags.end());
if (ArgsAdjuster)
- Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, File);
+ Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
return Cmd;
}
diff --git a/clang-tools-extra/clangd/test/did-change-configuration-params.test b/clang-tools-extra/clangd/test/did-change-configuration-params.test
index 29cf1786a835a..e90e26c0d3a7a 100644
--- a/clang-tools-extra/clangd/test/did-change-configuration-params.test
+++ b/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"}
---
diff --git a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
index 458549e9b86a2..11f0b409dfeca 100644
--- a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
@@ -43,7 +43,7 @@ TEST(CommandMangler, Everything) {
Mangler.ResourceDir = testPath("fake/resources");
Mangler.Sysroot = testPath("fake/sysroot");
std::vector<std::string> Cmd = {"clang++", "--", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"),
"-resource-dir=" + testPath("fake/resources"),
"-isysroot", testPath("fake/sysroot"), "--",
@@ -54,7 +54,7 @@ TEST(CommandMangler, ResourceDir) {
auto Mangler = CommandMangler::forTests();
Mangler.ResourceDir = testPath("fake/resources");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
}
@@ -63,7 +63,7 @@ TEST(CommandMangler, Sysroot) {
Mangler.Sysroot = testPath("fake/sysroot");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_THAT(llvm::join(Cmd, " "),
HasSubstr("-isysroot " + testPath("fake/sysroot")));
}
@@ -73,19 +73,19 @@ TEST(CommandMangler, ClangPath) {
Mangler.ClangPath = testPath("fake/clang");
std::vector<std::string> Cmd = {"clang++", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
Cmd = {"unknown-binary", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_EQ(testPath("fake/unknown-binary"), Cmd.front());
Cmd = {testPath("path/clang++"), "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_EQ(testPath("path/clang++"), Cmd.front());
Cmd = {"foo/unknown-binary", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_EQ("foo/unknown-binary", Cmd.front());
}
@@ -128,7 +128,7 @@ TEST(CommandMangler, ClangPathResolve) {
auto Mangler = CommandMangler::forTests();
Mangler.ClangPath = testPath("fake/clang");
std::vector<std::string> Cmd = {(TempDir + "/bin/foo").str(), "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
// Directory based on resolved symlink, basename preserved.
EXPECT_EQ((TempDir + "/lib/foo").str(), Cmd.front());
@@ -145,13 +145,13 @@ TEST(CommandMangler, ClangPathResolve) {
Mangler.ClangPath = testPath("fake/clang");
// Driver found on PATH.
Cmd = {"foo", "foo.cc"};
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
// 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, "foo.cc");
+ Mangler.adjust(Cmd);
EXPECT_EQ((TempDir + "/bin/foo").str(), Cmd.front());
}
#endif
@@ -170,7 +170,7 @@ TEST(CommandMangler, ConfigEdits) {
Argv = tooling::getInsertArgumentAdjuster("--hello")(Argv, "");
});
WithContextValue WithConfig(Config::Key, std::move(Cfg));
- Mangler.adjust(Cmd, "foo.cc");
+ Mangler.adjust(Cmd);
}
// Edits are applied in given order and before other mangling and they always
// go before filename.
diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp
index a848a50730caa..052603ce6851a 100644
--- a/clang-tools-extra/clangd/unittests/TestTU.cpp
+++ b/clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -59,7 +59,7 @@ ParseInputs TestTU::inputs(MockFS &FS) const {
Argv.push_back(FullFilename);
auto Mangler = CommandMangler::forTests();
- Mangler.adjust(Inputs.CompileCommand.CommandLine, FullFilename);
+ Mangler.adjust(Inputs.CompileCommand.CommandLine);
Inputs.CompileCommand.Filename = FullFilename;
Inputs.CompileCommand.Directory = testRoot();
Inputs.Contents = Code;
More information about the cfe-commits
mailing list