1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,immediate %s
2 // RUN: %clang_cc1 -fsyntax-only -fexperimental-late-parse-attributes %s -verify=expected,late
4 #define __counted_by(f) __attribute__((counted_by(f)))
10 struct bar
*fam
[] __counted_by(bork
); // expected-error {{use of undeclared identifier 'bork'}}
13 struct no_found_count_not_in_substruct
{
15 unsigned char count
; // expected-note {{'count' declared here}}
18 int array
[] __counted_by(count
); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
22 struct not_found_count_not_in_unnamed_substruct
{
23 unsigned char count
; // expected-note {{'count' declared here}}
26 int array
[] __counted_by(count
); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
30 struct not_found_count_not_in_unnamed_substruct_2
{
32 unsigned char count
; // expected-note {{'count' declared here}}
36 int array
[] __counted_by(count
); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
40 struct not_found_count_in_other_unnamed_substruct
{
47 int array
[] __counted_by(count
); // expected-error {{use of undeclared identifier 'count'}}
51 struct not_found_count_in_other_substruct
{
58 int array
[] __counted_by(count
); // expected-error {{use of undeclared identifier 'count'}}
62 struct not_found_count_in_other_substruct_2
{
67 int array
[] __counted_by(count
); // expected-error {{use of undeclared identifier 'count'}}
70 struct not_found_suggest
{
72 struct bar
*fam
[] __counted_by(blork
); // expected-error {{use of undeclared identifier 'blork'}}
75 int global
; // expected-note {{'global' declared here}}
77 struct found_outside_of_struct
{
79 struct bar
*fam
[] __counted_by(global
); // expected-error {{field 'global' in 'counted_by' not inside structure}}
82 struct self_referrential
{
84 // immediate-error@+2{{use of undeclared identifier 'self'}}
85 // late-error@+1{{'counted_by' requires a non-boolean integer type argument}}
86 struct bar
*self
[] __counted_by(self
);
89 struct non_int_count
{
91 struct bar
*fam
[] __counted_by(dbl_count
); // expected-error {{'counted_by' requires a non-boolean integer type argument}}
94 struct array_of_ints_count
{
96 struct bar
*fam
[] __counted_by(integers
); // expected-error {{'counted_by' requires a non-boolean integer type argument}}
101 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct bar' is an incomplete type}}
102 struct bar
*non_fam
__counted_by(count
);
105 struct not_a_c99_fam
{
107 struct bar
*non_c99_fam
[0] __counted_by(count
); // expected-error {{'counted_by' on arrays only applies to C99 flexible array members}}
110 struct annotated_with_anon_struct
{
114 int array
[] __counted_by(crount
); // expected-error {{use of undeclared identifier 'crount'}}
118 //==============================================================================
119 // __counted_by on a struct VLA with element type that has unknown size
120 //==============================================================================
122 struct size_unknown
; // expected-note 2{{forward declaration of 'struct size_unknown'}}
123 struct on_member_arr_incomplete_ty_ty_pos
{
125 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
126 // expected-error@+1{{array has incomplete element type 'struct size_unknown'}}
127 struct size_unknown buf
[] __counted_by(count
);
130 struct on_member_arr_incomplete_const_ty_ty_pos
{
132 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
133 // expected-error@+1{{array has incomplete element type 'const struct size_unknown'}}
134 const struct size_unknown buf
[] __counted_by(count
);
137 struct on_member_arr_void_ty_ty_pos
{
139 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
140 // expected-error@+1{{array has incomplete element type 'void'}}
141 void buf
[] __counted_by(count
);
144 typedef void(fn_ty
)(int);
146 struct on_member_arr_fn_ptr_ty
{
148 // An Array of function pointers is allowed
149 fn_ty
* buf
[] __counted_by(count
);
152 struct on_member_arr_fn_ty
{
154 // An array of functions is not allowed.
155 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
156 // expected-error@+1{{'buf' declared as array of functions of type 'fn_ty' (aka 'void (int)')}}
157 fn_ty buf
[] __counted_by(count
);
161 // `buffer_of_structs_with_unnannotated_vla`,
162 // `buffer_of_structs_with_annotated_vla`, and
163 // `buffer_of_const_structs_with_annotated_vla` are currently prevented because
164 // computing the size of `Arr` at runtime would require an O(N) walk of `Arr`
165 // elements to take into account the length of the VLA in each struct instance.
167 struct has_unannotated_VLA
{
172 struct has_annotated_VLA
{
174 char buffer
[] __counted_by(count
);
177 struct buffer_of_structs_with_unnannotated_vla
{
179 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
180 // expected-warning@+1{{'counted_by' should not be applied to an array with element of unknown size because 'struct has_unannotated_VLA' is a struct type with a flexible array member. This will be an error in a future compiler version}}
181 struct has_unannotated_VLA Arr
[] __counted_by(count
);
185 struct buffer_of_structs_with_annotated_vla
{
187 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
188 // expected-warning@+1{{'counted_by' should not be applied to an array with element of unknown size because 'struct has_annotated_VLA' is a struct type with a flexible array member. This will be an error in a future compiler version}}
189 struct has_annotated_VLA Arr
[] __counted_by(count
);
192 struct buffer_of_const_structs_with_annotated_vla
{
194 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
195 // Make sure the `const` qualifier is printed when printing the element type.
196 // expected-warning@+1{{'counted_by' should not be applied to an array with element of unknown size because 'const struct has_annotated_VLA' is a struct type with a flexible array member. This will be an error in a future compiler version}}
197 const struct has_annotated_VLA Arr
[] __counted_by(count
);