Debian Codegraph @

clang/test/Analysis/suppression-attr.cpp

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
2
3 void clang_analyzer_warnIfReached();
4
5 struct Clazz {
6 template <typename T>
7 static void templated_memfn();
8 };
9
10 // This must come before the 'templated_memfn' is defined!
11 static void instantiate() {
12 Clazz::templated_memfn<int>();
13 }
14
15 template <typename T>
16 void Clazz::templated_memfn() {
17 // When we report a bug in a function, we traverse the lexical decl context
18 // of it while looking for suppression attributes to record what source
19 // ranges should the suppression apply to.
20 // In the past, that traversal didn't follow template instantiations, only
21 // primary templates.
22 [[clang::suppress]] clang_analyzer_warnIfReached(); // no-warning
23
24 }
25
26 namespace [[clang::suppress]]
27 suppressed_namespace {
28 int foo() {
29 int *x = 0;
30 return *x;
31 }
32
33 int foo_forward();
34 }
35
36 int suppressed_namespace::foo_forward() {
37 int *x = 0;
38 return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
39 }
40
41 // Another instance of the same namespace.
42 namespace suppressed_namespace {
43 int bar() {
44 int *x = 0;
45 return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
46 }
47 }
48
49 void lambda() {
50 [[clang::suppress]] {
51 auto lam = []() {
52 int *x = 0;
53 return *x;
54 };
55 }
56 }
57
58 class [[clang::suppress]] SuppressedClass {
59 int foo() {
60 int *x = 0;
61 return *x;
62 }
63
64 int bar();
65 };
66
67 int SuppressedClass::bar() {
68 int *x = 0;
69 return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
70 }
71
72 class SuppressedMethodClass {
73 [[clang::suppress]] int foo() {
74 int *x = 0;
75 return *x;
76 }
77
78 [[clang::suppress]] int bar1();
79 int bar2();
80 };
81
82 int SuppressedMethodClass::bar1() {
83 int *x = 0;
84 return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
85 }
86
87 [[clang::suppress]]
88 int SuppressedMethodClass::bar2() {
89 int *x = 0;
90 return *x; // no-warning
91 }