[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: error: jump skips variable initialization
From: |
Bruno Haible |
Subject: |
Re: error: jump skips variable initialization |
Date: |
Mon, 25 Jun 2018 02:00:38 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-128-generic; KDE/5.18.0; x86_64; ; ) |
Hi Jim, Paul,
> parse-datetime.y: In function 'parse_datetime2':
> parse-datetime.y:1791:19: error: jump skips variable initialization \
> [-Werror=jump-misses-init]
First this occurred in af_alg.c with pretty standard Linux kernel style
with gotos. [1] Now here.
What is the point of this warning?
I've read [2][3].
Test case:
===========================================================
extern void bar (int);
int foo (int a)
{
if (a == 1) goto one;
int b = 4;
bar (b);
one:
return a;
}
===========================================================
$ gcc-version 8.1.0 -Wall -Wjump-misses-init -c foo.c
foo.c: In function 'foo':
foo.c:4:15: warning: jump skips variable initialization [-Wjump-misses-init]
if (a == 1) goto one;
^~~~
foo.c:7:2: note: label 'one' defined here
one:
^~~
foo.c:5:7: note: 'b' declared here
int b = 4;
^
In C++ such a warning makes sense, for types that have a destructor,
because the destructor of 'b' would run even through its constructor
has not run.
But here
1. The variable 'b' is of type 'int', which has a trivial destructor.
2. We are in C, no C++, where ISO C types don't have destructors anyway.
(And AFAICS there are no GCC extensions to C that would provide
destructors either.)
Is the point to help people reduce the scope of the variables?
===========================================================
extern void bar (int);
int foo (int a)
{
if (a == 1) goto one;
{
int b = 4;
bar (b);
}
one:
return a;
}
===========================================================
Is the point to force people to hoist the variable declarations
at the top of the function (back to the BSD style of the 1980ies),
like Paul did in [1]?
===========================================================
extern void bar (int);
int foo (int a)
{
int b;
if (a == 1) goto one;
b = 4;
bar (b);
one:
return a;
}
===========================================================
Is the point to help people write code that can be compiled with a C++ compiler?
If so, then there's no reason for it to be enabled on gnulib code.
Is the point merely "the standard says that a warning is possible, so let's
implement it"?
Can someone answer this?
The next question then is: Why don't we silence this warning in the
'manywarnings' module?
Bruno
[1] https://lists.gnu.org/archive/html/bug-gnulib/2018-05/msg00064.html
[2]
https://stackoverflow.com/questions/37371794/using-goto-to-jump-to-inner-or-sibling-scope
[3]
https://stackoverflow.com/questions/20998054/why-does-gcc-give-me-the-wjump-misses-init-warning