1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-undefined-compare %s
3*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wno-tautological-compare -Wtautological-undefined-compare %s
4*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare %s
5*67e74705SXin Li
test1(int & x)6*67e74705SXin Li void test1(int &x) {
7*67e74705SXin Li if (x == 1) { }
8*67e74705SXin Li if (&x == 0) { }
9*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
10*67e74705SXin Li if (&x != 0) { }
11*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
12*67e74705SXin Li }
13*67e74705SXin Li
14*67e74705SXin Li class test2 {
test2()15*67e74705SXin Li test2() : x(y) {}
16*67e74705SXin Li
foo()17*67e74705SXin Li void foo() {
18*67e74705SXin Li if (this == 0) { }
19*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to false}}
20*67e74705SXin Li if (this != 0) { }
21*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to true}}
22*67e74705SXin Li }
23*67e74705SXin Li
bar()24*67e74705SXin Li void bar() {
25*67e74705SXin Li if (x == 1) { }
26*67e74705SXin Li if (&x == 0) { }
27*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
28*67e74705SXin Li if (&x != 0) { }
29*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
30*67e74705SXin Li }
31*67e74705SXin Li
32*67e74705SXin Li int &x;
33*67e74705SXin Li int y;
34*67e74705SXin Li };
35*67e74705SXin Li
36*67e74705SXin Li namespace function_return_reference {
37*67e74705SXin Li int& get_int();
38*67e74705SXin Li // expected-note@-1 4{{'get_int' returns a reference}}
39*67e74705SXin Li class B {
40*67e74705SXin Li public:
41*67e74705SXin Li static int &stat();
42*67e74705SXin Li // expected-note@-1 4{{'stat' returns a reference}}
43*67e74705SXin Li int &get();
44*67e74705SXin Li // expected-note@-1 8{{'get' returns a reference}}
45*67e74705SXin Li };
46*67e74705SXin Li
test()47*67e74705SXin Li void test() {
48*67e74705SXin Li if (&get_int() == 0) {}
49*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
50*67e74705SXin Li if (&(get_int()) == 0) {}
51*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
52*67e74705SXin Li
53*67e74705SXin Li if (&get_int() != 0) {}
54*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
55*67e74705SXin Li if (&(get_int()) != 0) {}
56*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
57*67e74705SXin Li
58*67e74705SXin Li if (&B::stat() == 0) {}
59*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
60*67e74705SXin Li if (&(B::stat()) == 0) {}
61*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
62*67e74705SXin Li
63*67e74705SXin Li if (&B::stat() != 0) {}
64*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
65*67e74705SXin Li if (&(B::stat()) != 0) {}
66*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
67*67e74705SXin Li
68*67e74705SXin Li B b;
69*67e74705SXin Li if (&b.get() == 0) {}
70*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
71*67e74705SXin Li if (&(b.get()) == 0) {}
72*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
73*67e74705SXin Li
74*67e74705SXin Li if (&b.get() != 0) {}
75*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
76*67e74705SXin Li if (&(b.get()) != 0) {}
77*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
78*67e74705SXin Li
79*67e74705SXin Li B* b_ptr = &b;
80*67e74705SXin Li if (&b_ptr->get() == 0) {}
81*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
82*67e74705SXin Li if (&(b_ptr->get()) == 0) {}
83*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
84*67e74705SXin Li
85*67e74705SXin Li if (&b_ptr->get() != 0) {}
86*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
87*67e74705SXin Li if (&(b_ptr->get()) != 0) {}
88*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
89*67e74705SXin Li
90*67e74705SXin Li int& (B::*m_ptr)() = &B::get;
91*67e74705SXin Li if (&(b.*m_ptr)() == 0) {}
92*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
93*67e74705SXin Li if (&((b.*m_ptr)()) == 0) {}
94*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
95*67e74705SXin Li
96*67e74705SXin Li if (&(b.*m_ptr)() != 0) {}
97*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
98*67e74705SXin Li if (&((b.*m_ptr)()) != 0) {}
99*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
100*67e74705SXin Li
101*67e74705SXin Li int& (*f_ptr)() = &get_int;
102*67e74705SXin Li if (&(*f_ptr)() == 0) {}
103*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
104*67e74705SXin Li if (&((*f_ptr)()) == 0) {}
105*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
106*67e74705SXin Li
107*67e74705SXin Li if (&(*f_ptr)() != 0) {}
108*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
109*67e74705SXin Li if (&((*f_ptr)()) != 0) {}
110*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
111*67e74705SXin Li }
112*67e74705SXin Li }
113*67e74705SXin Li
114*67e74705SXin Li namespace macros {
115*67e74705SXin Li #define assert(x) if (x) {}
116*67e74705SXin Li
test(int & x)117*67e74705SXin Li void test(int &x) {
118*67e74705SXin Li assert(&x != 0);
119*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
120*67e74705SXin Li assert(&x == 0);
121*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
122*67e74705SXin Li assert(&x != 0 && "Expecting valid reference");
123*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to true}}
124*67e74705SXin Li assert(&x == 0 && "Expecting invalid reference");
125*67e74705SXin Li // expected-warning@-1{{reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false}}
126*67e74705SXin Li }
127*67e74705SXin Li
128*67e74705SXin Li class S {
test()129*67e74705SXin Li void test() {
130*67e74705SXin Li assert(this != 0);
131*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to true}}
132*67e74705SXin Li assert(this == 0);
133*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to false}}
134*67e74705SXin Li assert(this != 0 && "Expecting valid reference");
135*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to true}}
136*67e74705SXin Li assert(this == 0 && "Expecting invalid reference");
137*67e74705SXin Li // expected-warning@-1{{'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to false}}
138*67e74705SXin Li }
139*67e74705SXin Li };
140*67e74705SXin Li }
141