From patchwork Thu Oct 17 07:18:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1178340 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=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-511180-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="ytUP1hl6"; 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 46v0rc2MTJz9sNx for ; Thu, 17 Oct 2019 18:18:40 +1100 (AEDT) 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:reply-to:mime-version :content-type; q=dns; s=default; b=TLTgUwD9LTUgqR3Uqwn5fRgiJRGU2 uKBIo2Ciu8DYk91kjptB8q6LksgV8CYahz5erQXQw1U7XYufsytPqpMCeXOwYeiX QwOI+2CFvZyoCy9qbUCvO3xkaPa5r667ridcDpG0VTGNCpv1NTpfZycoQ40NOcPL VFr9K22LIUjk98= 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:reply-to:mime-version :content-type; s=default; bh=o/AnO896CtYR5/JIuFBIGr1yDc0=; b=ytU P1hl6fVPLFHBLxtURzUP4Lsn6sXUKIoT7S2NPL6stBEJLc99Vgf5wDNnQscxlMgM hAWr89PuX6tbfysAKwhVOTqsFSVGSZiFnTAxUN2IajaNRKNurWBTyswQPwSjwN3a 6I9he6k4Vs7dSeT1tUrAVPZdKqgyXTWZk8080NlI= Received: (qmail 115614 invoked by alias); 17 Oct 2019 07:18:33 -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 115606 invoked by uid 89); 17 Oct 2019 07:18:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=admit 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; Thu, 17 Oct 2019 07:18:32 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2EE373082B41 for ; Thu, 17 Oct 2019 07:18:31 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.36.118.135]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C9B6D60BE1; Thu, 17 Oct 2019 07:18:30 +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 x9H7ISH5006635; Thu, 17 Oct 2019 09:18:28 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x9H7IQFv006634; Thu, 17 Oct 2019 09:18:26 +0200 Date: Thu, 17 Oct 2019 09:18:26 +0200 From: Jakub Jelinek To: Jason Merrill , David Malcolm Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix handling of location wrappers in constexpr evaluation (PR c++/92015) Message-ID: <20191017071826.GW2116@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.11.3 (2019-02-01) X-IsSubscribed: yes Hi! As written in the PR, location wrappers are stripped by cxx_eval_constant_expression as the first thing it does after dealing with jump_target. The problem is that when this function is called on a CONSTRUCTOR, is TREE_CONSTANT and reduced_constant_expression_p (which allows location wrappers around constants), we don't recurse on the CONSTRUCTOR elements and so nothing strips them away. Then when cxx_eval_component_reference or bit_field_ref we actually can run into those location wrappers and the callers assume they don't appear anymore. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? I admit I'm not 100% sure about the second case which will call the function to do the stripping for all the elts until the one found. The alternative would be: tree bitpos = bit_position (field); if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) - return value; + { + STRIP_ANY_LOCATION_WRAPPER (value); + return value; + } if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE - && TREE_CODE (value) == INTEGER_CST && tree_fits_shwi_p (bitpos) && tree_fits_shwi_p (DECL_SIZE (field))) { HOST_WIDE_INT bit = tree_to_shwi (bitpos); HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); HOST_WIDE_INT shift; if (bit >= istart && bit + sz <= istart + isize) { + STRIP_ANY_LOCATION_WRAPPER (value); + if (TREE_CODE (value) != INTEGER_CST) + continue; but then we'd do those tree_fits_shwi_p/tree_to_shwi. Guess a micro-optimization without a clear winner. 2019-10-17 Jakub Jelinek PR c++/92015 * constexpr.c (cxx_eval_component_reference, cxx_eval_bit_field_ref): Use STRIP_ANY_LOCATION_WRAPPER on CONSTRUCTOR elts. * g++.dg/cpp0x/constexpr-92015.C: New test. Jakub --- gcc/cp/constexpr.c.jj 2019-10-16 09:30:57.300112739 +0200 +++ gcc/cp/constexpr.c 2019-10-16 17:06:03.943539476 +0200 @@ -2887,7 +2887,10 @@ cxx_eval_component_reference (const cons : field == part) { if (value) - return value; + { + STRIP_ANY_LOCATION_WRAPPER (value); + return value; + } else /* We're in the middle of initializing it. */ break; @@ -2977,6 +2980,7 @@ cxx_eval_bit_field_ref (const constexpr_ FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) { tree bitpos = bit_position (field); + STRIP_ANY_LOCATION_WRAPPER (value); if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) return value; if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE --- gcc/testsuite/g++.dg/cpp0x/constexpr-92015.C.jj 2019-10-16 17:16:44.204871319 +0200 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-92015.C 2019-10-16 17:14:14.773127884 +0200 @@ -0,0 +1,7 @@ +// PR c++/92015 +// { dg-do compile { target c++11 } } + +struct S1 { char c[6] {'h', 'e', 'l', 'l', 'o', 0}; }; +struct S2 { char c[6] = "hello"; }; +static_assert (S1{}.c[0] == 'h', ""); +static_assert (S2{}.c[0] == 'h', "");