[PATCH] D116196: [clangd] Add CompileFlags.Compiler option to override argv0

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 4 02:45:09 PST 2022


This revision was automatically updated to reflect the committed changes.
sammccall marked 2 inline comments as done.
Closed by commit rGf2b3e25f860e: [clangd] Add CompileFlags.Compiler option to override argv0 (authored by sammccall).

Changed prior to commit:
  https://reviews.llvm.org/D116196?vs=395945&id=397240#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116196/new/

https://reviews.llvm.org/D116196

Files:
  clang-tools-extra/clangd/ConfigCompile.cpp
  clang-tools-extra/clangd/ConfigFragment.h
  clang-tools-extra/clangd/ConfigYAML.cpp
  clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp


Index: clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
+++ clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
@@ -121,14 +121,15 @@
 }
 
 TEST_F(ConfigCompileTests, CompileCommands) {
+  Frag.CompileFlags.Compiler.emplace("tpc.exe");
   Frag.CompileFlags.Add.emplace_back("-foo");
   Frag.CompileFlags.Remove.emplace_back("--include-directory=");
   std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"};
   EXPECT_TRUE(compileAndApply());
-  EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
+  EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(3));
   for (auto &Edit : Conf.CompileFlags.Edits)
     Edit(Argv);
-  EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc"));
+  EXPECT_THAT(Argv, ElementsAre("tpc.exe", "-foo", "--", "a.cc"));
 }
 
 TEST_F(ConfigCompileTests, CompilationDatabase) {
Index: clang-tools-extra/clangd/ConfigYAML.cpp
===================================================================
--- clang-tools-extra/clangd/ConfigYAML.cpp
+++ clang-tools-extra/clangd/ConfigYAML.cpp
@@ -90,6 +90,10 @@
 
   void parse(Fragment::CompileFlagsBlock &F, Node &N) {
     DictParser Dict("CompileFlags", this);
+    Dict.handle("Compiler", [&](Node &N) {
+      if (auto Value = scalarValue(N, "Compiler"))
+        F.Compiler = std::move(*Value);
+    });
     Dict.handle("Add", [&](Node &N) {
       if (auto Values = scalarValues(N))
         F.Add = std::move(*Values);
Index: clang-tools-extra/clangd/ConfigFragment.h
===================================================================
--- clang-tools-extra/clangd/ConfigFragment.h
+++ clang-tools-extra/clangd/ConfigFragment.h
@@ -134,6 +134,16 @@
   ///
   /// This section modifies how the compile command is constructed.
   struct CompileFlagsBlock {
+    /// Override the compiler executable name to simulate.
+    ///
+    /// The name can affect how flags are parsed (clang++ vs clang).
+    /// If the executable name is in the --query-driver allowlist, then it will
+    /// be invoked to extract include paths.
+    ///
+    /// (That this simply replaces argv[0], and may mangle commands that use
+    /// more complicated drivers like ccache).
+    llvm::Optional<Located<std::string>> Compiler;
+
     /// List of flags to append to the compile command.
     std::vector<Located<std::string>> Add;
     /// List of flags to remove from the compile command.
Index: clang-tools-extra/clangd/ConfigCompile.cpp
===================================================================
--- clang-tools-extra/clangd/ConfigCompile.cpp
+++ clang-tools-extra/clangd/ConfigCompile.cpp
@@ -253,6 +253,16 @@
   }
 
   void compile(Fragment::CompileFlagsBlock &&F) {
+    if (F.Compiler)
+      Out.Apply.push_back(
+          [Compiler(std::move(**F.Compiler))](const Params &, Config &C) {
+            C.CompileFlags.Edits.push_back(
+                [Compiler](std::vector<std::string> &Args) {
+                  if (!Args.empty())
+                    Args.front() = Compiler;
+                });
+          });
+
     if (!F.Remove.empty()) {
       auto Remove = std::make_shared<ArgStripper>();
       for (auto &A : F.Remove)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116196.397240.patch
Type: text/x-patch
Size: 3261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220104/a5dc76d8/attachment-0001.bin>


More information about the cfe-commits mailing list