-
Notifications
You must be signed in to change notification settings - Fork 2k
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
assert: add static_assert if using c11 #8642
Conversation
Ping @gebart, @kaspar030? |
Change looks straight forward. However, my toolchain kung-fu isn't good enough to evaluate the value/impact of this change. |
core/include/assert.h
Outdated
/** | ||
* @brief define c11 static_assert() macro | ||
*/ | ||
#define static_assert _Static_assert |
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.
Wouldn't it make more sense to define
#if __STDC_VERSION__ >= 201112L && !defined __cplusplus
#define static_assert(...) _Static_assert(__VA_ARGS__)
#else
#define static_assert(...)
#endif
... so code compiles for both >=c11 and <c11 compilers?
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.
what do you think of
#if __STDC_VERSION__ >= 201112L && !defined __cplusplus
/**
* @brief define c11 static_assert() macro
*/
#define static_assert _Static_assert
#else
#define static_assert(test, msg) assert(test)
#endif
so static asserts are mapped on normal asserts if you don't compile for c11?
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 thought static asserts can be used at places where others can't, like at global level:
static_assert(sizeof(unsigned long) == 4);
void main() {...}
so defining to normal asserts would make that use case impossible.
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.
Yes you are right, I will change to your proposal
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 tested your proposal with
#include <assert.h>
int test;
static_assert(sizeof(test) == 4, "msg");
int main(void)
{
return 0;
}
but when I compile for c99. I get
~/RIOT/tests/static/main.c:25:41: error: ISO C does not allow extra
;’ outside of a function [-Werror=pedantic]
So it seems that if you want to use this c11 feature you should compile your code with c11.
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.
@kaspar030 What do you want me todo?
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.
can you try defining the non-c11 case to struct _static_assert_dummy
?
that would turn a non-c11 call to "static_assert(...);" to a forward declaration, which can be in a file multiple times and doesn't have to be used.
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.
(thanks for the nudge)
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 tested it and your proposal worked. OK to squash?
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.
yes, please squash!
Seems like c++ has its own version of "static_assert". Can you try applying |
4c7fcc7
to
1e13967
Compare
That seems to have done it. Please squash! |
e6c3904
to
729441f
Compare
Thanks! |
Contribution description
When using c11 the macro
static_assert
doesn't exist. This contribution adds the macro to RIOT's assert.h file.Issues/PRs references