From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23638 invoked by alias); 30 Apr 2014 16:10:40 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 23282 invoked by uid 48); 30 Apr 2014 16:10:35 -0000 From: "manu at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/7652] -Wswitch-break : Warn if a switch case falls through Date: Wed, 30 Apr 2014 16:10:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: manu at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-04/txt/msg02286.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D7652 --- Comment #23 from Manuel L=C3=B3pez-Ib=C3=A1=C3=B1ez --- (In reply to Michael Chapman from comment #21) > Created attachment 32716 [details] > Proposed patch >=20 > Patch to enable warnings (-Wswitch-fallthrough) when a switch case falls > through. Enabled by -Wall. Thanks! Patches need to be submitted to gcc-patches@gcc.gnu.org with a Changelog after bootstrapping and regression testing. The patch is missing a testcase for the regression testsuite showing in which cases it should warn= and in which cases it should not. First of all, you need to have a copyright assignment in place with the FSF. This is slightly annoying to do the first time, but you only have to do it once for all GNU projects. See: http://gcc.gnu.org/contribute.html About the patch: +int +c_stmt_ends_with_goto (tree t) This should return 'bool' +{ + if (TREE_CODE (t) =3D=3D GOTO_EXPR) + return TRUE; + if (TREE_CODE (t) =3D=3D BIND_EXPR) + return c_stmt_ends_with_goto (tsi_stmt (tsi_last (BIND_EXPR_BODY (t)))= ); + return FALSE; +} You can use 'true' and 'false' + +/* Handle -Wswitch-fallthrough */ +void=20 +c_do_switch_fallthru_warnings (tree body) +{ + tree_stmt_iterator i; + tree previous_stmt =3D NULL; + tree previous_label =3D NULL; + tree stmts =3D BIND_EXPR_BODY (body); I think it would be worthwhile to add: if (!warn_switch_fallthrough) return; to avoid going through the loop if we are not going to warn anyway. +Wswitch-fallthrough +C ObjC C++ ObjC++ Var(warn_switch_fallthrough) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) +Warn about switch cases which fall through to the next case + This says that the warning is available in C++, but I don't see any code in your patch that calls the new function from the C++ FE. It would be nice to have the same warning in C++. This will allow testing h= ow noisy it is in GCC itself, for instance. But you (or someone else) could do that as a follow-up. >>From gcc-bugs-return-450267-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Wed Apr 30 16:11:49 2014 Return-Path: Delivered-To: listarch-gcc-bugs@gcc.gnu.org Received: (qmail 26929 invoked by alias); 30 Apr 2014 16:11:49 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Delivered-To: mailing list gcc-bugs@gcc.gnu.org Received: (qmail 26847 invoked by uid 48); 30 Apr 2014 16:11:45 -0000 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/7652] -Wswitch-break : Warn if a switch case falls through Date: Wed, 30 Apr 2014 16:11:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-04/txt/msg02287.txt.bz2 Content-length: 385 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652 --- Comment #24 from Jonathan Wakely --- (In reply to Matthew Woehlke from comment #22) > [[gcc:fallthrough]] // suppress warning for fall-through to 'case C' N.B. the attribute-namespace for GNU extensions is "gnu" I agree that the attribute is essential before such warning could be enabled by -Wall