Index: clangd/GlobalCompilationDatabase.h =================================================================== --- clangd/GlobalCompilationDatabase.h +++ clangd/GlobalCompilationDatabase.h @@ -11,6 +11,8 @@ #include "Function.h" #include "Path.h" +#include "clang/Tooling/ArgumentsAdjusters.h" +#include "clang/Tooling/CompilationDatabase.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringMap.h" #include @@ -110,6 +112,9 @@ setCompileCommand(PathRef File, llvm::Optional CompilationCommand); + /// Adjusts given compile command for clangd. + tooling::CompileCommand adjustArguments(tooling::CompileCommand Cmd) const; + private: mutable std::mutex Mutex; llvm::StringMap Commands; /* GUARDED_BY(Mut) */ Index: clangd/GlobalCompilationDatabase.cpp =================================================================== --- clangd/GlobalCompilationDatabase.cpp +++ clangd/GlobalCompilationDatabase.cpp @@ -19,25 +19,6 @@ namespace clangd { namespace { -void adjustArguments(tooling::CompileCommand &Cmd, - llvm::StringRef ResourceDir) { - // Clangd does not generate dependency file. - Cmd.CommandLine = tooling::getClangStripDependencyFileAdjuster()( - 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. - Cmd.CommandLine = - tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename); - - Cmd.CommandLine = - tooling::getClangSyntaxOnlyAdjuster()(Cmd.CommandLine, Cmd.Filename); - // Inject the resource dir. - // FIXME: Don't overwrite it if it's already there. - if (!ResourceDir.empty()) - Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str()); -} - std::string getStandardResourceDir() { static int Dummy; // Just an address in this process. return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); @@ -162,8 +143,7 @@ Cmd = Base->getCompileCommand(File, Project); if (!Cmd) return llvm::None; - adjustArguments(*Cmd, ResourceDir); - return Cmd; + return adjustArguments(*Cmd); } tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { @@ -187,5 +167,25 @@ OnCommandChanged.broadcast({File}); } +tooling::CompileCommand +OverlayCDB::adjustArguments(tooling::CompileCommand Cmd) const { + // Clangd does not generate dependency file. + Cmd.CommandLine = tooling::getClangStripDependencyFileAdjuster()( + 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. + Cmd.CommandLine = + tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename); + + Cmd.CommandLine = + tooling::getClangSyntaxOnlyAdjuster()(Cmd.CommandLine, Cmd.Filename); + // Inject the resource dir. + // FIXME: Don't overwrite it if it's already there. + if (!ResourceDir.empty()) + Cmd.CommandLine.push_back("-resource-dir=" + ResourceDir); + return std::move(Cmd); +} + } // namespace clangd } // namespace clang Index: unittests/clangd/GlobalCompilationDatabaseTests.cpp =================================================================== --- unittests/clangd/GlobalCompilationDatabaseTests.cpp +++ unittests/clangd/GlobalCompilationDatabaseTests.cpp @@ -23,8 +23,8 @@ 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++. @@ -66,15 +66,17 @@ TEST_F(OverlayCDBTest, GetCompileCommand) { OverlayCDB CDB(Base.get(), {}, std::string("")); EXPECT_EQ(CDB.getCompileCommand(testPath("foo.cc")), - Base->getCompileCommand(testPath("foo.cc"))); + CDB.adjustArguments(*Base->getCompileCommand(testPath("foo.cc")))); 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_EQ(CDB.getCompileCommand(testPath("foo.cc")), + CDB.adjustArguments(Override)); EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), llvm::None); CDB.setCompileCommand(testPath("missing.cc"), Override); - EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), Override); + EXPECT_EQ(CDB.getCompileCommand(testPath("missing.cc")), + CDB.adjustArguments(Override)); } TEST_F(OverlayCDBTest, GetFallbackCommand) { @@ -88,6 +90,7 @@ EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), None); auto Override = cmd(testPath("bar.cc"), "-DA=5"); CDB.setCompileCommand(testPath("bar.cc"), Override); + Override = CDB.adjustArguments(Override); EXPECT_EQ(CDB.getCompileCommand(testPath("bar.cc")), Override); EXPECT_THAT(CDB.getFallbackCommand(testPath("foo.cc")).CommandLine,