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

A place of insane ideas, nothing to see here.
Post Reply
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

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

Post by Giovanni »

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
Post Reply