Debian Codegraph @

clang/test/Sema/builtin-counted-by-ref.c

raw
1 // RUN: %clang_cc1 -std=c99 -fsyntax-only -verify %s
2
3 typedef unsigned long int size_t;
4
5 int global_array[42];
6 int global_int;
7
8 struct fam_struct {
9 int x;
10 char count;
11 int array[] __attribute__((counted_by(count)));
12 };
13
14 void g(char *);
15 void h(char);
16
17 void test1(struct fam_struct *ptr, int size, int idx) {
18 size_t size_of = sizeof(__builtin_counted_by_ref(ptr->array)); // ok
19 int align_of = __alignof(__builtin_counted_by_ref(ptr->array)); // ok
20 size_t __ignored_assignment;
21
22 *__builtin_counted_by_ref(ptr->array) = size; // ok
23 *_Generic(__builtin_counted_by_ref(ptr->array),
24 void *: &__ignored_assignment,
25 default: __builtin_counted_by_ref(ptr->array)) = 42; // ok
26 h(*__builtin_counted_by_ref(ptr->array)); // ok
27 }
28
29 void test2(struct fam_struct *ptr, int idx) {
30 __builtin_counted_by_ref(); // expected-error {{too few arguments to function call, expected 1, have 0}}
31 __builtin_counted_by_ref(ptr->array, ptr->x, ptr->count); // expected-error {{too many arguments to function call, expected 1, have 3}}
32 }
33
34 void test3(struct fam_struct *ptr, int idx) {
35 __builtin_counted_by_ref(&ptr->array[0]); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
36 __builtin_counted_by_ref(&ptr->array[idx]); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
37 __builtin_counted_by_ref(&ptr->array); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
38 __builtin_counted_by_ref(ptr->x); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
39 __builtin_counted_by_ref(&ptr->x); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
40 __builtin_counted_by_ref(global_array); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
41 __builtin_counted_by_ref(global_int); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
42 __builtin_counted_by_ref(&global_int); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
43 }
44
45 void test4(struct fam_struct *ptr, int idx) {
46 __builtin_counted_by_ref(ptr++->array); // expected-error {{'__builtin_counted_by_ref' argument cannot have side-effects}}
47 __builtin_counted_by_ref(&ptr->array[idx++]); // expected-error {{'__builtin_counted_by_ref' argument cannot have side-effects}}
48 }
49
50 void *test5(struct fam_struct *ptr, int size, int idx) {
51 char *ref = __builtin_counted_by_ref(ptr->array); // expected-error {{value returned by '__builtin_counted_by_ref' cannot be assigned to a variable}}
52 char *int_ptr;
53 char *p;
54
55 ref = __builtin_counted_by_ref(ptr->array); // expected-error {{value returned by '__builtin_counted_by_ref' cannot be assigned to a variable}}
56 g(__builtin_counted_by_ref(ptr->array)); // expected-error {{value returned by '__builtin_counted_by_ref' cannot be passed into a function}}
57 g(ref = __builtin_counted_by_ref(ptr->array)); // expected-error {{value returned by '__builtin_counted_by_ref' cannot be assigned to a variable}}
58
59 if ((ref = __builtin_counted_by_ref(ptr->array))) // expected-error {{value returned by '__builtin_counted_by_ref' cannot be assigned to a variable}}
60 ;
61
62 for (p = __builtin_counted_by_ref(ptr->array); p && *p; ++p) // expected-error {{value returned by '__builtin_counted_by_ref' cannot be assigned to a variable}}
63 ;
64
65 return __builtin_counted_by_ref(ptr->array); // expected-error {{value returned by '__builtin_counted_by_ref' cannot be returned from a function}}
66 }
67
68 void test6(struct fam_struct *ptr, int size, int idx) {
69 *(__builtin_counted_by_ref(ptr->array) + 4) = 37; // expected-error {{value returned by '__builtin_counted_by_ref' cannot be used in a binary expression}}
70 __builtin_counted_by_ref(ptr->array)[3] = 37; // expected-error {{value returned by '__builtin_counted_by_ref' cannot be used in an array subscript expression}}
71 }
72
73 struct non_fam_struct {
74 char x;
75 long *pointer;
76 int array[42];
77 short count;
78 };
79
80 void *test7(struct non_fam_struct *ptr, int size) {
81 // Arrays and pointers without counted_by return void*
82 _Static_assert(_Generic(__builtin_counted_by_ref(ptr->array), void * : 1, default : 0) == 1, "should be void*");
83 _Static_assert(_Generic(__builtin_counted_by_ref(ptr->pointer), void * : 1, default : 0) == 1, "should be void*");
84 // These are not direct member accesses, so they error
85 __builtin_counted_by_ref(&ptr->array[0]); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
86 __builtin_counted_by_ref(&ptr->pointer[0]); // expected-error {{'__builtin_counted_by_ref' argument must reference a member with the 'counted_by' attribute}}
87 }
88
89 struct char_count {
90 char count;
91 int array[] __attribute__((counted_by(count)));
92 } *cp;
93
94 struct short_count {
95 short count;
96 int array[] __attribute__((counted_by(count)));
97 } *sp;
98
99 struct int_count {
100 int count;
101 int array[] __attribute__((counted_by(count)));
102 } *ip;
103
104 struct unsigned_count {
105 unsigned count;
106 int array[] __attribute__((counted_by(count)));
107 } *up;
108
109 struct long_count {
110 long count;
111 int array[] __attribute__((counted_by(count)));
112 } *lp;
113
114 struct unsigned_long_count {
115 unsigned long count;
116 int array[] __attribute__((counted_by(count)));
117 } *ulp;
118
119 void test8(void) {
120 _Static_assert(_Generic(__builtin_counted_by_ref(cp->array), char * : 1, default : 0) == 1, "wrong return type");
121 _Static_assert(_Generic(__builtin_counted_by_ref(sp->array), short * : 1, default : 0) == 1, "wrong return type");
122 _Static_assert(_Generic(__builtin_counted_by_ref(ip->array), int * : 1, default : 0) == 1, "wrong return type");
123 _Static_assert(_Generic(__builtin_counted_by_ref(up->array), unsigned int * : 1, default : 0) == 1, "wrong return type");
124 _Static_assert(_Generic(__builtin_counted_by_ref(lp->array), long * : 1, default : 0) == 1, "wrong return type");
125 _Static_assert(_Generic(__builtin_counted_by_ref(ulp->array), unsigned long * : 1, default : 0) == 1, "wrong return type");
126 }
127
128 // Tests for pointer members with counted_by attribute
129 struct ptr_char_count {
130 char count;
131 int *ptr __attribute__((counted_by(count)));
132 } *pcp;
133
134 struct ptr_short_count {
135 short count;
136 int *ptr __attribute__((counted_by(count)));
137 } *psp;
138
139 struct ptr_int_count {
140 int count;
141 int *ptr __attribute__((counted_by(count)));
142 } *pip;
143
144 struct ptr_unsigned_count {
145 unsigned count;
146 int *ptr __attribute__((counted_by(count)));
147 } *pup;
148
149 struct ptr_long_count {
150 long count;
151 int *ptr __attribute__((counted_by(count)));
152 } *plp;
153
154 struct ptr_unsigned_long_count {
155 unsigned long count;
156 int *ptr __attribute__((counted_by(count)));
157 } *pulp;
158
159 void test9(struct ptr_int_count *ptr, int size) {
160 size_t size_of = sizeof(__builtin_counted_by_ref(ptr->ptr)); // ok
161 int align_of = __alignof(__builtin_counted_by_ref(ptr->ptr)); // ok
162 size_t __ignored_assignment;
163
164 *__builtin_counted_by_ref(ptr->ptr) = size; // ok
165 *_Generic(__builtin_counted_by_ref(ptr->ptr),
166 void *: &__ignored_assignment,
167 default: __builtin_counted_by_ref(ptr->ptr)) = 42; // ok
168 }
169
170 void test10(void) {
171 _Static_assert(_Generic(__builtin_counted_by_ref(pcp->ptr), char * : 1, default : 0) == 1, "wrong return type");
172 _Static_assert(_Generic(__builtin_counted_by_ref(psp->ptr), short * : 1, default : 0) == 1, "wrong return type");
173 _Static_assert(_Generic(__builtin_counted_by_ref(pip->ptr), int * : 1, default : 0) == 1, "wrong return type");
174 _Static_assert(_Generic(__builtin_counted_by_ref(pup->ptr), unsigned int * : 1, default : 0) == 1, "wrong return type");
175 _Static_assert(_Generic(__builtin_counted_by_ref(plp->ptr), long * : 1, default : 0) == 1, "wrong return type");
176 _Static_assert(_Generic(__builtin_counted_by_ref(pulp->ptr), unsigned long * : 1, default : 0) == 1, "wrong return type");
177 }
178
179 // Pointer without counted_by returns void*
180 struct ptr_no_attr {
181 int count;
182 int *ptr; // No counted_by attribute
183 };
184
185 void test11(struct ptr_no_attr *p) {
186 _Static_assert(_Generic(__builtin_counted_by_ref(p->ptr), void * : 1, default : 0) == 1, "should be void*");
187 }