| 1 |
/* Testing the correct usage of the new __builtin_counted_by_ref. */ |
| 2 |
/* { dg-do compile } */ |
| 3 |
/* { dg-options "-O" } */ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
struct annotated { |
| 8 |
size_t b; |
| 9 |
int other; |
| 10 |
int c[] __attribute ((counted_by (b))); |
| 11 |
} *array_annotated; |
| 12 |
|
| 13 |
struct flex { |
| 14 |
size_t b; |
| 15 |
int other; |
| 16 |
int c[]; |
| 17 |
} *array_flex; |
| 18 |
|
| 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 *)) \ |
| 31 |
__builtin_abort (); \ |
| 32 |
} \ |
| 33 |
}) |
| 34 |
|
| 35 |
extern char c_count; |
| 36 |
extern short s_count; |
| 37 |
extern int i_count; |
| 38 |
extern long l_count; |
| 39 |
extern float f_count; |
| 40 |
|
| 41 |
extern int * foo (); |
| 42 |
|
| 43 |
int main(int argc, char *argv[]) |
| 44 |
{ |
| 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); |
| 53 |
|
| 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" } */ |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |