为什么_Printf_format_string_宏不产生任何警告? [英] why _Printf_format_string_ macro doesn't produce any warnings?

查看:364
本文介绍了为什么_Printf_format_string_宏不产生任何警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码段中,根据SAL规范,在MyFormat()调用内对格式说明符的错误使用应产生警告,并且如果我取消对相同的printf()调用的注释,则确实会收到所有这些警告,但是即使使用/W4,我的代码也会被静默编译.我究竟做错了什么?我正在使用MSVC 2017 15.9.7社区版.

In the following snippet the wrong usage of format specifiers inside the MyFormat() call should produce a warning, according to SAL specifications, and if I uncomment the identical call of printf(), I really will receive all these warnings, but my code is compiled silently even with /W4. What am I doing wrong? I'm using MSVC 2017 15.9.7 Community edition.

#include <stdio.h>
#include <stdarg.h>

void MyFormat(_Printf_format_string_ const char *fmt, ...)
{
   va_list va;
   va_start(va, fmt);
   vprintf(fmt, va);
   va_end(va);
}

int main()
{
   MyFormat("blabla %s\n", L"qq");
   // printf("blabla %s\n", L"qq");
   return 0;
}

推荐答案

添加/analyze标志将导致此标志发出警告.但是,这与从printf中得到的警告是不同的(并且我认为是次等的)警告.不幸的是,我找不到一种方法来使用户定义的函数产生这种警告样式.

Adding the /analyze flag will cause this to produce a warning. However it is a different (and in my opinion inferior) warning than what you would get from printf. Unfortunately I can't find a way to make a user-defined function to produce that style of warning.

这篇关于为什么_Printf_format_string_宏不产生任何警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆