r371697 - [clang-scan-deps] Add dependency targets

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 17:48:46 PDT 2019


Author: jkorous
Date: Wed Sep 11 17:48:45 2019
New Revision: 371697

URL: http://llvm.org/viewvc/llvm-project?rev=371697&view=rev
Log:
[clang-scan-deps] Add dependency targets

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

Modified:
    cfe/trunk/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json
    cfe/trunk/test/ClangScanDeps/Inputs/symlink_cdb.json
    cfe/trunk/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json
    cfe/trunk/test/ClangScanDeps/header_stat_before_open.m
    cfe/trunk/test/ClangScanDeps/regular_cdb.cpp
    cfe/trunk/test/ClangScanDeps/subframework_header_dir_symlink.m
    cfe/trunk/test/ClangScanDeps/vfsoverlay.cpp
    cfe/trunk/tools/clang-scan-deps/ClangScanDeps.cpp

Modified: cfe/trunk/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json (original)
+++ cfe/trunk/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json Wed Sep 11 17:48:45 2019
@@ -2,11 +2,11 @@
 {
   "directory": "DIR",
   "command": "clang -E DIR/subframework_header_dir_symlink_input.m -D EMPTY -iframework Inputs/frameworks",
-  "file": "DIR/subframework_header_dir_symlink.m"
+  "file": "DIR/subframework_header_dir_symlink_input.m"
 },
 {
   "directory": "DIR",
   "command": "clang -E DIR/subframework_header_dir_symlink_input2.m -FInputs/frameworks -iframework Inputs/frameworks_symlink",
-  "file": "DIR/subframework_header_dir_symlink2.m"
+  "file": "DIR/subframework_header_dir_symlink_input2.m"
 }
 ]

Modified: cfe/trunk/test/ClangScanDeps/Inputs/symlink_cdb.json
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/symlink_cdb.json?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/symlink_cdb.json (original)
+++ cfe/trunk/test/ClangScanDeps/Inputs/symlink_cdb.json Wed Sep 11 17:48:45 2019
@@ -2,11 +2,11 @@
 {
   "directory": "DIR",
   "command": "clang -E DIR/symlink_input.cpp -IInputs",
-  "file": "DIR/symlink.cpp"
+  "file": "DIR/symlink_input.cpp"
 },
 {
   "directory": "DIR",
   "command": "clang -E DIR/symlink_input2.cpp -IInputs",
-  "file": "DIR/symlink2.cpp"
+  "file": "DIR/symlink_input2.cpp"
 }
 ]

Modified: cfe/trunk/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json (original)
+++ cfe/trunk/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json Wed Sep 11 17:48:45 2019
@@ -2,6 +2,6 @@
 {
   "directory": "DIR",
   "command": "clang -E DIR/vfsoverlay_input.cpp -IInputs -ivfsoverlay DIR/vfsoverlay.yaml",
-  "file": "DIR/vfsoverlay.cpp"
+  "file": "DIR/vfsoverlay_input.cpp"
 }
 ]

Modified: cfe/trunk/test/ClangScanDeps/header_stat_before_open.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/header_stat_before_open.m?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/header_stat_before_open.m (original)
+++ cfe/trunk/test/ClangScanDeps/header_stat_before_open.m Wed Sep 11 17:48:45 2019
@@ -12,7 +12,7 @@
 #include "Framework/Framework.h"
 #include "Framework/PrivateHeader.h"
 
-// CHECK: clang-scan-deps dependency
+// CHECK: header_stat_before_open_input.o
 // CHECK-NEXT: header_stat_before_open_input.m
 // CHECK-NEXT: Inputs{{/|\\}}frameworks{{/|\\}}Framework.framework{{/|\\}}Headers{{/|\\}}Framework.h
 // CHECK-NEXT: Inputs{{/|\\}}frameworks{{/|\\}}Framework.framework{{/|\\}}PrivateHeaders{{/|\\}}PrivateHeader.h

Modified: cfe/trunk/test/ClangScanDeps/regular_cdb.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/regular_cdb.cpp?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/regular_cdb.cpp (original)
+++ cfe/trunk/test/ClangScanDeps/regular_cdb.cpp Wed Sep 11 17:48:45 2019
@@ -36,6 +36,7 @@
 #include "header.h"
 
 // CHECK1: regular_cdb_input2.cpp
+// CHECK1-NEXT: regular_cdb_input2.cpp
 // CHECK1-NEXT: Inputs{{/|\\}}header.h
 // CHECK1-NEXT: Inputs{{/|\\}}header2.h
 

Modified: cfe/trunk/test/ClangScanDeps/subframework_header_dir_symlink.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/subframework_header_dir_symlink.m?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/subframework_header_dir_symlink.m (original)
+++ cfe/trunk/test/ClangScanDeps/subframework_header_dir_symlink.m Wed Sep 11 17:48:45 2019
@@ -17,8 +17,8 @@
 #include "Framework/Framework.h"
 #endif
 
-// CHECK: clang-scan-deps dependency
+// CHECK: subframework_header_dir_symlink_input.o
 // CHECK-NEXT: subframework_header_dir_symlink_input.m
-// CHECK: clang-scan-deps dependency
+// CHECK: subframework_header_dir_symlink_input2.o
 // CHECK-NEXT: subframework_header_dir_symlink_input2.m
 // CHECK-NEXT: Inputs{{/|\\}}frameworks_symlink{{/|\\}}Framework.framework{{/|\\}}Headers{{/|\\}}Framework.h

Modified: cfe/trunk/test/ClangScanDeps/vfsoverlay.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/vfsoverlay.cpp?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/test/ClangScanDeps/vfsoverlay.cpp (original)
+++ cfe/trunk/test/ClangScanDeps/vfsoverlay.cpp Wed Sep 11 17:48:45 2019
@@ -12,6 +12,6 @@
 
 #include "not_real.h"
 
-// CHECK: clang-scan-deps dependency
+// CHECK: vfsoverlay_input.o
 // CHECK-NEXT: vfsoverlay_input.cpp
 // CHECK-NEXT: Inputs{{/|\\}}header.h

Modified: cfe/trunk/tools/clang-scan-deps/ClangScanDeps.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-scan-deps/ClangScanDeps.cpp?rev=371697&r1=371696&r2=371697&view=diff
==============================================================================
--- cfe/trunk/tools/clang-scan-deps/ClangScanDeps.cpp (original)
+++ cfe/trunk/tools/clang-scan-deps/ClangScanDeps.cpp Wed Sep 11 17:48:45 2019
@@ -178,6 +178,13 @@ llvm::cl::opt<bool> SkipExcludedPPRanges
 
 } // end anonymous namespace
 
+/// \returns object-file path derived from source-file path.
+static std::string getObjFilePath(StringRef SrcFile) {
+  SmallString<128> ObjFileName(SrcFile);
+  llvm::sys::path::replace_extension(ObjFileName, "o");
+  return ObjFileName.str();
+}
+
 int main(int argc, const char **argv) {
   llvm::InitLLVM X(argc, argv);
   llvm::cl::HideUnrelatedOptions(DependencyScannerCategory);
@@ -206,10 +213,45 @@ int main(int argc, const char **argv) {
       std::make_unique<tooling::ArgumentsAdjustingCompilations>(
           std::move(Compilations));
   AdjustingCompilations->appendArgumentsAdjuster(
-      [](const tooling::CommandLineArguments &Args, StringRef /*unused*/) {
+      [](const tooling::CommandLineArguments &Args, StringRef FileName) {
+        std::string LastO = "";
+        bool HasMT = false;
+        bool HasMQ = false;
+        bool HasMD = 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) {
+            if (It != Args.rbegin()) {
+              if (Args[Idx] == "-o")
+                LastO = Args[Idx + 1];
+              if (Args[Idx] == "-MT")
+                HasMT = true;
+              if (Args[Idx] == "-MQ")
+                HasMQ = true;
+              if (Args[Idx] == "-MD")
+                HasMD = true;
+            }
+            --Idx;
+          }
+        }
+        // If there's no -MT/-MQ Driver would add -MT with the value of the last
+        // -o option.
         tooling::CommandLineArguments AdjustedArgs = Args;
         AdjustedArgs.push_back("-o");
         AdjustedArgs.push_back("/dev/null");
+        if (!HasMT && !HasMQ) {
+          AdjustedArgs.push_back("-MT");
+          // We're interested in source dependencies of an object file.
+          if (!HasMD) {
+            // FIXME: We are missing the directory unless the -o value is an
+            // absolute path.
+            AdjustedArgs.push_back(!LastO.empty() ? LastO
+                                                  : getObjFilePath(FileName));
+          } else {
+            AdjustedArgs.push_back(FileName);
+          }
+        }
         AdjustedArgs.push_back("-Xclang");
         AdjustedArgs.push_back("-Eonly");
         AdjustedArgs.push_back("-Xclang");




More information about the cfe-commits mailing list