Rule 19.4 -TC1 example incorrect?

6.19 Preprocessing Directives

Moderators: misra-c, david ward

Graham Andrews
Posts: 5
Joined: Fri Apr 20, 2007 10:43 am
Location: Edinburgh

Rule 19.4 -TC1 example incorrect?

Post by Graham Andrews » Thu Apr 17, 2008 5:19 pm

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: Select all

#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: Select all

#define CAT PI + PI
Then the expansion of CAT would be to a non-parenthesised expression and rule 19.4 violated.
Graham Andrews
Edinburgh Design Centre
Analog Devices Inc

Posts: 572
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 19.4 -TC1 example incorrect?

Post by misra-c » Tue Jul 01, 2008 3:50 pm

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


Code: Select all

#define CAT PI 
is not compliant.

Code: Select all

#define CAT (PI)
is compliant.
Posted by and on behalf of
the MISRA C Working Group


Return to “6.19 Preprocessing Directives”