From patchwork Sun Sep 20 23:08:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1367994 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=ht/os22X; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bvjt35ZM6z9sPB for ; Mon, 21 Sep 2020 09:08:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC179397241A; Sun, 20 Sep 2020 23:08:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC179397241A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600643302; bh=MNo+WfPovrpVNw0/3kWrmD/CbKGij/8Cb/kBBKI1Nlc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ht/os22XkQ0QvAL9bf9MBkXTQ7JjZ8OTXczMA6TMUVsMowUd8OR97XW1TU7+3t0Cn jutZcP+j1XL08df/M7r27QP1fIsC0zrzGZeFn6WM5nCp2XJFRAQVj4GUxjsiRVqIfi mDmLVUItdLpNXDh33vq/WuaPByEX6Pz85alSTSAI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 3B2713857C61 for ; Sun, 20 Sep 2020 23:08:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3B2713857C61 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-137-9apD7pdjOea9wLCb5-P6Kw-1; Sun, 20 Sep 2020 19:08:16 -0400 X-MC-Unique: 9apD7pdjOea9wLCb5-P6Kw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 07E341868413; Sun, 20 Sep 2020 23:08:15 +0000 (UTC) Received: from pdp-11.redhat.com (ovpn-112-232.rdu2.redhat.com [10.10.112.232]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E2BC5D9CD; Sun, 20 Sep 2020 23:08:14 +0000 (UTC) To: GCC Patches , Joseph Myers Subject: [PATCH] c: Fix -Wduplicated-branches ICE [PR97125] Date: Sun, 20 Sep 2020 19:08:09 -0400 Message-Id: <20200920230809.2415770-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-15.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" We crash here because since r11-3302 the C FE uses codes like SWITCH_STMT in the else branches in the attached test, and inchash::add_expr in do_warn_duplicated_branches doesn't handle these front-end codes. In the C++ FE this works because by the time we get to do_warn_duplicated_branches we've already cp_genericize'd the SWITCH_STMT tree into a SWITCH_EXPR. The fix is to call do_warn_duplicated_branches_r only after loops and other structured control constructs have been lowered. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/c-family/ChangeLog: PR c/97125 * c-gimplify.c (c_genericize): Only call do_warn_duplicated_branches_r after loops and other structured control constructs have been lowered. gcc/testsuite/ChangeLog: PR c/97125 * c-c++-common/Wduplicated-branches-15.c: New test. --- gcc/c-family/c-gimplify.c | 8 ++--- .../c-c++-common/Wduplicated-branches-15.c | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wduplicated-branches-15.c base-commit: 363e7755f227656684c8e284307ceee451503ca4 diff --git a/gcc/c-family/c-gimplify.c b/gcc/c-family/c-gimplify.c index 8b326c99d48..d1e391590dd 100644 --- a/gcc/c-family/c-gimplify.c +++ b/gcc/c-family/c-gimplify.c @@ -533,10 +533,6 @@ c_genericize (tree fndecl) &pset); } - if (warn_duplicated_branches) - walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), - do_warn_duplicated_branches_r, NULL); - /* Genericize loops and other structured control constructs. The C++ front end has already done this in lang-specific code. */ if (!c_dialect_cxx ()) @@ -550,6 +546,10 @@ c_genericize (tree fndecl) pop_cfun (); } + if (warn_duplicated_branches) + walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), + do_warn_duplicated_branches_r, NULL); + /* Dump the C-specific tree IR. */ dump_orig = get_dump_info (TDI_original, &local_dump_flags); if (dump_orig) diff --git a/gcc/testsuite/c-c++-common/Wduplicated-branches-15.c b/gcc/testsuite/c-c++-common/Wduplicated-branches-15.c new file mode 100644 index 00000000000..d4943607086 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wduplicated-branches-15.c @@ -0,0 +1,32 @@ +/* PR c/97125 */ +/* { dg-do compile } */ +/* { dg-options "-Wduplicated-branches" } */ + +void foo (void); + +void +fn1 (void) +{ + if (0) + foo (); + else + switch (0); +} + +void +fn2 (void) +{ + if (0) + foo (); + else + while (0); +} + +void +fn3 (void) +{ + if (0) + foo (); + else + for (;;); +}