Skip to content

Commit cc64b04

Browse files
authored
Merge pull request #7556 from tautschnig/features/simp-is_null
Simplify tests of is-null that use integer types
2 parents a8abbf1 + 5d87cf0 commit cc64b04

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

regression/cbmc/null8/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
union U
2+
{
3+
void *ptr;
4+
__CPROVER_size_t n;
5+
};
6+
7+
int main()
8+
{
9+
int *p = __CPROVER_allocate(sizeof(int), 0);
10+
union U u = {.ptr = p};
11+
__CPROVER_assert(u.n != 0, "is not null");
12+
}

regression/cbmc/null8/test.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE new-smt-backend
2+
main.c
3+
4+
Generated 1 VCC\(s\), 0 remaining after simplification
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
^warning: ignoring

src/util/simplify_expr_int.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,43 @@ simplify_exprt::resultt<> simplify_exprt::simplify_inequality_rhs_is_constant(
18971897
}
18981898
}
18991899
}
1900+
1901+
if(config.ansi_c.NULL_is_zero)
1902+
{
1903+
const exprt &maybe_tc_op = skip_typecast(expr.op0());
1904+
if(maybe_tc_op.type().id() == ID_pointer)
1905+
{
1906+
// make sure none of the type casts lose information
1907+
const pointer_typet &p_type = to_pointer_type(maybe_tc_op.type());
1908+
bool bitwidth_unchanged = true;
1909+
const exprt *ep = &(expr.op0());
1910+
while(bitwidth_unchanged && ep->id() == ID_typecast)
1911+
{
1912+
if(auto t = type_try_dynamic_cast<bitvector_typet>(ep->type()))
1913+
{
1914+
bitwidth_unchanged = t->get_width() == p_type.get_width();
1915+
}
1916+
else
1917+
bitwidth_unchanged = false;
1918+
1919+
ep = &to_typecast_expr(*ep).op();
1920+
}
1921+
1922+
if(bitwidth_unchanged)
1923+
{
1924+
if(expr.id() == ID_equal || expr.id() == ID_ge || expr.id() == ID_le)
1925+
{
1926+
return changed(simplify_rec(
1927+
equal_exprt{maybe_tc_op, null_pointer_exprt{p_type}}));
1928+
}
1929+
else
1930+
{
1931+
return changed(simplify_rec(
1932+
notequal_exprt{maybe_tc_op, null_pointer_exprt{p_type}}));
1933+
}
1934+
}
1935+
}
1936+
}
19001937
}
19011938

19021939
// are we comparing with a typecast from bool?

0 commit comments

Comments
 (0)