Debian Codegraph @

gcc/testsuite/gcc.dg/flex-array-counted-by-3.c

raw
1 /* Test the attribute counted_by and its usage in
2 * __builtin_dynamic_object_size. */
3 /* { dg-do run } */
4 /* { dg-options "-O2" } */
5
6 #include "builtin-object-size-common.h"
7
8 struct flex {
9 int b;
10 int c[];
11 } *array_flex;
12
13 struct annotated {
14 int b;
15 int c[] __attribute__ ((counted_by (b)));
16 } *array_annotated;
17
18 struct nested_annotated {
19 struct {
20 union {
21 int b;
22 float f;
23 };
24 int n;
25 };
26 int c[] __attribute__ ((counted_by (b)));
27 } *array_nested_annotated;
28
29 void __attribute__((__noinline__)) setup (int normal_count, int attr_count)
30 {
31 array_flex
32 = (struct flex *)malloc (sizeof (struct flex)
33 + normal_count * sizeof (int));
34 array_flex->b = normal_count;
35
36 array_annotated
37 = (struct annotated *)malloc (sizeof (struct annotated)
38 + attr_count * sizeof (int));
39 array_annotated->b = attr_count;
40
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;
45
46 return;
47 }
48
49 void __attribute__((__noinline__)) test ()
50 {
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));
56 }
57
58 int main(int argc, char *argv[])
59 {
60 setup (10,10);
61 test ();
62 DONE ();
63 }