From patchwork Fri Aug 25 20:35:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 136940 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp2041712vqm; Fri, 25 Aug 2023 13:35:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLCDVTGYA974sbj5+Mwge+MBFnC95XurxaA5A7pz53/+o/EwvKgbu6gzNOVH5Fb+Pznv7P X-Received: by 2002:a17:907:2ccd:b0:9a1:be5b:f49d with SMTP id hg13-20020a1709072ccd00b009a1be5bf49dmr11722275ejc.7.1692995757768; Fri, 25 Aug 2023 13:35:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692995757; cv=none; d=google.com; s=arc-20160816; b=b8kQrBLk9FH3/pzme9hagwFZPLefplXL8hlJWXoa/tgvNz8BKEo+Kv/mXkYVYB3tJN qBzb7VMcKG16+YLKBIOGIGOMDQsjLwM49rHMKI592e12Q5Wha5mq6WTCwh9vOD+/o6OX URzNrt4Wnusv34INMSJg6NX/fyK/yRXyr3EHP9iOEOffEYkbxp/BVol8+jPv5Y/yo8bd 1BgryyKm+He6REZe7mMSXCwU/CA0dLGZ2v2TChELifTMZSMbcFJr90RPwES1Yakq74/m ra2+7I5vGk5zOZ2KH9L63iJlGkN9Q8941FKln3/5946pRETVTpUvPtUsDB/u3oeJst4v Vh3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=7jqHJfePDjiGNhvfX/SGGGYPlAGzShmm65ke06rQDWE=; fh=1VdxF09yTMShnpx+k1t8IgRTsND/3VsRJ6WE2a60R5U=; b=Y2424KyOGlAuauZD5zSwt2cnuWgda/e3DDUzkZAouM0q9klaatcxlwu1+CSx6t37y1 VTF+OFp00EBEl+wQ1OzsFlyAZCv/YkoN7XFbLhWVBS006XAe075axS3cvFjsvXBpsanh QsAh6NXulka83Q6z5AHagxKadNOTSUVUPY7IU/4cOMCgsCsTuaK5MuJ5R9POzNWe0rlc +G5kL4Diy/FesS2T7VefUtppIMsylt1bm1NRB+FnjpMhGqxdXIVtIzYdVNH4UEHWF2Mu zJUEqPMmRM9XSRebP5R9HrMdiicAp+Qc0fjG8f19MXeQY338X8FMRYu+MkeiYPpe3HX2 arxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=m7A8dC8V; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r5-20020a170906a20500b0098e15543465si1227124ejy.280.2023.08.25.13.35.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 13:35:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=m7A8dC8V; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 70BE93858402 for ; Fri, 25 Aug 2023 20:35:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 70BE93858402 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692995756; bh=7jqHJfePDjiGNhvfX/SGGGYPlAGzShmm65ke06rQDWE=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=m7A8dC8VPGHj5APZZTOPhGOFJRHiQiEpl0JRV0W5naGZIMWino4bFm8Mqbx+ISGdq 89ETsgMjIA19FNQ3wN6DBv+Cvo0wc3dEm4mL72XF326u6EsO43a1KBucuPa8FRSxRG bKvuoIA1j0aoguBXHI1Trl68XFU3T+qwyKiAD7I4= 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 [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id AAA413858D37 for ; Fri, 25 Aug 2023 20:35:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AAA413858D37 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-605-OJAIQOuHMF2EHgmhzczL0A-1; Fri, 25 Aug 2023 16:35:07 -0400 X-MC-Unique: OJAIQOuHMF2EHgmhzczL0A-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 621F6802C1E; Fri, 25 Aug 2023 20:35:07 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.165]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 208D9492C14; Fri, 25 Aug 2023 20:35:06 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37PKZ4GL3075990 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 25 Aug 2023 22:35:04 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37PKZ4J53075989; Fri, 25 Aug 2023 22:35:04 +0200 Date: Fri, 25 Aug 2023 22:35:04 +0200 To: Jason Merrill , Richard Biener , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Implement C++ DR 2406 - [[fallthrough]] attribute and iteration statements Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775234719635461672 X-GMAIL-MSGID: 1775234719635461672 Hi! The following patch implements CWG 2406 - [[fallthrough]] attribute and iteration statements The genericization of some loops leaves nothing at all or just a label after a body of a loop, so if the loop is later followed by case or default label in a switch, the fallthrough statement isn't diagnosed. The following patch implements it by marking the IFN_FALLTHROUGH call in such a case, such that during gimplification it can be pedantically diagnosed even if it is followed by case or default label or some normal labels followed by case/default labels. While looking into this, I've discovered other problems. expand_FALLTHROUGH_r is removing the IFN_FALLTHROUGH calls from the IL, but wasn't telling that to walk_gimple_stmt/walk_gimple_seq_mod, so the callers would then skip the next statement after it, and it would return non-NULL if the removed stmt was last in the sequence. This could lead to wi->callback_result being set even if it didn't appear at the very end of switch sequence. The patch makes use of wi->removed_stmt such that the callers properly know what happened, and use different way to handle the end of switch sequence case. That change discovered a bug in the gimple-walk handling of wi->removed_stmt. If that flag is set, the callback is telling the callers that the current statement has been removed and so the innermost walk_gimple_seq_mod shouldn't gsi_next. The problem is that wi->removed_stmt is only reset at the start of a walk_gimple_stmt, but that can be too late for some cases. If we have two nested gimple sequences, say GIMPLE_BIND as the last stmt of some gimple seq, we remove the last statement inside of that GIMPLE_BIND, set wi->removed_stmt there, don't do gsi_next correctly because already gsi_remove moved us to the next stmt, there is no next stmt, so we return back to the caller, but wi->removed_stmt is still set and so we don't do gsi_next even in the outer sequence, despite the GIMPLE_BIND (etc.) not being removed. That means we walk the GIMPLE_BIND with its whole sequence again. The patch fixes that by resetting wi->removed_stmt after we've used that flag in walk_gimple_seq_mod. Nothing really uses that flag after the outermost walk_gimple_seq_mod, it is just a private notification that the stmt callback has removed a stmt. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-08-25 Jakub Jelinek gcc/ * gimplify.cc (expand_FALLTHROUGH_r): Use wi->removed_stmt after gsi_remove, change the way of passing fallthrough stmt at the end of sequence to expand_FALLTHROUGH. Diagnose IFN_FALLTHROUGH with GF_CALL_NOTHROW flag. (expand_FALLTHROUGH): Change loc into array of 2 location_t elts, don't test wi.callback_result, instead check whether first elt is not UNKNOWN_LOCATION and in that case pedwarn with the second location. * gimple-walk.cc (walk_gimple_seq_mod): Clear wi->removed_stmt after the flag has been used. gcc/c-family/ * c-gimplify.cc (genericize_c_loop): For C++ mark IFN_FALLTHROUGH call at the end of loop body as TREE_NOTHROW. gcc/testsuite/ * g++.dg/DRs/dr2406.C: New test. Jakub --- gcc/gimplify.cc.jj 2023-08-23 11:22:28.115592483 +0200 +++ gcc/gimplify.cc 2023-08-25 13:43:58.711847414 +0200 @@ -2588,17 +2588,33 @@ expand_FALLTHROUGH_r (gimple_stmt_iterat *handled_ops_p = false; break; case GIMPLE_CALL: + static_cast(wi->info)[0] = UNKNOWN_LOCATION; if (gimple_call_internal_p (stmt, IFN_FALLTHROUGH)) { + location_t loc = gimple_location (stmt); gsi_remove (gsi_p, true); + wi->removed_stmt = true; + + /* nothrow flag is added by genericize_c_loop to mark fallthrough + statement at the end of some loop's body. Those should be + always diagnosed, either because they indeed don't precede + a case label or default label, or because the next statement + is not within the same iteration statement. */ + if ((stmt->subcode & GF_CALL_NOTHROW) != 0) + { + pedwarn (loc, 0, "attribute % not preceding " + "a case label or default label"); + break; + } + if (gsi_end_p (*gsi_p)) { - *static_cast(wi->info) = gimple_location (stmt); - return integer_zero_node; + static_cast(wi->info)[0] = BUILTINS_LOCATION; + static_cast(wi->info)[1] = loc; + break; } bool found = false; - location_t loc = gimple_location (stmt); gimple_stmt_iterator gsi2 = *gsi_p; stmt = gsi_stmt (gsi2); @@ -2648,6 +2664,7 @@ expand_FALLTHROUGH_r (gimple_stmt_iterat } break; default: + static_cast(wi->info)[0] = UNKNOWN_LOCATION; break; } return NULL_TREE; @@ -2659,14 +2676,16 @@ static void expand_FALLTHROUGH (gimple_seq *seq_p) { struct walk_stmt_info wi; - location_t loc; + location_t loc[2]; memset (&wi, 0, sizeof (wi)); - wi.info = (void *) &loc; + loc[0] = UNKNOWN_LOCATION; + loc[1] = UNKNOWN_LOCATION; + wi.info = (void *) &loc[0]; walk_gimple_seq_mod (seq_p, expand_FALLTHROUGH_r, NULL, &wi); - if (wi.callback_result == integer_zero_node) + if (loc[0] != UNKNOWN_LOCATION) /* We've found [[fallthrough]]; at the end of a switch, which the C++ standard says is ill-formed; see [dcl.attr.fallthrough]. */ - pedwarn (loc, 0, "attribute % not preceding " + pedwarn (loc[1], 0, "attribute % not preceding " "a case label or default label"); } --- gcc/gimple-walk.cc.jj 2023-01-02 09:32:28.298199849 +0100 +++ gcc/gimple-walk.cc 2023-08-25 14:21:10.264376130 +0200 @@ -56,11 +56,21 @@ walk_gimple_seq_mod (gimple_seq *pseq, w gcc_assert (wi); wi->callback_result = ret; - return wi->removed_stmt ? NULL : gsi_stmt (gsi); + gimple *g; + if (!wi->removed_stmt) + g = gsi_stmt (gsi); + else + { + g = NULL; + wi->removed_stmt = false; + } + return g; } if (!wi->removed_stmt) gsi_next (&gsi); + else + wi->removed_stmt = false; } if (wi) --- gcc/c-family/c-gimplify.cc.jj 2023-07-11 13:40:37.594467535 +0200 +++ gcc/c-family/c-gimplify.cc 2023-08-25 12:38:02.406574469 +0200 @@ -307,6 +307,27 @@ genericize_c_loop (tree *stmt_p, locatio } append_to_statement_list (body, &stmt_list); + if (c_dialect_cxx () + && stmt_list + && TREE_CODE (stmt_list) == STATEMENT_LIST) + { + tree_stmt_iterator tsi = tsi_last (stmt_list); + if (!tsi_end_p (tsi)) + { + tree t = *tsi; + while (TREE_CODE (t) == CLEANUP_POINT_EXPR + || TREE_CODE (t) == EXPR_STMT + || CONVERT_EXPR_CODE_P (TREE_CODE (t))) + t = TREE_OPERAND (t, 0); + /* For C++, if iteration statement body ends with fallthrough + statement, mark it such that we diagnose it even if next + statement would be labeled statement with case/default label. */ + if (TREE_CODE (t) == CALL_EXPR + && !CALL_EXPR_FN (t) + && CALL_EXPR_IFN (t) == IFN_FALLTHROUGH) + TREE_NOTHROW (t) = 1; + } + } finish_bc_block (&stmt_list, bc_continue, clab); if (incr) { --- gcc/testsuite/g++.dg/DRs/dr2406.C.jj 2023-08-25 14:16:53.095670934 +0200 +++ gcc/testsuite/g++.dg/DRs/dr2406.C 2023-08-25 14:16:04.732290555 +0200 @@ -0,0 +1,81 @@ +// DR 2406 - [[fallthrough]] attribute and iteration statements +// { dg-do compile { target c++11 } } +// { dg-options "-pedantic-errors -Wimplicit-fallthrough" } + +void bar (); +void baz (); +void qux (); + +void +foo (int n) +{ + switch (n) + { + case 1: + case 2: + bar (); + [[fallthrough]]; + case 3: + do + { + [[fallthrough]]; // { dg-error "attribute 'fallthrough' not preceding a case label or default label" } + } + while (false); + case 6: + do + { + [[fallthrough]]; // { dg-error "attribute 'fallthrough' not preceding a case label or default label" } + } + while (n--); + case 7: + while (false) + { + [[fallthrough]]; // { dg-error "attribute 'fallthrough' not preceding a case label or default label" } + } + case 5: + baz (); // { dg-warning "this statement may fall through" } + case 4: // { dg-message "here" } + qux (); + [[fallthrough]]; // { dg-error "attribute 'fallthrough' not preceding a case label or default label" } + } +} + +void +corge (int n) +{ + switch (n) + { + case 1: + { + int i = 0; + do + { + [[fallthrough]]; // { dg-error "attribute 'fallthrough' not preceding a case label or default label" } + } + while (false); + } + case 2: + bar (); + break; + default: + break; + } +} + +void +fred (int n) +{ + switch (n) + { + case 1: + { + int i = 0; + [[fallthrough]]; + } + case 2: + bar (); + break; + default: + break; + } +}