Skip to content

Commit 5541c09

Browse files
committed
Mimic GCC/Clang simplification behaviour when type checking ?:
Neither GCC nor Clang simplify expressions involving symbols even when the result would always be 0, which our simplifier figure out.
1 parent c71fcad commit 5541c09

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

regression/ansi-c/sizeof6/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int main()
2+
{
3+
long long i;
4+
#ifndef _MSC_VER
5+
_Static_assert(sizeof(int) == sizeof(*(1 ? ((void *)(0ll)) : (int *)1)), "");
6+
// We are able to simplify all of the expressions involving i below to 0, but
7+
// GCC and Clang don't do so. Hence, the static asserts pass for those
8+
// compilers.
9+
_Static_assert(
10+
sizeof(int) != sizeof(*(1 ? ((void *)(i * 0)) : (int *)1)), "");
11+
_Static_assert(
12+
sizeof(int) != sizeof(*(1 ? ((void *)(i - i)) : (int *)1)), "");
13+
_Static_assert(
14+
sizeof(int) != sizeof(*(1 ? ((void *)(i ? 0ll : 0ll)) : (int *)1)), "");
15+
_Static_assert(
16+
sizeof(int) != sizeof(*(1 ? ((void *)(0 ? i : 0ll)) : (int *)1)), "");
17+
#else
18+
static_assert(sizeof(int) == sizeof(*(1 ? ((void *)(0)) : (int *)1)), "");
19+
// Visual Studio rejects this as "illegal indirection"
20+
// static_assert(
21+
// sizeof(int) == sizeof(*(1 ? ((void *)(i * 0)) : (int *)1)), "");
22+
#endif
23+
}

regression/ansi-c/sizeof6/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,16 +1625,23 @@ void c_typecheck_baset::typecheck_expr_trinary(if_exprt &expr)
16251625
exprt tmp1=simplify_expr(operands[1], *this);
16261626
exprt tmp2=simplify_expr(operands[2], *this);
16271627

1628-
// is one of them void * AND null? Convert that to the other.
1629-
// (at least that's how GCC behaves)
1630-
if(operands[1].type().subtype().id()==ID_empty &&
1631-
tmp1.is_constant() &&
1632-
to_constant_expr(tmp1).get_value()==ID_NULL)
1628+
// Is one of them void * AND null? Convert that to the other.
1629+
// (At least that's how GCC, Clang, and Visual Studio behave. Presence of
1630+
// symbols blocks them from simplifying the expression to NULL.)
1631+
if(
1632+
operands[1].type().subtype().id() == ID_empty && tmp1.is_constant() &&
1633+
to_constant_expr(tmp1).get_value() == ID_NULL &&
1634+
find_symbols(operands[1]).empty())
1635+
{
16331636
implicit_typecast(operands[1], operands[2].type());
1634-
else if(operands[2].type().subtype().id()==ID_empty &&
1635-
tmp2.is_constant() &&
1636-
to_constant_expr(tmp2).get_value()==ID_NULL)
1637+
}
1638+
else if(
1639+
operands[2].type().subtype().id() == ID_empty && tmp2.is_constant() &&
1640+
to_constant_expr(tmp2).get_value() == ID_NULL &&
1641+
find_symbols(operands[2]).empty())
1642+
{
16371643
implicit_typecast(operands[2], operands[1].type());
1644+
}
16381645
else if(operands[1].type().subtype().id()!=ID_code ||
16391646
operands[2].type().subtype().id()!=ID_code)
16401647
{

0 commit comments

Comments
 (0)