Debian Codegraph @

clang/test/Sema/attr-counted-by-struct-ptrs.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2
3 #define __counted_by(f) __attribute__((counted_by(f)))
4
5 struct size_unknown;
6 struct size_known {
7 int field;
8 };
9
10 typedef void(*fn_ptr_ty)(void);
11
12 //==============================================================================
13 // __counted_by on struct member pointer in decl attribute position
14 //==============================================================================
15
16 struct on_member_pointer_complete_ty {
17 int count;
18 struct size_known * buf __counted_by(count);
19 };
20
21 struct on_member_pointer_incomplete_ty {
22 int count;
23 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct size_unknown' is an incomplete type}}
24 struct size_unknown * buf __counted_by(count);
25 };
26
27 struct on_member_pointer_const_incomplete_ty {
28 int count;
29 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'const struct size_unknown' is an incomplete type}}
30 const struct size_unknown * buf __counted_by(count);
31 };
32
33 struct on_member_pointer_void_ty {
34 int count;
35 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void' is an incomplete type}}
36 void* buf __counted_by(count);
37 };
38
39 struct on_member_pointer_fn_ptr_ty {
40 int count;
41 // buffer of `count` function pointers is allowed
42 void (**fn_ptr)(void) __counted_by(count);
43 };
44
45 struct on_member_pointer_fn_ptr_ty_ptr_ty {
46 int count;
47 // buffer of `count` function pointers is allowed
48 fn_ptr_ty* fn_ptr __counted_by(count);
49 };
50
51 struct on_member_pointer_fn_ty {
52 int count;
53 // buffer of `count` functions is not allowed
54 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
55 void (*fn_ptr)(void) __counted_by(count);
56 };
57
58 struct on_member_pointer_fn_ptr_ty_ty {
59 int count;
60 // buffer of `count` functions is not allowed
61 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
62 fn_ptr_ty fn_ptr __counted_by(count);
63 };
64
65 struct has_unannotated_vla {
66 int count;
67 int buffer[];
68 };
69
70 struct on_member_pointer_struct_with_vla {
71 int count;
72 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_unannotated_vla' is a struct type with a flexible array member}}
73 struct has_unannotated_vla* objects __counted_by(count);
74 };
75
76 struct has_annotated_vla {
77 int count;
78 int buffer[] __counted_by(count);
79 };
80
81 // Currently prevented because computing the size of `objects` at runtime would
82 // require an O(N) walk of `objects` to take into account the length of the VLA
83 // in each struct instance.
84 struct on_member_pointer_struct_with_annotated_vla {
85 int count;
86 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_annotated_vla' is a struct type with a flexible array member}}
87 struct has_annotated_vla* objects __counted_by(count);
88 };
89
90 struct on_pointer_anon_buf {
91 int count;
92 struct {
93 struct size_known *buf __counted_by(count);
94 };
95 };
96
97 struct on_pointer_anon_count {
98 struct {
99 int count;
100 };
101 struct size_known *buf __counted_by(count);
102 };
103
104 //==============================================================================
105 // __counted_by on struct member pointer in type attribute position
106 //==============================================================================
107 // TODO: Correctly parse counted_by as a type attribute. Currently it is parsed
108 // as a declaration attribute
109
110 struct on_member_pointer_complete_ty_ty_pos {
111 int count;
112 struct size_known *__counted_by(count) buf;
113 };
114
115 struct on_member_pointer_incomplete_ty_ty_pos {
116 int count;
117 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct size_unknown' is an incomplete type}}
118 struct size_unknown * __counted_by(count) buf;
119 };
120
121 struct on_member_pointer_const_incomplete_ty_ty_pos {
122 int count;
123 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'const struct size_unknown' is an incomplete type}}
124 const struct size_unknown * __counted_by(count) buf;
125 };
126
127 struct on_member_pointer_void_ty_ty_pos {
128 int count;
129 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void' is an incomplete type}}
130 void *__counted_by(count) buf;
131 };
132
133 // -
134
135 struct on_member_pointer_fn_ptr_ty_pos {
136 int count;
137 // buffer of `count` function pointers is allowed
138 void (** __counted_by(count) fn_ptr)(void);
139 };
140
141 struct on_member_pointer_fn_ptr_ty_ptr_ty_pos {
142 int count;
143 // buffer of `count` function pointers is allowed
144 fn_ptr_ty* __counted_by(count) fn_ptr;
145 };
146
147 struct on_member_pointer_fn_ty_ty_pos {
148 int count;
149 // buffer of `count` functions is not allowed
150 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
151 void (* __counted_by(count) fn_ptr)(void);
152 };
153
154 struct on_member_pointer_fn_ptr_ty_ty_pos {
155 int count;
156 // buffer of `count` functions is not allowed
157 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
158 fn_ptr_ty __counted_by(count) fn_ptr;
159 };
160
161 // TODO: This should be forbidden but isn't due to counted_by being treated
162 // as a declaration attribute.
163 struct on_member_pointer_fn_ptr_ty_ty_pos_inner {
164 int count;
165 void (* __counted_by(count) * fn_ptr)(void);
166 };
167
168 struct on_member_pointer_struct_with_vla_ty_pos {
169 int count;
170 // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_unannotated_vla' is a struct type with a flexible array member}}
171 struct has_unannotated_vla *__counted_by(count) objects;
172 };
173
174 // Currently prevented because computing the size of `objects` at runtime would
175 // require an O(N) walk of `objects` to take into account the length of the VLA
176 // in each struct instance.
177 struct on_member_pointer_struct_with_annotated_vla_ty_pos {
178 int count;
179 // expected-error@+1{{counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_annotated_vla' is a struct type with a flexible array member}}
180 struct has_annotated_vla* __counted_by(count) objects;
181 };
182
183 struct on_nested_pointer_inner {
184 // TODO: This should be disallowed because in the `-fbounds-safety` model
185 // `__counted_by` can only be nested when used in function parameters.
186 int count;
187 struct size_known *__counted_by(count) *buf;
188 };
189
190 struct on_nested_pointer_outer {
191 int count;
192 struct size_known **__counted_by(count) buf;
193 };
194
195 struct on_pointer_anon_buf_ty_pos {
196 int count;
197 struct {
198 struct size_known * __counted_by(count) buf;
199 };
200 };
201
202 struct on_pointer_anon_count_ty_pos {
203 struct {
204 int count;
205 };
206 struct size_known *__counted_by(count) buf;
207 };
208
209 //==============================================================================
210 // __counted_by on struct non-pointer members
211 //==============================================================================
212
213 struct on_pod_ty {
214 int count;
215 // expected-error@+1{{'counted_by' only applies to pointers or C99 flexible array members}}
216 int wrong_ty __counted_by(count);
217 };
218
219 struct on_void_ty {
220 int count;
221 // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
222 // expected-error@+1{{field has incomplete type 'void'}}
223 void wrong_ty __counted_by(count);
224 };