Identifier used as variable and function-like macro

6.19 Preprocessing Directives

Moderators: misra-c, david ward

Post Reply
dcrocker
Posts: 7
Joined: Fri Oct 07, 2005 7:46 pm
Company: Escher Technologies Ltd.
Location: Ash Vale, Surrey, UK
Contact:

Identifier used as variable and function-like macro

Post by dcrocker » Wed Mar 23, 2011 4:49 pm

I recently came across some code similar to the following:

Code: Select all

#define Sum(_x, _y) ((_x) + (_y))

int Sum;
The identifier Sum is used both as the name of a function-like macro and as the name of a variable. Does this violate any MISRA-C 2004 rules? The occurrence of the identifier Sum in the variable declaration is not an instantiation of the macro, because the following preprocessing token is not '(' (section 6.8.3 of the C90 standard), so it appears that rule 19.8 does not apply. It might be considered that rule 5.6 is violated, but section 6.1.2.3 of the standard does not mention a namespace for preprocessor macro names. It could also be considered that rule 5.7 is violated, depending on the interpretation of the word "reused".
David Crocker
Escher Technologies Ltd.
http://www.eschertech.com

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

Re: Identifier used as variable and function-like macro

Post by misra-c » Thu Mar 31, 2011 3:51 pm

Either of Rules 5.5 and 5.7 could be read as preventing Sum from being used as both a macro name and an object name, depending on the interpretation of "reused."

Rule 5.5 would apply only when an identifier is used both as a macro name and the name of an object with static storage duration. The text of the rule focusses on reuse of an identifier when it names two different objects or functions but it does not preclude applicability to macro names.

The more general Rule 5.7 would apply to all identifiers, whether or not any of their uses denote an object or function.

The wording of these rules will be modified in a future version of MISRA C to make it clear that identifiers that are used as macro names shall not be reused as object or function names.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.19 Preprocessing Directives”