From patchwork Tue Oct 29 08:38:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 99749 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2A64D3858402 for ; Tue, 29 Oct 2024 08:39:14 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id 5EBB63858D34 for ; Tue, 29 Oct 2024 08:38:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5EBB63858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5EBB63858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730191127; cv=none; b=ts5K+nVEw6HLvo1o8hghwGCUtYj3jVc39icZBWiwS8xNA9DUOiVTlml+b3UfKlQVwTMiu8Q3+6ZFAS1aS0EwMWRD68acS5AZToa+Oz1XXR7klCGgyV0P6zqGnkhMxhsQO9vG7OdvJPA0fxnUtFiDD+NVBCgrIPZRTh0DajdMQco= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730191127; c=relaxed/simple; bh=3pXOzAK50ovL7h5b/xy6bR+WTuZuMiMguZdwbMA9V5w=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=nuEBBayvtGITtUNE5zWS6Vmbp46uKE0zaQ31yJyVVpBeO/g9x57Dv7DA9uByXUANow4FWlqQpUAmFRosQDOdLPAA76b54mUzy+TsB/yaMYBKV70G/+Cq29CxmOUXnDxWkR39wEFIP7KUhLe/9wG4bmsLyhX9woRIycuN1FVVfkc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730191119; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=fQebE5dsrFX4Z2/zRr17FjSiw8BA0Jm+FarEXCLcNXQ=; b=IIOBZFOgCKxlCfw/blRW2g0cmvzxzcswx1XDi/cCoRVV+JjvfwuPlxKnxfeMPZmln8pyVC nip3tpaccFgwO8jLNYQFM9epBmQvUyMmp97RZ12ZwMgas9D3iGYospW2JUeN80nY7Q2sQO g04gL0bu/d8/Zyd3eefGjKZ8fP0+sTE= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-510-ckj0z4NGMDG0-lNqguPILg-1; Tue, 29 Oct 2024 04:38:37 -0400 X-MC-Unique: ckj0z4NGMDG0-lNqguPILg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7F4F219560A3 for ; Tue, 29 Oct 2024 08:38:36 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B57691956052; Tue, 29 Oct 2024 08:38:35 +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 49T8cWjT3081505 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 29 Oct 2024 09:38:32 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 49T8cW3X3081504; Tue, 29 Oct 2024 09:38:32 +0100 Date: Tue, 29 Oct 2024 09:38:32 +0100 From: Jakub Jelinek To: "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c-family: Handle RAW_DATA_CST in complete_array_type [PR117313] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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_H3, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! The following testcase ICEs, because add_flexible_array_elts_to_size -> complete_array_type is done only after braced_lists_to_strings which optimizes RAW_DATA_CST surrounded by INTEGER_CST into a larger RAW_DATA_CST covering even the boundaries, while I thought it is done before that. So, RAW_DATA_CST now can be the last constructor_elt in a CONSTRUCTOR and so we need the function to take it into account (handle it as RAW_DATA_CST standing for RAW_DATA_LENGTH consecutive elements). The function wants to support both CONSTRUCTORs without indexes and with them (for non-RAW_DATA_CST elts it was just adding 1 for the current index). So, if the RAW_DATA_CST elt has ce->index, we need to add RAW_DATA_LENGTH (ce->value) - 1, while if it doesn't (and it isn't cnt == 0 case where curindex is 0), add that plus 1, i.e. RAW_DATA_LENGTH (ce->value). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-10-29 Jakub Jelinek PR c/117313 gcc/c-family/ * c-common.cc (complete_array_type): For RAW_DATA_CST elements advance curindex by RAW_DATA_LENGTH or one less than that if ce->index is non-NULL. Handle even the first element if it is RAW_DATA_CST. Formatting fix. gcc/testsuite/ * c-c++-common/init-6.c: New test. Jakub --- gcc/c-family/c-common.cc.jj 2024-10-27 16:39:55.090871381 +0100 +++ gcc/c-family/c-common.cc 2024-10-28 12:30:01.215814079 +0100 @@ -7044,7 +7044,8 @@ complete_array_type (tree *ptype, tree i { int eltsize = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); - maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); + maxindex = size_int (TREE_STRING_LENGTH (initial_value) / eltsize + - 1); } else if (TREE_CODE (initial_value) == CONSTRUCTOR) { @@ -7059,23 +7060,25 @@ complete_array_type (tree *ptype, tree i else { tree curindex; - unsigned HOST_WIDE_INT cnt; + unsigned HOST_WIDE_INT cnt = 1; constructor_elt *ce; bool fold_p = false; if ((*v)[0].index) maxindex = (*v)[0].index, fold_p = true; + if (TREE_CODE ((*v)[0].value) == RAW_DATA_CST) + cnt = 0; curindex = maxindex; - for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) + for (; vec_safe_iterate (v, cnt, &ce); cnt++) { bool curfold_p = false; if (ce->index) curindex = ce->index, curfold_p = true; - else + if (!ce->index || TREE_CODE (ce->value) == RAW_DATA_CST) { - if (fold_p) + if (fold_p || curfold_p) { /* Since we treat size types now as ordinary unsigned types, we need an explicit overflow @@ -7083,9 +7086,17 @@ complete_array_type (tree *ptype, tree i tree orig = curindex; curindex = fold_convert (sizetype, curindex); overflow_p |= tree_int_cst_lt (curindex, orig); + curfold_p = false; } - curindex = size_binop (PLUS_EXPR, curindex, - size_one_node); + if (TREE_CODE (ce->value) == RAW_DATA_CST) + curindex + = size_binop (PLUS_EXPR, curindex, + size_int (RAW_DATA_LENGTH (ce->value) + - ((ce->index || !cnt) + ? 1 : 0))); + else + curindex = size_binop (PLUS_EXPR, curindex, + size_one_node); } if (tree_int_cst_lt (maxindex, curindex)) maxindex = curindex, fold_p = curfold_p; --- gcc/testsuite/c-c++-common/init-6.c.jj 2024-10-28 12:35:59.526803017 +0100 +++ gcc/testsuite/c-c++-common/init-6.c 2024-10-28 12:35:50.394930729 +0100 @@ -0,0 +1,29 @@ +/* PR c/117313 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +struct S { unsigned a; const unsigned char b[]; }; +struct S s = { + 1, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x81, 0xa5, + 0x81, 0xbd, 0x99, 0x81, 0x7e, 0x7e, 0xff, 0xdb, 0xff, 0xc3, 0xe7, + 0xff, 0x7e, 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00, 0x10, + 0x38, 0x00, 0x00, 0x38, 0x6c, 0x6c, 0x38, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x0c, 0xec, 0x6c, 0x3c, + } +}; +struct S t = { + 2, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x81, 0xa5, + 0x81, 0xbd, 0x99, 0x81, 0x7e, 0x7e, 0xff, 0xdb, 0xff, 0xc3, 0xe7, + 0xff, 0x7e, 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00, 0x10, + 0x38, 0x00, 0x00, 0x38, 0x6c, 0x6c, 0x38, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x0c, 0xec, 0x6c, 0x3c, + 0xff, 0x7e, 0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00, 0x10, + 0x38, 0x00, 0x00, 0x38, 0x6c, 0x6c, 0x38, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x0c, 0xec, 0x6c, 0x3c + } +};