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. */
7 /* { dg-options "-O0" } */
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) \
21 if (bos (obj, type) != size (obj, expect)) \
22 TEST_FAILURE (__LINE__, obj, type, size (obj, expect), bos (obj, type)); \
25 #define T(r0, r1, r2, r3, obj) \
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 . */
54 typedef __SIZE_TYPE__
size_t;
57 char ax2
[]; /* { dg-warning "assumed to have one element" } */
64 #if __SIZEOF_SHORT__ == 4
68 #elif __SIZEOF_INT__ == 4
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
89 T ( -1, -1, 0, 0, ax
);
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
145 T ( 0, 0, 0, 0, sx
.a
);
147 /* The expected size of a flexible array member of an unknown object
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
161 T ( 0, 0, 0, 0, s0
.a
);
163 /* The expected size of a zero-length array member of an unknown object
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);
195 test_structs (&sx
, &s0
, &s1
, &s9
);