libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / flex-array-counted-by-3.c
blob78f50230e891eb72ff7f2bcb8e71d6e283c1dd3b
1 /* Test the attribute counted_by and its usage in
2 * __builtin_dynamic_object_size. */
3 /* { dg-do run } */
4 /* { dg-options "-O2" } */
6 #include "builtin-object-size-common.h"
8 struct flex {
9 int b;
10 int c[];
11 } *array_flex;
13 struct annotated {
14 int b;
15 int c[] __attribute__ ((counted_by (b)));
16 } *array_annotated;
18 struct nested_annotated {
19 struct {
20 union {
21 int b;
22 float f;
24 int n;
26 int c[] __attribute__ ((counted_by (b)));
27 } *array_nested_annotated;
29 void __attribute__((__noinline__)) setup (int normal_count, int attr_count)
31 array_flex
32 = (struct flex *)malloc (sizeof (struct flex)
33 + normal_count * sizeof (int));
34 array_flex->b = normal_count;
36 array_annotated
37 = (struct annotated *)malloc (sizeof (struct annotated)
38 + attr_count * sizeof (int));
39 array_annotated->b = attr_count;
41 array_nested_annotated
42 = (struct nested_annotated *)malloc (sizeof (struct nested_annotated)
43 + attr_count * sizeof (int));
44 array_nested_annotated->b = attr_count;
46 return;
49 void __attribute__((__noinline__)) test ()
51 EXPECT(__builtin_dynamic_object_size(array_flex->c, 1), -1);
52 EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1),
53 array_annotated->b * sizeof (int));
54 EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1),
55 array_nested_annotated->b * sizeof (int));
58 int main(int argc, char *argv[])
60 setup (10,10);
61 test ();
62 DONE ();