1 // RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-output=text -verify %s
2 
3 struct S {
4   int *x;
5   int y;
6 };
7 
8 S &getSomeReference();
test(S * p)9 void test(S *p) {
10   S &r = *p;   //expected-note {{'r' initialized here}}
11   if (p) return;
12                //expected-note@-1{{Taking false branch}}
13                //expected-note@-2{{Assuming 'p' is null}}
14   r.y = 5; // expected-warning {{Access to field 'y' results in a dereference of a null pointer (loaded from variable 'r')}}
15            // expected-note@-1{{Access to field 'y' results in a dereference of a null pointer (loaded from variable 'r')}}
16 }
17 
testRefParam(int * ptr)18 void testRefParam(int *ptr) {
19 	int &ref = *ptr; // expected-note {{'ref' initialized here}}
20 	if (ptr)
21     // expected-note@-1{{Assuming 'ptr' is null}}
22     // expected-note@-2{{Taking false branch}}
23 		return;
24 
25 	extern void use(int &ref);
26 	use(ref); // expected-warning{{Forming reference to null pointer}}
27             // expected-note@-1{{Forming reference to null pointer}}
28 }
29 
testRefToNullPtr()30 int testRefToNullPtr() {
31   int *p = 0; // expected-note {{'p' initialized to a null pointer value}}
32   int *const &p2 = p; // expected-note{{'p2' initialized here}}
33   int *p3 = p2; // expected-note {{'p3' initialized to a null pointer value}}
34   return *p3; // expected-warning {{Dereference of null pointer}}
35               // expected-note@-1{{Dereference of null pointer}}
36 }
37 
testRefToNullPtr2()38 int testRefToNullPtr2() {
39   int *p = 0; // expected-note {{'p' initialized to a null pointer value}}
40   int *const &p2 = p;// expected-note{{'p2' initialized here}}
41   return *p2; //expected-warning {{Dereference of null pointer}}
42               // expected-note@-1{{Dereference of null pointer}}
43 }