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

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

问题描述

下面是一小片code的:

 的#include<&stdio.h中GT;
#包括LT&;&STDARG.H GT;无效MyPrintf(字符常量*格式,va_list的参数);
无效MyVariadicPrintf(字符常量*格式,...);无效MyPrintf(字符常量*格式,va_list的参数)
{
    vprintf(格式参数);
}无效MyVariadicPrintf(字符常量*格式,...)
{
    va_list的ARGS;
    的va_start(参数,格式);
    MyPrintf(格式参数);
    va_end用来(参数);
}INT主(INT,CHAR *)
{
    MyVariadicPrintf(%S/ *缺少第二个参数* /);    返回0;
}

我用GCC 4.0编译它,在Mac OS X Leopard的运行X code。结果
-Wformat和-Wmissing格式属性被启用。结果
这code给出了第9行警告(来电 vprintf ),这表明 MyPrintf 可以使用'格式属性:


  

功能可能是printf式的格式属性。

可能的候选

所以我添加的属性这种方式(不知道这是正确的):

 无效MyPrintf(字符常量*格式,va_list的参数)__attribute __((格式(printf的,1,0)));

在previous警告消失,同样的警告,现在出现在第16行(调用 MyPrintf ),这表明 MyVariadicPrintf 可以使用格式属性。结果
所以,我这种方式添加属性(pretty相信这是正确的这个时候):

 无效MyVariadicPrintf(字符常量*格式,...)__attribute __((格式(printf的,1,2)));

和现在我得到第22行预期的警告(来电 MyVariadicPrintf


  

有关格式参数太少



  1. 难道我这样做对吗?

  2. 我注意到,在 MyPrintf 声明,如果我删除了部分属性,我仍然会得到线22通缉警告我也注意到,在这部分属性,从1到2改变索引不会给任何警告或错误。哪一个是正确的,什么是该功能属性的目标是什么?

  3. 如果我添加的功能 MyVariadicPrintfT 并调用它(专业与字符),我LL得到警告,建议使用格式属性,此功能。我认为这是不可能的,因为格式参数取决于模板类型。我说得对不对?

     模板< typename的类型>
    无效MyVariadicPrintfT(类型为const *格式,...)
    {
        va_list的ARGS;
        的va_start(参数,格式);
        MyPrintf(格式参数);
        va_end用来(参数);
    }


最新的GNU文档可以在 gnu.org 被发现。结果
警告选项是部分3.8 (寻找-Wmissing格式属性)。< BR>
功能属性在部分6.30 (寻找格式(原型,与字符串指数先来检查))。

感谢。


解决方案

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



  1. 您已经发布的一个是正确的(格式(printf的,1,0))。 1,因为格式字符串参数1,0,因为有要检查没有可变参数。

Here is a little piece of code:

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

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:

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

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

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

too few arguments for 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);
    }
    

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

Thanks.

解决方案

The documentation has the answer you need. Particularly:

  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天全站免登陆