git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Fixes compile warning with -Wimplicit-fallthrough CFLAGS

From
Jeff King <peff@peff.net>
Date
Jan 23, 2018, 00:07 UTC
Message-ID
<20180123000727.GB26357@sigill.intra.peff.net>
In-Reply-To
<1516665078-107974-1-git-send-email-gitter.spiros@gmail.com>
On Mon, Jan 22, 2018 at 11:51:18PM +0000, Elia Pinto wrote:
Show 13 quoted lines
> 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.

Show 10 quoted lines
> 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
Previous: Elia PintoNext: Junio C Hamano
Message 2 of 7 in “Fixes compile warning with -Wimplicit-fallthrough CFLAGS”
  1. Fixes compile warning with -Wimplicit-fallthrough CFLAGSElia Pinto, Jan 22, 2018
  2. Jeff KingJan 23, 2018
  3. Junio C HamanoJan 23, 2018
  4. Jeff KingJan 23, 2018
  5. Eric SunshineJan 23, 2018
  6. Jeff KingJan 23, 2018
  7. Jacob KellerJan 23, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.