-
Notifications
You must be signed in to change notification settings - Fork 36
clar: introduce type-safe integer comparisons #117
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
Conversation
|
@ethomson Would you mind doing a review of this MR? 🙏 |
9e99603 to
e818e71
Compare
|
@pks-t Thanks for working on this, it will be a very welcome improvement. I've left a couple of small comments but this all looks sensible to me. |
e818e71 to
eeeeca8
Compare
|
@phillipwood Thanks for your review! I've rebased the PR now to fix conflicts and have applied your feedback, except for the |
Introduce a portable wrapper for `vsnprintf()`. This function will be used in subsequent commits.
While we already have `clar_fail()`, this function does not allow the caller to provide a printf(3p)-style formatting string. This makes it somewhat hard at times to give a proper explanation _why_ a specific test has failed. Introduce `clar_failf()` to plug this gap. Note that the core of this new functionality is implemented in `clar__failv()`, which receives a `va_list` as input. This indirection isn't needed right now, but we will add more callers in subsequent commits.
The macros we have to assert the state of integers are lacking due to
multiple reasons:
- We explicitly cast the values to `int`, which causes problems in
case the values do not fit into an `int`. Furthermore, this hides
issues in case one accidentally passes the wrong type to this macro.
- We only have macros to compare integers for equality. Notably
lacking are constructs to compare for non-equality, like "less
than" or "less or equal".
- We only have macros to compare _signed_ integers, but lack macros to
check for _unsigned_ macros.
Fix this issue by introducing `clar__assert_compare_i()` as well as an
equivalent for unsigned types, `clar__assert_compare_u()`. These macros:
- Get `intmax_t` and `uintmax_t` as input, respectively, which allows
us to get rid of the explicit casts. Instead, the compiler can now
verify types for us and print warnings when there is an incompatible
type.
- Get an enum as input for the various different comparisons. Like
this we don't only support equality checks, but also all the other
checks one would typically expect.
Adapt existing macros to use `clar__assert_compare_i()`. Furthermore,
introduce new macros that supersede the older variants and which allow
the caller to perform integer comparisons.
eeeeca8 to
06f0c35
Compare
phillipwood
left a 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.
@pks-t The range-diff looks good, thanks again for adding these assertions.
|
Thanks, @phillipwood! Let's merge this then. |
The macros we have to assert the state of integers are lacking due to
multiple reasons:
We explicitly cast the values to
int, which causes problems incase the values do not fit into an
int. Furthermore, this hidesissues in case one accidentally passes the wrong type to this macro.
We only have macros to compare integers for equality. Notably
lacking are constructs to compare for non-equality, like "less
than" or "less or equal".
We only have macros to compare signed integers, but lack macros to
check for unsigned macros.
Fix this issue by introducing
clar__assert_compare_i()as well as anequivalent for unsigned types,
clar__assert_compare_u(). These macros:Get
intmax_tanduintmax_tas input, respectively, which allowsus to get rid of the explicit casts. Instead, the compiler can now
verify types for us and print warnings when there is an incompatible
type.
Get an enum as input for the various different comparisons. Like
this we don't only support equality checks, but also all the other
checks one would typically expect.
Adapt existing macros to use
clar__assert_compare_i(). Furthermore,introduce new macros that supersede the older variants and which allow
the caller to perform integer comparisons.