clar: introduce type-safe integer comparisons #117
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.
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_t
anduintmax_t
as 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.