Skip to content

Move is_{true,false,zero,one} from exprt to constant_exprt #8664

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion jbmc/src/java_bytecode/java_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,12 @@ void java_object_factoryt::gen_nondet_init(
else
{
exprt within_bounds = interval.make_contains_expr(expr);
if(!within_bounds.is_true())
if(
!within_bounds.is_constant() ||
!to_constant_expr(within_bounds).is_true())
{
assignments.add(code_assumet(std::move(within_bounds)));
}
}

if(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@

static bool is_assume_false(goto_programt::const_targett inst)
{
return inst->is_assume() && inst->condition().is_false();
return inst->is_assume() && inst->condition().is_constant() &&
to_constant_expr(inst->condition()).is_false();
}

/// Interpret `program`, resolving classid comparisons assuming any actual
Expand All @@ -90,17 +91,20 @@
{
exprt guard = pc->condition();
guard = resolve_classid_test(guard, actual_class_id, ns);
if(guard.is_true())
if(!guard.is_constant())
{
// Can't resolve the test, so exit here:
return pc;

Check warning on line 97 in jbmc/unit/java_bytecode/goto-programs/remove_virtual_functions_without_fallback.cpp

View check run for this annotation

Codecov / codecov/patch

jbmc/unit/java_bytecode/goto-programs/remove_virtual_functions_without_fallback.cpp#L97

Added line #L97 was not covered by tests
}
else if(to_constant_expr(guard).is_true())
{
REQUIRE(pc->targets.begin() != pc->targets.end());
pc = *(pc->targets.begin());
}
else if(guard.is_false())
++pc;
else
{
// Can't resolve the test, so exit here:
return pc;
CHECK_RETURN(to_constant_expr(guard).is_false());
++pc;
}
}
else if(pc->type() == SKIP)
Expand Down
6 changes: 3 additions & 3 deletions src/analyses/constant_propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ void constant_propagator_domaint::transform(
else
g = not_exprt(from->condition());
partial_evaluate(values, g, ns);
if(g.is_false())
values.set_to_bottom();
if(g.is_constant() && to_constant_expr(g).is_false())
values.set_to_bottom();
else
two_way_propagate_rec(g, ns, cp);
}
Expand Down Expand Up @@ -376,7 +376,7 @@ bool constant_propagator_domaint::two_way_propagate_rec(

// x != FALSE ==> x == TRUE

if(rhs.is_zero() || rhs.is_false())
if(to_constant_expr(rhs).is_zero() || to_constant_expr(rhs).is_false())
rhs = from_integer(1, rhs.type());
else
rhs = from_integer(0, rhs.type());
Expand Down
18 changes: 9 additions & 9 deletions src/analyses/custom_bitvector_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@

const exprt result2 = simplify_expr(eval(guard, cba), ns);

if(result2.is_false())
if(result2.is_constant() && to_constant_expr(result2).is_false())

Check warning on line 533 in src/analyses/custom_bitvector_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/custom_bitvector_analysis.cpp#L533

Added line #L533 was not covered by tests
make_bottom();
}
break;
Expand Down Expand Up @@ -814,12 +814,12 @@
if(use_xml)
{
out << "<result status=\"";
if(result.is_true())
if(!result.is_constant())
out << "UNKNOWN";
else if(to_constant_expr(result).is_true())

Check warning on line 819 in src/analyses/custom_bitvector_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/custom_bitvector_analysis.cpp#L817-L819

Added lines #L817 - L819 were not covered by tests
out << "SUCCESS";
else if(result.is_false())
out << "FAILURE";
else
out << "UNKNOWN";
out << "FAILURE";

Check warning on line 822 in src/analyses/custom_bitvector_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/custom_bitvector_analysis.cpp#L822

Added line #L822 was not covered by tests
out << "\">\n";
out << xml(i_it->source_location());
out << "<description>"
Expand All @@ -838,12 +838,12 @@
out << '\n';
}

if(result.is_true())
if(!result.is_constant())
unknown++;
else if(to_constant_expr(result).is_true())

Check warning on line 843 in src/analyses/custom_bitvector_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/custom_bitvector_analysis.cpp#L841-L843

Added lines #L841 - L843 were not covered by tests
pass++;
else if(result.is_false())
fail++;
else
unknown++;
fail++;

Check warning on line 846 in src/analyses/custom_bitvector_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/custom_bitvector_analysis.cpp#L846

Added line #L846 was not covered by tests
}

if(!use_xml)
Expand Down
20 changes: 10 additions & 10 deletions src/analyses/goto_rw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@
const range_spect &range_start,
const range_spect &size)
{
if(if_expr.cond().is_false())
get_objects_rec(mode, if_expr.false_case(), range_start, size);
else if(if_expr.cond().is_true())
get_objects_rec(mode, if_expr.true_case(), range_start, size);
else
if(!if_expr.cond().is_constant())
{
get_objects_rec(get_modet::READ, if_expr.cond());

get_objects_rec(mode, if_expr.false_case(), range_start, size);
get_objects_rec(mode, if_expr.true_case(), range_start, size);
}
else if(to_constant_expr(if_expr.cond()).is_false())
get_objects_rec(mode, if_expr.false_case(), range_start, size);

Check warning on line 127 in src/analyses/goto_rw.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/goto_rw.cpp#L126-L127

Added lines #L126 - L127 were not covered by tests
else
get_objects_rec(mode, if_expr.true_case(), range_start, size);

Check warning on line 129 in src/analyses/goto_rw.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/goto_rw.cpp#L129

Added line #L129 was not covered by tests
}

void rw_range_sett::get_objects_dereference(
Expand Down Expand Up @@ -735,11 +735,7 @@
const range_spect &range_start,
const range_spect &size)
{
if(if_expr.cond().is_false())
get_objects_rec(mode, if_expr.false_case(), range_start, size);
else if(if_expr.cond().is_true())
get_objects_rec(mode, if_expr.true_case(), range_start, size);
else
if(!if_expr.cond().is_constant())

Check warning on line 738 in src/analyses/goto_rw.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/goto_rw.cpp#L738

Added line #L738 was not covered by tests
{
get_objects_rec(get_modet::READ, if_expr.cond());

Expand All @@ -753,6 +749,10 @@
get_objects_rec(mode, if_expr.true_case(), range_start, size);
guard = std::move(copy);
}
else if(to_constant_expr(if_expr.cond()).is_false())
get_objects_rec(mode, if_expr.false_case(), range_start, size);

Check warning on line 753 in src/analyses/goto_rw.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/goto_rw.cpp#L752-L753

Added lines #L752 - L753 were not covered by tests
else
get_objects_rec(mode, if_expr.true_case(), range_start, size);

Check warning on line 755 in src/analyses/goto_rw.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/goto_rw.cpp#L755

Added line #L755 was not covered by tests
}

void rw_guarded_range_set_value_sett::add(
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/guard_bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
else
{
if(expr.is_false())
if(expr.is_constant() && to_constant_expr(expr).is_false())

Check warning on line 47 in src/analyses/guard_bdd.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/guard_bdd.cpp#L47

Added line #L47 was not covered by tests
{
return boolean_negate(as_expr());
}
Expand Down
11 changes: 7 additions & 4 deletions src/analyses/guard_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exprt guard_exprt::guard_expr(exprt expr) const
}
else
{
if(expr.is_false())
if(expr.is_constant() && to_constant_expr(expr).is_false())
{
return boolean_negate(as_expr());
}
Expand All @@ -39,9 +39,10 @@ void guard_exprt::add(const exprt &expr)
{
PRECONDITION(expr.is_boolean());

if(is_false() || expr.is_true())
if(is_false() || (expr.is_constant() && to_constant_expr(expr).is_true()))
return;
else if(is_true() || expr.is_false())
else if(
is_true() || (expr.is_constant() && to_constant_expr(expr).is_false()))
{
this->expr = expr;

Expand Down Expand Up @@ -198,7 +199,9 @@ guard_exprt &operator|=(guard_exprt &g1, const guard_exprt &g2)

if(tmp != and_expr1)
{
if(and_expr1.is_true() || and_expr2.is_true())
if(
(and_expr1.is_constant() && to_constant_expr(and_expr1).is_true()) ||
(and_expr2.is_constant() && to_constant_expr(and_expr2).is_true()))
{
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/analyses/guard_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Author: Daniel Kroening, kroening@kroening.com
#ifndef CPROVER_ANALYSES_GUARD_EXPR_H
#define CPROVER_ANALYSES_GUARD_EXPR_H

#include <util/expr.h>
#include <util/std_expr.h>

/// This is unused by this implementation of guards, but can be used by other
/// implementations of the same interface.
Expand Down Expand Up @@ -59,12 +59,12 @@ class guard_exprt

bool is_true() const
{
return expr.is_true();
return expr.is_constant() && to_constant_expr(expr).is_true();
}

bool is_false() const
{
return expr.is_false();
return expr.is_constant() && to_constant_expr(expr).is_false();
}

friend guard_exprt &operator-=(guard_exprt &g1, const guard_exprt &g2);
Expand Down
7 changes: 5 additions & 2 deletions src/analyses/interval_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
{
goto_programt::const_targett previous = std::prev(i_it);

if(previous->is_goto() && !previous->condition().is_true())
if(
previous->is_goto() &&
(!previous->condition().is_constant() ||
!to_constant_expr(previous->condition()).is_true()))

Check warning on line 52 in src/analyses/interval_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/interval_analysis.cpp#L49-L52

Added lines #L49 - L52 were not covered by tests
{
// we follow a branch, instrument
}
Expand All @@ -69,7 +72,7 @@
for(const auto &symbol_expr : symbols)
{
exprt tmp=d.make_expression(symbol_expr);
if(!tmp.is_true())
if(!tmp.is_constant() || !to_constant_expr(tmp).is_true())

Check warning on line 75 in src/analyses/interval_analysis.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/interval_analysis.cpp#L75

Added line #L75 was not covered by tests
assertion.push_back(tmp);
}

Expand Down
6 changes: 4 additions & 2 deletions src/analyses/interval_domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ bool interval_domaint::ai_simplify(
// of when condition is true
if(!a.join(d)) // If d (this) is included in a...
{ // Then the condition is always true
unchanged=condition.is_true();
unchanged =
condition.is_constant() && to_constant_expr(condition).is_true();
condition = true_exprt();
}
}
Expand All @@ -514,7 +515,8 @@ bool interval_domaint::ai_simplify(
d.assume(not_exprt(condition), ns); // Restrict to when condition is false
if(d.is_bottom()) // If there there are none...
{ // Then the condition is always true
unchanged=condition.is_true();
unchanged =
condition.is_constant() && to_constant_expr(condition).is_true();
condition = true_exprt();
}
}
Expand Down
38 changes: 22 additions & 16 deletions src/analyses/invariant_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,17 @@
return;
}

if(expr.is_true())
{
// do nothing, it's useless
}
else if(expr.is_false())
if(expr.is_constant())

Check warning on line 397 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L397

Added line #L397 was not covered by tests
{
// wow, that's strong
make_false();
if(to_constant_expr(expr).is_true())

Check warning on line 399 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L399

Added line #L399 was not covered by tests
{
// do nothing, it's useless
}
else
{
// wow, that's strong
make_false();

Check warning on line 406 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L406

Added line #L406 was not covered by tests
}
}
else if(expr.id()==ID_not)
{
Expand Down Expand Up @@ -596,7 +599,7 @@
if(is_false) // can't get any stronger
return tvt(true);

if(expr.is_true())
if(expr.is_constant() && to_constant_expr(expr).is_true())

Check warning on line 602 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L602

Added line #L602 was not covered by tests
return tvt(true);
else if(expr.id()==ID_not)
{
Expand Down Expand Up @@ -701,15 +704,18 @@
if(!expr.is_boolean())
throw "nnf: non-Boolean expression";

if(expr.is_true())
{
if(negate)
expr=false_exprt();
}
else if(expr.is_false())
if(expr.is_constant())

Check warning on line 707 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L707

Added line #L707 was not covered by tests
{
if(negate)
expr=true_exprt();
if(to_constant_expr(expr).is_true())

Check warning on line 709 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L709

Added line #L709 was not covered by tests
{
if(negate)
expr = false_exprt();

Check warning on line 712 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L711-L712

Added lines #L711 - L712 were not covered by tests
}
else
{
if(negate)
expr = true_exprt();

Check warning on line 717 in src/analyses/invariant_set.cpp

View check run for this annotation

Codecov / codecov/patch

src/analyses/invariant_set.cpp#L716-L717

Added lines #L716 - L717 were not covered by tests
}
}
else if(expr.id()==ID_not)
{
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/local_bitvector_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ local_bitvector_analysist::flagst local_bitvector_analysist::get_rec(
{
if(rhs.is_constant())
{
if(rhs.is_zero())
if(to_constant_expr(rhs).is_zero())
return flagst::mk_null();
else
return flagst::mk_integer_address();
Expand Down
6 changes: 5 additions & 1 deletion src/analyses/local_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ void local_cfgt::build(const goto_programt &goto_program)
switch(instruction.type())
{
case GOTO:
if(!instruction.condition().is_true())
if(
!instruction.condition().is_constant() ||
!to_constant_expr(instruction.condition()).is_true())
{
node.successors.push_back(loc_nr+1);
}

for(const auto &target : instruction.targets)
{
Expand Down
2 changes: 1 addition & 1 deletion src/analyses/local_may_alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void local_may_aliast::get_rec(
{
if(rhs.is_constant())
{
if(rhs.is_zero())
if(to_constant_expr(rhs).is_zero())
dest.insert(objects.number(exprt(ID_null_object)));
else
dest.insert(objects.number(exprt(ID_integer_address_object)));
Expand Down
10 changes: 5 additions & 5 deletions src/analyses/variable-sensitivity/abstract_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ bool abstract_environmentt::assume(const exprt &expr, const namespacet &ns)
// Should be of the right type
INVARIANT(assumption.is_boolean(), "simplification preserves type");

if(assumption.is_false())
if(assumption.is_constant() && to_constant_expr(assumption).is_false())
{
bool currently_bottom = is_bottom();
make_bottom();
Expand Down Expand Up @@ -579,10 +579,10 @@ static auto inverse_operations =

static exprt invert_result(const exprt &result)
{
if(!result.is_boolean())
if(!result.is_boolean() || !result.is_constant())
return result;

if(result.is_true())
if(to_constant_expr(result).is_true())
return false_exprt();
return true_exprt();
}
Expand Down Expand Up @@ -640,7 +640,7 @@ exprt assume_and(
for(auto const &operand : and_expr.operands())
{
auto result = env.do_assume(operand, ns);
if(result.is_false())
if(result.is_constant() && to_constant_expr(result).is_false())
return result;
nil |= result.is_nil();
}
Expand Down Expand Up @@ -833,7 +833,7 @@ exprt assume_less_than(
auto reduced_le_expr =
binary_relation_exprt(left_lower, expr.id(), right_upper);
auto result = env.eval(reduced_le_expr, ns)->to_constant();
if(result.is_true())
if(result.is_constant() && to_constant_expr(result).is_true())
{
if(is_assignable(operands.lhs))
{
Expand Down
Loading
Loading