[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / attr-sized-by-struct-ptrs.c
blob07373b247d0f7903019c892717f334f5b9a91794
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fexperimental-late-parse-attributes -fsyntax-only -verify %s
4 #define __sized_by(f) __attribute__((sized_by(f)))
5 #define __counted_by(f) __attribute__((counted_by(f)))
7 struct size_unknown;
8 struct size_known {
9 int field;
12 typedef void(*fn_ptr_ty)(void);
14 //==============================================================================
15 // __sized_by on struct member pointer in decl attribute position
16 //==============================================================================
18 struct on_member_pointer_complete_ty {
19 int size;
20 struct size_known * buf __sized_by(size);
23 struct on_member_pointer_incomplete_ty {
24 int size;
25 struct size_unknown * buf __sized_by(size);
28 struct on_member_pointer_const_incomplete_ty {
29 int size;
30 const struct size_unknown * buf __sized_by(size);
33 struct on_member_pointer_void_ty {
34 int size;
35 void* buf __sized_by(size);
38 struct on_member_pointer_fn_ptr_ty {
39 int size;
40 // buffer of function pointers with size `size` is allowed
41 void (**fn_ptr)(void) __sized_by(size);
44 struct on_member_pointer_fn_ptr_ty_ptr_ty {
45 int size;
46 // buffer of function pointers with size `size` is allowed
47 fn_ptr_ty* fn_ptr __sized_by(size);
50 struct on_member_pointer_fn_ty {
51 int size;
52 // buffer of functions with size `size` is allowed
53 // expected-error@+1{{'sized_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
54 void (*fn_ptr)(void) __sized_by(size);
57 struct on_member_pointer_fn_ptr_ty_ty {
58 int size;
59 // buffer of functions with size `size` is allowed
60 // expected-error@+1{{'sized_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
61 fn_ptr_ty fn_ptr __sized_by(size);
64 struct has_unannotated_vla {
65 int count;
66 int buffer[];
69 struct on_member_pointer_struct_with_vla {
70 int size;
71 // we know the size so this is fine for tracking size, however indexing would be an issue
72 // 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}}
73 struct has_unannotated_vla* objects __sized_by(size);
76 struct has_annotated_vla {
77 int count;
78 int buffer[] __counted_by(count);
81 struct on_member_pointer_struct_with_annotated_vla {
82 int size;
83 // we know the size so this is fine for tracking size, however indexing would be an issue
84 // 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}}
85 struct has_annotated_vla* objects __sized_by(size);
88 struct on_pointer_anon_buf {
89 int size;
90 struct {
91 struct size_known *buf __sized_by(size);
95 struct on_pointer_anon_size {
96 struct {
97 int size;
99 struct size_known *buf __sized_by(size);
102 //==============================================================================
103 // __sized_by on struct member pointer in type attribute position
104 //==============================================================================
105 // TODO: Correctly parse sized_by as a type attribute. Currently it is parsed
106 // as a declaration attribute
108 struct on_member_pointer_complete_ty_ty_pos {
109 int size;
110 struct size_known *__sized_by(size) buf;
113 struct on_member_pointer_incomplete_ty_ty_pos {
114 int size;
115 struct size_unknown * __sized_by(size) buf;
118 struct on_member_pointer_const_incomplete_ty_ty_pos {
119 int size;
120 const struct size_unknown * __sized_by(size) buf;
123 struct on_member_pointer_void_ty_ty_pos {
124 int size;
125 void *__sized_by(size) buf;
128 // -
130 struct on_member_pointer_fn_ptr_ty_pos {
131 int size;
132 // buffer of `size` function pointers is allowed
133 void (** __sized_by(size) fn_ptr)(void);
136 struct on_member_pointer_fn_ptr_ty_ptr_ty_pos {
137 int size;
138 // buffer of `size` function pointers is allowed
139 fn_ptr_ty* __sized_by(size) fn_ptr;
142 struct on_member_pointer_fn_ty_ty_pos {
143 int size;
144 // expected-error@+1{{'sized_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
145 void (* __sized_by(size) fn_ptr)(void);
148 struct on_member_pointer_fn_ptr_ty_ty_pos {
149 int size;
150 // expected-error@+1{{'sized_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
151 fn_ptr_ty __sized_by(size) fn_ptr;
154 // TODO: This should be forbidden but isn't due to sized_by being treated
155 // as a declaration attribute.
156 struct on_member_pointer_fn_ptr_ty_ty_pos_inner {
157 int size;
158 void (* __sized_by(size) * fn_ptr)(void);
161 struct on_member_pointer_struct_with_vla_ty_pos {
162 int size;
163 // 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}}
164 struct has_unannotated_vla *__sized_by(size) objects;
167 struct on_member_pointer_struct_with_annotated_vla_ty_pos {
168 int size;
169 // 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}}
170 struct has_annotated_vla* __sized_by(size) objects;
173 struct on_nested_pointer_inner {
174 // TODO: This should be disallowed because in the `-fbounds-safety` model
175 // `__sized_by` can only be nested when used in function parameters.
176 int size;
177 struct size_known *__sized_by(size) *buf;
180 struct on_nested_pointer_outer {
181 int size;
182 struct size_known **__sized_by(size) buf;
185 struct on_pointer_anon_buf_ty_pos {
186 int size;
187 struct {
188 struct size_known * __sized_by(size) buf;
192 struct on_pointer_anon_size_ty_pos {
193 struct {
194 int size;
196 struct size_known *__sized_by(size) buf;
199 //==============================================================================
200 // __sized_by on struct non-pointer members
201 //==============================================================================
203 struct on_pod_ty {
204 int size;
205 // expected-error-re@+1{{'sized_by' only applies to pointers{{$}}}}
206 int wrong_ty __sized_by(size);
209 struct on_void_ty {
210 int size;
211 // expected-error-re@+2{{'sized_by' only applies to pointers{{$}}}}
212 // expected-error@+1{{field has incomplete type 'void'}}
213 void wrong_ty __sized_by(size);
216 struct on_member_array_complete_ty {
217 int size;
218 // expected-error@+1{{'sized_by' only applies to pointers; did you mean to use 'counted_by'?}}
219 struct size_known array[] __sized_by(size);