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