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)))
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
{
20 struct size_known
* buf
__sized_by(size
);
23 struct on_member_pointer_incomplete_ty
{
25 struct size_unknown
* buf
__sized_by(size
);
28 struct on_member_pointer_const_incomplete_ty
{
30 const struct size_unknown
* buf
__sized_by(size
);
33 struct on_member_pointer_void_ty
{
35 void* buf
__sized_by(size
);
38 struct on_member_pointer_fn_ptr_ty
{
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
{
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
{
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
{
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
{
69 struct on_member_pointer_struct_with_vla
{
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
{
78 int buffer
[] __counted_by(count
);
81 struct on_member_pointer_struct_with_annotated_vla
{
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
{
91 struct size_known
*buf
__sized_by(size
);
95 struct on_pointer_anon_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
{
110 struct size_known
*__sized_by(size
) buf
;
113 struct on_member_pointer_incomplete_ty_ty_pos
{
115 struct size_unknown
* __sized_by(size
) buf
;
118 struct on_member_pointer_const_incomplete_ty_ty_pos
{
120 const struct size_unknown
* __sized_by(size
) buf
;
123 struct on_member_pointer_void_ty_ty_pos
{
125 void *__sized_by(size
) buf
;
130 struct on_member_pointer_fn_ptr_ty_pos
{
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
{
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
{
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
{
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
{
158 void (* __sized_by(size
) * fn_ptr
)(void);
161 struct on_member_pointer_struct_with_vla_ty_pos
{
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
{
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.
177 struct size_known
*__sized_by(size
) *buf
;
180 struct on_nested_pointer_outer
{
182 struct size_known
**__sized_by(size
) buf
;
185 struct on_pointer_anon_buf_ty_pos
{
188 struct size_known
* __sized_by(size
) buf
;
192 struct on_pointer_anon_size_ty_pos
{
196 struct size_known
*__sized_by(size
) buf
;
199 //==============================================================================
200 // __sized_by on struct non-pointer members
201 //==============================================================================
205 // expected-error-re@+1{{'sized_by' only applies to pointers{{$}}}}
206 int wrong_ty
__sized_by(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
{
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
);