[clang] 79ba796 - [clang-scan-deps] Improve argument parsing to find target object file path.

Sylvain Audi via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 12:04:30 PST 2020


Author: Sylvain Audi
Date: 2020-12-01T15:04:15-05:00
New Revision: 79ba7967f4813445c3ce5624170c19584bbfda72

URL: https://github.com/llvm/llvm-project/commit/79ba7967f4813445c3ce5624170c19584bbfda72
DIFF: https://github.com/llvm/llvm-project/commit/79ba7967f4813445c3ce5624170c19584bbfda72.diff

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

Support the joined version of -o (-ofilepath), and ensure we use the last provided -o option.

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

Added: 
    clang/test/ClangScanDeps/Inputs/target-filename-cdb.json
    clang/test/ClangScanDeps/target-filename.cpp

Modified: 
    clang/tools/clang-scan-deps/ClangScanDeps.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/ClangScanDeps/Inputs/target-filename-cdb.json b/clang/test/ClangScanDeps/Inputs/target-filename-cdb.json
new file mode 100644
index 000000000000..af2e43e4e171
--- /dev/null
+++ b/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"
+}
+]

diff  --git a/clang/test/ClangScanDeps/target-filename.cpp b/clang/test/ClangScanDeps/target-filename.cpp
new file mode 100644
index 000000000000..c47166b21e4e
--- /dev/null
+++ b/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

diff  --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index 91d2dc23d861..1bb65ef12d6b 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -425,18 +425,21 @@ int main(int argc, const char **argv) {
         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;
           }
         }


        


More information about the cfe-commits mailing list