假设我使用 Visual Studio 2017 C++ 编译器编译以下内容:
int r = 0;
wprintf_s(L"%s", r);
它会给我这些非常方便的警告:
warning C4477: 'wprintf' : format string '%s' requires an argument of type 'wchar_t *', but variadic argument 1 has type 'int'
warning C4313: 'wprintf': '%s' in format string conflicts with argument 1 of type 'int'
但是当我尝试定义自己的可变参数函数时:
void MyFormat(_In_z_ _Printf_format_string_ LPCTSTR pszFormat, ...)
{
va_list argList;
va_start( argList, pszFormat );
//Do work ...
va_end( argList );
}
然后以类似的方式调用它:
int r = 0;
MyFormat(L"%s", r);
它不会触发它们。
所以我想知道是否可以为我自己的可变参数函数启用这些警告?
最佳答案
像_In_z_
和_Printf_format_string_
这样的东西是SAL注释宏。它们可以被静态分析工具识别,但在编译器看到它们之前就被预处理器删除。所以它们对于你的情况不是很有用。
一些第 3 方编译器实现特定于供应商的方法来启用用户定义函数上 printf 样式参数的编译时验证(例如 GCC 中的 __attribute__(format)
and __attribute__(format_arg)
),但是 Visual C++ 不是这些编译器之一(请参阅 __attribute__((format(printf, 1, 2))) for MSVC? )。 VC++ 团队选择仅对标准 printf/scanf
系列 C 运行时函数启用编译时验证,如 2015 年博客中所述:
C++ Team Blog: Format Specifiers Checking
By popular request, in Visual Studio 2015 RTM, we’ve implemented the checking of arguments given to printf/scanf and their variations in the C standard library. You can try the examples from this post in our online compiler.
...
Currently, the checking of format specifiers is only done for a predefined set of CRT functions and is not available for user-defined functions that would also benefit from similar checks. If there is enough interest, we will consider extending these warnings to work on such user-defined functions.
如果您确实想要对用户定义的可变参数函数进行编译时检查,请改用 variadic templates。
关于c++ - 我可以为我自己的可变参数函数启用有关错误使用说明符的警告吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916666/