这些破坏性的可变警告有什么意义? [英] What sense do these clobbered variable warnings make?

查看:499
本文介绍了这些破坏性的可变警告有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能:

  #include< setjmp.h> 
jmp_buf buf;
void func2(int g);
extern int some_global;
void func(int x)
{
if(setjmp(buf))
return;
if(some_global)
x + = 5;
func2(x);

code $ $ $ $ $ $ $ $ $ $ $ $ GCC警告:

 
test.c:函数'func':
test.c:5:warning:argument'x '可能会被'longjmp'或'vfork'破坏[-wroughbbered]

为什么?我的意思是,显然我并不关心是否 x 被破坏,因为它不可能在 setjmp 返回。即使编译器应该知道一些非常明显的东西,因为它具有一些类的特殊知识 setjmp



我的主要兴趣是在我继承的代码库中发现错误,因此使用这种编码风格并不是我正在寻找的建议。然而,这里有一些奇怪的曲折。例如,如果 x 是一个局部变量而不是一个参数,那么GCC不会抱怨。另外,如果(some_global)行没有>,GCC不会投诉。尼斯。有些东西搞乱了GCC的流程分析,或者GCC知道我不知道的东西。

所以,


  • 是否有一种简单的方法来抑制此函数的此警告,就像您可以将未使用的参数转换为(void)?一样?


  • 或者我只是压制项目范围的警告?

  • 缺少什么? / em>不同版本不会产生警告:

      #include< setjmp.h> 
    jmp_buf buf;
    void func2(int g);
    extern int some_global;
    void func(int y)
    {
    int x = y;
    if(setjmp(buf))
    return;
    if(some_global)
    x + = 5;
    func2(x);
    }


    解决方案

    ,并重新阅读GCC文档,我遇到了这个:
    $ b

    函数属性:


    returns_twice



    returns_twice 属性告诉编译器一个函数可能返回多次。编译器将确保在调用此函数之前所有寄存器都已死亡,并且会发出有关在函数第二次返回后可能被破坏的变量的警告。这些函数的例子是 setjmp vfork 。这个函数的 longjmp 类似对象(如果有的话)可能需要用 noreturn 属性标记。


    所以看起来GCC没有任何特殊知识 setjmp setjmp 返回两次,而不是它总是第一次返回0,然后返回非零。天哪,那本来不错。

    I have a function like this:

    #include <setjmp.h>
    jmp_buf buf;
    void func2(int g);
    extern int some_global;
    void func(int x)
    {
        if (setjmp(buf))
            return;
        if (some_global)
            x += 5;
        func2(x);
    }
    

    GCC (gcc (Debian 4.4.5-8) 4.4.5) gives a warning:

    test.c: In function ‘func’:
    test.c:5: warning: argument ‘x’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
    

    Why???? I mean, obviously I don't care if x is clobbered or not, because it can't possibly be used after setjmp returns. Even the compiler should be aware of something so blindingly obvious, given that it has some kind of special knowledge of setjmp.

    My main interest is finding bugs in a code base that I inherited, so, "use this coding style instead" is not advice I am looking for. However, there are a number of bizarre twists here. For example, if x is a local variable instead of a parameter, then GCC does not complain. Also, GCC will not complain without the if (some_global) line. Nice. Something is messing up GCC's flow analysis, or maybe GCC knows something I don't.

    So,

    • Is there an easy way to suppress this warning for this function, just the same way you can cast unused parameters to (void)?

    • Or do I just suppress the warning project-wide?

    • Or am I missing something?

    Update: Let me share with you a slightly different version that does not generate a warning:

    #include <setjmp.h>
    jmp_buf buf;
    void func2(int g);
    extern int some_global;
    void func(int y)
    {
        int x = y;
        if (setjmp(buf))
            return;
        if (some_global)
            x += 5;
        func2(x);
    }
    

    解决方案

    After scraping the net a bit, and re-reading the GCC docs, I came across this:

    Function Attributes:

    returns_twice

    The returns_twice attribute tells the compiler that a function may return more than one time. The compiler will ensure that all registers are dead before calling such a function and will emit a warning about the variables that may be clobbered after the second return from the function. Examples of such functions are setjmp and vfork. The longjmp-like counterpart of such function, if any, might need to be marked with the noreturn attribute.

    So it appears that GCC does not have any "special knowledge" of setjmp, it just insinuates that it does. All it knows is that setjmp returns twice, not that it always returns 0 the first time and nonzero afterwards. Gosh, that would have been nice.

    这篇关于这些破坏性的可变警告有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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