]> granicus.if.org Git - clang/commitdiff
[clang-scan-deps] Add dependency targets
authorJan Korous <jkorous@apple.com>
Thu, 12 Sep 2019 00:48:45 +0000 (00:48 +0000)
committerJan Korous <jkorous@apple.com>
Thu, 12 Sep 2019 00:48:45 +0000 (00:48 +0000)
Differential Revision: https://reviews.llvm.org/D67475

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371697 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 62627c482ca21951237496969d135de37d08140c..e458b5ca789f7686a70227194170758e85c82425 100644 (file)
@@ -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"
 }
 ]
index 8e911da1e5b6dec2aa1c24e6eae6ece1ee343737..6c8b720f001327f9b39e0f2cd63e486bfe95ab8d 100644 (file)
@@ -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"
 }
 ]
index c5cf6f5899c2842e4e5ea05542d231ab5ef5e4e7..779203bac674bcfc8ae45dd7050eaba3568344c2 100644 (file)
@@ -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"
 }
 ]
index 3f63189466221a995d9c6e4466e1774d1494aad3..f9f7d240f2c110b77bcc6a03022fa2073da9871f 100644 (file)
@@ -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
index 306087815477f29f72bbadb5c255694e6062eed4..5ba37e05cfef601a2fa55c7f1f5ca52245024693 100644 (file)
@@ -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
 
index f3d302f2ceae55b0e4d97383dc5e0dce410bb5c2..46f8f5bb0c52255523a3e43ac84abf52f15181e6 100644 (file)
@@ -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
index a5f4dc437f1716884d944aa2bd1f92cf0c625f7e..517738943ab57048ca54a0122eeaa0c2e058d92b 100644 (file)
@@ -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
index 2181e0e0d39c0247fb8ba7c00c28b94cbe800be1..4020a4ce64b241eaa48b820767a6cef272787ee8 100644 (file)
@@ -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");