From patchwork Mon Aug 6 15:35:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 953971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-483246-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="RUnYeXzM"; dkim-atps=neutral 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 41khZH6Cwnz9rvt for ; Tue, 7 Aug 2018 01:36:07 +1000 (AEST) 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:subject:message-id:mime-version:content-type; q=dns; s= default; b=sdTL+KXpKbOvk8XDKvMmv+tsFCPbyCwovp62tAO2NToJVrWEcg3JF 6M4Obdh4ed5Fx6prGT/loEjm2FUR4YcYlQJLXG9+mU2QD7yvwJTUWKtKoVER7P2i T90yunLEy7DFeBDJGSBk9oQro1EKAcWj/Ela+WLFx6FZ/Y+4jq+iFM= 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:subject:message-id:mime-version:content-type; s= default; bh=9Wukbg11BPEsZGPoFMuCoCcya98=; b=RUnYeXzMu0v8S4P81quw iHVi5wXP7nR0yGIcq0lnk0k+Myr6fcnnLEDMaBhW1ydBRExC7WKcu5r1c8qqjCem iEQjc48uYjhV++9oeHR7ocp3ynUdiDXK1PqbRNOHdSm7buX5Wz/qK8BloNGuQjZ6 9NJnPAERt9tiKFiJxoYBSYs= Received: (qmail 36022 invoked by alias); 6 Aug 2018 15:36:00 -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 36000 invoked by uid 89); 6 Aug 2018 15:35:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*jason, HTo:U*jason, HTo:D*acm.org, evaluated X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 06 Aug 2018 15:35:58 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D9069402243B; Mon, 6 Aug 2018 15:35:56 +0000 (UTC) Received: from redhat.com (ovpn-122-36.rdu2.redhat.com [10.10.122.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 964002166BA0; Mon, 6 Aug 2018 15:35:56 +0000 (UTC) Date: Mon, 6 Aug 2018 11:35:55 -0400 From: Marek Polacek To: GCC Patches , Jason Merrill , Nathan Sidwell Subject: C++ PATCH to fix infinite constexpr evaluation (PR c++/86767) Message-ID: <20180806153555.GD1051@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) This PR points out infinite looping in constexpr evaluation for e.g. for (...) { continue; while (1); } where we tried to evaluate the while despite the continue. The problem started with r240591 and its cxx_eval_statement_list changes. This patch fixes it by handling continue in such a way that we skip stmts that are not meant to be evaluated. Bootstrapped/regtested on x86_64-linux, ok for trunk/8.3? 2018-08-06 Marek Polacek PR c++/86767 * constexpr.c (cxx_eval_statement_list): Handle continue. * g++.dg/cpp1y/constexpr-86767.C: New test. Marek diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c index 365296d6e3b..79039ff2b79 100644 --- gcc/cp/constexpr.c +++ gcc/cp/constexpr.c @@ -3950,6 +3950,16 @@ cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) { tree stmt = tsi_stmt (i); + /* We've found a continue, so skip everything until we reach + the label its jumping to. */ + if (continues (jump_target)) + { + if (label_matches (ctx, jump_target, stmt)) + /* Found it. */ + *jump_target = NULL_TREE; + else + continue; + } if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) continue; r = cxx_eval_constant_expression (ctx, stmt, false, diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-86767.C gcc/testsuite/g++.dg/cpp1y/constexpr-86767.C index e69de29bb2d..2ad71d507d1 100644 --- gcc/testsuite/g++.dg/cpp1y/constexpr-86767.C +++ gcc/testsuite/g++.dg/cpp1y/constexpr-86767.C @@ -0,0 +1,119 @@ +// PR c++/86767 +// { dg-do compile { target c++14 } } + +constexpr int +fn0 () noexcept +{ + int r = 0; + for (int i = 0; i < 10; ++i) + { + continue; + r++; + for (int j = 0; j < 10; ++j ) + { + } + } + return r; +} +static_assert (fn0 () == 0, ""); + +constexpr int +fn1 () noexcept +{ + int r = 0; + for (int i = 0; i < 10; ++i) + for (int j = 0; j < 10; ++j) + { + continue; + r++; + } + return r; +} +static_assert (fn1 () == 0, ""); + +constexpr int +fn2 () noexcept +{ + int r = 0; + for (int i = 0; i < 10; ++i) + { + continue; + r++; + } + return r; +} +static_assert (fn2 () == 0, ""); + +constexpr int +fn3 () noexcept +{ + int r = 0; + for (int i = 0; i < 10; ++i) + { + continue; + r++; + while (1) + { + } + } + return r; +} +static_assert (fn3 () == 0, ""); + +constexpr int +fn4 () noexcept +{ + for (int i = 0; i < 10; ++i) + { + switch (i) + { + case 5: + return i; + default: + continue; + } + while (1) + { + } + } + return 0; +} +static_assert (fn4 () == 5, ""); + +constexpr int +fn5 () noexcept +{ + for (int i = 0; i < 10; ++i) + { + switch (i) + { + case 0: + case 1: + case 2: + case 3: + case 4: + continue; + default: + return i; + } + while (1) + { + } + } + return 0; +} +static_assert (fn5 () == 5, ""); + +constexpr int +fn6 () noexcept +{ + int r = 0; + for (int i = 0; i < 10; ++i) + { + continue; + for (int j = 0; j < 10; ++j ) + r++; + } + return r; +} +static_assert (fn6 () == 0, "");