MISRA Discussion Forums
Rule 19.4 -TC1 example incorrect? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.19 Preprocessing Directives (https://forum.misra.org.uk/forumdisplay.php?fid=43)
+---- Thread: Rule 19.4 -TC1 example incorrect? (/showthread.php?tid=497)



Rule 19.4 -TC1 example incorrect? - Graham Andrews - 17-04-2008

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
Code:
#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
Code:
#define CAT PI + PI
Then the expansion of CAT would be to a non-parenthesised expression and rule 19.4 violated.


Re: Rule 19.4 -TC1 example incorrect? - misra-c - 01-07-2008

This rule applies to the macro replacement list and not the ultimate expansion.

Therefore
Code:
#define CAT PI
is not compliant.

Code:
#define CAT (PI)
is compliant.