From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5673 invoked by alias); 25 Nov 2017 09:01:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 5663 invoked by uid 89); 25 Nov 2017 09:01:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=BAYES_00,KB_WAM_FROM_NAME_SINGLEWORD,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy= 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 ESMTP; Sat, 25 Nov 2017 09:01:36 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F18AEC04BD33; Sat, 25 Nov 2017 09:01:33 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-247.ams2.redhat.com [10.36.116.247]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 729475D6B2; Sat, 25 Nov 2017 09:01:33 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id vAP91QGL023839; Sat, 25 Nov 2017 10:01:31 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id vAP91MDX023838; Sat, 25 Nov 2017 10:01:22 +0100 Date: Sat, 25 Nov 2017 10:16:00 -0000 From: Jakub Jelinek To: Jason Merrill , Nathan Sidwell Cc: gcc-patches@gcc.gnu.org Subject: Re: [C++ PATCH] Avoid -Wreturn-type warnings if a switch has default label, no breaks inside of it, but is followed by a break (PR sanitizer/81275) Message-ID: <20171125090122.GH14653@tucnak> Reply-To: Jakub Jelinek References: <20171124215953.GE14653@tucnak> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171124215953.GE14653@tucnak> User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes X-SW-Source: 2017-11/txt/msg02234.txt.bz2 On Fri, Nov 24, 2017 at 10:59:53PM +0100, Jakub Jelinek wrote: > The testcase below has a useless break; that causes a bogus -Wreturn-type > warning. The C++ FE already has code to avoid adding a BREAK_STMT > after a return or similar sequence that is known not to return. > The following patch extends block_may_fallthrough to also return false > for SWITCH_STMTs that can't fall through. > > Those are ones with non-empty body where the whole body can't fallthrough, > additionally they need to have a default: case label (or cover the whole > range of values, but that is not what this patch can compute, that would > be too big duplication of the gimplification processing) and no BREAK_STMT. > > For the default: case label we need to look in all SWITCH_BODY children > except for nested SWITCH_STMTs, for BREAK_STMTs also not in > {FOR,DO,WHILE}_BODY. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Actually, thinking about it some more, maybe it would be more efficient to gather this information during construction of the SWITCH_STMT in some new flag on the tree, so cxx_block_may_fallthru would just: case SWITCH_STMT: if (SWITCH_STMT_CANT_FALLTHRU_P (stmt) && SWITCH_STMT_BODY (stmt) && !block_may_fallthru (SWITCH_STMT_BODY (stmt))) return false; return true; and /* Set if the body of a switch stmt contains a default: case label and does not contain any break; stmts, thus if SWITCH_STMT_BODY is not empty and doesn't fallthru, then the whole switch stmt can't. */ #define SWITCH_STMT_CANT_FALLTHRU_P(NODE) \ TREE_LANG_FLAG_0 (SWITCH_STMT_CHECK (NODE)) Seems the C++ FE already has switch_stack, so we could just add there a has_default_p, has_break_stmt_p and inside_loop_p flags and both inside of templates and outside in finish_case_label, in finish_break_stmt if actually adding BREAK_STMT and when entering a body of a FOR/DO/WHILE loop tweak those flags. Seems switch_stack is also maintained during pt.c, but we should compute it both during parsing and during pt.c (start with the bit clear on a new SWITCH_STMT). Thoughts on this? Guess for the C FE we should similarly handle SWITCH_EXPR, though a break; in there is a goto outside of the switch, so all we need to note is a default: case label. Jakub