[clangd] Adjust compile commands to be applicable for tooling

Summary:
As can be seen in https://github.com/llvm-mirror/clang/blob/master/lib/Tooling/Tooling.cpp#L385
clang tool invocations adjust commands normally like this. In clangd we have
different code paths for invoking a frontend action(preamble builds, ast builds,
background index, clangd-indexer) they all work on the same GlobalCompilationDatabase
abstraction, but later on are subject to different modifications.

This patch makes sure all of the clangd actions make use of the same compile
commands before invocation.

Enables background-index to work on chromium codebase(since they had dependency
file output in their compile commands).

Reviewers: gribozavr, hokein

Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, jdoerfert, cfe-commits

Tags: #clang

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

llvm-svn: 355669
This commit is contained in:
Kadir Cetinkaya 2019-03-08 08:38:25 +00:00
parent e7eb27a9a0
commit 65944ab50d
2 changed files with 53 additions and 12 deletions

View File

@ -21,11 +21,17 @@ namespace {
void adjustArguments(tooling::CompileCommand &Cmd, void adjustArguments(tooling::CompileCommand &Cmd,
llvm::StringRef ResourceDir) { llvm::StringRef ResourceDir) {
// Strip plugin related command line arguments. Clangd does tooling::ArgumentsAdjuster ArgsAdjuster = tooling::combineAdjusters(
// not support plugins currently. Therefore it breaks if // clangd should not write files to disk, including dependency files
// compiler tries to load plugins. // requested on the command line.
Cmd.CommandLine = tooling::getClangStripDependencyFileAdjuster(),
tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename); // Strip plugin related command line arguments. Clangd does
// not support plugins currently. Therefore it breaks if
// compiler tries to load plugins.
tooling::combineAdjusters(tooling::getStripPluginsAdjuster(),
tooling::getClangSyntaxOnlyAdjuster()));
Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
// Inject the resource dir. // Inject the resource dir.
// FIXME: Don't overwrite it if it's already there. // FIXME: Don't overwrite it if it's already there.
if (!ResourceDir.empty()) if (!ResourceDir.empty())

View File

@ -16,15 +16,18 @@
namespace clang { namespace clang {
namespace clangd { namespace clangd {
namespace { namespace {
using ::testing::AllOf;
using ::testing::Contains;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::EndsWith; using ::testing::EndsWith;
using ::testing::Not;
TEST(GlobalCompilationDatabaseTest, FallbackCommand) { TEST(GlobalCompilationDatabaseTest, FallbackCommand) {
DirectoryBasedGlobalCompilationDatabase DB(None); DirectoryBasedGlobalCompilationDatabase DB(None);
auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc")); auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc"));
EXPECT_EQ(Cmd.Directory, testPath("foo")); EXPECT_EQ(Cmd.Directory, testPath("foo"));
EXPECT_THAT(Cmd.CommandLine, ElementsAre( EXPECT_THAT(Cmd.CommandLine,
EndsWith("clang"), testPath("foo/bar.cc"))); ElementsAre(EndsWith("clang"), testPath("foo/bar.cc")));
EXPECT_EQ(Cmd.Output, ""); EXPECT_EQ(Cmd.Output, "");
// .h files have unknown language, so they are parsed liberally as obj-c++. // .h files have unknown language, so they are parsed liberally as obj-c++.
@ -65,16 +68,18 @@ protected:
TEST_F(OverlayCDBTest, GetCompileCommand) { TEST_F(OverlayCDBTest, GetCompileCommand) {
OverlayCDB CDB(Base.get(), {}, std::string("")); OverlayCDB CDB(Base.get(), {}, std::string(""));
EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")), EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
Base->getCompileCommand(testPath("foo.cc"))); AllOf(Contains(testPath("foo.cc")), Contains("-DA=1")));
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None); EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
auto Override = cmd(testPath("foo.cc"), "-DA=3"); auto Override = cmd(testPath("foo.cc"), "-DA=3");
CDB.setCompileCommand(testPath("foo.cc"), Override); CDB.setCompileCommand(testPath("foo.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")), Override); EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
Contains("-DA=3"));
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None); EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None);
CDB.setCompileCommand(testPath("missing.cc"), Override); CDB.setCompileCommand(testPath("missing.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), Override); EXPECT_THAT(CDB.getCompileCommand(testPath("missing.cc"))->CommandLine,
Contains("-DA=3"));
} }
TEST_F(OverlayCDBTest, GetFallbackCommand) { TEST_F(OverlayCDBTest, GetFallbackCommand) {
@ -88,7 +93,8 @@ TEST_F(OverlayCDBTest, NoBase) {
EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None); EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None);
auto Override = cmd(testPath("bar.cc"), "-DA=5"); auto Override = cmd(testPath("bar.cc"), "-DA=5");
CDB.setCompileCommand(testPath("bar.cc"), Override); CDB.setCompileCommand(testPath("bar.cc"), Override);
EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), Override); EXPECT_THAT(CDB.getCompileCommand(testPath("bar.cc"))->CommandLine,
Contains("-DA=5"));
EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine, EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,
ElementsAre(EndsWith("clang"), testPath("foo.cc"), "-DA=6")); ElementsAre(EndsWith("clang"), testPath("foo.cc"), "-DA=6"));
@ -111,6 +117,35 @@ TEST_F(OverlayCDBTest, Watch) {
ElementsAre("A.cpp"), ElementsAre("C.cpp"))); ElementsAre("A.cpp"), ElementsAre("C.cpp")));
} }
TEST_F(OverlayCDBTest, Adjustments) {
OverlayCDB CDB(Base.get(), {}, std::string(""));
auto Cmd = CDB.getCompileCommand(testPath("foo.cc")).getValue();
// Delete the file name.
Cmd.CommandLine.pop_back();
// Check dependency file commands are dropped.
Cmd.CommandLine.push_back("-MF");
Cmd.CommandLine.push_back("random-dependency");
// Check plugin-related commands are dropped.
Cmd.CommandLine.push_back("-Xclang");
Cmd.CommandLine.push_back("-load");
Cmd.CommandLine.push_back("-Xclang");
Cmd.CommandLine.push_back("random-plugin");
Cmd.CommandLine.push_back("-DA=5");
Cmd.CommandLine.push_back(Cmd.Filename);
CDB.setCompileCommand(testPath("foo.cc"), Cmd);
EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine,
AllOf(Contains("-fsyntax-only"), Contains("-DA=5"),
Contains(testPath("foo.cc")), Not(Contains("-MF")),
Not(Contains("random-dependency")),
Not(Contains("-Xclang")), Not(Contains("-load")),
Not(Contains("random-plugin"))));
}
} // namespace } // namespace
} // namespace clangd } // namespace clangd
} // namespace clang } // namespace clang