| 1 |
/* PR c/69002 */ |
| 2 |
/* Test we diagnose accessing elements of atomic structures or unions, |
| 3 |
which is undefined behavior (C11 6.5.2.3#5). */ |
| 4 |
/* { dg-do compile } */ |
| 5 |
/* { dg-options "-std=c11 -pedantic-errors" } */ |
| 6 |
|
| 7 |
struct S { int x; }; |
| 8 |
union U { int x; }; |
| 9 |
|
| 10 |
int |
| 11 |
fn1 (_Atomic struct S p) |
| 12 |
{ |
| 13 |
int e = 0 && p.x; |
| 14 |
return p.x + e; /* { dg-warning "accessing a member .x. of an atomic structure" } */ |
| 15 |
} |
| 16 |
|
| 17 |
int |
| 18 |
fn2 (_Atomic struct S *p) |
| 19 |
{ |
| 20 |
int e = 1 || p->x; |
| 21 |
return p->x + e; /* { dg-warning "accessing a member .x. of an atomic structure" } */ |
| 22 |
} |
| 23 |
|
| 24 |
void |
| 25 |
fn3 (_Atomic struct S p, int x) |
| 26 |
{ |
| 27 |
p.x = x; /* { dg-warning "accessing a member .x. of an atomic structure" } */ |
| 28 |
} |
| 29 |
|
| 30 |
void |
| 31 |
fn4 (_Atomic struct S *p, int x) |
| 32 |
{ |
| 33 |
p->x = x; /* { dg-warning "accessing a member .x. of an atomic structure" } */ |
| 34 |
} |
| 35 |
|
| 36 |
int |
| 37 |
fn5 (_Atomic struct S p) |
| 38 |
{ |
| 39 |
/* This is OK: Members can be safely accessed using a non-atomic |
| 40 |
object which is assigned to or from the atomic object. */ |
| 41 |
struct S s = p; |
| 42 |
return s.x; |
| 43 |
} |
| 44 |
|
| 45 |
int |
| 46 |
fn6 (_Atomic struct S *p) |
| 47 |
{ |
| 48 |
struct S s = *p; |
| 49 |
return s.x; |
| 50 |
} |
| 51 |
|
| 52 |
int |
| 53 |
fn7 (_Atomic union U p) |
| 54 |
{ |
| 55 |
int e = 0 && p.x; |
| 56 |
return p.x + e; /* { dg-warning "accessing a member .x. of an atomic union" } */ |
| 57 |
} |
| 58 |
|
| 59 |
int |
| 60 |
fn8 (_Atomic union U *p) |
| 61 |
{ |
| 62 |
int e = 1 || p->x; |
| 63 |
return p->x + e; /* { dg-warning "accessing a member .x. of an atomic union" } */ |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
fn9 (_Atomic union U p, int x) |
| 68 |
{ |
| 69 |
p.x = x; /* { dg-warning "accessing a member .x. of an atomic union" } */ |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
fn10 (_Atomic union U *p, int x) |
| 74 |
{ |
| 75 |
p->x = x; /* { dg-warning "accessing a member .x. of an atomic union" } */ |
| 76 |
} |
| 77 |
|
| 78 |
int |
| 79 |
fn11 (_Atomic union U p) |
| 80 |
{ |
| 81 |
/* This is OK: Members can be safely accessed using a non-atomic |
| 82 |
object which is assigned to or from the atomic object. */ |
| 83 |
union U s = p; |
| 84 |
return s.x; |
| 85 |
} |
| 86 |
|
| 87 |
int |
| 88 |
fn12 (_Atomic union U *p) |
| 89 |
{ |
| 90 |
union U s = *p; |
| 91 |
return s.x; |
| 92 |
} |