[PATCH] D95099: [clang-scan-deps] : Support -- in clang command lines.

Sylvain Audi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 20 14:48:23 PST 2021


saudi created this revision.
saudi added reviewers: arphaman, jkolek, Bigcheese, kousikk, dexonsmith, hans.
saudi added a project: clang.
Herald added a subscriber: tschuett.
saudi requested review of this revision.
Herald added a subscriber: cfe-commits.

This fixes argument injection when clang command line contains "--" in the argument.

Previously, the arguments were injected at the end of the command line. It could be added after `--`, and be interpreted as input file paths.

This fix is needed for a subsequent patch, D92191 <https://reviews.llvm.org/D92191>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95099

Files:
  clang/lib/Tooling/Tooling.cpp
  clang/test/ClangScanDeps/Inputs/regular_cdb.json
  clang/tools/clang-scan-deps/ClangScanDeps.cpp


Index: clang/tools/clang-scan-deps/ClangScanDeps.cpp
===================================================================
--- clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -421,14 +421,20 @@
         bool HasMQ = false;
         bool HasMD = false;
         bool HasResourceDir = false;
+        auto ArgsEnd = llvm::find(Args, "--");
+
         // We need to find the last -o value.
         if (!Args.empty()) {
-          std::size_t Idx = Args.size() - 1;
-          for (auto It = Args.rbegin(); It != Args.rend(); ++It) {
-            StringRef Arg = Args[Idx];
+          auto ArgsRBegin = llvm::make_reverse_iterator(ArgsEnd);
+          // Reverse scan, starting at the end or at the element before "--".
+          if (ArgsEnd != Args.end() && ArgsRBegin != Args.rend())
+            ++ArgsRBegin;
+
+          for (auto It = ArgsRBegin; It != Args.rend(); ++It) {
+            StringRef Arg = *It;
             if (LastO.empty()) {
               if (Arg == "-o" && It != Args.rbegin())
-                LastO = Args[Idx + 1];
+                LastO = *(It - 1); // Next argument (reverse iterator)
               else if (Arg.startswith("-o"))
                 LastO = Arg.drop_front(2).str();
             }
@@ -440,12 +446,11 @@
               HasMD = true;
             if (Arg == "-resource-dir")
               HasResourceDir = true;
-            --Idx;
           }
         }
         // If there's no -MT/-MQ Driver would add -MT with the value of the last
         // -o option.
-        tooling::CommandLineArguments AdjustedArgs = Args;
+        tooling::CommandLineArguments AdjustedArgs(Args.begin(), ArgsEnd);
         AdjustedArgs.push_back("-o");
         AdjustedArgs.push_back("/dev/null");
         if (!HasMT && !HasMQ) {
@@ -475,6 +480,7 @@
             AdjustedArgs.push_back(std::string(ResourceDir));
           }
         }
+        AdjustedArgs.insert(AdjustedArgs.end(), ArgsEnd, Args.end());
         return AdjustedArgs;
       });
   AdjustingCompilations->appendArgumentsAdjuster(
Index: clang/test/ClangScanDeps/Inputs/regular_cdb.json
===================================================================
--- clang/test/ClangScanDeps/Inputs/regular_cdb.json
+++ clang/test/ClangScanDeps/Inputs/regular_cdb.json
@@ -11,7 +11,7 @@
 },
 {
   "directory": "DIR",
-  "command": "clang -E DIR/regular_cdb_input.cpp -IInputs -o adena.o",
+  "command": "clang -E -IInputs -o adena.o -- DIR/regular_cdb_input.cpp",
   "file": "DIR/regular_cdb_input.cpp"
 }
 ]
Index: clang/lib/Tooling/Tooling.cpp
===================================================================
--- clang/lib/Tooling/Tooling.cpp
+++ clang/lib/Tooling/Tooling.cpp
@@ -435,13 +435,22 @@
 static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
                               void *MainAddr) {
   // Allow users to override the resource dir.
-  for (StringRef Arg : Args)
+  // Don't search/inject past "--", as the subsequent arguments are only file
+  // names.
+  auto ArgsEnd = Args.end();
+  for (auto I = Args.begin(), E = Args.end(); I != E; ++I) {
+    StringRef Arg = *I;
+    if (Arg == "--") {
+      ArgsEnd = I;
+      break;
+    }
     if (Arg.startswith("-resource-dir"))
       return;
+  }
 
   // If there's no override in place add our resource dir.
-  Args.push_back("-resource-dir=" +
-                 CompilerInvocation::GetResourcesPath(Argv0, MainAddr));
+  Args.insert(ArgsEnd, "-resource-dir=" + CompilerInvocation::GetResourcesPath(
+                                              Argv0, MainAddr));
 }
 
 int ClangTool::run(ToolAction *Action) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95099.318020.patch
Type: text/x-patch
Size: 3649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210120/146ebe66/attachment.bin>


More information about the cfe-commits mailing list