Skip to content

GCC switch range: don't enumerate elements #5724

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
Jan 8, 2021
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
7 changes: 6 additions & 1 deletion regression/cbmc/gcc_switch_case_range1/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <limits.h>

int main()
{
int x;
Expand All @@ -18,9 +20,12 @@ int main()
#endif
__CPROVER_assert(0, "... works");
break;
case 13:
case 14:
__CPROVER_assert(0, "13 works");
break;
case 15 ... INT_MAX:
__CPROVER_assert(0, "large range works");
break;
default:
__CPROVER_assert(0, "default works");
break;
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/gcc_switch_case_range1/test.desc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ main.c
^EXIT=10$
^SIGNAL=0$
^VERIFICATION FAILED$
^\*\* 4 of 4 failed
^\*\* 5 of 5 failed
--
^warning: ignoring
35 changes: 22 additions & 13 deletions src/goto-programs/goto_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ void goto_convertt::convert_gcc_switch_case_range(
targets.cases_map.insert({target, --targets.cases.end()}).first;
}

exprt::operandst &case_op_dest = cases_entry->second->second;

for(mp_integer i = *lb; i <= *ub; ++i)
case_op_dest.push_back(from_integer(i, code.lower().type()));
// create a skeleton for case_guard
cases_entry->second->second.push_back(
and_exprt{binary_relation_exprt{code.lower(), ID_le, nil_exprt{}},
binary_relation_exprt{nil_exprt{}, ID_le, code.upper()}});
}

/// converts 'code' and appends the result to 'dest'
Expand Down Expand Up @@ -1100,18 +1100,27 @@ exprt goto_convertt::case_guard(
{
PRECONDITION(!case_op.empty());

if(case_op.size() == 1)
return equal_exprt(value, case_op.at(0));
else
{
exprt::operandst disjuncts;
disjuncts.reserve(case_op.size());
exprt::operandst disjuncts;
disjuncts.reserve(case_op.size());

for(const auto &op : case_op)
for(const auto &op : case_op)
{
// could be a skeleton generated by convert_gcc_switch_case_range
if(op.id() == ID_and)
{
const binary_exprt &and_expr = to_binary_expr(op);
PRECONDITION(to_binary_expr(and_expr.op0()).op1().is_nil());
PRECONDITION(to_binary_expr(and_expr.op1()).op0().is_nil());
binary_exprt skeleton = and_expr;
to_binary_expr(skeleton.op0()).op1() = value;
to_binary_expr(skeleton.op1()).op0() = value;
disjuncts.push_back(skeleton);
}
else
disjuncts.push_back(equal_exprt(value, op));

return disjunction(disjuncts);
}

return disjunction(disjuncts);
}

void goto_convertt::convert_switch(
Expand Down