[clang] 488a19d - [clang-scan-deps] Support double-dashes in clang command lines

Alexandre Ganea via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 17 11:23:03 PDT 2021


Author: Sylvain Audi
Date: 2021-04-17T14:22:51-04:00
New Revision: 488a19d00cbaec479f8c5c298556d2246978f9e6

URL: https://github.com/llvm/llvm-project/commit/488a19d00cbaec479f8c5c298556d2246978f9e6
DIFF: https://github.com/llvm/llvm-project/commit/488a19d00cbaec479f8c5c298556d2246978f9e6.diff

LOG: [clang-scan-deps] Support double-dashes in clang command lines

This fixes argument injection in clang command lines, by adding them before "--".

Previously, the arguments were injected at the end of the command line and could be added after "--", which would be wrongly interpreted as input file paths.

This fix is needed for a subsequent patch, see D92191.

Differential Revision: https://reviews.llvm.org/D95099

Added: 
    

Modified: 
    clang/test/ClangScanDeps/Inputs/regular_cdb.json
    clang/tools/clang-scan-deps/ClangScanDeps.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/ClangScanDeps/Inputs/regular_cdb.json b/clang/test/ClangScanDeps/Inputs/regular_cdb.json
index 902c0b7761fb5..938880c1304f1 100644
--- a/clang/test/ClangScanDeps/Inputs/regular_cdb.json
+++ b/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"
 }
 ]

diff  --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index a8ff42ab104ca..e3ea098d8211a 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -418,14 +418,15 @@ int main(int argc, const char **argv) {
         bool HasMQ = false;
         bool HasMD = false;
         bool HasResourceDir = false;
-        // 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 FlagsEnd = llvm::find(Args, "--");
+        if (FlagsEnd != Args.begin()) {
+          // Reverse scan, starting at the end or at the element before "--".
+          auto R = llvm::make_reverse_iterator(FlagsEnd);
+          for (auto I = R, E = Args.rend(); I != E; ++I) {
+            StringRef Arg = *I;
             if (LastO.empty()) {
-              if (Arg == "-o" && It != Args.rbegin())
-                LastO = Args[Idx + 1];
+              if (Arg == "-o" && I != R)
+                LastO = I[-1]; // Next argument (reverse iterator)
               else if (Arg.startswith("-o"))
                 LastO = Arg.drop_front(2).str();
             }
@@ -437,12 +438,11 @@ int main(int argc, const char **argv) {
               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(), FlagsEnd);
         AdjustedArgs.push_back("-o");
         AdjustedArgs.push_back("/dev/null");
         if (!HasMT && !HasMQ) {
@@ -472,6 +472,7 @@ int main(int argc, const char **argv) {
             AdjustedArgs.push_back(std::string(ResourceDir));
           }
         }
+        AdjustedArgs.insert(AdjustedArgs.end(), FlagsEnd, Args.end());
         return AdjustedArgs;
       });
   AdjustingCompilations->appendArgumentsAdjuster(


        


More information about the cfe-commits mailing list