Skip to content

[clang-scan-deps] Expand response files before the argument adjuster #89950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,

// If user provided -o, that is the dependency target, except
// when we are only generating a dependency file.
Arg *OutputOpt = Args.getLastArg(options::OPT_o);
Arg *OutputOpt = Args.getLastArg(options::OPT_o, options::OPT__SLASH_Fo);
if (OutputOpt && Output.getType() != types::TY_Dependencies) {
DepTarget = OutputOpt->getValue();
} else {
Expand Down
56 changes: 56 additions & 0 deletions clang/test/ClangScanDeps/response-file-clang-cl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Check that the scanner can adjust arguments by reading .rsp files in advance.

// RUN: rm -rf %t
// RUN: split-file %s %t

// First run the tests with a .cdb
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
// RUN: sed -e "s|DIR|%/t|g" %t/args_nested.template > %t/args_nested.rsp

// RUN: cp %t/args_compilation.rsp %t/args.rsp
// RUN: clang-scan-deps --compilation-database %t/cdb.json > %t/deps.json
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s

// RUN: cp %t/args_preprocess.rsp %t/args.rsp
// RUN: clang-scan-deps --compilation-database %t/cdb.json > %t/deps.json
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s


// Now run the tests again with a in-place compilation database
// RUN: cd %t

// RUN: cp args_compilation.rsp args.rsp
// RUN: clang-scan-deps -o deps.json -- %clang_cl @args.rsp
// RUN: cat deps.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s

// RUN: cp args_preprocess.rsp args.rsp
// RUN: clang-scan-deps -o deps.json -- %clang_cl @args.rsp
// RUN: cat deps.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s

// Here we ensure that we got a qualified .obj with its full path, since that's what we're passing with /Fo
// CHECK: [[PREFIX]]/tu.obj:

//--- cdb.json.template
[{
"file": "DIR/tu.cpp",
"directory": "DIR",
"command": "clang-cl @DIR/args.rsp"
}]

//--- args_compilation.rsp
@args_nested.rsp
/c

//--- args_preprocess.rsp
@args_nested.rsp
/E

//--- args_nested.template
/I include
tu.cpp
/FoDIR/tu.obj

//--- include/header.h

//--- tu.cpp
#include "header.h"
3 changes: 2 additions & 1 deletion clang/test/Driver/cl-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,10 @@
// NOCLANG-SAME: "-vectorize-slp"
// NOCLANG-NOT: "--dependent-lib=msvcrt"

// RUN: %clang_cl -O2 -MD /clang:-fno-slp-vectorize /clang:-MD /clang:-MF /clang:my_dependency_file.dep -### -- %s 2>&1 | FileCheck -check-prefix=CLANG %s
// RUN: %clang_cl -O2 -MD /clang:-fno-slp-vectorize /clang:-MD /clang:-MF /clang:my_dependency_file.dep /c /Fo%/t/cl-options.obj -### -- %s 2>&1 | FileCheck -DPREFIX=%/t -check-prefix=CLANG %s
// CLANG: "--dependent-lib=msvcrt"
// CLANG-SAME: "-dependency-file" "my_dependency_file.dep"
// CLANG-SAME: "-MT" "[[PREFIX]]/cl-options.obj"
// CLANG-NOT: "--dependent-lib=libcmt"
// CLANG-NOT: "-vectorize-slp"

Expand Down
9 changes: 7 additions & 2 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ static bool DeprecatedDriverCommand;
static ResourceDirRecipeKind ResourceDirRecipe;
static bool Verbose;
static bool PrintTiming;
static llvm::BumpPtrAllocator Alloc;
static llvm::StringSaver Saver{Alloc};
static std::vector<const char *> CommandLine;

#ifndef NDEBUG
Expand All @@ -99,8 +101,6 @@ static bool RoundTripArgs = DoRoundTripDefault;
static void ParseArgs(int argc, char **argv) {
ScanDepsOptTable Tbl;
llvm::StringRef ToolName = argv[0];
llvm::BumpPtrAllocator Alloc;
llvm::StringSaver Saver{Alloc};
llvm::opt::InputArgList Args =
Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
llvm::errs() << Msg << '\n';
Expand Down Expand Up @@ -792,6 +792,11 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {

llvm::cl::PrintOptionValues();

// Expand response files in advance, so that we can "see" all the arguments
// when adjusting below.
Compilations = expandResponseFiles(std::move(Compilations),
llvm::vfs::getRealFileSystem());

// The command options are rewritten to run Clang in preprocessor only mode.
auto AdjustingCompilations =
std::make_unique<tooling::ArgumentsAdjustingCompilations>(
Expand Down
Loading