Debian Codegraph @

clang/test/Sema/attr-counted-by-vla.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2
3 #define __counted_by(f) __attribute__((counted_by(f)))
4
5 struct bar;
6
7 struct not_found {
8 int count;
9 struct bar *fam[] __counted_by(bork); // expected-error {{use of undeclared identifier 'bork'}}
10 };
11
12 struct no_found_count_not_in_substruct {
13 unsigned long flags;
14 unsigned char count; // expected-note {{'count' declared here}}
15 struct A {
16 int dummy;
17 int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
18 } a;
19 };
20
21 struct not_found_count_not_in_unnamed_substruct {
22 unsigned char count; // expected-note {{'count' declared here}}
23 struct {
24 int dummy;
25 int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
26 } a;
27 };
28
29 struct not_found_count_not_in_unnamed_substruct_2 {
30 struct {
31 unsigned char count; // expected-note {{'count' declared here}}
32 };
33 struct {
34 int dummy;
35 int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the annotated flexible array}}
36 } a;
37 };
38
39 struct not_found_count_in_other_unnamed_substruct {
40 struct {
41 unsigned char count;
42 } a1;
43
44 struct {
45 int dummy;
46 int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}}
47 };
48 };
49
50 struct not_found_count_in_other_substruct {
51 struct _a1 {
52 unsigned char count;
53 } a1;
54
55 struct {
56 int dummy;
57 int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}}
58 };
59 };
60
61 struct not_found_count_in_other_substruct_2 {
62 struct _a2 {
63 unsigned char count;
64 } a2;
65
66 int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}}
67 };
68
69 struct not_found_suggest {
70 int bork;
71 struct bar *fam[] __counted_by(blork); // expected-error {{use of undeclared identifier 'blork'}}
72 };
73
74 int global; // expected-note {{'global' declared here}}
75
76 struct found_outside_of_struct {
77 int bork;
78 struct bar *fam[] __counted_by(global); // expected-error {{field 'global' in 'counted_by' not inside structure}}
79 };
80
81 struct self_referrential {
82 int bork;
83 struct bar *self[] __counted_by(self); // expected-error {{use of undeclared identifier 'self'}}
84 };
85
86 struct non_int_count {
87 double dbl_count;
88 struct bar *fam[] __counted_by(dbl_count); // expected-error {{'counted_by' requires a non-boolean integer type argument}}
89 };
90
91 struct array_of_ints_count {
92 int integers[2];
93 struct bar *fam[] __counted_by(integers); // expected-error {{'counted_by' requires a non-boolean integer type argument}}
94 };
95
96 struct not_a_fam {
97 int count;
98 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct bar' is an incomplete type}}
99 struct bar *non_fam __counted_by(count);
100 };
101
102 struct not_a_c99_fam {
103 int count;
104 struct bar *non_c99_fam[0] __counted_by(count); // expected-error {{'counted_by' on arrays only applies to C99 flexible array members}}
105 };
106
107 struct annotated_with_anon_struct {
108 unsigned long flags;
109 struct {
110 unsigned char count;
111 int array[] __counted_by(crount); // expected-error {{use of undeclared identifier 'crount'}}
112 };
113 };
114
115 //==============================================================================
116 // __counted_by on a struct VLA with element type that has unknown size
117 //==============================================================================
118
119 struct size_unknown; // expected-note 2{{forward declaration of 'struct size_unknown'}}
120 struct on_member_arr_incomplete_ty_ty_pos {
121 int count;
122 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
123 // expected-error@+1{{array has incomplete element type 'struct size_unknown'}}
124 struct size_unknown buf[] __counted_by(count);
125 };
126
127 struct on_member_arr_incomplete_const_ty_ty_pos {
128 int count;
129 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
130 // expected-error@+1{{array has incomplete element type 'const struct size_unknown'}}
131 const struct size_unknown buf[] __counted_by(count);
132 };
133
134 struct on_member_arr_void_ty_ty_pos {
135 int count;
136 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
137 // expected-error@+1{{array has incomplete element type 'void'}}
138 void buf[] __counted_by(count);
139 };
140
141 typedef void(fn_ty)(int);
142
143 struct on_member_arr_fn_ptr_ty {
144 int count;
145 // An Array of function pointers is allowed
146 fn_ty* buf[] __counted_by(count);
147 };
148
149 struct on_member_arr_fn_ty {
150 int count;
151 // An array of functions is not allowed.
152 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
153 // expected-error@+1{{'buf' declared as array of functions of type 'fn_ty' (aka 'void (int)')}}
154 fn_ty buf[] __counted_by(count);
155 };
156
157
158 // `buffer_of_structs_with_unnannotated_vla`,
159 // `buffer_of_structs_with_annotated_vla`, and
160 // `buffer_of_const_structs_with_annotated_vla` are currently prevented because
161 // computing the size of `Arr` at runtime would require an O(N) walk of `Arr`
162 // elements to take into account the length of the VLA in each struct instance.
163
164 struct has_unannotated_VLA {
165 int count;
166 char buffer[];
167 };
168
169 struct has_annotated_VLA {
170 int count;
171 char buffer[] __counted_by(count);
172 };
173
174 struct buffer_of_structs_with_unnannotated_vla {
175 int count;
176 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
177 // 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}}
178 struct has_unannotated_VLA Arr[] __counted_by(count);
179 };
180
181
182 struct buffer_of_structs_with_annotated_vla {
183 int count;
184 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
185 // 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}}
186 struct has_annotated_VLA Arr[] __counted_by(count);
187 };
188
189 struct buffer_of_const_structs_with_annotated_vla {
190 int count;
191 // Treating this as a warning is a temporary fix for existing attribute adopters. It **SHOULD BE AN ERROR**.
192 // Make sure the `const` qualifier is printed when printing the element type.
193 // 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}}
194 const struct has_annotated_VLA Arr[] __counted_by(count);
195 };
196