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))
After scraping the net a bit, and re-reading the GCC docs, I came across this:
Function Attributes:
returns_twiceThe
returns_twiceattribute 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 aresetjmpandvfork. Thelongjmp-like counterpart of such function, if any, might need to be marked with thenoreturnattribute.
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.
From man longjmp:
The values of automatic variables are unspecified after a call to longjmp() if they meet all the following criteria:
· they are local to the function that made the corresponding setjmp(3)
call;
· their values are changed between the calls to setjmp(3) and
longjmp(); and
· they are not declared as volatile.
As it happens, your x variable in the first example meets the criteria:
setjmp if some_global is true.So its value may be unspecified (clobbered).
About why the second version does not emit the warning... no idea.