| 1 |
/* Testing the correct usage of attribute counted_by in c23, multiple |
| 2 |
* definitions of the same tag in same or different scopes. |
| 3 |
* { dg-do compile } |
| 4 |
* { dg-options "-std=c23" } |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Allowed redefinitions of the same struct in the same scope, with the |
| 8 |
same counted_by attribute. */ |
| 9 |
struct f { |
| 10 |
int b; |
| 11 |
int c; |
| 12 |
int a[] __attribute__ ((counted_by (b))); }; |
| 13 |
struct f { |
| 14 |
int b; |
| 15 |
int c; |
| 16 |
int a[] __attribute__ ((counted_by (b))); }; |
| 17 |
struct f { |
| 18 |
int b; |
| 19 |
int c; |
| 20 |
int a[]; }; /* { dg-error "redefinition of struct or union" } */ |
| 21 |
|
| 22 |
/* Error when the counted_by attribute is defined differently. */ |
| 23 |
struct f { |
| 24 |
int b; |
| 25 |
int c; |
| 26 |
int a[] __attribute__ ((counted_by (c))); }; /* { dg-error "redefinition of struct or union" } */ |
| 27 |
|
| 28 |
struct h { |
| 29 |
int b; |
| 30 |
int c; |
| 31 |
int a[] __attribute__ ((counted_by (b))); } p; |
| 32 |
|
| 33 |
void test (void) |
| 34 |
{ |
| 35 |
struct h { |
| 36 |
int b; |
| 37 |
int c; |
| 38 |
int a[] __attribute__ ((counted_by (b))); } x; |
| 39 |
|
| 40 |
p = x; |
| 41 |
} |
| 42 |
|
| 43 |
void test1 (void) |
| 44 |
{ |
| 45 |
struct h { |
| 46 |
int b; |
| 47 |
int c; |
| 48 |
int a[] __attribute__ ((counted_by (c))); } y; |
| 49 |
|
| 50 |
p = y; /* { dg-error "incompatible types when assigning to type" } */ |
| 51 |
} |
| 52 |
|
| 53 |
struct nested_f { |
| 54 |
struct { |
| 55 |
union { |
| 56 |
int b; |
| 57 |
float f; |
| 58 |
}; |
| 59 |
int n; |
| 60 |
}; |
| 61 |
char c[] __attribute__ ((counted_by (b))); |
| 62 |
}; |
| 63 |
|
| 64 |
struct nested_f { |
| 65 |
struct { |
| 66 |
union { |
| 67 |
int b; |
| 68 |
float f; |
| 69 |
}; |
| 70 |
int n; |
| 71 |
}; |
| 72 |
char c[] __attribute__ ((counted_by (b))); |
| 73 |
}; |
| 74 |
|
| 75 |
struct nested_f { |
| 76 |
struct { |
| 77 |
union { |
| 78 |
int b; |
| 79 |
float f; |
| 80 |
}; |
| 81 |
int n; |
| 82 |
}; |
| 83 |
char c[] __attribute__ ((counted_by (n))); |
| 84 |
}; /* { dg-error "redefinition of struct or union" } */ |
| 85 |
|
| 86 |
struct nested_h { |
| 87 |
struct { |
| 88 |
union { |
| 89 |
int b; |
| 90 |
float f; |
| 91 |
}; |
| 92 |
int n; |
| 93 |
}; |
| 94 |
char c[] __attribute__ ((counted_by (b))); |
| 95 |
} nested_p; |
| 96 |
|
| 97 |
void test_2 (void) |
| 98 |
{ |
| 99 |
struct nested_h { |
| 100 |
struct { |
| 101 |
union { |
| 102 |
int b; |
| 103 |
float f; |
| 104 |
}; |
| 105 |
int n; |
| 106 |
}; |
| 107 |
char c[] __attribute__ ((counted_by (b))); |
| 108 |
} nested_x; |
| 109 |
|
| 110 |
nested_p = nested_x; |
| 111 |
} |
| 112 |
|
| 113 |
void test_3 (void) |
| 114 |
{ |
| 115 |
struct nested_h { |
| 116 |
struct { |
| 117 |
union { |
| 118 |
int b; |
| 119 |
float f; |
| 120 |
}; |
| 121 |
int n; |
| 122 |
}; |
| 123 |
char c[] __attribute__ ((counted_by (n))); |
| 124 |
} nested_y; |
| 125 |
|
| 126 |
nested_p = nested_y; /* { dg-error "incompatible types when assigning to type" } */ |
| 127 |
} |