Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pks-t
Copy link
Member

@pks-t pks-t commented Apr 15, 2025

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.

Introduce a portable wrapper for `vsnprintf()`. This function will be
used in subsequent commits.
@pks-t
Copy link
Member Author

pks-t commented Apr 15, 2025

@ethomson Would you mind doing a review of this MR? 🙏

pks-t added 2 commits April 15, 2025 13:56
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.
@pks-t pks-t force-pushed the pks-integer-comparisons branch from 9e99603 to e818e71 Compare April 15, 2025 11:56
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.

1 participant