xref: /llvm-project/clang/test/Analysis/silence-checkers.cpp (revision 9cca5c1391d637b5500ada646cf136ddb38254a3)
1 // RUN: %clang_analyze_cc1 -verify="no-silence" %s \
2 // RUN:   -triple i386-unknown-linux-gnu \
3 // RUN:   -analyzer-checker=core,apiModeling \
4 // RUN:   -analyzer-checker=unix.Malloc \
5 // RUN:   -analyzer-checker=cplusplus.NewDelete
6 
7 // RUN: %clang_analyze_cc1 -verify="unix-silenced" %s \
8 // RUN:   -triple i386-unknown-linux-gnu \
9 // RUN:   -analyzer-checker=core,apiModeling \
10 // RUN:   -analyzer-checker=unix.Malloc \
11 // RUN:   -analyzer-checker=cplusplus.NewDelete\
12 // RUN:   -analyzer-config silence-checkers="unix"
13 
14 // RUN: %clang_analyze_cc1 -verify="deadstore-silenced" %s \
15 // RUN:   -analyzer-checker=core \
16 // RUN:   -analyzer-checker=apiModeling \
17 // RUN:   -analyzer-checker=deadcode \
18 // RUN:   -analyzer-config silence-checkers="deadcode.DeadStores"
19 
20 #include "Inputs/system-header-simulator-cxx.h"
21 
22 typedef __typeof(sizeof(int)) size_t;
23 void *malloc(size_t);
24 void free(void *);
25 void *realloc(void *ptr, size_t size);
26 void *calloc(size_t nmemb, size_t size);
27 char *strdup(const char *s);
28 
checkThatMallocCheckerIsRunning()29 void checkThatMallocCheckerIsRunning() {
30   malloc(4);
31 } // no-silence-warning{{Potential memory leak [unix.Malloc]}}
32 
33 int const_ptr_and_callback_def_param_null(int, const char *, int n, void (*)(void *) = 0);
r11160612_no_callback()34 void r11160612_no_callback() {
35   char *x = (char *)malloc(12);
36   const_ptr_and_callback_def_param_null(0, x, 12);
37 } // no-silence-warning{{Potential leak of memory pointed to by 'x' [unix.Malloc]}}
38 
39 #define ZERO_SIZE_PTR ((void *)16)
40 
test_delete_ZERO_SIZE_PTR()41 void test_delete_ZERO_SIZE_PTR() {
42   int *Ptr = (int *)ZERO_SIZE_PTR;
43   // ZERO_SIZE_PTR is specially handled but only for malloc family
44   delete Ptr; // no-silence-warning{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
45               // unix-silenced-warning@-1{{Argument to 'delete' is a constant address (16), which is not memory allocated by 'new' [cplusplus.NewDelete]}}
46 }
47 
48 // deadstore-silenced-no-diagnostics
49 
foo()50 int foo() {
51   int x = 42;
52   return x;
53 }
54 
g()55 void g() {
56   int y;
57   y = 7;
58   int x = foo();
59   y = 10;
60 }
61