MISRA Discussion Forums

Full Version: Rule 19.4 -TC1 example incorrect?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The rule states that C macros shall only expand to ...
This implies that the rule should only be invoked when the macro is expanded.
So for the not compliant example
#define CAT PI  /* non-parenthesised expression */
A checking tool should not attempt to check the violation until the macro is used.
In fact when CAT is being processed for expansion, it requires PI to be expanded. PI expands to 3.14159F, which is a constant, so its expansion is o.k. The expansion of CAT looks for other preprocessing tokens after PI has been substituted. There are no more tokens, so CAT expands to 3.14159F and is therefore not in violation of rule 19.4.
If the example had been written as
#define CAT PI + PI
Then the expansion of CAT would be to a non-parenthesised expression and rule 19.4 violated.
This rule applies to the macro replacement list and not the ultimate expansion.

#define CAT PI
is not compliant.

#define CAT (PI)
is compliant.