| 1 |
/* PR c/102103 - missing warning comparing array address to null |
| 2 |
{ dg-do compile } |
| 3 |
{ dg-options "-Wall" } */ |
| 4 |
|
| 5 |
#if __cplusplus < 201103L |
| 6 |
# define nullptr __null |
| 7 |
#endif |
| 8 |
|
| 9 |
struct A |
| 10 |
{ |
| 11 |
void f (); |
| 12 |
virtual void vf (); |
| 13 |
virtual void pvf () = 0; |
| 14 |
|
| 15 |
static void sf (); |
| 16 |
|
| 17 |
int *p; |
| 18 |
int a[2]; |
| 19 |
}; |
| 20 |
|
| 21 |
void T (bool); |
| 22 |
|
| 23 |
void warn_memptr_if () |
| 24 |
{ |
| 25 |
// Exercise warnings for addresses of nonstatic member functions. |
| 26 |
// On targets with TARGET_PTRMEMFUNC_VBIT_LOCATION == |
| 27 |
// ptrmemfunc_vbit_in_delta, cp_build_binary_op recurses to compare |
| 28 |
// the pfn from the ptrmemfunc with null, so we get two warnings. |
| 29 |
// This matches both. ??? Should we disable one of them? |
| 30 |
if (&A::f == 0) // { dg-warning "A::f" } |
| 31 |
T (0); |
| 32 |
|
| 33 |
if (&A::vf) // { dg-warning "-Waddress" } |
| 34 |
T (0); |
| 35 |
|
| 36 |
if (&A::pvf != 0) // { dg-warning "-Waddress" } |
| 37 |
T (0); |
| 38 |
|
| 39 |
// Exercise warnings for addresses of static member functions. |
| 40 |
if (&A::sf == 0) // { dg-warning "-Waddress" } |
| 41 |
T (0); |
| 42 |
|
| 43 |
if (&A::sf) // { dg-warning "-Waddress" } |
| 44 |
T (0); |
| 45 |
|
| 46 |
// Exercise warnings for addresses of nonstatic data members. |
| 47 |
if (&A::p == 0) // { dg-warning "the address '&A::p'" } |
| 48 |
T (0); |
| 49 |
|
| 50 |
if (&A::a == nullptr) // { dg-warning "-Waddress" } |
| 51 |
T (0); |
| 52 |
} |
| 53 |
|
| 54 |
void warn_memptr_bool () |
| 55 |
{ |
| 56 |
// Exercise warnings for addresses of nonstatic member functions. |
| 57 |
T (&A::f == 0); // { dg-warning "-Waddress" } |
| 58 |
T (&A::vf); // { dg-warning "-Waddress" } |
| 59 |
T (&A::pvf != 0); // { dg-warning "-Waddress" } |
| 60 |
|
| 61 |
// Exercise warnings for addresses of static member functions. |
| 62 |
T (&A::sf == 0); // { dg-warning "-Waddress" } |
| 63 |
T (&A::sf); // { dg-warning "-Waddress" } |
| 64 |
|
| 65 |
// Exercise warnings for addresses of nonstatic data members. |
| 66 |
T (&A::p == 0); // { dg-warning "-Waddress" } |
| 67 |
T (&A::a == nullptr); // { dg-warning "-Waddress" } |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/* Verify that no warnings are issued for a dependent expression in |
| 72 |
a template. */ |
| 73 |
|
| 74 |
template <int> |
| 75 |
struct B |
| 76 |
{ |
| 77 |
// This is why. |
| 78 |
struct F { void* operator& () const { return 0; } } f; |
| 79 |
}; |
| 80 |
|
| 81 |
template <class Type, int N> |
| 82 |
void nowarn_dependent (Type targ) |
| 83 |
{ |
| 84 |
T (&Type::x == 0); |
| 85 |
T (&targ == 0); |
| 86 |
|
| 87 |
Type tarr[1]; |
| 88 |
T (&tarr[0] == nullptr); |
| 89 |
|
| 90 |
T (&B<N>::f == 0); |
| 91 |
|
| 92 |
/* Like in the case above, the address-of operator could be a member |
| 93 |
of B<N>::vf that returns zero. */ |
| 94 |
T (&B<N>::vf); |
| 95 |
T (&B<N>::pvf != 0); |
| 96 |
T (&B<N>::p == 0); |
| 97 |
T (&B<N>::a == 0); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/* Verify that in an uninstantiated template warnings are not issued |
| 102 |
for dependent expressions but are issued otherwise. */ |
| 103 |
|
| 104 |
template <class Type> |
| 105 |
void warn_non_dependent (Type targ, Type *tptr, int i) |
| 106 |
{ |
| 107 |
/* The address of a pointer to a dependent type cannot be null but |
| 108 |
the warning doesn't have a chance to see it. */ |
| 109 |
T (&tptr == 0); // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } } |
| 110 |
T (&i == 0); // { dg-warning "-Waddress" } |
| 111 |
|
| 112 |
int iarr[1]; |
| 113 |
T (&iarr == 0); // { dg-warning "-Waddress" } |
| 114 |
T (&*iarr != 0); // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } } |
| 115 |
T (&iarr[0] == 0); // { dg-warning "-Waddress" } |
| 116 |
|
| 117 |
Type tarr[1]; |
| 118 |
T (&tarr == nullptr); // { dg-warning "-Waddress" "pr102378" { xfail *-*-* } } |
| 119 |
} |