diff --git a/clang-tools-extra/clangd/CompileCommands.cpp b/clang-tools-extra/clangd/CompileCommands.cpp --- a/clang-tools-extra/clangd/CompileCommands.cpp +++ b/clang-tools-extra/clangd/CompileCommands.cpp @@ -20,6 +20,8 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" +#include +#include namespace clang { namespace clangd { @@ -209,14 +211,20 @@ Cmd = tooling::getStripPluginsAdjuster()(Cmd, ""); Cmd = tooling::getClangSyntaxOnlyAdjuster()(Cmd, ""); + std::vector ToAppend; if (ResourceDir && !Has("-resource-dir")) - Cmd.push_back(("-resource-dir=" + *ResourceDir)); + ToAppend.push_back(("-resource-dir=" + *ResourceDir)); // Don't set `-isysroot` if it is already set or if `--sysroot` is set. // `--sysroot` is a superset of the `-isysroot` argument. if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) { - Cmd.push_back("-isysroot"); - Cmd.push_back(*Sysroot); + ToAppend.push_back("-isysroot"); + ToAppend.push_back(*Sysroot); + } + + if (!ToAppend.empty()) { + Cmd = tooling::getInsertArgumentAdjuster( + std::move(ToAppend), tooling::ArgumentInsertPosition::END)(Cmd, ""); } if (!Cmd.empty()) { @@ -504,7 +512,6 @@ Args.resize(Write); } - std::string printArgv(llvm::ArrayRef Args) { std::string Buf; llvm::raw_string_ostream OS(Buf); diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp --- a/clang-tools-extra/clangd/ConfigCompile.cpp +++ b/clang-tools-extra/clangd/ConfigCompile.cpp @@ -47,6 +47,7 @@ #include "llvm/Support/Regex.h" #include "llvm/Support/SMLoc.h" #include "llvm/Support/SourceMgr.h" +#include #include namespace clang { @@ -270,7 +271,9 @@ Add.push_back(std::move(*A)); Out.Apply.push_back([Add(std::move(Add))](const Params &, Config &C) { C.CompileFlags.Edits.push_back([Add](std::vector &Args) { - Args.insert(Args.end(), Add.begin(), Add.end()); + // The point to insert at. Just append when `--` isn't present. + auto It = llvm::find(Args, "--"); + Args.insert(It, Add.begin(), Add.end()); }); }); } diff --git a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp --- a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp +++ b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp @@ -41,13 +41,14 @@ Mangler.ClangPath = testPath("fake/clang"); Mangler.ResourceDir = testPath("fake/resources"); Mangler.Sysroot = testPath("fake/sysroot"); - std::vector Cmd = {"clang++", "-Xclang", "-load", "-Xclang", - "plugin", "-MF", "dep", "foo.cc"}; + std::vector Cmd = {"clang++", "-Xclang", "-load", + "-Xclang", "plugin", "-MF", + "dep", "--", "foo.cc"}; Mangler.adjust(Cmd); - EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc", - "-fsyntax-only", + EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "-fsyntax-only", "-resource-dir=" + testPath("fake/resources"), - "-isysroot", testPath("fake/sysroot"))); + "-isysroot", testPath("fake/sysroot"), "--", + "foo.cc")); } TEST(CommandMangler, ResourceDir) { @@ -378,4 +379,3 @@ } // namespace } // namespace clangd } // namespace clang - diff --git a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp --- a/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp +++ b/clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp @@ -123,12 +123,12 @@ TEST_F(ConfigCompileTests, CompileCommands) { Frag.CompileFlags.Add.emplace_back("-foo"); Frag.CompileFlags.Remove.emplace_back("--include-directory="); - std::vector Argv = {"clang", "-I", "bar/", "a.cc"}; + std::vector Argv = {"clang", "-I", "bar/", "--", "a.cc"}; EXPECT_TRUE(compileAndApply()); EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2)); for (auto &Edit : Conf.CompileFlags.Edits) Edit(Argv); - EXPECT_THAT(Argv, ElementsAre("clang", "a.cc", "-foo")); + EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc")); } TEST_F(ConfigCompileTests, CompilationDatabase) {