PVS Studio complains about a dangerous expression. The parameter 'msg' must be surrounded by parentheses on following code C++ code
#include <iostream>
#define X ("X")
#define Y ("Y")
#define Z ("Z")
#define FRED(msg) msg << Z // <<-- Warning from PVS Studio
#define WILMA(msg) X << FRED(msg)
#define BUDDY(msg) Y << FRED(msg)
int main()
{
std::cout << WILMA(BUDDY("xxxxxx")) << std::endl;
return 0;
}
The warning message from PVS Studio is
V1003 The macro 'FRED' is a dangerous expression. The parameter 'msg' must be surrounded by parentheses. sample_demo.cpp 7
Following the suggestion from this tool and adding parentheses: #include
#define X ("X")
#define Y ("Y")
#define Z ("Z")
#define FRED(msg) (msg) << Z
#define WILMA(msg) X << FRED(msg)
#define BUDDY(msg) Y << FRED(msg)
int main()
{
std::cout << WILMA(BUDDY("xxxxxx")) << std::endl;
return 0;
}
This change seem to create invalid code. The compiler error from VS2017 is as follows:
error C2296: '<<': illegal, left operand has type 'const char [2]'
error C2297 : '<<' : illegal, right operand has type 'const char [7]'
Question
I am pretty sure the suggestion from PVS Studio is not correct in this particular case. Did i miss something obvious and the tool is correct? Many thanks in advance.