147 lines · c
1// RUN: %clang_cc1 -fsyntax-only -Wpointer-arith -verify=expected,immediate %s2// RUN: %clang_cc1 -fsyntax-only -Wpointer-arith -fexperimental-late-parse-attributes -verify=expected,late %s3 4#define __counted_by_or_null(f) __attribute__((counted_by_or_null(f)))5 6// This has been adapted from clang/test/Sema/attr-counted-by-vla.c, but with VLAs replaced with pointers7 8struct bar;9 10struct not_found {11 int count;12 struct bar *ptr __counted_by_or_null(bork); // expected-error {{use of undeclared identifier 'bork'}}13};14 15struct no_found_count_not_in_substruct {16 unsigned long flags;17 unsigned char count; // expected-note {{'count' declared here}}18 struct A {19 int dummy;20 int * ptr __counted_by_or_null(count); // expected-error {{'counted_by_or_null' field 'count' isn't within the same struct as the annotated pointer}}21 } a;22};23 24struct not_found_count_not_in_unnamed_substruct {25 unsigned char count; // expected-note {{'count' declared here}}26 struct {27 int dummy;28 int * ptr __counted_by_or_null(count); // expected-error {{'counted_by_or_null' field 'count' isn't within the same struct as the annotated pointer}}29 } a;30};31 32struct not_found_count_not_in_unnamed_substruct_2 {33 struct {34 unsigned char count; // expected-note {{'count' declared here}}35 };36 struct {37 int dummy;38 int * ptr __counted_by_or_null(count); // expected-error {{'counted_by_or_null' field 'count' isn't within the same struct as the annotated pointer}}39 } a;40};41 42struct not_found_count_in_other_unnamed_substruct {43 struct {44 unsigned char count;45 } a1;46 47 struct {48 int dummy;49 int * ptr __counted_by_or_null(count); // expected-error {{use of undeclared identifier 'count'}}50 };51};52 53struct not_found_count_in_other_substruct {54 struct _a1 {55 unsigned char count;56 } a1;57 58 struct {59 int dummy;60 int * ptr __counted_by_or_null(count); // expected-error {{use of undeclared identifier 'count'}}61 };62};63 64struct not_found_count_in_other_substruct_2 {65 struct _a2 {66 unsigned char count;67 } a2;68 69 int * ptr __counted_by_or_null(count); // expected-error {{use of undeclared identifier 'count'}}70};71 72struct not_found_suggest {73 int bork;74 struct bar **ptr __counted_by_or_null(blork); // expected-error {{use of undeclared identifier 'blork'}}75};76 77int global; // expected-note {{'global' declared here}}78 79struct found_outside_of_struct {80 int bork;81 struct bar ** ptr __counted_by_or_null(global); // expected-error {{field 'global' in 'counted_by_or_null' not inside structure}}82};83 84struct self_referrential {85 int bork;86 // immediate-error@+2{{use of undeclared identifier 'self'}}87 // late-error@+1{{'counted_by_or_null' only applies to pointers; did you mean to use 'counted_by'?}}88 struct bar *self[] __counted_by_or_null(self);89};90 91struct non_int_count {92 double dbl_count;93 struct bar ** ptr __counted_by_or_null(dbl_count); // expected-error {{'counted_by_or_null' requires a non-boolean integer type argument}}94};95 96struct array_of_ints_count {97 int integers[2];98 struct bar ** ptr __counted_by_or_null(integers); // expected-error {{'counted_by_or_null' requires a non-boolean integer type argument}}99};100 101struct not_a_c99_fam {102 int count;103 struct bar *non_c99_fam[0] __counted_by_or_null(count); // expected-error {{'counted_by_or_null' only applies to pointers; did you mean to use 'counted_by'?}}104};105 106struct annotated_with_anon_struct {107 unsigned long flags;108 struct {109 unsigned char count;110 int * ptr __counted_by_or_null(crount); // expected-error {{use of undeclared identifier 'crount'}}111 };112};113 114//==============================================================================115// __counted_by_or_null on a struct ptr with element type that has unknown count116//==============================================================================117 118struct count_unknown;119struct on_member_ptr_incomplete_ty_ty_pos {120 int count;121 struct count_unknown * ptr __counted_by_or_null(count); // ok122};123 124struct on_member_ptr_incomplete_const_ty_ty_pos {125 int count;126 const struct count_unknown * ptr __counted_by_or_null(count); // ok127};128 129struct on_member_ptr_void_ty_ty_pos {130 int count;131 // expected-warning@+2{{'counted_by_or_null' on a pointer to void is a GNU extension, treated as 'sized_by_or_null'}}132 // expected-note@+1{{use '__sized_by_or_null' to suppress this warning}}133 void * ptr __counted_by_or_null(count);134};135 136typedef void(fn_ty)(int);137 138struct on_member_ptr_fn_ptr_ty {139 int count;140 fn_ty* * ptr __counted_by_or_null(count);141};142 143struct on_member_ptr_fn_ty {144 int count;145 fn_ty * ptr __counted_by_or_null(count); // expected-error {{'counted_by_or_null' cannot be applied to a pointer with pointee of unknown size because 'fn_ty' (aka 'void (int)') is a function type}}146};147