Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
On Tue, Jan 23, 2018 at 10:33:57AM -0800, Junio C Hamano wrote: > Jeff King writes: > > >> diff --git a/apply.c b/apply.c > >> index 321a9fa68..a22fb2881 100644 > >> --- a/apply.c > >> +++ b/apply.c > >> @@ -1450,7 +1450,7 @@ static void recount_diff(const char *line, int size, > >> struct fragment *fragment) > >>switch (*line) { > >>case ' ': case '\n': > >>newlines++; > >> - /* fall through */ > >> + GIT_FALLTHROUGH; > > > > Ugh, the semi-colon there makes it look like it's actual code. If we go > > this route, I wonder if it's worth hiding it inside the macro. > > What? You mean to shout in all caps without even terminating the > line with any punctuation? Please don't---I am sure it will break > auto indentation people rely on from their editors. True, that may be even worse. I just wonder if we can do something to make it look more obviously like a non-code attribute. The actual syntax is something like: [[fallthrough]]; which is pretty horrid, but at least a bit easier to see. gcc also provides "__attribute__((fallthrough))", but I don't think it works with clang. I vastly prefer the comment approach if we can use it. Apparently clang doesn't support it, but I have also not managed to get clang (either version 4, 6, or the upcoming 7) to actually report anything via -Wimplicit-fallthrough, either. Maybe I'm holding it wrong. -Peff
Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
Jeff King writes: >> diff --git a/apply.c b/apply.c >> index 321a9fa68..a22fb2881 100644 >> --- a/apply.c >> +++ b/apply.c >> @@ -1450,7 +1450,7 @@ static void recount_diff(const char *line, int size, >> struct fragment *fragment) >> switch (*line) { >> case ' ': case '\n': >> newlines++; >> -/* fall through */ >> +GIT_FALLTHROUGH; > > Ugh, the semi-colon there makes it look like it's actual code. If we go > this route, I wonder if it's worth hiding it inside the macro. What? You mean to shout in all caps without even terminating the line with any punctuation? Please don't---I am sure it will break auto indentation people rely on from their editors.
Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
On Mon, Jan 22, 2018 at 4:59 PM, Jeff King wrote: > On Mon, Jan 22, 2018 at 07:54:18PM -0500, Eric Sunshine wrote: > >> On Mon, Jan 22, 2018 at 6:51 PM, Elia Pinto wrote: >> > This patch add explicit fallthrough compiler attribute >> > when needed on switch case statement eliminating >> > the compile warning [-Werror=implicit-fallthrough=]. >> > It does this by means of a macro that takes into account >> > the versions of the compilers that include that attribute. >> > [...] >> > Signed-off-by: Elia Pinto >> > --- >> > diff --git a/convert.c b/convert.c >> > @@ -1554,7 +1554,7 @@ static int ident_filter_fn(struct stream_filter >> > *filter, >> > switch (ident->state) { >> > default: >> > strbuf_add(&ident->left, head, ident->state); >> > - /* fallthrough */ >> > + GIT_FALLTHROUGH; >> > case IDENT_SKIPPING: >> > /* fallthrough */ >> >> Why doesn't this /* fallthrough */ deserve the same treatment? >> >> > case IDENT_DRAINING: > > I can't answer that philosophically, but I can tell you why the compiler > does not complain. :) > > Usually case arms with no statements between them are exempt from > fallthrough warnings. So: > > switch (foo) > case 1: > case 2: > case 3: > /* do one thing */ > break; > case 4: > /* do another thing */ > break; > } > > does not need any annotations for cases 1 and 2 to fallthrough. Which > means that the original comment was not actually necessary for the > compiler, though the original author considered it a useful comment. > > So there you get into philosophy. Should it be converted to a > compiler-visible annotation, or is it better left as a comment? > > -Peff I'd personally rather stick to the comment if we can, or use something like "fallthrough;" to make it appear like a keyword, instead of an all caps macro, since at least to my sensibility, the all caps is a bit too crazy. Also, I would not put one inside an empty case statement that just falls through to the next branch and does nothing special itself. Thanks, Jake
Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
On Mon, Jan 22, 2018 at 07:54:18PM -0500, Eric Sunshine wrote: > On Mon, Jan 22, 2018 at 6:51 PM, Elia Pinto wrote: > > This patch add explicit fallthrough compiler attribute > > when needed on switch case statement eliminating > > the compile warning [-Werror=implicit-fallthrough=]. > > It does this by means of a macro that takes into account > > the versions of the compilers that include that attribute. > > [...] > > Signed-off-by: Elia Pinto > > --- > > diff --git a/convert.c b/convert.c > > @@ -1554,7 +1554,7 @@ static int ident_filter_fn(struct stream_filter > > *filter, > > switch (ident->state) { > > default: > > strbuf_add(&ident->left, head, ident->state); > > - /* fallthrough */ > > + GIT_FALLTHROUGH; > > case IDENT_SKIPPING: > > /* fallthrough */ > > Why doesn't this /* fallthrough */ deserve the same treatment? > > > case IDENT_DRAINING: I can't answer that philosophically, but I can tell you why the compiler does not complain. :) Usually case arms with no statements between them are exempt from fallthrough warnings. So: switch (foo) case 1: case 2: case 3: /* do one thing */ break; case 4: /* do another thing */ break; } does not need any annotations for cases 1 and 2 to fallthrough. Which means that the original comment was not actually necessary for the compiler, though the original author considered it a useful comment. So there you get into philosophy. Should it be converted to a compiler-visible annotation, or is it better left as a comment? -Peff
Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
On Mon, Jan 22, 2018 at 6:51 PM, Elia Pinto wrote: > This patch add explicit fallthrough compiler attribute > when needed on switch case statement eliminating > the compile warning [-Werror=implicit-fallthrough=]. > It does this by means of a macro that takes into account > the versions of the compilers that include that attribute. > [...] > Signed-off-by: Elia Pinto > --- > diff --git a/convert.c b/convert.c > @@ -1554,7 +1554,7 @@ static int ident_filter_fn(struct stream_filter *filter, > switch (ident->state) { > default: > strbuf_add(&ident->left, head, ident->state); > - /* fallthrough */ > + GIT_FALLTHROUGH; > case IDENT_SKIPPING: > /* fallthrough */ Why doesn't this /* fallthrough */ deserve the same treatment? > case IDENT_DRAINING:
Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS
On Mon, Jan 22, 2018 at 11:51:18PM +, Elia Pinto wrote: > This patch add explicit fallthrough compiler attribute > when needed on switch case statement eliminating > the compile warning [-Werror=implicit-fallthrough=]. > It does this by means of a macro that takes into account > the versions of the compilers that include that attribute. > > The fallthrough (or clang::fallthrough) attribute is used to annotate > intentional fall-through between switch labels. > Traditionally these are marked with a specific comment, but > this attribute is meant to replace comments with a more strict > annotation, which can be checked by the compiler (gcc-7 or clang). > The flags in question were introduced in gcc 7 and are also enabled > with -Wextra. Hrm. Your subject says "fixes compile warnings", but don't we already compile cleanly with -Wimplicit-fallthrough after my 1cf01a34ea (consistently use "fallthrough" comments in switches, 2017-09-21)? Certainly the tip of "master" seems to pass for me on both gcc 7 and clang 4. You can pump the warning up to level 5 on gcc to insist on the attribute, but I think the comments are more readable (and it is not like we have a problem with false positive comments). > It would also have been possible to introduce a specific comment > accepted by gcc 7 instead of the fallthrough attribute for this warning, > but clang does not have a similar implementation. The macro replaces > the previous, not uniform, comments and can acts as a comment itself. Interestingly clang seems to accept -Wimplicit-fallthrough, but I could not get it to actually trigger a warning, even after removing some of the existing comments. What version of clang are you using? I'm certainly puzzled by the behavior I'm seeing. > diff --git a/apply.c b/apply.c > index 321a9fa68..a22fb2881 100644 > --- a/apply.c > +++ b/apply.c > @@ -1450,7 +1450,7 @@ static void recount_diff(const char *line, int size, > struct fragment *fragment) > switch (*line) { > case ' ': case '\n': > newlines++; > - /* fall through */ > + GIT_FALLTHROUGH; Ugh, the semi-colon there makes it look like it's actual code. If we go this route, I wonder if it's worth hiding it inside the macro. -Peff