-
Notifications
You must be signed in to change notification settings - Fork 273
Restore member-of-union expressions in trace output #5633
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/byte_operators.h> | ||
#include <util/c_types.h> | ||
#include <util/pointer_expr.h> | ||
#include <util/pointer_offset_size.h> | ||
#include <util/std_code.h> | ||
|
||
#include <goto-programs/goto_model.h> | ||
|
@@ -116,3 +117,65 @@ void rewrite_union(goto_modelt &goto_model) | |
{ | ||
rewrite_union(goto_model.goto_functions); | ||
} | ||
|
||
/// Undo the union access -> byte_extract replacement that rewrite_union did for | ||
/// the purpose of displaying expressions to users. | ||
/// \param expr: expression to inspect and possibly transform | ||
/// \param ns: namespace | ||
/// \return True if, and only if, the expression is unmodified | ||
static bool restore_union_rec(exprt &expr, const namespacet &ns) | ||
{ | ||
bool unmodified = true; | ||
|
||
Forall_operands(it, expr) | ||
unmodified &= restore_union_rec(*it, ns); | ||
|
||
if( | ||
expr.id() == ID_byte_extract_little_endian || | ||
expr.id() == ID_byte_extract_big_endian) | ||
{ | ||
byte_extract_exprt &be = to_byte_extract_expr(expr); | ||
if(be.op().type().id() == ID_union || be.op().type().id() == ID_union_tag) | ||
{ | ||
const union_typet &union_type = to_union_type(ns.follow(be.op().type())); | ||
|
||
for(const auto &comp : union_type.components()) | ||
{ | ||
if(be.offset().is_zero() && be.type() == comp.type()) | ||
{ | ||
expr = member_exprt{be.op(), comp.get_name(), be.type()}; | ||
return false; | ||
} | ||
else if( | ||
comp.type().id() == ID_array || comp.type().id() == ID_struct || | ||
comp.type().id() == ID_struct_tag) | ||
{ | ||
optionalt<exprt> result = get_subexpression_at_offset( | ||
member_exprt{be.op(), comp.get_name(), comp.type()}, | ||
be.offset(), | ||
be.type(), | ||
ns); | ||
if(result.has_value()) | ||
{ | ||
expr = *result; | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return unmodified; | ||
} | ||
|
||
/// Undo the union access -> byte_extract replacement that rewrite_union did for | ||
/// the purpose of displaying expressions to users. | ||
/// \param expr: expression to inspect and possibly transform | ||
/// \param ns: namespace | ||
void restore_union(exprt &expr, const namespacet &ns) | ||
{ | ||
exprt tmp = expr; | ||
|
||
if(!restore_union_rec(tmp, ns)) | ||
expr.swap(tmp); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The difference between
original_condition
andcond_expr
is non-obvious, and does deserve a comment.Do we really need both?
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 was tempted to just re-purpose
cond_expr
, but it turns out it is actually used bygoto-checker/bmc_util.cpp
(I believe that is the only use).cond_expr
is the SSA-renamed expression, while we'd want to provide an expression that is meaningful to the user. So maybe adding a comment is the right approach?