Debian Codegraph @

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

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