[NO REPLY] The Preprocessor (rev #2, 2018-1-15)

A place of insane ideas, nothing to see here.
User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

[NO REPLY] The Preprocessor (rev #2, 2018-1-15)

Postby Giovanni » Sun Dec 10, 2017 5:24 pm

Ideas regarding the preprocessor:

Assertions

In order to simplify checks are make them stronger, add an #assert directive that triggers a compile error if the expression is false or any of the operands is undefined.

Example:

Code: Select all

#assert NUM_THREADS <= MAX_THREADS


Would replace:

Code: Select all

#if ! defined NUM_THREADS
#error "NUM_THREADS not defined"
#endif
#if ! defined MAX_THREADS
#error "MAX_THREADS not defined"
#endif
#if NUM_THREADS > MAX_THREADS
#error "(NUM_THREADS <= MAX_THREADS) is false"
#endif


This idea is courtesy of the HAL clock trees, mainly composed by pages and pages of such code where I spend a lot of fun time fixing errors.

Messages

Printing messages at compile time:

Code: Select all

#message "HSE clock frequency is: " STM32_HSE_CLK "Hz"


Note that macros are assumed to be integer expressions, the result is printed. If expansion is not complete or the expression is not correct then an error is printed instead replacing the whole "#message ...".
The message is printed as-is, without any prefix, and terminated by an EOL sequence.

Undefined Macros

No more assumption that undefined macros have a value of zero, it is an error, always.

Giovanni

Return to “Safer C”

Who is online

Users browsing this forum: No registered users and 4 guests