Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifier used as variable and function-like macro
#1
I recently came across some code similar to the following:

Code:
#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".
<r>David Crocker<br/>
Escher Technologies Ltd.<br/>
<URL url="http://www.eschertech.com">http://www.eschertech.com</URL></r>
Reply
#2
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
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)