PVS Studio抱怨危險的表情。參數「MSG」必須由括號上下面的代碼C++代碼包圍PVS Studio抱怨危險的宏觀表達
#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;
}
從PVS Studio中的警告消息是
V1003 The macro 'FRED' is a dangerous expression. The parameter 'msg' must be surrounded by parentheses. sample_demo.cpp 7
在從該工具的建議和添加括號: 的#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;
}
這種變化似乎產生無效代碼。從VS2017編譯器錯誤如下:
error C2296: '<<': illegal, left operand has type 'const char [2]'
error C2297 : '<<' : illegal, right operand has type 'const char [7]'
問題
我敢肯定從PVS Studio中的建議是不是在這種特殊情況下是正確的。我錯過了一些明顯的東西,這個工具是正確的嗎?提前謝謝了。
@bartoli:謝謝,但是如前所述,這會導致相同的編譯器錯誤。 – orbitcowboy
https://stackoverflow.com/questions/277258/how-do-i-see-a-c-c-source-file-after-preprocessing-in-visual-studio –