-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Typecheck objects passed to K_POLL_EVENT_INITIALIZER #72801
base: main
Are you sure you want to change the base?
Conversation
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.
Some notes. Seems clever.
include/zephyr/kernel.h
Outdated
#define K_POLL_EVENT_INITIALIZER(_event_type, _event_mode, _event_obj) \ | ||
{ \ | ||
.poller = NULL, \ | ||
.type = _event_type, \ | ||
.state = K_POLL_STATE_NOT_READY, \ | ||
.mode = _event_mode, \ | ||
.unused = 0, \ | ||
.unused = 0 + K_POLL_ASSERT_VALID_OBJECT(_event_type, _event_obj), \ |
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.
Seems weird to abuse this "unused" field (which is a placeholder for extra bits that would be expected to go away if we found a use for them). Can't you rework the expression to take and evaluate to the _event_obj, e.g. ".obj = VALIDATE_OBJ(_event_obj)
" or whatever?
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.
I tried, haven't been able to come up with any reasonable way to "get rid" of the expression resulting from the _Static_assert
. I'm all ears to hear from anyone with more cursed C knowledge than me though.
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.
GCC statement expressions[1] are the normal tool for that. ({ STATEMENT_A; STATEMENT_B; })
executes A and then B, but then evaluates syntactically as the result of B.
[1] An extension we do allow, because it's so pervasively supported and incredibly useful. See https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
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.
Those aren't allowed outside of function scopes, which would be a weird restriction to impose.
include/zephyr/toolchain/common.h
Outdated
@@ -175,6 +175,10 @@ | |||
} | |||
#endif | |||
|
|||
// Same as BUILD_ASSERT(), but evaluates to an expression of value 0. Useful in macros. | |||
#define BUILD_ASSERT_EXPR(EXPR, MSG...) \ | |||
(0 * sizeof(struct { BUILD_ASSERT(EXPR, MSG); int dummy; })) |
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.
As mentioned elsewhere, it's probably even more useful for this to take a parameter to which it will evaluate if it doesn't blow up.
077a513
to
bd5fc46
Compare
You are missing I think this can be made much simpler while fully leveraging the compiler's
This even trips an existing test that does this:
And I think that's a good thing. Initializers shouldn't be used with random |
Sort of leaning toward Nico's solution and allowing the compiler to better do its job. |
bd5fc46
to
007a0a0
Compare
Those are the same thing. Anyway, I've adopted your solution, it is nicer indeed. |
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.
Very clean
This prevents accidentally passing the wrong pointer type (e.g. one level of indirection too many) as the object `void *`. Co-authored-by: Nicolas Pitre <npitre@baylibre.com> Signed-off-by: Armin Brauns <armin.brauns@embedded-solutions.at>
007a0a0
to
0b9d441
Compare
Too many times have I accidentally passed a
struct k_sem **
or similar. Thankfully modern C can help us here.