Main Content

AUTOSAR C++14 Rule M16-0-5

Arguments to a function-like macro shall not contain tokens that look like pre-processing directives

Description

Rule Definition

Arguments to a function-like macro shall not contain tokens that look like pre-processing 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.

Examples

expand all

#include<cstdlib>
#include<iostream>
#define PRINT(ARG) std::cout<<#ARG
//....
#define Error1
//...


void foo(void){
	PRINT(
	#ifdef Error1  //Noncompliant  
	"Error 1"
	#else    //Noncompliant
	"Error 2"
	#endif  
	);
	
}

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, Automated

Version History

Introduced in R2019a