如何使用 GCC 属性“格式"? [英] How to use the GCC attribute 'format'?

查看:14
本文介绍了如何使用 GCC 属性“格式"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一小段代码:

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

void MyPrintf(char const* format, va_list args);
void MyVariadicPrintf(char const* format, ...);

void MyPrintf(char const* format, va_list args)
{
    vprintf(format, args);
}

void MyVariadicPrintf(char const* format, ...)
{
    va_list args;
    va_start(args, format);
    MyPrintf(format, args);
    va_end(args);
}

int main(int, char*)
{
    MyVariadicPrintf("%s" /* missing 2nd argument */);

    return 0;
}

我正在使用 GCC 4.0 编译它,在 Mac OS X Leopard 上运行 Xcode.
-Wformat 和 -Wmissing-format-attribute 已启用.
此代码在第 9 行给出警告(调用 vprintf),表明 MyPrintf 可以使用 'format' 属性:

I'm compiling it with GCC 4.0, running Xcode on Mac OS X Leopard.
-Wformat and -Wmissing-format-attribute are enabled.
This code gives a warning on line 9 (call to vprintf), suggesting that MyPrintf could use the 'format' attribute:

函数可能是printf"格式属性的候选对象

function might be possible candidate for 'printf' format attribute

所以我以这种方式添加属性(不确定是否正确):

So I add the attribute this way (not sure if this is right):

void MyPrintf(char const* format, va_list args) __attribute__((format(printf, 1, 0)));

之前的警告消失了,同样的警告现在出现在第 16 行(调用 MyPrintf),表明 MyVariadicPrintf 可以使用格式"属性.
所以我以这种方式添加属性(很确定这次是对的):

The previous warning disappears and the same warning now appears on line 16 (call to MyPrintf), suggesting that MyVariadicPrintf could use the 'format' attribute.
So I add the attribute this way (pretty sure this is right this time):

void MyVariadicPrintf(char const* format, ...) __attribute__((format(printf, 1, 2)));

现在我在第 22 行收到了预期的警告(调用 MyVariadicPrintf):

And now I get the expected warning on line 22 (call to MyVariadicPrintf):

格式参数太少

  1. 我做对了吗?
  2. 我注意到在 MyPrintf 声明中,如果我删除属性部分,我仍然会在第 22 行收到想要的警告.我还注意到在这个属性部分中,将索引从 1 更改to 2 不会给出任何警告或错误.哪一个是正确的,这个函数的属性的目标是什么?
  3. 如果我添加以下函数 MyVariadicPrintfT 并调用它(专用于 char),我将收到建议使用 'format' 属性的警告在这个功能上.我认为这是不可能的,因为 format 参数取决于模板类型.我说的对吗?

  1. Did I do this right?
  2. I noticed that on MyPrintf declaration, if I delete the attribute part, I'll still get the wanted warning on line 22. I also noticed that in this attribute part, changing the index from 1 to 2 won't give any warning or error. Which one is right and what is the goal of the attribute on this function?
  3. If I add the following function MyVariadicPrintfT and call it (specialized with char), I'll get the warning suggesting to use the 'format' attribute on this function. I think it's impossible because the format argument is dependent on the templated type. Am I right?

template<typename Type>
void MyVariadicPrintfT(Type const* format, ...)
{
    va_list args;
    va_start(args, format);
    MyPrintf(format, args);
    va_end(args);
}

最新的 gnu 文档可以在 gnu.org 找到.
警告选项位于 第 3.8 节 中(查找-Wmissing-format-属性").
函数属性在 第 6.30 节中(查找格式(archetype, string-index, first-to-check)").

The latest gnu documentation can be found at gnu.org.
Warning Options are in section 3.8 (look for "-Wmissing-format-attribute").
Function Attributes are in section 6.30 (look for "format (archetype, string-index, first-to-check)").

谢谢.

推荐答案

文档有你需要的答案.特别是:

The documentation has the answer you need. Particularly:

  1. 是的
  2. 您发布的那个是正确的(format(printf, 1, 0)).1 是因为格式字符串是参数 1,0 是因为没有要检查的可变参数.
  1. Yes
  2. The one you have posted is correct (format(printf, 1, 0)). 1 because the format string is parameter 1, 0 because there are no variadic arguments to be checked.

这篇关于如何使用 GCC 属性“格式"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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