Skip to content

Factor out computation of maximum union member #5251

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 1 commit into from
Mar 3, 2020

Conversation

tautschnig
Copy link
Collaborator

Avoids duplicate code and enables future re-use.

This is in preparation of further union support in byte-extract lowering. Refactoring is tested via existing unit tests.

  • 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/
  • 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).
  • 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

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

Cosmetic components

/// \return Pair of a member_exprt pointing to the maximum fixed bit-width
/// member of \p union_type and the bit width of that member.
static optionalt<std::pair<member_exprt, mp_integer>>
find_maximum_union_component(
Copy link
Contributor

Choose a reason for hiding this comment

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

maximum -> widest?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed (and left-hand sides are renamed accordingly).

return {};
else
return std::make_pair(
member_exprt{union_expr, max_comp_name, max_comp_type}, max_width);
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit surprising that a utility would make a member-expression, rather than just returning the widest component

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call - this makes one use slightly more work (having to construct the member_exprt in place), but avoids a parameter that isn't really necessary and could be used in a wrong way.

Avoids duplicate code and enables future re-use.
Copy link
Contributor

@chrisr-diffblue chrisr-diffblue left a comment

Choose a reason for hiding this comment

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

Happy enough for this to go in as it is, but just a mild query around the interface of find_widest_union_component

/// \return Pair of a componentt pointing to the maximum fixed bit-width
/// member of \p union_type and the bit width of that member.
static optionalt<std::pair<struct_union_typet::componentt, mp_integer>>
find_widest_union_component(const union_typet &union_type, const namespacet &ns)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any particular reason this returns an optional pair, rather than just an optional componentt ? Given that the componentt has the the appropriate type in it and the caller could just do pointer_offset_bits themselves. If it's just to simplify future clients, then fine :-) But just wondering as I don't see any current clients using the second half of the pair, and having the extra ->first or ->second slightly reduces readability at the call sites.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that second part is only used in #5252. I hope that's ok - I'll go ahead and merge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FWIW, you're of course right that the caller could compute the size themselves, but I thought duplicating work was not a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine by me. I was reviewing in PR order so hadn't seen the followup PR at that point.

@tautschnig tautschnig merged commit 62760f9 into diffblue:develop Mar 3, 2020
@tautschnig tautschnig deleted the factor-out-max-member branch March 3, 2020 12:31
@tautschnig tautschnig restored the factor-out-max-member branch December 27, 2020 06:37
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