| 1 |
/* PR c/102103 - missing warning comparing array address to null |
| 2 |
{ dg-do compile } |
| 3 |
Verify -Waddress for member arrays of structs and notes. |
| 4 |
{ dg-options "-Wall" } */ |
| 5 |
|
| 6 |
#if __cplusplus < 201103L |
| 7 |
# define nullptr __null |
| 8 |
#endif |
| 9 |
|
| 10 |
void T (bool); |
| 11 |
|
| 12 |
struct A |
| 13 |
{ |
| 14 |
int n; |
| 15 |
int ia[]; // { dg-message "'A::ia' declared here" } |
| 16 |
}; |
| 17 |
|
| 18 |
struct B |
| 19 |
{ |
| 20 |
A a[3]; // { dg-message "'B::a' declared here" } |
| 21 |
}; |
| 22 |
|
| 23 |
struct C |
| 24 |
{ |
| 25 |
B b[3]; // { dg-message "'C::b' declared here" } |
| 26 |
}; |
| 27 |
|
| 28 |
struct D |
| 29 |
{ |
| 30 |
C c[3]; // { dg-message "'D::c' declared here" } |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
void test_waddress_1d () |
| 35 |
{ |
| 36 |
D d[2]; // { dg-message "'d' declared here" } |
| 37 |
|
| 38 |
T (d); // { dg-warning "address of 'd'" } |
| 39 |
T (d == nullptr); // { dg-warning "address of 'd'" } |
| 40 |
T (&d); // { dg-warning "address of 'd'" } |
| 41 |
T (d->c); // { dg-warning "address of 'D::c'" } |
| 42 |
T (d->c != nullptr); // { dg-warning "address of 'D::c'" } |
| 43 |
T (d->c->b); // { dg-warning "address of 'C::b'" } |
| 44 |
T (d->c[1].b->a); // { dg-warning "address of 'B::a'" } |
| 45 |
T (d->c->b[2].a->ia); // { dg-warning "address of 'A::ia'" } |
| 46 |
|
| 47 |
if (d->c->b[2].a[1].ia) // { dg-warning "address of 'A::ia'" } |
| 48 |
T (0); |
| 49 |
|
| 50 |
if (bool b = d->c->b[1].a) // { dg-warning "address of 'B::a'" } |
| 51 |
T (b); |
| 52 |
|
| 53 |
/* The following is represented as a declaration of P followed |
| 54 |
by an if statement and so it isn't diagnosed. It's not clear |
| 55 |
that it should be since the pointer is then used. |
| 56 |
void *p = d->c->b[2].a; |
| 57 |
if (p) ... |
| 58 |
*/ |
| 59 |
if (void *p = d->c->b[2].a) // { dg-warning "address of 'A::ia'" "" { xfail *-*-* } } |
| 60 |
T (p); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void test_waddress_2d (int i) |
| 65 |
{ |
| 66 |
D d[2][3]; // { dg-message "'d' declared here" } |
| 67 |
|
| 68 |
T (d); // { dg-warning "address of 'd'" } |
| 69 |
T (d == nullptr); // { dg-warning "address of 'd'" } |
| 70 |
T (&d); // { dg-warning "address of 'd'" } |
| 71 |
T (*d); // { dg-warning "address of 'd'" } |
| 72 |
T (d[1] != nullptr); // { dg-warning "address of 'd'" } |
| 73 |
T (&d[1]->c); // { dg-warning "address of 'D::c'" } |
| 74 |
T (d[1]->c); // { dg-warning "address of 'D::c'" } |
| 75 |
T (d[1]->c == nullptr); // { dg-warning "address of 'D::c'" } |
| 76 |
T (d[i]->c[1].b); // { dg-warning "address of 'C::b'" } |
| 77 |
T ((*(d + i))->c->b->a); // { dg-warning "address of 'B::a'" } |
| 78 |
T (d[1][2].c->b->a->ia); // { dg-warning "address of 'A::ia'" } |
| 79 |
} |