Skip to content

Commit 506cd0d

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. Fixes: #7927
1 parent a531056 commit 506cd0d

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

regression/ansi-c/sizeof6/main.c

+23
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

+8
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

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/config.h>
1616
#include <util/cprover_prefix.h>
1717
#include <util/expr_util.h>
18+
#include <util/find_symbols.h>
1819
#include <util/floatbv_expr.h>
1920
#include <util/ieee_float.h>
2021
#include <util/mathematical_expr.h>
@@ -1608,17 +1609,20 @@ void c_typecheck_baset::typecheck_expr_trinary(if_exprt &expr)
16081609
exprt tmp1=simplify_expr(operands[1], *this);
16091610
exprt tmp2=simplify_expr(operands[2], *this);
16101611

1611-
// is one of them void * AND null? Convert that to the other.
1612-
// (at least that's how GCC behaves)
1612+
// Is one of them void * AND null? Convert that to the other.
1613+
// (At least that's how GCC, Clang, and Visual Studio behave. Presence of
1614+
// symbols blocks them from simplifying the expression to NULL.)
16131615
if(
16141616
to_pointer_type(operands[1].type()).base_type().id() == ID_empty &&
1615-
tmp1.is_constant() && is_null_pointer(to_constant_expr(tmp1)))
1617+
tmp1.is_constant() && is_null_pointer(to_constant_expr(tmp1)) &&
1618+
find_symbols(operands[1]).empty())
16161619
{
16171620
implicit_typecast(operands[1], operands[2].type());
16181621
}
16191622
else if(
16201623
to_pointer_type(operands[2].type()).base_type().id() == ID_empty &&
1621-
tmp2.is_constant() && is_null_pointer(to_constant_expr(tmp2)))
1624+
tmp2.is_constant() && is_null_pointer(to_constant_expr(tmp2)) &&
1625+
find_symbols(operands[2]).empty())
16221626
{
16231627
implicit_typecast(operands[2], operands[1].type());
16241628
}

0 commit comments

Comments
 (0)