Skip to content

Commit d204bd3

Browse files
committed
C front-end: recognize __builtin_inff() as constant expression
This is accepted as a constant expression by GCC, so we need to mimic this to successfully type check code that relies on this. Fixes: #8710
1 parent 4fe3ade commit d204bd3

File tree

9 files changed

+53
-82
lines changed

9 files changed

+53
-82
lines changed

regression/cbmc-library/__builtin_inf-01/main.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

regression/cbmc-library/__builtin_inff-01/main.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

regression/cbmc-library/__builtin_inff-01/test.desc

Lines changed: 0 additions & 8 deletions
This file was deleted.

regression/cbmc-library/__builtin_infl-01/main.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

regression/cbmc-library/__builtin_infl-01/test.desc

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <assert.h>
2+
#include <math.h>
3+
4+
float g = -INFINITY;
5+
6+
void test_static_float_infinity()
7+
{
8+
static float f = +INFINITY;
9+
assert(isinf(f));
10+
}
11+
12+
void test_static_double_infinity()
13+
{
14+
static double d = INFINITY;
15+
assert(isinf(d));
16+
}
17+
18+
void test_static_long_double_infinity()
19+
{
20+
static long double ld = INFINITY;
21+
assert(isinf(ld));
22+
}
23+
24+
void test_static_huge_val()
25+
{
26+
static float f = HUGE_VALF;
27+
static double d = HUGE_VAL;
28+
static long double ld = HUGE_VALL;
29+
30+
assert(isinf(f));
31+
assert(isinf(d));
32+
assert(isinf(ld));
33+
}
34+
35+
int main()
36+
{
37+
test_static_float_infinity();
38+
test_static_double_infinity();
39+
test_static_long_double_infinity();
40+
test_static_huge_val();
41+
return 0;
42+
}

regression/cbmc-library/__builtin_inf-01/test.desc renamed to regression/cbmc/builtin_inff_static_init/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--pointer-check --bounds-check
3+
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,8 +3265,9 @@ exprt c_typecheck_baset::do_special_functions(
32653265

32663266
return typecast_exprt::conditional_cast(isfinite_expr, expr.type());
32673267
}
3268-
else if(identifier==CPROVER_PREFIX "inf" ||
3269-
identifier=="__builtin_inf")
3268+
else if(
3269+
identifier == CPROVER_PREFIX "inf" || identifier == "__builtin_inf" ||
3270+
identifier == "__builtin_huge_val")
32703271
{
32713272
constant_exprt inf_expr=
32723273
ieee_floatt::plus_infinity(
@@ -3275,7 +3276,9 @@ exprt c_typecheck_baset::do_special_functions(
32753276

32763277
return std::move(inf_expr);
32773278
}
3278-
else if(identifier==CPROVER_PREFIX "inff")
3279+
else if(
3280+
identifier == CPROVER_PREFIX "inff" || identifier == "__builtin_inff" ||
3281+
identifier == "__builtin_huge_valf")
32793282
{
32803283
constant_exprt inff_expr=
32813284
ieee_floatt::plus_infinity(
@@ -3284,7 +3287,9 @@ exprt c_typecheck_baset::do_special_functions(
32843287

32853288
return std::move(inff_expr);
32863289
}
3287-
else if(identifier==CPROVER_PREFIX "infl")
3290+
else if(
3291+
identifier == CPROVER_PREFIX "infl" || identifier == "__builtin_infl" ||
3292+
identifier == "__builtin_huge_vall")
32883293
{
32893294
floatbv_typet type=to_floatbv_type(long_double_type());
32903295
constant_exprt infl_expr=

src/ansi-c/library/math.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -216,39 +216,6 @@ int __isnormalf(float f)
216216
return __CPROVER_isnormalf(f);
217217
}
218218

219-
/* FUNCTION: __builtin_inff */
220-
221-
float __builtin_inff(void)
222-
{
223-
#pragma CPROVER check push
224-
#pragma CPROVER check disable "float-div-by-zero"
225-
#pragma CPROVER check disable "float-overflow"
226-
return 1.0f / 0.0f;
227-
#pragma CPROVER check pop
228-
}
229-
230-
/* FUNCTION: __builtin_inf */
231-
232-
double __builtin_inf(void)
233-
{
234-
#pragma CPROVER check push
235-
#pragma CPROVER check disable "float-div-by-zero"
236-
#pragma CPROVER check disable "float-overflow"
237-
return 1.0 / 0.0;
238-
#pragma CPROVER check pop
239-
}
240-
241-
/* FUNCTION: __builtin_infl */
242-
243-
long double __builtin_infl(void)
244-
{
245-
#pragma CPROVER check push
246-
#pragma CPROVER check disable "float-div-by-zero"
247-
#pragma CPROVER check disable "float-overflow"
248-
return 1.0l / 0.0l;
249-
#pragma CPROVER check pop
250-
}
251-
252219
/* FUNCTION: __builtin_isinf */
253220

254221
int __builtin_isinf(double d)

0 commit comments

Comments
 (0)