Skip to content
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

Merged
merged 1 commit into from
Apr 3, 2018

Conversation

pwillem
Copy link

@pwillem pwillem commented Feb 26, 2018

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

@vincent-d vincent-d added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Feb 28, 2018
@miri64
Copy link
Member

miri64 commented Mar 15, 2018

Ping @gebart, @kaspar030?

@miri64
Copy link
Member

miri64 commented Mar 15, 2018

Change looks straight forward. However, my toolchain kung-fu isn't good enough to evaluate the value/impact of this change.

/**
* @brief define c11 static_assert() macro
*/
#define static_assert _Static_assert
Copy link
Contributor

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?

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Author

@pwillem pwillem Mar 20, 2018

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

Copy link
Author

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.

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(thanks for the nudge)

Copy link
Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, please squash!

@kaspar030 kaspar030 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Apr 2, 2018
@kaspar030
Copy link
Contributor

Seems like c++ has its own version of "static_assert".

Can you try applying https://gist.github.com/kaspar030/6a7b9d3b2a9e703603c514d32cbd5935?
It moves the else case within the "!defined(__cplusplus)```.

@pwillem pwillem force-pushed the feat/static_assert branch from 4c7fcc7 to 1e13967 Compare April 3, 2018 13:23
@kaspar030
Copy link
Contributor

That seems to have done it. Please squash!

@pwillem pwillem force-pushed the feat/static_assert branch from e6c3904 to 729441f Compare April 3, 2018 14:09
@kaspar030 kaspar030 merged commit d9993cc into RIOT-OS:master Apr 3, 2018
@kaspar030
Copy link
Contributor

Thanks!

@pwillem pwillem deleted the feat/static_assert branch April 4, 2018 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants