How to enable warnings for incorrect use of TRACE

2020-07-22 09:47发布

问题:

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?