MISRA Discussion Forums

Full Version: Identifier used as variable and function-like macro
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I recently came across some code similar to the following:

#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 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".
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.