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

Alexandre Ganea via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 24 10:53:13 PDT 2024


https://github.com/aganea updated https://github.com/llvm/llvm-project/pull/89950

>From f2340c98c95e0d72516fc240ff268fead9f15391 Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <aganea at havenstudios.com>
Date: Wed, 17 Apr 2024 16:28:21 -0400
Subject: [PATCH 1/2] [clang-scan-deps] Expand response files before the
 argument ajuster

Previously, since response (.rsp) files weren't expanded, we only parsed
the command-line as provided in the Clang CDB file. Unfortunately, when
using Unreal Engine, arguments are always generated in a .rsp file.

After this patch, /Fo can be parsed and added to the final command-line.
Without this option, the make targets that are emitted are made up from the
input file name alone. We have some cases where the same input in the project
generates several output files, so we end up with duplicate make targets
in the scan-deps emitted dependency file.
---
 clang/tools/clang-scan-deps/ClangScanDeps.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index f42af7e330e17a..7b7f10c4be7421 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -792,10 +792,15 @@ 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.
+  auto ResponseExpander = 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>(
-          std::move(Compilations));
+          std::move(ResponseExpander));
   ResourceDirectoryCache ResourceDirCache;
 
   AdjustingCompilations->appendArgumentsAdjuster(

>From 09e0595fa9f3c9795d6f40a7308d8a912254b1df Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <aganea at havenstudios.com>
Date: Wed, 24 Apr 2024 13:52:55 -0400
Subject: [PATCH 2/2] Add test

---
 .../ClangScanDeps/response-file-clang-cl.c    | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 clang/test/ClangScanDeps/response-file-clang-cl.c

diff --git a/clang/test/ClangScanDeps/response-file-clang-cl.c b/clang/test/ClangScanDeps/response-file-clang-cl.c
new file mode 100644
index 00000000000000..78e3d15deb1678
--- /dev/null
+++ b/clang/test/ClangScanDeps/response-file-clang-cl.c
@@ -0,0 +1,32 @@
+// Check that the scanner can adjust arguments by reading .rsp files in advance.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
+// RUN: echo /Fo%t/tu.obj >> %t/args_nested.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
+
+// 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/t.cpp",
+  "directory": "DIR",
+  "command": "clang-cl @DIR/args.rsp"
+}]
+
+//--- args.rsp
+ at args_nested.rsp
+/c tu.cpp
+
+//--- args_nested.rsp
+/I include
+
+//--- include/header.h
+
+//--- tu.cpp
+#include "header.h"



More information about the cfe-commits mailing list