以下MFC程序(取自this question)有明显错误,因为格式说明符与实际参数类型不匹配:
#include <afx.h>
int main()
{
int const a = 0;
ULONGLONG const len = 1000;
ULONGLONG const pos = 800;
double const perc = static_cast<double>( pos ) / static_cast<double>( len * 100 );
TRACE( "load from %X, Position: %ld, Length: %ld, Perc: %lf \n",
&a, pos, len, perc );
}
然而,没有编译器警告。我已经在调试配置中构建了这个警告级别4.代码分析也没有提出任何内容。
TRACE
取代printf
时会出现警告:
printf("load from %X, Position: %ld, Length: %ld, Perc: %lf \n",
&a, pos, len, perc );
编译器警告(省略重复):
warning C4477: "printf": The format string "% X" requires an argument of type "unsigned int", but the variadic argument "1" has the type "int *".
warning C4477: "printf": The format string "% ld" requires an argument of type "long", but the variadic argument "2" has the type "ULONGLONG".
note: If necessary, use% lld in the format string.
note: If necessary, use "% I64d" in the format string.
warning C6273: A non-integer was passed as _Param_ (2). However, calling "printf" requires an integer. Actual type: "int *". If a pointer value is passed,% p should be used.
warning C6328: Size Conflict: "unsigned __int64" was passed as _Param_ (3). However, calling "printf" requires "int".
warning C6340: Sign conflict: "unsigned __int64" is passed as _Param_ (3) if a signed type is required in the call to "printf".
TRACE
替换CString::Format()
不会产生编译器警告,但启用代码分析会产生类似的警告。
是否有可能在现有代码中错误使用TRACE
/ ATLTRACE
宏时产生此类警告(编译器警告或代码分析警告)?