Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: This statement may fall through - how? Date: Thu, 30 Apr 2020 17:27:59 -0700 Organization: None to speak of Lines: 90 Message-ID: <87pnbo4hyo.fsf@nosuchdomain.example.com> References: <87imhh7pk8.fsf@nosuchdomain.example.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="3a8562cb4f6a88758e42aeb600e70f1e"; logging-data="30565"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lyoLJSoqGkLPIJyhLncHt" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:2AMpXnh+5nvs9O4zmTjSSNnQ4ro= sha1:2pT1tyrk0UVR8sMLpKBWS/bO9Yg= Xref: csiph.com comp.lang.c:151898 mathog 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 > #include > #include > > 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 */