-
Notifications
You must be signed in to change notification settings - Fork 273
Cleanup asserts & throws - replace with invariants - dir util/s* (part1) #2898
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
Cleanup asserts & throws - replace with invariants - dir util/s* (part1) #2898
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to do a bit more than the very mechanical cleanup.
src/util/simplify_expr.cpp
Outdated
return false; | ||
} | ||
else if(operand.is_false()) | ||
{ | ||
expr=from_integer(0, expr_type); | ||
assert(expr.is_not_nil()); | ||
expr = from_integer(0, expr_type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
src/util/simplify_expr.cpp
Outdated
@@ -1373,7 +1373,7 @@ bool simplify_exprt::simplify_update(exprt &expr) | |||
std::size_t number=to_struct_type(value_ptr_type). | |||
component_number(component_name); | |||
|
|||
assert(number<value_ptr->operands().size()); | |||
CHECK_RETURN(number < value_ptr->operands().size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be rewritten to use get_component
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored.
src/util/simplify_expr.cpp
Outdated
assert(expr.operands().size()==1); | ||
DATA_INVARIANT( | ||
expr.operands().size() == 1, | ||
"typecasts must have exactly one argument"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce const typecast_exprt &tc = to_typecast_expr(expr);
above and change the code to use tc
with its proper API. Let's not duplicate consistency checks on expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/util/simplify_expr_boolean.cpp
Outdated
assert(op.operands().size()==2); | ||
DATA_INVARIANT( | ||
op.operands().size() == 2, | ||
"exists expression has two parameters"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please introduce to_exists_expr
which should do this sort of check and use it here.
src/util/simplify_expr_floatbv.cpp
Outdated
assert(expr.operands().size()==2); | ||
PRECONDITION(expr.id() == ID_floatbv_typecast); | ||
DATA_INVARIANT( | ||
expr.operands().size() == 2, "float typecast has two operands"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should take a floatbv_typecast_exprt
as a parameter and should not be doing those checks in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that'd be much cleaner, but note that currently all of the simplify_XXX
work like this (this is also because many of the simplify functions can take many different types of expressions). We could change that but if we were to do that I'd change all of them at once and that's out of scope for this PR IMHO (if we just changed the one the code would be quite inconsistent)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[...] currently all of the [...]
Not quite true - there is sort of a gradual migration happening. Take a look at the declarations in simplify_expr_class.h
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... I don't really like that. It ends up violating structural invariants to still use the input expression as an out parameter, because most simplifications will "return" a different type than they were given... I find the idea of returning something that's not a plus_exprt
through a plus_exprt
reference a bit disturbing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2910 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig Can you take a look at #2939 and tell me if that's roughly in line with what you were thinking? Would you be OK with going ahead with this PR and doing further refactorings in one or more separate PRs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig I'm going to split this part out - and make a note for it to be considered in the context of #2939 and your remarks on #2910 (comment)
see
#2939 (comment) that documents this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR failed Diffblue compatibility checks (cbmc commit: a2c876d).
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
a2c876d
to
9c79c23
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR failed Diffblue compatibility checks (cbmc commit: a2c876d).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/83831905
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 9c79c23).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/84019963
b0b44fa
to
42f660e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR failed Diffblue compatibility checks (cbmc commit: b0b44fa).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/85534606
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 42f660e).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/85532856
src/util/simplify_expr.cpp
Outdated
value_ptr=&value_ptr->operands()[number]; | ||
auto &designator_as_struct_expr = to_struct_expr(*value_ptr); | ||
value_ptr = &designator_as_struct_expr.component(component_name, ns); | ||
CHECK_RETURN(value_ptr->id() != ID_nil); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use value_ptr->is_not_nil()
src/util/simplify_expr.cpp
Outdated
|
||
if(!to_struct_type(value_ptr_type). | ||
has_component(component_name)) | ||
const struct_typet &value_ptr_struct_type=to_struct_type(value_ptr_type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick: whitespace around =
src/util/simplify_expr.cpp
Outdated
expr.operands().size() == 1, | ||
"typecasts must have exactly one argument"); | ||
auto const& typecast_expr = to_typecast_expr(expr); | ||
const typet &op_type=ns.follow(typecast_expr.op().type()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space around =
src/util/simplify_expr.cpp
Outdated
@@ -1429,10 +1419,10 @@ bool simplify_exprt::simplify_object(exprt &expr) | |||
// We do a bit of special treatment for (TYPE *)(a+(int)&o) and | |||
// (TYPE *)(a+(int)((T*)&o+x)), which are re-written to '&o'. | |||
|
|||
exprt tmp=expr.op0(); | |||
if(tmp.id()==ID_plus && tmp.operands().size()==2) | |||
exprt casted_expr=typecast_expr.op(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const exprt &casted_expr = typecast_expr.op();
src/util/simplify_expr.cpp
Outdated
exprt tmp=expr.op0(); | ||
if(tmp.id()==ID_plus && tmp.operands().size()==2) | ||
exprt casted_expr=typecast_expr.op(); | ||
if(casted_expr.id()==ID_plus && casted_expr.operands().size()==2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add spacing around ==
src/util/simplify_expr.cpp
Outdated
{ | ||
exprt cand=tmp.op0().id()==ID_typecast?tmp.op0():tmp.op1(); | ||
exprt cand=casted_expr.op0().id()==ID_typecast?casted_expr.op0():casted_expr.op1(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spacing
src/util/simplify_expr_boolean.cpp
Outdated
simplify_node(expr.op1()); | ||
auto const& op_as_exists = to_exists_expr(op); | ||
forall_exprt rewritten_op(op_as_exists.symbol() | ||
, op_as_exists.where()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forall_exprt rewritten_op(
op_as_exists.symbol(), not_exprt(op_as_exists.where()));
and remove the line below
@@ -1365,7 +1365,8 @@ bool simplify_exprt::simplify_update(exprt &expr) | |||
{ | |||
const irep_idt &component_name= | |||
e.get(ID_component_name); | |||
const struct_typet &value_ptr_struct_type=to_struct_type(value_ptr_type); | |||
const struct_typet &value_ptr_struct_type = | |||
to_struct_type(value_ptr_type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please squash with the previous commit, the clang-format changes are restricted to code that you have introduced in the preceding commit.
8734b32
to
cba81d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR failed Diffblue compatibility checks (cbmc commit: 8734b32).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86134560
Status will be re-evaluated on next push.
Please contact @peterschrammel, @thk123, or @allredj for support.
Common spurious failures:
- the cbmc commit has disappeared in the mean time (e.g. in a force-push)
- the author is not in the list of contributors (e.g. first-time contributors).
cba81d9
to
6c0ab09
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: cba81d9).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86174685
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 6c0ab09).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86178555
Deprecated c-style asserts in favour of cbmc invariants. Some minor refactoring that was relevant to the cleanup.
* Use to_XXX_expr functions instead of asserts to make sure invariants are not violated * Use named accessors instead of op0, op1 etc * Rename variables (e.g. "casted_expr" instead of "op0") for readability
6c0ab09
to
a168b24
Compare
@tautschnig I believe all of your comments (aside from changing the function signatures to be more strongly typed which will happen in another PR as previously discussed) have been addressed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: a168b24).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86316058
Cleanup of asserts - replaced with invariants.
Removed all unstructured
throw(...)
statements, replacing withINVARIANT
,PRECONDITION
, etc. where appropriate.