PR tree-optimization/71831 - __builtin_object_size poor results with no
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-16.c
blob15721e5ce897d5309d6ec6b201fefe5c6b1cf58c
1 /* PR 71831 - __builtin_object_size poor results with no optimization
2 Verify that even without optimization __builtin_object_size returns
3 a meaningful result for a subset of simple expressins. In cases
4 where the result could not easily be made to match the one obtained
5 with optimization the built-in was made to fail instead. */
6 /* { dg-do run } */
7 /* { dg-options "-O0" } */
9 static int nfails;
11 #define TEST_FAILURE(line, obj, type, expect, result) \
12 __builtin_printf ("FAIL: line %i: __builtin_object_size(" \
13 #obj ", %i) == %zu, got %zu\n", \
14 line, type, expect, result), ++nfails
16 #define bos(obj, type) __builtin_object_size (obj, type)
17 #define size(obj, n) ((size_t)n == X ? sizeof *obj : (size_t)n)
19 #define test(expect, type, obj) \
20 do { \
21 if (bos (obj, type) != size (obj, expect)) \
22 TEST_FAILURE (__LINE__, obj, type, size (obj, expect), bos (obj, type)); \
23 } while (0)
25 #define T(r0, r1, r2, r3, obj) \
26 do { \
27 test (r0, 0, obj); \
28 test (r1, 1, obj); \
29 test (r2, 2, obj); \
30 test (r3, 3, obj); \
31 } while (0)
33 /* For convenience. Substitute for 'sizeof object' in test cases where
34 the size can vary from target to target. */
35 #define X (size_t)0xdeadbeef
37 /* __builtin_object_size checking results are inconsistent for equivalent
38 expressions (see bug 71831). To avoid having duplicate the inconsistency
39 at -O0 the built-in simply fails. The results hardcoded in this test
40 are those obtained with optimization (for easy comparison) but without
41 optimization the macros below turn them into expected failures . */
42 #if __OPTIMIZE__
43 # define F0(n) n
44 # define F1(n) n
45 # define F2(n) n
46 # define F3(n) n
47 #else
48 # define F0(n) -1
49 # define F1(n) -1
50 # define F2(n) 0
51 # define F3(n) 0
52 #endif
54 typedef __SIZE_TYPE__ size_t;
56 extern char ax[];
57 char ax2[]; /* { dg-warning "assumed to have one element" } */
59 extern char a0[0];
60 static char a1[1];
61 static char a2[2];
62 static char a9[9];
64 #if __SIZEOF_SHORT__ == 4
65 extern short ia0[0];
66 static short ia1[1];
67 static short ia9[9];
68 #elif __SIZEOF_INT__ == 4
69 extern int ia0[0];
70 static int ia1[1];
71 static int ia9[9];
72 #endif
74 static char a2x2[2][2];
75 static char a3x5[3][5];
77 struct Sx { char n, a[]; } sx;
78 struct S0 { char n, a[0]; } s0;
79 struct S1 { char n, a[1]; } s1;
80 struct S2 { char n, a[2]; } s2;
81 struct S9 { char n, a[9]; } s9;
83 struct S2x2 { char n, a[2][2]; } s2x2;
84 struct S3x5 { char n, a[3][5]; } s3x5;
86 static __attribute__ ((noclone, noinline)) void
87 test_arrays ()
89 T ( -1, -1, 0, 0, ax);
91 T ( 0, 0, 0, 0, a0);
92 T ( 1, 1, 1, 1, ax2);
94 T ( 1, 1, 1, 1, a1);
95 T ( 2, 2, 2, 2, a2);
96 T ( 9, 9, 9, 9, a9);
98 T ( 0, 0, 0, 0, a0);
99 T ( 1, 1, 1, 1, ax2);
101 T ( 0, 0, 0, 0, ia0);
102 T ( 4, 4, 4, 4, ia1);
103 T ( 36, 36, 36, 36, ia9);
105 /* Not all results for multidimensional arrays make sense (see
106 bug 77293). The expected results below simply reflect those
107 obtained at -O2 (modulo the known limitations at -O1). */
108 T ( 4, 4, 4, 4, a2x2);
109 T ( 4, 4, 4, 4, &a2x2[0]);
110 T ( 4, 2, 4, 2, &a2x2[0][0]);
111 T ( 0, F1 (0), 0, 0, &a2x2 + 1);
112 T ( 2, F1 ( 2), 2, F3 ( 2), &a2x2[0] + 1);
113 T ( 3, F1 ( 1), 3, F3 ( 3), &a2x2[0][0] + 1);
115 T ( 15, 15, 15, 15, a3x5);
116 T ( 15, 5, 15, 5, &a3x5[0][0] + 0);
117 T ( 14, F1 ( 4), 14, F3 (14), &a3x5[0][0] + 1);
119 T ( 1, 1, 1, 1, a1 + 0);
120 T ( 0, F1 (0), 0, 0, a1 + 1);
121 T ( 0, F1 ( 0), 0, 0, &a1 + 1);
122 /* In the following the offset is out of bounds which makes
123 the expression undefined. Still, verify that the returned
124 size is zero (and not some large number). */
125 T ( 0, F1 (0), 0, 0, a1 + 2);
127 T ( 2, 2, 2, 2, a2 + 0);
128 T ( 1, F1 ( 1), 1, F3 ( 1), a2 + 1);
129 T ( 0, F1 ( 0), 0, 0, a2 + 2);
132 static __attribute__ ((noclone, noinline)) void
133 test_structs (struct Sx *psx, struct S0 *ps0, struct S1 *ps1, struct S9 *ps9)
135 /* The expected size of a declared object with a flexible array member
136 is sizeof sx in all __builtin_object_size types. */
137 T ( X, X, X, X, &sx);
139 /* The expected size of an unknown object with a flexible array member
140 is unknown in all __builtin_object_size types. */
141 T ( -1, -1, 0, 0, psx);
143 /* The expected size of a flexible array member of a declared object
144 is zero. */
145 T ( 0, 0, 0, 0, sx.a);
147 /* The expected size of a flexible array member of an unknown object
148 is unknown. */
149 T ( -1, -1, 0, 0, psx->a);
151 /* The expected size of a declared object with a zero-length array member
152 is sizeof sx in all __builtin_object_size types. */
153 T ( X, X, X, X, &s0);
155 /* The expected size of an unknown object with a zero-length array member
156 is unknown in all __builtin_object_size types. */
157 T ( -1, -1, 0, 0, ps0);
159 /* The expected size of a zero-length array member of a declared object
160 is zero. */
161 T ( 0, 0, 0, 0, s0.a);
163 /* The expected size of a zero-length array member of an unknown object
164 is unknown. */
165 T ( -1, -1, 0, 0, ps0->a);
167 T ( X, X, X, X, &s1);
168 T ( 1, 1, 1, 1, s1.a);
169 T ( 0, F1 (0), 0, 0, s1.a + 1);
171 /* GCC treats arrays of all sizes that are the last member of a struct
172 as flexible array members. */
173 T ( -1, -1, 0, 0, ps1);
174 T ( -1, -1, 0, 0, ps1->a);
175 T ( -1, -1, 0, 0, ps1->a + 1);
177 T ( X, X, X, X, &s9);
178 T ( 9, 9, 9, 9, s9.a);
179 T ( 9, 9, 9, 9, s9.a + 0);
180 T ( 8, F1 ( 8), 8, F3 ( 8), s9.a + 1);
181 T ( 7, F1 ( 7), 7, F3 ( 7), s9.a + 2);
182 T ( 0, F1 ( 0), 0, F3 ( 0), s9.a + 9);
184 /* The following make little sense but see bug 77301. */
185 T ( -1, -1, 0, 0, ps9);
186 T ( -1, -1, 0, 0, ps9->a);
187 T ( -1, -1, 0, 0, ps9->a + 1);
191 main()
193 test_arrays ();
195 test_structs (&sx, &s0, &s1, &s9);
197 if (nfails)
198 __builtin_abort ();
200 return 0;