MISRA C++:2008 Rule 16-0-5
Arguments to a function-like macro shall not contain tokens that look like preprocessing directives
Description
Rule Definition
Arguments to a function-like macro shall not contain tokens that look like preprocessing directives.
Rationale
When a compiler encounters function-like macros, it replaces the argument of the macro into the replacement code. If the argument contains a token that looks like preprocessing directives, the replacement process during macro expansion is undefined. Depending on the environment, such a function-like macro might behave in unexpected ways, leading to errors and bugs.
Polyspace Implementation
Polyspace® flags calls to function-like macros if their argument starts with the
character #
.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Avoid Arguments That Start with # in Function-Like Macros
#include<cstdlib> #include<iostream> #define PRINT(ARG) std::cout<<#ARG //.... #define Error1 //... void foo(void){ PRINT( #ifdef Error1 //Noncompliant "Error 1" #else "Error 2" #endif //Noncompliant ); }
In this example, the function-like macro PRINT
is invoked with an
argument that chooses between two strings by using an #ifdef
block.
Depending on the environment, the output of this code might be #ifdef Error1
//Noncompliant "Error 1" #else "Error 2" #endif //Noncompliant
or Error
1
. Polyspace flags the arguments that start with the character #
.
Check Information
Group: Preprocessing Directives |
Category: Required |
Version History
Introduced in R2013b