How to enable warnings for incorrect use of TRACE / ATLTRACE

懵懂的女人 提交于 2019-12-06 07:05:41

问题


The following MFC program (taken from this question) has obvious errors as the format specifiers do not match the actual argument types:

#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 ); 
}

Yet, there are no compiler warnings. I have build this in debug configuration with warning level 4. Code analysis also doesn't raise anything.

There are warnings when TRACE is replaced by printf:

printf("load from %X, Position: %ld, Length: %ld, Perc: %lf \n", 
       &a, pos, len, perc );

Compiler warnings (duplicates omitted):

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".

Replacing TRACE by CString::Format() does not produce compiler warnings, but enabling code analysis produces similar warnings.

Is it possible to produce such warnings (either compiler warnings or code analysis warnings) for incorrect use of the TRACE / ATLTRACE macros in existing code?

来源:https://stackoverflow.com/questions/48733619/how-to-enable-warnings-for-incorrect-use-of-trace-atltrace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!