Skip to content

Conversation

ZetaR60
Copy link
Contributor

@ZetaR60 ZetaR60 commented Jun 2, 2018

This provides a working static_assert for c versions < c11. Should work globally and locally.

Required by #9190 for detecting collisions in gpio_t definitions.

Example return:

[liveuser@localhost-live driver_gpio_exp_coll]$ make BOARD=mega-xplained
Building application "tests_driver_gpio_exp_coll" for "mega-xplained" with MCU "atmega1284p".

In file included from /home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/main.c:20:0:
/home/liveuser/Desktop/RIOT_gpio_exp/core/include/assert.h:123:48: error: division by zero [-Werror=div-by-zero]
     enum { static_assert_failed_on_div_by_0 = 1/(!!(cond)) }
                                               ~^~~~~~~~~~~
/home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/main.c:202:1: note: in expansion of macro 'static_assert'
 static_assert(COLL_RES, "[driver_gpio_exp_coll] Collision in GPIO_EXP_PIN");
 ^~~~~~~~~~~~~
/home/liveuser/Desktop/RIOT_gpio_exp/core/include/assert.h:123:12: error: enumerator value for 'static_assert_failed_on_div_by_0' is not an integer constant
     enum { static_assert_failed_on_div_by_0 = 1/(!!(cond)) }
            ^
/home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/main.c:202:1: note: in expansion of macro 'static_assert'
 static_assert(COLL_RES, "[driver_gpio_exp_coll] Collision in GPIO_EXP_PIN");
 ^~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[1]: *** [/home/liveuser/Desktop/RIOT_gpio_exp/Makefile.base:83: /home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/bin/mega-xplained/application_tests_driver_gpio_exp_coll/main.o] Error 1
make: *** [/home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/../../Makefile.include:359: /home/liveuser/Desktop/RIOT_gpio_exp/tests/driver_gpio_exp_coll/bin/mega-xplained/application_tests_driver_gpio_exp_coll.a] Error 2

Copy link
Member

@jnohlgard jnohlgard left a comment

Choose a reason for hiding this comment

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

Clever solution, I like it! Minor code style comments

*/
#define static_assert(...) struct static_assert_dummy
#define static_assert(cond, ...) \
enum { static_assert_failed_on_div_by_0 = 1/(!!(cond)) };
Copy link
Member

Choose a reason for hiding this comment

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

Remove the semicolon. The user will write a semicolon when using it

Copy link
Member

Choose a reason for hiding this comment

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

Put spaces around the division operator

Copy link
Member

@jnohlgard jnohlgard left a comment

Choose a reason for hiding this comment

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

ACK, please squash

@ZetaR60
Copy link
Contributor Author

ZetaR60 commented Jun 3, 2018

Thanks, but I can't take credit. I looked for a way of doing it via a web search and this was suggested in several places as a way of implementing static_assert. The more common way of doing it apparently is by a typedef for an array with a negative number of members if the condition fails, but because RIOT is set up to be very strict about code quality it gives a warning about an unused typedef. The enum doesn't give any warnings when it is unused.

@ZetaR60 ZetaR60 force-pushed the RIOT_c99_static_assert branch from 429a497 to 2bd1d7f Compare June 3, 2018 07:20
@ZetaR60
Copy link
Contributor Author

ZetaR60 commented Jun 3, 2018

@gebart Squashed and did a double-check to make sure it still works correctly with the semicolon removed. Should be good to go.

@jnohlgard jnohlgard added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: core Area: RIOT kernel. Handle PRs marked with this with care! CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 3, 2018
@kYc0o
Copy link
Contributor

kYc0o commented Jun 3, 2018

Everything is good here. Let's go!

@kYc0o kYc0o merged commit 5e5773c into RIOT-OS:master Jun 3, 2018
@cladmi cladmi added this to the Release 2018.07 milestone Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Area: RIOT kernel. Handle PRs marked with this with care! 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