llvm-project/clang/test/Analysis/silence-checkers.cpp
Kirstóf Umann 9cca5c1391 [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
2021-06-17 10:27:34 +02:00

61 lines
2.0 KiB
C++

// RUN: %clang_analyze_cc1 -verify="no-silence" %s \
// RUN: -triple i386-unknown-linux-gnu \
// RUN: -analyzer-checker=core,apiModeling \
// RUN: -analyzer-checker=unix.Malloc \
// RUN: -analyzer-checker=cplusplus.NewDelete
// RUN: %clang_analyze_cc1 -verify="unix-silenced" %s \
// RUN: -triple i386-unknown-linux-gnu \
// RUN: -analyzer-checker=core,apiModeling \
// RUN: -analyzer-checker=unix.Malloc \
// 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;
void *malloc(size_t);
void free(void *);
void *realloc(void *ptr, size_t size);
void *calloc(size_t nmemb, size_t size);
char *strdup(const char *s);
void checkThatMallocCheckerIsRunning() {
malloc(4);
} // no-silence-warning{{Potential memory leak [unix.Malloc]}}
int const_ptr_and_callback_def_param_null(int, const char *, int n, void (*)(void *) = 0);
void r11160612_no_callback() {
char *x = (char *)malloc(12);
const_ptr_and_callback_def_param_null(0, x, 12);
} // no-silence-warning{{Potential leak of memory pointed to by 'x' [unix.Malloc]}}
#define ZERO_SIZE_PTR ((void *)16)
void test_delete_ZERO_SIZE_PTR() {
int *Ptr = (int *)ZERO_SIZE_PTR;
// ZERO_SIZE_PTR is specially handled but only for malloc family
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@-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;
}