-
Notifications
You must be signed in to change notification settings - Fork 273
Refactor element access of pointer arrays into a separate function #4525
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
Refactor element access of pointer arrays into a separate function #4525
Conversation
3f3a702
to
4b7cf97
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: 4b7cf97).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108164284
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 is not JBMC-specific -- suggest move to util/
@@ -191,6 +191,12 @@ dereference_exprt checked_dereference(const exprt &expr) | |||
return result; | |||
} | |||
|
|||
dereference_exprt | |||
element_at_pointer_array_index(const exprt &pointer, const exprt &index) |
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.
Suggested name array_element_from_pointer
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.
Changed the name to your suggestion.
dereference_exprt | ||
element_at_pointer_array_index(const exprt &pointer, const exprt &index) | ||
{ | ||
return dereference_exprt{plus_exprt{pointer, index}}; |
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.
💡 Would it be a good idea to have something like INVARIANT(can_cast_type<pointer_typet>(pointer.type()));
at the beginning of this function?
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 (using PRECONDITION
).
4b7cf97
to
779f5df
Compare
This is rebased on develop 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.
I think this is fine for C as well. I'm wondering, however, whether util/pointer_offset_sum.h
would also be an ok header to place this? No particularly strong feelings about it, though.
array_element_from_pointer(const exprt &pointer, const exprt &index) | ||
{ | ||
PRECONDITION(can_cast_type<pointer_typet>(pointer.type())); | ||
return dereference_exprt{plus_exprt{pointer, index}}; |
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.
While at it, maybe also do PRECONDITION(index.type().id() == ID_signedbv || index.type().id() == ID_unsignedbv);
? I think those are the only two reasonable options, although maybe we could also work with some other bitvector types.
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 added the precondition. If we need other types in the future we can always add them later.
@@ -0,0 +1,32 @@ | |||
/*******************************************************************\ | |||
|
|||
Module: Util |
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 is usually a description of the file, not the directory
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 had seen Module: Util
in another file and just copied that. Most files in util
seem to just leave this empty... Anyway, I changed this one to "Element access in a pointer array".
/// given index of a pointer array: | ||
/// `*(pointer + index)` | ||
/// Arrays are sometimes (always in JBMC) represented as a pointer to their | ||
/// first element. This is because we cannot know the type of an array of |
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.
Suggest replace the second sentence with just , especially when their length is uncertain
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 replaced a part of the second sentence with that. 🙂
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: 779f5df).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108340077
This does not save many lines of code, but the way we access arrays using pointer arithmetic can be a bit confusing and it makes sense to abstract it away and keep the documentation of why we do things this way in one place.
779f5df
to
2088544
Compare
@tautschnig |
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: 2088544).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108571279
This PR is based on #4524. Please only review the last commit.