Bug 49859 - gcc could warn about statements between "switch" and first "case"
Summary: gcc could warn about statements between "switch" and first "case"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.5.2
: P3 enhancement
Target Milestone: 7.0
Assignee: Marek Polacek
URL:
Keywords: diagnostic, easyhack
: 51896 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-07-27 01:15 UTC by Wojciech Koszek
Modified: 2016-05-23 15:39 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-07-27 10:07:14


Attachments
Sample source code. (169 bytes, application/octet-stream)
2011-07-27 01:15 UTC, Wojciech Koszek
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Wojciech Koszek 2011-07-27 01:15:10 UTC
Created attachment 24838 [details]
Sample source code.

wkoszek@wkoszek:~/p/switch$ cat switch.c 
#include <stdio.h>

int
main(int argc, char **argv)
{
	int	type;

	type = 123;
	switch (type) {
		printf("SOMETHING\n");
	case 1:
		printf("1\n");
		break;
	case 2:
		printf("2\n");
		break;
	default:
		printf("default\n");
		return 1;
	}
	return 0;
}
wkoszek@wkoszek:~/p/switch$ gcc -pedantic -Wall -Wunused -Wunreachable-code -c switch.c
wkoszek@wkoszek:~/p/switch$ clang -pedantic -Wall -Wunused -Wunreachable-code -c switch.c
switch.c:10:3: warning: will never be executed [-Wunreachable-code]
                printf("SOMETHING\n");
                ^~~~~~~~~~~~~~~~~~~~~
1 warning generated.


wkoszek@wkoszek:~/p/switch$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=i386-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/i386-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/i386-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
Comment 1 Wojciech Koszek 2011-07-27 01:16:25 UTC
I'd expect GCC to warn me about this case, since it's very likely an error.
Comment 2 Andrew Pinski 2011-07-27 01:22:05 UTC
-Wunreachable-code support was removed because it dependent on optimization and caused some "false positives" (well they are not false positives in the sense they were unreachable code but rather the code compiled with some optimization was a truly unreachable but only with inlining and such).

Also does clang warn about Duff's loops?
Comment 3 Wojciech Koszek 2011-07-27 01:28:28 UTC
Looks like GCC and Clang both agree on Duff's stuff:

wkoszek@wkoszek:~/p/duff$ cat duff.c
#include <stdio.h>

void
duff(to, from, count)
register short *to, *from;
register int count;
{
        register int n=(count+7)/8;
        switch(count%8){
        case 0:      do{      *to = *from++;
        case 7:              *to = *from++;
        case 6:              *to = *from++;
        case 5:              *to = *from++;
        case 4:              *to = *from++;
        case 3:              *to = *from++;
        case 2:              *to = *from++;
        case 1:              *to = *from++;
                }while(--n>0);
        }
}

int
main(int argc, char **argv)
{
	short	*dummy = NULL;

	(void)argc;
	(void)argv;

	duff(dummy, dummy, 123);
	return 0;
}
wkoszek@wkoszek:~/p/duff$ gcc -Wall -pedantic -Wunreachable-code -c duff.c
wkoszek@wkoszek:~/p/duff$ clang -Wall -pedantic -Wunreachable-code -c duff.c
Comment 4 Richard Biener 2011-07-27 10:07:14 UTC
Confirmed.
Comment 5 Manuel López-Ibáñez 2014-09-23 15:24:11 UTC
For sure we do not need the old -Wunreachable here. Just when parsing a switch block, anything found before the first case is basically unreachable, no?

(Why the standard does not consider this an error?)
Comment 6 Marek Polacek 2016-01-28 09:37:42 UTC
*** Bug 51896 has been marked as a duplicate of this bug. ***
Comment 7 Marek Polacek 2016-01-28 09:39:01 UTC
Mine for GCC 7.
Comment 8 Marek Polacek 2016-05-12 11:04:45 UTC
C FE patch: <https://gcc.gnu.org/ml/gcc-patches/2016-05/msg00749.html>.
Comment 9 Marek Polacek 2016-05-23 15:37:41 UTC
Author: mpolacek
Date: Mon May 23 15:37:09 2016
New Revision: 236597

URL: https://gcc.gnu.org/viewcvs?rev=236597&root=gcc&view=rev
Log:
	PR c/49859
	* common.opt (Wswitch-unreachable): New option.
	* doc/invoke.texi: Document -Wswitch-unreachable.
	* gimplify.c (gimplify_switch_expr): Implement the -Wswitch-unreachable
	warning.

	* c-c++-common/Wswitch-unreachable-1.c: New test.
	* gcc.dg/Wswitch-unreachable-1.c: New test.
	* c-c++-common/goacc/sb-2.c (void foo): Add dg-warning.
	* g++.dg/cpp0x/lambda/lambda-switch.C (main): Likewise.
	* g++.dg/gomp/block-10.C: Likewise.
	* gcc.dg/gomp/block-10.c: Likewise.
	* g++.dg/gomp/block-9.C: Likewise.
	* gcc.dg/gomp/block-9.c: Likewise.
	* g++.dg/gomp/target-1.C: Likewise.
	* g++.dg/gomp/target-2.C: Likewise.
	* gcc.dg/gomp/target-1.c: Likewise.
	* gcc.dg/gomp/target-2.c: Likewise. 
	* g++.dg/gomp/taskgroup-1.C: Likewise.
	* gcc.dg/gomp/taskgroup-1.c: Likewise.
	* gcc.dg/gomp/teams-1.c: Likewise.
	* g++.dg/gomp/teams-1.C: Likewise.
	* g++.dg/overload/error3.C: Likewise.
	* g++.dg/tm/jump1.C: Likewise.
	* g++.dg/torture/pr40335.C: Likewise.
	* gcc.dg/c99-vla-jump-5.c: Likewise.
	* gcc.dg/switch-warn-1.c: Likewise.
	* gcc.dg/Wjump-misses-init-1.c: Use -Wno-switch-unreachable.
	* gcc.dg/nested-func-1.c: Likewise.
	* gcc.dg/pr67784-4.c: Likewise.

Added:
    trunk/gcc/testsuite/c-c++-common/Wswitch-unreachable-1.c
    trunk/gcc/testsuite/gcc.dg/Wswitch-unreachable-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/common.opt
    trunk/gcc/doc/invoke.texi
    trunk/gcc/gimplify.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/goacc/sb-2.c
    trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-switch.C
    trunk/gcc/testsuite/g++.dg/gomp/block-10.C
    trunk/gcc/testsuite/g++.dg/gomp/block-9.C
    trunk/gcc/testsuite/g++.dg/gomp/target-1.C
    trunk/gcc/testsuite/g++.dg/gomp/target-2.C
    trunk/gcc/testsuite/g++.dg/gomp/taskgroup-1.C
    trunk/gcc/testsuite/g++.dg/gomp/teams-1.C
    trunk/gcc/testsuite/g++.dg/overload/error3.C
    trunk/gcc/testsuite/g++.dg/tm/jump1.C
    trunk/gcc/testsuite/g++.dg/torture/pr40335.C
    trunk/gcc/testsuite/gcc.dg/Wjump-misses-init-1.c
    trunk/gcc/testsuite/gcc.dg/c99-vla-jump-5.c
    trunk/gcc/testsuite/gcc.dg/gomp/block-10.c
    trunk/gcc/testsuite/gcc.dg/gomp/block-9.c
    trunk/gcc/testsuite/gcc.dg/gomp/target-1.c
    trunk/gcc/testsuite/gcc.dg/gomp/target-2.c
    trunk/gcc/testsuite/gcc.dg/gomp/taskgroup-1.c
    trunk/gcc/testsuite/gcc.dg/gomp/teams-1.c
    trunk/gcc/testsuite/gcc.dg/nested-func-1.c
    trunk/gcc/testsuite/gcc.dg/pr67784-4.c
    trunk/gcc/testsuite/gcc.dg/switch-warn-1.c
Comment 10 Marek Polacek 2016-05-23 15:39:10 UTC
Implemented for GCC 7.