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