Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #151898

Re: This statement may fall through - how?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: This statement may fall through - how?
Date 2020-04-30 17:27 -0700
Organization None to speak of
Message-ID <87pnbo4hyo.fsf@nosuchdomain.example.com> (permalink)
References (1 earlier) <hgukkrFpf1aU6@mid.individual.net> <87imhh7pk8.fsf@nosuchdomain.example.com> <r8fcfn$fmn$1@dont-email.me> <seErO5Tpfw5MbWWICM@bongo-ra.co> <r8fmii$htv$1@dont-email.me>

Show all headers | View raw


mathog <mathog@caltech.edu> writes:
> On 4/30/20 2:09 PM, Spiros Bousbouras wrote:
>> I assumed that the "C++11" Keith wrote was a typo and he meant "C11". If this
>> really is about C++ then why are you asking here and not on  comp.lang.c++ ?
>
> The package is mostly C, I just didn't notice that this file happened
> to be C++.
>
> I have reduced it to a test case in C which does the same thing:
>
>
> cat >test.c <<'EOD'
> #include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
>
> void usage(void);
> void parse_command_line(int argc, char* argv[]);
>
>
> int main(int argc, char* argv[]){
>    parse_command_line(argc,argv);
>    printf("done\n");
>    exit(EXIT_SUCCESS);
> }
>
> void usage()
> {
>   printf("usage string\n");
>   exit(0);
> }
>
> void parse_command_line(int argc, char* argv[])
> {
>   int opt;
>
>   while ( (opt = getopt(argc, argv, "i:o:hw:te") ) != -1 ) {
>     switch(opt) {
>       case 'h':
>         usage();
>
>       default:
>         printf("anything else\n");
>         break;
>     }
>   }
> }
> EOD
> gcc -Wall -Wextra -o test test.c
> test.c: In function ‘parse_command_line’:
> test.c:28:9: warning: this statement may fall through
> [-Wimplicit-fallthrough=]
>          usage();
>          ^~~~~~~
> test.c:30:7: note: here
>        default:
>        ^~~~~~~
>
> It only does it with -Wextra.  These do the same thing:
>
> gcc -Wall -Wextra -std=c99 -D_XOPEN_SOURCE -o test test.c
> gcc -Wall -Wextra -std=c11 -D_XOPEN_SOURCE -o test test.c
> gcc -Wall -Wextra -std=c17 -D_XOPEN_SOURCE -o test test.c
>
> This is with gcc 8.3.1.

Now it's a non-fatal warning.  Since it's not a required diagnostic,
this is not a conformance issue.  Since you specifically requested
non-required diagnostics by compiling with "-Wextra", one might
argue that you're just getting what you asked for.

Apparently gcc does not analyze the implementation of the usage()
function to determine that it cannot return.  It's not required to
do so, and I'm not surprised that it doesn't.  I certainly wouldn't
expect it to do that analysis at optimization level 0, but even
with "-O3", gcc either doesn't do that analysis, or doesn't use
the results to decide not to issue the warning.

There are several ways you could inform the compiler that usage()
doesn't return.

If I add "_Noreturn" to the declaration of usage, the warning goes
away.  If you don't want to assume C11, there are gcc-specific ways
to do the same thing (and you could use #ifdef's to choose how to
do this).

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

This statement may fall through - how? mathog <mathog@caltech.edu> - 2020-04-29 17:18 -0700
  Re: This statement may fall through - how? Ian Collins <ian-news@hotmail.com> - 2020-04-30 12:32 +1200
    Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-29 18:03 -0700
      Re: This statement may fall through - how? Ian Collins <ian-news@hotmail.com> - 2020-04-30 14:01 +1200
      Re: This statement may fall through - how? mathog <mathog@caltech.edu> - 2020-04-30 13:29 -0700
        Re: This statement may fall through - how? Spiros Bousbouras <spibou@gmail.com> - 2020-04-30 21:09 +0000
          Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 15:19 -0700
          Re: This statement may fall through - how? mathog <mathog@caltech.edu> - 2020-04-30 16:21 -0700
            Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 17:27 -0700
            Re: This statement may fall through - how? Ian Collins <ian-news@hotmail.com> - 2020-05-01 12:44 +1200
              Re: This statement may fall through - how? mathog <mathog@caltech.edu> - 2020-05-01 10:28 -0700
                Re: This statement may fall through - how? chad <cdalten@gmail.com> - 2020-05-01 12:43 -0700
  Re: This statement may fall through - how? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-29 17:58 -0700
  Re: This statement may fall through - how? scott@slp53.sl.home (Scott Lurndal) - 2020-04-30 01:25 +0000
    Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-29 18:37 -0700
  Re: This statement may fall through - how? Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 10:33 +0200
    Re: This statement may fall through - how? gazelle@shell.xmission.com (Kenny McCormack) - 2020-04-30 08:48 +0000
      Re: This statement may fall through - how? Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 10:54 +0200
        Re: This statement may fall through - how? James Harris <james.harris.1@gmail.com> - 2020-04-30 10:03 +0100
          Re: This statement may fall through - how? Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 11:08 +0200
          Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 02:15 -0700
            Re: This statement may fall through - how? James Harris <james.harris.1@gmail.com> - 2020-04-30 12:27 +0100
              Re: This statement may fall through - how? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-30 13:42 +0100
              Re: This statement may fall through - how? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-30 10:26 -0400
                Re: This statement may fall through - how? James Harris <james.harris.1@gmail.com> - 2020-04-30 17:43 +0100
                Re: This statement may fall through - how? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-30 13:01 -0400
                Re: This statement may fall through - how? Spiros Bousbouras <spibou@gmail.com> - 2020-04-30 17:34 +0000
                Re: This statement may fall through - how? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-04-30 13:53 -0400
                Re: This statement may fall through - how? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-30 13:21 -0700
                Re: This statement may fall through - how? scott@slp53.sl.home (Scott Lurndal) - 2020-04-30 17:54 +0000
                Re: This statement may fall through - how? chad <cdalten@gmail.com> - 2020-04-30 10:55 -0700
              Re: This statement may fall through - how? Richard Damon <Richard@Damon-Family.org> - 2020-04-30 10:30 -0400
        Re: This statement may fall through - how? gazelle@shell.xmission.com (Kenny McCormack) - 2020-04-30 09:10 +0000
          Re: This statement may fall through - how? Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-30 11:18 +0200
            Re: This statement may fall through - how? mathog <mathog@caltech.edu> - 2020-04-30 13:13 -0700

csiph-web