Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update comment to allow GCC skip fallthrough warnings #2770

Merged
merged 1 commit into from
Feb 24, 2020

Conversation

seisman
Copy link
Member

@seisman seisman commented Feb 19, 2020

GCC 7 reports a lot of -Wimplicit-fallthrough warnings. See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html and
https://developers.redhat.com/blog/2017/03/10/wimplicit-fallthrough-in-gcc-7/ for explanations.

For GCC, setting -Wimplicit-fallthrough=2 can let GCC understand the magic /* fall through */ comments and skip these warnings. However, Apple Clang only supports -Wimplicit-fallthrough. Any thoughts?

@seisman seisman changed the title Let gcc understand the fallthrough comments WIP: Let gcc understand the fallthrough comments Feb 19, 2020
@PaulWessel
Copy link
Member

Well, I guess it makes sense to add the /* fall through */ comment for our own sake as well (we do have some comments about this in places but did not know about the magic phrase), and perhaps clang eventually will adopt the same solution.

@joa-quim
Copy link
Member

WE will be triggering a couple of them there are there on purpose and where already reported by coverity.

@seisman
Copy link
Member Author

seisman commented Feb 21, 2020

According to the GCC manual, -Wextra enables -Wimplicit-fallthrough, which is also equivalent to -Wimplicit-fallthrough=3.

When -Wimplicit-fallthrough=3 is set, comments match one of the following regex are accepted:

-fallthrough
@fallthrough@
lint -fallthrough[ \t]*
[ \t.!]*(ELSE,? |INTENTIONAL(LY)? )?FALL(S | |-)?THR(OUGH|U)[ \t.!]*(-[^\n\r]*)?
[ \t.!]*(Else,? |Intentional(ly)? )?Fall((s | |-)[Tt]|t)hr(ough|u)[ \t.!]*(-[^\n\r]*)?
[ \t.!]*([Ee]lse,? |[Ii]ntentional(ly)? )?fall(s | |-)?thr(ough|u)[ \t.!]*(-[^\n\r]*)?

The regex looks really complicated, but the following comment is good:

/* Intentionally fall through - anything we want to say */

Maybe we should change the "fall-through" comments to this one?

@PaulWessel
Copy link
Member

Yes, that is probably safe. So that second part is specific to why we fall through in each instance?

@seisman
Copy link
Member Author

seisman commented Feb 21, 2020

So that second part is specific to why we fall through in each instance?

Yes, and the - is required.

@seisman seisman force-pushed the gcc-fallthrough branch 2 times, most recently from 3f90eb3 to 83380be Compare February 22, 2020 22:35
@seisman
Copy link
Member Author

seisman commented Feb 23, 2020

I've updated most comments to match the fall-through regex, and the number of fall-through warnings decreases.

We still have some warnings, mostly in gmt_init.c. For example, GCC reports fall-through warning for following snippet, because there is no "break" statement in the if-clause.

case GMTCASE_INPUT_CLOCK_FORMAT:
	if (gmt_M_compat_check (GMT, 4))	/* GMT4: */
		GMT_COMPAT_WARN;
	else { error = gmtinit_badvalreport (GMT, keyword); break; }	/* Not recognized so give error message */
case GMTCASE_FORMAT_CLOCK_IN:
	strncpy (value, GMT->current.setting.format_clock_in,  GMT_BUFSIZ-1);
	break;

I tried to add a comment line in the if-clause, but it didn't work. It seems I have to add the comment line exactly before the next case.

case GMTCASE_INPUT_CLOCK_FORMAT:
	if (gmt_M_compat_check (GMT, 4))	/* GMT4: */
		GMT_COMPAT_WARN;
	else { error = gmtinit_badvalreport (GMT, keyword); break; }	/* Not recognized so give error message */
        /* Intentionally fall through */
case GMTCASE_FORMAT_CLOCK_IN:
	strncpy (value, GMT->current.setting.format_clock_in,  GMT_BUFSIZ-1);
	break;

It's not ideal, but I don't see a workaround.

@PaulWessel
Copy link
Member

OK< so your last example works? I think that is pretty good.

@seisman seisman changed the title WIP: Let gcc understand the fallthrough comments Update comment to allow GCC skip fallthrough warnings Feb 24, 2020
@seisman
Copy link
Member Author

seisman commented Feb 24, 2020

@PaulWessel Now GCC doesn't report any fall-through warnings.

@PaulWessel
Copy link
Member

Fantastic, thanks!

@seisman seisman merged commit 440b455 into master Feb 24, 2020
@seisman seisman deleted the gcc-fallthrough branch February 24, 2020 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants