Debian Codegraph @

clang/test/Sema/attr-counted-by-or-null-last-field.c

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