[clang] 9cca5c1 - [analyzer] Make checker silencing work for non-pathsensitive bug reports

Kristóf Umann via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 17 01:28:14 PDT 2021


Author: Kirstóf Umann
Date: 2021-06-17T10:27:34+02:00
New Revision: 9cca5c1391d637b5500ada646cf136ddb38254a3

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

LOG: [analyzer] Make checker silencing work for non-pathsensitive bug reports

D66572 separated BugReport and BugReporter into basic and path sensitive
versions. As a result, checker silencing, which worked deep in the path
sensitive report generation facilities became specific to it. DeadStoresChecker,
for instance, despite being in the static analyzer, emits non-pathsensitive
reports, and was impossible to silence.

This patch moves the corresponding code before the call to the virtual function
generateDiagnosticForConsumerMap (which is overriden by the specific kinds of
bug reporters). Although we see bug reporting as relatively lightweight compared
to the analysis, this will get rid of several steps we used to throw away.

Quoting from D65379:

At a very high level, this consists of 3 steps:

For all BugReports in the same BugReportEquivClass, collect all their error
nodes in a set. With that set, create a new, trimmed ExplodedGraph whose leafs
are all error nodes.
Until a valid report is found, construct a bug path, which is yet another
ExplodedGraph, that is linear from a given error node to the root of the graph.
Run all visitors on the constructed bug path. If in this process the report got
invalidated, start over from step 2.
Checker silencing used to kick in after all of these. Now it does before any of
them :^)

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

Change-Id: Ice42939304516f2bebd05a1ea19878b89c96a25d

Added: 
    clang/test/Analysis/silence-checkers.cpp

Modified: 
    clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Removed: 
    clang/test/Analysis/silence-checkers-malloc.cpp


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
index 61734808d1ba..6ace986b4e0d 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp
@@ -1989,13 +1989,6 @@ PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
   const SourceManager &SM = getSourceManager();
   const AnalyzerOptions &Opts = getAnalyzerOptions();
 
-  // See whether we need to silence the checker/package.
-  // FIXME: This will not work if the report was emitted with an incorrect tag.
-  for (const std::string &CheckerOrPackage : Opts.SilencedCheckersAndPackages) {
-    if (R->getBugType().getCheckerName().startswith(CheckerOrPackage))
-      return nullptr;
-  }
-
   if (!PDC->shouldGenerateDiagnostics())
     return generateEmptyDiagnosticForReport(R, getSourceManager());
 
@@ -3040,6 +3033,14 @@ void BugReporter::FlushReport(BugReportEquivClass& EQ) {
   if (!report)
     return;
 
+  // See whether we need to silence the checker/package.
+  for (const std::string &CheckerOrPackage :
+       getAnalyzerOptions().SilencedCheckersAndPackages) {
+    if (report->getBugType().getCheckerName().startswith(
+            CheckerOrPackage))
+      return;
+  }
+
   ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
   std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
       generateDiagnosticForConsumerMap(report, Consumers, bugReports);

diff  --git a/clang/test/Analysis/silence-checkers-malloc.cpp b/clang/test/Analysis/silence-checkers.cpp
similarity index 81%
rename from clang/test/Analysis/silence-checkers-malloc.cpp
rename to clang/test/Analysis/silence-checkers.cpp
index 2f6a9dd2d5b8..a2f1e60a4522 100644
--- a/clang/test/Analysis/silence-checkers-malloc.cpp
+++ b/clang/test/Analysis/silence-checkers.cpp
@@ -11,6 +11,12 @@
 // RUN:   -analyzer-checker=cplusplus.NewDelete\
 // RUN:   -analyzer-config silence-checkers="unix"
 
+// RUN: %clang_analyze_cc1 -verify="deadstore-silenced" %s \
+// RUN:   -analyzer-checker=core \
+// RUN:   -analyzer-checker=apiModeling \
+// RUN:   -analyzer-checker=deadcode \
+// RUN:   -analyzer-config silence-checkers="deadcode.DeadStores"
+
 #include "Inputs/system-header-simulator-cxx.h"
 
 typedef __typeof(sizeof(int)) size_t;
@@ -38,3 +44,17 @@ void test_delete_ZERO_SIZE_PTR() {
   delete Ptr; // no-silence-warning{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
               // unix-silenced-warning at -1{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
 }
+
+// deadstore-silenced-no-diagnostics
+
+int foo() {
+  int x = 42;
+  return x;
+}
+
+void g() {
+  int y;
+  y = 7;
+  int x = foo();
+  y = 10;
+}


        


More information about the cfe-commits mailing list