From patchwork Thu May 26 18:44:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 626841 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rFyjp60yyz9t0t for ; Fri, 27 May 2016 04:44:28 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=t3cwNajF; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=N9V3oibXO1hjUveK9eKkwBNT/NITYA0JfUeTZgo3WWW9T6PSkY iZgAGJCXEMZTze6uLphzAFDLHLe6pXsmOEt9WmRzhows5ih15iYKq0kvU/39Iefn HfC1l3v3Z/YHpC82isxq7tDHJbNKIH8B6qbCW8X+SbZw06earQaWYC5Rk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=t/OtacFFPLO/nm6+55sqcyED6c4=; b=t3cwNajFKdEi5ijQyU57 U3cBJOKITMA4CBy+mKPDhgCtEbr5U/JTRK3EBXb+GEaQTHQnXGCCXBi86unc2wnK 79kqFwQwtJIPRRWn+SDBMlLdg3SMaA2AuZdJpRKniSuO13UB8/6ylqLZ20My46o6 kCoSyUBT6fiV2ipLOZl+LQA= Received: (qmail 14196 invoked by alias); 26 May 2016 18:44:21 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 14186 invoked by uid 89); 26 May 2016 18:44:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=user-written, userwritten X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 26 May 2016 18:44:19 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4113280F92; Thu, 26 May 2016 18:44:18 +0000 (UTC) Received: from redhat.com (ovpn-204-26.brq.redhat.com [10.40.204.26]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4QIiDJF030504 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 26 May 2016 14:44:16 -0400 Date: Thu, 26 May 2016 20:44:13 +0200 From: Marek Polacek To: GCC Patches , Jason Merrill , Joseph Myers Cc: Martin Sebor Subject: Further refinement to -Wswitch-unreachable Message-ID: <20160526184413.GD3014@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.6.1 (2016-04-27) Martin complained that -Wswitch-unreachable wouldn't warn on try-blocks, either compiler-generated or user-written. This patch, which looks into GIMPLE_TRY's body, seems to DTRT for both. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2016-05-26 Marek Polacek * gimplify.c (gimplify_switch_expr): Also handle GIMPLE_TRY. * c-c++-common/Wswitch-unreachable-3.c: New test. * g++.dg/warn/Wswitch-unreachable-1.C: New test. Marek diff --git gcc/gimplify.c gcc/gimplify.c index 8316bb8..8b7dddc 100644 --- gcc/gimplify.c +++ gcc/gimplify.c @@ -1609,10 +1609,17 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p) while (gimple_code (seq) == GIMPLE_BIND) seq = gimple_bind_body (as_a (seq)); gimple *stmt = gimple_seq_first_stmt (seq); - enum gimple_code code = gimple_code (stmt); - if (code != GIMPLE_LABEL && code != GIMPLE_TRY) + if (gimple_code (stmt) == GIMPLE_TRY) { - if (code == GIMPLE_GOTO + /* A compiler-generated cleanup or a user-written try block. + Try to get the first statement in its try-block, for better + location. */ + if ((seq = gimple_try_eval (stmt))) + stmt = gimple_seq_first_stmt (seq); + } + if (gimple_code (stmt) != GIMPLE_LABEL) + { + if (gimple_code (stmt) == GIMPLE_GOTO && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL && DECL_ARTIFICIAL (gimple_goto_dest (stmt))) /* Don't warn for compiler-generated gotos. These occur diff --git gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c index e69de29..3748701 100644 --- gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c +++ gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ + +extern void f (int *, int); + +void +g (int i) +{ + switch (i) + { + int a[3]; + __builtin_memset (a, 0, sizeof a); /* { dg-warning "statement will never be executed" } */ + + default: + f (a, 3); + } +} diff --git gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C index e69de29..99d9a83 100644 --- gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C +++ gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C @@ -0,0 +1,34 @@ +// { dg-do compile } + +extern int j; + +void +f (int i) +{ + switch (i) // { dg-warning "statement will never be executed" } + { + try + { + } + catch (...) + { + } + case 1:; + } +} + +void +g (int i) +{ + switch (i) + { + try + { + j = 42; // { dg-warning "statement will never be executed" } + } + catch (...) + { + } + case 1:; + } +}