1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -analyzer-config c++-inlining=destructors -std=c++11 -verify -Wno-tautological-undefined-compare %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=plist-multi-file -analyzer-config c++-inlining=destructors -std=c++11 %s -o %t.plist -Wno-tautological-undefined-compare
3 // RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/path-notes.cpp.plist -
7 static void use(int *p
) {
8 *p
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
9 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
14 // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
15 // expected-note@-2 {{Calling 'Foo::use'}}
19 static int *globalPtr
;
25 // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
26 // expected-note@-2 {{Calling constructor for 'Foo'}}
33 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
34 } // expected-note {{Calling '~Bar'}}
37 void testAnonymous() {
41 *p
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'p')}}
42 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'p')}}
47 // expected-note@-1 {{Passing null pointer value via 1st parameter 'p'}}
48 // expected-note@-2 {{Calling 'method'}}
52 // A simplified version of std::move.
55 return static_cast<T
&&>(obj
);
63 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
64 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
67 Dereferencer(const Dereferencer
&Other
) {
68 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
69 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
72 Dereferencer(Dereferencer
&&Other
) {
73 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
74 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
77 void operator=(const Dereferencer
&Other
) {
78 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
79 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
82 void operator=(Dereferencer
&&Other
) {
83 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
84 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
88 *globalPtr
= 1; // expected-warning {{Dereference of null pointer (loaded from variable 'globalPtr')}}
89 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'globalPtr')}}
97 class MovableWrapper
{
100 MovableWrapper() = default;
102 MovableWrapper(MovableWrapper
&&Other
) = default;
103 // expected-note@-1 {{Calling move constructor for 'Dereferencer'}}
105 MovableWrapper
&operator=(MovableWrapper
&&Other
) = default;
106 // expected-note@-1 {{Calling move assignment operator for 'Dereferencer'}}
109 void testDefaultConstruction() {
111 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
113 // expected-note@-1 {{Calling implicit default constructor for 'Wrapper'}}
114 // expected-note@-2 {{Calling default constructor for 'Dereferencer'}}
117 void testCopyConstruction(const Wrapper
&input
) {
119 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
121 // expected-note@-1 {{Calling implicit copy constructor for 'Wrapper'}}
122 // expected-note@-2 {{Calling copy constructor for 'Dereferencer'}}
125 void testMoveConstruction(MovableWrapper
&&input
) {
127 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
128 MovableWrapper w
{move(input
)};
129 // expected-note@-1 {{Calling defaulted move constructor for 'MovableWrapper'}}
132 void testCopyAssignment(const Wrapper
&input
) {
135 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
137 // expected-note@-1 {{Calling implicit copy assignment operator for 'Wrapper'}}
138 // expected-note@-2 {{Calling copy assignment operator for 'Dereferencer'}}
141 void testMoveAssignment(MovableWrapper
&&input
) {
144 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
146 // expected-note@-1 {{Calling defaulted move assignment operator for 'MovableWrapper'}}
149 void testDestruction() {
152 // expected-note@-1 {{Null pointer value stored to 'globalPtr'}}
154 // expected-note@-1 {{Calling implicit destructor for 'Wrapper'}}
155 // expected-note@-2 {{Calling '~Dereferencer'}}
158 namespace ReturnZeroNote
{
161 // expected-note@-1 {{Returning zero}}
164 const int &getZeroByRef() {
167 // expected-note@-1 {{The value 0 is assigned to 'zeroVar'}}
169 // expected-note@-1 {{Returning zero (reference to 'zeroVar')}}
173 int problem
= 1 / getZero(); // expected-warning {{Division by zero}}
174 // expected-note@-1 {{Calling 'getZero'}}
175 // expected-note@-2 {{Returning from 'getZero'}}
176 // expected-note@-3 {{Division by zero}}
180 int problem
= 1 / getZeroByRef(); // expected-warning {{Division by zero}}
181 // expected-note@-1 {{Calling 'getZeroByRef'}}
182 // expected-note@-2 {{Returning from 'getZeroByRef'}}
183 // expected-note@-3 {{Division by zero}}
187 int &returnNullReference() {
189 // expected-note@-1 {{'x' initialized to a null pointer value}}
190 return *x
; // expected-warning{{Returning null reference}}
191 // expected-note@-1 {{Returning null reference}}
194 struct FooWithInitializer
{
196 FooWithInitializer(int *p
) : ptr(p
) { // expected-note {{Null pointer value stored to 'f.ptr'}}
197 *ptr
= 1; // expected-note {{Dereference of null pointer (loaded from field 'ptr')}}
198 // expected-warning@-1 {{Dereference of null pointer (loaded from field 'ptr')}}
202 void testPathNoteOnInitializer() {
203 int *p
= 0; // expected-note {{'p' initialized to a null pointer value}}
205 FooWithInitializer
f(p
); // expected-note {{Passing null pointer value via 1st parameter 'p'}}
206 // expected-note@-1 {{Calling constructor for 'FooWithInitializer'}}
209 int testNonPrintableAssignment(int **p
) {
210 int *&y
= *p
; // expected-note {{'y' initialized here}}
211 y
= 0; // expected-note {{Storing null pointer value}}
212 return *y
; // expected-warning {{Dereference of null pointer (loaded from variable 'y')}}
213 // expected-note@-1 {{Dereference of null pointer (loaded from variable 'y')}}
216 struct Base
{ int *x
; };
217 struct Derived
: public Base
{};
219 void test(Derived d
) {
220 d
.x
= 0; //expected-note {{Null pointer value stored to 'd.x'}}
221 *d
.x
= 1; // expected-warning {{Dereference of null pointer (loaded from field 'x')}}
222 // expected-note@-1 {{Dereference of null pointer (loaded from field 'x')}}
230 void testGetDerefExprOnMemberExprWithADot();
233 void Owner::testGetDerefExprOnMemberExprWithADot() {
234 if (arr
) // expected-note {{Assuming field 'arr' is null}}
235 // expected-note@-1 {{Taking false branch}}
237 arr
[1].x
= 1; //expected-warning {{Dereference of null pointer}}
238 //expected-note@-1 {{Dereference of null pointer}}
241 void testGetDerefExprOnMemberExprWithADot() {
242 Owner::Wrapper
*arr
; // expected-note {{'arr' declared without an initial value}}
243 arr
[2].x
= 1; // expected-warning {{Dereference of undefined pointer value}}
244 // expected-note@-1 {{Dereference of undefined pointer value}}
253 const A
& testDeclRefExprToReferenceInGetDerefExpr(const A
*ptr
) {
254 const A
& val
= *ptr
; //expected-note {{'val' initialized here}}
256 // This is not valid C++; if 'ptr' were null, creating 'ref' would be illegal.
257 // However, this is not checked at runtime, so this branch is actually
259 if (&val
== 0) { //expected-note {{Assuming pointer value is null}}
260 // expected-note@-1 {{Taking true branch}}
261 val
.bar(); // expected-warning {{Called C++ object pointer is null}}
262 // expected-note@-1 {{Called C++ object pointer is null}}
268 int generateNoteOnDefaultArgument(int one
, int two
= 0) {
269 return one
/two
; // expected-warning {{Division by zero}}
270 // expected-note@-1 {{Division by zero}}
272 int callGenerateNoteOnDefaultArgument(int o
) {
273 return generateNoteOnDefaultArgument(o
); //expected-note{{Calling 'generateNoteOnDefaultArgument'}}
274 //expected-note@-1 {{Passing the value 0 via 2nd parameter 'two'}}
281 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
282 // expected-note@-1 {{Dereference of null pointer}}
291 // expected-note@-1 {{Calling '~Inner'}}
295 void test(Outer
*outer
) {
297 // expected-note@-1 {{Calling '~Outer'}}