[PATCH] D97265: [clang] Allow clang-check to customize output file name

Ella Ma via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 23 01:45:50 PST 2021


OikawaKirie created this revision.
OikawaKirie added reviewers: hokein, sammccall, kbobyrev, alexfh.
OikawaKirie added a project: clang.
OikawaKirie requested review of this revision.
Herald added a subscriber: cfe-commits.

Required by https://stackoverflow.com/questions/58073606

As the output argument is stripped out in the `clang-check` tool, it seems impossible for `clang-check` users to customize the output file name, even with `-extra-args` and `-extra-arg-before`.

This patch adds the `-output` argument to allow users to adjust the output name. And if the argument is not set, the original strip output adjuster will remove the output arguments.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97265

Files:
  clang/test/Tooling/clang-check-reset-o.cpp
  clang/tools/clang-check/ClangCheck.cpp


Index: clang/tools/clang-check/ClangCheck.cpp
===================================================================
--- clang/tools/clang-check/ClangCheck.cpp
+++ clang/tools/clang-check/ClangCheck.cpp
@@ -76,7 +76,9 @@
     Analyze("analyze",
             cl::desc(Options.getOptionHelpText(options::OPT_analyze)),
             cl::cat(ClangCheckCategory));
-
+static cl::opt<std::string>
+    Output("output", cl::desc(Options.getOptionHelpText(options::OPT_o)),
+           cl::cat(ClangCheckCategory));
 static cl::opt<bool>
     Fixit("fixit", cl::desc(Options.getOptionHelpText(options::OPT_fixit)),
           cl::cat(ClangCheckCategory));
@@ -206,7 +208,18 @@
 
   // Clear adjusters because -fsyntax-only is inserted by the default chain.
   Tool.clearArgumentsAdjusters();
-  Tool.appendArgumentsAdjuster(getClangStripOutputAdjuster());
+
+  // Reset output path if is provided by user.
+  Tool.appendArgumentsAdjuster(
+      [&](const CommandLineArguments &Args, StringRef File) {
+        auto Ret = getClangStripOutputAdjuster()(Args, File);
+        if (!Output.empty()) {
+          Ret.emplace_back("-o");
+          Ret.emplace_back(Output);
+        }
+        return Ret;
+      });
+
   Tool.appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
 
   // Running the analyzer requires --analyze. Other modes can work with the
Index: clang/test/Tooling/clang-check-reset-o.cpp
===================================================================
--- /dev/null
+++ clang/test/Tooling/clang-check-reset-o.cpp
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: echo '[{"directory":".","command":"clang++ -c %t/test.cpp -o foo -ofoo","file":"%t/test.cpp"}]' | sed -e 's/\\/\//g' > %t/compile_commands.json
+// RUN: cp "%s" "%t/test.cpp"
+// RUN: not clang-check -p "%t" "%t/test.cpp" -analyze -output=qwerty -extra-arg=-v 2>&1|FileCheck %s
+// FIXME: Make the above easier.
+
+// CHECK: Invocation
+// CHECK: {{qwerty}}
+// CHECK: C++ requires
+invalid;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97265.325710.patch
Type: text/x-patch
Size: 1988 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210223/d94263ab/attachment.bin>


More information about the cfe-commits mailing list