From: Jan Korous Date: Thu, 12 Sep 2019 00:48:45 +0000 (+0000) Subject: [clang-scan-deps] Add dependency targets X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23a6eed24ca3cbe75d9173ca0272cac2d88578de;p=clang [clang-scan-deps] Add dependency targets Differential Revision: https://reviews.llvm.org/D67475 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371697 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json b/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json index 62627c482c..e458b5ca78 100644 --- a/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json +++ b/test/ClangScanDeps/Inputs/subframework_header_dir_symlink_cdb.json @@ -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" } ] diff --git a/test/ClangScanDeps/Inputs/symlink_cdb.json b/test/ClangScanDeps/Inputs/symlink_cdb.json index 8e911da1e5..6c8b720f00 100644 --- a/test/ClangScanDeps/Inputs/symlink_cdb.json +++ b/test/ClangScanDeps/Inputs/symlink_cdb.json @@ -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" } ] diff --git a/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json b/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json index c5cf6f5899..779203bac6 100644 --- a/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json +++ b/test/ClangScanDeps/Inputs/vfsoverlay_cdb.json @@ -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" } ] diff --git a/test/ClangScanDeps/header_stat_before_open.m b/test/ClangScanDeps/header_stat_before_open.m index 3f63189466..f9f7d240f2 100644 --- a/test/ClangScanDeps/header_stat_before_open.m +++ b/test/ClangScanDeps/header_stat_before_open.m @@ -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 diff --git a/test/ClangScanDeps/regular_cdb.cpp b/test/ClangScanDeps/regular_cdb.cpp index 3060878154..5ba37e05cf 100644 --- a/test/ClangScanDeps/regular_cdb.cpp +++ b/test/ClangScanDeps/regular_cdb.cpp @@ -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 diff --git a/test/ClangScanDeps/subframework_header_dir_symlink.m b/test/ClangScanDeps/subframework_header_dir_symlink.m index f3d302f2ce..46f8f5bb0c 100644 --- a/test/ClangScanDeps/subframework_header_dir_symlink.m +++ b/test/ClangScanDeps/subframework_header_dir_symlink.m @@ -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 diff --git a/test/ClangScanDeps/vfsoverlay.cpp b/test/ClangScanDeps/vfsoverlay.cpp index a5f4dc437f..517738943a 100644 --- a/test/ClangScanDeps/vfsoverlay.cpp +++ b/test/ClangScanDeps/vfsoverlay.cpp @@ -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 diff --git a/tools/clang-scan-deps/ClangScanDeps.cpp b/tools/clang-scan-deps/ClangScanDeps.cpp index 2181e0e0d3..4020a4ce64 100644 --- a/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/tools/clang-scan-deps/ClangScanDeps.cpp @@ -178,6 +178,13 @@ llvm::cl::opt 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( 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");