Skip to content

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

Merged

Conversation

antlechner
Copy link
Contributor

@antlechner antlechner commented Apr 12, 2019

This PR is based on #4524. Please only review the last commit.

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • n/a Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • n/a My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

Copy link
Contributor

@allredj allredj left a 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

@tautschnig tautschnig changed the title Refactor element access of pointer arrays into a separate function (review last commit only) Refactor element access of pointer arrays into a separate function (review last commit only) [depends-on: #4524] Apr 13, 2019
Copy link
Contributor

@smowton smowton left a 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)
Copy link
Contributor

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

Copy link
Contributor Author

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}};
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (using PRECONDITION).

@antlechner
Copy link
Contributor Author

This is rebased on develop now.
@smowton I moved it to a new file util/array_element_from_pointer.h and changed the documentation slightly. I don't actually know how we represent arrays for C / when we represent them as pointers to the first element, so let me know if anything should be added to the documentation as it is now.

Copy link
Collaborator

@tautschnig tautschnig left a 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}};
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

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. 🙂

Copy link
Contributor

@allredj allredj left a 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

@peterschrammel peterschrammel changed the title Refactor element access of pointer arrays into a separate function (review last commit only) [depends-on: #4524] Refactor element access of pointer arrays into a separate function Apr 15, 2019
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.
@antlechner antlechner force-pushed the antonia/array-index-arithmetic branch from 779f5df to 2088544 Compare April 16, 2019 17:41
@antlechner
Copy link
Contributor Author

@tautschnig pointer_offset_sum.h contains only one function with the same name as the file, so I'll keep this one separate for now to avoid confusion.

Copy link
Contributor

@allredj allredj left a 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

@tautschnig tautschnig merged commit abbafd2 into diffblue:develop Apr 17, 2019
@antlechner antlechner deleted the antonia/array-index-arithmetic branch April 17, 2019 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants