[PATCH] D116329: [clang-check] Adjust argument adjusters for clang-check to strip options blocking the static analyzer

Ella Ma via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 13 17:00:04 PST 2022


OikawaKirie updated this revision to Diff 399841.
OikawaKirie added a comment.

Updated as suggested.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116329/new/

https://reviews.llvm.org/D116329

Files:
  clang/test/Tooling/clang-check-analyze-save-temps.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
@@ -208,27 +208,37 @@
   ClangTool Tool(OptionsParser.getCompilations(),
                  OptionsParser.getSourcePathList());
 
-  // Clear adjusters because -fsyntax-only is inserted by the default chain.
-  Tool.clearArgumentsAdjusters();
-
-  // 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
-  // -fsyntax-only option.
-  Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster(
-      Analyze ? "--analyze" : "-fsyntax-only", ArgumentInsertPosition::BEGIN));
+  if (Analyze) {
+    // Set output path if is provided by user.
+    //
+    // As the original -o options have been removed by default via the
+    // strip-output adjuster, we only need to add the analyzer -o options here
+    // when it is provided by users.
+    if (!AnalyzerOutput.empty())
+      Tool.appendArgumentsAdjuster(
+          getInsertArgumentAdjuster(CommandLineArguments{"-o", AnalyzerOutput},
+                                    ArgumentInsertPosition::END));
+
+    // Running the analyzer requires --analyze. Other modes can work with the
+    // -fsyntax-only option.
+    //
+    // The syntax-only adjuster is installed by default.
+    // Good: It also strips options that trigger extra output, like -save-temps.
+    // Bad:  We don't want the -fsyntax-only when executing the static analyzer.
+    //
+    // To enable the static analyzer, we first strip all -fsyntax-only options
+    // and then add an --analyze option to the front.
+    Tool.appendArgumentsAdjuster(
+        [&](const CommandLineArguments &Args, StringRef /*unused*/) {
+          CommandLineArguments AdjustedArgs;
+          for (const std::string &Arg : Args)
+            if (Arg != "-fsyntax-only")
+              AdjustedArgs.emplace_back(Arg);
+          return AdjustedArgs;
+        });
+    Tool.appendArgumentsAdjuster(
+        getInsertArgumentAdjuster("--analyze", ArgumentInsertPosition::BEGIN));
+  }
 
   ClangCheckActionFactory CheckFactory;
   std::unique_ptr<FrontendActionFactory> FrontendFactory;
Index: clang/test/Tooling/clang-check-analyze-save-temps.cpp
===================================================================
--- /dev/null
+++ clang/test/Tooling/clang-check-analyze-save-temps.cpp
@@ -0,0 +1,19 @@
+// Check whether output generation options (like -save-temps) will not affect
+// the execution of the analyzer.
+
+// RUN: clang-check -analyze %s -- -save-temps -c -Xclang -verify
+
+// Check whether redundant -fsyntax-only options will affect the execution of
+// the analyzer.
+
+// RUN: clang-check -analyze %s -- \
+// RUN:   -fsyntax-only -c -fsyntax-only -Xclang -verify 2>&1 | \
+// RUN:   FileCheck %s --allow-empty
+
+// CHECK-NOT: argument unused during compilation: '--analyze'
+
+void a(int *x) {
+  if (x) {
+  }
+  *x = 47; // expected-warning {{Dereference of null pointer}}
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116329.399841.patch
Type: text/x-patch
Size: 3599 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220114/00f5bd5b/attachment.bin>


More information about the cfe-commits mailing list