mity / md4c

C Markdown parser. Fast. SAX-like interface. Compliant to CommonMark specification.
MIT License
756 stars 138 forks source link

GCC 10.2 warns under -Wimplicit-fallthrough #140

Closed dangelog closed 3 years ago

dangelog commented 3 years ago

There are few switch statements in the code where the fallthrough between case labels causes warning. This is from a build of Qt:

../src/3rdparty/md4c/md4c.c: In function ‘md_rollback’:
../src/3rdparty/md4c/md4c.c:2668:19: warning: this statement may fall through [-Wimplicit-fallthrough=]
 2668 |                 if((mark_flags & MD_MARK_CLOSER)  &&  mark->prev > opener_index) {
      |                   ^
../src/3rdparty/md4c/md4c.c:2675:13: note: here
 2675 |             default:
      |             ^~~~~~~
../src/3rdparty/md4c/md4c.c: In function ‘md_process_inlines’:
../src/3rdparty/md4c/md4c.c:4125:23: warning: this statement may fall through [-Wimplicit-fallthrough=]
 4125 |                     if(ctx->parser.flags & MD_FLAG_UNDERLINE) {
      |                       ^
../src/3rdparty/md4c/md4c.c:4141:17: note: here
 4141 |                 case '*':       /* Emphasis, strong emphasis. */
      |                 ^~~~
../src/3rdparty/md4c/md4c.c:4229:23: warning: this statement may fall through [-Wimplicit-fallthrough=]
 4229 |                     if(!(mark->flags & MD_MARK_AUTOLINK)) {
      |                       ^
../src/3rdparty/md4c/md4c.c:4239:17: note: here
 4239 |                 case '@':       /* Permissive e-mail autolink. */
      |                 ^~~~
../src/3rdparty/md4c/md4c.c: In function ‘md_enter_child_containers’:
../src/3rdparty/md4c/md4c.c:5506:33: warning: this statement may fall through [-Wimplicit-fallthrough=]
 5506 |                 is_ordered_list = TRUE;
      |                                 ^
../src/3rdparty/md4c/md4c.c:5509:13: note: here
 5509 |             case _T('-'):
      |             ^~~~
../src/3rdparty/md4c/md4c.c: In function ‘md_leave_child_containers’:
../src/3rdparty/md4c/md4c.c:5552:33: warning: this statement may fall through [-Wimplicit-fallthrough=]
 5552 |                 is_ordered_list = TRUE;
      |                                 ^
../src/3rdparty/md4c/md4c.c:5555:13: note: here
 5555 |             case _T('-'):
      |             ^~~~

These statements are already annotated in the code with a comment (that is, these fall throughs are INTENDED).

        switch(c->ch) {
            case _T(')'):
            case _T('.'):
                is_ordered_list = TRUE;
                /* Pass through */

            case _T('-'):
            case _T('+'):
            case _T('*'):

The problem is that GCC doesn't recognize the comment as-is. -Wimplicit-fallthrough only recognizes "Fall through" and some variations, and not "Pass". See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wimplicit-fallthrough

Suggestion: either rename "Pass through" in "Fall through", or define a MD_FALLTHROUGH macro, and make it expand to __attribute__((fallthrough)) on GCC >= 7, and also to whatever appeases other compilers (I think only Clang 12 also supports the attribute syntax; Clang in general won't recognize the comment).

mity commented 3 years ago

Let me declare clearly I have no intention to invest my time in silencing a warning in one particular compiler which I consider silly (really, what if my comments are in Swahili?), which is not enabled by default nor with -Wall and which does not really make the code better.

But feel free to make a PR, if it does not make things worse, I could merge it. That's more or less the maximum I am willing to do about it.

(And see also discussion in #34 and PR #35.)

dangelog commented 3 years ago

https://github.com/mity/md4c/pull/141

mity commented 3 years ago

Fixed by merging #141