What sense do these clobbered variable warnings make?

前端 未结 2 1161
萌比男神i
萌比男神i 2020-12-30 03:48

I have a function like this:

#include 
jmp_buf buf;
void func2(int g);
extern int some_global;
void func(int x)
{
    if (setjmp(buf))
               


        
2条回答
  •  太阳男子
    2020-12-30 04:10

    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.

提交回复
热议问题