1 /* Testing the correct usage of the new __builtin_counted_by_ref. */
2 /* { dg-do compile } */
3 /* { dg-options "-O" } */
10 int c
[] __attribute ((counted_by (b
)));
19 #define MAX(A, B) (A > B) ? (A) : (B)
20 #define MY_ALLOC(P, FAM, COUNT) ({ \
21 __auto_type __p = &(P); \
22 __auto_type __c = (COUNT); \
23 size_t __size = MAX (sizeof (*(*__p)),\
24 __builtin_offsetof (__typeof(*(*__p)),FAM) \
25 + sizeof (*((*__p)->FAM)) * __c); \
26 if ((*__p = __builtin_malloc(__size))) { \
27 __builtin_memset(*__p, 0, __size); \
28 __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \
29 *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \
30 if (sizeof (__builtin_counted_by_ref ((*__p)->FAM)) != sizeof (char *)) \
43 int main(int argc
, char *argv
[])
45 /* The good usages. */
46 MY_ALLOC(array_annotated
, c
, 10);
47 MY_ALLOC(array_flex
, c
, 20);
48 MY_ALLOC(array_annotated
, c
, c_count
);
49 MY_ALLOC(array_flex
, c
, i_count
);
50 MY_ALLOC(array_annotated
, c
, l_count
);
51 MY_ALLOC(array_flex
, c
, c_count
* 3);
52 MY_ALLOC(array_annotated
, c
, l_count
* i_count
);
54 /* The bad usages, issue errors. */
55 __builtin_counted_by_ref (); /* { dg-error "wrong number of arguments to" } */
56 __builtin_counted_by_ref (array_annotated
->c
, 10); /* { dg-error "wrong number of arguments to" } */
57 __builtin_counted_by_ref (array_annotated
->other
); /* { dg-error "must be an array" } */
58 __builtin_counted_by_ref (foo()); /* { dg-error "must be an array" } */