Skip to content

Fix quantifiers with nested statement-expressions #8616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions regression/cbmc/Quantifiers-statement-expression/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ int main()
// clang-format off
// no side effects!
int j = 0;
//assert(j++);
//assert(({int i = 0; while(i <3) i++; i <3;}));
int a[5] = {0 , 0, 0, 0, 0};
assert(__CPROVER_forall { int i; ({ int j = i; i=i; if(i < 0 || i >4) i = 1; ( a[i] < 5); }) });
// clang-format on
Expand Down
11 changes: 11 additions & 0 deletions regression/cbmc/Quantifiers-statement-expression3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int main()
{
// clang-format off
// no side effects!
int j;
int a[5] = {0 , 0, 0, 0, 0};
assert(__CPROVER_forall { int i; ({ ( 0 <= i && i < 4) ==> ({ int k = j; if(j < 0 || j > i) k = 1; ( a[k] == 0); }); }) });
// clang-format on

return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc/Quantifiers-statement-expression3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
36 changes: 29 additions & 7 deletions src/ansi-c/goto-conversion/goto_clean_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
{
return find_base_symbol(to_dereference_expr(expr).pointer());
}
else if(expr.id() == ID_typecast)

Check warning on line 46 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L46

Added line #L46 was not covered by tests
{
return find_base_symbol(to_typecast_expr(expr).op());

Check warning on line 48 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L48

Added line #L48 was not covered by tests
}
else if(expr.id() == ID_address_of)

Check warning on line 50 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L50

Added line #L50 was not covered by tests
{
return find_base_symbol(to_address_of_expr(expr).op());

Check warning on line 52 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L52

Added line #L52 was not covered by tests
}
else
{
throw "unsupported expression type for finding base symbol";
Expand All @@ -63,6 +71,10 @@
INVARIANT(
natural_loops.loop_map.size() == 0, "quantifier must not contain loops");

std::unordered_set<symbol_exprt, irep_hash> declared_symbols;
// All bound variables are local.
declared_symbols.insert(qex.variables().begin(), qex.variables().end());

// `last` is the instruction corresponding to the last expression in the
// statement expression.
goto_programt::const_targett last = where.instructions.end();
Expand All @@ -75,13 +87,24 @@
{
last = it;
}

if(it->is_decl())
{
declared_symbols.insert(it->decl_symbol());
}
}

DATA_INVARIANT(
last != where.instructions.end(),
"expression statements must contain a terminator expression");

auto last_expr = to_code_expression(last->get_other()).expression();
if(
last_expr.id() == ID_typecast &&
to_typecast_expr(last_expr).type().id() == ID_empty)

Check warning on line 104 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L104

Added line #L104 was not covered by tests
{
to_typecast_expr(last_expr).type() = bool_typet();

Check warning on line 106 in src/ansi-c/goto-conversion/goto_clean_expr.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_clean_expr.cpp#L106

Added line #L106 was not covered by tests
}

struct pathst
{
Expand Down Expand Up @@ -139,10 +162,6 @@
{1, where.instructions.begin()},
{1, std::make_pair(true_exprt(), replace_mapt())});

std::unordered_set<symbol_exprt, irep_hash> declared_symbols;
// All bound variables are local.
declared_symbols.insert(qex.variables().begin(), qex.variables().end());

exprt res = true_exprt();

// Visit the quantifier body along `paths`.
Expand All @@ -151,9 +170,12 @@
auto &current_it = paths.back_it();
auto &path_condition = paths.back_path_condition();
auto &value_map = paths.back_value_map();
INVARIANT(
current_it != where.instructions.end(),
"Quantifier body must have a unique end expression.");

if(current_it == where.instructions.end())
{
paths.pop_back();
continue;
}

switch(current_it->type())
{
Expand Down
Loading