[clang] da168dd - [clang] Allow clang-check to customize analyzer output file or dir name

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 15 07:49:50 PST 2021


Author: Ella Ma
Date: 2021-11-15T16:49:41+01:00
New Revision: da168dd875bf0392e8e88834009d776bfbaae376

URL: https://github.com/llvm/llvm-project/commit/da168dd875bf0392e8e88834009d776bfbaae376
DIFF: https://github.com/llvm/llvm-project/commit/da168dd875bf0392e8e88834009d776bfbaae376.diff

LOG: [clang] Allow clang-check to customize analyzer output file or dir name

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 -analyzer-output-path argument to allow users to adjust the output name. And if the argument is not set or the analyzer is not enabled, the original strip output adjuster will remove the output arguments.

Differential Revision: https://reviews.llvm.org/D97265

Added: 
    clang/test/Tooling/clang-check-set-analyzer-output-path.cpp

Modified: 
    clang/tools/clang-check/ClangCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Tooling/clang-check-set-analyzer-output-path.cpp b/clang/test/Tooling/clang-check-set-analyzer-output-path.cpp
new file mode 100644
index 000000000000..ff5e86ae7892
--- /dev/null
+++ b/clang/test/Tooling/clang-check-set-analyzer-output-path.cpp
@@ -0,0 +1,14 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: cd %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: echo '// CHECK: {{qwerty}}' > %t/cclog-check
+// RUN: clang-check -p "%t" "%t/test.cpp" -analyze -analyzer-output-path=%t/qwerty -extra-arg=-v -extra-arg=-Xclang -extra-arg=-verify 2>&1 | FileCheck %t/cclog-check
+// RUN: FileCheck %s --input-file=%t/qwerty
+
+// CHECK: DOCTYPE plist
+// CHECK: Division by zero
+int f() {
+  return 1 / 0; // expected-warning {{Division by zero}}
+}

diff  --git a/clang/tools/clang-check/ClangCheck.cpp b/clang/tools/clang-check/ClangCheck.cpp
index 11fdeb71fd9e..4d6ded029e2f 100644
--- a/clang/tools/clang-check/ClangCheck.cpp
+++ b/clang/tools/clang-check/ClangCheck.cpp
@@ -76,6 +76,10 @@ static cl::opt<bool>
     Analyze("analyze",
             cl::desc(Options.getOptionHelpText(options::OPT_analyze)),
             cl::cat(ClangCheckCategory));
+static cl::opt<std::string>
+    AnalyzerOutput("analyzer-output-path",
+                   cl::desc(Options.getOptionHelpText(options::OPT_o)),
+                   cl::cat(ClangCheckCategory));
 
 static cl::opt<bool>
     Fixit("fixit", cl::desc(Options.getOptionHelpText(options::OPT_fixit)),
@@ -206,7 +210,19 @@ int main(int argc, const char **argv) {
 
   // 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(
+      Analyze ? [&](const CommandLineArguments &Args, StringRef File) {
+                  auto Ret = getClangStripOutputAdjuster()(Args, File);
+                  if (!AnalyzerOutput.empty()) {
+                    Ret.emplace_back("-o");
+                    Ret.emplace_back(AnalyzerOutput);
+                  }
+                  return Ret;
+                }
+              : getClangStripOutputAdjuster());
+
   Tool.appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
 
   // Running the analyzer requires --analyze. Other modes can work with the


        


More information about the cfe-commits mailing list