Author: kadircet Date: Fri Mar 8 00:38:25 2019 New Revision: 355669 URL: http://llvm.org/viewvc/llvm-project?rev=355669&view=rev Log: [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 Modified: clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp clang-tools-extra/trunk/unittests/clangd/GlobalCompilationDatabaseTests.cpp Modified: clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp?rev=355669&r1=355668&r2=355669&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp (original) +++ clang-tools-extra/trunk/clangd/GlobalCompilationDatabase.cpp Fri Mar 8 00:38:25 2019 @@ -21,11 +21,17 @@ namespace { void adjustArguments(tooling::CompileCommand &Cmd, llvm::StringRef ResourceDir) { - // Strip plugin related command line arguments. Clangd does - // not support plugins currently. Therefore it breaks if - // compiler tries to load plugins. - Cmd.CommandLine = - tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename); + tooling::ArgumentsAdjuster ArgsAdjuster = tooling::combineAdjusters( + // clangd should not write files to disk, including dependency files + // requested on the command line. + tooling::getClangStripDependencyFileAdjuster(), + // 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. // FIXME: Don't overwrite it if it's already there. if (!ResourceDir.empty()) Modified: clang-tools-extra/trunk/unittests/clangd/GlobalCompilationDatabaseTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/GlobalCompilationDatabaseTests.cpp?rev=355669&r1=355668&r2=355669&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/GlobalCompilationDatabaseTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/GlobalCompilationDatabaseTests.cpp Fri Mar 8 00:38:25 2019 @@ -16,15 +16,18 @@ namespace clang { namespace clangd { namespace { +using ::testing::AllOf; +using ::testing::Contains; using ::testing::ElementsAre; using ::testing::EndsWith; +using ::testing::Not; TEST(GlobalCompilationDatabaseTest, FallbackCommand) { DirectoryBasedGlobalCompilationDatabase DB(None); auto Cmd = DB.getFallbackCommand(testPath("foo/bar.cc")); EXPECT_EQ(Cmd.Directory, testPath("foo")); - EXPECT_THAT(Cmd.CommandLine, ElementsAre( - EndsWith("clang"), testPath("foo/bar.cc"))); + EXPECT_THAT(Cmd.CommandLine, + ElementsAre(EndsWith("clang"), testPath("foo/bar.cc"))); EXPECT_EQ(Cmd.Output, ""); // .h files have unknown language, so they are parsed liberally as obj-c++. @@ -65,16 +68,18 @@ protected: TEST_F(OverlayCDBTest, GetCompileCommand) { OverlayCDB CDB(Base.get(), {}, std::string("")); - EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")), - Base->getCompileCommand(testPath("foo.cc"))); + EXPECT_THAT(CDB.getCompileCommand(testPath("foo.cc"))->CommandLine, + AllOf(Contains(testPath("foo.cc")), Contains("-DA=1"))); EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None); auto Override = cmd(testPath("foo.cc"), "-DA=3"); 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); 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) { @@ -88,7 +93,8 @@ TEST_F(OverlayCDBTest, NoBase) { EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None); auto Override = cmd(testPath("bar.cc"), "-DA=5"); 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, ElementsAre(EndsWith("clang"), testPath("foo.cc"), "-DA=6")); @@ -111,6 +117,35 @@ TEST_F(OverlayCDBTest, Watch) { 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 clangd } // namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits