From patchwork Mon Apr 29 23:06:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1929248 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=jrhhDv9F; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VSzV93QtQz23hd for ; Tue, 30 Apr 2024 09:06:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F9EE3858D37 for ; Mon, 29 Apr 2024 23:06:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [69.48.154.134]) by sourceware.org (Postfix) with ESMTPS id 827AA3858D35 for ; Mon, 29 Apr 2024 23:06:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 827AA3858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 827AA3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714431977; cv=none; b=okHMitrT+n0Dgaj4t70P60DFNnYylGc/lNIYttD10JAbiFpCMHAVQ1iGg6jsylqTEN8QoNImq5du5qHvjR4Zoa0Zq6LrLjHspEXlhVfqMsHMjh4OcABn8S8Afxqc+zp6HsDS/unmI6cQxLNkrFTgyzQvbVk9mesmyMCFyQxWjLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714431977; c=relaxed/simple; bh=TR6CHplTLgLgkN5duJoSsIkY/9kZDX5ZtO2G3Y2T8H4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oAkNCKytpyMGb+S1NuTW5J6OASCeQXRFKYYaQO8u+qM+w/EIlr/8Je1SyBB2a3PqohX9k7487Dcv41nMfBrJC5BJRj6Ufw8I6bB1gMeAh0O3NNNhdMhIjq9Z5iyU+ojkVXIfCYfAXD8ms8/BewWsC0pjTlhR48/5iUF0eH0FIyg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=q2GaCyplM5tPXg8zb9N7lxqKyvI6cCVBrgAsdU9pAj0=; b=jrhhDv9F6+nz1byNR/zq0+84QA MbROHArTdqzVL3k01q2L9OxhjTyOvcx3C7EmN6qJnUnHPbaZTHGnOSaLzovAGfZ0j3Qs8Gt/bDtmG 97NAsfLeegShU4uI8J144Ad7I/9ZlooVTtrxEj1XO81wRyyg4gnZjav9dj3/UwZLtO02VkfgzM5m6 MnefYJEGF2u/1dBVAdmETKSU96ItQ0vs75uhJVybLTqKLf5jj6HANd0GE6ZNKcswY6vUGGGWdxRNf qkcPIY1CAGG1fXSoHYiQJ4MKr+Zw6ALvMJcAYnRNXTWbguVx9uxZBrfU3Vm3bo9Fe275aHXZk++mN 20rn2AjQ==; Received: from [168.86.199.132] (port=55594 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1s1a4d-00BBfh-1e for gcc-patches@gcc.gnu.org; Mon, 29 Apr 2024 19:06:15 -0400 From: "Roger Sayle" To: Subject: [C PATCH] PR c/109618: ICE-after-error from error_mark_node. Date: Tue, 30 Apr 2024 00:06:13 +0100 Message-ID: <010401da9a89$d5a33150$80e993f0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdqaiV6D4XsrlrprSHGOGZDr5xidyA== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This patch solves another ICE-after-error problem in the C family front-ends. Upon a conflicting type redeclaration, the ambiguous type is poisoned with an error_mark_node to indicate to the middle-end that the type is suspect, but care has to be taken by the front-end to avoid passing these malformed trees into the middle-end during error recovery. In this case, a var_decl with a poisoned type appears within a sizeof() expression (wrapped in NOP_EXPR) which causes problems. This revision of the patch tests seen_error() to avoid tree traversal (STRIP_NOPs) in the most common case that an error hasn't occurred. Both this version (and an earlier revision that didn't test seen_error) have survived bootstrap and regression testing on x86_64-pc-linux-gnu. As a consolation, this code also contains a minor performance improvement, by avoiding trying to create (and folding away) a CEIL_DIV_EXPR in the common case that "char" is a single-byte. The current code relies on the middle-end's tree folding to recognize that CEIL_DIV_EXPR of integer_one_node is a no-op, that can be optimized away. Ok for mainline? 2024-04-30 Roger Sayle gcc/c-family/ChangeLog PR c/109618 * c-common.cc (c_sizeof_or_alignof_type): If seen_error() check whether value is (a VAR_DECL) of type error_mark_node, or a NOP_EXPR thereof. Avoid folding CEIL_DIV_EXPR for the common case where char_type is a single byte. gcc/testsuite/ChangeLog PR c/109618 * gcc.dg/pr109618.c: New test case. Thanks in advance, Roger diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 6fa8243..be8ff09 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -3993,10 +3993,31 @@ c_sizeof_or_alignof_type (location_t loc, else { if (is_sizeof) - /* Convert in case a char is more than one unit. */ - value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), - size_int (TYPE_PRECISION (char_type_node) - / BITS_PER_UNIT)); + { + value = TYPE_SIZE_UNIT (type); + + /* PR 109618: Check for erroneous types, stripping NOPs. */ + if (seen_error ()) + { + tree tmp = value; + while (CONVERT_EXPR_P (tmp) + || TREE_CODE (tmp) == NON_LVALUE_EXPR) + { + if (TREE_TYPE (tmp) == error_mark_node) + return error_mark_node; + tmp = TREE_OPERAND (tmp, 0); + } + if (tmp == error_mark_node + || TREE_TYPE (tmp) == error_mark_node) + return error_mark_node; + } + + /* Convert in case a char is more than one unit. */ + if (TYPE_PRECISION (char_type_node) != BITS_PER_UNIT) + value = size_binop_loc (loc, CEIL_DIV_EXPR, value, + size_int (TYPE_PRECISION (char_type_node) + / BITS_PER_UNIT)); + } else if (min_alignof) value = size_int (min_align_of_type (type)); else diff --git a/gcc/testsuite/gcc.dg/pr109618.c b/gcc/testsuite/gcc.dg/pr109618.c new file mode 100644 index 0000000..f240907 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr109618.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O0" } */ +int foo() +{ + const unsigned int var_1 = 2; + + char var_5[var_1]; + + int var_1[10]; /* { dg-error "conflicting type" } */ + + return sizeof(var_5); +} +