[PATCH] D92330: [clang-scan-deps] Improve argument parsing to find target object file path.

Sylvain Audi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 09:12:14 PST 2020


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

This patch adds support for the joined version of `-o` (e.g. `-ofilename`)  command parameter when reading the clang arguments.

It also fixes a bug, as the first occurence of `-o` was used instead of the last one.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92330

Files:
  clang/test/ClangScanDeps/Inputs/target-filename-cdb.json
  clang/test/ClangScanDeps/target-filename.cpp
  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
@@ -425,18 +425,21 @@
         if (!Args.empty()) {
           std::size_t Idx = Args.size() - 1;
           for (auto It = Args.rbegin(); It != Args.rend(); ++It) {
-            if (It != Args.rbegin()) {
-              if (Args[Idx] == "-o")
+            StringRef Arg = Args[Idx];
+            if (LastO.empty()) {
+              if (Arg == "-o" && It != Args.rbegin())
                 LastO = Args[Idx + 1];
-              if (Args[Idx] == "-MT")
-                HasMT = true;
-              if (Args[Idx] == "-MQ")
-                HasMQ = true;
-              if (Args[Idx] == "-MD")
-                HasMD = true;
-              if (Args[Idx] == "-resource-dir")
-                HasResourceDir = true;
+              else if (Arg.startswith("-o"))
+                LastO = Arg.drop_front(2).str();
             }
+            if (Arg == "-MT")
+              HasMT = true;
+            if (Arg == "-MQ")
+              HasMQ = true;
+            if (Arg == "-MD")
+              HasMD = true;
+            if (Arg == "-resource-dir")
+              HasResourceDir = true;
             --Idx;
           }
         }
Index: clang/test/ClangScanDeps/target-filename.cpp
===================================================================
--- /dev/null
+++ clang/test/ClangScanDeps/target-filename.cpp
@@ -0,0 +1,20 @@
+// RUN: rm -rf %t.dir
+// RUN: rm -rf %t.cdb
+// RUN: mkdir -p %t.dir
+// RUN: cp %s %t.dir/target-filename_input.cpp
+// RUN: mkdir %t.dir/Inputs
+// RUN: cp %S/Inputs/header.h %t.dir/Inputs/header.h
+// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/target-filename-cdb.json > %t.cdb
+// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 | FileCheck %s
+
+// CHECK: target-filename_input.o:
+// CHECK-NEXT: target-filename_input.cpp
+
+// CHECK-NEXT: a.o:
+// CHECK-NEXT: target-filename_input.cpp
+
+// CHECK-NEXT: b.o:
+// CHECK-NEXT: target-filename_input.cpp
+
+// CHECK-NEXT: last.o:
+// CHECK-NEXT: target-filename_input.cpp
Index: clang/test/ClangScanDeps/Inputs/target-filename-cdb.json
===================================================================
--- /dev/null
+++ clang/test/ClangScanDeps/Inputs/target-filename-cdb.json
@@ -0,0 +1,22 @@
+[
+{
+  "directory": "DIR",
+  "command": "clang -E DIR/target-filename_input.cpp",
+  "file": "DIR/target-filename_input.cpp"
+},
+{
+  "directory": "DIR",
+  "command": "clang -E DIR/target-filename_input.cpp -o a.o",
+  "file": "DIR/target-filename_input.cpp"
+},
+{
+  "directory": "DIR",
+  "command": "clang -E DIR/target-filename_input.cpp -ob.o",
+  "file": "DIR/target-filename_input.cpp"
+},
+{
+  "directory": "DIR",
+  "command": "clang -E DIR/target-filename_input.cpp -o first.o -o last.o",
+  "file": "DIR/target-filename_input.cpp"
+}
+]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92330.308391.patch
Type: text/x-patch
Size: 2958 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201130/a6dcdfae/attachment-0001.bin>


More information about the cfe-commits mailing list